Merge pull request #15933 from civicrm/5.20
[civicrm-core.git] / CRM / Activity / Form / Task / AddToTag.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class provides the functionality to delete a group of
20 * contacts. This class provides functionality for the actual
21 * addition of contacts to groups.
22 */
23 class CRM_Activity_Form_Task_AddToTag extends CRM_Activity_Form_Task {
24
25 /**
26 * Name of the tag.
27 *
28 * @var string
29 */
30 protected $_name;
31
32 /**
33 * All the tags in the system.
34 *
35 * @var array
36 */
37 protected $_tags;
38
39 /**
40 * Build the form object.
41 */
42 public function buildQuickForm() {
43 // add select for tag
44 $this->_tags = CRM_Core_BAO_Tag::getTags('civicrm_activity');
45
46 foreach ($this->_tags as $tagID => $tagName) {
47 $this->_tagElement = &$this->addElement('checkbox', "tag[$tagID]", NULL, $tagName);
48 }
49
50 $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_activity');
51
52 CRM_Core_Form_Tag::buildQuickForm($this, $parentNames, 'civicrm_activity');
53
54 $this->addDefaultButtons(ts('Tag Activities'));
55 }
56
57 public function addRules() {
58 $this->addFormRule(['CRM_Activity_Form_Task_AddToTag', 'formRule']);
59 }
60
61 /**
62 * @param CRM_Core_Form $form
63 * @param $rule
64 *
65 * @return array
66 */
67 public static function formRule($form, $rule) {
68 $errors = [];
69 if (empty($form['tag']) && empty($form['activity_taglist'])) {
70 $errors['_qf_default'] = ts("Please select at least one tag.");
71 }
72 return $errors;
73 }
74
75 /**
76 * Process the form after the input has been submitted and validated.
77 */
78 public function postProcess() {
79 // Get the submitted values in an array.
80 $params = $this->controller->exportValues($this->_name);
81 $activityTags = $tagList = [];
82
83 // check if contact tags exists
84 if (!empty($params['tag'])) {
85 $activityTags = $params['tag'];
86 }
87
88 // check if tags are selected from taglists
89 if (!empty($params['activity_taglist'])) {
90 foreach ($params['activity_taglist'] as $val) {
91 if ($val) {
92 if (is_numeric($val)) {
93 $tagList[$val] = 1;
94 }
95 else {
96 $tagIDs = explode(',', $val);
97 if (!empty($tagIDs)) {
98 foreach ($tagIDs as $tagID) {
99 if (is_numeric($tagID)) {
100 $tagList[$tagID] = 1;
101 }
102 }
103 }
104 }
105 }
106 }
107 }
108
109 $tagSets = CRM_Core_BAO_Tag::getTagsUsedFor('civicrm_activity', FALSE, TRUE);
110
111 foreach ($tagSets as $key => $value) {
112 $this->_tags[$key] = $value['name'];
113 }
114
115 // merge activity and taglist tags
116 $allTags = CRM_Utils_Array::crmArrayMerge($activityTags, $tagList);
117
118 $this->_name = [];
119 foreach ($allTags as $key => $dnc) {
120 $this->_name[] = $this->_tags[$key];
121
122 list($total, $added, $notAdded) = CRM_Core_BAO_EntityTag::addEntitiesToTag($this->_activityHolderIds, $key,
123 'civicrm_activity', FALSE);
124
125 $status = [ts('Activity tagged', ['count' => $added, 'plural' => '%count activities tagged'])];
126 if ($notAdded) {
127 $status[] = ts('1 activity already had this tag', [
128 'count' => $notAdded,
129 'plural' => '%count activities already had this tag',
130 ]);
131 }
132 $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>';
133 CRM_Core_Session::setStatus($status, ts("Added Tag <em>%1</em>", [1 => $this->_tags[$key]]), 'success', ['expires' => 0]);
134 }
135
136 }
137
138 }