Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-08-25-10-57-01
[civicrm-core.git] / CRM / Activity / Form / Task / AddToTag.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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_Activity_Form_Task_AddToTag extends CRM_Activity_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('civicrm_activity');
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_activity');
73
74 CRM_Core_Form_Tag::buildQuickForm($this, $parentNames, 'civicrm_activity');
75
76 $this->addDefaultButtons(ts('Tag Activities'));
77 }
78
79 function addRules() {
80 $this->addFormRule(array('CRM_Activity_Form_Task_AddToTag', 'formRule'));
81 }
82
83 /**
84 * @param $form
85 * @param $rule
86 *
87 * @return array
88 */
89 static function formRule($form, $rule) {
90 $errors = array();
91 if (empty($form['tag']) && empty($form['activity_taglist'])) {
92 $errors['_qf_default'] = ts("Please select at least one tag.");
93 }
94 return $errors;
95 }
96
97 /**
98 * process the form after the input has been submitted and validated
99 *
100 * @access public
101 *
102 * @return void
103 */
104 public function postProcess() {
105 //get the submitted values in an array
106 $params = $this->controller->exportValues($this->_name);
107 $activityTags = $tagList = array();
108
109 // check if contact tags exists
110 if (!empty($params['tag'])) {
111 $activityTags = $params['tag'];
112 }
113
114 // check if tags are selected from taglists
115 if (!empty($params['activity_taglist'])) {
116 foreach ($params['activity_taglist'] as $val) {
117 if ($val) {
118 if (is_numeric($val)) {
119 $tagList[$val] = 1;
120 }
121 else {
122 $tagIDs = explode(',', $val);
123 if (!empty($tagIDs)) {
124 foreach ($tagIDs as $tagID) {
125 if (is_numeric($tagID)) {
126 $tagList[$tagID] = 1;
127 }
128 }
129 }
130 }
131 }
132 }
133 }
134
135 $tagSets = CRM_Core_BAO_Tag::getTagsUsedFor('civicrm_activity', FALSE, TRUE);
136
137 foreach ($tagSets as $key => $value) {
138 $this->_tags[$key] = $value['name'];
139 }
140
141 // merge activity and taglist tags
142 $allTags = CRM_Utils_Array::crmArrayMerge($activityTags, $tagList);
143
144 $this->_name = array();
145 foreach ($allTags as $key => $dnc) {
146 $this->_name[] = $this->_tags[$key];
147
148 list($total, $added, $notAdded) = CRM_Core_BAO_EntityTag::addEntitiesToTag($this->_activityHolderIds, $key, 'civicrm_activity');
149
150 $status = array(ts('%count activities tagged', array('count' => $added, 'plural' => '%count activities tagged')));
151 if ($notAdded) {
152 $status[] = ts('%count activities already had this tag', array('count' => $notAdded, 'plural' => '%count activities already had this tag'));
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 //end of function
160 }
161