Example #1
0
 public function editAction(ServerRequestInterface $Request, ResponseInterface $Response, callable $Next = null)
 {
     $data = [];
     $id = $Request->getAttribute('id', false);
     if (!empty($id) && 'POST' === $Request->getMethod()) {
         $post = $Request->getParsedBody();
         // TODO [SECURITY]: validate content
         $content = $post['content'];
         unset($post['content']);
         $themepath = \Page\ModuleConfig::themepath();
         // $path = getcwd().'/templates'.$post['path'];
         $path = $themepath . $post['path'];
         if (substr($path, -1) !== '/') {
             $path .= '/';
         }
         if (!is_dir($path)) {
             // dir doesn't exist, make it
             mkdir($path, 0775, true);
         }
         $file = $path . $post['name'] . '.html.twig';
         if (file_exists($file)) {
             //backup file per day
             $bakfile = $file . '.' . date('Ymd') . '.bak';
             if (!copy($file, $bakfile)) {
                 throw new \Exception('failed to backup template: ' . $file);
             }
         }
         file_put_contents($file, $content);
         try {
             $plugin = \RpkPluginManager\PluginChain::getInstance();
             $params = $plugin->prepareArgs(['id' => $id, 'data' => $post]);
             $plugin->trigger('page/template::edit-update.pre', $params);
             $post = $params['data'];
             $this->storage->update('page_templates', $post, ['id' => $id]);
         } catch (\Exception $e) {
             // var_dump($e->getMessage());exit(__FILE__.'::'.__LINE__);
             if (!copy($bakfile, $file)) {
                 throw new \Exception('failed to update template and to restore content of the old one from: ' . $bakfile);
             } else {
                 throw new \Exception('failed to update template the old content was restored!');
             }
         }
         // delete cache of the pages that use this template
         // TODO [PERFORMANCE]: query only for slug column
         $pages = $this->storage->fetchAllPages(['templateId' => $id]);
         // $cachepath = getcwd().'/public/data/cache/html/';
         $cachepath = \Page\ModuleConfig::cachepath();
         foreach ($pages as $page) {
             // $file = $cachepath.$page['slug'].'.html';
             $file = $cachepath . $page['path'] . '/' . $page['slug'] . '.html';
             if (file_exists($file)) {
                 unlink(realpath($file));
             }
         }
         $url = $this->router->generateUri('admin.page-template', ['action' => 'edit', 'id' => $id]);
         return $Response->withStatus(302)->withHeader('Location', (string) $url);
     }
     $entity = $this->storage->fetch('page_templates', $id);
     $themepath = \Page\ModuleConfig::themepath();
     // $content = file_get_contents(getcwd().'/templates'.$entity['path'].$entity['name'].'.html.twig');
     $content = file_get_contents($themepath . $entity['path'] . $entity['name'] . '.html.twig');
     $data['page_template'] = $entity;
     $data['page_template']['content'] = $content;
     // return new HtmlResponse($this->template->render('page/template::edit', $data));
     $plugin = \RpkPluginManager\PluginChain::getInstance();
     $params = $plugin->prepareArgs(['template' => 'page/template::edit', 'data' => $data]);
     $plugin->trigger('page/template::edit-render.pre', $params);
     $htmlResponse = new HtmlResponse($this->template->render($params['template'], $params['data']));
     $response = $Next($Request, $htmlResponse);
     return $response;
 }
Example #2
0
 private function cache_RenderPre($Params)
 {
     // var_dump($Params['data']);exit(__FILE__.'::'.__LINE__);
     // $paginator = $this->storage->fetch('page_paginator', $Params['data']['id'], 'pageId');
     $query = "  select page_paginator.*, page_templates.path as `page_templates.path` , page_templates.name as `page_templates.name`\n                    from page_paginator \n                    inner join page_templates \n                        on page_paginator.listTemplateId = page_templates.id \n                    where page_paginator.pageId = :id";
     $select = $this->storage->query($query, [':id' => $Params['data']['id']]);
     if ($select) {
         $paginator = $select->fetch(\PDO::FETCH_ASSOC);
     }
     // var_dump($paginator);exit(__FILE__.'::'.__LINE__);
     if (!$paginator) {
         return;
     }
     // based on paginator settings we get the list of pages
     $where = 'WHERE pages.state = 1 AND pages.id != ' . $Params['data']['id'];
     if (!empty($paginator['sortBy'])) {
         $sort = $paginator['sortBy'] . ' ' . $paginator['sortOrder'];
     } else {
         $sort = 'pages.creationDate DESC';
     }
     $select = "CALL `fetchAllPages` (:where, :orderby, :offset, :limit); ";
     $selectparams = [':where' => $where, ':orderby' => $sort, ':offset' => 0, ':limit' => PHP_INT_MAX];
     $select = $this->storage->query($select, $selectparams);
     $pages = array();
     do {
         $pages = $select->fetchAll();
     } while ($select->nextRowset() && $select->columnCount());
     $count = count($pages);
     $nrpages = ceil($count / $paginator['itemsNumber']);
     // alter the data to be used in the main page template
     $Params['data']['paginator'] = [];
     $Params['data']['paginator']['pages'] = array_slice($pages, 0, $paginator['itemsNumber']);
     $Params['data']['paginator']['count'] = $nrpages;
     // var_dump($Params['data']);exit(__FILE__.'::'.__LINE__);
     // if there is only one page then no need for sublists
     if ($nrpages < 2) {
         return;
     }
     $Params['data']['paginator']['next'] = $Params['data']['slug'] . '/' . $paginator['name'] . '-2' . '.html';
     // get sublists template
     // $listTpl = 'templates'.$paginator['page_templates.path'].'::'.$paginator['page_templates.name'];
     $themepath = \Page\ModuleConfig::themepath(true);
     $listTpl = $themepath . $paginator['page_templates.path'] . '::' . $paginator['page_templates.name'];
     // determine sublist dir
     // $cachepath = $this->pageConf['cache']['path'];
     $cachepath = \Page\ModuleConfig::cachepath();
     $sublistdir = $cachepath . $Params['data']['path'] . '/' . $Params['data']['slug'];
     // var_dump($sublistdir);exit(__FILE__.'::'.__LINE__);
     // create page dir if not exists
     if (!is_dir($sublistdir)) {
         // dir doesn't exist, make it
         mkdir($sublistdir, 0777, true);
     }
     // generate sublists
     for ($i = 2; $i <= $nrpages; $i++) {
         // get items per page
         $items = array_slice($pages, $paginator['itemsNumber'] * ($i - 1), $paginator['itemsNumber']);
         // generate content
         $listdata = ['page' => $Params['data'], 'pages' => $items, 'paginator' => []];
         // paginator prev
         if ($i == 2) {
             $listdata['paginator']['prev'] = '../' . $Params['data']['slug'] . '.html';
         } else {
             $listdata['paginator']['prev'] = $paginator['name'] . '-' . ($i - 1) . '.html';
         }
         // paginator next
         if ($i < $nrpages) {
             $listdata['paginator']['next'] = $paginator['name'] . '-' . ($i + 1) . '.html';
         }
         $content = $this->template->render($listTpl, $listdata);
         // determine page name
         $pagename = '/' . $paginator['name'] . '-' . $i . '.html';
         $file = $sublistdir . $pagename;
         if (file_put_contents($file, $content)) {
             // do something nice
         }
     }
 }
Example #3
0
 public function __invoke(ServerRequestInterface $Request, ResponseInterface $Response, callable $Next = null)
 {
     try {
         $url = $Request->getUri();
         $path = $url->getPath();
         $query = $url->getQuery();
         // var_dump($url);exit(__FILE__.'::'.__LINE__);
         //detecting language
         // $lang = $Request->getAttribute('lang', 'en');
         //detecting requested page
         // TODO [PERFORMANCE]: check if better get from request first
         if (preg_match('#\\.(html|xml)$#', $path, $matches)) {
             $page = substr($path, 1, -strlen(reset($matches)));
         } else {
             // $page = $Request->getAttribute('page', 'index');
             if ('/' == $path) {
                 $page = 'index';
                 $path = '/index.html';
             } else {
                 $page = substr($path, 1);
                 $path .= '.html';
             }
         }
         $filename = $page;
         $pageschema = explode('/', $page);
         $page = array_pop($pageschema);
         unset($pageschema);
         // var_dump($page);exit(__FILE__.'::'.__LINE__);
         $data['page'] = $page;
         // $data['language'] = date('Y-m-d H:i:s');
         // $file = getcwd().'/data/cache/html/'.$lang.'_'.$page.'.html';
         // TODO [IMPROVEMENT]: get from config or pass responsability to a service
         // $cachepath = getcwd().'/public/data/cache/html';
         $cachepath = \Page\ModuleConfig::cachepath();
         $file = $cachepath . '/' . $filename . '.html';
         // var_dump($file);exit(__FILE__.'::'.__LINE__);
         if (!file_exists($file)) {
             // $pagedb = $this->storage->fetch('pages', $page, 'slug');
             $pagedb = $this->storage->pageBySlug($page);
             // var_dump($pagedb);exit(__FILE__.'::'.__LINE__);
             if (false == $pagedb) {
                 throw new StorageException('Page not found');
             }
             // load page extra info
             $data['page_meta'] = $this->storage->fetchAll('page_meta', ['pageId' => $pagedb['id']]);
             $data['page_meta_tags'] = $this->storage->fetchAll('page_meta_tags', ['pageId' => $pagedb['id']]);
             $pagedb = \Zend\Stdlib\ArrayUtils::merge($pagedb, $data);
             // var_dump($pagedb['content']);exit(__FILE__.'::'.__LINE__);
             // $content = file_get_contents(getcwd().'/data/cache/html/en_test.html');
             $plugin = \RpkPluginManager\PluginChain::getInstance();
             $themepath = \Page\ModuleConfig::themepath(true);
             // $params = $plugin->prepareArgs(['template'=> 'templates'.$pagedb['page_templates.path'].'::'.$pagedb['page_templates.name'], 'data' => $pagedb]);
             $params = $plugin->prepareArgs(['template' => $themepath . $pagedb['page_templates.path'] . '::' . $pagedb['page_templates.name'], 'data' => $pagedb]);
             $plugin->trigger('page::cache-render.pre', $params);
             // $content = $this->template->render('templates'.$pagedb['page_templates.path'].'::'.$pagedb['page_templates.name'], $pagedb);
             $content = $this->template->render($params['template'], $params['data']);
             // var_dump($content);exit(__FILE__.'::'.__LINE__);
             $pagename = '/' . $pagedb['slug'] . '.html';
             $pagedir = $cachepath . $pagedb['path'];
             if (!is_dir($pagedir)) {
                 // dir doesn't exist, make it
                 mkdir($pagedir, 0777, true);
             }
             $file = $pagedir . $pagename;
             if (file_put_contents($file, $content)) {
                 $path = $pagedb['path'] . $pagename;
             }
         }
         // var_dump($path);exit(__FILE__.'::'.__LINE__);
         // return $this->redirect($path.'.html', $url, $Response);
         // return $this->redirect("/data/cache/html/$filename", $url, $Response);
         return $this->redirect($path, $url, $Response);
     } catch (\Exception $e) {
         // var_dump($e->getMessage());exit(__FILE__.'::'.__LINE__);
         return $Next($Request, $Response);
     }
 }