Merge pull request #18302 from civicrm/5.29
[civicrm-core.git] / CRM / Campaign / Form / Survey / Main.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
3819f101 19 * This class generates form components for processing a survey.
6a488035
TO
20 */
21class CRM_Campaign_Form_Survey_Main extends CRM_Campaign_Form_Survey {
22
f157740d
SL
23 /**
24 * values
e70a7fc0
TO
25 *
26 * @var array
f157740d 27 *
e70a7fc0 28 */
6a488035 29
f157740d 30
6a488035
TO
31 public $_values;
32
33 /**
fe482240 34 * Context.
6a488035
TO
35 *
36 * @var string
37 */
38 protected $_context;
d5965a37 39
6a488035
TO
40 public function preProcess() {
41 parent::preProcess();
42
edc80cda 43 $this->_context = CRM_Utils_Request::retrieve('context', 'Alphanumeric', $this);
6a488035
TO
44
45 $this->assign('context', $this->_context);
46
47 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this);
48
49 if ($this->_action & CRM_Core_Action::UPDATE) {
50 CRM_Utils_System::setTitle(ts('Configure Survey') . ' - ' . $this->_surveyTitle);
51 }
52
14c67729
MWMC
53 // Add custom data to form
54 CRM_Custom_Form_CustomData::addToForm($this);
6a488035
TO
55
56 if ($this->_name != 'Petition') {
57 $url = CRM_Utils_System::url('civicrm/campaign', 'reset=1&subPage=survey');
be2fb01f 58 CRM_Utils_System::appendBreadCrumb([['title' => ts('Survey Dashboard'), 'url' => $url]]);
6a488035
TO
59 }
60
61 $this->_values = $this->get('values');
62 if (!is_array($this->_values)) {
be2fb01f 63 $this->_values = [];
6a488035 64 if ($this->_surveyId) {
be2fb01f 65 $params = ['id' => $this->_surveyId];
6a488035
TO
66 CRM_Campaign_BAO_Survey::retrieve($params, $this->_values);
67 }
68 $this->set('values', $this->_values);
69 }
70
71 $this->assign('action', $this->_action);
72 $this->assign('surveyId', $this->_surveyId);
6a488035
TO
73 }
74
75 /**
c490a46a 76 * Set default values for the form. Note that in edit/view mode
6a488035
TO
77 * the default values are retrieved from the database
78 *
a6c01b45
CW
79 * @return array
80 * array of default values
6a488035 81 */
00be9182 82 public function setDefaultValues() {
6a488035
TO
83
84 $defaults = $this->_values;
85
86 if ($this->_surveyId) {
87
8cc574cf 88 if (!empty($defaults['result_id']) && !empty($defaults['recontact_interval'])) {
6a488035
TO
89
90 $resultId = $defaults['result_id'];
f24846d5 91 $recontactInterval = CRM_Utils_String::unserialize($defaults['recontact_interval']);
6a488035
TO
92
93 unset($defaults['recontact_interval']);
94 $defaults['option_group_id'] = $resultId;
95 }
96 }
97
98 if (!isset($defaults['is_active'])) {
99 $defaults['is_active'] = 1;
100 }
101
102 $defaultSurveys = CRM_Campaign_BAO_Survey::getSurveys(TRUE, TRUE);
103 if (!isset($defaults['is_default']) && empty($defaultSurveys)) {
104 $defaults['is_default'] = 1;
105 }
106
107 return $defaults;
108 }
109
110 /**
fe482240 111 * Build the form object.
6a488035
TO
112 */
113 public function buildQuickForm() {
6a488035
TO
114 $this->add('text', 'title', ts('Title'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'title'), TRUE);
115
6a488035 116 // Activity Type id
be2fb01f 117 $this->addSelect('activity_type_id', ['option_url' => 'civicrm/admin/campaign/surveyType'], TRUE);
6a488035 118
f7b32533 119 $this->addEntityRef('campaign_id', ts('Campaign'), [
af00ced5 120 'entity' => 'Campaign',
f7b32533 121 'create' => TRUE,
33ccf435 122 'select' => ['minimumInputLength' => 0],
f7b32533 123 ]);
6a488035
TO
124
125 // script / instructions
be2fb01f 126 $this->add('wysiwyg', 'instructions', ts('Instructions for interviewers'), ['rows' => 5, 'cols' => 40]);
6a488035
TO
127
128 // release frequency
9970fd53 129 $this->add('number', 'release_frequency', ts('Release Frequency'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'release_frequency'));
6a488035
TO
130
131 $this->addRule('release_frequency', ts('Release Frequency interval should be a positive number.'), 'positiveInteger');
132
133 // max reserved contacts at a time
9970fd53 134 $this->add('number', 'default_number_of_contacts', ts('Maximum reserved at one time'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'default_number_of_contacts'));
6a488035
TO
135 $this->addRule('default_number_of_contacts', ts('Maximum reserved at one time should be a positive number'), 'positiveInteger');
136
137 // total reserved per interviewer
9970fd53 138 $this->add('number', 'max_number_of_contacts', ts('Total reserved per interviewer'), CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'max_number_of_contacts'));
6a488035
TO
139 $this->addRule('max_number_of_contacts', ts('Total reserved contacts should be a positive number'), 'positiveInteger');
140
141 // is active ?
142 $this->add('checkbox', 'is_active', ts('Active?'));
143
144 // is default ?
145 $this->add('checkbox', 'is_default', ts('Default?'));
146
147 parent::buildQuickForm();
148 }
149
150 /**
fe482240 151 * Process the form.
6a488035
TO
152 */
153 public function postProcess() {
154 // store the submitted values in an array
155 $params = $this->controller->exportValues($this->_name);
156
157 $session = CRM_Core_Session::singleton();
158
159 $params['last_modified_id'] = $session->get('userID');
160 $params['last_modified_date'] = date('YmdHis');
161
162 if ($this->_surveyId) {
163 $params['id'] = $this->_surveyId;
164 }
165 else {
166 $params['created_id'] = $session->get('userID');
167 $params['created_date'] = date('YmdHis');
168 }
169
170 $params['is_active'] = CRM_Utils_Array::value('is_active', $params, 0);
171 $params['is_default'] = CRM_Utils_Array::value('is_default', $params, 0);
172
14c67729
MWMC
173 $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, $this->getEntityId(), $this->getDefaultEntity());
174
6a488035
TO
175 $survey = CRM_Campaign_BAO_Survey::create($params);
176 $this->_surveyId = $survey->id;
177
a7488080 178 if (!empty($this->_values['result_id'])) {
6a488035 179 $query = "SELECT COUNT(*) FROM civicrm_survey WHERE result_id = %1";
353ffa53 180 $countSurvey = (int) CRM_Core_DAO::singleValueQuery($query,
be2fb01f
CW
181 [
182 1 => [
353ffa53 183 $this->_values['result_id'],
6a488035 184 'Positive',
be2fb01f
CW
185 ],
186 ]
6a488035
TO
187 );
188 // delete option group if no any survey is using it.
189 if (!$countSurvey) {
190 CRM_Core_BAO_OptionGroup::del($this->_values['result_id']);
191 }
192 }
193
194 parent::endPostProcess();
195 }
96025800 196
6a488035 197}