Merge pull request #2211 from colemanw/master
[civicrm-core.git] / CRM / Activity / Form / Task / RemoveFromTag.php
CommitLineData
f70ca446
PN
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
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 remove tags of contact(s).
38 */
39class CRM_Activity_Form_Task_RemoveFromTag extends CRM_Activity_Form_Task {
40
41 /**
42 * name of the tag
43 *
44 * @var string
45 */
46 protected $_name;
47
48 /**
49 * all the tags in the system
50 *
51 * @var array
52 */
53 protected $_tags;
54
55 /**
56 * Build the form
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('civicrm_activity');
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_activity');
70 CRM_Core_Form_Tag::buildQuickForm($this, $parentNames, 'civicrm_activity', NULL, TRUE, FALSE, TRUE);
71
72 $this->addDefaultButtons(ts('Remove Tags from activities'));
73 }
74
75 function addRules() {
76 $this->addFormRule(array('CRM_Activity_Form_Task_RemoveFromTag', 'formRule'));
77 }
78
79 static function formRule($form, $rule) {
80 $errors = array();
81 if (empty($form['tag']) && empty($form['activity_taglist'])) {
82 $errors['_qf_default'] = "Please select atleast one tag.";
83 }
84 return $errors;
85 }
86
87 /**
88 * process the form after the input has been submitted and validated
89 *
90 * @access public
91 *
92 * @return None
93 */
94 public function postProcess() {
95 //get the submitted values in an array
96 $params = $this->controller->exportValues($this->_name);
97
98 $activityTags = $tagList = array();
99
100 // check if contact tags exists
101 if (CRM_Utils_Array::value('tag', $params)) {
102 $activityTags = $params['tag'];
103 }
104
105 // check if tags are selected from taglists
106 if (CRM_Utils_Array::value('activity_taglist', $params)) {
107 foreach ($params['activity_taglist'] as $val) {
108 if ($val) {
109 if (is_numeric($val)) {
110 $tagList[$val] = 1;
111 }
112 else {
113 list($label, $tagID) = explode(',', $val);
114 $tagList[$tagID] = 1;
115 }
116 }
117 }
118 }
119 $tagSets = CRM_Core_BAO_Tag::getTagsUsedFor('civicrm_activity', FALSE, TRUE);
120
121 foreach ($tagSets as $key => $value) {
122 $this->_tags[$key] = $value['name'];
123 }
124 // merge contact and taglist tags
125 $allTags = CRM_Utils_Array::crmArrayMerge($activityTags, $tagList);
126
127 $this->_name = array();
128 foreach ($allTags as $key => $dnc) {
129 $this->_name[] = $this->_tags[$key];
130
131 list($total, $removed, $notRemoved) = CRM_Core_BAO_EntityTag::removeEntitiesFromTag($this->_activityHolderIds, $key, 'civicrm_activity');
132
133 $status = array(ts('%count activities un-tagged', array('count' => $removed, 'plural' => '%count activities un-tagged')));
134 if ($notRemoved) {
135 $status[] = ts('1 activity already did not have this tag', array('count' => $notRemoved, 'plural' => '%count activities already did not have this tag'));
136 }
137 $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>';
138 CRM_Core_Session::setStatus($status, ts("Removed Tag <em>%1</em>", array(1 => $this->_tags[$key])), 'success', array('expires' => 0));
139 }
140 }
141 //end of function
142}
143