-- CRM-12682 applied patch and made some modifications
[civicrm-core.git] / CRM / Contact / Form / Task / AddToTag.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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
59 *
60 * @access public
61 *
62 * @return void
63 */
64 function buildQuickForm() {
65 // add select for tag
66 $this->_tags = CRM_Core_BAO_Tag::getTags();
67
68 foreach ($this->_tags as $tagID => $tagName) {
69 $this->_tagElement = &$this->addElement('checkbox', "tag[$tagID]", NULL, $tagName);
70 }
71
72 $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_contact');
73 CRM_Core_Form_Tag::buildQuickForm($this, $parentNames, 'civicrm_contact');
74
75 $this->addDefaultButtons(ts('Tag Contacts'));
76 }
77
78 function addRules() {
79 $this->addFormRule(array('CRM_Contact_Form_Task_AddToTag', 'formRule'));
80 }
81
82 static function formRule($form, $rule) {
83 $errors = array();
84 if (empty($form['tag']) && empty($form['contact_taglist'])) {
85 $errors['_qf_default'] = ts("Please select at least one tag.");
86 }
87 return $errors;
88 }
89
90 /**
91 * process the form after the input has been submitted and validated
92 *
93 * @access public
94 *
95 * @return None
96 */
97 public function postProcess() {
98 //get the submitted values in an array
99 $params = $this->controller->exportValues($this->_name);
100 $contactTags = $tagList = array();
101
102 // check if contact tags exists
103 if (CRM_Utils_Array::value('tag', $params)) {
104 $contactTags = $params['tag'];
105 }
106
107 // check if tags are selected from taglists
108 if (CRM_Utils_Array::value('contact_taglist', $params)) {
109 foreach ($params['contact_taglist'] as $val) {
110 if ($val) {
111 if (is_numeric($val)) {
112 $tagList[$val] = 1;
113 }
114 else {
115 $tagIDs = explode(',', $val);
116 if (!empty($tagIDs)) {
117 foreach ($tagIDs as $tagID) {
118 if (is_numeric($tagID)) {
119 $tagList[$tagID] = 1;
120 }
121 }
122 }
123 }
124 }
125 }
126 }
127
128 $tagSets = CRM_Core_BAO_Tag::getTagsUsedFor('civicrm_contact', FALSE, TRUE);
129
130 foreach ($tagSets as $key => $value) {
131 $this->_tags[$key] = $value['name'];
132 }
133
134 // merge contact and taglist tags
135 $allTags = CRM_Utils_Array::crmArrayMerge($contactTags, $tagList);
136
137 $this->_name = array();
138 foreach ($allTags as $key => $dnc) {
139 $this->_name[] = $this->_tags[$key];
140
141 list($total, $added, $notAdded) = CRM_Core_BAO_EntityTag::addEntitiesToTag($this->_contactIds, $key);
142
143 $status = array(ts('%count contact tagged', array('count' => $added, 'plural' => '%count contacts tagged')));
144 if ($notAdded) {
145 $status[] = ts('%count contact already had this tag', array('count' => $notAdded, 'plural' => '%count contacts already had this tag'));
146 }
147 $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>';
148 CRM_Core_Session::setStatus($status, ts("Added Tag <em>%1</em>", array(1 => $this->_tags[$key])), 'success', array('expires' => 0));
149 }
150
151 }
152 //end of function
153 }
154