3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * This class generates form components for processing a survey
40 class CRM_Campaign_Form_Survey_Results
extends CRM_Campaign_Form_Survey
{
44 protected $_reportTitle;
52 const NUM_OPTION
= 11;
54 public function preProcess() {
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
);
64 $this->set('values', $this->_values
);
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
;
77 * Set default values for the form. Note that in edit/view mode
78 * the default values are retrieved from the database
81 * array of default values
83 public function setDefaultValues() {
84 $defaults = $this->_values
;
86 // set defaults for weight.
87 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
88 $defaults["option_weight[{$i}]"] = $i;
91 $defaults['create_report'] = 1;
92 if ($this->_reportId
) {
93 $defaults['report_title'] = $this->_reportTitle
;
99 * Build the form object
103 public function buildQuickForm() {
104 $optionGroups = CRM_Campaign_BAO_Survey
::getResultSets();
106 if (empty($optionGroups)) {
107 $optionTypes = array('1' => ts('Create new result set'));
110 $optionTypes = array(
111 '1' => ts('Create new result set'),
112 '2' => ts('Use existing result set'),
116 ts('Select Result Set'),
118 '' => ts('- select -'),
119 ) +
$optionGroups, FALSE,
120 array('onChange' => 'loadOptionGroup( )')
124 $element = &$this->addRadio('option_type',
125 ts('Survey Responses'),
128 'onclick' => "showOptionSelect();",
132 if (empty($optionGroups) ||
empty($this->_values
['result_id'])) {
133 $this->setdefaults(array('option_type' => 1));
135 elseif (!empty($this->_values
['result_id'])) {
136 $this->setdefaults(array(
138 'option_group_id' => $this->_values
['result_id'],
142 // form fields of Custom Option rows
143 $defaultOption = array();
144 $_showHide = new CRM_Core_ShowHideBlocks('', '');
146 $optionAttributes = CRM_Core_DAO
::getAttribute('CRM_Core_DAO_OptionValue');
147 $optionAttributes['label']['size'] = $optionAttributes['value']['size'] = 25;
149 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
150 //the show hide blocks
151 $showBlocks = 'optionField_' . $i;
153 $_showHide->addHide($showBlocks);
154 if ($i == self
::NUM_OPTION
) {
155 $_showHide->addHide('additionalOption');
159 $_showHide->addShow($showBlocks);
162 $this->add('text', 'option_label[' . $i . ']', ts('Label'),
163 $optionAttributes['label']
167 $this->add('text', 'option_value[' . $i . ']', ts('Value'),
168 $optionAttributes['value']
172 $this->add('text', "option_weight[$i]", ts('Order'),
173 $optionAttributes['weight']
176 $this->add('text', 'option_interval[' . $i .
177 ']', ts('Recontact Interval'),
178 CRM_Core_DAO
::getAttribute('CRM_Campaign_DAO_Survey', 'release_frequency')
181 $defaultOption[$i] = $this->createElement('radio', NULL, NULL, NULL, $i);
184 //default option selection
185 $this->addGroup($defaultOption, 'default_option');
187 $_showHide->addToTemplate();
189 $this->addElement('checkbox', 'create_report', ts('Create Report'));
190 $this->addElement('text', 'report_title', ts('Report Title'));
192 if ($this->_reportId
) {
193 $this->freeze('create_report');
194 $this->freeze('report_title');
197 $this->addFormRule(array(
198 'CRM_Campaign_Form_Survey_Results',
202 parent
::buildQuickForm();
206 * Global validation rules for the form
208 public static function formRule($fields, $files, $form) {
210 if (!empty($fields['option_label']) && !empty($fields['option_value']) &&
211 (count(array_filter($fields['option_label'])) == 0) &&
212 (count(array_filter($fields['option_value'])) == 0)
214 $errors['option_label[1]'] = ts('Enter at least one result option.');
217 elseif (empty($fields['option_label']) && empty($fields['option_value'])) {
222 $fields['option_type'] == 2 && empty($fields['option_group_id'])
224 $errors['option_group_id'] = ts("Please select a Survey Result Set.");
228 $_flagOption = $_rowError = 0;
229 $_showHide = new CRM_Core_ShowHideBlocks('', '');
231 //capture duplicate Custom option values
232 if (!empty($fields['option_value'])) {
233 $countValue = count($fields['option_value']);
234 $uniqueCount = count(array_unique($fields['option_value']));
236 if ($countValue > $uniqueCount) {
238 while ($start < self
::NUM_OPTION
) {
239 $nextIndex = $start +
1;
241 while ($nextIndex <= self
::NUM_OPTION
) {
242 if ($fields['option_value'][$start] ==
243 $fields['option_value'][$nextIndex] &&
244 !empty($fields['option_value'][$nextIndex])
247 $errors['option_value[' . $start .
248 ']'] = ts('Duplicate Option values');
249 $errors['option_value[' . $nextIndex .
250 ']'] = ts('Duplicate Option values');
260 //capture duplicate Custom Option label
261 if (!empty($fields['option_label'])) {
262 $countValue = count($fields['option_label']);
263 $uniqueCount = count(array_unique($fields['option_label']));
265 if ($countValue > $uniqueCount) {
267 while ($start < self
::NUM_OPTION
) {
268 $nextIndex = $start +
1;
270 while ($nextIndex <= self
::NUM_OPTION
) {
271 if ($fields['option_label'][$start] ==
272 $fields['option_label'][$nextIndex] &&
273 !empty($fields['option_label'][$nextIndex])
275 $errors['option_label[' . $start .
276 ']'] = ts('Duplicate Option label');
277 $errors['option_label[' . $nextIndex .
278 ']'] = ts('Duplicate Option label');
288 for ($i = 1; $i <= self
::NUM_OPTION
; $i++
) {
289 if (!$fields['option_label'][$i]) {
290 if ($fields['option_value'][$i]) {
291 $errors['option_label[' . $i .
292 ']'] = ts('Option label cannot be empty');
299 elseif (!strlen(trim($fields['option_value'][$i]))) {
300 if (!$fields['option_value'][$i]) {
301 $errors['option_value[' . $i .
302 ']'] = ts('Option value cannot be empty');
307 if (!empty($fields['option_interval'][$i]) &&
308 !CRM_Utils_Rule
::integer($fields['option_interval'][$i])
311 $errors['option_interval[' . $i .
312 ']'] = ts('Please enter a valid integer.');
315 $showBlocks = 'optionField_' . $i;
317 $_showHide->addShow($showBlocks);
321 if (!empty($_emptyRow)) {
322 $_showHide->addHide($showBlocks);
325 $_showHide->addShow($showBlocks);
328 if ($i == self
::NUM_OPTION
) {
329 $hideBlock = 'additionalOption';
330 $_showHide->addHide($hideBlock);
333 $_flagOption = $_emptyRow = 0;
335 $_showHide->addToTemplate();
337 return empty($errors) ?
TRUE : $errors;
345 public function postProcess() {
346 // store the submitted values in an array
348 $params = $this->controller
->exportValues($this->_name
);
349 $params['id'] = $this->_surveyId
;
351 $updateResultSet = FALSE;
352 $resultSetOptGrpId = NULL;
353 if ((CRM_Utils_Array
::value('option_type', $params) == 2) &&
354 !empty($params['option_group_id'])
356 $updateResultSet = TRUE;
357 $resultSetOptGrpId = $params['option_group_id'];
360 $recontactInterval = array();
361 if ($updateResultSet) {
362 $optionValue = new CRM_Core_DAO_OptionValue();
363 $optionValue->option_group_id
= $resultSetOptGrpId;
364 $optionValue->delete();
366 $params['result_id'] = $resultSetOptGrpId;
369 $opGroupName = 'civicrm_survey_' . rand(10, 1000) . '_' . date('YmdHis');
371 $optionGroup = new CRM_Core_DAO_OptionGroup();
372 $optionGroup->name
= $opGroupName;
373 $optionGroup->title
= $this->_values
['title'] . ' Result Set';
374 $optionGroup->is_active
= 1;
375 $optionGroup->save();
377 $params['result_id'] = $optionGroup->id
;
380 foreach ($params['option_value'] as $k => $v) {
381 if (strlen(trim($v))) {
382 $optionValue = new CRM_Core_DAO_OptionValue();
383 $optionValue->option_group_id
= $params['result_id'];
384 $optionValue->label
= $params['option_label'][$k];
385 $optionValue->name
= CRM_Utils_String
::titleToVar($params['option_label'][$k]);
386 $optionValue->value
= trim($v);
387 $optionValue->weight
= $params['option_weight'][$k];
388 $optionValue->is_active
= 1;
390 if (!empty($params['default_option']) &&
391 $params['default_option'] == $k
393 $optionValue->is_default
= 1;
396 $optionValue->save();
398 // using is_numeric since 0 is a valid value for option_interval
399 if (is_numeric($params['option_interval'][$k])) {
400 $recontactInterval[$optionValue->label
] = $params['option_interval'][$k];
405 $params['recontact_interval'] = serialize($recontactInterval);
406 $survey = CRM_Campaign_BAO_Survey
::create($params);
408 // create report if required.
409 if (!$this->_reportId
&& $survey->id
&& !empty($params['create_report'])) {
410 $activityStatus = CRM_Core_PseudoConstant
::activityStatus('name');
411 $activityStatus = array_flip($activityStatus);
412 $this->_params
= array(
413 'name' => "survey_{$survey->id}",
414 'title' => $params['report_title'] ?
$params['report_title'] : $this->_values
['title'],
415 'status_id_op' => 'eq',
416 'status_id_value' => $activityStatus['Scheduled'], // reserved status
417 'survey_id_value' => array($survey->id
),
418 'description' => ts('Detailed report for canvassing, phone-banking, walk lists or other surveys.'),
420 //Default value of order by
421 $this->_params
['order_bys'] = array(
423 'column' => 'sort_name',
427 // for WalkList or default
428 $displayFields = array(
437 if (CRM_Core_OptionGroup
::getValue('activity_type', 'WalkList') ==
438 $this->_values
['activity_type_id']
440 $this->_params
['order_bys'] = array(
442 'column' => 'street_name',
446 'column' => 'street_number_odd_even',
450 'column' => 'street_number',
454 'column' => 'sort_name',
459 elseif (CRM_Core_OptionGroup
::getValue('activity_type', 'PhoneBank') ==
460 $this->_values
['activity_type_id']
462 array_push($displayFields, 'phone');
464 elseif ((CRM_Core_OptionGroup
::getValue('activity_type', 'Survey') ==
465 $this->_values
['activity_type_id']) ||
466 (CRM_Core_OptionGroup
::getValue('activity_type', 'Canvass') ==
467 $this->_values
['activity_type_id'])
469 array_push($displayFields, 'phone', 'city', 'state_province_id', 'postal_code', 'email');
471 foreach ($displayFields as $key) {
472 $this->_params
['fields'][$key] = 1;
474 $this->_createNew
= TRUE;
475 $this->_id
= CRM_Report_Utils_Report
::getInstanceIDForValue('survey/detail');
476 CRM_Report_Form_Instance
::postProcess($this, FALSE);
478 $query = "SELECT MAX(id) FROM civicrm_report_instance WHERE name = %1";
479 $reportID = CRM_Core_DAO
::singleValueQuery($query, array(
481 "survey_{$survey->id}",
486 $url = CRM_Utils_System
::url("civicrm/report/instance/{$reportID}", 'reset=1');
487 $status = ts("A Survey Detail Report <a href='%1'>%2</a> has been created.",
488 array(1 => $url, 2 => $this->_params
['title']));
493 // reset status as we don't want status set by Instance::postProcess
494 $session = CRM_Core_Session
::singleton();
495 $session->getStatus(TRUE);
497 CRM_Core_Session
::setStatus($status, ts('Saved'), 'success');
500 parent
::endPostProcess();