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 * Form for merging tags.
21 class CRM_Tag_Form_Merge
extends CRM_Core_Form
{
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);
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);
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);
47 * Build the form object.
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
));
56 'name' => ts('Merge'),
57 'spacing' => ' ',
62 'name' => ts('Cancel'),
68 * Set default values for the form.
72 public function setDefaultValues() {
73 $primary = CRM_Utils_Array
::first($this->_tags
);
75 'name' => $primary['name'],
80 * Process the form submission.
82 public function postProcess() {
83 $params = $this->exportValues();
84 $deleted = CRM_Utils_Array
::collect('name', $this->_tags
);
85 $primary = array_shift($this->_tags
);
87 foreach ($this->_tags
as $tag) {
88 CRM_Core_BAO_EntityTag
::mergeTags($primary['id'], $tag['id']);
91 if ($params['name'] != $primary['name']) {
92 civicrm_api3('Tag', 'create', ['id' => $primary['id'], 'name' => $params['name']]);
95 $key = array_search($params['name'], $deleted);
97 unset($deleted[$key]);
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
)]),
106 CRM_Core_Session
::singleton()->pushUserContext(CRM_Utils_System
::url('civicrm/tag'));