| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.6 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
| 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-2015 |
| 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 | * Set default values for the form. Note that in edit/view mode |
| 78 | * the default values are retrieved from the database |
| 79 | * |
| 80 | * @return array |
| 81 | * array of default values |
| 82 | */ |
| 83 | public function setDefaultValues() { |
| 84 | $defaults = $this->_values; |
| 85 | |
| 86 | // set defaults for weight. |
| 87 | for ($i = 1; $i <= self::NUM_OPTION; $i++) { |
| 88 | $defaults["option_weight[{$i}]"] = $i; |
| 89 | } |
| 90 | |
| 91 | $defaults['create_report'] = 1; |
| 92 | if ($this->_reportId) { |
| 93 | $defaults['report_title'] = $this->_reportTitle; |
| 94 | } |
| 95 | return $defaults; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Build the form object. |
| 100 | * |
| 101 | * @return void |
| 102 | */ |
| 103 | public function buildQuickForm() { |
| 104 | $optionGroups = CRM_Campaign_BAO_Survey::getResultSets(); |
| 105 | |
| 106 | if (empty($optionGroups)) { |
| 107 | $optionTypes = array('1' => ts('Create new result set')); |
| 108 | } |
| 109 | else { |
| 110 | $optionTypes = array( |
| 111 | '1' => ts('Create new result set'), |
| 112 | '2' => ts('Use existing result set'), |
| 113 | ); |
| 114 | $this->add('select', |
| 115 | 'option_group_id', |
| 116 | ts('Select Result Set'), |
| 117 | array( |
| 118 | '' => ts('- select -'), |
| 119 | ) + $optionGroups, FALSE, |
| 120 | array('onChange' => 'loadOptionGroup( )') |
| 121 | ); |
| 122 | } |
| 123 | |
| 124 | $element = &$this->addRadio('option_type', |
| 125 | ts('Survey Responses'), |
| 126 | $optionTypes, |
| 127 | array( |
| 128 | 'onclick' => "showOptionSelect();", |
| 129 | ), '<br/>', TRUE |
| 130 | ); |
| 131 | |
| 132 | if (empty($optionGroups) || empty($this->_values['result_id'])) { |
| 133 | $this->setdefaults(array('option_type' => 1)); |
| 134 | } |
| 135 | elseif (!empty($this->_values['result_id'])) { |
| 136 | $this->setdefaults(array( |
| 137 | 'option_type' => 2, |
| 138 | 'option_group_id' => $this->_values['result_id'], |
| 139 | )); |
| 140 | } |
| 141 | |
| 142 | // form fields of Custom Option rows |
| 143 | $defaultOption = array(); |
| 144 | $_showHide = new CRM_Core_ShowHideBlocks('', ''); |
| 145 | |
| 146 | $optionAttributes = CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue'); |
| 147 | $optionAttributes['label']['size'] = $optionAttributes['value']['size'] = 25; |
| 148 | |
| 149 | for ($i = 1; $i <= self::NUM_OPTION; $i++) { |
| 150 | //the show hide blocks |
| 151 | $showBlocks = 'optionField_' . $i; |
| 152 | if ($i > 2) { |
| 153 | $_showHide->addHide($showBlocks); |
| 154 | if ($i == self::NUM_OPTION) { |
| 155 | $_showHide->addHide('additionalOption'); |
| 156 | } |
| 157 | } |
| 158 | else { |
| 159 | $_showHide->addShow($showBlocks); |
| 160 | } |
| 161 | |
| 162 | $this->add('text', 'option_label[' . $i . ']', ts('Label'), |
| 163 | $optionAttributes['label'] |
| 164 | ); |
| 165 | |
| 166 | // value |
| 167 | $this->add('text', 'option_value[' . $i . ']', ts('Value'), |
| 168 | $optionAttributes['value'] |
| 169 | ); |
| 170 | |
| 171 | // weight |
| 172 | $this->add('text', "option_weight[$i]", ts('Order'), |
| 173 | $optionAttributes['weight'] |
| 174 | ); |
| 175 | |
| 176 | $this->add('text', 'option_interval[' . $i . |
| 177 | ']', ts('Recontact Interval'), |
| 178 | CRM_Core_DAO::getAttribute('CRM_Campaign_DAO_Survey', 'release_frequency') |
| 179 | ); |
| 180 | |
| 181 | $defaultOption[$i] = $this->createElement('radio', NULL, NULL, NULL, $i); |
| 182 | } |
| 183 | |
| 184 | //default option selection |
| 185 | $this->addGroup($defaultOption, 'default_option'); |
| 186 | |
| 187 | $_showHide->addToTemplate(); |
| 188 | |
| 189 | $this->addElement('checkbox', 'create_report', ts('Create Report')); |
| 190 | $this->addElement('text', 'report_title', ts('Report Title')); |
| 191 | |
| 192 | if ($this->_reportId) { |
| 193 | $this->freeze('create_report'); |
| 194 | $this->freeze('report_title'); |
| 195 | } |
| 196 | |
| 197 | $this->addFormRule(array( |
| 198 | 'CRM_Campaign_Form_Survey_Results', |
| 199 | 'formRule', |
| 200 | ), $this); |
| 201 | |
| 202 | parent::buildQuickForm(); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Global validation rules for the form. |
| 207 | * |
| 208 | * @param $fields |
| 209 | * @param $files |
| 210 | * @param $form |
| 211 | * |
| 212 | * @return array|bool |
| 213 | */ |
| 214 | public static function formRule($fields, $files, $form) { |
| 215 | $errors = array(); |
| 216 | if (!empty($fields['option_label']) && !empty($fields['option_value']) && |
| 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 | } |
| 223 | elseif (empty($fields['option_label']) && empty($fields['option_value'])) { |
| 224 | return $errors; |
| 225 | } |
| 226 | |
| 227 | if ( |
| 228 | $fields['option_type'] == 2 && empty($fields['option_group_id']) |
| 229 | ) { |
| 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) { |
| 248 | if ($fields['option_value'][$start] == |
| 249 | $fields['option_value'][$nextIndex] && |
| 250 | !empty($fields['option_value'][$nextIndex]) |
| 251 | ) { |
| 252 | |
| 253 | $errors['option_value[' . $start . |
| 254 | ']'] = ts('Duplicate Option values'); |
| 255 | $errors['option_value[' . $nextIndex . |
| 256 | ']'] = ts('Duplicate Option values'); |
| 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) { |
| 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'); |
| 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]) { |
| 297 | $errors['option_label[' . $i . |
| 298 | ']'] = ts('Option label cannot be empty'); |
| 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]) { |
| 307 | $errors['option_value[' . $i . |
| 308 | ']'] = ts('Option value cannot be empty'); |
| 309 | $_flagOption = 1; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | if (!empty($fields['option_interval'][$i]) && |
| 314 | !CRM_Utils_Rule::integer($fields['option_interval'][$i]) |
| 315 | ) { |
| 316 | $_flagOption = 1; |
| 317 | $errors['option_interval[' . $i . |
| 318 | ']'] = ts('Please enter a valid integer.'); |
| 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 | * @return void |
| 350 | */ |
| 351 | public function postProcess() { |
| 352 | // store the submitted values in an array |
| 353 | $status = ''; |
| 354 | $params = $this->controller->exportValues($this->_name); |
| 355 | $params['id'] = $this->_surveyId; |
| 356 | |
| 357 | $updateResultSet = FALSE; |
| 358 | $resultSetOptGrpId = NULL; |
| 359 | if ((CRM_Utils_Array::value('option_type', $params) == 2) && |
| 360 | !empty($params['option_group_id']) |
| 361 | ) { |
| 362 | $updateResultSet = TRUE; |
| 363 | $resultSetOptGrpId = $params['option_group_id']; |
| 364 | } |
| 365 | |
| 366 | $recontactInterval = array(); |
| 367 | if ($updateResultSet) { |
| 368 | $optionValue = new CRM_Core_DAO_OptionValue(); |
| 369 | $optionValue->option_group_id = $resultSetOptGrpId; |
| 370 | $optionValue->delete(); |
| 371 | |
| 372 | $params['result_id'] = $resultSetOptGrpId; |
| 373 | } |
| 374 | else { |
| 375 | $opGroupName = 'civicrm_survey_' . rand(10, 1000) . '_' . date('YmdHis'); |
| 376 | |
| 377 | $optionGroup = new CRM_Core_DAO_OptionGroup(); |
| 378 | $optionGroup->name = $opGroupName; |
| 379 | $optionGroup->title = $this->_values['title'] . ' Result Set'; |
| 380 | $optionGroup->is_active = 1; |
| 381 | $optionGroup->save(); |
| 382 | |
| 383 | $params['result_id'] = $optionGroup->id; |
| 384 | } |
| 385 | |
| 386 | foreach ($params['option_value'] as $k => $v) { |
| 387 | if (strlen(trim($v))) { |
| 388 | $optionValue = new CRM_Core_DAO_OptionValue(); |
| 389 | $optionValue->option_group_id = $params['result_id']; |
| 390 | $optionValue->label = $params['option_label'][$k]; |
| 391 | $optionValue->name = CRM_Utils_String::titleToVar($params['option_label'][$k]); |
| 392 | $optionValue->value = trim($v); |
| 393 | $optionValue->weight = $params['option_weight'][$k]; |
| 394 | $optionValue->is_active = 1; |
| 395 | |
| 396 | if (!empty($params['default_option']) && |
| 397 | $params['default_option'] == $k |
| 398 | ) { |
| 399 | $optionValue->is_default = 1; |
| 400 | } |
| 401 | |
| 402 | $optionValue->save(); |
| 403 | |
| 404 | // using is_numeric since 0 is a valid value for option_interval |
| 405 | if (is_numeric($params['option_interval'][$k])) { |
| 406 | $recontactInterval[$optionValue->label] = $params['option_interval'][$k]; |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | $params['recontact_interval'] = serialize($recontactInterval); |
| 412 | $survey = CRM_Campaign_BAO_Survey::create($params); |
| 413 | |
| 414 | // create report if required. |
| 415 | if (!$this->_reportId && $survey->id && !empty($params['create_report'])) { |
| 416 | $activityStatus = CRM_Core_PseudoConstant::activityStatus('name'); |
| 417 | $activityStatus = array_flip($activityStatus); |
| 418 | $this->_params = array( |
| 419 | 'name' => "survey_{$survey->id}", |
| 420 | 'title' => $params['report_title'] ? $params['report_title'] : $this->_values['title'], |
| 421 | 'status_id_op' => 'eq', |
| 422 | 'status_id_value' => $activityStatus['Scheduled'], // reserved status |
| 423 | 'survey_id_value' => array($survey->id), |
| 424 | 'description' => ts('Detailed report for canvassing, phone-banking, walk lists or other surveys.'), |
| 425 | ); |
| 426 | //Default value of order by |
| 427 | $this->_params['order_bys'] = array( |
| 428 | 1 => array( |
| 429 | 'column' => 'sort_name', |
| 430 | 'order' => 'ASC', |
| 431 | ), |
| 432 | ); |
| 433 | // for WalkList or default |
| 434 | $displayFields = array( |
| 435 | 'id', |
| 436 | 'sort_name', |
| 437 | 'result', |
| 438 | 'street_number', |
| 439 | 'street_name', |
| 440 | 'street_unit', |
| 441 | 'survey_response', |
| 442 | ); |
| 443 | if (CRM_Core_OptionGroup::getValue('activity_type', 'WalkList') == |
| 444 | $this->_values['activity_type_id'] |
| 445 | ) { |
| 446 | $this->_params['order_bys'] = array( |
| 447 | 1 => array( |
| 448 | 'column' => 'street_name', |
| 449 | 'order' => 'ASC', |
| 450 | ), |
| 451 | 2 => array( |
| 452 | 'column' => 'street_number_odd_even', |
| 453 | 'order' => 'ASC', |
| 454 | ), |
| 455 | 3 => array( |
| 456 | 'column' => 'street_number', |
| 457 | 'order' => 'ASC', |
| 458 | ), |
| 459 | 4 => array( |
| 460 | 'column' => 'sort_name', |
| 461 | 'order' => 'ASC', |
| 462 | ), |
| 463 | ); |
| 464 | } |
| 465 | elseif (CRM_Core_OptionGroup::getValue('activity_type', 'PhoneBank') == |
| 466 | $this->_values['activity_type_id'] |
| 467 | ) { |
| 468 | array_push($displayFields, 'phone'); |
| 469 | } |
| 470 | elseif ((CRM_Core_OptionGroup::getValue('activity_type', 'Survey') == |
| 471 | $this->_values['activity_type_id']) || |
| 472 | (CRM_Core_OptionGroup::getValue('activity_type', 'Canvass') == |
| 473 | $this->_values['activity_type_id']) |
| 474 | ) { |
| 475 | array_push($displayFields, 'phone', 'city', 'state_province_id', 'postal_code', 'email'); |
| 476 | } |
| 477 | foreach ($displayFields as $key) { |
| 478 | $this->_params['fields'][$key] = 1; |
| 479 | } |
| 480 | $this->_createNew = TRUE; |
| 481 | $this->_id = CRM_Report_Utils_Report::getInstanceIDForValue('survey/detail'); |
| 482 | CRM_Report_Form_Instance::postProcess($this, FALSE); |
| 483 | |
| 484 | $query = "SELECT MAX(id) FROM civicrm_report_instance WHERE name = %1"; |
| 485 | $reportID = CRM_Core_DAO::singleValueQuery($query, array( |
| 486 | 1 => array( |
| 487 | "survey_{$survey->id}", |
| 488 | 'String', |
| 489 | ), |
| 490 | )); |
| 491 | if ($reportID) { |
| 492 | $url = CRM_Utils_System::url("civicrm/report/instance/{$reportID}", 'reset=1'); |
| 493 | $status = ts("A Survey Detail Report <a href='%1'>%2</a> has been created.", |
| 494 | array(1 => $url, 2 => $this->_params['title'])); |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | if ($status) { |
| 499 | // reset status as we don't want status set by Instance::postProcess |
| 500 | $session = CRM_Core_Session::singleton(); |
| 501 | $session->getStatus(TRUE); |
| 502 | // set new status |
| 503 | CRM_Core_Session::setStatus($status, ts('Saved'), 'success'); |
| 504 | } |
| 505 | |
| 506 | parent::endPostProcess(); |
| 507 | } |
| 508 | |
| 509 | } |