Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-07-14-13-42-39
[civicrm-core.git] / CRM / Admin / Form / Tag.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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 void
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 }
58 else {
59 $this->_isTagSet = CRM_Utils_Request::retrieve('tagset', 'Positive', $this);
60
61 if (!$this->_isTagSet &&
62 $this->_id &&
63 CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $this->_id, 'is_tagset')
64 ) {
65 $this->_isTagSet = TRUE;
66 }
67
68 $allTag = array('' => '- ' . ts('select') . ' -') + CRM_Core_BAO_Tag::getTagsNotInTagset();
69
70 if ($this->_id) {
71 unset($allTag[$this->_id]);
72 }
73
74 if (!$this->_isTagSet) {
75 $this->add('select', 'parent_id', ts('Parent Tag'), $allTag, FALSE, array('class' => 'crm-select2'));
76 }
77
78 $this->assign('isTagSet', $this->_isTagSet);
79
80 $this->applyFilter('__ALL__', 'trim');
81
82 $this->add('text', 'name', ts('Name'),
83 CRM_Core_DAO::getAttribute('CRM_Core_DAO_Tag', 'name'), TRUE
84 );
85 $this->addRule('name', ts('Name already exists in Database.'), 'objectExists', array('CRM_Core_DAO_Tag', $this->_id));
86
87 $this->add('text', 'description', ts('Description'),
88 CRM_Core_DAO::getAttribute('CRM_Core_DAO_Tag', 'description')
89 );
90
91 //@lobo haven't a clue why the checkbox isn't displayed (it should be checked by default
92 $this->add('checkbox', 'is_selectable', ts("If it's a tag or a category"));
93
94 $isReserved = $this->add('checkbox', 'is_reserved', ts('Reserved?'));
95
96 $usedFor = $this->addSelect('used_for', array('multiple' => TRUE, 'option_url' => NULL));
97
98 if ($this->_id &&
99 CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $this->_id, 'parent_id')
100 ) {
101 $usedFor->freeze();
102 }
103
104 $adminTagset = TRUE;
105 if (!CRM_Core_Permission::check('administer Tagsets')) {
106 $adminTagset = FALSE;
107 }
108 $this->assign('adminTagset', $adminTagset);
109
110 $adminReservedTags = TRUE;
111 if (!CRM_Core_Permission::check('administer reserved tags')) {
112 $isReserved->freeze();
113 $adminReservedTags = FALSE;
114 }
115 $this->assign('adminReservedTags', $adminReservedTags);
116
117 }
118 parent::buildQuickForm();
119 }
120
121 /**
122 * Function to process the form
123 *
124 * @access public
125 *
126 * @return void
127 */
128 public function postProcess() {
129 $params = $ids = array();
130
131 // store the submitted values in an array
132 $params = $this->exportValues();
133
134 $ids['tag'] = $this->_id;
135 if ($this->_action == CRM_Core_Action::ADD ||
136 $this->_action == CRM_Core_Action::UPDATE
137 ) {
138 $params['used_for'] = implode(",", $params['used_for']);
139 }
140
141 $params['is_tagset'] = 0;
142 if ($this->_isTagSet) {
143 $params['is_tagset'] = 1;
144 }
145
146 if (!isset($params['is_reserved'])) {
147 $params['is_reserved'] = 0;
148 }
149
150 if ($this->_action == CRM_Core_Action::DELETE) {
151 if ($this->_id > 0) {
152 $tag = civicrm_api3('tag', 'getsingle', array('id' => $this->_id));
153 CRM_Core_BAO_Tag::del($this->_id);
154 CRM_Core_Session::setStatus(ts('The tag \'%1\' has been deleted.', array(1 => $tag['name'])), ts('Deleted'), 'success');
155 }
156 }
157 else {
158 $tag = CRM_Core_BAO_Tag::add($params, $ids);
159 CRM_Core_Session::setStatus(ts('The tag \'%1\' has been saved.', array(1 => $tag->name)), ts('Saved'), 'success');
160 }
161 }
162 //end of function
163 }
164