Merge pull request #2564 from jaapjansma/CRM-14276
[civicrm-core.git] / CRM / Campaign / Form / SurveyType.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 Option Group
38 *
39 */
40 class CRM_Campaign_Form_SurveyType extends CRM_Admin_Form {
41 protected $_gid;
42
43 /**
44 * The option group name
45 *
46 * @var string
47 * @static
48 */
49 protected $_gName;
50
51 /**
52 * id
53 *
54 * @var int
55 */
56 protected $_id;
57
58 /**
59 * action
60 *
61 * @var int
62 */
63 protected $_action;
64
65 /**
66 * Function to set variables up before form is built
67 *
68 * @param null
69 *
70 * @return void
71 * @access public
72 */ function preProcess() {
73 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this);
74
75 if ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::DELETE)) {
76 $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, TRUE);
77 $this->assign('id', $this->_id);
78 }
79 $this->assign('action', $this->_action);
80 $this->assign('id', $this->_id);
81
82 $this->_BAOName = 'CRM_Core_BAO_OptionValue';
83 $this->_gName = 'activity_type';
84 $this->_gid = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $this->_gName, 'id', 'name');
85
86 $session = CRM_Core_Session::singleton();
87 $url = CRM_Utils_System::url('civicrm/admin/campaign/surveyType', 'reset=1');
88 $session->pushUserContext($url);
89
90 if ($this->_id && in_array($this->_gName, CRM_Core_OptionGroup::$_domainIDGroups)) {
91 $domainID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $this->_id, 'domain_id', 'id');
92 if (CRM_Core_Config::domainID() != $domainID) {
93 CRM_Core_Error::fatal(ts('You do not have permission to access this page'));
94 }
95 }
96 }
97
98 /**
99 * This function sets the default values for the form.
100 * the default values are retrieved from the database.
101 *
102 * @param null
103 *
104 * @return array array of default values
105 * @access public
106 */
107 function setDefaultValues() {
108 $defaults = parent::setDefaultValues();
109
110 if (!isset($defaults['weight']) || !$defaults['weight']) {
111 $fieldValues = array('option_group_id' => $this->_gid);
112 $defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue', $fieldValues);
113 }
114
115 return $defaults;
116 }
117
118 /**
119 * Function to build the form
120 *
121 * @return void
122 * @access public
123 */
124 public function buildQuickForm() {
125 parent::buildQuickForm();
126 if ($this->_action & CRM_Core_Action::DELETE) {
127 return;
128 }
129
130 $this->applyFilter('__ALL__', 'trim');
131 $this->add('text', 'label', ts('Title'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'label'), TRUE);
132
133 $this->addWysiwyg('description',
134 ts('Description'),
135 CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'description')
136 );
137
138
139 $this->add('checkbox', 'is_active', ts('Enabled?'));
140
141 if ($this->_action == CRM_Core_Action::UPDATE &&
142 CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $this->_id, 'is_reserved')
143 ) {
144 $this->freeze(array('label', 'is_active'));
145 }
146 $this->add('text', 'weight', ts('Weight'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'weight'), TRUE);
147
148 $this->assign('id', $this->_id);
149 }
150
151 /**
152 * Function to process the form
153 *
154 * @access public
155 *
156 * @return void
157 */
158 public function postProcess() {
159
160 if ($this->_action & CRM_Core_Action::DELETE) {
161 $fieldValues = array('option_group_id' => $this->_gid);
162 $wt = CRM_Utils_Weight::delWeight('CRM_Core_DAO_OptionValue', $this->_id, $fieldValues);
163
164 if (CRM_Core_BAO_OptionValue::del($this->_id)) {
165 CRM_Core_Session::setStatus(ts('Selected Survey type has been deleted.'), ts('Record Deleted'), 'success');
166 }
167 }
168 else {
169 $params = $ids = array();
170 $params = $this->exportValues();
171
172 // set db value of filter in params if filter is non editable
173 if ($this->_id && !array_key_exists('filter', $params)) {
174 $params['filter'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $this->_id, 'filter', 'id');
175 }
176
177 $groupParams = array('name' => ($this->_gName));
178 $params['component_id'] = CRM_Core_Component::getComponentID('CiviCampaign');
179 $optionValue = CRM_Core_OptionValue::addOptionValue($params, $groupParams, $this->_action, $this->_id);
180
181 CRM_Core_Session::setStatus(ts('The Survey type \'%1\' has been saved.', array(1 => $optionValue->label)), ts('Saved'), 'success');
182 }
183 }
184 }
185