- <?php
-
-
- /**
- * class file.
- *
- * @license http://opensource.org/licenses/mozilla1.1.php Mozilla Public License
- * @copyright 2005, diemeisterei GmbH. All rights reserved.
- * @author $Author: schmunk $
- * @version $Revision: 252 $ $Date: 2006-06-06 00:42:56 +0200 (Di, 06 Jun 2006) $
- * @package Lithron.LVirtualNodes
- * @subpackage Services
- */
-
- /**
- * Extended page service.
- *
- * This service is able to use propel node tree objects from a database
- * (l_virtual_nodes). You can request a node with a predefined NODE_PREFIX the node
- * maps to a .page or and external link.
- *
- * @package System.Web.Services
- * @subpackage Services
- */
-
- Prado :: setPathOfAlias("LPageService", dirname(__FILE__));
- if (is_dir(dirname(__FILE__) . "/db/propel"))
- {
- #Prado :: setPathOfAlias("LPageServiceDb", );
- Prado :: using("LPageService.db.propel.*");
- Prado :: using("LPageService.db.propel.l_virtual_nodes.*");
- }
-
- class LPageService extends TPageService
- {
- const NODE_PREFIX= '*';
- const PATH_PREFIX= 'ยง';
-
- private $_nodes;
- private $_rootNodeObj;
- private $_currentNode;
- private $_areNodesAvailable;
-
- /**
- * Returns if node-based service is available
- */
- public function areNodesAvailable()
- {
- if ($this->Application->getGlobalState("LVirtualNodes:available") !== true)
- {
- $this->checkVirtualNodes();
- $this->Application->setGlobalState("LVirtualNodes:available", $this->_areNodesAvailable);
- }
- else
- {
- $this->_areNodesAvailable= $this->Application->getGlobalState("LVirtualNodes:available");
- }
- return $this->_areNodesAvailable;
- }
-
- /**
- * Returns all nodes in tree structure starting from root
- */
- function getNodes()
- {
- if (!is_object($this->_nodes))
- {
- $this->prepareNodes();
- }
- return $this->_nodes;
- }
-
- /**
- * Returns the root node object
- */
- function getRootNodeObj()
- {
- return $this->_rootNodeObj;
- }
-
- /**
- * Returns the current node object
- */
- function getCurrentNode()
- {
- return $this->_currentNode;
- }
- /**
- * return current selected page for this service
- */
- public function getServicePage()
- {
- $p= $this->getRequest()->getServiceParameter();
- $return= (empty ($p)) ? $this->getDefaultPage() : $p;
- return $return;
- }
-
- /**
- * Reads MasterConfig
- */
- public function init($config)
- {
- $_file= $config->getAttributes()->itemAt("LPropelConfigFile");
- if (is_file(dirname(__FILE__) . "/" . $_file))
- {
- $this->_lPropelConfig= require (dirname(__FILE__) . "/" . $_file);
- }
- parent :: init($config);
- }
-
- public function reload()
- {
- $url= $this->constructUrl($this->getRequest()->getServiceParameter());
- $this->Response->redirect($url);
- }
-
- /**
- * Checks availablity of node service
- */
- private function checkVirtualNodes()
- {
- try
- {
- $conn= $this->Application->getModule("database")->getConnection("l_virtual_nodes", "creole");
- $query= "SELECT id FROM l_virtual_nodes_site LIMIT 1";
- $result= LCreole :: doQuery($query, $conn); // Propel-like object (getter & setter)
- #var_dump($result[0]);
- $this->_areNodesAvailable= true;
- Prado :: trace("Virtual Nodes available", 'Lithron.LPageService');
- }
- catch (Exception $e)
- {
- Prado :: log($e->getMessage(), 8, 'Lithron.LPageService');
- $this->_areNodesAvailable= false;
- }
- }
- /**
- * Queries all nodes in database and serves them for later use
- */
- private function prepareNodes()
- {
- // TODO
- // retrieves data item from cache
- Prado :: trace("Looking for nodes in cache ...", 'Lithron.LPageService');
- if ($cache= $this->Application->Cache)
- {
- $c= $cache->get("LPageService:Nodes");
- if (is_string($c))
- {
- $nodes= Prado :: unserialize($c);
- #$this->_nodes= $nodes;
- }
- }
- // query nodes if not in cache
- if (!is_object($this->_nodes))
- {
- $conn= $this->Application->getModule("database")->getConnection("l_virtual_nodes", "propel");
- $this->_nodes= LVirtualNodesSiteNodePeer :: retrieveRootNode(true, $conn);
-
- if ($cache)
- {
- try
- {
- $c= Prado :: serialize($this->_nodes);
- $cache->set("LPageService:Nodes", $c, 60);
- }
- catch (Exception $e)
- {
- Prado :: log("Node caching failed: " . $e->getMessage(), 8, 'Lithron.LPageService');
-
- }
- }
- }
- else
- {
- Prado :: trace("Using cached root node", 'Lithron.LPageService');
- }
-
- }
-
-
- public function getNodeById($id)
- {
- return $this->findNodeById($this->getNodes(), str_replace(self::NODE_PREFIX,"",$id));
- }
- /**
- * Finds a node in the tree by id
- */
- private function findNodeById($startNode, $searchId)
- {
- $child= $startNode->getFirstChildNode();
- while ($child)
- {
- $nodeId= $child->getId();
- if ($nodeId == $searchId)
- {
- return $child;
- }
- $return= $this->findNodeById($child, $searchId);
- if ($return)
- return $return;
- $child= $child->getSiblingNode();
- }
- }
-
- /**
- * Determines the requested page path.
- * 1. nodeId (database needed)
- * 2. path (database needed)
- * 3. page
- * @return string page path requested
- */
- protected function determineRequestedPagePath()
- {
- $special= $this->getRequest()->getServiceParameter();
- $special_key= substr($this->getRequest()->getServiceParameter(), 0, 1);
- $special_string= substr($this->getRequest()->getServiceParameter(), 1);
-
- // initialize virtual node system $nodeId = $this->getRequest()->itemAt("nodeId")
- if ($special_key == self :: NODE_PREFIX && is_numeric($special_string))
- {
- // find current node
- $this->_currentNode= & $this->findNodeById($this->getNodes(), $special_string);
- if (!is_object($this->_currentNode))
- {
- Prado :: log("Node (" . $special_string . ") NOT FOUND", 3, 'Lithron.LPageService');
- }
- else
- {
- // set pagePath with value from node object
- $pagePath= $this->_currentNode->getPage();
- Prado :: log("Current node found ($pagePath)", 2, 'Lithron.LPageService');
- }
- }
- elseif ($special_key == self :: PATH_PREFIX)
- {
- // TODO
- Prado :: trace("Function currently not implemented", 'Lithron.LPageService');
- }
- else
- {
- // use normal mode
- $pagePath= $this->getRequest()->getServiceParameter();
- Prado :: log("Using PRADO-style request ($pagePath)", 2, 'Lithron.LPageService');
- }
- if (empty ($pagePath))
- $pagePath= $this->getDefaultPage();
- return $pagePath;
- }
-
- /**
- * wrapper function
- */
- public function constructUrl($pagePath, $getParams= null, $encodeAmpersand= false, $encodeGetItems= true)
- {
- return $this->getRequest()->constructUrl($this->getID(), $pagePath, $getParams, $encodeAmpersand, $encodeGetItems);
- }
-
- }
- ?>