Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class provides the functionality to remove tags of contact(s). | |
38 | */ | |
39 | class CRM_Contact_Form_Task_RemoveFromTag extends CRM_Contact_Form_Task { | |
40 | ||
41 | /** | |
100fef9d | 42 | * Name of the tag |
6a488035 TO |
43 | * |
44 | * @var string | |
45 | */ | |
46 | protected $_name; | |
47 | ||
48 | /** | |
100fef9d | 49 | * All the tags in the system |
6a488035 TO |
50 | * |
51 | * @var array | |
52 | */ | |
53 | protected $_tags; | |
54 | ||
55 | /** | |
c490a46a | 56 | * Build the form object |
6a488035 TO |
57 | * |
58 | * @access public | |
59 | * | |
60 | * @return void | |
61 | */ | |
62 | function buildQuickForm() { | |
63 | // add select for tag | |
64 | $this->_tags = CRM_Core_BAO_Tag::getTags(); | |
65 | foreach ($this->_tags as $tagID => $tagName) { | |
66 | $this->_tagElement = &$this->addElement('checkbox', "tag[$tagID]", NULL, $tagName); | |
67 | } | |
68 | ||
69 | $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_contact'); | |
4dff5e17 | 70 | CRM_Core_Form_Tag::buildQuickForm($this, $parentNames, 'civicrm_contact', NULL, TRUE, FALSE); |
6a488035 TO |
71 | |
72 | $this->addDefaultButtons(ts('Remove Tags from Contacts')); | |
73 | } | |
74 | ||
75 | function addRules() { | |
76 | $this->addFormRule(array('CRM_Contact_Form_Task_RemoveFromTag', 'formRule')); | |
77 | } | |
78 | ||
86538308 | 79 | /** |
c490a46a | 80 | * @param CRM_Core_Form $form |
86538308 EM |
81 | * @param $rule |
82 | * | |
83 | * @return array | |
84 | */ | |
6a488035 TO |
85 | static function formRule($form, $rule) { |
86 | $errors = array(); | |
87 | if (empty($form['tag']) && empty($form['contact_taglist'])) { | |
88 | $errors['_qf_default'] = "Please select atleast one tag."; | |
89 | } | |
90 | return $errors; | |
91 | } | |
92 | ||
93 | /** | |
100fef9d | 94 | * Process the form after the input has been submitted and validated |
6a488035 TO |
95 | * |
96 | * @access public | |
97 | * | |
355ba699 | 98 | * @return void |
6a488035 TO |
99 | */ |
100 | public function postProcess() { | |
101 | //get the submitted values in an array | |
102 | $params = $this->controller->exportValues($this->_name); | |
103 | ||
104 | $contactTags = $tagList = array(); | |
105 | ||
106 | // check if contact tags exists | |
a7488080 | 107 | if (!empty($params['tag'])) { |
6a488035 TO |
108 | $contactTags = $params['tag']; |
109 | } | |
110 | ||
111 | // check if tags are selected from taglists | |
a7488080 | 112 | if (!empty($params['contact_taglist'])) { |
6a488035 TO |
113 | foreach ($params['contact_taglist'] as $val) { |
114 | if ($val) { | |
115 | if (is_numeric($val)) { | |
116 | $tagList[$val] = 1; | |
117 | } | |
118 | else { | |
119 | list($label, $tagID) = explode(',', $val); | |
120 | $tagList[$tagID] = 1; | |
121 | } | |
122 | } | |
123 | } | |
124 | } | |
125 | $tagSets = CRM_Core_BAO_Tag::getTagsUsedFor('civicrm_contact', FALSE, TRUE); | |
126 | ||
127 | foreach ($tagSets as $key => $value) { | |
128 | $this->_tags[$key] = $value['name']; | |
129 | } | |
130 | // merge contact and taglist tags | |
131 | $allTags = CRM_Utils_Array::crmArrayMerge($contactTags, $tagList); | |
132 | ||
133 | $this->_name = array(); | |
134 | foreach ($allTags as $key => $dnc) { | |
135 | $this->_name[] = $this->_tags[$key]; | |
136 | ||
137 | list($total, $removed, $notRemoved) = CRM_Core_BAO_EntityTag::removeEntitiesFromTag($this->_contactIds, $key); | |
138 | ||
139 | $status = array(ts('%count contact un-tagged', array('count' => $removed, 'plural' => '%count contacts un-tagged'))); | |
140 | if ($notRemoved) { | |
141 | $status[] = ts('1 contact already did not have this tag', array('count' => $notRemoved, 'plural' => '%count contacts already did not have this tag')); | |
142 | } | |
143 | $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>'; | |
144 | CRM_Core_Session::setStatus($status, ts("Removed Tag <em>%1</em>", array(1 => $this->_tags[$key])), 'success', array('expires' => 0)); | |
145 | } | |
146 | } | |
6a488035 TO |
147 | } |
148 |