[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/data/ -> VTEntityDelta.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  require_once  'include/events/VTEntityData.inc';
  11  
  12  class VTEntityDelta extends VTEventHandler {
  13      private static $oldEntity;
  14      private static $newEntity;
  15      private static $entityDelta;
  16  
  17  	function  __construct() {
  18          
  19      }
  20  
  21  	function handleEvent($eventName, $entityData) {
  22  
  23          $adb = PearDatabase::getInstance();
  24          $moduleName = $entityData->getModuleName();
  25          $recordId = $entityData->getId();
  26          
  27          if($eventName == 'vtiger.entity.beforesave') {
  28              if(!empty($recordId)) {
  29                  $entityData = VTEntityData::fromEntityId($adb, $recordId);
  30                  if($moduleName == 'HelpDesk') {
  31                      $entityData->set('comments', getTicketComments($recordId));
  32                  } elseif($moduleName == 'Invoice'){
  33                      $entityData->set('invoicestatus', getInvoiceStatus($recordId));
  34                  }
  35                  self::$oldEntity[$moduleName][$recordId] = $entityData;
  36              }
  37          }
  38  
  39          if($eventName == 'vtiger.entity.aftersave'){
  40              $this->fetchEntity($moduleName, $recordId);
  41              $this->computeDelta($moduleName, $recordId);
  42          }
  43      }
  44  
  45  	function fetchEntity($moduleName, $recordId) {
  46          $adb = PearDatabase::getInstance();
  47          $entityData = VTEntityData::fromEntityId($adb, $recordId, $moduleName);
  48          if($moduleName == 'HelpDesk') {
  49              $entityData->set('comments', getTicketComments($recordId));
  50          } elseif($moduleName == 'Invoice') {
  51              $entityData->set('invoicestatus', getInvoiceStatus($recordId));
  52          }
  53          self::$newEntity[$moduleName][$recordId] = $entityData;
  54      }
  55  
  56  	function computeDelta($moduleName, $recordId) {
  57  
  58          $delta = array();
  59  
  60          $oldData = array();
  61          if(!empty(self::$oldEntity[$moduleName][$recordId])) {
  62              $oldEntity = self::$oldEntity[$moduleName][$recordId];
  63              $oldData = $oldEntity->getData();
  64          }
  65          $newEntity = self::$newEntity[$moduleName][$recordId];
  66          $newData = $newEntity->getData();
  67          /** Detect field value changes **/
  68          foreach($newData as $fieldName => $fieldValue) {
  69              $isModified = false;
  70              if(empty($oldData[$fieldName])) {
  71                  if(!empty($newData[$fieldName])) {
  72                      $isModified = true;
  73                  }
  74              } elseif($oldData[$fieldName] != $newData[$fieldName]) {
  75                  $isModified = true;
  76              }
  77              if($isModified) {
  78                  $delta[$fieldName] = array('oldValue' => $oldData[$fieldName],
  79                                          'currentValue' => $newData[$fieldName]);
  80              }
  81          }
  82          self::$entityDelta[$moduleName][$recordId] = $delta;
  83      }
  84  
  85  	function getEntityDelta($moduleName, $recordId, $forceFetch=false) {
  86          if($forceFetch) {
  87              $this->fetchEntity($moduleName, $recordId);
  88              $this->computeDelta($moduleName, $recordId);
  89          }
  90          return self::$entityDelta[$moduleName][$recordId];
  91      }
  92  
  93  	function getOldValue($moduleName, $recordId, $fieldName) {
  94          $entityDelta = self::$entityDelta[$moduleName][$recordId];
  95          return $entityDelta[$fieldName]['oldValue'];
  96      }
  97  
  98  	function getCurrentValue($moduleName, $recordId, $fieldName) {
  99          $entityDelta = self::$entityDelta[$moduleName][$recordId];
 100          return $entityDelta[$fieldName]['currentValue'];
 101      }
 102  
 103  	function getOldEntity($moduleName, $recordId) {
 104          return self::$oldEntity[$moduleName][$recordId];
 105      }
 106  
 107  	function getNewEntity($moduleName, $recordId) {
 108          return self::$newEntity[$moduleName][$recordId];
 109      }
 110      
 111  	function hasChanged($moduleName, $recordId, $fieldName, $fieldValue = NULL) {
 112          if(empty(self::$oldEntity[$moduleName][$recordId])) {
 113              return false;
 114          }
 115          $fieldDelta = self::$entityDelta[$moduleName][$recordId][$fieldName];
 116          $result = $fieldDelta['oldValue'] != $fieldDelta['currentValue'];
 117          if ($fieldValue !== NULL) {
 118              $result = $result && ($fieldDelta['currentValue'] === $fieldValue);
 119          }
 120          return $result;
 121      }
 122  
 123  }
 124  ?>


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