/** * saveResponseAction * Save the actual save response action * * @return void * */ public function Admin_Action_SaveResponse() { $surveyId = (int) IEM::requestGetPOST('formId'); // check permission here $this->_checkSurveyAccess($surveyId); $responseId = IEM::requestGetPOST('responseId'); $responseNumber = IEM::requestGetPOST('responseNumber'); $postWidgets = IEM::requestGetPOST('widget'); $errors = 0; if ($postWidgets || $_FILES) { // If there are files, take the values and place them in the $postWidgets array so they can // get validated and entered into the response values in the same manner. Uploads will be // handled separately. if (isset($_FILES['widget'])) { foreach ($_FILES['widget']['name'] as $widgetId => $widget) { foreach ($widget as $fields) { foreach ($fields as $fieldId => $field) { if ($field['value']) { $postWidgets[$widgetId]['field'][$fieldId]['value'] = 'file_' . $field['value']; } } } } } $survey_api = $this->getApi(); $survey_api->Load($surveyId); $widgets = $survey_api->getWidgets(); $widgetErrors = array(); foreach ($widgets as $widget) { if (!isset($widgetErrors[$widget['id']])) { $widgetErrors[$widget['id']] = array(); } // validate required fields if ($widget['is_required']) { // the widget is assumed blank until one of it's fields is found not blank $isBlank = true; $isOther = false; // make sure the required widget was even posted if (isset($postWidgets[$widget['id']])) { foreach ($postWidgets[$widget['id']]['field'] as $field) { if (isset($field['value'])) { // get the value of an "other" field if it is one, otherwise just grab // the normal value if ($field['value'] == '__other__') { $isOther = true; $value = $field['other']; } else { $value = $field['value']; } // make sure the value isn't blank if (!$this->_validateIsBlank($value)) { $isBlank = false; } } } } // if the widget is blank, flag an error if ($isBlank) { if ($isOther) { $error = GetLang('Addon_Surveys_ErrorRequiredOther'); } else { $error = GetLang('Addon_Surveys_ErrorRequired'); } $widgetErrors[$widget['id']][] = $error; $errors++; } } // validate file types if (isset($postWidgets[$widget['id']]) && $widget['allowed_file_types']) { $typeArr = preg_split('/\\s*,\\s*/', strtolower($widget['allowed_file_types'])); $invalidType = false; // foreach of the passed fields (most likely 1) check and see if they are valid file types foreach ($postWidgets[$widget->id]['field'] as $field) { $parts = explode('.', $field['value']); $ext = strtolower(end($parts)); // only if the field has a value we will test its file type if (trim($field['value']) != '' && !in_array($ext, $typeArr)) { $invalidType = true; } } // if the a file is not a valid file type, then the whole widget fails validation if ($invalidType) { $lastFileType = '<em>.' . array_pop($typeArr) . '</em>'; $firstFileTypes = '<em>.' . implode('</em>, <em>.', $typeArr) . '</em>'; $widgetErrors[$widget->id][] = sprintf(GetLang('errorInvalidFileType'), $firstFileTypes, $lastFileType); $errors++; } } } // if there were errors, redirect back and display the errors if ($errors) { echo '<pre style="border: 1px solid red";><b style="color:RED;">YUDI_DEBUG:' . __FILE__ . ' ON LINE: ' . __LINE__ . '</b><br />'; print_r($widgetErrors); echo '</pre>'; die; // set the widget errors so we can retrieve them for the user IEM::sessionSet('survey.addon.widgetErrors', $widgetErrors); IEM::sessionSet('MessageText', GetLang('Addon_Surveys_saveResponseMessageError')); IEM::sessionSet('MessageType', MSG_ERROR); } else { // isntantiate a new response object $response_api = $this->getSpecificApi('responses'); $response_api->Load($responseId); // delete the values in this response, since they will be added back in $response_api->deleteValues(); // if the response was saved, then associate values to the response if ($response_api->Save()) { $responseValue = $this->getSpecificApi('responsesvalue'); // foreach of the posted widgets, check to see if it belongs in this form and save it if it does foreach ($postWidgets as $postWidgetId => $postWidget) { // iterate through each field and enter it in the feedback foreach ($postWidget['field'] as $field) { if (!isset($field['value'])) { continue; } // foreign key for the response id $responseValue->surveys_response_id = $responseId; // set the widget id foreign key; widgets can have multiple field values and // should be treated as such $responseValue->surveys_widgets_id = $postWidgetId; // set the value of the feedback; this should be a single value since widgets // can have multiple feed back values if ($field['value'] == '__other__') { $responseValue->value = $field['other']; $responseValue->is_othervalue = 1; } else { $responseValue->file_value = ""; if (substr($field['value'], 0, 5) == "file_") { $value = str_replace("file_", "", $field['value']); $responseValue->file_value = md5($value); } $responseValue->value = $field['value']; } // save it $responseValue->Save(); } } // perform file uploading if (isset($_FILES['widget']['name'])) { $files = $_FILES['widget']['name']; foreach ($files as $widgetId => $widget) { foreach ($widget as $widgetKey => $fields) { foreach ($fields as $fieldId => $field) { // gather file information $name = $_FILES['widget']['name'][$widgetId]['field'][$fieldId]['value']; $type = $_FILES['widget']['type'][$widgetId]['field'][$fieldId]['value']; $tmpName = $_FILES['widget']['tmp_name'][$widgetId]['field'][$fieldId]['value']; $error = $_FILES['widget']['error'][$widgetId]['field'][$fieldId]['value']; $size = $_FILES['widget']['size'][$widgetId]['field'][$fieldId]['value']; // if the upload was successful to the temporary folder, move it if ($error == UPLOAD_ERR_OK) { $curDir = TEMP_DIRECTORY . DIRECTORY_SEPARATOR . 'surveys'; $upBaseDir = $curDir . DIRECTORY_SEPARATOR . $surveyId; $upDir = $upBaseDir . DIRECTORY_SEPARATOR . $response_api->GetId(); // if the main survey folder is not yet created then create it if (!is_dir($curDir)) { mkdir($curDir, 0755); } // if the base upload directory doesn't exist create it if (!is_dir($upBaseDir)) { mkdir($upBaseDir, 0755); } // if the upload directory doesn't exist create it if (!is_dir($upDir)) { mkdir($upDir, 0755); } // upload the file move_uploaded_file($tmpName, $upDir . DIRECTORY_SEPARATOR . $name); } } } } } IEM::sessionSet('MessageText', GetLang('Addon_Surveys_saveResponseMessageSuccess')); IEM::sessionSet('MessageType', SS_FLASH_MSG_SUCCESS); } } } // if view is set, then go to the view page for this response if (!$errors && IEM::requestGetPOST('view')) { if (IEM::requestGetPost('viewNext')) { $responseId = IEM::requestGetPost('viewNext'); } header('Location: index.php?Page=Addons&Addon=surveys&Action=viewresponses&surveyId=' . $surveyId . '&responseId=' . $responseId); exit; } // redirect back to the edit page header('Location: index.php?Page=Addons&Addon=surveys&Action=editresponse&surveyId=' . $surveyId . '&responseId=' . $responseId); exit; }