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