+ // {{{ function handler_edit() : edits a survey (in normal mode unless called by handler_adminEdit() )
+ function handler_edit($page, $action = 'show', $qid = 'root')
+ {
+ $this->load('survey.inc.php');
+ $action = Post::v('survey_action', $action);
+ $qid = Post::v('survey_qid', $qid);
+ if (Post::has('survey_cancel')) { // after cancelling changes, shows the survey
+ if (S::has('survey')) {
+ $action = 'show';
+ } else { // unless no editing has been done at all (shows to the surveys index page)
+ return $this->handler_index($page);
+ }
+ }
+ $page->assign('survey_editmode', true);
+ if (S::has('survey_id')) { // if 'survey_id' is in session, it means we are modifying a survey in admin mode
+ $page->assign('survey_updatemode', true);
+ }
+ if ($action == 'show' && !S::has('survey')) {
+ $action = 'new';
+ }
+ if ($action == 'question') { // {{{ modifies an existing question
+ if (Post::has('survey_submit')) { // if the form has been submitted, makes the modifications
+ $survey = unserialize(S::v('survey'));
+ $args = Post::v('survey_question');
+ if (!$survey->editQuestion($qid, $args)) { // update the survey object structure
+ return $this->show_error($page, '', 'survey/edit');
+ }
+ $this->show_survey($page, $survey);
+ $this->store_session($survey);
+ } else { // if a form has not been submitted, shows modification form
+ $survey = unserialize(S::v('survey'));
+ $current = $survey->toArray($qid); // gets the current parameters of the question
+ if ($current == null) {
+ return $this->show_error($page, '', 'survey/edit');
+ }
+ $this->show_form($page, $action, $qid, $current['type'], $current);
+ } // }}}
+ } elseif ($action == 'new') { // {{{ create a new survey : actually store the root question
+ if (Post::has('survey_submit')) { // if the form has been submitted, creates the survey
+ $this->clear_session();
+ $survey = new Survey(Post::v('survey_question')); // creates the object structure
+ $this->show_survey($page, $survey);
+ $this->store_session($survey);
+ } else {
+ $this->clear_session();
+ $this->show_form($page, $action, 'root', 'newsurvey');
+ } // }}}
+ } elseif ($action == 'add') { // {{{ adds a new question
+ if (Post::has('survey_submit')) { // if the form has been submitted, adds the question
+ $survey = unserialize(S::v('survey'));
+ if (!$survey->addQuestion($qid, $survey->factory(Post::v('survey_type'), Post::v('survey_question')))) {
+ return $this->show_error($page, '', 'survey/edit');
+ }
+ $this->show_survey($page, $survey);
+ $this->store_session($survey);
+ } else {
+ $this->show_form($page, $action, $qid);
+ } // }}}
+ } elseif ($action == 'del') { // {{{ deletes a question
+ if (Post::has('survey_submit')) { // if a confirmation has been sent, deletes the question
+ $survey = unserialize(S::v('survey'));
+ if (!$survey->delQuestion(Post::v('survey_qid'))) { // deletes the node in the survey object structure
+ return $this->show_error($page, '', 'survey/edit');
+ }
+ $this->show_survey($page, $survey);
+ $this->store_session($survey);
+ } else { // if user has not confirmed, shows a confirmation form
+ $survey = unserialize(S::v('survey'));
+ $current = $survey->toArray($qid); // needed to get the title of the question to delete (more user-friendly than an id)
+ if ($current == null) {
+ return $this->show_error($page, '', 'survey/edit');
+ }
+ $this->show_confirm($page, 'Êtes-vous certain de vouloir supprimer la question intitulé "'.$current['question'].'" ? '
+ .'Attention, cela supprimera en même temps toutes les questions qui dépendent de celle-ci.',
+ 'edit', array('action' => 'del', 'qid' => $qid));
+ } // }}}
+ } elseif ($action == 'show') { // {{{ simply shows the survey in its current state
+ $this->show_survey($page, unserialize(S::v('survey'))); // }}}
+ } elseif ($action == 'valid') { // {{{ validates the proposition, i.e stores the proposition in the database
+ // but an admin will still need to validate the survey before it is activated
+ if (Post::has('survey_submit')) { // needs a confirmation before storing the proposition
+ $survey = unserialize(S::v('survey'));
+ if (S::has('survey_id')) { // if 'survey_id' is in session, we are modifying an existing survey (in admin mode) instead of proposing a new one
+ $link = (S::has('survey_validate'))? 'admin/validate' : 'survey/admin';
+ if ($survey->updateSurvey()) { // updates the database according the new survey object structure
+ $this->show_success($page, "Les modifications sur le sondage ont bien été enregistrées.", $link);
+ } else {
+ $this->show_error($page, '', $link);
+ }
+ } else { // if no 'survey_id' is in session, we are indeed proposing a new survey
+ if ($survey->proposeSurvey()) { // stores the survey object structure in database
+ $this->show_success($page, "Votre proposition de sondage a bien été enregistrée,
+ elle est en attente de validation par un administrateur du site.", 'survey');
+ } else {
+ $this->show_error($page, '', 'survey');
+ }
+ }
+ $this->clear_session();
+ } else { // asks for a confirmation if it has not been sent
+ $survey = unserialize(S::v('survey'));
+ $errors = $survey->checkSyntax();
+ if (!is_null($errors)) {
+ $this->show_error($page, "", 'survey/edit', $errors);
+ } else {
+ if (S::has('survey_id')) {
+ $this->show_confirm($page, "Veuillez confirmer l'enregistrement des modifications apportées à ce sondage.", 'edit', array('action' => 'valid'));
+ } else {
+ $this->show_confirm($page, "Veuillez confirmer l'envoi de cette proposition de sondage.", 'edit', array('action' => 'valid'));
+ }
+ }
+ } // }}}
+ } elseif ($action == 'cancel') { // {{{ cancels the creation/modification of a survey
+ if (Post::has('survey_submit')) { // needs a confirmation
+ if (S::has('survey_id')) { // only possible when modifying a survey in admin mode
+ if (S::has('survey_validate')) { // if a link has been supplied, uses it
+ $this->clear_session();
+ return $this->show_success($page, "Les modifications effectuées ont été annulées", 'admin/validate');
+ } else { // else shows the admin index
+ $this->clear_session();
+ return $this->handler_admin($page);
+ }
+ } else {
+ $this->clear_session();
+ return $this->handler_index($page); // else shows the 'normal' index
+ }
+ } else { // asks for a confirmation if it has not been sent
+ $this->show_confirm($page, "Êtes-vous certain de vouloir annuler totalement l'édition de ce sondage ? Attention, "
+ ."toutes les données éditées jusque là seront définitivement perdues.",
+ 'edit', array('action' => $action));
+ }
+ } // }}}
+ }
+ // }}}