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