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