Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
35 | * This class provides the functionality to remove tags of contact(s). | |
36 | */ | |
37 | class CRM_Contact_Form_Task_RemoveFromTag extends CRM_Contact_Form_Task { | |
38 | ||
39 | /** | |
fe482240 | 40 | * Name of the tag. |
6a488035 TO |
41 | * |
42 | * @var string | |
43 | */ | |
44 | protected $_name; | |
45 | ||
46 | /** | |
fe482240 | 47 | * All the tags in the system. |
6a488035 TO |
48 | * |
49 | * @var array | |
50 | */ | |
51 | protected $_tags; | |
52 | ||
53 | /** | |
fe482240 | 54 | * Build the form object. |
6a488035 | 55 | */ |
00be9182 | 56 | public function buildQuickForm() { |
6a488035 TO |
57 | // add select for tag |
58 | $this->_tags = CRM_Core_BAO_Tag::getTags(); | |
59 | foreach ($this->_tags as $tagID => $tagName) { | |
60 | $this->_tagElement = &$this->addElement('checkbox', "tag[$tagID]", NULL, $tagName); | |
61 | } | |
62 | ||
63 | $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_contact'); | |
4dff5e17 | 64 | CRM_Core_Form_Tag::buildQuickForm($this, $parentNames, 'civicrm_contact', NULL, TRUE, FALSE); |
6a488035 TO |
65 | |
66 | $this->addDefaultButtons(ts('Remove Tags from Contacts')); | |
67 | } | |
68 | ||
00be9182 | 69 | public function addRules() { |
be2fb01f | 70 | $this->addFormRule(['CRM_Contact_Form_Task_RemoveFromTag', 'formRule']); |
6a488035 TO |
71 | } |
72 | ||
86538308 | 73 | /** |
c490a46a | 74 | * @param CRM_Core_Form $form |
86538308 EM |
75 | * @param $rule |
76 | * | |
77 | * @return array | |
78 | */ | |
00be9182 | 79 | public static function formRule($form, $rule) { |
be2fb01f | 80 | $errors = []; |
6a488035 TO |
81 | if (empty($form['tag']) && empty($form['contact_taglist'])) { |
82 | $errors['_qf_default'] = "Please select atleast one tag."; | |
83 | } | |
84 | return $errors; | |
85 | } | |
86 | ||
87 | /** | |
fe482240 | 88 | * Process the form after the input has been submitted and validated. |
6a488035 TO |
89 | */ |
90 | public function postProcess() { | |
91 | //get the submitted values in an array | |
92 | $params = $this->controller->exportValues($this->_name); | |
93 | ||
be2fb01f | 94 | $contactTags = $tagList = []; |
6a488035 TO |
95 | |
96 | // check if contact tags exists | |
a7488080 | 97 | if (!empty($params['tag'])) { |
6a488035 TO |
98 | $contactTags = $params['tag']; |
99 | } | |
100 | ||
101 | // check if tags are selected from taglists | |
a7488080 | 102 | if (!empty($params['contact_taglist'])) { |
6a488035 TO |
103 | foreach ($params['contact_taglist'] as $val) { |
104 | if ($val) { | |
105 | if (is_numeric($val)) { | |
106 | $tagList[$val] = 1; | |
107 | } | |
108 | else { | |
109 | list($label, $tagID) = explode(',', $val); | |
110 | $tagList[$tagID] = 1; | |
111 | } | |
112 | } | |
113 | } | |
114 | } | |
115 | $tagSets = CRM_Core_BAO_Tag::getTagsUsedFor('civicrm_contact', FALSE, TRUE); | |
116 | ||
117 | foreach ($tagSets as $key => $value) { | |
118 | $this->_tags[$key] = $value['name']; | |
119 | } | |
120 | // merge contact and taglist tags | |
121 | $allTags = CRM_Utils_Array::crmArrayMerge($contactTags, $tagList); | |
122 | ||
be2fb01f | 123 | $this->_name = []; |
6a488035 TO |
124 | foreach ($allTags as $key => $dnc) { |
125 | $this->_name[] = $this->_tags[$key]; | |
126 | ||
424616b8 | 127 | list($total, $removed, $notRemoved) = CRM_Core_BAO_EntityTag::removeEntitiesFromTag($this->_contactIds, $key, |
128 | 'civicrm_contact', FALSE); | |
6a488035 | 129 | |
be2fb01f CW |
130 | $status = [ |
131 | ts('%count contact un-tagged', [ | |
69078420 SL |
132 | 'count' => $removed, |
133 | 'plural' => '%count contacts un-tagged', | |
134 | ]), | |
be2fb01f | 135 | ]; |
6a488035 | 136 | if ($notRemoved) { |
be2fb01f | 137 | $status[] = ts('1 contact already did not have this tag', [ |
69078420 SL |
138 | 'count' => $notRemoved, |
139 | 'plural' => '%count contacts already did not have this tag', | |
140 | ]); | |
6a488035 TO |
141 | } |
142 | $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>'; | |
be2fb01f | 143 | CRM_Core_Session::setStatus($status, ts("Removed Tag <em>%1</em>", [1 => $this->_tags[$key]]), 'success', ['expires' => 0]); |
6a488035 TO |
144 | } |
145 | } | |
96025800 | 146 | |
6a488035 | 147 | } |