eZ Publish  [trunk]
ezpathelement.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * File containing the eZPathElement class.
00004  *
00005  * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
00006  * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
00007  * @version //autogentag//
00008  * @package kernel
00009  */
00010 
00011 /*!
00012   \class eZPathElement ezpathelement.php
00013   \brief Handles singular path elements in URL aliases
00014 
00015   This class is similar to eZURLAliasML but is designed to work on single path
00016   elements instead of considering the whole url.
00017 
00018   The definition() of this class is the same as eZURLAliasML but it is not
00019   possible to store and remove items of this class.
00020 */
00021 
00022 class eZPathElement extends eZPersistentObject
00023 {
00024     /*!
00025      Initializes a new path element.
00026     */
00027     function eZPathElement( $row )
00028     {
00029         $this->Path = null;
00030         $this->PathArray = null;
00031         if ( array_key_exists( 'always_available', $row )  )
00032         {
00033             $this->AlwaysAvailable = $row['always_available'];
00034         }
00035         $this->eZPersistentObject( $row );
00036     }
00037 
00038     static public function definition()
00039     {
00040         static $definition = array( "fields" => array( "id" => array( 'name' => 'ID',
00041                                                         'datatype' => 'integer',
00042                                                         'default' => 0,
00043                                                         'required' => true ),
00044                                          "parent" => array( 'name' => 'Parent',
00045                                                             'datatype' => 'integer',
00046                                                             'default' => 0,
00047                                                             'required' => true ),
00048                                          "lang_mask" => array( 'name' => 'LangMask',
00049                                                                'datatype' => 'integer',
00050                                                                'default' => 0,
00051                                                                'required' => true ),
00052                                          "text" => array( 'name' => 'Text',
00053                                                           'datatype' => 'string',
00054                                                           'default' => '',
00055                                                           'required' => true ),
00056                                          "text_md5" => array( 'name' => 'TextMD5',
00057                                                               'datatype' => 'string',
00058                                                               'default' => '',
00059                                                               'required' => true ),
00060                                          "action" => array( 'name' => 'Action',
00061                                                             'datatype' => 'string',
00062                                                             'default' => '',
00063                                                             'required' => true ),
00064                                          "action_type" => array( 'name' => 'ActionType',
00065                                                                  'datatype' => 'string',
00066                                                                  'default' => '',
00067                                                                  'required' => true ),
00068                                          "link" => array( 'name' => 'Link',
00069                                                           'datatype' => 'integer',
00070                                                           'default' => 0,
00071                                                           'required' => true ),
00072                                          "is_alias" => array( 'name' => 'IsAlias',
00073                                                                  'datatype' => 'integer',
00074                                                                  'default' => 0,
00075                                                                  'required' => true ),
00076                                          "is_original" => array( 'name' => 'IsOriginal',
00077                                                                  'datatype' => 'integer',
00078                                                                  'default' => 0,
00079                                                                  'required' => true ),
00080                                          "alias_redirects" => array( 'name' => 'AliasRedirects',
00081                                                                      'datatype' => 'integer',
00082                                                                      'default' => 1,
00083                                                                      'required' => true ) ),
00084                       "keys" => array( "parent", "text" ),
00085                       "function_attributes" => array( "language_object" => "getLanguage",
00086                                                       "action_url" => "actionURL",
00087                                                       "path" => "getPath",
00088                                                       "always_available" => "alwaysAvailable",
00089                                                       "path_array" => "getPathArray" ),
00090                       "class_name" => "eZURLAliasML",
00091                       "name" => "ezurlalias_ml" );
00092         return $definition;
00093     }
00094 
00095     /*!
00096      Storing of path elements is not allowed.
00097      */
00098     function store( $fieldFilters = null )
00099     {
00100         eZDebug::writeError( "Cannot store objects of eZPathElement, use eZURLAliasML instead" );
00101         return;
00102     }
00103 
00104     /*!
00105      Removal of path elements is not allowed.
00106      */
00107     function removeThis()
00108     {
00109         eZDebug::writeError( "Cannot remove objects of eZPathElement, use eZURLAliasML instead" );
00110         return;
00111     }
00112 
00113     /*!
00114      Returns the eZContentLanguage object which maches the element language mask.
00115      */
00116     function getLanguage()
00117     {
00118         return eZContentLanguage::fetch( $this->LangMask );
00119     }
00120 
00121     /*!
00122      Converts the action property into a real url which responds to the
00123      module/view on the site.
00124      */
00125     function actionURL()
00126     {
00127         return eZURLAliasML::actionToUrl( $this->Action );
00128     }
00129 
00130     /*!
00131      Fetches path elements which has the parent $parentID and name $name.
00132      \return An array of path element objects.
00133      */
00134     static public function fetchNamedByParentID( $parentID, $name )
00135     {
00136         $filter = new eZURLAliasQuery();
00137         $filter->paren = $parentID;
00138         $filter->text  = $name;
00139         $filter->limit = false;
00140         return $filter->fetchAll();
00141     }
00142 
00143     /*!
00144      Calculates the full path for the current item and returns it.
00145 
00146      \note If you know the action values of the path use fetchPathByActionList() instead, it is more optimized.
00147      \note The calculated path is cached in $Path.
00148      */
00149     function getPath()
00150     {
00151         if ( $this->Path !== null )
00152             return $this->Path;
00153 
00154         // Fetch path 'text' elements of correct parent path
00155         $path = array( $this->Text );
00156         $id = (int)$this->Parent;
00157         $db = eZDB::instance();
00158         while ( $id != 0 )
00159         {
00160             $query = "SELECT parent, lang_mask, text FROM ezurlalias_ml WHERE id={$id}";
00161             $rows = $db->arrayQuery( $query );
00162             if ( count( $rows ) == 0 )
00163             {
00164                 break;
00165             }
00166             $result = eZURLAliasML::choosePrioritizedRow( $rows );
00167             if ( !$result )
00168             {
00169                 $result = $rows[0];
00170             }
00171             $id = (int)$result['parent'];
00172             array_unshift( $path, $result['text'] );
00173         }
00174         $this->Path = implode( '/', $path );
00175         return $this->Path;
00176     }
00177 
00178     function getPathArray()
00179     {
00180         if ( $this->PathArray !== null )
00181             return $this->PathArray;
00182 
00183         // Fetch path 'text' elements of correct parent path
00184         $path = array( $this );
00185         $id = (int)$this->Parent;
00186         $db = eZDB::instance();
00187         while ( $id != 0 )
00188         {
00189             $query = "SELECT * FROM ezurlalias_ml WHERE id={$id}";
00190             $rows = $db->arrayQuery( $query );
00191             if ( count( $rows ) == 0 )
00192             {
00193                 break;
00194             }
00195             $result = eZURLAliasML::choosePrioritizedRow( $rows );
00196             if ( !$result )
00197             {
00198                 $result = $rows[0];
00199             }
00200             $id = (int)$result['parent'];
00201             array_unshift( $path, new eZPathElement( $result ) );
00202         }
00203         $this->PathArray = $path;
00204         return $this->PathArray;
00205     }
00206 
00207     // Calculates always_available attribute from language mask
00208     function alwaysAvailable()
00209     {
00210         return $this->AlwaysAvailable;
00211     }
00212 
00213     public $AlwaysAvailable;
00214 }
00215 
00216 ?>