public function processForm(HTTPRequest $request) { if ($request->method() == 'POST') { $news = new News(['auteur' => $request->postData('auteur'), 'titre' => $request->postData('titre'), 'contenu' => $request->postData('contenu')]); if ($request->getExists('id')) { $news->setId($request->getData('id')); } } else { // L'identifiant de la news est transmis si on veut la modifier if ($request->getExists('id')) { $news = $this->managers->getManagerOf('News')->getUnique($request->getData('id')); } else { $news = new News(); } } $formBuilder = new NewsFormBuilder($news); $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, $this->managers->getManagerOf('News'), $request); if ($formHandler->process()) { // Ici ne résident plus que les opérations à effectuer une fois l'entité du formulaire enregistrée // (affichage d'un message informatif, redirection, etc.). $this->app->user()->setFlash($news->isNew() ? 'La news a bien été ajoutée !' : 'La news a bien été modifiée !'); $this->app->httpResponse()->redirect('/admin/'); } $this->page->addVar('form', $form->createView()); }
/** * Méthode permettant de traiter le formulaire * et d'enregistrer la news en DB * * @param HTTPRequest $request * @return void */ public function processForm(HTTPRequest $request) { if ($request->method() == 'POST') { $news = new News(['auteur' => $request->postData('auteur'), 'titre' => $request->postData('titre'), 'contenu' => $request->postData('contenu')]); if ($request->getExists('id')) { $news->setId($request->getData('id')); } } else { // Transmission de l'identifiant de la news en cas de modification if ($request->getExists('id')) { $news = $this->managers->getManagerOf('News')->getUnique($request->getData('id')); } else { $news = new News(); } } $formBuilder = new NewsFormBuilder($news); $formBuilder->build(); $form = $formBuilder->form(); $formHandler = new FormHandler($form, $this->managers->getManagerOf('News'), $request); if ($formHandler->process()) { $this->app->user()->setFlash($news->isNew() ? 'La news a bien été ajoutée !' : 'La news a bien été modifiée !'); $this->app->httpResponse()->redirect('/admin/'); } $this->page->addVar('form', $form->createView()); }
/** * Méthode permettant d'enregistrer une news * Cette méthode s'implémente directement dans NewsManager * car elle ne dépend pas du DAO * * save() ajoute la news si nouvelle ou la met à jour si déjà enregistrée * * @param News $news * @see self::add() * @see self::modify() * @return void * @throws \RuntimeException */ public function save(News $news) { if ($news->isValid()) { $news->isNew() ? $this->add($news) : $this->modify($news); } else { throw new \RuntimeException('La news doit être validée pour être enregistrée'); } }
protected function modify(News $news) { $requete = $this->dao->prepare('UPDATE news SET auteur = :auteur, titre = :titre, contenu = :contenu, dateModif = NOW() WHERE id = :id'); $requete->bindValue(':titre', $news->titre()); $requete->bindValue(':auteur', $news->auteur()); $requete->bindValue(':contenu', $news->contenu()); $requete->bindValue(':id', $news->id(), \PDO::PARAM_INT); $requete->execute(); }
/** * build form * @param FormBuilderInterface $builder bulder object * @param array $options available options to inject * * @see \Symfony\Component\Form\AbstractType::buildForm() * * @return void */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('translations', 'a2lix_translations_gedmo', array('locales' => $this->locales, 'fields' => array('title' => array('required' => false, 'label' => 'Title', 'field_type' => TextType::class, 'constraints' => array(new VALIDATOR\NotBlank(), new VALIDATOR\Length(array('max' => 150))), 'attr' => array('oninvalid' => "setCustomValidity('" . 'Please fill in this value.' . "')", 'onfocus' => 'setCustomValidity(\'\')')), 'shortDesc' => array('required' => false, 'field_type' => TextareaType::class, 'label' => 'Short description', 'constraints' => array(new VALIDATOR\NotBlank()), 'attr' => array('oninvalid' => "setCustomValidity(\n '" . $this->container->get('translator')->trans('Please fill in this value', array(), 'messages') . "')", 'onfocus' => "setCustomValidity('')", 'class' => 'tinymce', 'data-theme' => 'advanced')), 'description' => array('field_type' => TextareaType::class, 'constraints' => array(new VALIDATOR\NotBlank()), 'attr' => array('oninvalid' => "setCustomValidity(\n '" . $this->container->get('translator')->trans('Please fill in this value', array(), 'messages') . "')", 'onfocus' => "setCustomValidity('')", 'class' => 'tinymce', 'data-theme' => 'advanced'))))); $object = isset($options['data']) ? $options['data'] : null; $builder->add('publishedAt', 'date', array('required' => false, 'attr' => array('id' => 'published_at'), 'format' => 'yyyy-MM-dd', 'data' => isset($object) ? $object->getPublishedAt() ? $object->getPublishedAt() : new \DateTime() : new \DateTime(), 'widget' => 'single_text', 'read_only' => true)); $builder->add('status', ChoiceType::class, array('choices' => News::getStatusOptions(), 'expanded' => true, 'multiple' => false, 'data' => isset($object) ? $object->getStatus() ? $object->getStatus() : 'A' : 'A', 'attr' => array('class' => 'minimal display-inline'))); //$builder->add('logo', 'file', array('required' => false, 'label' => 'Image')); $builder->add('logo', 'file', array('required' => false, 'label' => 'Image', 'constraints' => array(new VALIDATOR\File(array('mimeTypes' => array('image/jpg', 'image/jpeg', 'image/png', 'image/bmp', 'image/gif'), 'mimeTypesMessage' => "Please Upload Valid File"))))); $builder->add('updatedAt', 'hidden', array('data' => '')); }
public function saveTags(News $news) { $this->deleteTags($news->id()); if (str_replace(' ', '', $news->tags()) == '') { return; } $query = 'INSERT INTO T_NEW_tagc (NTC_name) SELECT name FROM ('; foreach ($news->tagsArray() as $key => $tag) { $query .= ($key === 0 ? '' : ' UNION ') . 'SELECT ' . $this->dao->quote($tag) . ' AS name'; } $query .= ') AS A LEFT OUTER JOIN T_NEW_tagc AS B ON A.name = B.NTC_name WHERE B.NTC_id IS NULL'; //echo $query; // var_dump($query); $this->dao->exec($query); $requete = 'INSERT INTO t_new_tagd (NTD_fk_NNC, NTD_fk_NTC) SELECT ' . (int) $news->id() . ', NTC_id FROM T_NEW_tagc WHERE NTC_name IN('; foreach ($news->tagsArray() as $key => $tag) { $requete .= ($key === 0 ? '' : ',') . $this->dao->quote($tag); } $this->dao->exec($requete . ');'); }
public function processForm(HTTPRequest $request) { if (!$this->app()->user()->isAuthenticated()) { $this->app->user()->setFlash('Connectez vous ou inscrivez vous pour accéder à cette section'); $this->app->httpResponse()->redirect('/'); } if ($request->method() == 'POST') { $news = new News(['auteur' => $this->app()->user()->member()->id(), 'titre' => $request->postData('titre'), 'contenu' => $request->postData('contenu'), 'tags' => $request->postData('tags')]); if ($request->getExists('id')) { $news->setId($request->getData('id')); } } else { // L'identifiant de la news est transmis si on veut la modifier if ($request->getExists('id')) { $news = $this->managers->getManagerOf('News')->getUnique($request->getData('id'), true); if ($news == NULL) { $this->generateOtherPage('error', 'La news n°' . $request->getData('id') . ' n\'existe pas'); return; } } else { $news = new News(); } } $formBuilder = new NewsFormBuilder($news); $formBuilder->build(); $form = $formBuilder->form(); $formHandler = new FormHandler($form, $this->managers->getManagerOf('News'), $request); if ($formHandler->process()) { $this->app->user()->setFlash($news->isNew() ? 'La news a bien été ajoutée !' : 'La news a bien été modifiée !'); $this->app->httpResponse()->redirect('/'); } $this->managers->getManagerOf('News')->saveTags($news); $this->page->addVar('form', $form->createView()); $this->page()->addVar('user', $this->app()->user()); }
/** * build form * @param FormBuilderInterface $builder bulder object * @param array $options available options to inject * * @see \Symfony\Component\Form\AbstractType::buildForm() * * @return void */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('search', TextType::class, array('mapped' => false, 'required' => false)); $builder->remove('translations'); $builder->add('status', ChoiceType::class, array('choices' => News::getStatusOptions(), 'expanded' => false, 'multiple' => false, 'attr' => array('class' => 'minimal'))); }
public function processForm(HTTPRequest $request) { $this->run(); //manager news $managersNews = $this->managers->getManagerOf('News'); // manager tag $managersTag = $this->managers->getManagerOf('Tag'); if ($request->method() == 'POST') { // Éléménet News $news = new News(['auteur' => $this->app->user()->getAttribute('user'), 'titre' => $request->postData('titre'), 'contenu' => $request->postData('contenu')]); // Élément Tag if ($request->postData('name') != '') { $tag = new Tag(['name' => $request->postData('name')]); $table = NewsController::separateTag($tag->name()); //vraiment ici ? //on sauvegarde tout les tags rentré si ils n'existes pas ! } else { $tag = new Tag(); } if ($request->getExists('id')) { $news->setId($request->getData('id')); $tag = new Tag(); } } else { // L'identifiant de la news est transmis si on veut la modifier if ($request->getExists('id')) { $news = $managersNews->getUnique($request->getData('id')); $text = ''; foreach ($managersTag->getUnique($request->getData('id')) as $value) { $text = $text . ' ' . $value['NTC_name']; } $tag = new Tag(['name' => $text]); if ($news == null) { $this->app->httpResponse()->redirect404(); } } else { $news = new News(); $tag = new Tag(); } } //construction formulaire news $formBuilderNews = new NewsMemberFormBuilder($news); $formBuilderNews->build(); $formNews = $formBuilderNews->form(); $formHandlerNews = new FormHandler($formNews, $managersNews, $request); //Construction formulaire Tag $formBuilderTag = new TagFormBuilder($tag); // même formbuilder ? $formBuilderTag->build($managersTag); $formTag = $formBuilderTag->form(); // manager tagd $managersTagd = $this->managers->getManagerOf('Tagd'); if ($formHandlerNews->process()) { //Refaire proprement le handler pour les tags $this->app->user()->setFlash($news->isNew() ? 'La news a bien été ajoutée !' : 'La news a bien été modifiée !'); // récupérer le dernier id de news if ($news->isNew()) { $news->setId($managersNews->getlastid()); } //var_dump($news->id()); NewsController::saveTag($table, $managersTag); foreach ($table as $key => $value) { $idtag = $managersTag->getId($value); $tagd = new Tagd(); $tagd->setIdnew($news->id()); $tagd->setIdtag($idtag); $managersTagd->add($tagd); } $this->app->httpResponse()->redirect($this->app->router()->getBuiltRoute('News', 'index', [])); } $this->page->addVar('formNews', $formNews->createView()); $this->page->addVar('formTag', $formTag->createView()); }
public function processForm(HTTPRequest $request) { if ($request->method() == 'POST') { $news = new News(['auteur' => $request->postData('auteur'), 'titre' => $request->postData('titre'), 'contenu' => $request->postData('contenu')]); if ($request->getExists('id')) { $news->setId($request->getData('id')); } } else { // L'identifiant de la news est transmis si on veut la modifier if ($request->getExists('id')) { $news = $this->managers->getManagerOf('News')->getUnique($request->getData('id')); } else { $news = new News(); } } $formBuilder = new NewsFormBuilder($news); $formBuilder->build(); $form = $formBuilder->form(); $formHandler = new FormHandler($form, $this->managers->getManagerOf('News'), $request); if ($formHandler->process()) { if ($news->isNew()) { $news->setId($request->getData('id')); // Suppression des caches si la news est nouvelle $cache = new Cache($news); $cache->unlinkDataAndShowCaches(); $cache->unlinkIndexCache(); } else { // Suppression des caches si la news est modifiée $cache = new Cache(null, $news); $cache->unlinkDataAndShowCaches(); } $this->app->user()->setFlash($news->isNew() ? 'La news a bien été ajoutée !' : 'La news a bien été modifiée !'); $this->app->httpResponse()->redirect('/admin/'); } $this->page->addVar('form', $form->createView()); }
protected function modify(News $news) { $requete = $this->dao->prepare('UPDATE t_for_newsc SET FNC_title = :titre, FNC_content = :contenu, FNC_dateupdate = NOW() WHERE FNC_id = :id'); $requete->bindValue(':titre', $news->titre(), \PDO::PARAM_STR); $requete->bindValue(':contenu', $news->contenu(), \PDO::PARAM_STR); $requete->bindValue(':id', $news->id(), \PDO::PARAM_INT); $requete->execute(); }