[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/modules/Vtiger/actions/ -> ExportData.php (source)

   1  <?php
   2  /*+***********************************************************************************
   3   * The contents of this file are subject to the vtiger CRM Public License Version 1.0
   4   * ("License"); You may not use this file except in compliance with the License
   5   * The Original Code is:  vtiger CRM Open Source
   6   * The Initial Developer of the Original Code is vtiger.
   7   * Portions created by vtiger are Copyright (C) vtiger.
   8   * All Rights Reserved.
   9   *************************************************************************************/
  10  
  11  class Vtiger_ExportData_Action extends Vtiger_Mass_Action {
  12  
  13  	function checkPermission(Vtiger_Request $request) {
  14          $moduleName = $request->getModule();
  15          $moduleModel = Vtiger_Module_Model::getInstance($moduleName);
  16  
  17          $currentUserPriviligesModel = Users_Privileges_Model::getCurrentUserPrivilegesModel();
  18          if(!$currentUserPriviligesModel->hasModuleActionPermission($moduleModel->getId(), 'Export')) {
  19              throw new AppException('LBL_PERMISSION_DENIED');
  20          }
  21      }
  22  
  23      /**
  24       * Function is called by the controller
  25       * @param Vtiger_Request $request
  26       */
  27  	function process(Vtiger_Request $request) {
  28          $this->ExportData($request);
  29      }
  30  
  31      private $moduleInstance;
  32      private $focus;
  33  
  34      /**
  35       * Function exports the data based on the mode
  36       * @param Vtiger_Request $request
  37       */
  38  	function ExportData(Vtiger_Request $request) {
  39          $db = PearDatabase::getInstance();
  40          $moduleName = $request->get('source_module');
  41  
  42          $this->moduleInstance = Vtiger_Module_Model::getInstance($moduleName);
  43          $this->moduleFieldInstances = $this->moduleInstance->getFields();
  44          $this->focus = CRMEntity::getInstance($moduleName);
  45  
  46          $query = $this->getExportQuery($request);
  47          $result = $db->pquery($query, array());
  48  
  49          $headers = array();
  50          //Query generator set this when generating the query
  51          if(!empty($this->accessibleFields)) {
  52              $accessiblePresenceValue = array(0,2);
  53              foreach($this->accessibleFields as $fieldName) {
  54                  $fieldModel = $this->moduleFieldInstances[$fieldName];
  55                  // Check added as querygenerator is not checking this for admin users
  56                  $presence = $fieldModel->get('presence');
  57                  if(in_array($presence, $accessiblePresenceValue)) {
  58                      $headers[] = $fieldModel->get('label');
  59                  }
  60              }
  61          } else {
  62              foreach($this->moduleFieldInstances as $field) $headers[] = $field->get('label');
  63          }
  64          $translatedHeaders = array();
  65          foreach($headers as $header) $translatedHeaders[] = vtranslate(html_entity_decode($header, ENT_QUOTES), $moduleName);
  66  
  67          $entries = array();
  68          for($j=0; $j<$db->num_rows($result); $j++) {
  69              $entries[] = $this->sanitizeValues($db->fetchByAssoc($result, $j));
  70          }
  71  
  72          $this->output($request, $translatedHeaders, $entries);
  73      }
  74  
  75      /**
  76       * Function that generates Export Query based on the mode
  77       * @param Vtiger_Request $request
  78       * @return <String> export query
  79       */
  80  	function getExportQuery(Vtiger_Request $request) {
  81          $currentUser = Users_Record_Model::getCurrentUserModel();
  82          $mode = $request->getMode();
  83          $cvId = $request->get('viewname');
  84          $moduleName = $request->get('source_module');
  85  
  86          $queryGenerator = new QueryGenerator($moduleName, $currentUser);
  87          $queryGenerator->initForCustomViewById($cvId);
  88          $fieldInstances = $this->moduleFieldInstances;
  89  
  90          $accessiblePresenceValue = array(0,2);
  91          foreach($fieldInstances as $field) {
  92              // Check added as querygenerator is not checking this for admin users
  93              $presence = $field->get('presence');
  94              if(in_array($presence, $accessiblePresenceValue)) {
  95                  $fields[] = $field->getName();
  96              }
  97          }
  98          $queryGenerator->setFields($fields);
  99          $query = $queryGenerator->getQuery();
 100  
 101          if(in_array($moduleName, getInventoryModules())){
 102              $query = $this->moduleInstance->getExportQuery($this->focus, $query);
 103          }
 104  
 105          $this->accessibleFields = $queryGenerator->getFields();
 106  
 107          switch($mode) {
 108              case 'ExportAllData' :    return $query;
 109                                      break;
 110  
 111              case 'ExportCurrentPage' :    $pagingModel = new Vtiger_Paging_Model();
 112                                          $limit = $pagingModel->getPageLimit();
 113  
 114                                          $currentPage = $request->get('page');
 115                                          if(empty($currentPage)) $currentPage = 1;
 116  
 117                                          $currentPageStart = ($currentPage - 1) * $limit;
 118                                          if ($currentPageStart < 0) $currentPageStart = 0;
 119                                          $query .= ' LIMIT '.$currentPageStart.','.$limit;
 120  
 121                                          return $query;
 122                                          break;
 123  
 124              case 'ExportSelectedRecords' :    $idList = $this->getRecordsListFromRequest($request);
 125                                              $baseTable = $this->moduleInstance->get('basetable');
 126                                              $baseTableColumnId = $this->moduleInstance->get('basetableid');
 127                                              if(!empty($idList)) {
 128                                                  if(!empty($baseTable) && !empty($baseTableColumnId)) {
 129                                                      $idList = implode(',' , $idList);
 130                                                      $query .= ' AND '.$baseTable.'.'.$baseTableColumnId.' IN ('.$idList.')';
 131                                                  }
 132                                              } else {
 133                                                  $query .= ' AND '.$baseTable.'.'.$baseTableColumnId.' NOT IN ('.implode(',',$request->get('excluded_ids')).')';
 134                                              }
 135                                              return $query;
 136                                              break;
 137  
 138  
 139              default :    return $query;
 140                          break;
 141          }
 142      }
 143  
 144      /**
 145       * Function returns the export type - This can be extended to support different file exports
 146       * @param Vtiger_Request $request
 147       * @return <String>
 148       */
 149  	function getExportContentType(Vtiger_Request $request) {
 150          $type = $request->get('export_type');
 151          if(empty($type)) {
 152              return 'text/csv';
 153          }
 154      }
 155  
 156      /**
 157       * Function that create the exported file
 158       * @param Vtiger_Request $request
 159       * @param <Array> $headers - output file header
 160       * @param <Array> $entries - outfput file data
 161       */
 162  	function output($request, $headers, $entries) {
 163          $moduleName = $request->get('source_module');
 164          $fileName = str_replace(' ','_',decode_html(vtranslate($moduleName, $moduleName)));
 165          $exportType = $this->getExportContentType($request);
 166  
 167          header("Content-Disposition:attachment;filename=$fileName.csv");
 168          header("Content-Type:$exportType;charset=UTF-8");
 169          header("Expires: Mon, 31 Dec 2000 00:00:00 GMT" );
 170          header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT" );
 171          header("Cache-Control: post-check=0, pre-check=0", false );
 172  
 173          $header = implode("\", \"", $headers);
 174          $header = "\"" .$header;
 175          $header .= "\"\r\n";
 176          echo $header;
 177  
 178          foreach($entries as $row) {
 179              $line = implode("\",\"",$row);
 180              $line = "\"" .$line;
 181              $line .= "\"\r\n";
 182              echo $line;
 183          }
 184      }
 185  
 186      private $picklistValues;
 187      private $fieldArray;
 188      private $fieldDataTypeCache = array();
 189      /**
 190       * this function takes in an array of values for an user and sanitizes it for export
 191       * @param array $arr - the array of values
 192       */
 193  	function sanitizeValues($arr){
 194          $db = PearDatabase::getInstance();
 195          $currentUser = Users_Record_Model::getCurrentUserModel();
 196          $roleid = $currentUser->get('roleid');
 197          if(empty ($this->fieldArray)){
 198              $this->fieldArray = $this->moduleFieldInstances;
 199              foreach($this->fieldArray as $fieldName => $fieldObj){
 200                  //In database we have same column name in two tables. - inventory modules only
 201                  if($fieldObj->get('table') == 'vtiger_inventoryproductrel' && ($fieldName == 'discount_amount' || $fieldName == 'discount_percent')){
 202                      $fieldName = 'item_'.$fieldName;
 203                      $this->fieldArray[$fieldName] = $fieldObj;
 204                  } else {
 205                      $columnName = $fieldObj->get('column');
 206                      $this->fieldArray[$columnName] = $fieldObj;
 207                  }
 208              }
 209          }
 210          $moduleName = $this->moduleInstance->getName();
 211          foreach($arr as $fieldName=>&$value){
 212              if(isset($this->fieldArray[$fieldName])){
 213                  $fieldInfo = $this->fieldArray[$fieldName];
 214              }else {
 215                  unset($arr[$fieldName]);
 216                  continue;
 217              }
 218              $value = trim(decode_html($value),"\"");
 219              $uitype = $fieldInfo->get('uitype');
 220              $fieldname = $fieldInfo->get('name');
 221  
 222              if(!$this->fieldDataTypeCache[$fieldName]) {
 223                  $this->fieldDataTypeCache[$fieldName] = $fieldInfo->getFieldDataType();
 224              }
 225              $type = $this->fieldDataTypeCache[$fieldName];
 226  
 227              if($fieldname != 'hdnTaxType' && ($uitype == 15 || $uitype == 16 || $uitype == 33)){
 228                  if(empty($this->picklistValues[$fieldname])){
 229                      $this->picklistValues[$fieldname] = $this->fieldArray[$fieldname]->getPicklistValues();
 230                  }
 231                  // If the value being exported is accessible to current user
 232                  // or the picklist is multiselect type.
 233                  if($uitype == 33 || $uitype == 16 || array_key_exists($value,$this->picklistValues[$fieldname])){
 234                      // NOTE: multipicklist (uitype=33) values will be concatenated with |# delim
 235                      $value = trim($value);
 236                  } else {
 237                      $value = '';
 238                  }
 239              } elseif($uitype == 52 || $type == 'owner') {
 240                  $value = Vtiger_Util_Helper::getOwnerName($value);
 241              }elseif($type == 'reference'){
 242                  $value = trim($value);
 243                  if(!empty($value)) {
 244                      $parent_module = getSalesEntityType($value);
 245                      $displayValueArray = getEntityName($parent_module, $value);
 246                      if(!empty($displayValueArray)){
 247                          foreach($displayValueArray as $k=>$v){
 248                              $displayValue = $v;
 249                          }
 250                      }
 251                      if(!empty($parent_module) && !empty($displayValue)){
 252                          $value = $parent_module."::::".$displayValue;
 253                      }else{
 254                          $value = "";
 255                      }
 256                  } else {
 257                      $value = '';
 258                  }
 259              } elseif($uitype == 72 || $uitype == 71) {
 260                  $value = CurrencyField::convertToUserFormat($value, null, true, true);
 261              } elseif($uitype == 7 && $fieldInfo->get('typeofdata') == 'N~O' || $uitype == 9){
 262                  $value = decimalFormat($value);
 263              } else if($type == 'date' || $type == 'datetime'){
 264                  $value = DateTimeField::convertToUserFormat($value);
 265              }
 266              if($moduleName == 'Documents' && $fieldname == 'description'){
 267                  $value = strip_tags($value);
 268                  $value = str_replace('&nbsp;','',$value);
 269                  array_push($new_arr,$value);
 270              }
 271          }
 272          return $arr;
 273      }
 274  }


Generated: Fri Nov 28 20:08:37 2014 Cross-referenced by PHPXref 0.7.1