[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/modules/Vtiger/models/ -> Record.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 Entity Record Model Class
  13   */
  14  class Vtiger_Record_Model extends Vtiger_Base_Model {
  15  
  16      protected $module = false;
  17  
  18      /**
  19       * Function to get the id of the record
  20       * @return <Number> - Record Id
  21       */
  22  	public function getId() {
  23          return $this->get('id');
  24      }
  25  
  26      /**
  27       * Function to set the id of the record
  28       * @param <type> $value - id value
  29       * @return <Object> - current instance
  30       */
  31  	public function setId($value) {
  32          return $this->set('id',$value);
  33      }
  34  
  35      /**
  36       * Fuction to get the Name of the record
  37       * @return <String> - Entity Name of the record
  38       */
  39  	public function getName() {
  40          $displayName = $this->get('label');
  41          if(empty($displayName)) {
  42              $displayName = $this->getDisplayName();
  43          }
  44          return Vtiger_Util_Helper::toSafeHTML(decode_html($displayName));
  45      }
  46  
  47      /**
  48       * Function to get the Module to which the record belongs
  49       * @return Vtiger_Module_Model
  50       */
  51  	public function getModule() {
  52          return $this->module;
  53      }
  54  
  55      /**
  56       * Function to set the Module to which the record belongs
  57       * @param <String> $moduleName
  58       * @return Vtiger_Record_Model or Module Specific Record Model instance
  59       */
  60  	public function setModule($moduleName) {
  61          $this->module = Vtiger_Module_Model::getInstance($moduleName);
  62          return $this;
  63      }
  64  
  65      /**
  66       * Function to set the Module to which the record belongs from the Module model instance
  67       * @param <Vtiger_Module_Model> $module
  68       * @return Vtiger_Record_Model or Module Specific Record Model instance
  69       */
  70  	public function setModuleFromInstance($module) {
  71          $this->module = $module;
  72          return $this;
  73      }
  74  
  75      /**
  76       * Function to get the entity instance of the recrod
  77       * @return CRMEntity object
  78       */
  79  	public function getEntity() {
  80          return $this->entity;
  81      }
  82  
  83      /**
  84       * Function to set the entity instance of the record
  85       * @param CRMEntity $entity
  86       * @return Vtiger_Record_Model instance
  87       */
  88  	public function setEntity($entity) {
  89          $this->entity = $entity;
  90          return $this;
  91      }
  92  
  93      /**
  94       * Function to get raw data
  95       * @return <Array>
  96       */
  97  	public function getRawData() {
  98          return $this->rawData;
  99      }
 100  
 101      /**
 102       * Function to set raw data
 103       * @param <Array> $data
 104       * @return Vtiger_Record_Model instance
 105       */
 106  	public function setRawData($data) {
 107          $this->rawData = $data;
 108          return $this;
 109      }
 110  
 111      /**
 112       * Function to get the Detail View url for the record
 113       * @return <String> - Record Detail View Url
 114       */
 115  	public function getDetailViewUrl() {
 116          $module = $this->getModule();
 117          return 'index.php?module='.$this->getModuleName().'&view='.$module->getDetailViewName().'&record='.$this->getId();
 118      }
 119  
 120      /**
 121       * Function to get the complete Detail View url for the record
 122       * @return <String> - Record Detail View Url
 123       */
 124  	public function getFullDetailViewUrl() {
 125          $module = $this->getModule();
 126          return 'index.php?module='.$this->getModuleName().'&view='.$module->getDetailViewName().'&record='.$this->getId().'&mode=showDetailViewByMode&requestMode=full';
 127      }
 128  
 129      /**
 130       * Function to get the Edit View url for the record
 131       * @return <String> - Record Edit View Url
 132       */
 133  	public function getEditViewUrl() {
 134          $module = $this->getModule();
 135          return 'index.php?module='.$this->getModuleName().'&view='.$module->getEditViewName().'&record='.$this->getId();
 136      }
 137  
 138      /**
 139       * Function to get the Update View url for the record
 140       * @return <String> - Record Upadte view Url
 141       */
 142  	public function getUpdatesUrl() {
 143          return $this->getDetailViewUrl()."&mode=showRecentActivities&page=1&tab_label=LBL_UPDATES";
 144      }
 145  
 146      /**
 147       * Function to get the Delete Action url for the record
 148       * @return <String> - Record Delete Action Url
 149       */
 150  	public function getDeleteUrl() {
 151          $module = $this->getModule();
 152          return 'index.php?module='.$this->getModuleName().'&action='.$module->getDeleteActionName().'&record='.$this->getId();
 153      }
 154  
 155      /**
 156       * Function to get the name of the module to which the record belongs
 157       * @return <String> - Record Module Name
 158       */
 159  	public function getModuleName() {
 160          return $this->getModule()->get('name');
 161      }
 162  
 163      /**
 164       * Function to get the Display Name for the record
 165       * @return <String> - Entity Display Name for the record
 166       */
 167  	public function getDisplayName() {
 168          return Vtiger_Util_Helper::getLabel($this->getId());
 169      }
 170  
 171      /**
 172       * Function to retieve display value for a field
 173       * @param <String> $fieldName - field name for which values need to get
 174       * @return <String>
 175       */
 176  	public function getDisplayValue($fieldName,$recordId = false) {
 177          if(empty($recordId)) {
 178              $recordId = $this->getId();
 179          }
 180          $fieldModel = $this->getModule()->getField($fieldName);
 181         
 182          // For showing the "Date Sent" and "Time Sent" in email related list in user time zone
 183          if($fieldName == "time_start" && $this->getModule()->getName() == "Emails"){
 184              $date = new DateTime();
 185              $dateTime = new DateTimeField($date->format('Y-m-d').' '.$this->get($fieldName));
 186              $value = $dateTime->getDisplayTime();
 187              $this->set($fieldName, $value);
 188              return $value;
 189          }else if($fieldName == "date_start" && $this->getModule()->getName() == "Emails"){
 190              $dateTime = new DateTimeField($this->get($fieldName).' '.$this->get('time_start'));
 191              $value = $dateTime->getDisplayDate();
 192              $this->set($fieldName, $value);
 193              return $value;
 194          }
 195          // End
 196          
 197          if($fieldModel) {
 198              return $fieldModel->getDisplayValue($this->get($fieldName), $recordId, $this);
 199          }
 200          return false;
 201      }
 202  
 203      /**
 204       * Function returns the Vtiger_Field_Model
 205       * @param <String> $fieldName - field name
 206       * @return <Vtiger_Field_Model>
 207       */
 208  	public function getField($fieldName) {
 209          return $this->getModule()->getField($fieldName);
 210      }
 211  
 212      /**
 213       * Function returns all the field values in user format
 214       * @return <Array>
 215       */
 216  	public function getDisplayableValues() {
 217          $displayableValues = array();
 218          $data = $this->getData();
 219          foreach($data as $fieldName=>$value) {
 220              $fieldValue = $this->getDisplayValue($fieldName);
 221              $displayableValues[$fieldName] = ($fieldValue) ? $fieldValue : $value;
 222          }
 223          return $displayableValues;
 224      }
 225  
 226      /**
 227       * Function to save the current Record Model
 228       */
 229  	public function save() {
 230          $this->getModule()->saveRecord($this);
 231      }
 232  
 233      /**
 234       * Function to delete the current Record Model
 235       */
 236  	public function delete() {
 237          $this->getModule()->deleteRecord($this);
 238      }
 239  
 240      /**
 241       * Static Function to get the instance of a clean Vtiger Record Model for the given module name
 242       * @param <String> $moduleName
 243       * @return Vtiger_Record_Model or Module Specific Record Model instance
 244       */
 245  	public static function getCleanInstance($moduleName) {
 246          //TODO: Handle permissions
 247          $focus = CRMEntity::getInstance($moduleName);
 248          $modelClassName = Vtiger_Loader::getComponentClassName('Model', 'Record', $moduleName);
 249          $instance = new $modelClassName();
 250          return $instance->setData($focus->column_fields)->setModule($moduleName)->setEntity($focus);
 251      }
 252  
 253      /**
 254       * Static Function to get the instance of the Vtiger Record Model given the recordid and the module name
 255       * @param <Number> $recordId
 256       * @param <String> $moduleName
 257       * @return Vtiger_Record_Model or Module Specific Record Model instance
 258       */
 259  	public static function getInstanceById($recordId, $module=null) {
 260          //TODO: Handle permissions
 261          if(is_object($module) && is_a($module, 'Vtiger_Module_Model')) {
 262              $moduleName = $module->get('name');
 263          } elseif (is_string($module)) {
 264              $module = Vtiger_Module_Model::getInstance($module);
 265              $moduleName = $module->get('name');
 266          } elseif(empty($module)) {
 267              $moduleName = getSalesEntityType($recordId);
 268              $module = Vtiger_Module_Model::getInstance($moduleName);
 269          }
 270  
 271          $focus = CRMEntity::getInstance($moduleName);
 272          $focus->id = $recordId;
 273          $focus->retrieve_entity_info($recordId, $moduleName);
 274          $modelClassName = Vtiger_Loader::getComponentClassName('Model', 'Record', $moduleName);
 275          $instance = new $modelClassName();
 276          return $instance->setData($focus->column_fields)->set('id',$recordId)->setModuleFromInstance($module)->setEntity($focus);
 277      }
 278  
 279      /**
 280       * Static Function to get the list of records matching the search key
 281       * @param <String> $searchKey
 282       * @return <Array> - List of Vtiger_Record_Model or Module Specific Record Model instances
 283       */
 284  	public static function getSearchResult($searchKey, $module=false) {
 285          $db = PearDatabase::getInstance();
 286  
 287          $query = 'SELECT label, crmid, setype, createdtime FROM vtiger_crmentity WHERE label LIKE ? AND vtiger_crmentity.deleted = 0';
 288          $params = array("%$searchKey%");
 289  
 290          if($module !== false) {
 291              $query .= ' AND setype = ?';
 292              $params[] = $module;
 293          }
 294          //Remove the ordering for now to improve the speed
 295          //$query .= ' ORDER BY createdtime DESC';
 296  
 297          $result = $db->pquery($query, $params);
 298          $noOfRows = $db->num_rows($result);
 299  
 300          $moduleModels = $matchingRecords = $leadIdsList = array();
 301          for($i=0; $i<$noOfRows; ++$i) {
 302              $row = $db->query_result_rowdata($result, $i);
 303              if ($row['setype'] === 'Leads') {
 304                  $leadIdsList[] = $row['crmid'];
 305              }
 306          }
 307          $convertedInfo = Leads_Module_Model::getConvertedInfo($leadIdsList);
 308  
 309          for($i=0, $recordsCount = 0; $i<$noOfRows && $recordsCount<100; ++$i) {
 310              $row = $db->query_result_rowdata($result, $i);
 311              if ($row['setype'] === 'Leads' && $convertedInfo[$row['crmid']]) {
 312                  continue;
 313              }
 314              if(Users_Privileges_Model::isPermitted($row['setype'], 'DetailView', $row['crmid'])) {
 315                  $row['id'] = $row['crmid'];
 316                  $moduleName = $row['setype'];
 317                  if(!array_key_exists($moduleName, $moduleModels)) {
 318                      $moduleModels[$moduleName] = Vtiger_Module_Model::getInstance($moduleName);
 319                  }
 320                  $moduleModel = $moduleModels[$moduleName];
 321                  $modelClassName = Vtiger_Loader::getComponentClassName('Model', 'Record', $moduleName);
 322                  $recordInstance = new $modelClassName();
 323                  $matchingRecords[$moduleName][$row['id']] = $recordInstance->setData($row)->setModuleFromInstance($moduleModel);
 324                  $recordsCount++;
 325              }
 326          }
 327          return $matchingRecords;
 328      }
 329  
 330      /**
 331       * Function to get details for user have the permissions to do actions
 332       * @return <Boolean> - true/false
 333       */
 334  	public function isEditable() {
 335          return Users_Privileges_Model::isPermitted($this->getModuleName(), 'EditView', $this->getId());
 336      }
 337  
 338      /**
 339       * Function to get details for user have the permissions to do actions
 340       * @return <Boolean> - true/false
 341       */
 342  	public function isDeletable() {
 343          return Users_Privileges_Model::isPermitted($this->getModuleName(), 'Delete', $this->getId());
 344      }
 345  
 346      /**
 347       * Funtion to get Duplicate Record Url
 348       * @return <String>
 349       */
 350  	public function getDuplicateRecordUrl() {
 351          $module = $this->getModule();
 352          return 'index.php?module='.$this->getModuleName().'&view='.$module->getEditViewName().'&record='.$this->getId().'&isDuplicate=true';
 353  
 354      }
 355  
 356      /**
 357       * Function to get Display value for RelatedList
 358       * @param <String> $value
 359       * @return <String>
 360       */
 361  	public function getRelatedListDisplayValue($fieldName) {
 362          $fieldModel = $this->getModule()->getField($fieldName);
 363          return $fieldModel->getRelatedListDisplayValue($this->get($fieldName));
 364      }
 365  
 366      /**
 367       * Function to delete corresponding image
 368       * @param <type> $imageId
 369       */
 370  	public function deleteImage($imageId) {
 371          $db = PearDatabase::getInstance();
 372  
 373          $checkResult = $db->pquery('SELECT crmid FROM vtiger_seattachmentsrel WHERE attachmentsid = ?', array($imageId));
 374          $crmId = $db->query_result($checkResult, 0, 'crmid');
 375  
 376          if ($this->getId() === $crmId) {
 377              $db->pquery('DELETE FROM vtiger_attachments WHERE attachmentsid = ?', array($imageId));
 378              $db->pquery('DELETE FROM vtiger_seattachmentsrel WHERE attachmentsid = ?', array($imageId));
 379              return true;
 380          }
 381          return false;
 382      }
 383  
 384      /**
 385       * Function to get Descrption value for this record
 386       * @return <String> Descrption
 387       */
 388  	public function getDescriptionValue() {
 389          $description = $this->get('description');
 390          if(empty($description)) {
 391              $db = PearDatabase::getInstance();
 392              $result = $db->pquery("SELECT description FROM vtiger_crmentity WHERE crmid = ?", array($this->getId()));
 393              $description =  $db->query_result($result, 0, "description");
 394          }
 395          return $description;
 396      }
 397  
 398      /**
 399       * Function to transfer related records of parent records to this record
 400       * @param <Array> $recordIds
 401       * @return <Boolean> true/false
 402       */
 403  	public function transferRelationInfoOfRecords($recordIds = array()) {
 404          if ($recordIds) {
 405              $moduleName = $this->getModuleName();
 406              $focus = CRMEntity::getInstance($moduleName);
 407              if (method_exists($focus, 'transferRelatedRecords')) {
 408                  $focus->transferRelatedRecords($moduleName, $recordIds, $this->getId());
 409              }
 410          }
 411          return true;
 412      }
 413  }


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