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