Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///////////////////////// VALIDATOR ////////////////////////////
- <?php
- namespace Application\ChallengerzBundle\Validator\Constraints;
- use Symfony\Component\Validator\Constraint;
- use Symfony\Component\Validator\ConstraintValidator;
- use Symfony\Component\Validator\Exception\UnexpectedTypeException;
- use Symfony\Component\HttpFoundation\Request;
- //use Comways\Component\Form\SubmitChallengeType;
- /**
- * Validates a SubmitChallengeType
- *
- */
- class SubmitUrlConstraintValidator extends ConstraintValidator
- {
- /**
- * @param SubmitChallengeType $value
- * @param Constraint $constraint
- * @throws UnexpectedTypeException
- * @return boolean
- */
- public function isValid($value, Constraint $constraint)
- {
- if ((!empty($value))) {
- $file = $value;
- $extension_upload = strtolower(substr(strrchr($file->getClientOriginalName(),'.'),1));
- if (!in_array($extension_upload,$constraint->extensions_valides)) {
- $this->setMessage($constraint->message2);
- return false;
- }
- }else {
- $this->setMessage($constraint->message);
- return false;
- }
- return true;
- }
- }
- ////////////// FUNCTION CALLED ON THE SUBMIT ////////////////////////
- public function postAction() {
- $challenge = new WbChallenge();
- $picture = new WbPicture();
- $formChallenge = $this->createForm(new SubmitChallengeType());
- $request = $this->get('request');
- return $this->render('ApplicationChallengerzBundle:WbChallenge:challenge_je-publie.html.twig', array('formChallenge' => $formChallenge->createView(),'errors' => $formChallenge->getErrors()));
- }
- public function createAction() {
- $challenge_exist = False;
- $challenge = new WbChallenge();
- $category = new WbCategory();
- $formChallenge = $this->createForm(new SubmitChallengeType());
- $picture = new WbPicture();
- $request = $this->container->get('request');
- if ($request->getMethod() == 'POST') {
- $formChallenge->bindRequest($request);
- if ($formChallenge->isValid()) {
- $em = $this->get('doctrine')->getEntityManager();
- $formObject = $formChallenge->getData();
- $challenge_temp = $formObject->getTitle();
- $challenge_search = $em->getRepository('ApplicationChallengerzBundle:WbChallenge')
- ->findOneByTitle($challenge_temp);
- $category_temp = $formObject->getCategory();
- $category = $em->getRepository('ApplicationChallengerzBundle:WbCategory')
- ->findOneByName($category_temp);
- if (!is_null($challenge_search)) {
- $challenge = $challenge_search;
- $challenge_exist = True;
- } else {
- $challenge->setTitle($formObject->getTitle());
- $challenge->setPseudo($formObject->getPseudo());
- $challenge->setIpaddress($request->getClientIp());
- $challenge->setCreatedAt(new \DateTime('now'));
- $challenge->setCategory($category);
- }
- $file = $formObject->getUrl();
- if (!empty($file)){
- $extension_upload = strtolower(substr(strrchr($file->getClientOriginalName(),'.'),1));
- $today = new \DateTime('now');
- $formatedDate = $today->format('Y-m-d H:i:s');
- $fileName = hash('md5',$formatedDate.$formObject->getImagename());
- $file->move($this->directory_picture.$category->getId().'/', $fileName.'.'.$extension_upload);
- $picture->setTitle($formObject->getImagename());
- $picture->setPlace($formObject->getPlace());
- $picture->setUrl($this->directory_picture.$category->getId().'/'.$fileName.'.'.$extension_upload);
- $picture->setCreatedAt(new \DateTime('now'));
- $picture->setIpaddress($request->getClientIp());
- $picture->setPseudo($formObject->getPseudo());
- $picture->setChallenge($challenge);
- if ($challenge_exist == True) {
- $em->persist($challenge);
- $em->flush();
- }
- $em->persist($challenge);
- $em->flush();
- $em->persist($picture);
- $em->flush();
- //return $this->render('ApplicationChallengerzBundle:Challengerz:index.html.twig');
- return $this->redirect($this->generateUrl('submitChallenge_success'));
- }
- } else {
- return $this->render('ApplicationChallengerzBundle:WbChallenge:challenge_je-publie.html.twig', array('formChallenge' => $formChallenge->createView()));
- }
- }else {
- return $this->render('ApplicationChallengerzBundle:Challengerz:index.html.twig');
- }
- }
- //////////////////////////////// TWIG /////////////////////////////////////
- {% extends "ApplicationChallengerzBundle::layout.html.twig" %}
- {% block title %}
- {{ 'challenge.titre.jepublie' | trans }}
- {% endblock %}
- {% block head %}
- <style type="text/css">@import url("{{ asset('/bundles/applicationchallengerz/css/style_challenge.css') }}");</style>
- <style type="text/css">@import url("{{ asset('/bundles/applicationchallengerz/css/autocomplete.css') }}");</style>
- <script type="text/javascript" src="{{ asset('/bundles/applicationchallengerz/js/challenge.js') }}"></script>
- {% endblock %}
- {% block content %}
- <div class="div_creationChallenge">
- <div class="sectionTitle" id="sectionTitleChallenge">
- <h1>{{ 'challenge.suggestiontonchallenge'|trans }}</h1>
- </div>
- <div class="challengeCreation">
- <form action="{{ path('challenge_create') }}" method="post" {{ form_enctype(formChallenge) }}>
- {{ form_errors(formChallenge) }}
- <div id="challenge">
- <table>
- <tr>
- <th>
- {{ form_label(formChallenge.title) }}
- </th>
- <td>
- {{ form_errors(formChallenge.title) }}
- {{ form_widget(formChallenge.title) }}
- <div id="search_suggest"></div>
- </td>
- </tr>
- <tr>
- <th>
- {{ form_label(formChallenge.category) }}
- </th>
- <td>
- {{ form_errors(formChallenge.category) }}
- {{ form_widget(formChallenge.category) }}
- </td>
- </tr>
- <tr>
- <th>
- {{ form_label(formChallenge.pseudo) }}
- </th>
- <td>
- {{ form_errors(formChallenge.pseudo) }}
- {{ form_widget(formChallenge.pseudo) }}
- </td>
- </tr>
- </table>
- </div>
- <div class="sectionTitle">
- <h1>{{ 'challenge.suggestionphoto'|trans }}</h1>
- </div>
- <div id="picture">
- <table>
- <tr>
- <th>
- {{ form_label(formChallenge.imagename) }}
- </th>
- <td>
- {{ form_errors(formChallenge.imagename) }}
- {{ form_widget(formChallenge.imagename) }}
- </td>
- </tr>
- <tr>
- <th>
- {{ form_label(formChallenge.place) }}
- </th>
- <td>
- {{ form_errors(formChallenge.place) }}
- {{ form_widget(formChallenge.place) }}
- </td>
- </tr>
- <tr>
- <th>
- {{ form_label(formChallenge.url) }}
- </th>
- <td>
- {{ form_errors(formChallenge.url) }}
- {{ form_widget(formChallenge.url) }}
- </td>
- </tr>
- </table>
- </div>
- {{ form_rest(formChallenge) }}
- </br>
- <input submitButtton type="submit" class="submitChallenge" value="{{ 'challenge.form.valider' | trans }}"/>
- </form>
- </div>
- </div>
- {% endblock %}
- {% block script %}
- {% endblock %}
Add Comment
Please, Sign In to add comment