|
eZ Publish
[4.0]
|
00001 <?php 00002 // 00003 // Definition of eZURLAliasFilter class 00004 // 00005 // Created on: <28-Jun-2007 19:50:27 amos> 00006 // 00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ## 00008 // SOFTWARE NAME: eZ Publish 00009 // SOFTWARE RELEASE: 4.0.x 00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS 00011 // SOFTWARE LICENSE: GNU General Public License v2.0 00012 // NOTICE: > 00013 // This program is free software; you can redistribute it and/or 00014 // modify it under the terms of version 2.0 of the GNU General 00015 // Public License as published by the Free Software Foundation. 00016 // 00017 // This program is distributed in the hope that it will be useful, 00018 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00020 // GNU General Public License for more details. 00021 // 00022 // You should have received a copy of version 2.0 of the GNU General 00023 // Public License along with this program; if not, write to the Free 00024 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 00025 // MA 02110-1301, USA. 00026 // 00027 // 00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ## 00029 // 00030 00031 /*! \file ezurlaliasquery.php 00032 */ 00033 00034 /*! 00035 \class eZURLAliasQuery ezurlaliasquery.php 00036 \brief Handles querying of URL aliases with different filters 00037 00038 To use this class instantiate it and then fill in the public 00039 properties with the wanted values. When finished call count() 00040 and/or fetchAll() to perform the wanted operation. Repeated 00041 calls will simply return the cached values. 00042 00043 If you change the properties afterwards call prepare() to reset 00044 internally cached values. 00045 00046 Objects of this class can also be sent to the template system 00047 as a variable and the properties can be accessed directly 00048 including 'count' for the count() function and 'items' for the 00049 'fetchAll' function. 00050 */ 00051 00052 00053 /*! 00054 \todo The hasAttribute, attribute and setAttribute functions can be turned into properties for PHP 5. 00055 */ 00056 class eZURLAliasQuery 00057 { 00058 /*! 00059 Array of action values to include, set to null to fetch all kinds. 00060 e.g. eznode:60 00061 */ 00062 public $actions; 00063 /*! 00064 Array of action types to include, set to null to fetch all kinds. 00065 e.g. eznode 00066 */ 00067 public $actionTypes; 00068 /*! 00069 Array of action types to exclude, set to null to disable. 00070 e.g. eznode 00071 */ 00072 public $actionTypesEx; 00073 /*! 00074 If non-null it forces only elements with this parent to be considered. 00075 */ 00076 public $paren; 00077 /*! 00078 If non-null it forces only elements with this text to be considered. 00079 */ 00080 public $text; 00081 /*! 00082 Type of elements to count, use 'name' for only real names for actions, 'alias' for only aliases to the actions or 'all' for real and aliases. 00083 */ 00084 public $type = 'alias'; 00085 /*! 00086 If true languages are filtered, otherwise all languages are fetched. 00087 */ 00088 public $languages = true; 00089 /*! 00090 The offset to start the fetch. 00091 \note It only applies to fetchAll() 00092 */ 00093 public $offset = 0; 00094 /*! 00095 The max limit of the fetch. 00096 \note It only applies to fetchAll() 00097 */ 00098 public $limit = 15; 00099 /*! 00100 The order in which elements are fetched, refers the the DB column of the table. 00101 \note It only applies to fetchAll() 00102 */ 00103 public $order = 'text'; 00104 00105 /*! 00106 \private 00107 Cached value of partial query, used for both count() and fetchAll(). 00108 */ 00109 public $query; 00110 /*! 00111 \private 00112 Cached value of the total count. 00113 */ 00114 public $count; 00115 /*! 00116 \private 00117 Cached value of the fetch items. 00118 */ 00119 public $items; 00120 00121 function eZURLAliasQuery() 00122 { 00123 } 00124 00125 function hasAttribute( $name ) 00126 { 00127 return in_array( $name, 00128 array_diff( get_object_vars( $this ), 00129 array( 'query' ) ) ); 00130 } 00131 00132 function attribute( $name ) 00133 { 00134 switch ( $name ) 00135 { 00136 case 'count': 00137 { 00138 return $this->count(); 00139 } break; 00140 case 'items': 00141 { 00142 return $this->fetchAll(); 00143 } break; 00144 default: 00145 { 00146 return $this->$name; 00147 } break; 00148 } 00149 } 00150 00151 function setAttribute( $name, $value ) 00152 { 00153 $this->$name = $value; 00154 $this->query = null; 00155 $this->count = null; 00156 $this->items = null; 00157 } 00158 00159 /*! 00160 Resets all internally cached values. This must be called 00161 if the properties have been changed after count() or 00162 fetchAll() has been used. 00163 */ 00164 function prepare() 00165 { 00166 $this->query = null; 00167 $this->count = null; 00168 $this->items = null; 00169 } 00170 00171 /*! 00172 Counts the total number of items available using the current 00173 filters as specified with the properties. 00174 \note Can also be fetched from templates by using the 'count' property. 00175 */ 00176 function count() 00177 { 00178 if ( $this->count !== null ) 00179 return $this->count; 00180 00181 if ( $this->query === null ) 00182 { 00183 $this->query = $this->generateSQL(); 00184 } 00185 if ( $this->query === false ) 00186 return 0; 00187 $query = "SELECT count(*) AS count {$this->query}"; 00188 $db = eZDB::instance(); 00189 $rows = $db->arrayQuery( $query ); 00190 if ( count( $rows ) == 0 ) 00191 $this->count = 0; 00192 else 00193 $this->count = $rows[0]['count']; 00194 return $this->count; 00195 } 00196 00197 /*! 00198 Fetches the items in the current range (offset/limit) using the current 00199 filters as specified with the properties. 00200 \note Can also be fetched from templates by using the 'items' property. 00201 */ 00202 function fetchAll() 00203 { 00204 if ( $this->items !== null ) 00205 return $this->items; 00206 00207 if ( $this->query === null ) 00208 { 00209 $this->query = $this->generateSQL(); 00210 } 00211 if ( $this->query === false ) 00212 return array(); 00213 $query = "SELECT * {$this->query} ORDER BY {$this->order}"; 00214 $params = array( 'offset' => $this->offset, 00215 'limit' => $this->limit ); 00216 $db = eZDB::instance(); 00217 $rows = $db->arrayQuery( $query, $params ); 00218 if ( count( $rows ) == 0 ) 00219 $this->items = array(); 00220 else 00221 $this->items = eZURLAliasQuery::makeList( $rows ); 00222 return $this->items; 00223 } 00224 00225 /*! 00226 \private 00227 Generates the common part of the SQL using the properties as filters and returns it. 00228 */ 00229 protected function generateSQL() 00230 { 00231 if ( !in_array( $this->type, array( 'name', 'alias', 'all' ) ) ) 00232 { 00233 eZDebug::writeError( "Parameter \$type must be one of name, alias or all. The value which was used was '{$this->type}'." ); 00234 return null; 00235 } 00236 00237 $db = eZDB::instance(); 00238 if ( $this->languages === true ) 00239 { 00240 $langMask = trim( eZContentLanguage::languagesSQLFilter( 'ezurlalias_ml', 'lang_mask' ) ); 00241 $conds[] = "($langMask)"; 00242 } 00243 00244 if ( $this->paren !== null ) 00245 { 00246 $conds[] = "parent = {$this->paren}"; 00247 } 00248 00249 if ( $this->text !== null ) 00250 { 00251 $conds[] = "text_md5 = " . $db->md5( "'" . $db->escapeString( $this->text ) . "'" ); 00252 } 00253 00254 if ( $this->actions !== null ) 00255 { 00256 // Check for conditions which will return no rows. 00257 if ( count( $this->actions ) == 0 ) 00258 return false; 00259 00260 if ( count( $this->actions ) == 1 ) 00261 { 00262 $action = $this->actions[0]; 00263 $actionStr = $db->escapeString( $action ); 00264 $conds[] = "action = '$actionStr'"; 00265 } 00266 else 00267 { 00268 $actions = array(); 00269 foreach ( $this->actions as $action ) 00270 { 00271 $actions[] = "'" . $db->escapeString( $action ) . "'"; 00272 } 00273 $conds[] = "action IN (" . join( ", ", $actions ) . ")"; 00274 } 00275 } 00276 $actionTypes = null; 00277 if ( $this->actionTypes !== null ) 00278 { 00279 $actionTypes = $this->actionTypes; 00280 } 00281 if ( $this->actionTypesEx !== null ) 00282 { 00283 if ( $actionTypes == null ) 00284 { 00285 $rows = $db->arrayQuery( "SELECT DISTINCT action_type FROM ezurlalias_ml" ); 00286 $actionTypes = array(); 00287 foreach ( $rows as $row ) 00288 { 00289 $actionTypes[] = $row['action_type']; 00290 } 00291 } 00292 $actionTypes = array_values( array_diff( $actionTypes, $this->actionTypesEx ) ); 00293 } 00294 if ( $actionTypes !== null ) 00295 { 00296 // Check for conditions which will return no rows. 00297 if ( count( $actionTypes ) == 0 ) 00298 return false; 00299 00300 if ( count( $actionTypes ) == 1 ) 00301 { 00302 $action = $actionTypes[0]; 00303 $actionStr = $db->escapeString( $action ); 00304 $conds[] = "action_type = '$actionStr'"; 00305 } 00306 else 00307 { 00308 $actions = array(); 00309 foreach ( $actionTypes as $action ) 00310 { 00311 $actions[] = "'" . $db->escapeString( $action ) . "'"; 00312 } 00313 $conds[] = "action_type IN (" . join( ", ", $actions ) . ")"; 00314 } 00315 } 00316 00317 $conds[] = 'is_original = 1'; 00318 if ( $this->type == 'name' ) 00319 { 00320 $conds[] = 'is_alias = 0'; 00321 } 00322 else if ( $this->type == 'alias' ) 00323 { 00324 $conds[] = 'is_alias = 1'; 00325 } 00326 else // 'all' 00327 { 00328 } 00329 00330 return "FROM ezurlalias_ml WHERE " . join( " AND ", $conds ); 00331 } 00332 00333 /*! 00334 \static 00335 Takes an array with database data in $row and turns them into eZPathElement objects. 00336 Entries which have multiple languages will be turned into multiple objects. 00337 */ 00338 static public function makeList( $rows ) 00339 { 00340 if ( !is_array( $rows ) || count( $rows ) == 0 ) 00341 return array(); 00342 $list = array(); 00343 foreach ( $rows as $row ) 00344 { 00345 $row['always_available'] = $row['lang_mask'] % 2; 00346 $mask = $row['lang_mask'] & ~1; 00347 for ( $i = 1; $i < 30; ++$i ) 00348 { 00349 $newMask = (1 << $i); 00350 if ( ($newMask & $mask) > 0 ) 00351 { 00352 $row['lang_mask'] = (1 << $i); 00353 $list[] = $row; 00354 } 00355 } 00356 } 00357 //include_once( 'kernel/classes/ezpathelement.php' ); 00358 $objectList = eZPersistentObject::handleRows( $list, 'eZPathElement', true ); 00359 return $objectList; 00360 } 00361 } 00362 00363 ?>