Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class generates form components for Tag | |
38 | * | |
39 | */ | |
40 | class CRM_Admin_Form_Tag extends CRM_Admin_Form { | |
41 | protected $_isTagSet; | |
42 | ||
43 | /** | |
eceb18cc | 44 | * Build the form object. |
6a488035 | 45 | * |
355ba699 | 46 | * @return void |
6a488035 TO |
47 | */ |
48 | public function buildQuickForm() { | |
e2046b33 CW |
49 | $this->setPageTitle($this->_isTagSet ? ts('Tag Set') : ts('Tag')); |
50 | ||
6a488035 TO |
51 | if ($this->_action == CRM_Core_Action::DELETE) { |
52 | if ($this->_id && $tag = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $this->_id, 'name', 'parent_id')) { | |
6a488035 | 53 | $url = CRM_Utils_System::url('civicrm/admin/tag', "reset=1"); |
e2046b33 | 54 | CRM_Core_Error::statusBounce(ts("This tag cannot be deleted. You must delete all its child tags ('%1', etc) prior to deleting this tag.", array(1 => $tag)), $url); |
6a488035 | 55 | } |
bf4b6f8f JP |
56 | if ($this->_values['is_reserved'] == 1 && !CRM_Core_Permission::check('administer reserved tags')) { |
57 | CRM_Core_Error::statusBounce(ts("You do not have sufficient permission to delete this reserved tag.")); | |
58 | } | |
6a488035 TO |
59 | } |
60 | else { | |
61 | $this->_isTagSet = CRM_Utils_Request::retrieve('tagset', 'Positive', $this); | |
62 | ||
63 | if (!$this->_isTagSet && | |
64 | $this->_id && | |
65 | CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $this->_id, 'is_tagset') | |
66 | ) { | |
67 | $this->_isTagSet = TRUE; | |
68 | } | |
69 | ||
e2046b33 | 70 | $allTag = array('' => ts('- select -')) + CRM_Core_BAO_Tag::getTagsNotInTagset(); |
6a488035 TO |
71 | |
72 | if ($this->_id) { | |
73 | unset($allTag[$this->_id]); | |
74 | } | |
75 | ||
76 | if (!$this->_isTagSet) { | |
4c863787 | 77 | $this->add('select', 'parent_id', ts('Parent Tag'), $allTag, FALSE, array('class' => 'crm-select2')); |
69582599 DG |
78 | |
79 | // Tagsets are not selectable by definition so only include the selectable field if NOT a tagset. | |
80 | $selectable = $this->add('checkbox', 'is_selectable', ts('Selectable?')); | |
81 | // Selectable should be checked by default when creating a new tag | |
82 | if ($this->_action == CRM_Core_Action::ADD) { | |
02fc859b | 83 | $selectable->setValue(1); |
69582599 DG |
84 | } |
85 | ||
6a488035 TO |
86 | } |
87 | ||
88 | $this->assign('isTagSet', $this->_isTagSet); | |
89 | ||
90 | $this->applyFilter('__ALL__', 'trim'); | |
91 | ||
92 | $this->add('text', 'name', ts('Name'), | |
93 | CRM_Core_DAO::getAttribute('CRM_Core_DAO_Tag', 'name'), TRUE | |
94 | ); | |
353ffa53 TO |
95 | $this->addRule('name', ts('Name already exists in Database.'), 'objectExists', array( |
96 | 'CRM_Core_DAO_Tag', | |
795492f3 | 97 | $this->_id, |
353ffa53 | 98 | )); |
6a488035 TO |
99 | |
100 | $this->add('text', 'description', ts('Description'), | |
101 | CRM_Core_DAO::getAttribute('CRM_Core_DAO_Tag', 'description') | |
102 | ); | |
103 | ||
6a488035 TO |
104 | $isReserved = $this->add('checkbox', 'is_reserved', ts('Reserved?')); |
105 | ||
4c863787 | 106 | $usedFor = $this->addSelect('used_for', array('multiple' => TRUE, 'option_url' => NULL)); |
6a488035 TO |
107 | |
108 | if ($this->_id && | |
109 | CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $this->_id, 'parent_id') | |
110 | ) { | |
111 | $usedFor->freeze(); | |
112 | } | |
113 | ||
114 | $adminTagset = TRUE; | |
115 | if (!CRM_Core_Permission::check('administer Tagsets')) { | |
116 | $adminTagset = FALSE; | |
117 | } | |
118 | $this->assign('adminTagset', $adminTagset); | |
119 | ||
120 | $adminReservedTags = TRUE; | |
121 | if (!CRM_Core_Permission::check('administer reserved tags')) { | |
122 | $isReserved->freeze(); | |
123 | $adminReservedTags = FALSE; | |
124 | } | |
125 | $this->assign('adminReservedTags', $adminReservedTags); | |
126 | ||
6a488035 | 127 | } |
a1f552a9 | 128 | parent::buildQuickForm(); |
6a488035 TO |
129 | } |
130 | ||
131 | /** | |
eceb18cc | 132 | * Process the form submission. |
6a488035 | 133 | * |
6a488035 | 134 | * |
355ba699 | 135 | * @return void |
6a488035 TO |
136 | */ |
137 | public function postProcess() { | |
138 | $params = $ids = array(); | |
139 | ||
140 | // store the submitted values in an array | |
141 | $params = $this->exportValues(); | |
142 | ||
143 | $ids['tag'] = $this->_id; | |
144 | if ($this->_action == CRM_Core_Action::ADD || | |
145 | $this->_action == CRM_Core_Action::UPDATE | |
146 | ) { | |
147 | $params['used_for'] = implode(",", $params['used_for']); | |
148 | } | |
149 | ||
150 | $params['is_tagset'] = 0; | |
151 | if ($this->_isTagSet) { | |
152 | $params['is_tagset'] = 1; | |
153 | } | |
154 | ||
155 | if (!isset($params['is_reserved'])) { | |
156 | $params['is_reserved'] = 0; | |
157 | } | |
158 | ||
f37c6bf1 T |
159 | if (!isset($params['is_selectable'])) { |
160 | $params['is_selectable'] = 0; | |
161 | } | |
162 | ||
6a488035 TO |
163 | if ($this->_action == CRM_Core_Action::DELETE) { |
164 | if ($this->_id > 0) { | |
e51af626 | 165 | $tag = civicrm_api3('tag', 'getsingle', array('id' => $this->_id)); |
6a488035 | 166 | CRM_Core_BAO_Tag::del($this->_id); |
72984a52 | 167 | CRM_Core_Session::setStatus(ts('The tag \'%1\' has been deleted.', array(1 => $tag['name'])), ts('Deleted'), 'success'); |
6a488035 TO |
168 | } |
169 | } | |
170 | else { | |
171 | $tag = CRM_Core_BAO_Tag::add($params, $ids); | |
172 | CRM_Core_Session::setStatus(ts('The tag \'%1\' has been saved.', array(1 => $tag->name)), ts('Saved'), 'success'); | |
173 | } | |
174 | } | |
e2046b33 | 175 | |
6a488035 | 176 | } |