Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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-2013 | |
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 | /** | |
44 | * Function to build the form | |
45 | * | |
46 | * @return None | |
47 | * @access public | |
48 | */ | |
49 | public function buildQuickForm() { | |
50 | if ($this->_action == CRM_Core_Action::DELETE) { | |
51 | if ($this->_id && $tag = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $this->_id, 'name', 'parent_id')) { | |
52 | CRM_Core_Session::setStatus(ts("This tag cannot be deleted. You must delete all its child tags ('%1', etc) prior to deleting this tag.", array(1 => $tag)), ts('Sorry'), 'error'); | |
53 | $url = CRM_Utils_System::url('civicrm/admin/tag', "reset=1"); | |
54 | CRM_Utils_System::redirect($url); | |
55 | return TRUE; | |
56 | } | |
57 | else { | |
58 | $this->addButtons(array( | |
59 | array( | |
60 | 'type' => 'next', | |
61 | 'name' => ts('Delete'), | |
62 | 'isDefault' => TRUE, | |
63 | ), | |
64 | array( | |
65 | 'type' => 'cancel', | |
66 | 'name' => ts('Cancel'), | |
67 | ), | |
68 | ) | |
69 | ); | |
70 | } | |
71 | } | |
72 | else { | |
73 | $this->_isTagSet = CRM_Utils_Request::retrieve('tagset', 'Positive', $this); | |
74 | ||
75 | if (!$this->_isTagSet && | |
76 | $this->_id && | |
77 | CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $this->_id, 'is_tagset') | |
78 | ) { | |
79 | $this->_isTagSet = TRUE; | |
80 | } | |
81 | ||
82 | $allTag = array('' => '- ' . ts('select') . ' -') + CRM_Core_BAO_Tag::getTagsNotInTagset(); | |
83 | ||
84 | if ($this->_id) { | |
85 | unset($allTag[$this->_id]); | |
86 | } | |
87 | ||
88 | if (!$this->_isTagSet) { | |
89 | $this->add('select', 'parent_id', ts('Parent Tag'), $allTag); | |
90 | } | |
91 | ||
92 | $this->assign('isTagSet', $this->_isTagSet); | |
93 | ||
94 | $this->applyFilter('__ALL__', 'trim'); | |
95 | ||
96 | $this->add('text', 'name', ts('Name'), | |
97 | CRM_Core_DAO::getAttribute('CRM_Core_DAO_Tag', 'name'), TRUE | |
98 | ); | |
99 | $this->addRule('name', ts('Name already exists in Database.'), 'objectExists', array('CRM_Core_DAO_Tag', $this->_id)); | |
100 | ||
101 | $this->add('text', 'description', ts('Description'), | |
102 | CRM_Core_DAO::getAttribute('CRM_Core_DAO_Tag', 'description') | |
103 | ); | |
104 | ||
105 | //@lobo haven't a clue why the checkbox isn't displayed (it should be checked by default | |
106 | $this->add('checkbox', 'is_selectable', ts("If it's a tag or a category")); | |
107 | ||
108 | $isReserved = $this->add('checkbox', 'is_reserved', ts('Reserved?')); | |
109 | ||
110 | $usedFor = $this->add('select', 'used_for', ts('Used For'), | |
111 | CRM_Core_OptionGroup::values('tag_used_for') | |
112 | ); | |
113 | $usedFor->setMultiple(TRUE); | |
114 | ||
115 | if ($this->_id && | |
116 | CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $this->_id, 'parent_id') | |
117 | ) { | |
118 | $usedFor->freeze(); | |
119 | } | |
120 | ||
121 | $adminTagset = TRUE; | |
122 | if (!CRM_Core_Permission::check('administer Tagsets')) { | |
123 | $adminTagset = FALSE; | |
124 | } | |
125 | $this->assign('adminTagset', $adminTagset); | |
126 | ||
127 | $adminReservedTags = TRUE; | |
128 | if (!CRM_Core_Permission::check('administer reserved tags')) { | |
129 | $isReserved->freeze(); | |
130 | $adminReservedTags = FALSE; | |
131 | } | |
132 | $this->assign('adminReservedTags', $adminReservedTags); | |
133 | ||
134 | parent::buildQuickForm(); | |
135 | } | |
136 | } | |
137 | ||
138 | /** | |
139 | * Function to process the form | |
140 | * | |
141 | * @access public | |
142 | * | |
143 | * @return None | |
144 | */ | |
145 | public function postProcess() { | |
146 | $params = $ids = array(); | |
147 | ||
148 | // store the submitted values in an array | |
149 | $params = $this->exportValues(); | |
150 | ||
151 | $ids['tag'] = $this->_id; | |
152 | if ($this->_action == CRM_Core_Action::ADD || | |
153 | $this->_action == CRM_Core_Action::UPDATE | |
154 | ) { | |
155 | $params['used_for'] = implode(",", $params['used_for']); | |
156 | } | |
157 | ||
158 | $params['is_tagset'] = 0; | |
159 | if ($this->_isTagSet) { | |
160 | $params['is_tagset'] = 1; | |
161 | } | |
162 | ||
163 | if (!isset($params['is_reserved'])) { | |
164 | $params['is_reserved'] = 0; | |
165 | } | |
166 | ||
167 | if ($this->_action == CRM_Core_Action::DELETE) { | |
168 | if ($this->_id > 0) { | |
e51af626 | 169 | $tag = civicrm_api3('tag', 'getsingle', array('id' => $this->_id)); |
6a488035 | 170 | CRM_Core_BAO_Tag::del($this->_id); |
72984a52 | 171 | CRM_Core_Session::setStatus(ts('The tag \'%1\' has been deleted.', array(1 => $tag['name'])), ts('Deleted'), 'success'); |
6a488035 TO |
172 | } |
173 | } | |
174 | else { | |
175 | $tag = CRM_Core_BAO_Tag::add($params, $ids); | |
176 | CRM_Core_Session::setStatus(ts('The tag \'%1\' has been saved.', array(1 => $tag->name)), ts('Saved'), 'success'); | |
177 | } | |
178 | } | |
179 | //end of function | |
180 | } | |
181 |