Merge pull request #15815 from artfulrobot/issue-1108-fix-unsubscribe
[civicrm-core.git] / CRM / Activity / Form / Task / RemoveFromTag.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 remove tags of contact(s).
20 */
21class CRM_Activity_Form_Task_RemoveFromTag extends CRM_Activity_Form_Task {
22
23 /**
100fef9d 24 * Name of the tag
f70ca446
PN
25 *
26 * @var string
27 */
28 protected $_name;
29
30 /**
100fef9d 31 * All the tags in the system
f70ca446
PN
32 *
33 * @var array
34 */
35 protected $_tags;
36
37 /**
b6c94f42 38 * Build the form object.
f70ca446 39 */
00be9182 40 public function buildQuickForm() {
f70ca446
PN
41 // add select for tag
42 $this->_tags = CRM_Core_BAO_Tag::getTags('civicrm_activity');
43 foreach ($this->_tags as $tagID => $tagName) {
44 $this->_tagElement = &$this->addElement('checkbox', "tag[$tagID]", NULL, $tagName);
45 }
46
47 $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_activity');
4dff5e17 48 CRM_Core_Form_Tag::buildQuickForm($this, $parentNames, 'civicrm_activity', NULL, TRUE, FALSE);
f70ca446
PN
49
50 $this->addDefaultButtons(ts('Remove Tags from activities'));
51 }
52
00be9182 53 public function addRules() {
be2fb01f 54 $this->addFormRule(['CRM_Activity_Form_Task_RemoveFromTag', 'formRule']);
f70ca446
PN
55 }
56
ffd93213 57 /**
c490a46a 58 * @param CRM_Core_Form $form
ffd93213
EM
59 * @param $rule
60 *
61 * @return array
62 */
00be9182 63 public static function formRule($form, $rule) {
be2fb01f 64 $errors = [];
f70ca446
PN
65 if (empty($form['tag']) && empty($form['activity_taglist'])) {
66 $errors['_qf_default'] = "Please select atleast one tag.";
67 }
68 return $errors;
69 }
70
71 /**
fe482240 72 * Process the form after the input has been submitted and validated.
f70ca446
PN
73 */
74 public function postProcess() {
75 //get the submitted values in an array
76 $params = $this->controller->exportValues($this->_name);
77
be2fb01f 78 $activityTags = $tagList = [];
f70ca446
PN
79
80 // check if contact tags exists
a7488080 81 if (!empty($params['tag'])) {
f70ca446
PN
82 $activityTags = $params['tag'];
83 }
84
85 // check if tags are selected from taglists
a7488080 86 if (!empty($params['activity_taglist'])) {
f70ca446
PN
87 foreach ($params['activity_taglist'] as $val) {
88 if ($val) {
89 if (is_numeric($val)) {
90 $tagList[$val] = 1;
91 }
92 else {
93 list($label, $tagID) = explode(',', $val);
94 $tagList[$tagID] = 1;
95 }
96 }
97 }
98 }
99 $tagSets = CRM_Core_BAO_Tag::getTagsUsedFor('civicrm_activity', FALSE, TRUE);
100
101 foreach ($tagSets as $key => $value) {
102 $this->_tags[$key] = $value['name'];
103 }
104 // merge contact and taglist tags
105 $allTags = CRM_Utils_Array::crmArrayMerge($activityTags, $tagList);
106
be2fb01f 107 $this->_name = [];
f70ca446
PN
108 foreach ($allTags as $key => $dnc) {
109 $this->_name[] = $this->_tags[$key];
110
424616b8 111 list($total, $removed, $notRemoved) = CRM_Core_BAO_EntityTag::removeEntitiesFromTag($this->_activityHolderIds,
112 $key, 'civicrm_activity', FALSE);
f70ca446 113
be2fb01f
CW
114 $status = [
115 ts('%count activity un-tagged', [
c5c263ca
AH
116 'count' => $removed,
117 'plural' => '%count activities un-tagged',
be2fb01f
CW
118 ]),
119 ];
f70ca446 120 if ($notRemoved) {
be2fb01f 121 $status[] = ts('1 activity already did not have this tag', [
c5c263ca
AH
122 'count' => $notRemoved,
123 'plural' => '%count activities already did not have this tag',
be2fb01f 124 ]);
f70ca446
PN
125 }
126 $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>';
be2fb01f 127 CRM_Core_Session::setStatus($status, ts("Removed Tag <em>%1</em>", [1 => $this->_tags[$key]]), 'success', ['expires' => 0]);
f70ca446
PN
128 }
129 }
96025800 130
f70ca446 131}