[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/include/events/ -> VTEventTrigger.inc (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/VTBatchEventTrigger.inc';
  11      require_once ("include/events/SqlResultIterator.inc");
  12      class VTEventTrigger{
  13  
  14          /* EventTrigger cache management */

  15          private static $cache = array();
  16          private static $cacheLookupType = '';
  17  
  18          private static $mandatoryEventClass = array('VTEntityDelta', 'ModTrackerHandler');
  19  
  20          const CACHE_LOOKUP_TYPE_ALL = 'ALL';
  21  
  22  		static function initCache($name = false, $force = false) {
  23              global $adb;
  24  
  25              if($name) {
  26                  $names = $name;
  27                  if(!is_array($names)) $names = array($name);
  28  
  29                  $evtinfos = self::getActiveEventInfos($adb, $names);
  30  
  31                  foreach($evtinfos as $k=>$v) {
  32                      if(!self::isCached($k) || $force) {
  33                          self::$cache[$forname] = $evtinfos;
  34                      }
  35                  }
  36              } else {
  37                  if(!self::isCached() || $force) {
  38                      self::$cache = self::getActiveEventInfos($adb, $name);
  39                      self::$cacheLookupType = self::CACHE_LOOKUP_TYPE_ALL;
  40                  }
  41              }
  42          }
  43  		static function isCached($name = false) {
  44              if($name === false) {
  45                  if(self::$cacheLookupType == self::CACHE_LOOKUP_TYPE_ALL) {
  46                      // Was init cache done for ALL earlier?

  47                      return true;
  48                  }
  49              } else {
  50                  return isset(self::$cache[$name]);
  51              }
  52              return false;
  53          }
  54  		static function clearCache($name = false) {
  55              if($name === false) {
  56                  self::$cache = array();
  57                  self::$cacheLookupType = '';
  58              } else if(self::isCached($name)) {
  59                  unset(self::$cache[$name]);
  60              }
  61          }
  62  		static function lookupCache($name) {
  63              if(self::isCached($name)) {
  64                  return self::$cache[$name];
  65              } else if(self::$cacheLookupType == self::CACHE_LOOKUP_TYPE_ALL) {
  66                  return array();
  67              }
  68              return false;
  69          }
  70  
  71  		static function getActiveEventInfos($adb, $name = false) {
  72              $params = array();
  73              $query = "SELECT * FROM vtiger_eventhandlers WHERE is_active=true";
  74              if($name !== false) {
  75                  if(is_array($name)) {
  76                      $query .= " AND event_name IN (" . generateQuestionMarks($name) . ")";
  77                  } else {
  78                      $query .= " AND event_name = ?";
  79                  }
  80                  $params[] = $name;
  81              }
  82              if(CRMEntity::isBulkSaveMode()) {
  83                  $query .= " AND handler_class IN (" . generateQuestionMarks(self::$mandatoryEventClass) . ")";
  84                  array_push($params, self::$mandatoryEventClass);
  85              }
  86  
  87              $evtinfosbyname = array();
  88  
  89              $result= $adb->pquery($query, $params);
  90              $it = new SqlResultIterator($adb, $result);
  91              foreach($it as $row) {
  92                  $evtinfosbyname[$row->event_name][] = array(
  93                      'condition'     => $row->cond,
  94                      'handler_class' => $row->handler_class,
  95                      'handler_path'  => $row->handler_path,
  96                      'dependent_on'    => $row->dependent_on,
  97                  );
  98              }
  99  
 100              if($name) return $evtinfosbyname[$name];
 101              else return $evtinfosbyname;
 102          }
 103          /** END **/

 104  
 105  		function __construct($adb, $name){
 106              $this->name=$name;
 107              $this->adb = $adb;
 108          }
 109  
 110  		function trigger($data){
 111              $adb = $this->adb;
 112  
 113              $eventInfos = self::lookupCache($this->name);
 114              if($eventInfos === false) {
 115                  $eventInfos = self::getActiveEventInfos($this->adb, $this->name);
 116              }
 117  
 118              $completedEvents = array();
 119              if($eventInfos) {
 120                  while(count($eventInfos) > count($completedEvents)) {
 121                      $handlerCounter = 0; // Tracks the number of handlers triggered for the current iteration.

 122                      foreach($eventInfos as $eventInfo){
 123                          $condition = new VTEventCondition($eventInfo['condition']);
 124                          if($condition->test($data)){
 125                              $handler_class = $eventInfo['handler_class'];
 126                              if(in_array($handler_class, $completedEvents)) {
 127                                  continue;
 128                              }
 129  
 130                              $dependentEventsNotCompleted = false;
 131                              $dependentOn = $eventInfo['dependent_on'];
 132                              $dependentEvents = Zend_Json::decode($dependentOn);
 133                              foreach($dependentEvents as $eventHandlerClass) {
 134                                  if(!in_array($eventHandlerClass, $completedEvents)) {
 135                                      $dependentEventsNotCompleted = true;
 136                                  }
 137                              }
 138                              if($dependentEventsNotCompleted) continue;
 139  
 140                              require_once($eventInfo['handler_path']);
 141                              $handler = new $handler_class();
 142  
 143                              $handler->handleEvent($this->name, $data);
 144                              $completedEvents[] = $handler_class;
 145                              $handlerCounter++;
 146                          } else {
 147                              // Mark the event-handler as finished - without actually invoking.

 148                              $completedEvents[] = $eventInfo['handler_class'];
 149                          }
 150                      }
 151                      if($handlerCounter == 0 && count($eventInfos) > count($completedEvents)) {
 152                          $uncompletedEvents = array();
 153                          foreach($eventInfos as $eventInfo){
 154                              if(!in_array($eventInfo['handler_class'], $completedEvents)) {
 155                                  $uncompletedEvents[] = $eventInfo['handler_class'];
 156                              }
 157                          }
 158                          throw new Exception("Deadlock occured for events: ". implode(' , ', $uncompletedEvents));
 159                      }
 160                  }
 161              }
 162          }
 163  
 164  		public static function getInstance($adb, $triggerName) {
 165              if(stripos($triggerName, 'batch')) {
 166                  return new VTBatchEventTrigger($adb, $triggerName);
 167              }
 168              return new self($adb, $triggerName);
 169          }
 170      }
 171  ?>


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