Merge pull request #18216 from eileenmcnaughton/meta
[civicrm-core.git] / CRM / Tag / Form / Merge.php
CommitLineData
edfb12c4
CW
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
edfb12c4 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 |
edfb12c4
CW
9 +--------------------------------------------------------------------+
10 */
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
edfb12c4
CW
16 */
17
18/**
19 * Form for merging tags.
20 */
21class CRM_Tag_Form_Merge extends CRM_Core_Form {
22 protected $_id;
23 protected $_tags;
24
25 public function preProcess() {
26 $this->_id = CRM_Utils_Request::retrieve('id', 'String', $this, FALSE);
27 $this->_id = explode(',', $this->_id);
28 $url = CRM_Utils_System::url('civicrm/tag');
29 if (count($this->_id) < 2) {
9eadcdb7 30 CRM_Core_Error::statusBounce(ts("You must select at least 2 tags for merging."), $url);
edfb12c4 31 }
be2fb01f 32 $tags = civicrm_api3('Tag', 'get', ['id' => ['IN' => $this->_id], 'options' => ['limit' => 0]]);
edfb12c4
CW
33 $this->_tags = $tags['values'];
34 if (count($this->_id) != count($this->_tags)) {
35 CRM_Core_Error::statusBounce(ts("Unknown tag."), $url);
36 }
9eadcdb7
CW
37 if (!CRM_Core_Permission::check('administer reserved tags')) {
38 foreach ($tags['values'] as $tag) {
39 if (!empty($tag['is_reserved'])) {
40 CRM_Core_Error::statusBounce(ts("You do not have permission to administer reserved tags."), $url);
41 }
42 }
43 }
edfb12c4
CW
44 }
45
46 /**
47 * Build the form object.
48 */
49 public function buildQuickForm() {
50 $this->add('text', 'name', ts('Name of combined tag'), TRUE);
51 $this->assign('tags', CRM_Utils_Array::collect('name', $this->_tags));
52
be2fb01f
CW
53 $this->addButtons([
54 [
edfb12c4
CW
55 'type' => 'next',
56 'name' => ts('Merge'),
57 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
58 'isDefault' => TRUE,
be2fb01f
CW
59 ],
60 [
edfb12c4
CW
61 'type' => 'cancel',
62 'name' => ts('Cancel'),
be2fb01f 63 ],
3655bea4 64 ]);
edfb12c4
CW
65 }
66
67 /**
68 * Set default values for the form.
69 *
70 * @return array
71 */
72 public function setDefaultValues() {
73 $primary = CRM_Utils_Array::first($this->_tags);
be2fb01f 74 return [
edfb12c4 75 'name' => $primary['name'],
be2fb01f 76 ];
edfb12c4
CW
77 }
78
79 /**
80 * Process the form submission.
81 */
82 public function postProcess() {
83 $params = $this->exportValues();
84 $deleted = CRM_Utils_Array::collect('name', $this->_tags);
85 $primary = array_shift($this->_tags);
86
87 foreach ($this->_tags as $tag) {
88 CRM_Core_BAO_EntityTag::mergeTags($primary['id'], $tag['id']);
89 }
90
91 if ($params['name'] != $primary['name']) {
be2fb01f 92 civicrm_api3('Tag', 'create', ['id' => $primary['id'], 'name' => $params['name']]);
edfb12c4
CW
93 }
94
95 $key = array_search($params['name'], $deleted);
96 if ($key !== FALSE) {
97 unset($deleted[$key]);
98 }
99
100 CRM_Core_Session::setStatus(
be2fb01f
CW
101 ts('All records previously tagged %1 are now tagged %2.', [1 => implode(' ' . ts('or') . ' ', $deleted), 2 => $params['name']]),
102 ts('%1 Tags Merged', [1 => count($this->_id)]),
edfb12c4
CW
103 'success'
104 );
105
106 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/tag'));
107 }
108
109}