Merge pull request #15820 from seamuslee001/dev_core_183_custom_contribsybnt
[civicrm-core.git] / CRM / Activity / Form / Task / AddToTag.php
CommitLineData
f70ca446
PN
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
f70ca446 5 | |
bc77d7c0
TO
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 |
f70ca446 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
f70ca446
PN
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
f70ca446
PN
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 */
23class CRM_Activity_Form_Task_AddToTag extends CRM_Activity_Form_Task {
24
25 /**
fe482240 26 * Name of the tag.
f70ca446
PN
27 *
28 * @var string
29 */
30 protected $_name;
31
32 /**
fe482240 33 * All the tags in the system.
f70ca446
PN
34 *
35 * @var array
36 */
37 protected $_tags;
38
39 /**
fe482240 40 * Build the form object.
f70ca446 41 */
00be9182 42 public function buildQuickForm() {
f70ca446
PN
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
00be9182 57 public function addRules() {
be2fb01f 58 $this->addFormRule(['CRM_Activity_Form_Task_AddToTag', 'formRule']);
f70ca446
PN
59 }
60
ffd93213 61 /**
c490a46a 62 * @param CRM_Core_Form $form
ffd93213
EM
63 * @param $rule
64 *
65 * @return array
66 */
00be9182 67 public static function formRule($form, $rule) {
be2fb01f 68 $errors = [];
f70ca446
PN
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 /**
fe482240 76 * Process the form after the input has been submitted and validated.
f70ca446
PN
77 */
78 public function postProcess() {
7808aae6 79 // Get the submitted values in an array.
f70ca446 80 $params = $this->controller->exportValues($this->_name);
be2fb01f 81 $activityTags = $tagList = [];
f70ca446
PN
82
83 // check if contact tags exists
a7488080 84 if (!empty($params['tag'])) {
f70ca446
PN
85 $activityTags = $params['tag'];
86 }
87
88 // check if tags are selected from taglists
a7488080 89 if (!empty($params['activity_taglist'])) {
f70ca446
PN
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
be2fb01f 118 $this->_name = [];
f70ca446
PN
119 foreach ($allTags as $key => $dnc) {
120 $this->_name[] = $this->_tags[$key];
121
424616b8 122 list($total, $added, $notAdded) = CRM_Core_BAO_EntityTag::addEntitiesToTag($this->_activityHolderIds, $key,
123 'civicrm_activity', FALSE);
f70ca446 124
be2fb01f 125 $status = [ts('Activity tagged', ['count' => $added, 'plural' => '%count activities tagged'])];
f70ca446 126 if ($notAdded) {
be2fb01f 127 $status[] = ts('1 activity already had this tag', [
ae5ffbb7
TO
128 'count' => $notAdded,
129 'plural' => '%count activities already had this tag',
be2fb01f 130 ]);
f70ca446
PN
131 }
132 $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>';
be2fb01f 133 CRM_Core_Session::setStatus($status, ts("Added Tag <em>%1</em>", [1 => $this->_tags[$key]]), 'success', ['expires' => 0]);
f70ca446
PN
134 }
135
136 }
96025800 137
f70ca446 138}