index.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. require_once '../inc/global.inc.php';
  3. use Silex\Application;
  4. use Symfony\Component\Validator\Constraints as Assert;
  5. use Doctrine\ORM\Tools\Pagination\Paginator;
  6. use Pagerfanta\Pagerfanta;
  7. use Pagerfanta\Adapter\DoctrineORMAdapter;
  8. use Pagerfanta\View\TwitterBootstrapView;
  9. //use Pagerfanta\View\DefaultView;
  10. //use Pages\PagesAdmin;
  11. class PagesController {
  12. /*
  13. function indexAction(Application $app, $page) {
  14. return $this->listAction($app, $page);
  15. }*/
  16. function addAction(Application $app) {
  17. $request = $app['request'];
  18. $form = $this->getForm($app);
  19. if ('POST' == $request->getMethod()) {
  20. $form->bindRequest($request);
  21. if ($form->isValid()) {
  22. $page = $form->getData();
  23. $page->setSlug($page->getTitle());
  24. $em = $app['orm.em'];
  25. /*$page_data = $form->getData();
  26. $page->setContent($page_data['content']);
  27. $page->setSlug($page_data['slug']);
  28. $page->setTitle($page_data['title']);
  29. $em->persist($page);*/
  30. $em->persist($page);
  31. $em->flush();
  32. return $app->redirect($app['url_generator']->generate('show', array('id'=> $page->getId())), 201);
  33. }
  34. }
  35. return $app['template']->render_template('pages/add.tpl', array('form' => $form->createView()));
  36. }
  37. function editAction(Application $app, $id) {
  38. $request = $app['request'];
  39. $page = $app['orm.em']->find('Entity\Pages', $id);
  40. if (empty($page)) {
  41. $app->abort(404, "Page $id does not exist.");
  42. }
  43. $form = $this->getForm($app, $page);
  44. if ('POST' == $request->getMethod()) {
  45. $form->bind($request);
  46. if ($form->isValid()) {
  47. $em = $app['orm.em'];
  48. //$page = $form->getData();
  49. $page->setTitle($page->getTitle());
  50. $em->persist($page);
  51. $em->flush();
  52. return $app->redirect($app['url_generator']->generate('show', array('id'=> $page->getId())), 201);
  53. }
  54. }
  55. return $app['template']->render_template('pages/add.tpl', array('form' => $form->createView()));
  56. }
  57. function showAction(Application $app, $id) {
  58. $page = $app['orm.em']->find('Entity\Pages', $id);
  59. $actions = Display::url(Display::return_icon('list.png', get_lang('Listing'), array(), ICON_SIZE_MEDIUM), $app['url_generator']->generate('index'));
  60. return $app['template']->render_template('pages/show.tpl', array(
  61. 'page' => $page,
  62. 'actions' => $actions,
  63. ));
  64. }
  65. function deleteAction(Application $app, $id) {
  66. $em = $app['orm.em'];
  67. $page = $em->find('Entity\Pages', $id);
  68. $em->remove($page);
  69. $em->flush();
  70. return $app->redirect($app['url_generator']->generate('index'), 201);
  71. }
  72. function listAction(Application $app, $page = 1) {
  73. /*
  74. $source = new Entity('Entity\Pages');
  75. $grid = new Grid();
  76. // Attach the source to the grid
  77. $grid->setSource($source);
  78. // Return the response of the grid to the template
  79. //return $grid->getGridResponse('MyProjectMyBundle::myGrid.html.twig');
  80. */
  81. $em = $app['orm.em'];
  82. $dql = 'SELECT a FROM Entity\Pages a';
  83. $query = $em->createQuery($dql)->setFirstResult(0)->setMaxResults(100);
  84. //or using the repository
  85. //
  86. //$query = $em->getRepository('Entity\Pages')->getLatestPages();
  87. $adapter = new DoctrineORMAdapter($query);
  88. $pagerfanta = new Pagerfanta($adapter);
  89. $routeGenerator = function($page) use ($app) {
  90. return $app['url_generator']->generate('list', array('page' => $page));
  91. };
  92. $page = intval($app['request']->get('page'));
  93. $pagerfanta->setMaxPerPage(2); // 10 by default
  94. $pagerfanta->setCurrentPage($page);
  95. //$view = new DefaultView();
  96. $view = new TwitterBootstrapView();
  97. $pagination = $view->render($pagerfanta, $routeGenerator, array(
  98. 'proximity' => 3,
  99. ));
  100. $actions = Display::url(Display::return_icon('add.png', get_lang('Add'), array(), ICON_SIZE_MEDIUM), $app['url_generator']->generate('add'));
  101. //$paginator = new Paginator($query, $fetchJoinCollection = true);
  102. return $app['template']->render_template('pages/listing.tpl', array(
  103. //'pages' => $paginator->getIterator(),
  104. 'pages' => $pagerfanta,
  105. 'pagination' => $pagination,
  106. 'actions' => $actions
  107. ));
  108. }
  109. function getForm(Application $app, $entity = null) {
  110. if (empty($entity)) {
  111. $entity = new Entity\Pages();
  112. }
  113. $form = $app['form.factory']->createBuilder('form', $entity);
  114. $form->add('title', 'text', array(
  115. 'constraints' => array(new Assert\NotBlank(), new Assert\MinLength(5))
  116. ));
  117. $form->add('slug', 'text', array(
  118. //'constraints' => array(new Assert\NotBlank())
  119. ));
  120. $form->add('content', 'textarea', array(
  121. // 'constraints' => array()
  122. ));
  123. return $form->getForm();
  124. }
  125. }
  126. $app->get('/', 'pages.controller:listAction')->bind('index');
  127. $app->get('/page', 'pages.controller:listAction')->bind('list');
  128. $app->get('/show/{id}', 'pages.controller:showAction')
  129. ->bind('show')
  130. ->assert('id', '\d+');
  131. $app->get('/delete/{id}', 'pages.controller:deleteAction')
  132. ->bind('delete')
  133. ->assert('id', '\d+');
  134. $app->match('/edit/{id}', 'pages.controller:editAction', 'GET|POST')
  135. ->bind('edit')
  136. ->assert('id', '\d+');
  137. $app->match('/add', 'pages.controller:addAction', 'GET|POST')->bind('add');
  138. $app->run();