Source for file MapFishLayerTreeWidget.php

Documentation is available at MapFishLayerTreeWidget.php

  1. <?php
  2. /**
  3. * Widget de type mapfishlayertree
  4. *
  5. * PHP versions 5
  6. @category  PHP
  7. @package   GeoPrisma
  8. @author    Pascal Martin
  9. @copyright 2009, Boreal - Information Strategies
  10. @license   http://www.geoprisma.org/license BSD License
  11. @link      http://www.geoprisma.org
  12. */
  13.  
  14. /**
  15. * Widget de type mapfishlayertree
  16. @category   PHP
  17. @package    GeoPrisma
  18. @subpackage Widget
  19. @author     Pascal Martin
  20. */  
  21. {
  22.     const MAP_FISH_LAYER_TREE_WIDGET = 'mapfishlayertree';
  23.  
  24.     const PATH_DELIMITER = '/';
  25.  
  26.     const DOMAIN_KEY_LOOKUP_PATTERN = '#@([^@:]*):([^@:]*)@#';
  27.     
  28.     /**
  29.     * @var DOMDocument 
  30.     */
  31.     private $m_objDomDocumentOptions null
  32.     
  33.     /**
  34.     * Construit un instance de org_geoprisma_widget_MapWidget
  35.     *
  36.     * @param array &$pobjArray Config node
  37.     * 
  38.     * @return org_geoprisma_widget_MapWidget 
  39.     */
  40.     public function __construct(&$pobjArray
  41.     {
  42.         if (isset($pobjArray['id'])) 
  43.         {
  44.             $strName = (string)$pobjArray['id'];
  45.         }
  46.         else
  47.         {
  48.             $strName = (string)$pobjArray['name'];
  49.         }
  50.  
  51.         $this->m_objArrayOptions = $pobjArray['options'];
  52.  
  53.         if (!isset($this->m_objArrayOptions['nodes'])) 
  54.         {
  55.             $this->m_objArrayOptions['nodes'array();
  56.         }
  57.  
  58.         parent::__construct($strName);
  59.     }
  60.  
  61.     /**
  62.     * Given an array of nodes, remove each ones that are leaves and that have
  63.     *     non-authorized or inexistant resources.  Nodes that are not leaves
  64.     *     (that contain other nodes) should have its childs dealt with also.
  65.     * 
  66.     * @param array $pobjArrayNodes Array of nodes
  67.     * 
  68.     * @return array 
  69.     */
  70.     private function removeNodesWithNoRight($pobjArrayNodes)
  71.     {
  72.         foreach ($pobjArrayNodes as $strNodeKey => $objArrayNode)
  73.         {
  74.             // if current node has children
  75.             if (isset($objArrayNode['nodes'])) 
  76.             {
  77.                 $pobjArrayNodes[$strNodeKey]['nodes'
  78.                     = $this->removeNodesWithNoRight($objArrayNode['nodes']);
  79.  
  80.                 // if current node had all its children removed, remove it
  81.                 if (count($pobjArrayNodes[$strNodeKey]['nodes']== 0
  82.                 {
  83.                     unset($pobjArrayNodes[$strNodeKey]);
  84.                 }
  85.             }
  86.             // if current node is a leaf
  87.             else
  88.             {
  89.                 if (isset($objArrayNode['resourcename'])) 
  90.                 {
  91.                     $strResource $objArrayNode['resourcename'];
  92.                     $strAction $this->getAction();
  93.  
  94.                     try
  95.                     {
  96.                         $objResource org_geoprisma_config_ConfigFactory::getConfig()->getResource($strResource);
  97.                         if (!org_geoprisma_acl_ACLFactory::getACL()->isAuthorized($objResource$strAction)) 
  98.                         {                       
  99.                             unset($pobjArrayNodes[$strNodeKey]);
  100.                         }                 
  101.                     }
  102.                     catch(org_geoprisma_exception_ResourceNotFoundException $objError)
  103.                     {
  104.                         unset($pobjArrayNodes[$strNodeKey]);
  105.                     }
  106.                 }
  107.             }
  108.         }
  109.  
  110.         return $pobjArrayNodes;
  111.     }
  112.  
  113.     /**
  114.     * Remove a specific leaf using its resourcename and servicetype values from
  115.     *     given nodes.  If the parent node becomes empty, it's removed as well.
  116.     * 
  117.     * @param array  &$pobjArrayNodes  Array of nodes
  118.     * @param string $pstrResourceName The name of the resourcename of the node
  119.     *                                  to remove
  120.     * @param string $pstrServiceType  The servicetype of the node to remove
  121.     * 
  122.     * @return void 
  123.     */
  124.     public function removeLeaf(
  125.         &$pobjArrayNodes$pstrResourceName$pstrServiceType
  126.     {
  127.         foreach ($pobjArrayNodes as $strNodeKey => &$objArrayNode)
  128.         {
  129.             // if current node has children
  130.             if (isset($objArrayNode['nodes'])) 
  131.             {
  132.                     $this->removeLeaf(
  133.                         $objArrayNode['nodes'],
  134.                         $pstrResourceName,
  135.                         $pstrServiceType
  136.                     );
  137.  
  138.                 // if current node had all its children removed, remove it
  139.                 if (count($pobjArrayNodes[$strNodeKey]['nodes']== 0
  140.                 {
  141.                     unset($pobjArrayNodes[$strNodeKey]);
  142.                 }
  143.             }
  144.             // if current node is a leaf
  145.             else
  146.             {
  147.                 if (isset($objArrayNode['resourcename']
  148.                     && $objArrayNode['resourcename'== $pstrResourceName
  149.                     && isset($objArrayNode['servicetype']
  150.                     && $objArrayNode['servicetype'== $pstrServiceType
  151.                 
  152.                 {         
  153.                     unset($pobjArrayNodes[$strNodeKey]);
  154.                 }
  155.             }
  156.         }
  157.     }
  158.     
  159.     /**
  160.     * Retourne le type du widget
  161.     * 
  162.     * @return string 
  163.     */
  164.     public function getType()
  165.     {
  166.         return self::MAP_FISH_LAYER_TREE_WIDGET;   
  167.     }
  168.     
  169.     /**
  170.     *  Ajout le widget courant (this) dans le xml
  171.     * 
  172.     * @param DOMElement  &$pobjDomElementWidgets Noeud xml ou le widget doit ĂȘtre ajouter
  173.     * @param DOMDocument &$pobjDomDocument       Document XMl ou le noeud xml datastore est ajouter, utiliser pour construire les noeuds
  174.     * 
  175.     * @return void 
  176.     */
  177.     public function insertIntoXML(DOMElement &$pobjDomElementWidgetsDOMDocument &$pobjDomDocument)
  178.     {
  179.         $this->m_objArrayOptions['nodes']
  180.             = $this->removeNodesWithNoRight($this->m_objArrayOptions['nodes']);
  181.  
  182.         if ($this->getResources()->count(0
  183.         {
  184.             $objDomElementWidget $pobjDomDocument->createElement('widget');
  185.             $pobjDomElementWidgets->appendChild($objDomElementWidget);    
  186.            
  187.             $objDomElementType $pobjDomDocument->createElement('type'$this->getType());
  188.             $objDomElementName $pobjDomDocument->createElement('name'$this->getName());
  189.             $objDomElementWidget->appendChild($objDomElementType);
  190.             $objDomElementWidget->appendChild($objDomElementName);       
  191.  
  192.             $objDomNodeParams $this->getDomElements(
  193.                 'options'$this->m_objArrayOptions$pobjDomDocument
  194.             );
  195.  
  196.             $objDomElementWidget->appendChild($objDomNodeParams);  
  197.             
  198.             return $objDomElementWidget;
  199.         }
  200.         else
  201.         {
  202.             return null;   
  203.         }
  204.     }
  205.     
  206.     /**
  207.     * Retourne une liste de nom de ressource utiliser par ce widget
  208.     * 
  209.     * @return ArrayObject of string
  210.     */
  211.     public function getResources()
  212.     {    
  213.         $objArrayResource new ArrayObject();        
  214.         $objArrayResourceNames 
  215.             = $this->getResourceNames($this->m_objArrayOptions['nodes']);
  216.  
  217.         foreach ($objArrayResourceNames as $strResourceName
  218.         {
  219.             try
  220.             {
  221.                 org_geoprisma_config_ConfigFactory::getConfig()->getResource($strResourceName);                
  222.                 $objArrayResource->append($strResourceName);
  223.             
  224.             catch(org_geoprisma_exception_ResourceNotFoundException $objError)
  225.             {
  226.                 continue;
  227.             }
  228.         }
  229.  
  230.         return $objArrayResource;         
  231.     }
  232.     
  233.     /**
  234.     * Retourne une liste d'information sur les datastores utilisĂ©
  235.     * 
  236.     * @return ArrayObject of array('resourcename' => ?, 'datastorename' => ?,  'servicename' => ? )
  237.     */
  238.     public function getDatastores()
  239.     {
  240.         return self::getDatastoresFromResources($this->getResources());        
  241.     }
  242.  
  243.     /**
  244.     * Return an array of resource name unique values from given "nodes" array.
  245.     * 
  246.     * @param array $pobjArrayNodes Array of node.
  247.     *
  248.     * @return array 
  249.     */
  250.     public function getResourceNames($pobjArrayNodes
  251.     {
  252.         $objArrayResourceNames array();
  253.  
  254.         foreach ($pobjArrayNodes as $objArrayNode
  255.         {
  256.             if (isset($objArrayNode['resourcename'])) 
  257.             {
  258.                 array_push(
  259.                     $objArrayResourceNames$objArrayNode['resourcename']
  260.                 );
  261.             }
  262.             else if (isset($objArrayNode['nodes'])) 
  263.             {
  264.                 $objArrayResourceNames array_merge(
  265.                     $objArrayResourceNames,
  266.                     $this->getResourceNames($objArrayNode['nodes'])
  267.                 );
  268.             }
  269.         }
  270.  
  271.         return array_unique($objArrayResourceNames);
  272.     }
  273.  
  274.     /**
  275.     * Prepare and call to the 'addLayerNodeFromPathArray' method to set the
  276.     *     options 'nodes' property with the new layer node added.
  277.     * 
  278.     * @param string                          &$pstrPath        The path in which
  279.     *                                                           to add the new
  280.     *                                                           layer node
  281.     * @param org_geoprisma_resource_Resource &$pobjResource    Resource object
  282.     * @param string                          &$pstrServiceType Service type
  283.     * @param array                           &$pobjOptions     Array of layer
  284.     *                                                           node options.
  285.     *
  286.     * @return void 
  287.     */
  288.     public function addLayerNodeFromPath(
  289.         &$pstrPath,
  290.         &$pobjResource,
  291.         &$pstrServiceType,
  292.         &$pobjOptions
  293.     {
  294.         $objArrayNodesToAdd explode(self::PATH_DELIMITER$pstrPath);
  295.  
  296.         $this->addLayerNodeFromPathArray(
  297.             $objArrayNodesToAdd,
  298.             $this->m_objArrayOptions['nodes'],
  299.             $pobjResource,
  300.             $pstrServiceType,
  301.             $pobjOptions
  302.         );
  303.     }
  304.  
  305.     /**
  306.     * Add a new layer node to the 'nodes' array OR browse nodes using the
  307.     *     'paths' array in order to add it to the according tree position.  If
  308.     *     a 'path' node doesn't exists, it creates it at the same time.
  309.     * 
  310.     * @param array                           &$pobjArrayPaths  Array of nodes
  311.     *                                                           in which to add
  312.     *                                                           the new layer
  313.     *                                                           node
  314.     * @param array                           &$pobjArrayNodes  Array of nodes in
  315.     *                                                           which to push the
  316.     *                                                           a new node (leaf
  317.     *                                                           or container).
  318.     * @param org_geoprisma_resource_Resource &$pobjResource    Resource object
  319.     * @param string                          &$pstrServiceType Service type
  320.     * @param array                           &$pobjOptions     Array of layer
  321.     *                                                           node options.
  322.     *
  323.     * @return array Nodes containing the new ones added.
  324.     */
  325.     public function addLayerNodeFromPathArray(
  326.         &$pobjArrayPaths,
  327.         &$pobjArrayNodes,
  328.         &$pobjResource,
  329.         &$pstrServiceType,
  330.         &$pobjOptions
  331.     {
  332.         // if there are no more paths, that means we are in the leaf container,
  333.         // so create and push the new leaf
  334.         if (count($pobjArrayPaths== 0
  335.         {
  336.             array_push(
  337.                 $pobjArrayNodes,
  338.                 array_merge(
  339.                     $pobjOptions,
  340.                     array(
  341.                         'servicetype' => $pstrServiceType,
  342.                         'resourcename' => $pobjResource->getName(),
  343.                         'textkey' => $pobjResource->getTitle()
  344.                     )
  345.                 )
  346.             );
  347.         }
  348.         // see if the path has an existing node.  If if does, move in it, else
  349.         // create a new one then move in it.
  350.         else
  351.         {
  352.             // shift current path
  353.             $strPath array_shift($pobjArrayPaths);
  354.  
  355.             // check if path contains domain:key
  356.             preg_match(
  357.                 self::DOMAIN_KEY_LOOKUP_PATTERN,
  358.                 $strPath,
  359.                 $objArrayMatchs
  360.             );
  361.  
  362.             // if it does
  363.             if (count($objArrayMatchs== 3
  364.             {
  365.                 $objArrayTextKey array(
  366.                     "domain" => $objArrayMatchs[1],
  367.                     "key" => $objArrayMatchs[2]
  368.                 );
  369.             }
  370.             else
  371.             {
  372.                 $objArrayTextKey $strPath;
  373.             }
  374.             
  375.             $iIndex $this->indexOfNodeByTextKey(
  376.                 $pobjArrayNodes,
  377.                 $objArrayTextKey
  378.             );
  379.  
  380.             // if node exists
  381.             if ($iIndex != -&& isset($pobjArrayNodes[$iIndex]['nodes'])) 
  382.             {
  383.                 $this->addLayerNodeFromPathArray(
  384.                     $pobjArrayPaths,
  385.                     $pobjArrayNodes[$iIndex]['nodes'],
  386.                     $pobjResource,
  387.                     $pstrServiceType,
  388.                     $pobjOptions
  389.                 );
  390.             }
  391.             // if node doesn't exists
  392.             else
  393.             {
  394.                 $objArrayNode array(
  395.                     "textkey" => $objArrayTextKey,
  396.                     "checked" => "undefined",
  397.                     "nodes" => array()
  398.                 );
  399.  
  400.                 $this->addLayerNodeFromPathArray(
  401.                     $pobjArrayPaths,
  402.                     $objArrayNode['nodes'],
  403.                     $pobjResource,
  404.                     $pstrServiceType,
  405.                     $pobjOptions
  406.                 );
  407.  
  408.                 array_push(
  409.                     $pobjArrayNodes,
  410.                     $objArrayNode
  411.                 );
  412.             }
  413.         }
  414.     }
  415.  
  416.     /**
  417.     * Check if a node exists in an array of nodes using the 'textkey' property.
  418.     *     It may sometimes be defined as a 'domain' & 'key' array, so it takes
  419.     *     care of that too.
  420.     *
  421.     * If it does, returns the key of the node, else returns -1.
  422.     * 
  423.     * @param array  &$pobjArrayNodes Array of nodes
  424.     * @param string $pstrTextKey     TextKey value.  Can also be an array with
  425.     *                                 'domain' and 'key' keys.
  426.     *
  427.     * @return integer 
  428.     */
  429.     public function indexOfNodeByTextKey(&$pobjArrayNodes$pstrTextKey
  430.     {
  431.         if (is_array($pstrTextKey)) 
  432.         {
  433.             foreach ($pobjArrayNodes as $iKey => $objNode
  434.             {
  435.                 if (getType($objNode['textkey']== 'array'
  436.                 {
  437.                     if (is_array($objNode['textkey'])
  438.                         && isset($objNode['textkey']['domain'])
  439.                         && isset($objNode['textkey']['key'])
  440.                         && $objNode['textkey']['domain'=== $pstrTextKey['domain']
  441.                         && $objNode['textkey']['key'=== $pstrTextKey['key']
  442.                     
  443.                     {
  444.                         return $iKey;
  445.                     }
  446.                 }
  447.             }
  448.         }
  449.         else
  450.         {
  451.             foreach ($pobjArrayNodes as $iKey => $objNode
  452.             {
  453.                 if ($objNode['textkey'=== $pstrTextKey
  454.                 {
  455.                     return $iKey;
  456.                 }
  457.             }
  458.         }
  459.  
  460.         return -1;
  461.     }
  462.  
  463.     /**
  464.     * Returns a reference to the root nodes of the layertree inside the options.
  465.     *
  466.     * @return array 
  467.     */
  468.     public function &getNodes(
  469.     {
  470.         return $this->m_objArrayOptions['nodes'];
  471.     }
  472. }
  473.   
  474. ?>

Documentation generated on Mon, 20 Feb 2012 13:46:24 -0500 by phpDocumentor 1.4.1