You are here

function civicrm_entity_civicrm_post in CiviCRM Entity 7.2

Same name and namespace in other branches
  1. 8.3 civicrm_entity.module \civicrm_entity_civicrm_post()
  2. 7 civicrm_entity.module \civicrm_entity_civicrm_post()

Implement the post hook and fire the corresponding rules event.

Parameters

$op:

$object_name:

$object_id:

$object_ref:

File

./civicrm_entity.module, line 3511

Code

function civicrm_entity_civicrm_post($op, $object_name, $object_id, &$object_ref) {
  if (!module_exists('rules')) {
    return;
  }
  $contact_types = array(
    'Individual',
    'Household',
    'Organization',
  );
  if (in_array($object_name, $contact_types)) {
    $object_name = 'Contact';
  }
  $valid_objects = _civicrm_entity_enabled_entities();
  $entity_name = _civicrm_entity_get_entity_name_from_camel($object_name);
  if (!in_array($entity_name, $valid_objects, TRUE)) {
    return;
  }
  $event_name = NULL;
  switch ($op) {
    case 'create':
    case 'edit':
      $event_name = 'civicrm_' . $entity_name . "_{$op}";
      break;

    /*
     *  Here we are checking if the contact entity is being deleted
     *  AND if the contact has sent to the trash already. If the
     *  contact has been sent to the trash, we want to permanently
     *  delete it. If not, we will just fire the "update" event to
     *  let the subscribed rules know that the "is_delete" flag has
     *  changed.
     */
    case 'delete':
      if ($entity_name == 'contact') {
        if ($object_ref->is_deleted == 1) {
          $event_name = 'civicrm_' . $entity_name . "_{$op}";
        }
        else {

          //we are receiving a reference to the contact object before the flag has changed. So we need to force it to change.
          $object_ref->is_deleted = 1;
          $event_name = 'civicrm_' . $entity_name . "_edit";
        }
      }
      else {
        $event_name = 'civicrm_' . $entity_name . "_{$op}";
      }
      break;

    //We need to alert subscribed rules that the "is_delete" flag has changed.
    case 'restore':

      //we are receiving a reference to the contact object before the flag has changed. So we need to force it to change.
      $object_ref->is_deleted = 0;
      $event_name = 'civicrm_' . $entity_name . "_edit";
      break;
    default:
      break;
  }
  if ($entity_name == 'entity_tag') {

    // Argh entity tag is completely non-standard!!!
    // @see CRM-11933
    foreach ($object_ref[0] as $entity_tag) {
      $object = new CRM_Core_BAO_EntityTag();
      $object->entity_id = $entity_tag;
      $object->entity_table = 'civicrm_contact';
      $object->tag_id = $object_id;
      if ($object
        ->find(TRUE)) {

        // This find is probably not necessary but until more testing
        // on the tag create is done I will.
        rules_invoke_event($event_name, $object);
      }
    }
  }
  else {
    if ($event_name) {

      //this addition here causes a custom field value to be saved every time, duplicates and empty values can be made

      /*if(isset($object_ref->id)){
          $entity = entity_load_single('civicrm_' . $entity_name, $object_ref->id);
        }*/
      rules_invoke_event($event_name, $object_ref);

      //rules_invoke_event($event_name, $entity);
    }
  }
}