[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

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


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