Merge pull request #17641 from MegaphoneJon/core-1590
[civicrm-core.git] / CRM / Contact / Form / Task / RemoveFromTag.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
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 /**
24 * Name of the tag.
25 *
26 * @var string
27 */
28 protected $_name;
29
30 /**
31 * All the tags in the system.
32 *
33 * @var array
34 */
35 protected $_tags;
36
37 /**
38 * Build the form object.
39 */
40 public function buildQuickForm() {
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');
48 CRM_Core_Form_Tag::buildQuickForm($this, $parentNames, 'civicrm_contact', NULL, TRUE, FALSE);
49
50 $this->addDefaultButtons(ts('Remove Tags from Contacts'));
51 }
52
53 public function addRules() {
54 $this->addFormRule(['CRM_Contact_Form_Task_RemoveFromTag', 'formRule']);
55 }
56
57 /**
58 * @param CRM_Core_Form $form
59 * @param $rule
60 *
61 * @return array
62 */
63 public static function formRule($form, $rule) {
64 $errors = [];
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 /**
72 * Process the form after the input has been submitted and validated.
73 */
74 public function postProcess() {
75 //get the submitted values in an array
76 $params = $this->controller->exportValues($this->_name);
77
78 $contactTags = $tagList = [];
79
80 // check if contact tags exists
81 if (!empty($params['tag'])) {
82 $contactTags = $params['tag'];
83 }
84
85 // check if tags are selected from taglists
86 if (!empty($params['contact_taglist'])) {
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
107 $this->_name = [];
108 foreach ($allTags as $key => $dnc) {
109 $this->_name[] = $this->_tags[$key];
110
111 list($total, $removed, $notRemoved) = CRM_Core_BAO_EntityTag::removeEntitiesFromTag($this->_contactIds, $key,
112 'civicrm_contact', FALSE);
113
114 $status = [
115 ts('%count contact un-tagged', [
116 'count' => $removed,
117 'plural' => '%count contacts un-tagged',
118 ]),
119 ];
120 if ($notRemoved) {
121 $status[] = ts('1 contact already did not have this tag', [
122 'count' => $notRemoved,
123 'plural' => '%count contacts already did not have this tag',
124 ]);
125 }
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]);
128 }
129 }
130
131 }