[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/modules/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   * Vtiger ListView Model Class
  13   */
  14  class Vtiger_ListView_Model extends Vtiger_Base_Model {
  15  
  16  
  17  
  18      /**
  19       * Function to get the Module Model
  20       * @return Vtiger_Module_Model instance
  21       */
  22  	public function getModule() {
  23          return $this->get('module');
  24      }
  25  
  26      /**
  27       * Function to get the Quick Links for the List view of the module
  28       * @param <Array> $linkParams
  29       * @return <Array> List of Vtiger_Link_Model instances
  30       */
  31  	public function getSideBarLinks($linkParams) {
  32          $linkTypes = array('SIDEBARLINK', 'SIDEBARWIDGET');
  33          $moduleLinks = $this->getModule()->getSideBarLinks($linkParams);
  34  
  35          $listLinkTypes = array('LISTVIEWSIDEBARLINK', 'LISTVIEWSIDEBARWIDGET');
  36          $listLinks = Vtiger_Link_Model::getAllByType($this->getModule()->getId(), $listLinkTypes);
  37  
  38          if($listLinks['LISTVIEWSIDEBARLINK']) {
  39              foreach($listLinks['LISTVIEWSIDEBARLINK'] as $link) {
  40                  $moduleLinks['SIDEBARLINK'][] = $link;
  41              }
  42          }
  43  
  44          if($listLinks['LISTVIEWSIDEBARWIDGET']) {
  45              foreach($listLinks['LISTVIEWSIDEBARWIDGET'] as $link) {
  46                  $moduleLinks['SIDEBARWIDGET'][] = $link;
  47              }
  48          }
  49  
  50          return $moduleLinks;
  51      }
  52  
  53      /**
  54       * Function to get the list of listview links for the module
  55       * @param <Array> $linkParams
  56       * @return <Array> - Associate array of Link Type to List of Vtiger_Link_Model instances
  57       */
  58  	public function getListViewLinks($linkParams) {
  59          $currentUserModel = Users_Record_Model::getCurrentUserModel();
  60          $moduleModel = $this->getModule();
  61  
  62          $linkTypes = array('LISTVIEWBASIC', 'LISTVIEW', 'LISTVIEWSETTING');
  63          $links = Vtiger_Link_Model::getAllByType($moduleModel->getId(), $linkTypes, $linkParams);
  64  
  65          $basicLinks = $this->getBasicLinks();
  66  
  67          foreach($basicLinks as $basicLink) {
  68              $links['LISTVIEWBASIC'][] = Vtiger_Link_Model::getInstanceFromValues($basicLink);
  69          }
  70  
  71          $advancedLinks = $this->getAdvancedLinks();
  72  
  73          foreach($advancedLinks as $advancedLink) {
  74              $links['LISTVIEW'][] = Vtiger_Link_Model::getInstanceFromValues($advancedLink);
  75          }
  76  
  77          if($currentUserModel->isAdminUser()) {
  78  
  79              $settingsLinks = $this->getSettingLinks();
  80              foreach($settingsLinks as $settingsLink) {
  81                  $links['LISTVIEWSETTING'][] = Vtiger_Link_Model::getInstanceFromValues($settingsLink);
  82              }
  83          }
  84  
  85          return $links;
  86      }
  87  
  88      /**
  89       * Function to get the list of Mass actions for the module
  90       * @param <Array> $linkParams
  91       * @return <Array> - Associative array of Link type to List of  Vtiger_Link_Model instances for Mass Actions
  92       */
  93  	public function getListViewMassActions($linkParams) {
  94          $currentUserModel = Users_Privileges_Model::getCurrentUserPrivilegesModel();
  95          $moduleModel = $this->getModule();
  96  
  97          $linkTypes = array('LISTVIEWMASSACTION');
  98          $links = Vtiger_Link_Model::getAllByType($moduleModel->getId(), $linkTypes, $linkParams);
  99  
 100  
 101          $massActionLinks = array();
 102          if($currentUserModel->hasModuleActionPermission($moduleModel->getId(), 'EditView')) {
 103              $massActionLinks[] = array(
 104                  'linktype' => 'LISTVIEWMASSACTION',
 105                  'linklabel' => 'LBL_EDIT',
 106                  'linkurl' => 'javascript:Vtiger_List_Js.triggerMassEdit("index.php?module='.$moduleModel->get('name').'&view=MassActionAjax&mode=showMassEditForm");',
 107                  'linkicon' => ''
 108              );
 109          }
 110          if($currentUserModel->hasModuleActionPermission($moduleModel->getId(), 'Delete')) {
 111              $massActionLinks[] = array(
 112                  'linktype' => 'LISTVIEWMASSACTION',
 113                  'linklabel' => 'LBL_DELETE',
 114                  'linkurl' => 'javascript:Vtiger_List_Js.massDeleteRecords("index.php?module='.$moduleModel->get('name').'&action=MassDelete");',
 115                  'linkicon' => ''
 116              );
 117          }
 118  
 119          $modCommentsModel = Vtiger_Module_Model::getInstance('ModComments');
 120          if($moduleModel->isCommentEnabled() && $modCommentsModel->isPermitted('EditView')) {
 121              $massActionLinks[] = array(
 122                  'linktype' => 'LISTVIEWMASSACTION',
 123                  'linklabel' => 'LBL_ADD_COMMENT',
 124                  'linkurl' => 'index.php?module='.$moduleModel->get('name').'&view=MassActionAjax&mode=showAddCommentForm',
 125                  'linkicon' => ''
 126              );
 127          }
 128  
 129          foreach($massActionLinks as $massActionLink) {
 130              $links['LISTVIEWMASSACTION'][] = Vtiger_Link_Model::getInstanceFromValues($massActionLink);
 131          }
 132  
 133          return $links;
 134      }
 135  
 136      /**
 137       * Function to get the list view header
 138       * @return <Array> - List of Vtiger_Field_Model instances
 139       */
 140  	public function getListViewHeaders() {
 141          $listViewContoller = $this->get('listview_controller');
 142          $module = $this->getModule();
 143          $headerFieldModels = array();
 144          $headerFields = $listViewContoller->getListViewHeaderFields();
 145          foreach($headerFields as $fieldName => $webserviceField) {
 146              if($webserviceField && !in_array($webserviceField->getPresence(), array(0,2))) continue;
 147              $headerFieldModels[$fieldName] = Vtiger_Field_Model::getInstance($fieldName,$module);
 148          }
 149          return $headerFieldModels;
 150      }
 151  
 152      /**
 153       * Function to get the list view entries
 154       * @param Vtiger_Paging_Model $pagingModel
 155       * @return <Array> - Associative array of record id mapped to Vtiger_Record_Model instance.
 156       */
 157  	public function getListViewEntries($pagingModel) {
 158          $db = PearDatabase::getInstance();
 159  
 160          $moduleName = $this->getModule()->get('name');
 161          $moduleFocus = CRMEntity::getInstance($moduleName);
 162          $moduleModel = Vtiger_Module_Model::getInstance($moduleName);
 163  
 164          $queryGenerator = $this->get('query_generator');
 165          $listViewContoller = $this->get('listview_controller');
 166  
 167           $searchParams = $this->get('search_params');
 168          if(empty($searchParams)) {
 169              $searchParams = array();
 170          }
 171          $glue = "";
 172          if(count($queryGenerator->getWhereFields()) > 0 && (count($searchParams)) > 0) {
 173              $glue = QueryGenerator::$AND;
 174          }
 175          $queryGenerator->parseAdvFilterList($searchParams, $glue);
 176  
 177          $searchKey = $this->get('search_key');
 178          $searchValue = $this->get('search_value');
 179          $operator = $this->get('operator');
 180          if(!empty($searchKey)) {
 181              $queryGenerator->addUserSearchConditions(array('search_field' => $searchKey, 'search_text' => $searchValue, 'operator' => $operator));
 182          }
 183  
 184          
 185          $orderBy = $this->getForSql('orderby');
 186          $sortOrder = $this->getForSql('sortorder');
 187  
 188          //List view will be displayed on recently created/modified records
 189          if(empty($orderBy) && empty($sortOrder) && $moduleName != "Users"){
 190              $orderBy = 'modifiedtime';
 191              $sortOrder = 'DESC';
 192          }
 193  
 194          if(!empty($orderBy)){
 195              $columnFieldMapping = $moduleModel->getColumnFieldMapping();
 196              $orderByFieldName = $columnFieldMapping[$orderBy];
 197              $orderByFieldModel = $moduleModel->getField($orderByFieldName);
 198              if($orderByFieldModel && $orderByFieldModel->getFieldDataType() == Vtiger_Field_Model::REFERENCE_TYPE){
 199                  //IF it is reference add it in the where fields so that from clause will be having join of the table
 200                  $queryGenerator = $this->get('query_generator');
 201                  $queryGenerator->addWhereField($orderByFieldName);
 202                  //$queryGenerator->whereFields[] = $orderByFieldName;
 203              }
 204          }
 205          $listQuery = $this->getQuery();
 206  
 207          $sourceModule = $this->get('src_module');
 208          if(!empty($sourceModule)) {
 209              if(method_exists($moduleModel, 'getQueryByModuleField')) {
 210                  $overrideQuery = $moduleModel->getQueryByModuleField($sourceModule, $this->get('src_field'), $this->get('src_record'), $listQuery);
 211                  if(!empty($overrideQuery)) {
 212                      $listQuery = $overrideQuery;
 213                  }
 214              }
 215          }
 216  
 217          $startIndex = $pagingModel->getStartIndex();
 218          $pageLimit = $pagingModel->getPageLimit();
 219  
 220          if(!empty($orderBy)) {
 221              if($orderByFieldModel && $orderByFieldModel->isReferenceField()){
 222                  $referenceModules = $orderByFieldModel->getReferenceList();
 223                  $referenceNameFieldOrderBy = array();
 224                  foreach($referenceModules as $referenceModuleName) {
 225                      $referenceModuleModel = Vtiger_Module_Model::getInstance($referenceModuleName);
 226                      $referenceNameFields = $referenceModuleModel->getNameFields();
 227  
 228                      $columnList = array();
 229                      foreach($referenceNameFields as $nameField) {
 230                          $fieldModel = $referenceModuleModel->getField($nameField);
 231                          $columnList[] = $fieldModel->get('table').$orderByFieldModel->getName().'.'.$fieldModel->get('column');
 232                      }
 233                      if(count($columnList) > 1) {
 234                          $referenceNameFieldOrderBy[] = getSqlForNameInDisplayFormat(array('first_name'=>$columnList[0],'last_name'=>$columnList[1]),'Users', '').' '.$sortOrder;
 235                      } else {
 236                          $referenceNameFieldOrderBy[] = implode('', $columnList).' '.$sortOrder ;
 237                      }
 238                  }
 239                  $listQuery .= ' ORDER BY '. implode(',',$referenceNameFieldOrderBy);
 240              }
 241              else if (!empty($orderBy) && $orderBy === 'smownerid') { 
 242                  $fieldModel = Vtiger_Field_Model::getInstance('assigned_user_id', $moduleModel); 
 243                  if ($fieldModel->getFieldDataType() == 'owner') { 
 244                      $orderBy = 'COALESCE(CONCAT(vtiger_users.first_name,vtiger_users.last_name),vtiger_groups.groupname)'; 
 245                  } 
 246                  $listQuery .= ' ORDER BY '. $orderBy . ' ' .$sortOrder;
 247              }
 248              else{
 249                  $listQuery .= ' ORDER BY '. $orderBy . ' ' .$sortOrder;
 250              }
 251          }
 252  
 253          $viewid = ListViewSession::getCurrentView($moduleName);
 254          if(empty($viewid)) {
 255              $viewid = $pagingModel->get('viewid');
 256          }
 257          $_SESSION['lvs'][$moduleName][$viewid]['start'] = $pagingModel->get('page');
 258  
 259          ListViewSession::setSessionQuery($moduleName, $listQuery, $viewid);
 260  
 261          $listQuery .= " LIMIT $startIndex,".($pageLimit+1);
 262  
 263          $listResult = $db->pquery($listQuery, array());
 264  
 265          $listViewRecordModels = array();
 266          $listViewEntries =  $listViewContoller->getListViewRecords($moduleFocus,$moduleName, $listResult);
 267  
 268          $pagingModel->calculatePageRange($listViewEntries);
 269  
 270          if($db->num_rows($listResult) > $pageLimit){
 271              array_pop($listViewEntries);
 272              $pagingModel->set('nextPageExists', true);
 273          }else{
 274              $pagingModel->set('nextPageExists', false);
 275          }
 276  
 277          $index = 0;
 278          foreach($listViewEntries as $recordId => $record) {
 279              $rawData = $db->query_result_rowdata($listResult, $index++);
 280              $record['id'] = $recordId;
 281              $listViewRecordModels[$recordId] = $moduleModel->getRecordFromArray($record, $rawData);
 282          }
 283          return $listViewRecordModels;
 284      }
 285  
 286      /**
 287       * Function to get the list view entries
 288       * @param Vtiger_Paging_Model $pagingModel
 289       * @return <Array> - Associative array of record id mapped to Vtiger_Record_Model instance.
 290       */
 291  	public function getListViewCount() {
 292          $db = PearDatabase::getInstance();
 293  
 294          $queryGenerator = $this->get('query_generator');
 295  
 296          
 297          $searchParams = $this->get('search_params');
 298          if(empty($searchParams)) {
 299              $searchParams = array();
 300          }
 301          
 302          $glue = "";
 303          if(count($queryGenerator->getWhereFields()) > 0 && (count($searchParams)) > 0) {
 304              $glue = QueryGenerator::$AND;
 305          }
 306          $queryGenerator->parseAdvFilterList($searchParams, $glue);
 307          
 308          $searchKey = $this->get('search_key');
 309          $searchValue = $this->get('search_value');
 310          $operator = $this->get('operator');
 311          if(!empty($searchKey)) {
 312              $queryGenerator->addUserSearchConditions(array('search_field' => $searchKey, 'search_text' => $searchValue, 'operator' => $operator));
 313          }
 314          $moduleName = $this->getModule()->get('name');
 315          $moduleModel = Vtiger_Module_Model::getInstance($moduleName);
 316          
 317          
 318  
 319          $listQuery = $this->getQuery();
 320  
 321  
 322          $sourceModule = $this->get('src_module');
 323          if(!empty($sourceModule)) {
 324              $moduleModel = $this->getModule();
 325              if(method_exists($moduleModel, 'getQueryByModuleField')) {
 326                  $overrideQuery = $moduleModel->getQueryByModuleField($sourceModule, $this->get('src_field'), $this->get('src_record'), $listQuery);
 327                  if(!empty($overrideQuery)) {
 328                      $listQuery = $overrideQuery;
 329                  }
 330              }
 331          }
 332          $position = stripos($listQuery, ' from ');
 333          if ($position) {
 334              $split = spliti(' from ', $listQuery);
 335              $splitCount = count($split);
 336              $listQuery = 'SELECT count(*) AS count ';
 337              for ($i=1; $i<$splitCount; $i++) {
 338                  $listQuery = $listQuery. ' FROM ' .$split[$i];
 339              }
 340          }
 341  
 342          if($this->getModule()->get('name') == 'Calendar'){
 343              $listQuery .= ' AND activitytype <> "Emails"';
 344          }
 345  
 346          $listResult = $db->pquery($listQuery, array());
 347          return $db->query_result($listResult, 0, 'count');
 348      }
 349  
 350  	function getQuery() {
 351          $queryGenerator = $this->get('query_generator');
 352          $listQuery = $queryGenerator->getQuery();
 353          return $listQuery;
 354      }
 355      /**
 356       * Static Function to get the Instance of Vtiger ListView model for a given module and custom view
 357       * @param <String> $moduleName - Module Name
 358       * @param <Number> $viewId - Custom View Id
 359       * @return Vtiger_ListView_Model instance
 360       */
 361  	public static function getInstance($moduleName, $viewId='0') {
 362          $db = PearDatabase::getInstance();
 363          $currentUser = vglobal('current_user');
 364  
 365          $modelClassName = Vtiger_Loader::getComponentClassName('Model', 'ListView', $moduleName);
 366          $instance = new $modelClassName();
 367          $moduleModel = Vtiger_Module_Model::getInstance($moduleName);
 368          $queryGenerator = new QueryGenerator($moduleModel->get('name'), $currentUser);
 369          $customView = new CustomView();
 370          if (!empty($viewId) && $viewId != "0") {
 371              $queryGenerator->initForCustomViewById($viewId);
 372  
 373              //Used to set the viewid into the session which will be used to load the same filter when you refresh the page
 374              $viewId = $customView->getViewId($moduleName);
 375          } else {
 376              $viewId = $customView->getViewId($moduleName);
 377              if(!empty($viewId) && $viewId != 0) {
 378                  $queryGenerator->initForDefaultCustomView();
 379              } else {
 380                  $entityInstance = CRMEntity::getInstance($moduleName);
 381                  $listFields = $entityInstance->list_fields_name;
 382                  $listFields[] = 'id';
 383                  $queryGenerator->setFields($listFields);
 384              }
 385          }
 386          $controller = new ListViewController($db, $currentUser, $queryGenerator);
 387  
 388          return $instance->set('module', $moduleModel)->set('query_generator', $queryGenerator)->set('listview_controller', $controller);
 389      }
 390  
 391      /**
 392       * Static Function to get the Instance of Vtiger ListView model for a given module and custom view
 393       * @param <String> $value - Module Name
 394       * @param <Number> $viewId - Custom View Id
 395       * @return Vtiger_ListView_Model instance
 396       */
 397  	public static function getInstanceForPopup($value) {
 398          $db = PearDatabase::getInstance();
 399          $currentUser = vglobal('current_user');
 400  
 401          $modelClassName = Vtiger_Loader::getComponentClassName('Model', 'ListView', $value);
 402          $instance = new $modelClassName();
 403          $moduleModel = Vtiger_Module_Model::getInstance($value);
 404  
 405          $queryGenerator = new QueryGenerator($moduleModel->get('name'), $currentUser);
 406          
 407          $listFields = $moduleModel->getPopupViewFieldsList();
 408          
 409          $listFields[] = 'id';
 410          $queryGenerator->setFields($listFields);
 411  
 412          $controller = new ListViewController($db, $currentUser, $queryGenerator);
 413  
 414          return $instance->set('module', $moduleModel)->set('query_generator', $queryGenerator)->set('listview_controller', $controller);
 415      }
 416  
 417      /*
 418       * Function to give advance links of a module
 419       *    @RETURN array of advanced links
 420       */
 421  	public function getAdvancedLinks(){
 422          $moduleModel = $this->getModule();
 423          $createPermission = Users_Privileges_Model::isPermitted($moduleModel->getName(), 'EditView');
 424          $advancedLinks = array();
 425          $importPermission = Users_Privileges_Model::isPermitted($moduleModel->getName(), 'Import');
 426          if($importPermission && $createPermission) {
 427              $advancedLinks[] = array(
 428                              'linktype' => 'LISTVIEW',
 429                              'linklabel' => 'LBL_IMPORT',
 430                              'linkurl' => $moduleModel->getImportUrl(),
 431                              'linkicon' => ''
 432              );
 433          }
 434  
 435          $exportPermission = Users_Privileges_Model::isPermitted($moduleModel->getName(), 'Export');
 436          if($exportPermission) {
 437              $advancedLinks[] = array(
 438                      'linktype' => 'LISTVIEW',
 439                      'linklabel' => 'LBL_EXPORT',
 440                      'linkurl' => 'javascript:Vtiger_List_Js.triggerExportAction("'.$this->getModule()->getExportUrl().'")',
 441                      'linkicon' => ''
 442                  );
 443          }
 444  
 445          $duplicatePermission = Users_Privileges_Model::isPermitted($moduleModel->getName(), 'DuplicatesHandling');
 446          if($duplicatePermission) {
 447              $advancedLinks[] = array(
 448                  'linktype' => 'LISTVIEWMASSACTION',
 449                  'linklabel' => 'LBL_FIND_DUPLICATES',
 450                  'linkurl' => 'Javascript:Vtiger_List_Js.showDuplicateSearchForm("index.php?module='.$moduleModel->getName().
 451                                  '&view=MassActionAjax&mode=showDuplicatesSearchForm")',
 452                  'linkicon' => ''
 453              );
 454          }
 455  
 456          return $advancedLinks;
 457      }
 458  
 459      /*
 460       * Function to get Setting links
 461       * @return array of setting links
 462       */
 463  	public function getSettingLinks() {
 464          return $this->getModule()->getSettingLinks();
 465      }
 466  
 467      /*
 468       * Function to get Basic links
 469       * @return array of Basic links
 470       */
 471  	public function getBasicLinks(){
 472          $basicLinks = array();
 473          $moduleModel = $this->getModule();
 474          $createPermission = Users_Privileges_Model::isPermitted($moduleModel->getName(), 'EditView');
 475          if($createPermission) {
 476              $basicLinks[] = array(
 477                      'linktype' => 'LISTVIEWBASIC',
 478                      'linklabel' => 'LBL_ADD_RECORD',
 479                      'linkurl' => $moduleModel->getCreateRecordUrl(),
 480                      'linkicon' => ''
 481              );
 482          }
 483          return $basicLinks;
 484      }
 485  
 486  	public function extendPopupFields($fieldsList) {
 487          $moduleModel = $this->get('module');
 488          $queryGenerator = $this->get('query_generator');
 489  
 490          $listFields = $moduleModel->getPopupViewFieldsList();
 491          
 492          $listFields[] = 'id';
 493          $listFields = array_merge($listFields, $fieldsList);
 494          $queryGenerator->setFields($listFields);
 495          $this->get('query_generator', $queryGenerator);
 496      }
 497  }


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