Commit | Line | Data |
---|---|---|
edfb12c4 CW |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.7 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2017 | | |
7 | +--------------------------------------------------------------------+ | |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
31 | * @copyright CiviCRM LLC (c) 2004-2017 | |
32 | */ | |
33 | ||
34 | /** | |
35 | * Form for merging tags. | |
36 | */ | |
37 | class CRM_Tag_Form_Merge extends CRM_Core_Form { | |
38 | protected $_id; | |
39 | protected $_tags; | |
40 | ||
41 | public function preProcess() { | |
42 | $this->_id = CRM_Utils_Request::retrieve('id', 'String', $this, FALSE); | |
43 | $this->_id = explode(',', $this->_id); | |
44 | $url = CRM_Utils_System::url('civicrm/tag'); | |
45 | if (count($this->_id) < 2) { | |
9eadcdb7 | 46 | CRM_Core_Error::statusBounce(ts("You must select at least 2 tags for merging."), $url); |
edfb12c4 CW |
47 | } |
48 | $tags = civicrm_api3('Tag', 'get', array('id' => array('IN' => $this->_id), 'options' => array('limit' => 0))); | |
49 | $this->_tags = $tags['values']; | |
50 | if (count($this->_id) != count($this->_tags)) { | |
51 | CRM_Core_Error::statusBounce(ts("Unknown tag."), $url); | |
52 | } | |
9eadcdb7 CW |
53 | if (!CRM_Core_Permission::check('administer reserved tags')) { |
54 | foreach ($tags['values'] as $tag) { | |
55 | if (!empty($tag['is_reserved'])) { | |
56 | CRM_Core_Error::statusBounce(ts("You do not have permission to administer reserved tags."), $url); | |
57 | } | |
58 | } | |
59 | } | |
edfb12c4 CW |
60 | } |
61 | ||
62 | /** | |
63 | * Build the form object. | |
64 | */ | |
65 | public function buildQuickForm() { | |
66 | $this->add('text', 'name', ts('Name of combined tag'), TRUE); | |
67 | $this->assign('tags', CRM_Utils_Array::collect('name', $this->_tags)); | |
68 | ||
69 | $this->addButtons(array( | |
70 | array( | |
71 | 'type' => 'next', | |
72 | 'name' => ts('Merge'), | |
73 | 'spacing' => ' ', | |
74 | 'isDefault' => TRUE, | |
75 | ), | |
76 | array( | |
77 | 'type' => 'cancel', | |
78 | 'name' => ts('Cancel'), | |
79 | ), | |
80 | ) | |
81 | ); | |
82 | } | |
83 | ||
84 | /** | |
85 | * Set default values for the form. | |
86 | * | |
87 | * @return array | |
88 | */ | |
89 | public function setDefaultValues() { | |
90 | $primary = CRM_Utils_Array::first($this->_tags); | |
91 | return array( | |
92 | 'name' => $primary['name'], | |
93 | ); | |
94 | } | |
95 | ||
96 | /** | |
97 | * Process the form submission. | |
98 | */ | |
99 | public function postProcess() { | |
100 | $params = $this->exportValues(); | |
101 | $deleted = CRM_Utils_Array::collect('name', $this->_tags); | |
102 | $primary = array_shift($this->_tags); | |
103 | ||
104 | foreach ($this->_tags as $tag) { | |
105 | CRM_Core_BAO_EntityTag::mergeTags($primary['id'], $tag['id']); | |
106 | } | |
107 | ||
108 | if ($params['name'] != $primary['name']) { | |
109 | civicrm_api3('Tag', 'create', array('id' => $primary['id'], 'name' => $params['name'])); | |
110 | } | |
111 | ||
112 | $key = array_search($params['name'], $deleted); | |
113 | if ($key !== FALSE) { | |
114 | unset($deleted[$key]); | |
115 | } | |
116 | ||
117 | CRM_Core_Session::setStatus( | |
118 | ts('All records previously tagged %1 are now tagged %2.', array(1 => implode(' ' . ts('or') . ' ', $deleted), 2 => $params['name'])), | |
119 | ts('%1 Tags Merged', array(1 => count($this->_id))), | |
120 | 'success' | |
121 | ); | |
122 | ||
123 | CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/tag')); | |
124 | } | |
125 | ||
126 | } |