Example #1
0
 public function executeCommentStreamUpdatingUsingAjax(HTTPRequest $Request)
 {
     $CommentManager = $this->managers->getManagerOf('Comments');
     $Comments_a = $CommentManager->getAfterId($Request->postData('news_id'), $Request->postData('last_insert_id'));
     $this->page->setTemplate('jsonLayout.php');
     if (!$Comments_a) {
         $this->page->addVar('ajax', json_encode(array('success' => false, 'message' => 'aucun nouveau commentaire')));
         return;
     }
     $comments_views_a = array();
     $Router = new Router();
     foreach ($Comments_a as $Comment) {
         $PageComment = new Page($this->app);
         $PageComment->setContentFile(__DIR__ . '/Views/comment.php');
         $PageComment->addVar('comment', $Comment);
         $PageComment->addVar('router', $Router);
         $PageComment->setTemplate('jsonLayout.php');
         $comments_views_a[$Comment->id()] = $PageComment->getGeneratedPage();
     }
     $ajax = array('success' => true, 'comments' => $comments_views_a, 'validation_message' => 'Votre message a bien été ajouté.');
     $this->page->addVar('ajax', json_encode($ajax));
 }
Example #2
0
 public function executeInsertCommentUsingAjax(HTTPRequest $request)
 {
     $CommentManager = $this->managers->getManagerOf('Comments');
     $NewsManager = $this->managers->getManagerOf('News');
     $this->page->setTemplate('jsonLayout.php');
     $News = $NewsManager->getUnique($request->getData('news_id'));
     if (!$News) {
         $this->page->addVar('ajax', json_encode(array('success' => false, 'erreurs' => 'Cette news n\\existe pas !')));
         return;
     }
     $Comment = new Comment(['newsId' => $request->getData('news_id'), 'auteurId' => $this->app->user()->getAttribute('user')->id(), 'contenu' => $request->postData('contenu')]);
     $FormBuilder = new CommentFormBuilder($Comment);
     $FormBuilder->build();
     $Form = $FormBuilder->form();
     // On récupère le gestionnaire de formulaire (le paramètre de getManagerOf() est bien entendu à remplacer).
     $FormHandler = new \OCFram\FormHandler($Form, $CommentManager, $request);
     if (!$FormHandler->process()) {
         $errors_a = [];
         foreach ($Form->fields() as $Field) {
             $Field->isValid();
             $errors_a[$Field->id()] = $Field->errorMessage();
         }
         $this->page->addVar('erreurs', json_encode(array('erreurs' => $errors_a)));
         return;
     }
     $Comments_a = $CommentManager->getAfterId($News->id(), $request->postData('last_insert_id'));
     $comments_views_a = array();
     $Router = new Router();
     foreach ($Comments_a as $Comment) {
         $PageComment = new Page($this->app);
         $PageComment->setContentFile(__DIR__ . '/Views/comment.php');
         $PageComment->addVar('comment', $Comment);
         $PageComment->addVar('router', $Router);
         $PageComment->setTemplate('jsonLayout.php');
         $comments_views_a[$Comment->id()] = $PageComment->getGeneratedPage();
     }
     $ajax = array('success' => true, 'comments' => $comments_views_a, 'validation_message' => 'Votre message a bien été ajouté.');
     $this->page->addVar('ajax', json_encode($ajax));
 }