Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
fa938177 | 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
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 | |
fa938177 | 31 | * @copyright CiviCRM LLC (c) 2004-2016 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
5a409b50 | 35 | * This class provides the functionality to delete a group of contacts. |
36 | * | |
37 | * This class provides functionality for the actual addition of contacts to groups. | |
38 | * | |
39 | * Wow is that copy & paste gone wrong or what? What does this class do? Anyone, anyone. | |
6a488035 TO |
40 | */ |
41 | class CRM_Contact_Form_Task_AddToTag extends CRM_Contact_Form_Task { | |
42 | ||
43 | /** | |
fe482240 | 44 | * Name of the tag. |
6a488035 TO |
45 | * |
46 | * @var string | |
47 | */ | |
48 | protected $_name; | |
49 | ||
50 | /** | |
fe482240 | 51 | * All the tags in the system. |
6a488035 TO |
52 | * |
53 | * @var array | |
54 | */ | |
55 | protected $_tags; | |
56 | ||
57 | /** | |
fe482240 | 58 | * Build the form object. |
6a488035 | 59 | */ |
00be9182 | 60 | public function buildQuickForm() { |
6a488035 TO |
61 | // add select for tag |
62 | $this->_tags = CRM_Core_BAO_Tag::getTags(); | |
63 | ||
64 | foreach ($this->_tags as $tagID => $tagName) { | |
65 | $this->_tagElement = &$this->addElement('checkbox', "tag[$tagID]", NULL, $tagName); | |
66 | } | |
67 | ||
68 | $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_contact'); | |
69 | CRM_Core_Form_Tag::buildQuickForm($this, $parentNames, 'civicrm_contact'); | |
70 | ||
71 | $this->addDefaultButtons(ts('Tag Contacts')); | |
72 | } | |
73 | ||
00be9182 | 74 | public function addRules() { |
6a488035 TO |
75 | $this->addFormRule(array('CRM_Contact_Form_Task_AddToTag', 'formRule')); |
76 | } | |
77 | ||
86538308 | 78 | /** |
c490a46a | 79 | * @param CRM_Core_Form $form |
86538308 EM |
80 | * @param $rule |
81 | * | |
82 | * @return array | |
83 | */ | |
00be9182 | 84 | public static function formRule($form, $rule) { |
6a488035 TO |
85 | $errors = array(); |
86 | if (empty($form['tag']) && empty($form['contact_taglist'])) { | |
87 | $errors['_qf_default'] = ts("Please select at least one tag."); | |
88 | } | |
89 | return $errors; | |
90 | } | |
91 | ||
92 | /** | |
fe482240 | 93 | * Process the form after the input has been submitted and validated. |
6a488035 TO |
94 | */ |
95 | public function postProcess() { | |
96 | //get the submitted values in an array | |
97 | $params = $this->controller->exportValues($this->_name); | |
98 | $contactTags = $tagList = array(); | |
99 | ||
100 | // check if contact tags exists | |
a7488080 | 101 | if (!empty($params['tag'])) { |
6a488035 TO |
102 | $contactTags = $params['tag']; |
103 | } | |
104 | ||
105 | // check if tags are selected from taglists | |
a7488080 | 106 | if (!empty($params['contact_taglist'])) { |
6a488035 TO |
107 | foreach ($params['contact_taglist'] as $val) { |
108 | if ($val) { | |
109 | if (is_numeric($val)) { | |
110 | $tagList[$val] = 1; | |
111 | } | |
112 | else { | |
113 | $tagIDs = explode(',', $val); | |
114 | if (!empty($tagIDs)) { | |
115 | foreach ($tagIDs as $tagID) { | |
116 | if (is_numeric($tagID)) { | |
117 | $tagList[$tagID] = 1; | |
118 | } | |
119 | } | |
120 | } | |
121 | } | |
122 | } | |
123 | } | |
124 | } | |
125 | ||
126 | $tagSets = CRM_Core_BAO_Tag::getTagsUsedFor('civicrm_contact', FALSE, TRUE); | |
127 | ||
128 | foreach ($tagSets as $key => $value) { | |
129 | $this->_tags[$key] = $value['name']; | |
130 | } | |
131 | ||
132 | // merge contact and taglist tags | |
133 | $allTags = CRM_Utils_Array::crmArrayMerge($contactTags, $tagList); | |
134 | ||
135 | $this->_name = array(); | |
136 | foreach ($allTags as $key => $dnc) { | |
137 | $this->_name[] = $this->_tags[$key]; | |
138 | ||
424616b8 | 139 | list($total, $added, $notAdded) = CRM_Core_BAO_EntityTag::addEntitiesToTag($this->_contactIds, $key, |
140 | 'civicrm_contact', FALSE); | |
6a488035 TO |
141 | |
142 | $status = array(ts('%count contact tagged', array('count' => $added, 'plural' => '%count contacts tagged'))); | |
143 | if ($notAdded) { | |
353ffa53 TO |
144 | $status[] = ts('%count contact already had this tag', array( |
145 | 'count' => $notAdded, | |
af9b09df | 146 | 'plural' => '%count contacts already had this tag', |
353ffa53 | 147 | )); |
6a488035 TO |
148 | } |
149 | $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>'; | |
150 | CRM_Core_Session::setStatus($status, ts("Added Tag <em>%1</em>", array(1 => $this->_tags[$key])), 'success', array('expires' => 0)); | |
151 | } | |
152 | ||
153 | } | |
96025800 | 154 | |
6a488035 | 155 | } |