Merge pull request #17641 from MegaphoneJon/core-1590
[civicrm-core.git] / CRM / Contact / 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 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.
24 */
25 class CRM_Contact_Form_Task_AddToTag extends CRM_Contact_Form_Task {
26
27 /**
28 * Name of the tag.
29 *
30 * @var string
31 */
32 protected $_name;
33
34 /**
35 * All the tags in the system.
36 *
37 * @var array
38 */
39 protected $_tags;
40
41 /**
42 * Build the form object.
43 */
44 public function buildQuickForm() {
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
58 public function addRules() {
59 $this->addFormRule(['CRM_Contact_Form_Task_AddToTag', 'formRule']);
60 }
61
62 /**
63 * @param CRM_Core_Form $form
64 * @param $rule
65 *
66 * @return array
67 */
68 public static function formRule($form, $rule) {
69 $errors = [];
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 /**
77 * Process the form after the input has been submitted and validated.
78 */
79 public function postProcess() {
80 //get the submitted values in an array
81 $params = $this->controller->exportValues($this->_name);
82 $contactTags = $tagList = [];
83
84 // check if contact tags exists
85 if (!empty($params['tag'])) {
86 $contactTags = $params['tag'];
87 }
88
89 // check if tags are selected from taglists
90 if (!empty($params['contact_taglist'])) {
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
119 $this->_name = [];
120 foreach ($allTags as $key => $dnc) {
121 $this->_name[] = $this->_tags[$key];
122
123 list($total, $added, $notAdded) = CRM_Core_BAO_EntityTag::addEntitiesToTag($this->_contactIds, $key,
124 'civicrm_contact', FALSE);
125
126 $status = [ts('%count contact tagged', ['count' => $added, 'plural' => '%count contacts tagged'])];
127 if ($notAdded) {
128 $status[] = ts('%count contact already had this tag', [
129 'count' => $notAdded,
130 'plural' => '%count contacts already had this tag',
131 ]);
132 }
133 $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>';
134 CRM_Core_Session::setStatus($status, ts("Added Tag <em>%1</em>", [1 => $this->_tags[$key]]), 'success', ['expires' => 0]);
135 }
136
137 }
138
139 }