public function actionPostSubmission() { $this->requirePostRequest(); $submission = new Formerly_SubmissionModel(); $submission->formId = craft()->request->getRequiredPost('formId'); //check file upload for errors, craft will not be happy otherwise $errors = false; if (sizeof($_FILES) > 0 && array_key_exists('questions', $_FILES) && array_key_exists('error', $_FILES['questions'])) { foreach ($_FILES['questions']['error'] as $key => $error) { switch ($error) { case UPLOAD_ERR_OK: break; case UPLOAD_ERR_NO_FILE: break; case UPLOAD_ERR_INI_SIZE: case UPLOAD_ERR_FORM_SIZE: $submission->addError($key, 'File size exceeded size limit.'); $errors = true; //clear key so setContentFromPost works unset($_FILES['questions']['name'][$key]); unset($_FILES['questions']['error'][$key]); unset($_FILES['questions']['type'][$key]); unset($_FILES['questions']['size'][$key]); unset($_FILES['questions']['tmp_name'][$key]); break; default: $submission->addError($key, 'File upload failed.'); //clear key so setContentFromPost works unset($_FILES['questions']['name'][$key]); unset($_FILES['questions']['error'][$key]); unset($_FILES['questions']['type'][$key]); unset($_FILES['questions']['size'][$key]); unset($_FILES['questions']['tmp_name'][$key]); $errors = true; } } if ($errors) { $submission->setContentFromPost('questions'); craft()->urlManager->setRouteVariables(array('submission' => $submission)); return; } } $submission->setContentFromPost('questions'); if (craft()->formerly_submissions->postSubmission($submission)) { if (craft()->request->isAjaxRequest()) { $this->returnJson(array('ok' => 'yes', 'id' => $submission->id)); } else { $this->redirectToPostedUrl($submission); } } else { if (craft()->request->isAjaxRequest()) { $this->returnJson(array('ok' => 'no', 'errors' => $submission->getErrors())); } else { craft()->urlManager->setRouteVariables(array('submission' => $submission)); } } }
public function populateElementModel($row) { return Formerly_SubmissionModel::populateModel($row); }
private function _renderSubmissionTemplate($template, Formerly_SubmissionModel $submission) { $formHandle = $submission->getForm()->handle; $formattedTemplate = $template; //check that all the tags are valid before passing them to the template engine, otherwise it //crashes with a obscure error preg_match_all('/{([^}]*)}/', $template, $matches); $qs = $submission->getForm()->getQuestions(); $tagsAllFound = true; foreach ($matches[1] as $a) { foreach ($qs as $q) { if ($q->handle == $formHandle . '_' . $a) { //this is a valid twig field replace it with a temporary start and end tag //(because we want to replace all non matches later with something so twig doesn't try to replace the nonmatches) $formattedTemplate = str_replace("{" . $a . "}", "@@@1" . $formHandle . '_' . $a . '1@@@', $formattedTemplate); break; } } } //replace any stragglers $formattedTemplate = str_replace("{", "<<<", $formattedTemplate); $formattedTemplate = str_replace("}", ">>>", $formattedTemplate); //fix up actual matches $formattedTemplate = str_replace("@@@1", "{", $formattedTemplate); $formattedTemplate = str_replace("1@@@", "}", $formattedTemplate); $result = craft()->templates->renderObjectTemplate($formattedTemplate, $submission); //put unmatched handles back the way they were $result = str_replace('<<<', '{', $result); $result = str_replace(">>>", "}", $result); return $result; }
private function _renderSubmissionTemplate($template, Formerly_SubmissionModel $submission) { $formHandle = $submission->getForm()->handle; $formattedTemplate = $template; //check that all the tags are valid before passing them to the template engine, otherwise it //crashes with an obscure error preg_match_all('/{(.+?)(\\||})/', $template, $matches); $qs = $submission->getForm()->getQuestions(); $tagsAllFound = true; foreach ($matches[1] as $a) { foreach ($qs as $q) { if (strstr($formHandle . '_' . $a, $q->handle)) { //if multiple result field do replace now //this is a valid twig field replace it with a temporary start and end tag //(because we want to replace all non matches later with something so twig doesn't try to replace the nonmatches) $formattedTemplate = str_replace("{" . $a . "}", "@@@1" . $formHandle . '_' . $a . '1@@@', $formattedTemplate); $formattedTemplate = str_replace("{" . $a . "|", "@@@2" . $formHandle . '_' . $a . '2@@@', $formattedTemplate); break; } } } //replace any stragglers preg_match_all('/{(.+?)(\\||})/', $template, $matches); foreach ($matches[1] as $a) { $formattedTemplate = str_replace("{" . $a . "}", "<<<" . $a . '>>>', $formattedTemplate); } //fix up actual matches $formattedTemplate = str_replace("@@@1", "{", $formattedTemplate); $formattedTemplate = str_replace("1@@@", "}", $formattedTemplate); $formattedTemplate = str_replace("@@@2", "{", $formattedTemplate); $formattedTemplate = str_replace("2@@@", "|", $formattedTemplate); $result = craft()->templates->renderObjectTemplate($formattedTemplate, $submission); //put unmatched handles back the way they were $result = str_replace('<<<', '{', $result); $result = str_replace(">>>", "}", $result); $result = str_replace("{id}", $submission->id, $result); $siteUrl = craft()->config->get("siteUrl"); if (is_array($siteUrl) && count($siteUrl) > 0) { $result = str_replace("{siteUrl}", $siteUrl[CRAFT_LOCALE], $result); } else { $result = str_replace("{siteUrl}", $siteUrl, $result); } return $result; }