src/AppBundle/Controller/BlogController.php line 126

Open in your IDE?
  1. <?php
  2. namespace AppBundle\Controller;
  3. use Pimcore\Controller\FrontendController;
  4. use Pimcore\Model\DataObject;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Zend\Paginator\Paginator;
  7. class BlogController extends FrontendController
  8. {
  9.     public function indexAction(Request $request)
  10.     {
  11.         // get a list of news objects and order them by date
  12.         $blogList = new DataObject\BlogArticle\Listing();
  13.         $blogList->setOrderKey('date');
  14.         $blogList->setOrder('DESC');
  15.         // selected category
  16.         $selectedCategory null;
  17.         if ($selectedCategoryId $request->get('category')) {
  18.             $selectedCategory DataObject\BlogCategory::getById((int)$selectedCategoryId);
  19.             $this->view->selectedCategory $selectedCategory;
  20.         }
  21.         // selected archive
  22.         $selectedArchive null;
  23.         if ($selectedArchive $request->get('archive')) {
  24.             $this->view->selectedArchive $selectedArchive;
  25.         }
  26.         $conditions = [];
  27.         if ($selectedCategory) {
  28.             $conditions[] = 'categories LIKE ' $blogList->quote('%,' $selectedCategory->getId() . ',%');
  29.         }
  30.         if ($request->get('archive')) {
  31.             $conditions[] = "DATE_FORMAT(FROM_UNIXTIME(date), '%Y-%c') = " $blogList->quote($selectedArchive);
  32.         }
  33.         if (!empty($conditions)) {
  34.             $blogList->setCondition(implode(' AND '$conditions));
  35.         }
  36.         // we're using Zend\Paginator here, but you can use any other paginator (e.g. Pagerfanta)
  37.         $paginator = new Paginator($blogList);
  38.         $paginator->setCurrentPageNumber($request->get('page'));
  39.         $paginator->setItemCountPerPage(2);
  40.         $this->view->articles $paginator;
  41.         // get all categories
  42.         $categories DataObject\BlogCategory::getList(); // this is an alternative way to get an object list
  43.         $this->view->categories $categories;
  44.         // archive information, we have to do this in pure SQL
  45.         $db = \Pimcore\Db::get();
  46.         $ranges $db->fetchCol("SELECT DATE_FORMAT(FROM_UNIXTIME(date), '%Y-%c') as ranges FROM object_" $blogList->getClassId() . " GROUP BY DATE_FORMAT(FROM_UNIXTIME(date), '%b-%Y') ORDER BY ranges ASC");
  47.         $this->view->archiveRanges $ranges;
  48.         if ($request->get('pdf')) {
  49.             //Simple usage
  50.             $html $this->renderView(':Blog:index.html.php'$this->view->getAllParameters());
  51.             return new \Symfony\Component\HttpFoundation\Response(
  52.                 \Pimcore\Web2Print\Processor::getInstance()->getPdfFromString($html),
  53.                 200,
  54.                 [
  55.                     'Content-Type' => 'application/pdf',
  56.                 ]
  57.             );
  58.             //Advanced usage
  59.             /*$params = $this->view->getAllParameters();
  60.             $params['testPlaceholder'] = ' :-)';
  61.             $html = $this->renderView(':Blog:index.html.php', $params);
  62.             $adapter = \Pimcore\Web2Print\Processor::getInstance();
  63.             //add custom settings if necessary
  64.             if ($adapter instanceof \Pimcore\Web2Print\Processor\WkHtmlToPdf) {
  65.                 $params['adapterConfig'] = '-O landscape';
  66.             } elseif($adapter instanceof \Pimcore\Web2Print\Processor\PdfReactor8) {
  67.                 //Config settings -> http://www.pdfreactor.com/product/doc/webservice/php.html#Configuration
  68.                 $params['adapterConfig'] = [
  69.                     'author' => 'Max Mustermann',
  70.                     'title' => 'Custom Title',
  71.                     'javaScriptMode' => 0,
  72.                     'addLinks' => true,
  73.                     'appendLog' => true,
  74.                     'enableDebugMode' => true
  75.                 ];
  76.             }
  77.             return new \Symfony\Component\HttpFoundation\Response(
  78.                 $adapter->getPdfFromString($html, $params),
  79.                 200,
  80.                 array(
  81.                     'Content-Type' => 'application/pdf',
  82.                     // 'Content-Disposition'   => 'attachment; filename="custom-pdf.pdf"' //direct download
  83.                 )
  84.             );*/
  85.         }
  86.     }
  87.     public function detailAction(Request $request)
  88.     {
  89.         // "id" is the named parameters in "Static Routes"
  90.         $article DataObject\BlogArticle::getById($request->get('id'));
  91.         if (!$article instanceof DataObject\BlogArticle || !$article->isPublished()) {
  92.             throw $this->createNotFoundException('Invalid request - no such blog article');
  93.         }
  94.         $this->view->article $article;
  95.     }
  96.     public function sidebarBoxAction(Request $request)
  97.     {
  98.         $items = (int) $request->get('items');
  99.         if (!$items) {
  100.             $items 3;
  101.         }
  102.         // this is the alternative way of getting a list of objects
  103.         $blogList DataObject\BlogArticle::getList([
  104.             'limit' => $items,
  105.             'order' => 'DESC',
  106.             'orderKey' => 'date'
  107.         ]);
  108.         $this->view->articles $blogList;
  109.     }
  110. }