[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/modules/Reports/models/ -> Folder.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 Reports_Folder_Model extends Vtiger_Base_Model {
  12  
  13      /**

  14       * Function to get the id of the folder

  15       * @return <Number>

  16       */
  17  	function getId() {
  18          return $this->get('folderid');
  19      }
  20  
  21      /**

  22       * Function to set the if for the folder

  23       * @param <Number>

  24       */
  25  	function setId($value) {
  26          $this->set('folderid', $value);
  27      }
  28  
  29      /**

  30       * Function to get the name of the folder

  31       * @return <String>

  32       */
  33  	function getName() {
  34          return $this->get('foldername');
  35      }
  36      
  37      /**

  38       * Function returns the instance of Folder model

  39       * @return <Reports_Folder_Model>

  40       */
  41  	public static function getInstance() {
  42          return new self();
  43      }
  44  
  45      /**

  46       * Function saves the folder

  47       */
  48  	function save() {
  49          $db = PearDatabase::getInstance();
  50  
  51          $folderId = $this->getId();
  52          if(!empty($folderId)) {
  53              $db->pquery('UPDATE vtiger_reportfolder SET foldername = ?, description = ? WHERE folderid = ?',
  54                      array($this->getName(), $this->getDescription(), $folderId));
  55          } else {
  56              $result = $db->pquery('SELECT MAX(folderid) AS folderid FROM vtiger_reportfolder', array());
  57              $folderId = (int) ($db->query_result($result, 0, 'folderid')) + 1;
  58  
  59              $db->pquery('INSERT INTO vtiger_reportfolder(folderid, foldername, description, state) VALUES(?, ?, ?, ?)', array($folderId, $this->getName(), $this->getDescription(), 'CUSTOMIZED'));
  60              $this->set('folderid', $folderId);
  61          }
  62      }
  63  
  64      /**

  65       * Function deletes the folder

  66       */
  67  	function delete() {
  68          $db = PearDatabase::getInstance();
  69          $db->pquery('DELETE FROM vtiger_reportfolder WHERE folderid = ?', array($this->getId()));
  70      }
  71  
  72      /**

  73       * Function returns Report Models for the folder

  74       * @param <Vtiger_Paging_Model> $pagingModel

  75       * @return <Reports_Record_Model>

  76       */
  77  	function getReports($pagingModel) {
  78          $paramsList = array(
  79                          'startIndex'=>$pagingModel->getStartIndex(),
  80                          'pageLimit'=>$pagingModel->getPageLimit(),
  81                          'orderBy'=>$this->get('orderby'),
  82                          'sortBy'=>$this->get('sortby'));
  83  
  84          $reportClassInstance = Vtiger_Module_Model::getClassInstance('Reports');
  85          
  86          $fldrId = $this->getId ();
  87          if($fldrId == 'All') {
  88              $fldrId = false;
  89              $paramsList = array( 'startIndex'=>$pagingModel->getStartIndex(),
  90                                   'pageLimit'=>$pagingModel->getPageLimit(),
  91                                   'orderBy'=>$this->get('orderby'),
  92                                   'sortBy'=>$this->get('sortby')
  93                              );
  94          }
  95          
  96          $reportsList = $reportClassInstance->sgetRptsforFldr($fldrId, $paramsList);
  97          if(!$fldrId){
  98              foreach ($reportsList as $reportId => $reports) {
  99                  $reportsCount += count($reports);
 100              }
 101          }else{
 102              $reportsCount = count($reportsList);
 103          }
 104          
 105          $pageLimit = $pagingModel->getPageLimit();
 106          if($reportsCount > $pageLimit){
 107              if(!$fldrId){
 108                  $lastKey = end(array_keys($reportsList));
 109                  array_pop($reportsList[$lastKey]);
 110              }else{
 111                  array_pop($reportsList);
 112              }
 113              $pagingModel->set('nextPageExists', true);
 114          }else{
 115              $pagingModel->set('nextPageExists', false);
 116          }
 117          
 118          $reportModuleModel = Vtiger_Module_Model::getInstance('Reports');
 119          
 120          if($fldrId == false) {
 121              return $this->getAllReportModels($reportsList, $reportModuleModel);
 122          } else {
 123              $reportModels = array();
 124              for($i=0; $i < count($reportsList); $i++) {
 125                  $reportModel = new Reports_Record_Model();
 126  
 127                  $reportModel->setData($reportsList[$i])->setModuleFromInstance($reportModuleModel);
 128                  $reportModels[] = $reportModel;
 129                  unset($reportModel);
 130              }
 131              return $reportModels;
 132          }
 133      }
 134  
 135      /**

 136       * Function to get the description of the folder

 137       * @return <String>

 138       */
 139  	function getDescription() {
 140          return $this->get('description');
 141      }
 142  
 143      /**

 144       * Function to get the url for edit folder from list view of the module

 145       * @return <string> - url

 146       */
 147  	function getEditUrl() {
 148          return 'index.php?module=Reports&view=EditFolder&folderid='.$this->getId();
 149      }
 150  
 151      /**

 152       * Function to get the url for delete folder from list view of the module

 153       * @return <string> - url

 154       */
 155  	function getDeleteUrl() {
 156          return 'index.php?module=Reports&action=Folder&mode=delete&folderid='.$this->getId();
 157      }
 158  
 159      /**

 160       * Function returns the instance of Folder model

 161       * @param FolderId

 162       * @return <Reports_Folder_Model>

 163       */
 164  	public static function getInstanceById($folderId) {
 165          $folderModel = Vtiger_Cache::get('reportsFolder',$folderId);
 166          if(!$folderModel){
 167              $db = PearDatabase::getInstance();
 168              $folderModel = Reports_Folder_Model::getInstance();
 169  
 170              $result = $db->pquery("SELECT * FROM vtiger_reportfolder WHERE folderid = ?", array($folderId));
 171  
 172              if ($db->num_rows($result) > 0) {
 173                  $values = $db->query_result_rowdata($result, 0);
 174                  $folderModel->setData($values);
 175              }
 176              Vtiger_Cache::set('reportsFolder',$folderId,$folderModel);
 177          }
 178          return $folderModel;
 179      }
 180      
 181      /**

 182       * Function returns the instance of Folder model

 183       * @return <Reports_Folder_Model>

 184       */
 185      public static function getAll() {
 186          $db = PearDatabase::getInstance();
 187          $folders = Vtiger_Cache::get('reports', 'folders');
 188          if (!$folders) {
 189              $folders = array();
 190              $result = $db->pquery("SELECT * FROM vtiger_reportfolder ORDER BY foldername ASC", array());
 191              $noOfFolders = $db->num_rows($result);
 192              if ($noOfFolders > 0) {
 193                  for ($i = 0; $i < $noOfFolders; $i++) {
 194                      $folderModel = Reports_Folder_Model::getInstance();
 195                      $values = $db->query_result_rowdata($result, $i);
 196                      $folders[$values['folderid']] = $folderModel->setData($values);
 197                      Vtiger_Cache::set('reportsFolder',$values['folderid'],$folderModel);
 198                  }
 199              }
 200              Vtiger_Cache::set('reports','folders',$folders);
 201          }
 202          return $folders;
 203      }
 204  
 205      /**

 206       * Function returns duplicate record status of the module

 207       * @return true if duplicate records exists else false

 208       */
 209  	function checkDuplicate() {
 210          $db = PearDatabase::getInstance();
 211  
 212          $query = 'SELECT 1 FROM vtiger_reportfolder WHERE foldername = ?';
 213          $params = array($this->getName());
 214  
 215          $folderId = $this->getId();
 216          if ($folderId) {
 217              $query .= ' AND folderid != ?';
 218              array_push($params, $folderId);
 219          }
 220  
 221          $result = $db->pquery($query, $params);
 222  
 223          if ($db->num_rows($result) > 0) {
 224              return true;
 225          }
 226          return false;
 227      }
 228  
 229      /**

 230       * Function returns whether reports are exist or not in this folder

 231       * @return true if exists else false

 232       */
 233  	function hasReports() {
 234          $db = PearDatabase::getInstance();
 235  
 236          $result = $db->pquery('SELECT 1 FROM vtiger_report WHERE folderid = ?', array($this->getId()));
 237  
 238          if ($db->num_rows($result) > 0) {
 239              return true;
 240          }
 241          return false;
 242      }
 243  
 244      /**

 245       * Function returns whether folder is Default or not

 246       * @return true if it is read only else false

 247       */
 248  	function isDefault() {
 249          if ($this->get('state') == 'SAVED') {
 250              return true;
 251          }
 252          return false;
 253      }
 254      
 255      /**

 256       * Function to get info array while saving a folder

 257       * @return Array  info array

 258       */
 259  	public function getInfoArray() {
 260          return array('folderId' => $this->getId(),
 261              'folderName' => $this->getName(),
 262              'editURL' => $this->getEditUrl(),
 263              'deleteURL' => $this->getDeleteUrl(),
 264              'isEditable' => $this->isEditable(),
 265              'isDeletable' => $this->isDeletable());
 266      }
 267  
 268      /**

 269       * Function to check whether folder is editable or not

 270       * @return <boolean>

 271       */
 272  	public function isEditable() {
 273          if ($this->isDefault()) {
 274              return false;
 275          }
 276          return true;
 277      }
 278  
 279      /**

 280       * Function to get check whether folder is deletable or not

 281       * @return <boolean>

 282       */
 283  	public function isDeletable() {
 284          if ($this->isDefault()) {
 285              return false;
 286          }
 287          return true;
 288      }
 289  
 290      /**

 291       * Function to calculate number of reports in this folder

 292       * @return <Integer>

 293       */
 294  	public function getReportsCount() {
 295          $db = PearDatabase::getInstance();
 296          $params = array();
 297          
 298          // To get the report ids which are permitted for the user

 299              $query = "SELECT reportmodulesid, primarymodule from vtiger_reportmodules";
 300              $result = $db->pquery($query, array());
 301              $noOfRows = $db->num_rows($result);
 302              $allowedReportIds = array();
 303              for($i=0;$i<$noOfRows;$i++){
 304                  $primaryModule = $db->query_result($result,$i,'primarymodule');
 305                  $reportid = $db->query_result($result,$i,'reportmodulesid');
 306                  if(isPermitted($primaryModule,'index') == "yes"){
 307                      $allowedReportIds[] = $reportid;
 308                  }
 309              }
 310          //End

 311          $sql = "SELECT count(*) AS count FROM vtiger_report
 312                  INNER JOIN vtiger_reportfolder ON vtiger_reportfolder.folderid = vtiger_report.folderid AND 
 313                  vtiger_report.reportid in (".implode(',',$allowedReportIds).")";
 314          $fldrId = $this->getId();
 315          if($fldrId == 'All') {
 316              $fldrId = false;
 317          }
 318  
 319          // If information is required only for specific report folder?

 320          if($fldrId !== false) {
 321              $sql .= " WHERE vtiger_reportfolder.folderid=?";
 322              array_push($params,$fldrId);
 323          }
 324          $currentUserModel = Users_Privileges_Model::getCurrentUserPrivilegesModel();
 325          if (!$currentUserModel->isAdminUser()) {
 326              $currentUserId = $currentUserModel->getId();
 327              
 328              $groupId = implode(',',$currentUserModel->get('groups'));
 329              if ($groupId) {
 330                  $groupQuery = "(SELECT reportid from vtiger_reportsharing WHERE shareid IN ($groupId) AND setype = 'groups') OR ";
 331              }
 332              
 333              $sql .= " AND (vtiger_report.reportid IN (SELECT reportid from vtiger_reportsharing WHERE $groupQuery shareid = ? AND setype = 'users')
 334                          OR vtiger_report.sharingtype = 'Public'
 335                          OR vtiger_report.owner = ?
 336                          OR vtiger_report.owner IN (SELECT vtiger_user2role.userid FROM vtiger_user2role
 337                                                      INNER JOIN vtiger_users ON vtiger_users.id = vtiger_user2role.userid
 338                                                      INNER JOIN vtiger_role ON vtiger_role.roleid = vtiger_user2role.roleid
 339                                                      WHERE vtiger_role.parentrole LIKE ?))";
 340  
 341              $parentRoleSeq = $currentUserModel->get('parent_role_seq').'::%';
 342              array_push($params, $currentUserId, $currentUserId, $parentRoleSeq);
 343          }
 344          $result = $db->pquery($sql, $params);
 345          return $db->query_result($result, 0, 'count');
 346      }
 347      
 348      /**

 349       * Function to get all Report Record Models

 350       * @param <Array> $allReportsList

 351       * @param <Vtiger_Module_Model> - Reports Module Model

 352       * @return <Array> Reports Record Models

 353       */
 354  	public function getAllReportModels($allReportsList, $reportModuleModel){
 355          $allReportModels = array();
 356          $folders = self::getAll();
 357          foreach ($allReportsList as $key => $reportsList) {
 358              for($i=0; $i < count($reportsList); $i++) {
 359                  $reportModel = new Reports_Record_Model();
 360                  $reportModel->setData($reportsList[$i])->setModuleFromInstance($reportModuleModel);
 361                  $reportModel->set('foldername', $folders[$key]->getName());
 362                  $allReportModels[] = $reportModel;
 363                  unset($reportModel);
 364              }
 365          }
 366          return $allReportModels;
 367      }
 368      
 369       /**

 370       * Function which provides the records for the current view

 371       * @param <Boolean> $skipRecords - List of the RecordIds to be skipped

 372       * @return <Array> List of RecordsIds

 373       */
 374  	public function getRecordIds($skipRecords=false, $module) {
 375          $db = PearDatabase::getInstance();
 376          $baseTableName = "vtiger_report";
 377          $baseTableId = "reportid";
 378          $folderId = $this->getId();
 379          $listQuery = $this->getListViewQuery($folderId);
 380  
 381          if($skipRecords && !empty($skipRecords) && is_array($skipRecords) && count($skipRecords) > 0) {
 382              $listQuery .= ' AND '.$baseTableName.'.'.$baseTableId.' NOT IN ('. implode(',', $skipRecords) .')';
 383          }
 384          $result = $db->query($listQuery);
 385          $noOfRecords = $db->num_rows($result);
 386          $recordIds = array();
 387          for($i=0; $i<$noOfRecords; ++$i) {
 388              $recordIds[] = $db->query_result($result, $i, $baseTableId);
 389          }
 390          return $recordIds;
 391      }
 392      
 393      /**

 394       * Function returns Report Models for the folder

 395       * @return <Reports_Record_Model>

 396       */
 397  	function getListViewQuery($folderId) {
 398          $sql = "select vtiger_report.*, vtiger_reportmodules.*, vtiger_reportfolder.folderid from vtiger_report 
 399                  inner join vtiger_reportfolder on vtiger_reportfolder.folderid = vtiger_report.folderid 
 400                  inner join vtiger_reportmodules on vtiger_reportmodules.reportmodulesid = vtiger_report.reportid ";
 401          
 402          if($folderId != "All") {
 403                  $sql = $sql." where vtiger_reportfolder.folderid = ".$folderId;
 404          }        
 405          return $sql;
 406      }
 407  }


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