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