Example #1
0
 /**
  * event method
  *
  * @param string $eventKey
  * @param mixed $data
  *
  * @return mixed
  */
 public function on($eventKey, $data = null)
 {
     switch ($eventKey) {
         case "request_uri":
             // we need to intercept and store the current page
             $p = new \Phile\Repository\Page();
             $this->pagelist_snippet->currentPage = $p->findByPath($data['uri']);
             break;
         case "template_engine_registered":
             // Here we inject the result of the pagelist into the template variables.
             $data['data']['pagelist'] = $this->pagelist_snippet->pagelist;
             if ($data['data']['current_page'] instanceof DummyPage) {
                 // we're rendering the pagelist snippet, and the user didn't
                 // specify a template
                 // -> we force Twig to accept our default template
                 if ($this->pagelist_snippet->templateDir) {
                     // TODO: Twig only :-(
                     $twig = $data['engine'];
                     $loader = $twig->getLoader();
                     $loader->addPath($this->pagelist_snippet->templateDir);
                     $data['data']['theme_dir'] = $this->pagelist_snippet->templateDir;
                 }
             }
             break;
         case "plugins_loaded":
             // Here we register the (pagelist) snippet with the Snippets class
             $plugins = \Phile\Bootstrap::getInstance()->getPlugins();
             if (array_key_exists('infostreams\\snippets', $plugins)) {
                 $snippets = $plugins['infostreams\\snippets'];
                 $snippets->add($this->pagelist_snippet);
             }
             break;
     }
 }
Example #2
0
 /**
  * Turns internal links into proper absolute links, and/or adds 'http'
  * to external links that were specified without one.
  *
  * @param $link
  * @return string
  */
 protected function getLink($link)
 {
     $page = new \Phile\Repository\Page();
     $linked_page = $page->findByPath($link);
     if ($linked_page) {
         // the user linked to an internal page
         return \Phile\Utility::getBaseUrl() . '/' . $linked_page->getUrl();
     }
     $file = ROOT_DIR . DIRECTORY_SEPARATOR . $link;
     if (file_exists($file)) {
         // the user linked to an internal file
         return \Phile\Utility::getBaseUrl() . '/' . $link;
     }
     // it's not an internal page, it's not an internal file -
     // let's see if it's (at least) a somewhat valid URL
     $url_parts = parse_url($link);
     if (is_array($url_parts)) {
         if (!array_key_exists("scheme", $url_parts)) {
             // it doesn't have a http:// or https:// or similar prefix.
             // This could mean the user provided something like: (link: cnn.com)
             // -> check if the first part of the link looks like a domain name
             $p = explode('/', $url_parts["path"]);
             $domain = $p[0];
             if ($this->isValidDomainName($domain)) {
                 // the first part of the link looks like a valid domain name
                 // -> just prepend 'http://' and continue
                 return "http://{$link}";
             }
         }
     }
     // If we get to this point, we just return whatever the user typed.
     return $link;
 }
 public function pagelist($where, $template = null, $order = null, $under = null, $filter = null, $keyword = null, $param = "q", $inclusive = false)
 {
     $p = new \Phile\Repository\Page();
     $settings = \Phile\Registry::get('Phile_Settings');
     if ($order) {
         $settings = array_merge($settings, array('pages_order' => $order));
     }
     if (!$template) {
         $template = "pagelist-default";
         $this->templateDir = PLUGINS_DIR . "infostreams/pagelistSnippet/Templates";
     }
     $all = $p->findAll($settings);
     $list = array();
     if (is_array($where)) {
         // user provided a list of pages
         // - we obtain those from '$all' to make sure any provided sorting order is applied
         $where = array_map('trim', $where);
         foreach ($all as $p) {
             if (in_array($p->getUrl(), $where)) {
                 $list[] = $p;
             }
         }
     } else {
         // user provided a keyword, such as 'all', 'below' or 'search'
         switch ($where) {
             default:
             case "below":
             case "search":
                 $root = null;
                 if ($under) {
                     $p = new \Phile\Repository\Page();
                     $root = $p->findByPath($under);
                 }
                 // for 'search', we support providing an 'under' argument
                 if ($where == "search" && $root == null) {
                     // however, if it isn't provided, we default to searching all pages
                     $list = $all;
                     break;
                 }
                 if (!$root) {
                     $root = $this->currentPage;
                 }
                 $root_dir = dirname($root->getFilePath());
                 $inclusive = $this->isTrue($inclusive);
                 foreach ($all as $p) {
                     if (strpos($p->getFilePath(), $root_dir) !== false) {
                         if ($inclusive || !$inclusive && $p->getFilePath() != $root->getFilePath()) {
                             $list[] = $p;
                         }
                     }
                 }
                 break;
             case "all":
                 $list = $all;
                 break;
         }
         // once we have obtained a base list of pages, we can search and filter
         if ($where == "search") {
             if (array_key_exists($param, $_GET)) {
                 // retrieve the keyword so we know what to search for,
                 $keyword = urldecode($_GET[$param]);
                 // ... and then remove it from the $_GET array to make sure
                 // that we don't run the search again if we come across a page
                 // that contains another (pagelist: search) snippet.
                 //
                 // This fixes https://github.com/infostreams/pagelist-snippet/issues/4
                 unset($_GET[$param]);
                 $search = new Search($list);
                 $list = $search->query($keyword);
             } else {
                 $list = array();
             }
         }
     }
     if (!is_null($filter) && is_array($filter)) {
         // apply filter to filter by meta tag
         $filter_count = count($filter);
         foreach ($list as $i => $p) {
             $match_count = 0;
             foreach ($filter as $k => $v) {
                 // Check if each filter matches.
                 // Filters can be specified as regular expressions.
                 if (preg_match('/' . trim($v, '/') . '/', $p->getMeta()->{$k})) {
                     $match_count++;
                 }
             }
             if ($match_count != $filter_count) {
                 // if one or more filters don't match, then remove this page from the list
                 unset($list[$i]);
             }
         }
         // renumber filtered list
         $list = array_values($list);
     }
     // if the user provides a keyword to filter/search, do that now
     if (!is_null($keyword)) {
         foreach ($list as $i => $l) {
             if ($l == $this->currentPage) {
                 unset($list[$i]);
                 break;
             }
         }
         $search = new Search($list);
         $list = $search->query($keyword);
     }
     // We have now obtained the list of pages to display. Store it in a public
     // place so that we can inject it into the template rendering process by
     // intercepting the 'template_engine_registered' event in the main Plugin class.
     $this->pagelist = $list;
     // We can start rendering now, but only if the recursion protection says we're good
     if (self::$renderDepth < 3) {
         // (The recursion limit is here to make sure we don't accidentally time-out
         //  recursively rendering the same page)
         self::$renderDepth++;
         // get the template engine, and make sure we're working on an independent copy
         $templateEngine = \Phile\ServiceLocator::getService('Phile_Template');
         $clone = clone $templateEngine;
         // The template engine determines which template to use by looking at the
         // page's metadata. We create a dummy page with only one piece of metadata
         // (the name of the template file) to force the engine to use the template
         // we specify.
         $clone->setCurrentPage(new DummyPage($template));
         // now do the actual rendering and return the output
         $output = $clone->render();
         self::$renderDepth--;
         return $output;
     }
     // the recursion limit has been reached - return an empty string
     return "";
 }
Example #4
0
 /**
  * get the next page if one exist
  *
  * @return null|\Phile\Model\Page
  */
 public function getNextPage()
 {
     $pageRepository = new \Phile\Repository\Page();
     return $pageRepository->getPageOffset($this, 1);
 }