Example #1
0
 /**
  * Adds a form.
  *
  * Called when this component receives an HTTP POST request to
  * /form(/).
  */
 public function addForm()
 {
     Logger::Log('starts POST AddForm', LogLevel::DEBUG);
     $header = $this->app->request->headers->all();
     $body = $this->app->request->getBody();
     $this->app->response->setStatus(201);
     $forms = Form::decodeForm($body);
     // always been an array
     $arr = true;
     if (!is_array($forms)) {
         $forms = array($forms);
         $arr = false;
     }
     // this array contains the indices of the inserted objects
     $res = array();
     $choices = array();
     foreach ($forms as $key => $form) {
         $choices[] = $form->getChoices();
         $forms[$key]->setChoices(null);
     }
     $resForms = array();
     foreach ($forms as $form) {
         $method = 'POST';
         $URL = '/form';
         if ($form->getFormId() !== null) {
             $method = 'PUT';
             $URL = '/form/' . $form->getFormId();
         }
         $result = Request::routeRequest($method, $URL, array(), Form::encodeForm($form), $this->_form, 'form');
         // checks the correctness of the query
         if ($result['status'] >= 200 && $result['status'] <= 299 && isset($result['content'])) {
             $newform = Form::decodeForm($result['content']);
             if ($form->getFormId() === null) {
                 $form->setFormId($newform->getFormId());
             }
             $resForms[] = $form;
         } else {
             $f = new Form();
             $f->setStatus(409);
             $resForms[] = $f;
         }
     }
     $forms = $resForms;
     $i = 0;
     foreach ($choices as &$choicelist) {
         foreach ($choicelist as $key2 => $choice) {
             if ($forms[$i]->getFormId() !== null) {
                 $formId = $forms[$i]->getFormId();
                 $choicelist[$key2]->setFormId($formId);
                 $method = 'POST';
                 $URL = '/choice';
                 if ($choicelist[$key2]->getChoiceId() !== null) {
                     $method = 'PUT';
                     $URL = '/choice/' . $choice->getChoiceId();
                 }
                 $result = Request::routeRequest($method, $URL, array(), Choice::encodeChoice($choicelist[$key2]), $this->_choice, 'choice');
                 if ($result['status'] >= 200 && $result['status'] <= 299) {
                     $newchoice = Choice::decodeChoice($result['content']);
                     if ($choicelist[$key2]->getChoiceId() === null) {
                         $choicelist[$key2]->setChoiceId($newchoice->getChoiceId());
                     }
                     $choicelist[$key2]->setStatus(201);
                 } else {
                     $choicelist[$key2]->setStatus(409);
                 }
             }
         }
         $forms[$i]->setChoices($choicelist);
         $i++;
     }
     // checks the correctness of the query
     /*if ( $result['status'] >= 200 && 
                  $result['status'] <= 299 && isset($result['content'])){
                 $newforms = Form::decodeForm($result['content']);
                 if ( !is_array( $newforms ) )
                     $newforms = array($newforms);
                     
                 $i=0;    
                 foreach ($forms as &$form){
                     if ($form->getFormId() === null)
                         $form->setFormId($newforms[$i]->getFormId());
                     $i++;
                 }            
     
                 $sendChoices = array();
                 $i=0;
                 foreach ( $choices as $choicelist ){
                     foreach ( $choicelist as $choice ){
                         $choice->setFormId($forms[$i]->getFormId());
                         $sendChoices[] = $choice;
                     }
                 $i++; 
                 }
                 $choices = $sendChoices;
                 
                 $result = Request::routeRequest( 
                                                 'POST',
                                                 '/choice',
                                                 $this->app->request->headers->all( ),
                                                 Choice::encodeChoice($choices),
                                                 $this->_choice,
                                                 'choice'
                                                 );
                                 
                 // checks the correctness of the query
                 if ( $result['status'] >= 200 && 
                      $result['status'] <= 299 ){
                     $newchoices = Choice::decodeChoice($result['content']);
     
                     $choicelist = array();
                     $i=0;
                     foreach ( $choices as &$choice ){
                         $choice->setChoiceId($newchoices[$i]->getChoiceId());
                         
                         if (!isset($choicelist[$choice->getFormId()]))
                             $choicelist[$choice->getFormId()] = array();
                           
                         $choicelist[$choice->getFormId()][] = $choice;
                         
                         $i++;
                     }
                     
                     foreach ( $forms as &$form ){
                         $form->setChoices($choicelist[$form->getFormId()]);
                     }
                     
                     $res[] = $forms;
                     
                 } else{
                     // remove forms on failure
                     foreach ($forms as $form){
                         $result = Request::routeRequest( 
                                         'DELETE',
                                         '/form/'.$form->getFormId(),
                                         $this->app->request->headers->all( ),
                                         '',
                                         $this->_form,
                                         'form'
                                         );
                     }
                     
                     $res[] = null;
                     $this->app->response->setStatus( 409 );
                 }
                           
             } else {
                 $res[] = null;
                 $this->app->response->setStatus( 409 );
             }*/
     if ($this->app->response->getStatus() != 201) {
         Logger::Log('POST AddForms failed', LogLevel::ERROR);
     }
     if (!$arr && count($res) == 1) {
         $this->app->response->setBody(Form::encodeForm($res[0]));
     } else {
         $this->app->response->setBody(Form::encodeForm($res));
     }
 }