/**
  * Method pre to store the entity data before the data in the database is changed
  * for the edit operation
  *
  * @param string $op
  * @param string $objectName
  * @param int $objectId
  * @param array $params
  * @access public
  * @static
  *
  */
 public static function pre($op, $objectName, $objectId, $params)
 {
     $nonPreEntities = array('GroupContact', 'EntityTag', 'ActionLog');
     if ($op != 'edit' || in_array($objectName, $nonPreEntities)) {
         return;
     }
     /**
      * Not every object in CiviCRM sets the object id in the pre hook
      * But we need this to fetch the current data state from the database.
      * So we check if the ID is in the params array and if so we use that id
      * for fetching the data
      *
      */
     $id = $objectId;
     if (empty($id) && isset($params['id']) && !empty($params['id'])) {
         $id = $params['id'];
     }
     if (empty($id)) {
         return;
     }
     //retrieve data as it is currently in the database
     $entity = CRM_Civirules_Utils_ObjectName::convertToEntity($objectName);
     if (!$entity) {
         return;
     }
     try {
         $data = civicrm_api3($entity, 'getsingle', array('id' => $id));
     } catch (Exception $e) {
         return;
     }
     self::setPreData($entity, $id, $data);
 }
 /**
  * Trigger a rule for this trigger
  *
  * @param $op
  * @param $objectName
  * @param $objectId
  * @param $objectRef
  */
 public function triggerTrigger($op, $objectName, $objectId, $objectRef)
 {
     $entity = CRM_Civirules_Utils_ObjectName::convertToEntity($objectName);
     //only execute entity tag for setting or removing tags from contacts
     //beceuase we need to know the contact id for the trigger engine
     //and we only know this when the tag is on contact level
     if (!isset($objectRef['1']) || $objectRef['1'] != 'civicrm_contact') {
         return;
     }
     foreach ($objectRef['0'] as $cid) {
         $data = array('tag_id' => $objectId, 'entity_id' => $cid, 'entity_table' => $objectRef['1'], 'contact_id' => $cid);
         $triggerData = new CRM_Civirules_TriggerData_Post($entity, $objectId, $data);
         CRM_Civirules_Engine::triggerRule($this, $triggerData);
     }
 }
 /**
  * Get trigger data belonging to this specific post event
  *
  * Sub classes could override this method. E.g. a post on GroupContact doesn't give on object of GroupContact
  * it rather gives an array with contact Id's
  *
  * @param $op
  * @param $objectName
  * @param $objectId
  * @param $objectRef
  * @return CRM_Civirules_TriggerData_Edit|CRM_Civirules_TriggerData_Post
  */
 protected function getTriggerDataFromPost($op, $objectName, $objectId, $objectRef)
 {
     $entity = CRM_Civirules_Utils_ObjectName::convertToEntity($objectName);
     $data = $this->convertObjectRefToDataArray($entity, $objectRef, $objectId);
     if ($op == 'edit') {
         //set also original data with an edit event
         $oldData = CRM_Civirules_Utils_PreData::getPreData($entity, $objectId);
         $triggerData = new CRM_Civirules_TriggerData_Edit($entity, $objectId, $data, $oldData);
     } else {
         $triggerData = new CRM_Civirules_TriggerData_Post($entity, $objectId, $data);
     }
     $this->alterTriggerData($triggerData);
     return $triggerData;
 }
 /**
  * Get event data belonging to this specific post event
  *
  * Sub classes could override this method. E.g. a post on GroupContact doesn't give on object of GroupContact
  * it rather gives an array with contact Id's
  *
  * @param $op
  * @param $objectName
  * @param $objectId
  * @param $objectRef
  * @return CRM_Civirules_EventData_Edit|CRM_Civirules_EventData_Post
  */
 protected function getEventDataFromPost($op, $objectName, $objectId, $objectRef)
 {
     $entity = CRM_Civirules_Utils_ObjectName::convertToEntity($objectName);
     //set data
     $data = array();
     if (is_object($objectRef)) {
         CRM_Core_DAO::storeValues($objectRef, $data);
     } elseif (is_array($objectRef)) {
         $data = $objectRef;
     }
     if ($op == 'edit') {
         //set also original data with an edit event
         $oldData = CRM_Civirules_Utils_PreData::getPreData($entity, $objectId);
         $eventData = new CRM_Civirules_EventData_Edit($entity, $objectId, $data, $oldData);
     } else {
         $eventData = new CRM_Civirules_EventData_Post($entity, $objectId, $data);
     }
     return $eventData;
 }