Phundament-Packages
[ class tree: Phundament-Packages ] [ index: Phundament-Packages ] [ all elements ]

Source for file LPageService.php

Documentation is available at LPageService.php

  1. <?php
  2.  
  3.  
  4. /**
  5.  * class file.
  6.  *
  7.  * @license http://opensource.org/licenses/mozilla1.1.php Mozilla Public License
  8.  * @copyright 2005, diemeisterei GmbH. All rights reserved.
  9.  * @author $Author: schmunk $
  10.  * @version $Revision: 636 $  $Date: 2007-02-25 14:01:09 +0100 (So, 25 Feb 2007) $
  11.  * @package Phundament.Packages
  12.  * @subpackage LVirtualNodes
  13.  */
  14.  
  15. /**
  16.  * Extended page service.
  17.  *
  18.  * This service is able to use propel node tree objects from a database
  19.  * (l_virtual_nodes). You can request a node with a predefined NODE_PREFIX the node
  20.  * maps to a .page or and external link.
  21.  *
  22.  * @package System.Web.Services
  23.  * @subpackage Services
  24.  */
  25.  
  26. Prado :: setPathOfAlias("LPageService"dirname(__FILE__));
  27. if (is_dir(dirname(__FILE__"/db/propel"))
  28. {
  29.     Prado :: using("LPageService.db.propel.*");
  30.     Prado :: using("LPageService.db.propel.l_virtual_nodes.*");
  31. }
  32.  
  33. class LPageService extends TPageService
  34. {
  35.     const NODE_PREFIX'*';
  36.     const PATH_PREFIX'ยง';
  37.  
  38.     private $_nodes;
  39.     private $_nodes_list;
  40.     private $_currentNode            null;
  41.     private $_currentNodeId        0;
  42.     private $_currentNodeAlias    null;
  43.  
  44.     private $_areNodesAvailable;
  45.  
  46.     /**
  47.      * Returns if node-based service is available
  48.      */
  49.     public function areNodesAvailable()
  50.     {
  51.         if ($this->Application->getGlobalState("LVirtualNodes:available"!== true)
  52.         {
  53.             $this->checkVirtualNodes();
  54.             $this->Application->setGlobalState("LVirtualNodes:available"$this->_areNodesAvailable);
  55.         }
  56.         else
  57.         {
  58.             $this->_areNodesAvailable$this->Application->getGlobalState("LVirtualNodes:available");
  59.         }
  60.         return $this->_areNodesAvailable;
  61.     }
  62.  
  63.     /**
  64.      * Returns all nodes in tree structure starting from root
  65.      */
  66.     function getNodes()
  67.     {
  68.         if (!is_object($this->_nodes))
  69.         {
  70.             $this->prepareNodes();
  71.         }
  72.         return $this->_nodes;
  73.     }
  74.  
  75.     /**
  76.      * Returns all nodes in list structure
  77.      */
  78.     function getNodesList()
  79.     {
  80.         if (!is_object($this->_nodes_list))
  81.         {
  82.             $this->prepareNodes();
  83.         }
  84.         return $this->_nodes_list;
  85.     }
  86.  
  87.     /**
  88.      * Returns the current node object
  89.      */
  90.     function getCurrentNode()
  91.     {
  92.         if ($this->_currentNodeId == || $this->_currentNode !== null)
  93.             return $this->_currentNode;
  94.  
  95.         $this->_currentNode $this->findNodeById($this->getNodes()$this->_currentNodeId);
  96.         return $this->_currentNode;
  97.     }
  98.  
  99.     /**
  100.      * Returns the current node object
  101.      */
  102.     function getPageName()
  103.     {
  104.         if (!is_object($this->getCurrentNode()))
  105.             return $this->getServicePage();
  106.         $string($this->getCurrentNode()->getDescriptiveName()) $this->getCurrentNode()->getDescriptiveName($this->getCurrentNode()->getAlias();
  107.         return $string;
  108.     }
  109.  
  110.     /**
  111.      * return current selected page for this service
  112.      */
  113.     public function getServicePage($return_node_aliasfalse)
  114.     {
  115.         if (!$this->_currentNodeAlias && !$this->_currentNodeId)
  116.         {
  117.             // static page
  118.             $p $this->getRequest()->getServiceParameter();
  119.             return (empty ($p)) $this->getDefaultPage($p;
  120.         }
  121.         else if ($return_node_alias)
  122.             return $this->_currentNodeAlias;
  123.         else
  124.             return self :: NODE_PREFIX $this->_currentNodeId;
  125.     }
  126.  
  127.     /**
  128.      * Reads MasterConfig
  129.      */
  130.     public function init($config)
  131.     {
  132.         parent :: init($config);
  133.     }
  134.  
  135.     /**
  136.      * Checks availablity of node service
  137.      */
  138.     private function checkVirtualNodes()
  139.     {
  140.         try
  141.         {
  142.             $conn$this->Application->getModule("database")->getConnection("l_virtual_nodes""propel");
  143.             $query"SELECT id FROM l_virtual_nodes_site LIMIT 1";
  144.             $resultLCreole :: doQuery($query$conn)// Propel-like object (getter & setter)
  145.             #var_dump($result[0]);
  146.             $this->_areNodesAvailabletrue;
  147.             Prado :: trace("Virtual Nodes available"'Lithron.LPageService');
  148.         }
  149.         catch (Exception $e)
  150.         {
  151.             Prado :: log($e->getMessage()4'Lithron.LPageService');
  152.             $this->_areNodesAvailablefalse;
  153.         }
  154.     }
  155.     /**
  156.      * Queries all nodes in database and serves them for later use
  157.      */
  158.     private function prepareNodes()
  159.     {
  160.         // query nodes if not in cache
  161.         if (!is_object($this->_nodes))
  162.         {
  163.             $conn$this->Application->getModule("database")->getConnection("l_virtual_nodes""propel");
  164.             $this->_nodesLVirtualNodesSiteNodePeer :: retrieveRootNode(true$conn);
  165.  
  166.             $fcnew Criteria;
  167.             $fc->addAnd(LVirtualNodesSitePeer :: ID1Criteria :: NOT_EQUAL);
  168.             $fc->addAscendingOrderByColumn(LVirtualNodesSitePeer :: PATH);
  169.             $this->_nodes_listLVirtualNodesSitePeer :: doSelect($fc);
  170.  
  171.             Prado :: trace("Nodes prepared ..."'Lithron.LPageService');
  172.         }
  173.     }
  174.  
  175.     public function getNodeById($id)
  176.     {
  177.         if (substr($id01== self :: NODE_PREFIX)
  178.         {
  179.             $idstr_replace(self :: NODE_PREFIX""$id);
  180.             return $this->findNodeById($this->getNodes()$id);
  181.         }
  182.         elseif (is_numeric($id))
  183.         {
  184.             return $this->findNodeById($this->getNodes()$id);
  185.         }
  186.         else
  187.         {
  188.             return $this->findNodeByAlias($this->getNodes()$id);
  189.         }
  190.     }
  191.  
  192.     /**
  193.      * Finds a node in the tree by id
  194.      */
  195.     private function findNodeById($startNode$searchId)
  196.     {
  197.         if ($startNode->getId(== $searchId)
  198.             return $startNode;
  199.         $child$startNode->getFirstChildNode();
  200.         while ($child)
  201.         {
  202.             $nodeId$child->getId();
  203.             if ($nodeId == $searchId)
  204.             {
  205.                 return $child;
  206.             }
  207.             $return$this->findNodeById($child$searchId);
  208.             if ($return)
  209.                 return $return;
  210.             $child$child->getSiblingNode();
  211.         }
  212.     }
  213.  
  214.     /**
  215.      * Finds a node in the tree by alias
  216.      */
  217.     private function findNodeByAlias($startNode$searchAlias)
  218.     {
  219.         #echo $startNode->getAlias()."=$searchAlias | ";
  220.         if ($startNode->getAlias(== $searchAlias)
  221.             return $startNode;
  222.         $child$startNode->getFirstChildNode();
  223.         while ($child)
  224.         {
  225.             $alias$child->getAlias();
  226.             if ($alias == $searchAlias)
  227.             {
  228.                 return $child;
  229.             }
  230.             $return$this->findNodeByAlias($child$searchAlias);
  231.             if ($return)
  232.                 return $return;
  233.             $child$child->getSiblingNode();
  234.         }
  235.     }
  236.  
  237.     /**
  238.      * Determines the requested page path.
  239.      * 1. nodeId (database needed)
  240.      * 2. path (database needed)
  241.      * 3. page
  242.      * @return string page path requested
  243.      */
  244.     protected function determineRequestedPagePath()
  245.     {
  246.         Prado :: log("Determining requested PagePath"1'Lithron.LPageService');
  247.  
  248.         $param $this->getRequest()->getServiceParameter();
  249.         $cache $this->Application->Cache;
  250.         $cache null;
  251.  
  252.         // default page
  253.         if (empty ($param))
  254.         {
  255.             $param $this->getDefaultPage();
  256.             Prado :: log("default page requested ($param)"1'Lithron.LPageService');
  257.         }
  258.  
  259.         // static page
  260.         $static_page_file Prado :: getPathOfAlias("Pages""/" str_replace(".""/"$param".page";
  261.         if (file_exists($static_page_file))
  262.         {
  263.             $pagePath $param;
  264.             Prado :: log("static page found ($pagePath)"1'Lithron.LPageService');
  265.             return $pagePath;
  266.         }
  267.  
  268.         // check specials
  269.         $special $param[0];
  270.         $suffix substr($param1);
  271.         switch($special)
  272.         {
  273.             case self::NODE_PREFIX:
  274.                 if (!is_numeric($suffix))
  275.                     throw new Exception("no valid id given!");
  276.                 $this->_currentNodeId TPropertyValue::ensureInteger($suffix);
  277.                 $cache_entry_path  "LVirtualNodes:PathForNodeId:" $suffix;
  278.                 $cache_entry_alias "LVirtualNodes:AliasForNodeId:" $suffix;
  279.                 if ($cache && $cache->get($cache_entry_path&& $cache->get($cache_entry_alias))
  280.                 {
  281.                     $pagePath $cache->get($cache_entry_path);
  282.                     $this->_currentNodeAlias $cache->get($cache_entry_alias);
  283.                     Prado :: log("node by id ($suffixfound in cache ($pagePath)"1'Lithron.LPageService');
  284.                     return $pagePath;
  285.                 }
  286.                 $current_node $this->findNodeById($this->getNodes()$suffix);
  287.                 if (!$current_node)
  288.                     throw new Exception("node with id $suffix not found!");
  289.                 $pagePath $current_node->getPage();
  290.                 $this->_currentNodeAlias $current_node->getAlias();
  291.                 if ($cache)
  292.                 {
  293.                     $cache->set($cache_entry_path$pagePath);
  294.                     $cache->set($cache_entry_alias$this->_currentNodeAlias);
  295.                 }
  296.                 Prado :: log("node by id ($suffixfound in database and saved to cache ($pagePath)"1'Lithron.LPageService');
  297.                 return $pagePath;
  298.         }
  299.  
  300.         // check alias
  301.         $cache_entry_path "LVirtualNodes:PathForNodeAlias:" $param;
  302.         $cache_entry_id   "LVirtualNodes:IdForNodeAlias:" $param;
  303.         $this->_currentNodeAlias $param;
  304.         if ($cache && $cache->get($cache_entry_path&& $cache->get($cache_entry_id))
  305.         {
  306.             $pagePath $cache->get($cache_entry_path);
  307.             $this->_currentNodeId $cache->get($cache_entry_id);
  308.             Prado :: log("node by alias ($paramfound in cache ($pagePath)"1'Lithron.LPageService');
  309.             return $pagePath;
  310.         }
  311.         $current_node $this->findNodeByAlias($this->getNodes()$param);
  312.         if ($current_node)
  313.         {
  314.             $pagePath $current_node->getPage();
  315.             $this->_currentNodeId $current_node->getId();
  316.             if ($cache)
  317.             {
  318.                 $cache->set($cache_entry_path$pagePath);
  319.                 $cache->set($cache_entry_id$this->_currentNodeId);
  320.             }
  321.             Prado :: log("node by alias ($paramfound in database and saved to cache ($pagePath)"1'Lithron.LPageService');
  322.             return $pagePath;
  323.         }
  324.  
  325.         // when we come here, the requested page is no
  326.         // A) static page
  327.         // B) node page by id
  328.         // C) node page by alias
  329.         // we could fall back to prado here, but I will rather throw an exception,
  330.         // because it's pretty sure Prado can't serve anything else.
  331.         throw new THttpException(404,'pageservice_page_unknown'$param);
  332.  
  333.         $pagePath $param;
  334.         Prado :: log("unknown pagetype - falling back to PRADO ($pagePath)"1'Lithron.LPageService');
  335.         return $pagePath;
  336.     }
  337.  
  338.     /**
  339.      * wrapper function
  340.      */
  341.     public function constructUrl($pagePath$getParamsnull$encodeAmpersandtrue$encodeGetItemstrue$logfalse)
  342.     {
  343.         if ($logecho "constructUrl for $pagePath<br>";
  344.         if ($pattern$this->Application->Parameters['LinkUrlType'])
  345.         {
  346.             $p$this->parseGetParams($getParams$encodeAmpersand$encodeGetItems);
  347.             if ($this->Application->getModule('geo_ip'instanceof TModule)
  348.             {
  349.                 $geoip_country_code$this->Application->getModule('geo_ip')->getCountryCode();
  350.                 $geoip_country_name$this->Application->getModule('geo_ip')->getCountryName();
  351.             }
  352.             else
  353.             {
  354.                 $geoip_country_code"--";
  355.                 $geoip_country_name"--";
  356.             }
  357.             $transarray (
  358.                 "{REQ_APPURL}" => $this->Request->ApplicationUrl,
  359.                 "{REQ_APPURL_NOINDEX}" => str_replace("index.php",
  360.                 "",
  361.                 $this->Request->ApplicationUrl
  362.             )"{CULTURE}" => $this->Application->Globalization->Culture"{GEOIP_COUNTRY}" => $geoip_country_code"{ALIAS}" => $pagePath"{AMP_GET_PARAM}" => ($p "&" $p "")"{QM_GET_PARAM}" => ($p "?" $p ""),);
  363.  
  364.             $res strtr($pattern$trans);
  365.             if ($logvar_dump($res);
  366.             return $res;
  367.         }
  368.  
  369.         return $this->getRequest()->constructUrl($this->getID()$pagePath$getParams$encodeAmpersand$encodeGetItems);
  370.  
  371.     }
  372.  
  373.  
  374.     private function parseGetParams($getParamsnull$encodeAmpersandfalse$encodeGetItemstrue)
  375.     {
  376.         $params"";
  377.         if ($getParams != null)
  378.         {
  379.             foreach ($getParams AS $key => $value)
  380.                 $params .= $key "=" $value "&"// TODO
  381.             return $params;
  382.         }
  383.         else
  384.         {
  385.             return "";
  386.         }
  387.     }
  388.  
  389.     public function reload()
  390.     {
  391.         Lithron :: saveLogging($this->Application);
  392.         $url $this->constructUrl($this->getServicePage(true));
  393.         $this->Response->redirect($url);
  394.     }
  395.  
  396.  
  397. }
  398. ?>

Documentation generated on Sun, 25 Feb 2007 16:11:28 +0100 by phpDocumentor 1.3.1