3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class provides the functionality to delete a group of contacts.
21 * This class provides functionality for the actual addition of contacts to groups.
23 * Wow is that copy & paste gone wrong or what? What does this class do? Anyone, anyone.
25 class CRM_Contact_Form_Task_AddToTag
extends CRM_Contact_Form_Task
{
35 * All the tags in the system.
42 * Build the form object.
44 public function buildQuickForm() {
46 $this->_tags
= CRM_Core_BAO_Tag
::getTags();
48 foreach ($this->_tags
as $tagID => $tagName) {
49 $this->_tagElement
= &$this->addElement('checkbox', "tag[$tagID]", NULL, $tagName);
52 $parentNames = CRM_Core_BAO_Tag
::getTagSet('civicrm_contact');
53 CRM_Core_Form_Tag
::buildQuickForm($this, $parentNames, 'civicrm_contact');
55 $this->addDefaultButtons(ts('Tag Contacts'));
58 public function addRules() {
59 $this->addFormRule(['CRM_Contact_Form_Task_AddToTag', 'formRule']);
63 * @param CRM_Core_Form $form
68 public static function formRule($form, $rule) {
70 if (empty($form['tag']) && empty($form['contact_taglist'])) {
71 $errors['_qf_default'] = ts("Please select at least one tag.");
77 * Process the form after the input has been submitted and validated.
79 public function postProcess() {
80 //get the submitted values in an array
81 $params = $this->controller
->exportValues($this->_name
);
82 $contactTags = $tagList = [];
84 // check if contact tags exists
85 if (!empty($params['tag'])) {
86 $contactTags = $params['tag'];
89 // check if tags are selected from taglists
90 if (!empty($params['contact_taglist'])) {
91 foreach ($params['contact_taglist'] as $val) {
93 if (is_numeric($val)) {
97 $tagIDs = explode(',', $val);
98 if (!empty($tagIDs)) {
99 foreach ($tagIDs as $tagID) {
100 if (is_numeric($tagID)) {
101 $tagList[$tagID] = 1;
110 $tagSets = CRM_Core_BAO_Tag
::getTagsUsedFor('civicrm_contact', FALSE, TRUE);
112 foreach ($tagSets as $key => $value) {
113 $this->_tags
[$key] = $value['name'];
116 // merge contact and taglist tags
117 $allTags = CRM_Utils_Array
::crmArrayMerge($contactTags, $tagList);
120 foreach ($allTags as $key => $dnc) {
121 $this->_name
[] = $this->_tags
[$key];
123 list($total, $added, $notAdded) = CRM_Core_BAO_EntityTag
::addEntitiesToTag($this->_contactIds
, $key,
124 'civicrm_contact', FALSE);
126 $status = [ts('%count contact tagged', ['count' => $added, 'plural' => '%count contacts tagged'])];
128 $status[] = ts('%count contact already had this tag', [
129 'count' => $notAdded,
130 'plural' => '%count contacts already had this tag',
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]);