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