3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class provides the functionality to remove tags of contact(s).
21 class CRM_Contact_Form_Task_RemoveFromTag
extends CRM_Contact_Form_Task
{
31 * All the tags in the system.
38 * Build the form object.
40 public function buildQuickForm() {
42 $this->_tags
= CRM_Core_BAO_Tag
::getTags();
43 foreach ($this->_tags
as $tagID => $tagName) {
44 $this->_tagElement
= &$this->addElement('checkbox', "tag[$tagID]", NULL, $tagName);
47 $parentNames = CRM_Core_BAO_Tag
::getTagSet('civicrm_contact');
48 CRM_Core_Form_Tag
::buildQuickForm($this, $parentNames, 'civicrm_contact', NULL, TRUE, FALSE);
50 $this->addDefaultButtons(ts('Remove Tags from Contacts'));
53 public function addRules() {
54 $this->addFormRule(['CRM_Contact_Form_Task_RemoveFromTag', 'formRule']);
58 * @param CRM_Core_Form $form
63 public static function formRule($form, $rule) {
65 if (empty($form['tag']) && empty($form['contact_taglist'])) {
66 $errors['_qf_default'] = "Please select atleast one tag.";
72 * Process the form after the input has been submitted and validated.
74 public function postProcess() {
75 //get the submitted values in an array
76 $params = $this->controller
->exportValues($this->_name
);
78 $contactTags = $tagList = [];
80 // check if contact tags exists
81 if (!empty($params['tag'])) {
82 $contactTags = $params['tag'];
85 // check if tags are selected from taglists
86 if (!empty($params['contact_taglist'])) {
87 foreach ($params['contact_taglist'] as $val) {
89 if (is_numeric($val)) {
93 list($label, $tagID) = explode(',', $val);
99 $tagSets = CRM_Core_BAO_Tag
::getTagsUsedFor('civicrm_contact', FALSE, TRUE);
101 foreach ($tagSets as $key => $value) {
102 $this->_tags
[$key] = $value['name'];
104 // merge contact and taglist tags
105 $allTags = CRM_Utils_Array
::crmArrayMerge($contactTags, $tagList);
108 foreach ($allTags as $key => $dnc) {
109 $this->_name
[] = $this->_tags
[$key];
111 list($total, $removed, $notRemoved) = CRM_Core_BAO_EntityTag
::removeEntitiesFromTag($this->_contactIds
, $key,
112 'civicrm_contact', FALSE);
115 ts('%count contact un-tagged', [
117 'plural' => '%count contacts un-tagged',
121 $status[] = ts('1 contact already did not have this tag', [
122 'count' => $notRemoved,
123 'plural' => '%count contacts already did not have this tag',
126 $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>';
127 CRM_Core_Session
::setStatus($status, ts("Removed Tag <em>%1</em>", [1 => $this->_tags
[$key]]), 'success', ['expires' => 0]);