Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | ||
18 | /** | |
19 | * This class provides the functionality to remove tags of contact(s). | |
20 | */ | |
21 | class CRM_Contact_Form_Task_RemoveFromTag extends CRM_Contact_Form_Task { | |
22 | ||
23 | /** | |
fe482240 | 24 | * Name of the tag. |
6a488035 TO |
25 | * |
26 | * @var string | |
27 | */ | |
28 | protected $_name; | |
29 | ||
30 | /** | |
fe482240 | 31 | * All the tags in the system. |
6a488035 TO |
32 | * |
33 | * @var array | |
34 | */ | |
35 | protected $_tags; | |
36 | ||
37 | /** | |
fe482240 | 38 | * Build the form object. |
6a488035 | 39 | */ |
00be9182 | 40 | public function buildQuickForm() { |
6a488035 TO |
41 | // add select for tag |
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); | |
45 | } | |
46 | ||
47 | $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_contact'); | |
4dff5e17 | 48 | CRM_Core_Form_Tag::buildQuickForm($this, $parentNames, 'civicrm_contact', NULL, TRUE, FALSE); |
6a488035 TO |
49 | |
50 | $this->addDefaultButtons(ts('Remove Tags from Contacts')); | |
51 | } | |
52 | ||
00be9182 | 53 | public function addRules() { |
be2fb01f | 54 | $this->addFormRule(['CRM_Contact_Form_Task_RemoveFromTag', 'formRule']); |
6a488035 TO |
55 | } |
56 | ||
86538308 | 57 | /** |
c490a46a | 58 | * @param CRM_Core_Form $form |
86538308 EM |
59 | * @param $rule |
60 | * | |
61 | * @return array | |
62 | */ | |
00be9182 | 63 | public static function formRule($form, $rule) { |
be2fb01f | 64 | $errors = []; |
6a488035 TO |
65 | if (empty($form['tag']) && empty($form['contact_taglist'])) { |
66 | $errors['_qf_default'] = "Please select atleast one tag."; | |
67 | } | |
68 | return $errors; | |
69 | } | |
70 | ||
71 | /** | |
fe482240 | 72 | * Process the form after the input has been submitted and validated. |
6a488035 TO |
73 | */ |
74 | public function postProcess() { | |
75 | //get the submitted values in an array | |
76 | $params = $this->controller->exportValues($this->_name); | |
77 | ||
be2fb01f | 78 | $contactTags = $tagList = []; |
6a488035 TO |
79 | |
80 | // check if contact tags exists | |
a7488080 | 81 | if (!empty($params['tag'])) { |
6a488035 TO |
82 | $contactTags = $params['tag']; |
83 | } | |
84 | ||
85 | // check if tags are selected from taglists | |
a7488080 | 86 | if (!empty($params['contact_taglist'])) { |
6a488035 TO |
87 | foreach ($params['contact_taglist'] as $val) { |
88 | if ($val) { | |
89 | if (is_numeric($val)) { | |
90 | $tagList[$val] = 1; | |
91 | } | |
92 | else { | |
93 | list($label, $tagID) = explode(',', $val); | |
94 | $tagList[$tagID] = 1; | |
95 | } | |
96 | } | |
97 | } | |
98 | } | |
99 | $tagSets = CRM_Core_BAO_Tag::getTagsUsedFor('civicrm_contact', FALSE, TRUE); | |
100 | ||
101 | foreach ($tagSets as $key => $value) { | |
102 | $this->_tags[$key] = $value['name']; | |
103 | } | |
104 | // merge contact and taglist tags | |
105 | $allTags = CRM_Utils_Array::crmArrayMerge($contactTags, $tagList); | |
106 | ||
be2fb01f | 107 | $this->_name = []; |
6a488035 TO |
108 | foreach ($allTags as $key => $dnc) { |
109 | $this->_name[] = $this->_tags[$key]; | |
110 | ||
424616b8 | 111 | list($total, $removed, $notRemoved) = CRM_Core_BAO_EntityTag::removeEntitiesFromTag($this->_contactIds, $key, |
112 | 'civicrm_contact', FALSE); | |
6a488035 | 113 | |
be2fb01f CW |
114 | $status = [ |
115 | ts('%count contact un-tagged', [ | |
69078420 SL |
116 | 'count' => $removed, |
117 | 'plural' => '%count contacts un-tagged', | |
118 | ]), | |
be2fb01f | 119 | ]; |
6a488035 | 120 | if ($notRemoved) { |
be2fb01f | 121 | $status[] = ts('1 contact already did not have this tag', [ |
69078420 SL |
122 | 'count' => $notRemoved, |
123 | 'plural' => '%count contacts already did not have this tag', | |
124 | ]); | |
6a488035 TO |
125 | } |
126 | $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>'; | |
be2fb01f | 127 | CRM_Core_Session::setStatus($status, ts("Removed Tag <em>%1</em>", [1 => $this->_tags[$key]]), 'success', ['expires' => 0]); |
6a488035 TO |
128 | } |
129 | } | |
96025800 | 130 | |
6a488035 | 131 | } |