Merge pull request #17879 from colemanw/relCacheApi
[civicrm-core.git] / CRM / Tag / Form / Merge.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 * Form for merging tags.
20 */
21 class 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) {
30 CRM_Core_Error::statusBounce(ts("You must select at least 2 tags for merging."), $url);
31 }
32 $tags = civicrm_api3('Tag', 'get', ['id' => ['IN' => $this->_id], 'options' => ['limit' => 0]]);
33 $this->_tags = $tags['values'];
34 if (count($this->_id) != count($this->_tags)) {
35 CRM_Core_Error::statusBounce(ts("Unknown tag."), $url);
36 }
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 }
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
53 $this->addButtons([
54 [
55 'type' => 'next',
56 'name' => ts('Merge'),
57 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
58 'isDefault' => TRUE,
59 ],
60 [
61 'type' => 'cancel',
62 'name' => ts('Cancel'),
63 ],
64 ]);
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);
74 return [
75 'name' => $primary['name'],
76 ];
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']) {
92 civicrm_api3('Tag', 'create', ['id' => $primary['id'], 'name' => $params['name']]);
93 }
94
95 $key = array_search($params['name'], $deleted);
96 if ($key !== FALSE) {
97 unset($deleted[$key]);
98 }
99
100 CRM_Core_Session::setStatus(
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)]),
103 'success'
104 );
105
106 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/tag'));
107 }
108
109 }