Merge pull request #18631 from eileenmcnaughton/ppp
[civicrm-core.git] / CRM / Contact / Form / Task / AddToTag.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
5a409b50 19 * This class provides the functionality to delete a group of contacts.
20 *
21 * This class provides functionality for the actual addition of contacts to groups.
22 *
23 * Wow is that copy & paste gone wrong or what? What does this class do? Anyone, anyone.
6a488035
TO
24 */
25class CRM_Contact_Form_Task_AddToTag extends CRM_Contact_Form_Task {
26
27 /**
fe482240 28 * Name of the tag.
6a488035
TO
29 *
30 * @var string
31 */
32 protected $_name;
33
34 /**
fe482240 35 * All the tags in the system.
6a488035
TO
36 *
37 * @var array
38 */
39 protected $_tags;
40
41 /**
fe482240 42 * Build the form object.
6a488035 43 */
00be9182 44 public function buildQuickForm() {
6a488035
TO
45 // add select for tag
46 $this->_tags = CRM_Core_BAO_Tag::getTags();
47
48 foreach ($this->_tags as $tagID => $tagName) {
49 $this->_tagElement = &$this->addElement('checkbox', "tag[$tagID]", NULL, $tagName);
50 }
51
52 $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_contact');
53 CRM_Core_Form_Tag::buildQuickForm($this, $parentNames, 'civicrm_contact');
54
55 $this->addDefaultButtons(ts('Tag Contacts'));
56 }
57
00be9182 58 public function addRules() {
be2fb01f 59 $this->addFormRule(['CRM_Contact_Form_Task_AddToTag', 'formRule']);
6a488035
TO
60 }
61
86538308 62 /**
c490a46a 63 * @param CRM_Core_Form $form
86538308
EM
64 * @param $rule
65 *
66 * @return array
67 */
00be9182 68 public static function formRule($form, $rule) {
be2fb01f 69 $errors = [];
6a488035
TO
70 if (empty($form['tag']) && empty($form['contact_taglist'])) {
71 $errors['_qf_default'] = ts("Please select at least one tag.");
72 }
73 return $errors;
74 }
75
76 /**
fe482240 77 * Process the form after the input has been submitted and validated.
6a488035
TO
78 */
79 public function postProcess() {
80 //get the submitted values in an array
81 $params = $this->controller->exportValues($this->_name);
be2fb01f 82 $contactTags = $tagList = [];
6a488035
TO
83
84 // check if contact tags exists
a7488080 85 if (!empty($params['tag'])) {
6a488035
TO
86 $contactTags = $params['tag'];
87 }
88
89 // check if tags are selected from taglists
a7488080 90 if (!empty($params['contact_taglist'])) {
6a488035
TO
91 foreach ($params['contact_taglist'] as $val) {
92 if ($val) {
93 if (is_numeric($val)) {
94 $tagList[$val] = 1;
95 }
96 else {
97 $tagIDs = explode(',', $val);
98 if (!empty($tagIDs)) {
99 foreach ($tagIDs as $tagID) {
100 if (is_numeric($tagID)) {
101 $tagList[$tagID] = 1;
102 }
103 }
104 }
105 }
106 }
107 }
108 }
109
110 $tagSets = CRM_Core_BAO_Tag::getTagsUsedFor('civicrm_contact', FALSE, TRUE);
111
112 foreach ($tagSets as $key => $value) {
113 $this->_tags[$key] = $value['name'];
114 }
115
116 // merge contact and taglist tags
117 $allTags = CRM_Utils_Array::crmArrayMerge($contactTags, $tagList);
118
be2fb01f 119 $this->_name = [];
6a488035
TO
120 foreach ($allTags as $key => $dnc) {
121 $this->_name[] = $this->_tags[$key];
122
424616b8 123 list($total, $added, $notAdded) = CRM_Core_BAO_EntityTag::addEntitiesToTag($this->_contactIds, $key,
124 'civicrm_contact', FALSE);
6a488035 125
be2fb01f 126 $status = [ts('%count contact tagged', ['count' => $added, 'plural' => '%count contacts tagged'])];
6a488035 127 if ($notAdded) {
be2fb01f 128 $status[] = ts('%count contact already had this tag', [
69078420
SL
129 'count' => $notAdded,
130 'plural' => '%count contacts already had this tag',
131 ]);
6a488035
TO
132 }
133 $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>';
be2fb01f 134 CRM_Core_Session::setStatus($status, ts("Added Tag <em>%1</em>", [1 => $this->_tags[$key]]), 'success', ['expires' => 0]);
6a488035
TO
135 }
136
137 }
96025800 138
6a488035 139}