[ Index ]

PHP Cross Reference of ACL Module

title

Body

[close]

/components/strategies/nestedSet/pathMaterialization/ -> PmPathManager.php (source)

   1  <?php
   2  
   3  /**

   4   * The Path-Manager provides basic functionality for working with paths such as:

   5   * - building paths

   6   * - splitting the paths up into their ids

   7   * - building Query-conditions

   8   * 

   9   * @author dispy <dispyfree@googlemail.com>

  10   * @license LGPLv2

  11   * @package acl.strategies.nestedSet.pathMaterialiization 

  12   */
  13  class PmPathManager {
  14     
  15      /**

  16       * Appends the given ID (denoting an element) to the given path 

  17       * 

  18       * @param string $path

  19       * @param string $id

  20       * @return string   the full path including the new element 

  21       */
  22      public static function appendToPath($path, $id){
  23          $separator = PmPathManager::getSeparator();
  24          if(strlen($path) > 0 && $path[strlen($path) -1] != $separator)
  25              $path .= $separator;
  26          return $path.$id.self::getSeparator();
  27      }
  28      
  29      /**

  30       * Returns the parent path of the given path and the ID of the parent

  31       * @param array[string path, string ID] $path 

  32       */
  33      public static function getParentPath($path){
  34          $pos = strrpos($path, PmPathManager::getSeparator());
  35          
  36          //If it's like: /blablubb/dong/

  37          //But consider path "4" ^^

  38          if($pos == strlen($path) - 1 && strlen($path) > 1 ){
  39              $path = substr($path, 0, -1);
  40              $pos = strrpos($path, PmPathManager::getSeparator());
  41          }
  42          //Why? If the separator hasn't been found, we don't need to omit him!

  43          $id = substr($path, $pos + ($pos !== false ? 1 : 0));
  44          $newPath = substr($path, 0, $pos);
  45          
  46          return array('path' => $newPath,'id' => $id);
  47      }
  48      
  49      /**

  50       * Builds an sql-condition like: ($field LIKE [...] [AND condition]) OR ($field LIKE [...] [AND condition])

  51       * for all the given paths

  52       * @param string    $field  the field to match (for example: path)

  53       * @param array[string] $paths

  54       * @param string $additionalCondition If given, this will be used as an additional condition

  55       * to every single path-like-condition (bound with and)

  56       * Occurences of :path will be replaced with the provided path

  57       * @return string   the condition 

  58       */
  59      public static function buildMultiplePathCondition($field, $paths, $additionalCondition = ''){
  60          $condition = '';
  61          
  62          foreach($paths as $path){
  63              //If we aren'T at the beginning

  64              if(strlen($condition) != 0)
  65                  $condition .= ' OR ';
  66              
  67              $condition .= " (".$field." REGEXP CONCAT('^', '".$path."') ". 
  68                          ($additionalCondition ? ' AND '.str_replace(':path', $path, $additionalCondition) : '')
  69                          ." ) ";
  70          }
  71          
  72          return $condition;
  73      }
  74      
  75      public static function getSeparator(){
  76          return '/';
  77      }
  78      
  79  }
  80  
  81  ?>


Generated: Sun Jul 1 19:24:45 2012 Cross-referenced by PHPXref 0.7.1