[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/vtlib/Vtiger/ -> Event.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  include_once ('vtlib/Vtiger/Utils.php');
  11  include_once ('modules/Users/Users.php');
  12  @include_once ('include/events/include.inc');
  13  
  14  /**
  15   * Provides API to work with vtiger CRM Eventing (available from vtiger 5.1)
  16   * @package vtlib
  17   */
  18  class Vtiger_Event {
  19      /** Event name like: vtiger.entity.aftersave, vtiger.entity.beforesave */
  20      var $eventname;
  21      /** Event handler class to use */
  22      var $classname;
  23      /** Filename where class is defined */
  24      var $filename;
  25      /** Condition for the event */
  26      var $condition;
  27  
  28      /** Internal caching */
  29      static $is_supported = '';
  30      
  31      /**
  32       * Helper function to log messages
  33       * @param String Message to log
  34       * @param Boolean true appends linebreak, false to avoid it
  35       * @access private
  36       */
  37  	static function log($message, $delim=true) {
  38          Vtiger_Utils::Log($message, $delim);
  39      }
  40  
  41      /**
  42       * Check if vtiger CRM support Events
  43       */
  44  	static function hasSupport() {
  45          if(self::$is_supported === '') {
  46              self::$is_supported = Vtiger_Utils::checkTable('vtiger_eventhandlers');
  47          }
  48          return self::$is_supported;
  49      }
  50  
  51      /**
  52       * Handle event registration for module
  53       * @param Vtiger_Module Instance of the module to use
  54       * @param String Name of the Event like vtiger.entity.aftersave, vtiger.entity.beforesave
  55       * @param String Name of the Handler class (should extend VTEventHandler)
  56       * @param String File path which has Handler class definition
  57       * @param String Condition for the event to trigger (default blank)
  58       */
  59  	static function register($moduleInstance, $eventname, $classname, $filename, $condition='', $dependent='[]') {
  60          // Security check on fileaccess, don't die if it fails
  61          if(Vtiger_Utils::checkFileAccess($filename, false)) {
  62              global $adb;
  63              $eventsManager = new VTEventsManager($adb);
  64              $eventsManager->registerHandler($eventname, $filename, $classname, $condition, $dependent);
  65              $eventsManager->setModuleForHandler($moduleInstance->name, $classname);
  66  
  67              self::log("Registering Event $eventname with [$filename] $classname ... DONE");
  68          }
  69      }
  70  
  71      /**
  72       * Trigger event based on CRM Record
  73       * @param String Name of the Event to trigger
  74       * @param Integer CRM record id on which event needs to be triggered.
  75       */
  76  	static function trigger($eventname, $crmid) {
  77          if(!self::hasSupport()) return;
  78  
  79          global $adb;
  80          $checkres = $adb->pquery("SELECT setype, crmid, deleted FROM vtiger_crmentity WHERE crmid=?", Array($crmid));
  81          if($adb->num_rows($checkres)) {
  82              $result = $adb->fetch_array($checkres, 0);
  83              if($result['deleted'] == '0') {
  84                  $module = $result['setype'];
  85                  $moduleInstance = CRMEntity::getInstance($module);
  86                  $moduleInstance->retrieve_entity_info($result['crmid'], $module);
  87                  $moduleInstance->id = $result['crmid'];
  88  
  89                  global $current_user;
  90                  if(!$current_user) {
  91                      $current_user = new Users();
  92                      $current_user->id = $moduleInstance->column_fields['assigned_user_id'];
  93                  }
  94  
  95                  // Trigger the event
  96                  $em = new VTEventsManager($adb);
  97                  $em->triggerEvent($eventname, VTEntityData::fromCRMEntity($moduleInstance));
  98              }
  99          }        
 100      }
 101  
 102      /**
 103       * Get all the registered module events
 104       * @param Vtiger_Module Instance of the module to use
 105       */
 106  	static function getAll($moduleInstance) {
 107          global $adb;
 108          $events = false;
 109          if(self::hasSupport()) {
 110              // Get all events related to module
 111              $records = $adb->pquery("SELECT * FROM vtiger_eventhandlers WHERE handler_class IN 
 112                  (SELECT handler_class FROM vtiger_eventhandler_module WHERE module_name=?)", Array($moduleInstance->name)); 
 113              if($records) {
 114                  while($record = $adb->fetch_array($records)) {
 115                      $event = new Vtiger_Event();                    
 116                      $event->eventname = $record['event_name'];
 117                      $event->classname = $record['handler_class']; 
 118                      $event->filename  = $record['handler_path'];
 119                      $event->condition = $record['condition'];
 120                      $events[] = $event;
 121                  }
 122              }
 123          }
 124          return $events;
 125      }        
 126  }
 127  ?>


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