Commit | Line | Data |
---|---|---|
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 processing a survey | |
38 | * | |
39 | */ | |
40 | class CRM_Campaign_Form_Survey_Results extends CRM_Campaign_Form_Survey { | |
41 | ||
42 | protected $_reportId; | |
43 | ||
44 | protected $_reportTitle; | |
45 | ||
46 | /* values | |
47 | * | |
48 | * @var array | |
49 | */ | |
50 | public $_values; | |
51 | ||
52 | CONST NUM_OPTION = 11; | |
53 | ||
54 | public function preProcess() { | |
55 | parent::preProcess(); | |
56 | ||
57 | $this->_values = $this->get('values'); | |
58 | if (!is_array($this->_values)) { | |
59 | $this->_values = array(); | |
60 | if ($this->_surveyId) { | |
61 | $params = array('id' => $this->_surveyId); | |
62 | CRM_Campaign_BAO_Survey::retrieve($params, $this->_values); | |
63 | } | |
64 | $this->set('values', $this->_values); | |
65 | } | |
66 | ||
67 | $query = "SELECT MAX(id) as id, title FROM civicrm_report_instance WHERE name = %1"; | |
68 | $params = array( 1 => array("survey_{$this->_surveyId}",'String') ); | |
69 | $result = CRM_Core_DAO::executeQuery($query, $params); | |
70 | if ( $result->fetch() ) { | |
71 | $this->_reportId = $result->id; | |
72 | $this->_reportTitle = $result->title; | |
73 | } | |
74 | } | |
75 | ||
76 | /** | |
77 | * This function sets the default values for the form. Note that in edit/view mode | |
78 | * the default values are retrieved from the database | |
79 | * | |
80 | * @param null | |
81 | * | |
82 | * @return array array of default values | |
83 | * @access public | |
84 | */ | |
85 | function setDefaultValues() { | |
86 | $defaults = $this->_values; | |
87 | ||
88 | // set defaults for weight. | |
89 | for ($i = 1; $i <= self::NUM_OPTION; $i++) { | |
90 | $defaults["option_weight[{$i}]"] = $i; | |
91 | } | |
92 | ||
93 | $defaults['create_report'] = 1; | |
94 | if ($this->_reportId) { | |
95 | $defaults['report_title'] = $this->_reportTitle; | |
96 | } | |
97 | return $defaults; | |
98 | } | |
99 | ||
100 | /** | |
101 | * Function to actually build the form | |
102 | * | |
103 | * @param null | |
104 | * | |
105 | * @return void | |
106 | * @access public | |
107 | */ | |
108 | public function buildQuickForm() { | |
109 | $optionGroups = CRM_Campaign_BAO_Survey::getResultSets(); | |
110 | ||
111 | if (empty($optionGroups)) { | |
112 | $optionTypes = array('1' => ts('Create new result set')); | |
113 | } | |
114 | else { | |
115 | $optionTypes = array('1' => ts('Create a new result set'), | |
116 | '2' => ts('Use existing result set'), | |
117 | ); | |
118 | $this->add('select', | |
119 | 'option_group_id', | |
120 | ts('Select Result Set'), | |
121 | array( | |
122 | '' => ts('- select -')) + $optionGroups, FALSE, | |
123 | array('onChange' => 'loadOptionGroup( )') | |
124 | ); | |
125 | } | |
126 | ||
127 | $element = &$this->addRadio('option_type', | |
128 | ts('Survey Responses'), | |
129 | $optionTypes, | |
130 | array( | |
131 | 'onclick' => "showOptionSelect();"), '<br/>', TRUE | |
132 | ); | |
133 | ||
8cc574cf | 134 | if (empty($optionGroups) || empty($this->_values['result_id'])) { |
6a488035 TO |
135 | $this->setdefaults(array('option_type' => 1)); |
136 | } | |
a7488080 | 137 | elseif (!empty($this->_values['result_id'])) { |
6a488035 TO |
138 | $this->setdefaults(array( |
139 | 'option_type' => 2, | |
140 | 'option_group_id' => $this->_values['result_id'], | |
141 | )); | |
142 | } | |
143 | ||
144 | // form fields of Custom Option rows | |
145 | $defaultOption = array(); | |
146 | $_showHide = new CRM_Core_ShowHideBlocks('', ''); | |
147 | ||
148 | $optionAttributes = CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue'); | |
149 | $optionAttributes['label']['size'] = $optionAttributes['value']['size'] = 25; | |
150 | ||
151 | for ($i = 1; $i <= self::NUM_OPTION; $i++) { | |
152 | //the show hide blocks | |
153 | $showBlocks = 'optionField_' . $i; | |
154 | if ($i > 2) { | |
155 | $_showHide->addHide($showBlocks); | |
156 | if ($i == self::NUM_OPTION) { | |
157 | $_showHide->addHide('additionalOption'); | |
158 | } | |
159 | } | |
160 | else { | |
161 | $_showHide->addShow($showBlocks); | |
162 | } | |
163 | ||
164 | $this->add('text', 'option_label[' . $i . ']', ts('Label'), | |
165 | $optionAttributes['label'] | |
166 | ); | |
167 | ||
168 | // value | |
169 | $this->add('text', 'option_value[' . $i . ']', ts('Value'), | |
170 | $optionAttributes['value'] | |
171 | ); | |
172 | ||
173 | // weight | |
174 | $this->add('text', "option_weight[$i]", ts('Order'), | |
175 | $optionAttributes['weight'] | |
176 | ); | |
177 | ||
178 | $this->add('text', 'option_interval[' . $i . ']', ts('Recontact Interval'), | |
179 | CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'release_frequency') | |
180 | ); | |
181 | ||
182 | $defaultOption[$i] = $this->createElement('radio', NULL, NULL, NULL, $i); | |
183 | } | |
184 | ||
185 | //default option selection | |
186 | $this->addGroup($defaultOption, 'default_option'); | |
187 | ||
188 | $_showHide->addToTemplate(); | |
189 | ||
190 | $this->addElement('checkbox', 'create_report', ts('Create Report')); | |
191 | $this->addElement('text', 'report_title', ts('Report Title')); | |
8ef12e64 | 192 | |
6a488035 TO |
193 | if( $this->_reportId){ |
194 | $this->freeze('create_report'); | |
195 | $this->freeze('report_title'); | |
196 | } | |
197 | ||
198 | $this->addFormRule(array('CRM_Campaign_Form_Survey_Results', 'formRule'), $this); | |
199 | ||
200 | parent::buildQuickForm(); | |
201 | } | |
202 | ||
203 | /** | |
204 | * global validation rules for the form | |
205 | * | |
206 | */ | |
207 | static function formRule($fields, $files, $form) { | |
208 | $errors = array(); | |
8cc574cf | 209 | if (!empty($fields['option_label']) && !empty($fields['option_value']) && |
6a488035 TO |
210 | (count(array_filter($fields['option_label'])) == 0) && |
211 | (count(array_filter($fields['option_value'])) == 0) | |
212 | ) { | |
213 | $errors['option_label[1]'] = ts('Enter at least one result option.'); | |
214 | return $errors; | |
215 | } | |
8cc574cf | 216 | elseif (empty($fields['option_label']) && empty($fields['option_value'])) { |
6a488035 TO |
217 | return $errors; |
218 | } | |
219 | ||
968c2c5f | 220 | if ( |
8cc574cf | 221 | $fields['option_type'] == 2 && empty($fields['option_group_id'])) { |
6a488035 TO |
222 | $errors['option_group_id'] = ts("Please select a Survey Result Set."); |
223 | return $errors; | |
224 | } | |
225 | ||
226 | $_flagOption = $_rowError = 0; | |
227 | $_showHide = new CRM_Core_ShowHideBlocks('', ''); | |
228 | ||
229 | //capture duplicate Custom option values | |
230 | if (!empty($fields['option_value'])) { | |
231 | $countValue = count($fields['option_value']); | |
232 | $uniqueCount = count(array_unique($fields['option_value'])); | |
233 | ||
234 | if ($countValue > $uniqueCount) { | |
235 | $start = 1; | |
236 | while ($start < self::NUM_OPTION) { | |
237 | $nextIndex = $start + 1; | |
238 | ||
239 | while ($nextIndex <= self::NUM_OPTION) { | |
240 | if ($fields['option_value'][$start] == $fields['option_value'][$nextIndex] && | |
241 | !empty($fields['option_value'][$nextIndex]) | |
242 | ) { | |
243 | ||
244 | $errors['option_value[' . $start . ']'] = ts('Duplicate Option values'); | |
245 | $errors['option_value[' . $nextIndex . ']'] = ts('Duplicate Option values'); | |
246 | $_flagOption = 1; | |
247 | } | |
248 | $nextIndex++; | |
249 | } | |
250 | $start++; | |
251 | } | |
252 | } | |
253 | } | |
254 | ||
255 | //capture duplicate Custom Option label | |
256 | if (!empty($fields['option_label'])) { | |
257 | $countValue = count($fields['option_label']); | |
258 | $uniqueCount = count(array_unique($fields['option_label'])); | |
259 | ||
260 | if ($countValue > $uniqueCount) { | |
261 | $start = 1; | |
262 | while ($start < self::NUM_OPTION) { | |
263 | $nextIndex = $start + 1; | |
264 | ||
265 | while ($nextIndex <= self::NUM_OPTION) { | |
266 | if ($fields['option_label'][$start] == $fields['option_label'][$nextIndex] && !empty($fields['option_label'][$nextIndex])) { | |
267 | $errors['option_label[' . $start . ']'] = ts('Duplicate Option label'); | |
268 | $errors['option_label[' . $nextIndex . ']'] = ts('Duplicate Option label'); | |
269 | $_flagOption = 1; | |
270 | } | |
271 | $nextIndex++; | |
272 | } | |
273 | $start++; | |
274 | } | |
275 | } | |
276 | } | |
277 | ||
278 | for ($i = 1; $i <= self::NUM_OPTION; $i++) { | |
279 | if (!$fields['option_label'][$i]) { | |
280 | if ($fields['option_value'][$i]) { | |
281 | $errors['option_label[' . $i . ']'] = ts('Option label cannot be empty'); | |
282 | $_flagOption = 1; | |
283 | } | |
284 | else { | |
285 | $_emptyRow = 1; | |
286 | } | |
287 | } | |
288 | elseif (!strlen(trim($fields['option_value'][$i]))) { | |
289 | if (!$fields['option_value'][$i]) { | |
290 | $errors['option_value[' . $i . ']'] = ts('Option value cannot be empty'); | |
291 | $_flagOption = 1; | |
292 | } | |
293 | } | |
294 | ||
a7488080 | 295 | if (!empty($fields['option_interval'][$i]) && !CRM_Utils_Rule::integer($fields['option_interval'][$i])) { |
6a488035 TO |
296 | $_flagOption = 1; |
297 | $errors['option_interval[' . $i . ']'] = ts('Please enter a valid integer.'); | |
298 | } | |
299 | ||
300 | $showBlocks = 'optionField_' . $i; | |
301 | if ($_flagOption) { | |
302 | $_showHide->addShow($showBlocks); | |
303 | $_rowError = 1; | |
304 | } | |
305 | ||
306 | if (!empty($_emptyRow)) { | |
307 | $_showHide->addHide($showBlocks); | |
308 | } | |
309 | else { | |
310 | $_showHide->addShow($showBlocks); | |
311 | } | |
312 | ||
313 | if ($i == self::NUM_OPTION) { | |
314 | $hideBlock = 'additionalOption'; | |
315 | $_showHide->addHide($hideBlock); | |
316 | } | |
317 | ||
318 | $_flagOption = $_emptyRow = 0; | |
319 | } | |
320 | $_showHide->addToTemplate(); | |
321 | ||
322 | return empty($errors) ? TRUE : $errors; | |
323 | } | |
324 | ||
325 | /** | |
326 | * Process the form | |
327 | * | |
328 | * @param null | |
329 | * | |
330 | * @return void | |
331 | * @access public | |
332 | */ | |
333 | public function postProcess() { | |
334 | // store the submitted values in an array | |
335 | $status = ''; | |
336 | $params = $this->controller->exportValues($this->_name); | |
337 | $params['id'] = $this->_surveyId; | |
338 | ||
339 | $updateResultSet = FALSE; | |
340 | $resultSetOptGrpId = NULL; | |
8cc574cf | 341 | if ((CRM_Utils_Array::value('option_type', $params) == 2) && !empty($params['option_group_id'])) { |
6a488035 TO |
342 | $updateResultSet = TRUE; |
343 | $resultSetOptGrpId = $params['option_group_id']; | |
344 | } | |
345 | ||
346 | $recontactInterval = array(); | |
347 | if ($updateResultSet) { | |
348 | $optionValue = new CRM_Core_DAO_OptionValue(); | |
349 | $optionValue->option_group_id = $resultSetOptGrpId; | |
350 | $optionValue->delete(); | |
351 | ||
352 | $params['result_id'] = $resultSetOptGrpId; | |
353 | } | |
354 | else { | |
355 | $opGroupName = 'civicrm_survey_' . rand(10, 1000) . '_' . date('YmdHis'); | |
356 | ||
357 | $optionGroup = new CRM_Core_DAO_OptionGroup(); | |
358 | $optionGroup->name = $opGroupName; | |
359 | $optionGroup->title = $this->_values['title'] . ' Result Set'; | |
360 | $optionGroup->is_active = 1; | |
361 | $optionGroup->save(); | |
362 | ||
363 | $params['result_id'] = $optionGroup->id; | |
364 | } | |
365 | ||
366 | foreach ($params['option_value'] as $k => $v) { | |
367 | if (strlen(trim($v))) { | |
368 | $optionValue = new CRM_Core_DAO_OptionValue(); | |
369 | $optionValue->option_group_id = $params['result_id']; | |
370 | $optionValue->label = $params['option_label'][$k]; | |
371 | $optionValue->name = CRM_Utils_String::titleToVar($params['option_label'][$k]); | |
372 | $optionValue->value = trim($v); | |
373 | $optionValue->weight = $params['option_weight'][$k]; | |
374 | $optionValue->is_active = 1; | |
375 | ||
a7488080 | 376 | if (!empty($params['default_option']) && |
6a488035 TO |
377 | $params['default_option'] == $k |
378 | ) { | |
379 | $optionValue->is_default = 1; | |
380 | } | |
381 | ||
382 | $optionValue->save(); | |
383 | ||
384 | // using is_numeric since 0 is a valid value for option_interval | |
385 | if ( is_numeric($params['option_interval'][$k])) { | |
386 | $recontactInterval[$optionValue->label] = $params['option_interval'][$k]; | |
387 | } | |
388 | } | |
389 | } | |
390 | ||
391 | $params['recontact_interval'] = serialize($recontactInterval); | |
392 | $survey = CRM_Campaign_BAO_Survey::create($params); | |
393 | ||
394 | // create report if required. | |
8cc574cf | 395 | if ( !$this->_reportId && $survey->id && !empty($params['create_report'])) { |
6a488035 TO |
396 | $activityStatus = CRM_Core_PseudoConstant::activityStatus('name'); |
397 | $activityStatus = array_flip($activityStatus); | |
8ef12e64 | 398 | $this->_params = |
6a488035 | 399 | array( 'name' => "survey_{$survey->id}", |
8ef12e64 | 400 | 'title' => $params['report_title'] ? $params['report_title'] : $this->_values['title'], |
6a488035 TO |
401 | 'status_id_op' => 'eq', |
402 | 'status_id_value' => $activityStatus['Scheduled'], // reserved status | |
8ef12e64 | 403 | 'survey_id_value' => array($survey->id), |
6a488035 TO |
404 | 'description' => ts('Detailed report for canvassing, phone-banking, walk lists or other surveys.'), |
405 | ); | |
406 | //Default value of order by | |
407 | $this->_params['order_bys'] = | |
408 | array( | |
409 | 1 => | |
410 | array( | |
411 | 'column' => 'sort_name', | |
412 | 'order' => 'ASC' | |
413 | ), | |
414 | ); | |
415 | // for WalkList or default | |
416 | $displayFields = array('id', 'sort_name', 'result', 'street_number','street_name','street_unit','survey_response'); | |
417 | if ( CRM_Core_OptionGroup::getValue('activity_type','WalkList') == $this->_values['activity_type_id'] ) { | |
418 | $this->_params['order_bys'] = | |
419 | array( | |
420 | 1 => | |
421 | array( | |
422 | 'column' => 'street_name', | |
423 | 'order' => 'ASC' | |
424 | ), | |
425 | 2 => | |
426 | array( | |
427 | 'column' => 'street_number_odd_even', | |
428 | 'order' => 'ASC' | |
429 | ), | |
430 | 3 => | |
431 | array( | |
432 | 'column' => 'street_number', | |
433 | 'order' => 'ASC' | |
434 | ), | |
435 | 4 => | |
436 | array( | |
437 | 'column' => 'sort_name', | |
438 | 'order' => 'ASC' | |
439 | ), | |
440 | ); | |
441 | } | |
442 | elseif ( CRM_Core_OptionGroup::getValue('activity_type','PhoneBank') == $this->_values['activity_type_id'] ) { | |
443 | array_push($displayFields, 'phone'); | |
444 | } | |
8ef12e64 | 445 | elseif ((CRM_Core_OptionGroup::getValue('activity_type','Survey') == $this->_values['activity_type_id']) || |
6a488035 TO |
446 | (CRM_Core_OptionGroup::getValue('activity_type','Canvass') == $this->_values['activity_type_id']) ) { |
447 | array_push($displayFields, 'phone','city','state_province_id','postal_code','email'); | |
448 | } | |
449 | foreach($displayFields as $key){ | |
450 | $this->_params['fields'][$key] = 1; | |
8ef12e64 | 451 | } |
6a488035 TO |
452 | $this->_createNew = TRUE; |
453 | $this->_id = CRM_Report_Utils_Report::getInstanceIDForValue('survey/detail'); | |
454 | CRM_Report_Form_Instance::postProcess($this, FALSE); | |
8ef12e64 | 455 | |
6a488035 TO |
456 | $query = "SELECT MAX(id) FROM civicrm_report_instance WHERE name = %1"; |
457 | $reportID = CRM_Core_DAO::singleValueQuery($query, array(1 => array("survey_{$survey->id}",'String'))); | |
458 | if ($reportID) { | |
459 | $url = CRM_Utils_System::url("civicrm/report/instance/{$reportID}",'reset=1'); | |
8ef12e64 | 460 | $status = ts("A Survey Detail Report <a href='%1'>%2</a> has been created.", |
6a488035 TO |
461 | array(1 => $url, 2 => $this->_params['title'])); |
462 | } | |
463 | } | |
464 | ||
465 | if ($status) { | |
466 | // reset status as we don't want status set by Instance::postProcess | |
467 | $session = CRM_Core_Session::singleton(); | |
468 | $session->getStatus(TRUE); | |
469 | // set new status | |
470 | CRM_Core_Session::setStatus($status, ts('Saved'), 'success'); | |
471 | } | |
472 | ||
473 | parent::endPostProcess(); | |
474 | } | |
475 | } |