[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/modules/Documents/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 Documents_ListView_Model extends Vtiger_ListView_Model {
  12  
  13      /**
  14       * Function to get the list of listview links for the module
  15       * @param <Array> $linkParams
  16       * @return <Array> - Associate array of Link Type to List of Vtiger_Link_Model instances
  17       */
  18  	public function getListViewLinks($linkParams) {
  19          $currentUserModel = Users_Record_Model::getCurrentUserModel();
  20          $moduleModel = $this->getModule();
  21  
  22          $linkTypes = array('LISTVIEWBASIC', 'LISTVIEW', 'LISTVIEWSETTING');
  23          $links = Vtiger_Link_Model::getAllByType($moduleModel->getId(), $linkTypes, $linkParams);
  24  
  25          $createPermission = Users_Privileges_Model::isPermitted($moduleModel->getName(), 'EditView');
  26          if($createPermission) {
  27              $basicLinks = array(
  28                      array(
  29                              'linktype' => 'LISTVIEWBASIC',
  30                              'linklabel' => 'LBL_ADD_RECORD',
  31                              'linkurl' => $moduleModel->getCreateRecordUrl(),
  32                              'linkicon' => ''
  33                      ),
  34                      array(
  35                              'linktype' => 'LISTVIEWBASIC',
  36                              'linklabel' => 'LBL_ADD_FOLDER',
  37                              'linkurl' => 'javascript:Documents_List_Js.triggerAddFolder("'.$moduleModel->getAddFolderUrl().'")',
  38                              'linkicon' => ''
  39                      )
  40              );
  41              foreach($basicLinks as $basicLink) {
  42                  $links['LISTVIEWBASIC'][] = Vtiger_Link_Model::getInstanceFromValues($basicLink);
  43              }
  44          }
  45  
  46          $exportPermission = Users_Privileges_Model::isPermitted($moduleModel->getName(), 'Export');
  47          if($exportPermission) {
  48              $advancedLink = array(
  49                      'linktype' => 'LISTVIEW',
  50                      'linklabel' => 'LBL_EXPORT',
  51                      'linkurl' => 'javascript:Vtiger_List_Js.triggerExportAction("'.$moduleModel->getExportUrl().'")',
  52                      'linkicon' => ''
  53              );
  54              $links['LISTVIEW'][] = Vtiger_Link_Model::getInstanceFromValues($advancedLink);
  55          }
  56  
  57          if($currentUserModel->isAdminUser()) {
  58              $settingsLinks = $this->getSettingLinks();
  59              foreach($settingsLinks as $settingsLink) {
  60                  $links['LISTVIEWSETTING'][] = Vtiger_Link_Model::getInstanceFromValues($settingsLink);
  61              }
  62          }
  63          return $links;
  64      }
  65  
  66      /**
  67       * Function to get the list of Mass actions for the module
  68       * @param <Array> $linkParams
  69       * @return <Array> - Associative array of Link type to List of  Vtiger_Link_Model instances for Mass Actions
  70       */
  71  	public function getListViewMassActions($linkParams) {
  72          $currentUserModel = Users_Privileges_Model::getCurrentUserPrivilegesModel();
  73          $moduleModel = $this->getModule();
  74  
  75          $linkTypes = array('LISTVIEWMASSACTION');
  76          $links = Vtiger_Link_Model::getAllByType($moduleModel->getId(), $linkTypes, $linkParams);
  77  
  78                  //Opensource fix to make documents module mass editable
  79                  if($currentUserModel->hasModuleActionPermission($moduleModel->getId(), 'EditView')) { 
  80                      $massActionLink = array( 
  81                                  'linktype' => 'LISTVIEWMASSACTION', 
  82                                  'linklabel' => 'LBL_EDIT', 
  83                                  'linkurl' => 'javascript:Vtiger_List_Js.triggerMassEdit("index.php?module='.$moduleModel->get('name').'&view=MassActionAjax&mode=showMassEditForm");', 
  84                                  'linkicon' => '' 
  85                          ); 
  86                      $links['LISTVIEWMASSACTION'][] = Vtiger_Link_Model::getInstanceFromValues($massActionLink); 
  87                  } 
  88          
  89          if ($currentUserModel->hasModuleActionPermission($moduleModel->getId(), 'Delete')) {
  90              $massActionLink = array(
  91                  'linktype' => 'LISTVIEWMASSACTION',
  92                  'linklabel' => 'LBL_DELETE',
  93                  'linkurl' => 'javascript:Vtiger_List_Js.massDeleteRecords("index.php?module=' . $moduleModel->getName() . '&action=MassDelete");',
  94                  'linkicon' => ''
  95              );
  96  
  97              $links['LISTVIEWMASSACTION'][] = Vtiger_Link_Model::getInstanceFromValues($massActionLink);
  98          }
  99  
 100          $massActionLink = array(
 101              'linktype' => 'LISTVIEWMASSACTION',
 102              'linklabel' => 'LBL_MOVE',
 103              'linkurl' => 'javascript:Documents_List_Js.massMove("index.php?module=' . $moduleModel->getName() . '&view=MoveDocuments");',
 104              'linkicon' => ''
 105          );
 106  
 107          $links['LISTVIEWMASSACTION'][] = Vtiger_Link_Model::getInstanceFromValues($massActionLink);
 108  
 109          return $links;
 110      }
 111  
 112      /**
 113       * Function to get the list view entries
 114       * @param Vtiger_Paging_Model $pagingModel
 115       * @return <Array> - Associative array of record id mapped to Vtiger_Record_Model instance.
 116       */
 117  	public function getListViewEntries($pagingModel) {
 118  
 119          $db = PearDatabase::getInstance();
 120  
 121          $moduleName = $this->getModule()->get('name');
 122          $moduleFocus = CRMEntity::getInstance($moduleName);
 123          $moduleModel = Vtiger_Module_Model::getInstance($moduleName);
 124  
 125          $queryGenerator = $this->get('query_generator');
 126          $listViewContoller = $this->get('listview_controller');
 127  
 128          $folderKey = $this->get('folder_id');
 129          $folderValue = $this->get('folder_value');
 130          if(!empty($folderValue)) {
 131              $queryGenerator->addCondition($folderKey,$folderValue,'e');
 132          }
 133  
 134          $searchParams = $this->get('search_params');
 135          if(empty($searchParams)) {
 136              $searchParams = array();
 137          }
 138          
 139          $glue = "";
 140          if(count($queryGenerator->getWhereFields()) > 0 && (count($searchParams)) > 0) {
 141              $glue = QueryGenerator::$AND;
 142          }
 143          $queryGenerator->parseAdvFilterList($searchParams, $glue);
 144  
 145          $searchKey = $this->get('search_key');
 146          $searchValue = $this->get('search_value');
 147          $operator = $this->get('operator');
 148          if(!empty($searchKey)) {
 149              $queryGenerator->addUserSearchConditions(array('search_field' => $searchKey, 'search_text' => $searchValue, 'operator' => $operator));
 150          }
 151          
 152          $orderBy = $this->getForSql('orderby');
 153          $sortOrder = $this->getForSql('sortorder');
 154  
 155          //List view will be displayed on recently created/modified records
 156          if(empty($orderBy) && empty($sortOrder) && $moduleName != "Users"){
 157              $orderBy = 'modifiedtime';
 158              $sortOrder = 'DESC';
 159          }
 160  
 161          if(!empty($orderBy)){
 162              $columnFieldMapping = $moduleModel->getColumnFieldMapping();
 163              $orderByFieldName = $columnFieldMapping[$orderBy];
 164              $orderByFieldModel = $moduleModel->getField($orderByFieldName);
 165              if($orderByFieldModel && $orderByFieldModel->getFieldDataType() == Vtiger_Field_Model::REFERENCE_TYPE){
 166                  //IF it is reference add it in the where fields so that from clause will be having join of the table
 167                  $queryGenerator = $this->get('query_generator');
 168                  $queryGenerator->addWhereField($orderByFieldName);
 169                  //$queryGenerator->whereFields[] = $orderByFieldName;
 170              }
 171          }
 172          if (!empty($orderBy) && $orderBy === 'smownerid') { 
 173              $fieldModel = Vtiger_Field_Model::getInstance('assigned_user_id', $moduleModel); 
 174              if ($fieldModel->getFieldDataType() == 'owner') { 
 175                  $orderBy = 'COALESCE(CONCAT(vtiger_users.first_name,vtiger_users.last_name),vtiger_groups.groupname)'; 
 176              } 
 177          } 
 178          $listQuery = $this->getQuery();
 179  
 180          $sourceModule = $this->get('src_module');
 181          if(!empty($sourceModule)) {
 182              if(method_exists($moduleModel, 'getQueryByModuleField')) {
 183                  $overrideQuery = $moduleModel->getQueryByModuleField($sourceModule, $this->get('src_field'), $this->get('src_record'), $listQuery);
 184                  if(!empty($overrideQuery)) {
 185                      $listQuery = $overrideQuery;
 186                  }
 187              }
 188          }
 189  
 190          $startIndex = $pagingModel->getStartIndex();
 191          $pageLimit = $pagingModel->getPageLimit();
 192  
 193          if(!empty($orderBy)) {
 194              if($orderByFieldModel && $orderByFieldModel->isReferenceField()){
 195                  $referenceModules = $orderByFieldModel->getReferenceList();
 196                  $referenceNameFieldOrderBy = array();
 197                  foreach($referenceModules as $referenceModuleName) {
 198                      $referenceModuleModel = Vtiger_Module_Model::getInstance($referenceModuleName);
 199                      $referenceNameFields = $referenceModuleModel->getNameFields();
 200  
 201                      $columnList = array();
 202                      foreach($referenceNameFields as $nameField) {
 203                          $fieldModel = $referenceModuleModel->getField($nameField);
 204                          $columnList[] = $fieldModel->get('table').$orderByFieldModel->getName().'.'.$fieldModel->get('column');
 205                      }
 206                      if(count($columnList) > 1) {
 207                          $referenceNameFieldOrderBy[] = getSqlForNameInDisplayFormat(array('first_name'=>$columnList[0],'last_name'=>$columnList[1]),'Users').' '.$sortOrder;
 208                      } else {
 209                          $referenceNameFieldOrderBy[] = implode('', $columnList).' '.$sortOrder ;
 210                      }
 211                  }
 212                  $listQuery .= ' ORDER BY '. implode(',',$referenceNameFieldOrderBy);
 213              }else{
 214                  $listQuery .= ' ORDER BY '. $orderBy . ' ' .$sortOrder;
 215              }
 216          }
 217  
 218          $viewid = ListViewSession::getCurrentView($moduleName);
 219          if(empty($viewid)){
 220              $viewid = $pagingModel->get('viewid');
 221          }
 222          $_SESSION['lvs'][$moduleName][$viewid]['start'] = $pagingModel->get('page');
 223          ListViewSession::setSessionQuery($moduleName, $listQuery, $viewid);
 224  
 225          $listQuery .= " LIMIT $startIndex,".($pageLimit+1);
 226  
 227          $listResult = $db->pquery($listQuery, array());
 228  
 229          $listViewRecordModels = array();
 230          $listViewEntries =  $listViewContoller->getListViewRecords($moduleFocus,$moduleName, $listResult);
 231  
 232          $pagingModel->calculatePageRange($listViewEntries);
 233  
 234          if($db->num_rows($listResult) > $pageLimit){
 235              array_pop($listViewEntries);
 236              $pagingModel->set('nextPageExists', true);
 237          }else{
 238              $pagingModel->set('nextPageExists', false);
 239          }
 240  
 241          $index = 0;
 242          foreach($listViewEntries as $recordId => $record) {
 243              $rawData = $db->query_result_rowdata($listResult, $index++);
 244              $record['id'] = $recordId;
 245              $listViewRecordModels[$recordId] = $moduleModel->getRecordFromArray($record, $rawData);
 246          }
 247          return $listViewRecordModels;
 248      }
 249  
 250  }


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