/**
 *
 * @param <type> $params
 * @param <type> $op
 *
 * @return <type>
 */
function civicrm_entity_tag_common(&$params, $op = 'add')
{
    $entityIDs = array();
    $tagsIDs = array();
    $entityTable = 'civicrm_contact';
    if (is_array($params)) {
        foreach ($params as $n => $v) {
            if (substr($n, 0, 10) == 'contact_id' || substr($n, 0, 9) == 'entity_id') {
                $entityIDs[] = $v;
            } elseif (substr($n, 0, 6) == 'tag_id') {
                $tagIDs[] = $v;
            } elseif (substr($n, 0, 12) == 'entity_table') {
                $entityTable = $v;
            }
        }
    }
    if (empty($entityIDs)) {
        return civicrm_create_error(ts('contact_id is a required field'));
    }
    if (empty($tagIDs)) {
        return civicrm_create_error(ts('tag_id is a required field'));
    }
    require_once 'CRM/Core/BAO/EntityTag.php';
    $values = array('is_error' => 0);
    if ($op == 'add') {
        $values['total_count'] = $values['added'] = $values['not_added'] = 0;
        foreach ($tagIDs as $tagID) {
            list($te, $a, $na) = CRM_Core_BAO_EntityTag::addEntitiesToTag($entityIDs, $tagID, $entityTable);
            $values['total_count'] += $te;
            $values['added'] += $a;
            $values['not_added'] += $na;
        }
    } else {
        $values['total_count'] = $values['removed'] = $values['not_removed'] = 0;
        foreach ($tagIDs as $tagID) {
            list($te, $r, $nr) = CRM_Core_BAO_EntityTag::removeEntitiesFromTag($entityIDs, $tagID, $entityTable);
            $values['total_count'] += $te;
            $values['removed'] += $r;
            $values['not_removed'] += $nr;
        }
    }
    return $values;
}
Exemple #2
0
/**
 * Helper function for formatting tags (part of api v2 legacy).
 *
 * @param array $params
 * @param string $op
 *
 * @return array
 */
function _civicrm_api3_entity_tag_common($params, $op = 'add')
{
    $entityIDs = array();
    $entityTable = 'civicrm_contact';
    if (is_array($params)) {
        foreach ($params as $n => $v) {
            if (substr($n, 0, 10) == 'contact_id' || substr($n, 0, 9) == 'entity_id') {
                $entityIDs[] = $v;
            } elseif (substr($n, 0, 6) == 'tag_id') {
                $tagIDs[] = $v;
            } elseif (substr($n, 0, 12) == 'entity_table') {
                $entityTable = $v;
            }
        }
    }
    if (empty($entityIDs)) {
        return civicrm_api3_create_error('contact_id is a required field');
    }
    if (empty($tagIDs)) {
        if ($op == 'remove') {
            $tagIDs = array_keys(CRM_Core_BAO_EntityTag::getContactTags($entityIDs[0]));
        } else {
            return civicrm_api3_create_error('tag_id is a required field');
        }
    }
    $values = array('is_error' => 0);
    if ($op == 'add') {
        $values['total_count'] = $values['added'] = $values['not_added'] = 0;
        foreach ($tagIDs as $tagID) {
            list($te, $a, $na) = CRM_Core_BAO_EntityTag::addEntitiesToTag($entityIDs, $tagID, $entityTable, CRM_Utils_Array::value('check_permissions', $params));
            $values['total_count'] += $te;
            $values['added'] += $a;
            $values['not_added'] += $na;
        }
    } else {
        $values['total_count'] = $values['removed'] = $values['not_removed'] = 0;
        foreach ($tagIDs as $tagID) {
            list($te, $r, $nr) = CRM_Core_BAO_EntityTag::removeEntitiesFromTag($entityIDs, $tagID, $entityTable, CRM_Utils_Array::value('check_permissions', $params));
            $values['total_count'] += $te;
            $values['removed'] += $r;
            $values['not_removed'] += $nr;
        }
    }
    if (empty($values['added']) && empty($values['removed'])) {
        $values['is_error'] = 1;
        $values['error_message'] = "Unable to {$op} tags";
    }
    return $values;
}
 /**
  * process the form after the input has been submitted and validated
  *
  * @access public
  *
  * @return void
  */
 public function postProcess()
 {
     //get the submitted values in an array
     $params = $this->controller->exportValues($this->_name);
     $activityTags = $tagList = array();
     // check if contact tags exists
     if (!empty($params['tag'])) {
         $activityTags = $params['tag'];
     }
     // check if tags are selected from taglists
     if (!empty($params['activity_taglist'])) {
         foreach ($params['activity_taglist'] as $val) {
             if ($val) {
                 if (is_numeric($val)) {
                     $tagList[$val] = 1;
                 } else {
                     list($label, $tagID) = explode(',', $val);
                     $tagList[$tagID] = 1;
                 }
             }
         }
     }
     $tagSets = CRM_Core_BAO_Tag::getTagsUsedFor('civicrm_activity', FALSE, TRUE);
     foreach ($tagSets as $key => $value) {
         $this->_tags[$key] = $value['name'];
     }
     // merge contact and taglist tags
     $allTags = CRM_Utils_Array::crmArrayMerge($activityTags, $tagList);
     $this->_name = array();
     foreach ($allTags as $key => $dnc) {
         $this->_name[] = $this->_tags[$key];
         list($total, $removed, $notRemoved) = CRM_Core_BAO_EntityTag::removeEntitiesFromTag($this->_activityHolderIds, $key, 'civicrm_activity');
         $status = array(ts('%count activities un-tagged', array('count' => $removed, 'plural' => '%count activities un-tagged')));
         if ($notRemoved) {
             $status[] = ts('1 activity already did not have this tag', array('count' => $notRemoved, 'plural' => '%count activities already did not have this tag'));
         }
         $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>';
         CRM_Core_Session::setStatus($status, ts("Removed Tag <em>%1</em>", array(1 => $this->_tags[$key])), 'success', array('expires' => 0));
     }
 }
 /**
  * process the form after the input has been submitted and validated
  *
  * @access public
  *
  * @return None
  */
 public function postProcess()
 {
     //get the submitted values in an array
     $params = $this->controller->exportValues($this->_name);
     $contactTags = $tagList = array();
     // check if contact tags exists
     if (CRM_Utils_Array::value('tag', $params)) {
         $contactTags = $params['tag'];
     }
     // check if tags are selected from taglists
     if (CRM_Utils_Array::value('contact_taglist', $params)) {
         foreach ($params['contact_taglist'] as $val) {
             if ($val) {
                 if (is_numeric($val)) {
                     $tagList[$val] = 1;
                 } else {
                     list($label, $tagID) = explode(',', $val);
                     $tagList[$tagID] = 1;
                 }
             }
         }
     }
     $tagSets = CRM_Core_BAO_Tag::getTagsUsedFor('civicrm_contact', FALSE, TRUE);
     foreach ($tagSets as $key => $value) {
         $this->_tags[$key] = $value['name'];
     }
     // merge contact and taglist tags
     $allTags = CRM_Utils_Array::crmArrayMerge($contactTags, $tagList);
     $this->_name = array();
     foreach ($allTags as $key => $dnc) {
         $this->_name[] = $this->_tags[$key];
         list($total, $removed, $notRemoved) = CRM_Core_BAO_EntityTag::removeEntitiesFromTag($this->_contactIds, $key);
         $status = array('Contact(s) tagged as: ' . implode(',', $this->_name), 'Total Selected Contact(s): ' . $total);
     }
     if ($removed) {
         $status[] = 'Total Contact(s) to be  removed from tag: ' . $removed;
     }
     if ($notRemoved) {
         $status[] = 'Total Contact(s) already removed: ' . $notRemoved;
     }
     CRM_Core_Session::setStatus($status);
 }