[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/modules/Settings/Vtiger/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  /*
  12   * Settings List View Model Class
  13   */
  14  
  15  class Settings_Vtiger_ListView_Model extends Vtiger_Base_Model {
  16  
  17      /**
  18       * Function to get the Module Model
  19       * @return Vtiger_Module_Model instance
  20       */
  21  	public function getModule() {
  22          return $this->module;
  23      }
  24  
  25  	public function setModule($name) {
  26          $modelClassName = Vtiger_Loader::getComponentClassName('Model', 'Module', $name);
  27          $this->module = new $modelClassName();
  28          return $this;
  29      }
  30  
  31  	public function setModuleFromInstance($module) {
  32          $this->module = $module;
  33          return $this;
  34      }
  35  
  36      /**
  37       * Function to get the list view header
  38       * @return <Array> - List of Vtiger_Field_Model instances
  39       */
  40  	public function getListViewHeaders() {
  41          $module = $this->getModule();
  42          return $module->getListFields();
  43      }
  44      
  45      public function getBasicListQuery() {
  46          $module = $this->getModule();
  47          return 'SELECT * FROM '. $module->getBaseTable();
  48      }
  49  
  50      /**
  51       * Function to get the list view entries
  52       * @param Vtiger_Paging_Model $pagingModel
  53       * @return <Array> - Associative array of record id mapped to Vtiger_Record_Model instance.
  54       */
  55  	public function getListViewEntries($pagingModel) {
  56          $db = PearDatabase::getInstance();
  57  
  58          $module = $this->getModule();
  59          $moduleName = $module->getName();
  60          $parentModuleName = $module->getParentName();
  61          $qualifiedModuleName = $moduleName;
  62          if (!empty($parentModuleName)) {
  63              $qualifiedModuleName = $parentModuleName . ':' . $qualifiedModuleName;
  64          }
  65          $recordModelClass = Vtiger_Loader::getComponentClassName('Model', 'Record', $qualifiedModuleName);
  66          $listQuery = $this->getBasicListQuery();
  67          
  68          $startIndex = $pagingModel->getStartIndex();
  69          $pageLimit = $pagingModel->getPageLimit();
  70  
  71          $orderBy = $this->getForSql('orderby');
  72          if (!empty($orderBy) && $orderBy === 'smownerid') { 
  73              $fieldModel = Vtiger_Field_Model::getInstance('assigned_user_id', $moduleModel); 
  74              if ($fieldModel->getFieldDataType() == 'owner') { 
  75                  $orderBy = 'COALESCE(CONCAT(vtiger_users.first_name,vtiger_users.last_name),vtiger_groups.groupname)'; 
  76              } 
  77          }
  78          if (!empty($orderBy)) {
  79              $listQuery .= ' ORDER BY ' . $orderBy . ' ' . $this->getForSql('sortorder');
  80          }
  81          if($module->isPagingSupported()) {
  82              $nextListQuery = $listQuery.' LIMIT '.($startIndex+$pageLimit).',1';
  83              $listQuery .= " LIMIT $startIndex, $pageLimit";
  84          }
  85  
  86          $listResult = $db->pquery($listQuery, array());
  87          $noOfRecords = $db->num_rows($listResult);
  88  
  89          $listViewRecordModels = array();
  90          for ($i = 0; $i < $noOfRecords; ++$i) {
  91              $row = $db->query_result_rowdata($listResult, $i);
  92              $record = new $recordModelClass();
  93              $record->setData($row);
  94  
  95              if (method_exists($record, 'getModule') && method_exists($record, 'setModule')) {
  96                  $moduleModel = Settings_Vtiger_Module_Model::getInstance($qualifiedModuleName);
  97                  $record->setModule($moduleModel);
  98              }
  99  
 100              $listViewRecordModels[$record->getId()] = $record;
 101          }
 102          if($module->isPagingSupported()) {
 103              $pagingModel->calculatePageRange($listViewRecordModels);
 104              
 105              $nextPageResult = $db->pquery($nextListQuery, array());
 106              $nextPageNumRows = $db->num_rows($nextPageResult);
 107              
 108              if($nextPageNumRows <= 0) {
 109                  $pagingModel->set('nextPageExists', false);
 110              }
 111          }
 112          return $listViewRecordModels;
 113      }
 114      
 115  	public function getListViewLinks() {
 116          $links = array();
 117          $basicLinks = $this->getBasicLinks();
 118          
 119          foreach($basicLinks as $basicLink) {
 120              $links['LISTVIEWBASIC'][] = Vtiger_Link_Model::getInstanceFromValues($basicLink);
 121          }
 122          return $links;
 123      }
 124      
 125      /*
 126       * Function to get Basic links
 127       * @return array of Basic links
 128       */
 129  	public function getBasicLinks(){
 130          $basicLinks = array();
 131          $moduleModel = $this->getModule();
 132          if($moduleModel->hasCreatePermissions())
 133              $basicLinks[] = array(
 134                      'linktype' => 'LISTVIEWBASIC',
 135                      'linklabel' => 'LBL_ADD_RECORD',
 136                      'linkurl' => $moduleModel->getCreateRecordUrl(),
 137                      'linkicon' => ''
 138              );
 139          
 140          return $basicLinks;
 141      }
 142  
 143      /*     * * 
 144       * Function which will get the list view count  
 145       * @return - number of records 
 146       */
 147  
 148  	public function getListViewCount() {
 149          $db = PearDatabase::getInstance();
 150  
 151          $listQuery = $this->getBasicListQuery();
 152  
 153          $position = stripos($listQuery, ' from ');
 154          if ($position) {
 155              $split = spliti(' from ', $listQuery);
 156              $splitCount = count($split);
 157              $listQuery = 'SELECT count(*) AS count ';
 158              for ($i=1; $i<$splitCount; $i++) {
 159                  $listQuery = $listQuery. ' FROM ' .$split[$i];
 160              }
 161          }
 162  
 163          $listResult = $db->pquery($listQuery, array());
 164          return $db->query_result($listResult, 0, 'count');
 165      }
 166  
 167      /**
 168       * Function to get the instance of Settings module model
 169       * @return Settings_Vtiger_Module_Model instance
 170       */
 171  	public static function getInstance($name = 'Settings:Vtiger') {
 172          $modelClassName = Vtiger_Loader::getComponentClassName('Model', 'ListView', $name);
 173          $instance = new $modelClassName();
 174          return $instance->setModule($name);
 175      }
 176  }


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