/**
  * Handle content posted by a user.
  * This should be called before you try to list new participations (else, freshly submited content won't be shwown)
  * 
  * Valid order is :
  * - $this->handlePost()
  * - list and display validated participations
  * - show participation form ($this->render() )
  *
  * handlePost() does nothing if the participation form has not been submited 
  *
  */
 function handlePost()
 {
     global $thinkedit;
     // if form sent, validate
     if ($this->form->isSent()) {
         $this->content->setArray($_POST);
         $valid_captcha = true;
         if ($this->enable_captcha) {
             require_once ROOT . '/class/captcha.class.php';
             $captcha = new captcha();
             if ($_REQUEST['captcha'] != $captcha->get()) {
                 $valid_captcha = false;
             }
         }
         // first case : invalid content
         if (!$this->content->validate() || !$valid_captcha) {
             $this->form->add('<div class="participation_error">');
             $this->form->add($this->invalid_message);
             $this->form->add('</div>');
         } else {
             // if a captcha was used, reset it in order to have a new one for another message
             if ($this->enable_captcha) {
                 $captcha->reset();
                 unset($_REQUEST['captcha']);
             }
             $failure = false;
             // save content to db
             if (!$this->content->insert()) {
                 $failure = true;
             }
             // add content to curent node
             if (isset($this->parent_node)) {
                 // add content in the container
                 $new_node = $this->parent_node->add($this->content, 'bottom');
                 if (!$new_node) {
                     $failure = true;
                 }
                 /*
                 // update db
                 if (!$new_node->save())
                 {
                 		$failure = true;
                 }
                 */
                 //echo 'publish : ' . $new_node->get('publish');
                 // publish if needed
                 if ($this->enable_moderation) {
                     //echo 'moderation enabled';
                 } else {
                     //echo 'moderation disabled, publishing directly';
                     $new_node->publish();
                 }
                 //echo 'publish after : ' . $new_node->get('publish');
                 /*
                 // move to bottom of curent branch if needed
                 if ($this->move_to_bottom)
                 {
                 		$new_node->moveBottom();
                 		echo 'publish after move to bottom : ' . $new_node->get('publish');
                 }
                 else
                 {
                 		$new_node->rebuild();
                 		echo 'publish after rebuild : ' . $new_node->get('publish');
                 }
                 */
             }
             if ($failure) {
                 $this->form->add('<div class="participation_error">');
                 $this->form->add($this->failure_message);
                 $this->form->add('</div>');
             } else {
                 if (isset($this->notification_email)) {
                     require_once ROOT . '/class/mailer.class.php';
                     $mailer = new mailer();
                     $mailer->isHtml(true);
                     $mailer->setTo($this->notification_email);
                     // todo : find the first email field type in the record to use it as a sender
                     // $mailer->setFrom($this->notification_email);
                     $mailer->setSubject($this->notification_email_subject . $this->content->getTitle());
                     $message = '';
                     foreach ($this->content->field as $field) {
                         $message .= '<b>' . $field->getTitle();
                         $message .= ' : ' . '</b>';
                         $message .= '<br/>';
                         $message .= $field->get();
                         $message .= '<br/>';
                         $message .= '<br/>';
                     }
                     $url = $thinkedit->newUrl();
                     $url->set('node_id', $this->parent_node->getId());
                     $message .= '<a href="' . $url->renderAbsoluteUrl('/edit/structure.php') . '">' . translate('participation_email_admin_link') . '</a>';
                     $mailer->setBody($message);
                     $mailer->send();
                 }
                 $this->form->add('<div class="participation_success">');
                 $this->form->add($this->success_message);
                 $this->form->add('</div>');
             }
         }
     }
 }