Guest User

Untitled

a guest
Oct 22nd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.87 KB | None | 0 0
  1. ///////////////////////// VALIDATOR ////////////////////////////
  2.  
  3. <?php
  4.  
  5. namespace Application\ChallengerzBundle\Validator\Constraints;
  6.  
  7. use Symfony\Component\Validator\Constraint;
  8. use Symfony\Component\Validator\ConstraintValidator;
  9. use Symfony\Component\Validator\Exception\UnexpectedTypeException;
  10. use Symfony\Component\HttpFoundation\Request;
  11. //use Comways\Component\Form\SubmitChallengeType;
  12.  
  13. /**
  14. * Validates a SubmitChallengeType
  15. *
  16. */
  17. class SubmitUrlConstraintValidator extends ConstraintValidator
  18. {
  19. /**
  20. * @param SubmitChallengeType $value
  21. * @param Constraint $constraint
  22. * @throws UnexpectedTypeException
  23. * @return boolean
  24. */
  25. public function isValid($value, Constraint $constraint)
  26. {
  27. if ((!empty($value))) {
  28. $file = $value;
  29. $extension_upload = strtolower(substr(strrchr($file->getClientOriginalName(),'.'),1));
  30. if (!in_array($extension_upload,$constraint->extensions_valides)) {
  31. $this->setMessage($constraint->message2);
  32. return false;
  33. }
  34. }else {
  35. $this->setMessage($constraint->message);
  36. return false;
  37. }
  38. return true;
  39. }
  40. }
  41.  
  42.  
  43. ////////////// FUNCTION CALLED ON THE SUBMIT ////////////////////////
  44.  
  45. public function postAction() {
  46.  
  47. $challenge = new WbChallenge();
  48. $picture = new WbPicture();
  49. $formChallenge = $this->createForm(new SubmitChallengeType());
  50. $request = $this->get('request');
  51. return $this->render('ApplicationChallengerzBundle:WbChallenge:challenge_je-publie.html.twig', array('formChallenge' => $formChallenge->createView(),'errors' => $formChallenge->getErrors()));
  52.  
  53. }
  54.  
  55. public function createAction() {
  56. $challenge_exist = False;
  57. $challenge = new WbChallenge();
  58. $category = new WbCategory();
  59. $formChallenge = $this->createForm(new SubmitChallengeType());
  60. $picture = new WbPicture();
  61. $request = $this->container->get('request');
  62. if ($request->getMethod() == 'POST') {
  63. $formChallenge->bindRequest($request);
  64. if ($formChallenge->isValid()) {
  65. $em = $this->get('doctrine')->getEntityManager();
  66. $formObject = $formChallenge->getData();
  67. $challenge_temp = $formObject->getTitle();
  68. $challenge_search = $em->getRepository('ApplicationChallengerzBundle:WbChallenge')
  69. ->findOneByTitle($challenge_temp);
  70. $category_temp = $formObject->getCategory();
  71. $category = $em->getRepository('ApplicationChallengerzBundle:WbCategory')
  72. ->findOneByName($category_temp);
  73. if (!is_null($challenge_search)) {
  74. $challenge = $challenge_search;
  75. $challenge_exist = True;
  76. } else {
  77. $challenge->setTitle($formObject->getTitle());
  78. $challenge->setPseudo($formObject->getPseudo());
  79. $challenge->setIpaddress($request->getClientIp());
  80. $challenge->setCreatedAt(new \DateTime('now'));
  81. $challenge->setCategory($category);
  82. }
  83. $file = $formObject->getUrl();
  84. if (!empty($file)){
  85. $extension_upload = strtolower(substr(strrchr($file->getClientOriginalName(),'.'),1));
  86.  
  87. $today = new \DateTime('now');
  88. $formatedDate = $today->format('Y-m-d H:i:s');
  89. $fileName = hash('md5',$formatedDate.$formObject->getImagename());
  90. $file->move($this->directory_picture.$category->getId().'/', $fileName.'.'.$extension_upload);
  91.  
  92. $picture->setTitle($formObject->getImagename());
  93. $picture->setPlace($formObject->getPlace());
  94. $picture->setUrl($this->directory_picture.$category->getId().'/'.$fileName.'.'.$extension_upload);
  95. $picture->setCreatedAt(new \DateTime('now'));
  96. $picture->setIpaddress($request->getClientIp());
  97. $picture->setPseudo($formObject->getPseudo());
  98. $picture->setChallenge($challenge);
  99.  
  100. if ($challenge_exist == True) {
  101. $em->persist($challenge);
  102. $em->flush();
  103. }
  104. $em->persist($challenge);
  105. $em->flush();
  106. $em->persist($picture);
  107. $em->flush();
  108.  
  109. //return $this->render('ApplicationChallengerzBundle:Challengerz:index.html.twig');
  110. return $this->redirect($this->generateUrl('submitChallenge_success'));
  111. }
  112. } else {
  113. return $this->render('ApplicationChallengerzBundle:WbChallenge:challenge_je-publie.html.twig', array('formChallenge' => $formChallenge->createView()));
  114. }
  115.  
  116. }else {
  117. return $this->render('ApplicationChallengerzBundle:Challengerz:index.html.twig');
  118. }
  119. }
  120.  
  121.  
  122. //////////////////////////////// TWIG /////////////////////////////////////
  123.  
  124.  
  125. {% extends "ApplicationChallengerzBundle::layout.html.twig" %}
  126. {% block title %}
  127. {{ 'challenge.titre.jepublie' | trans }}
  128. {% endblock %}
  129.  
  130. {% block head %}
  131. <style type="text/css">@import url("{{ asset('/bundles/applicationchallengerz/css/style_challenge.css') }}");</style>
  132. <style type="text/css">@import url("{{ asset('/bundles/applicationchallengerz/css/autocomplete.css') }}");</style>
  133. <script type="text/javascript" src="{{ asset('/bundles/applicationchallengerz/js/challenge.js') }}"></script>
  134. {% endblock %}
  135.  
  136.  
  137.  
  138. {% block content %}
  139. <div class="div_creationChallenge">
  140. <div class="sectionTitle" id="sectionTitleChallenge">
  141. <h1>{{ 'challenge.suggestiontonchallenge'|trans }}</h1>
  142. </div>
  143.  
  144. <div class="challengeCreation">
  145.  
  146. <form action="{{ path('challenge_create') }}" method="post" {{ form_enctype(formChallenge) }}>
  147. {{ form_errors(formChallenge) }}
  148. <div id="challenge">
  149. <table>
  150. <tr>
  151. <th>
  152. {{ form_label(formChallenge.title) }}
  153. </th>
  154. <td>
  155. {{ form_errors(formChallenge.title) }}
  156. {{ form_widget(formChallenge.title) }}
  157. <div id="search_suggest"></div>
  158. </td>
  159. </tr>
  160. <tr>
  161. <th>
  162. {{ form_label(formChallenge.category) }}
  163. </th>
  164. <td>
  165. {{ form_errors(formChallenge.category) }}
  166. {{ form_widget(formChallenge.category) }}
  167. </td>
  168. </tr>
  169. <tr>
  170. <th>
  171. {{ form_label(formChallenge.pseudo) }}
  172. </th>
  173. <td>
  174. {{ form_errors(formChallenge.pseudo) }}
  175. {{ form_widget(formChallenge.pseudo) }}
  176. </td>
  177. </tr>
  178. </table>
  179. </div>
  180. <div class="sectionTitle">
  181. <h1>{{ 'challenge.suggestionphoto'|trans }}</h1>
  182. </div>
  183. <div id="picture">
  184. <table>
  185. <tr>
  186. <th>
  187. {{ form_label(formChallenge.imagename) }}
  188. </th>
  189. <td>
  190. {{ form_errors(formChallenge.imagename) }}
  191. {{ form_widget(formChallenge.imagename) }}
  192. </td>
  193. </tr>
  194. <tr>
  195. <th>
  196. {{ form_label(formChallenge.place) }}
  197. </th>
  198. <td>
  199. {{ form_errors(formChallenge.place) }}
  200. {{ form_widget(formChallenge.place) }}
  201. </td>
  202. </tr>
  203. <tr>
  204. <th>
  205. {{ form_label(formChallenge.url) }}
  206. </th>
  207. <td>
  208. {{ form_errors(formChallenge.url) }}
  209. {{ form_widget(formChallenge.url) }}
  210. </td>
  211. </tr>
  212. </table>
  213. </div>
  214. {{ form_rest(formChallenge) }}
  215. </br>
  216. <input submitButtton type="submit" class="submitChallenge" value="{{ 'challenge.form.valider' | trans }}"/>
  217. </form>
  218.  
  219. </div>
  220. </div>
  221.  
  222. {% endblock %}
  223.  
  224.  
  225. {% block script %}
  226.  
  227. {% endblock %}
Add Comment
Please, Sign In to add comment