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