[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/include/events/ -> VTEventsManager.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  
  11      /**
  12       * Create and trigger events in vtiger
  13       */
  14      class VTEventsManager{
  15  		function __construct($adb){
  16              $this->adb = $adb;
  17          }
  18  
  19          /**
  20           * Register an event handler
  21           *
  22           * @param $forEvent The name of the event to handle
  23           * @param $path The path to the php file containing the handler
  24           * @param $className The name of the VTEventHandler class.
  25           * @param $condition A condition which must evaluate true for the event to be accepted.
  26           */
  27  		function registerHandler($forEvent, $path, $className, $condition='', $depedentOn='[]'){
  28              $adb = $this->adb;
  29              $result = $adb->pquery("SELECT * FROM vtiger_eventhandlers WHERE event_name=? AND handler_path=? AND handler_class=?",
  30                  array($forEvent, $path, $className));
  31              if($adb->num_rows($result)===0){
  32                  $handlerId = $adb->getUniqueId('vtiger_eventhandlers');
  33                  $adb->pquery("insert into vtiger_eventhandlers
  34                      (eventhandler_id, event_name, handler_path, handler_class, cond, is_active, dependent_on)
  35                      values (?,?,?,?,?, true, ?)", array($handlerId, $forEvent, $path, $className, $condition, $depedentOn));
  36  
  37                  $this->clearTriggerCache($forEvent);
  38              }
  39          }
  40  
  41          /**
  42           * Initialize Event Trigger Cache for the required event types.
  43           *
  44           * @param Object $for Optional String or Array of event_names for initializing.
  45           * @param Boolean $force Optional Force the initialization of cache?
  46           */
  47  		function initTriggerCache($for = false, $force = false) {
  48              VTEventTrigger::initCache($for, $force);
  49          }
  50  
  51          /**
  52           * Clear the Event Trigger Cache
  53           *
  54           * @param Object $forEvent
  55           */
  56  		function clearTriggerCache($forEvent = false) {
  57              VTEventTrigger::clearCache($forEvent);
  58          }
  59  
  60          /**
  61           * Set an event handler as inactive
  62           * @param The handler class to set as inactive
  63           *
  64           */
  65  		function setHandlerInActive($handlerClass){
  66              $adb = $this->adb;
  67              $adb->pquery("update vtiger_eventhandlers set is_active=false where handler_class=?", array($handlerClass));
  68  
  69              $this->clearTriggerCache();
  70          }
  71  
  72          /**
  73           * Set an event handler as active
  74           *
  75           * @param The handler class to set as active
  76           */
  77  		function setHandlerActive($handlerClass){
  78              $adb = $this->adb;
  79              $adb->pquery("update vtiger_eventhandlers set is_active=true where handler_class=?", array($handlerClass));
  80  
  81              $this->clearTriggerCache();
  82          }
  83  
  84  
  85          /**
  86           * Unregister a registered handler
  87           *
  88           * @param $className The name of teh VTEventHandler class to unregister
  89           */
  90  		function unregisterHandler($className){
  91              $adb = $this->adb;
  92              $adb->pquery("delete from vtiger_eventhandlers where handler_class=?",
  93                  array($className));
  94              $adb->pquery("delete from vtiger_eventhandler_module where handler_class=?",
  95                  array($className));
  96  
  97              $this->clearTriggerCache();
  98          }
  99  
 100          /**
 101           * Get an event triger instance
 102           *
 103           * @param $triggerName The name of the event.
 104           * @return The trigger object for the event.
 105           */
 106  		function getTrigger($triggerName){
 107              $adb=$this->adb;
 108              return VTEventTrigger::getInstance($adb, $triggerName);
 109          }
 110  
 111          /**
 112           * Trigger an event
 113           *
 114           * @param $triggerName The name of the event.
 115           * @return The trigger object for the event.
 116           */
 117  		function triggerEvent($triggerName, $data){
 118              $this->getTrigger($triggerName)->trigger($data);
 119          }
 120  
 121          /**
 122           * Set the module the handler belongs to
 123           *
 124           * @param moduleName - The name of the module
 125           * @param handlerClass - The name of the handler class
 126           */
 127  		function setModuleForHandler($moduleName, $handlerClass){
 128              $adb = $this->adb;
 129              $result = $adb->pquery("SELECT * FROM vtiger_eventhandler_module WHERE handler_class=?",
 130                  array($handlerClass));
 131              if($adb->num_rows($result)===0){
 132                  $handlerModuleId = $adb->getUniqueId('vtiger_eventhandler_module');
 133                  $adb->pquery("insert into vtiger_eventhandler_module
 134                      (eventhandler_module_id, module_name, handler_class)
 135                      values (?,?,?)", array($handlerModuleId, $moduleName, $handlerClass));
 136              }
 137          }
 138  
 139          /**
 140           * List handler classes for a module
 141           *
 142           * @param moduleName - The name of the module
 143           */
 144  		function listHandlersForModule($moduleName){
 145            $adb = $this->adb;
 146            $result = $adb->pquery('SELECT handler_class FROM vtiger_eventhandler_module WHERE module_name=?', array($moduleName));
 147            $it = new SqlResultIterator($adb, $result);
 148            $arr = array();
 149            foreach($it as $row){
 150              $arr[] = $row->handler_class;
 151            }
 152            return $arr;
 153          }
 154  
 155          /**
 156           * List active events.
 157           *
 158           * @return A list of registered events.
 159           */
 160  		function listActiveEventHandlers(){
 161              $adb = $this->adb;
 162              $result = $adb->pquery("select * from vtiger_eventhandlers where is_active=true",
 163                      array());
 164              return $this->listEventHandlers($result);
 165          }
 166  
 167  		function listAllEventHandlers(){
 168              $adb = $this->adb;
 169              $result = $adb->pquery("select * from vtiger_eventhandlers", array());
 170              return $this->listEventHandlers($result);
 171  
 172          }
 173  
 174  		private function listEventHandlers($result){
 175              $adb = $this->adb;
 176              $it = new SQLResultIterator($adb, $result);
 177              $out = array();
 178              foreach($it as $row){
 179                  $el = array();
 180                  $el['eventName'] = $row->event_name;
 181                  $el['handlerPath'] = $row->handler_path;
 182                  $el['handlerClass'] = $row->handler_class;
 183                  $el['condition'] = $row->cond;
 184                  $el['isActive'] = $row->is_active;
 185                  $out[] = $el;
 186              }
 187              return $out;
 188          }
 189      }
 190  ?>


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