[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/modules/Calendar/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   * Vtiger ListView Model Class
  13   */
  14  class Calendar_ListView_Model extends Vtiger_ListView_Model {
  15  
  16  
  17  	public function getBasicLinks() {
  18          $basicLinks = array();
  19          $moduleModel = $this->getModule();
  20          $createPermission = Users_Privileges_Model::isPermitted($moduleModel->getName(), 'EditView');
  21          if($createPermission) {
  22              $basicLinks[] = array(
  23                      'linktype' => 'LISTVIEWBASIC',
  24                      'linklabel' => 'LBL_ADD_TASK',
  25                      'linkurl' => $this->getModule()->getCreateTaskRecordUrl(),
  26                      'linkicon' => ''
  27              );
  28  
  29              $basicLinks[] = array(
  30                      'linktype' => 'LISTVIEWBASIC',
  31                      'linklabel' => 'LBL_ADD_EVENT',
  32                      'linkurl' => $this->getModule()->getCreateEventRecordUrl(),
  33                      'linkicon' => ''
  34              );
  35          }
  36          return $basicLinks;
  37      }
  38  
  39  
  40      /*
  41       * Function to give advance links of a module
  42       *    @RETURN array of advanced links
  43       */
  44  	public function getAdvancedLinks(){
  45          $moduleModel = $this->getModule();
  46          $createPermission = Users_Privileges_Model::isPermitted($moduleModel->getName(), 'EditView');
  47          $advancedLinks = array();
  48          $importPermission = Users_Privileges_Model::isPermitted($moduleModel->getName(), 'Import');
  49          if($importPermission && $createPermission) {
  50              $advancedLinks[] = array(
  51                              'linktype' => 'LISTVIEW',
  52                              'linklabel' => 'LBL_IMPORT',
  53                              'linkurl' => 'javascript:Calendar_List_Js.triggerImportAction("'.$moduleModel->getImportUrl().'")',
  54                              'linkicon' => ''
  55              );
  56          }
  57  
  58          $exportPermission = Users_Privileges_Model::isPermitted($moduleModel->getName(), 'Export');
  59          if($exportPermission) {
  60              $advancedLinks[] = array(
  61                      'linktype' => 'LISTVIEW',
  62                      'linklabel' => 'LBL_EXPORT',
  63                      'linkurl' => 'javascript:Calendar_List_Js.triggerExportAction("'.$this->getModule()->getExportUrl().'")',
  64                      'linkicon' => ''
  65                  );
  66          }
  67          return $advancedLinks;
  68      }
  69  
  70      /**
  71       * Function to get query to get List of records in the current page
  72       * @return <String> query
  73       */
  74  	function getQuery() {
  75          $queryGenerator = $this->get('query_generator');
  76          // Added to remove emails from the calendar list
  77          $queryGenerator->addCondition('activitytype','Emails','n','AND');
  78  
  79          $listQuery = $queryGenerator->getQuery();
  80          return $listQuery;
  81      }
  82  
  83  
  84      /**
  85       * Function to get the list of Mass actions for the module
  86       * @param <Array> $linkParams
  87       * @return <Array> - Associative array of Link type to List of  Vtiger_Link_Model instances for Mass Actions
  88       */
  89  	public function getListViewMassActions($linkParams) {
  90          $currentUserModel = Users_Privileges_Model::getCurrentUserPrivilegesModel();
  91          $moduleModel = $this->getModule();
  92  
  93          $linkTypes = array('LISTVIEWMASSACTION');
  94          $links = Vtiger_Link_Model::getAllByType($moduleModel->getId(), $linkTypes, $linkParams);
  95  
  96  
  97          $massActionLinks = array();
  98          if($currentUserModel->hasModuleActionPermission($moduleModel->getId(), 'EditView')) {
  99              $massActionLinks[] = array(
 100                  'linktype' => 'LISTVIEWMASSACTION',
 101                  'linklabel' => 'LBL_CHANGE_OWNER',
 102                  'linkurl' => 'javascript:Calendar_List_Js.triggerMassEdit("index.php?module='.$moduleModel->get('name').'&view=MassActionAjax&mode=showMassEditForm");',
 103                  'linkicon' => ''
 104              );
 105          }
 106          if($currentUserModel->hasModuleActionPermission($moduleModel->getId(), 'Delete')) {
 107              $massActionLinks[] = array(
 108                  'linktype' => 'LISTVIEWMASSACTION',
 109                  'linklabel' => 'LBL_DELETE',
 110                  'linkurl' => 'javascript:Vtiger_List_Js.massDeleteRecords("index.php?module='.$moduleModel->get('name').'&action=MassDelete");',
 111                  'linkicon' => ''
 112              );
 113          }
 114  
 115          foreach($massActionLinks as $massActionLink) {
 116              $links['LISTVIEWMASSACTION'][] = Vtiger_Link_Model::getInstanceFromValues($massActionLink);
 117          }
 118  
 119          return $links;
 120      }
 121      
 122      /**
 123       * Function to get the list view header
 124       * @return <Array> - List of Vtiger_Field_Model instances
 125       */
 126  	public function getListViewHeaders() {
 127          $listViewContoller = $this->get('listview_controller');
 128          $module = $this->getModule();
 129          $moduleName = $module->get('name');
 130          $headerFieldModels = array();
 131          $headerFields = $listViewContoller->getListViewHeaderFields();
 132          foreach($headerFields as $fieldName => $webserviceField) {
 133              if($webserviceField && !in_array($webserviceField->getPresence(), array(0,2))) continue;
 134              $fieldInstance = Vtiger_Field_Model::getInstance($fieldName,$module);
 135              if(!$fieldInstance) {
 136                  if($moduleName == 'Calendar') {
 137                      $eventmodule = Vtiger_Module_Model::getInstance('Events');
 138                      $fieldInstance = Vtiger_Field_Model::getInstance($fieldName,$eventmodule);
 139                  }
 140              }
 141              $headerFieldModels[$fieldName] = $fieldInstance;
 142          }
 143          return $headerFieldModels;
 144      }
 145      
 146      /**
 147       * Function to get the list view entries
 148       * @param Vtiger_Paging_Model $pagingModel
 149       * @return <Array> - Associative array of record id mapped to Vtiger_Record_Model instance.
 150       */
 151  	public function getListViewEntries($pagingModel) {
 152          $db = PearDatabase::getInstance();
 153  
 154          $moduleName = $this->getModule()->get('name');
 155          $moduleFocus = CRMEntity::getInstance($moduleName);
 156          $moduleModel = Vtiger_Module_Model::getInstance($moduleName);
 157          $currentUser = Users_Record_Model::getCurrentUserModel();
 158          
 159          $queryGenerator = $this->get('query_generator');
 160          $listViewContoller = $this->get('listview_controller');
 161          $listViewFields = array('visibility','assigned_user_id');
 162          $queryGenerator->setFields(array_unique(array_merge($queryGenerator->getFields(), $listViewFields)));
 163          
 164          $searchParams = $this->get('search_params');
 165          if(empty($searchParams)) {
 166              $searchParams = array();
 167          }
 168          
 169          $glue = "";
 170          if(count($queryGenerator->getWhereFields()) > 0 && (count($searchParams)) > 0) {
 171              $glue = QueryGenerator::$AND;
 172          }
 173          $queryGenerator->parseAdvFilterList($searchParams, $glue);
 174  
 175          $searchKey = $this->get('search_key');
 176          $searchValue = $this->get('search_value');
 177          $operator = $this->get('operator');
 178          if(!empty($searchKey)) {
 179              $queryGenerator->addUserSearchConditions(array('search_field' => $searchKey, 'search_text' => $searchValue, 'operator' => $operator));
 180          }
 181          
 182          $orderBy = $this->getForSql('orderby');
 183          $sortOrder = $this->getForSql('sortorder');
 184  
 185          //List view will be displayed on recently created/modified records
 186          if(empty($orderBy) && empty($sortOrder) && $moduleName != "Users"){
 187              $orderBy = 'modifiedtime';
 188              $sortOrder = 'DESC';
 189          }
 190  
 191          if(!empty($orderBy)){
 192              $columnFieldMapping = $moduleModel->getColumnFieldMapping();
 193              $orderByFieldName = $columnFieldMapping[$orderBy];
 194              $orderByFieldModel = $moduleModel->getField($orderByFieldName);
 195              if($orderByFieldModel && $orderByFieldModel->getFieldDataType() == Vtiger_Field_Model::REFERENCE_TYPE){
 196                  //IF it is reference add it in the where fields so that from clause will be having join of the table
 197                  $queryGenerator = $this->get('query_generator');
 198                  $queryGenerator->addWhereField($orderByFieldName);
 199                  //$queryGenerator->whereFields[] = $orderByFieldName;
 200              }
 201          }
 202          if (!empty($orderBy) && $orderBy === 'smownerid') { 
 203              $fieldModel = Vtiger_Field_Model::getInstance('assigned_user_id', $moduleModel); 
 204              if ($fieldModel->getFieldDataType() == 'owner') { 
 205                  $orderBy = 'COALESCE(CONCAT(vtiger_users.first_name,vtiger_users.last_name),vtiger_groups.groupname)'; 
 206              } 
 207          }
 208          //To combine date and time fields for sorting
 209          if($orderBy == 'date_start') {
 210              $orderBy = "str_to_date(concat(date_start,time_start),'%Y-%m-%d %H:%i:%s')";
 211          }else if($orderBy == 'due_date') {
 212              $orderBy = "str_to_date(concat(due_date,time_end),'%Y-%m-%d %H:%i:%s')";
 213          }
 214  
 215          $listQuery = $this->getQuery();
 216          
 217          $sourceModule = $this->get('src_module');
 218          if(!empty($sourceModule)) {
 219              if(method_exists($moduleModel, 'getQueryByModuleField')) {
 220                  $overrideQuery = $moduleModel->getQueryByModuleField($sourceModule, $this->get('src_field'), $this->get('src_record'), $listQuery);
 221                  if(!empty($overrideQuery)) {
 222                      $listQuery = $overrideQuery;
 223                  }
 224              }
 225          }
 226  
 227          $startIndex = $pagingModel->getStartIndex();
 228          $pageLimit = $pagingModel->getPageLimit();
 229  
 230  
 231  
 232          if(!empty($orderBy)) {
 233              if($orderByFieldModel && $orderByFieldModel->isReferenceField()){
 234                  $referenceModules = $orderByFieldModel->getReferenceList();
 235                  $referenceNameFieldOrderBy = array();
 236                  foreach($referenceModules as $referenceModuleName) {
 237                      $referenceModuleModel = Vtiger_Module_Model::getInstance($referenceModuleName);
 238                      $referenceNameFields = $referenceModuleModel->getNameFields();
 239  
 240                      $columnList = array();
 241                      foreach($referenceNameFields as $nameField) {
 242                          $fieldModel = $referenceModuleModel->getField($nameField);
 243                          $columnList[] = $fieldModel->get('table').$orderByFieldModel->getName().'.'.$fieldModel->get('column');
 244                      }
 245                      if(count($columnList) > 1) {
 246                          $referenceNameFieldOrderBy[] = getSqlForNameInDisplayFormat(array('first_name'=>$columnList[0],'last_name'=>$columnList[1]),'Users').' '.$sortOrder;
 247                      } else {
 248                          $referenceNameFieldOrderBy[] = implode('', $columnList).' '.$sortOrder ;
 249                      }
 250                  }
 251                  $listQuery .= ' ORDER BY '. implode(',',$referenceNameFieldOrderBy);
 252              }else{
 253                  $listQuery .= ' ORDER BY '. $orderBy . ' ' .$sortOrder;
 254              }
 255          }
 256  
 257          $viewid = ListViewSession::getCurrentView($moduleName);
 258          if(empty($viewid)){
 259              $viewid = $pagingModel->get('viewid');
 260          }
 261          $_SESSION['lvs'][$moduleName][$viewid]['start'] = $pagingModel->get('page');
 262          ListViewSession::setSessionQuery($moduleName, $listQuery, $viewid);
 263  
 264          $listQueryWithNoLimit = $listQuery;
 265          $listQuery .= " LIMIT $startIndex,".($pageLimit+1);
 266  
 267          $listResult = $db->pquery($listQuery, array());
 268  
 269          $listViewRecordModels = array();
 270          $listViewEntries =  $listViewContoller->getListViewRecords($moduleFocus,$moduleName, $listResult);
 271  
 272          $pagingModel->calculatePageRange($listViewEntries);
 273  
 274          if($db->num_rows($listResult) > $pageLimit){
 275              array_pop($listViewEntries);
 276              $pagingModel->set('nextPageExists', true);
 277          }else{
 278              $pagingModel->set('nextPageExists', false);
 279          }
 280          
 281          $groupsIds = Vtiger_Util_Helper::getGroupsIdsForUsers($currentUser->getId());
 282          $index = 0;
 283          foreach($listViewEntries as $recordId => $record) {
 284              $rawData = $db->query_result_rowdata($listResult, $index++);
 285              $visibleFields = array('activitytype','date_start','due_date','assigned_user_id','visibility','smownerid');
 286              $ownerId = $rawData['smownerid'];
 287              $visibility = true;
 288              if(in_array($ownerId, $groupsIds)) {
 289                  $visibility = false;
 290              } else if($ownerId == $currentUser->getId()){
 291                  $visibility = false;
 292              }
 293              
 294              if(!$currentUser->isAdminUser() && $rawData['activitytype'] != 'Task' && $rawData['visibility'] == 'Private' && $ownerId && $visibility) {
 295                  foreach($record as $data => $value) {
 296                      if(in_array($data, $visibleFields) != -1) {
 297                          unset($rawData[$data]);
 298                          unset($record[$data]);
 299                      }
 300                  }
 301                  $record['subject'] = vtranslate('Busy','Events').'*';
 302              }
 303              if($record['activitytype'] == 'Task') {
 304                  unset($record['visibility']);
 305                  unset($rawData['visibility']);
 306              }
 307              
 308              $record['id'] = $recordId;
 309              $listViewRecordModels[$recordId] = $moduleModel->getRecordFromArray($record, $rawData);
 310          }
 311          return $listViewRecordModels;
 312      }
 313  }


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