[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/modules/PriceBooks/models/ -> ListView.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 PriceBooks_ListView_Model extends Vtiger_ListView_Model {
  12      /*
  13       * Function to give advance links of a module
  14       *    @RETURN array of advanced links
  15      */
  16  	public function getAdvancedLinks() {
  17          return array();
  18      }
  19  
  20      /**
  21       * Function to get the list view entries
  22       * @param Vtiger_Paging_Model $pagingModel
  23       * @return <Array> - Associative array of record id mapped to Vtiger_Record_Model instance.
  24       */
  25  	public function getListViewEntries($pagingModel) {
  26          $db = PearDatabase::getInstance();
  27  
  28          $moduleName = $this->getModule()->get('name');
  29          $moduleFocus = CRMEntity::getInstance($moduleName);
  30          $moduleModel = Vtiger_Module_Model::getInstance($moduleName);
  31  
  32          $queryGenerator = $this->get('query_generator');
  33          $listViewContoller = $this->get('listview_controller');
  34  
  35           $searchParams = $this->get('search_params');
  36          if(empty($searchParams)) {
  37              $searchParams = array();
  38          }
  39          
  40          $glue = "";
  41          if(count($queryGenerator->getWhereFields()) > 0 && (count($searchParams)) > 0) {
  42              $glue = QueryGenerator::$AND;
  43          }
  44          $queryGenerator->parseAdvFilterList($searchParams, $glue);
  45          
  46          $searchKey = $this->get('search_key');
  47          $searchValue = $this->get('search_value');
  48          $operator = $this->get('operator');
  49          if(!empty($searchKey)) {
  50              $queryGenerator->addUserSearchConditions(array('search_field' => $searchKey, 'search_text' => $searchValue, 'operator' => $operator));
  51          }
  52          
  53          
  54  
  55          $orderBy = $this->getForSql('orderby');
  56          $sortOrder = $this->getForSql('sortorder');
  57  
  58          //List view will be displayed on recently created/modified records
  59          if(empty($orderBy) && empty($sortOrder) && $moduleName != "Users"){
  60              $orderBy = 'modifiedtime';
  61              $sortOrder = 'DESC';
  62          }
  63  
  64          if(!empty($orderBy)){
  65              $columnFieldMapping = $moduleModel->getColumnFieldMapping();
  66              $orderByFieldName = $columnFieldMapping[$orderBy];
  67              $orderByFieldModel = $moduleModel->getField($orderByFieldName);
  68              if($orderByFieldModel && ($orderByFieldModel->isReferenceField()
  69                      || $orderByFieldModel->getFieldDataType() == Vtiger_Field_Model::CURRENCY_LIST)){
  70                  //IF it is reference add it in the where fields so that from clause will be having join of the table
  71                  $queryGenerator = $this->get('query_generator');
  72                  $queryGenerator->addWhereField($orderByFieldName);
  73                  //$queryGenerator->whereFields[] = $orderByFieldName;
  74              }
  75          }
  76          
  77          if (!empty($orderBy) && $orderBy === 'smownerid') { 
  78              $fieldModel = Vtiger_Field_Model::getInstance('assigned_user_id', $moduleModel); 
  79              if ($fieldModel->getFieldDataType() == 'owner') { 
  80                  $orderBy = 'COALESCE(CONCAT(vtiger_users.first_name,vtiger_users.last_name),vtiger_groups.groupname)'; 
  81              } 
  82          } 
  83  
  84          $listQuery = $this->getQuery();
  85  
  86          $sourceModule = $this->get('src_module');
  87          $sourceField = $this->get('src_field');
  88          if(!empty($sourceModule)) {
  89              if(method_exists($moduleModel, 'getQueryByModuleField')) {
  90                  $overrideQuery = $moduleModel->getQueryByModuleField($sourceModule, $this->get('src_field'), $this->get('src_record'), $listQuery, $this->get('currency_id'));
  91                  if(!empty($overrideQuery)) {
  92                      $listQuery = $overrideQuery;
  93                  }
  94              }
  95          }
  96  
  97          $startIndex = $pagingModel->getStartIndex();
  98          $pageLimit = $pagingModel->getPageLimit();
  99  
 100          if(!empty($orderBy)) {
 101              if($orderByFieldModel && $orderByFieldModel->isReferenceField()){
 102                  $referenceModules = $orderByFieldModel->getReferenceList();
 103                  $referenceNameFieldOrderBy = array();
 104                  foreach($referenceModules as $referenceModuleName) {
 105                      $referenceModuleModel = Vtiger_Module_Model::getInstance($referenceModuleName);
 106                      $referenceNameFields = $referenceModuleModel->getNameFields();
 107  
 108                      $columnList = array();
 109                      foreach($referenceNameFields as $nameField) {
 110                          $fieldModel = $referenceModuleModel->getField($nameField);
 111                          $columnList[] = $fieldModel->get('table').$orderByFieldModel->getName().'.'.$fieldModel->get('column');
 112                      }
 113                      if(count($columnList) > 1) {
 114                          $referenceNameFieldOrderBy[] = getSqlForNameInDisplayFormat(array('first_name'=>$columnList[0],'last_name'=>$columnList[1]),'Users').' '.$sortOrder;
 115                      } else {
 116                          $referenceNameFieldOrderBy[] = implode('', $columnList).' '.$sortOrder ;
 117                      }
 118                  }
 119                  $listQuery .= ' ORDER BY '. implode(',',$referenceNameFieldOrderBy);
 120  
 121              } else if($orderByFieldModel && $orderByFieldModel->getFieldDataType() == Vtiger_Field_Model::CURRENCY_LIST) {
 122                  $listQuery .= ' ORDER BY ' . $orderByFieldModel->getUITypeModel()->getCurrenyListReferenceFieldName() . ' ' . $sortOrder;
 123              } else {
 124                  $listQuery .= ' ORDER BY '. $orderBy . ' ' .$sortOrder;
 125              }
 126          }
 127  
 128          $viewid = ListViewSession::getCurrentView($moduleName);
 129          if(empty($viewid)){
 130              $viewid = $pagingModel->get('viewid');
 131          }
 132          $_SESSION['lvs'][$moduleName][$viewid]['start'] = $pagingModel->get('page');
 133          ListViewSession::setSessionQuery($moduleName, $listQuery, $viewid);
 134          
 135          //For Pricebooks popup in Products and Services Related list
 136          if($sourceField !== 'productsRelatedList') {
 137              $listQuery .= " LIMIT $startIndex,".($pageLimit+1);
 138          }
 139  
 140          $listResult = $db->pquery($listQuery, array());
 141  
 142          $listViewRecordModels = array();
 143          $listViewEntries =  $listViewContoller->getListViewRecords($moduleFocus,$moduleName, $listResult);
 144  
 145          $pagingModel->calculatePageRange($listViewEntries);
 146  
 147          //To check if next page
 148          if($db->num_rows($listResult) > $pageLimit && $sourceField !== 'productsRelatedList'){
 149              array_pop($listViewEntries);
 150              $pagingModel->set('nextPageExists', true);
 151          } else {
 152              $pagingModel->set('nextPageExists', false);
 153          }
 154  
 155          $index = 0;
 156          foreach($listViewEntries as $recordId => $record) {
 157              $rawData = $db->query_result_rowdata($listResult, $index++);
 158              $record['id'] = $recordId;
 159  
 160              // Pass through the src_record state to dependent model
 161              if ($this->has('src_record')) {
 162                  $rawData['src_record'] = $this->get('src_record');
 163              }
 164  
 165              $listViewRecordModels[$recordId] = $moduleModel->getRecordFromArray($record, $rawData);
 166          }
 167  
 168          return $listViewRecordModels;
 169      }
 170  
 171      /**
 172       * Function to get the list view entries
 173       * @param Vtiger_Paging_Model $pagingModel
 174       * @return <Array> - Associative array of record id mapped to Vtiger_Record_Model instance.
 175       */
 176  	public function getListViewCount() {
 177          $db = PearDatabase::getInstance();
 178  
 179          $queryGenerator = $this->get('query_generator');
 180  
 181          $moduleName = $this->getModule()->get('name');
 182          $moduleModel = Vtiger_Module_Model::getInstance($moduleName);
 183  
 184          $searchParams = $this->get('search_params');
 185          if(empty($searchParams)) {
 186              $searchParams = array();
 187          }
 188          
 189          $glue = "";
 190          if(count($queryGenerator->getWhereFields()) > 0 && (count($searchParams)) > 0) {
 191              $glue = QueryGenerator::$AND;
 192          }
 193          $queryGenerator->parseAdvFilterList($searchParams, $glue);
 194          
 195          $searchKey = $this->get('search_key');
 196          $searchValue = $this->get('search_value');
 197          $operator = $this->get('operator');
 198          if(!empty($searchKey)) {
 199              $queryGenerator->addUserSearchConditions(array('search_field' => $searchKey, 'search_text' => $searchValue, 'operator' => $operator));
 200          }
 201          
 202          
 203  
 204          $listQuery = $this->getQuery();
 205          $sourceModule = $this->get('src_module');
 206          if(!empty($sourceModule)) {
 207              if(method_exists($moduleModel, 'getQueryByModuleField')) {
 208                  $overrideQuery = $moduleModel->getQueryByModuleField($sourceModule, $this->get('src_field'), $this->get('src_record'), $listQuery, $this->get('currency_id'));
 209                  if(!empty($overrideQuery)) {
 210                      $listQuery = $overrideQuery;
 211                  }
 212              }
 213          }
 214          $position = stripos($listQuery, ' from ');
 215          if ($position) {
 216              $split = spliti(' from ', $listQuery);
 217              $splitCount = count($split);
 218              $listQuery = 'SELECT count(*) AS count ';
 219              for ($i=1; $i<$splitCount; $i++) {
 220                  $listQuery = $listQuery. ' FROM ' .$split[$i];
 221              }
 222          }
 223  
 224          if($this->getModule()->get('name') == 'Calendar'){
 225              $listQuery .= ' AND activitytype <> "Emails"';
 226          }
 227          $listResult = $db->pquery($listQuery, array());
 228          return $db->query_result($listResult, 0, 'count');
 229      }
 230  
 231  }


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