[ Index ] |
|
Code source de eZ Publish 3.9.0 |
1 <?php 2 // 3 // Definition of eZOrderStatus class 4 // 5 // Created on: <07-Mar-2005 17:20:18 jhe> 6 // 7 // SOFTWARE NAME: eZ publish 8 // SOFTWARE RELEASE: 3.9.0 9 // BUILD VERSION: 17785 10 // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS 11 // SOFTWARE LICENSE: GNU General Public License v2.0 12 // NOTICE: > 13 // This program is free software; you can redistribute it and/or 14 // modify it under the terms of version 2.0 of the GNU General 15 // Public License as published by the Free Software Foundation. 16 // 17 // This program is distributed in the hope that it will be useful, 18 // but WITHOUT ANY WARRANTY; without even the implied warranty of 19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 // GNU General Public License for more details. 21 // 22 // You should have received a copy of version 2.0 of the GNU General 23 // Public License along with this program; if not, write to the Free 24 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 25 // MA 02110-1301, USA. 26 // 27 // 28 29 /*! 30 \class eZOrderStatus ezorderstatus.php 31 \brief Handles statuses which can be used on orders. 32 33 This encapsulates the information about a status using 34 the database table ezorder_status. 35 36 This status can be selected on an order and is also stored 37 in a history per order (eZOrderStatusHistory). 38 39 The status consists of a name, a global ID and whether it is 40 considered active or not. 41 42 The following attributes are defined: 43 - id - The auto increment ID for the status, this is only 44 used to fetch a given status element from the database. 45 - status_id - The global ID of the status, values below 1000 is considerd 46 internal and cannot be removed by the user. 47 - name - The name of the status. 48 - is_active - Whether the status can be used by the end-user or not. 49 50 Some special attributes: 51 - is_internal - Returns true if the status is considerd an internal one (ID less than 1000). 52 53 If the user creates a new status the function storeCustom() must be used, it will 54 find the next available ID in the database and will use locking to avoid race conditions. 55 56 To fetch a given status use fetch() when you have the DB ID or fetchByStatus() if you have 57 a status ID. 58 To fetch lists use fetchList() or fetchOrderedList() for a list sorted by name. 59 If you intend to lookup many statuses using the ID the map from fetchMap() might be useful. 60 To find the number of statuses in the system use orderStatusCount(). 61 62 */ 63 64 include_once ( "kernel/classes/ezpersistentobject.php" ); 65 66 // Define for statuses that doesn't really exist (DB error) 67 define( 'EZ_ORDER_STATUS_UNDEFINED', 0 ); 68 69 // Some predefined statuses, they will also appear in the database. 70 define( 'EZ_ORDER_STATUS_PENDING', 1 ); 71 define( 'EZ_ORDER_STATUS_PROCESSING', 2 ); 72 define( 'EZ_ORDER_STATUS_DELIVERED', 3 ); 73 74 // All custom order statuses have this value or higher 75 define( 'EZ_ORDER_STATUS_CUSTOM', 1000 ); 76 77 class eZOrderStatus extends eZPersistentObject 78 { 79 /*! 80 */ 81 function eZOrderStatus( $row ) 82 { 83 $this->eZPersistentObject( $row ); 84 } 85 86 /*! 87 \return the persistent object definition for the eZOrderStatus class. 88 */ 89 function definition() 90 { 91 return array( "fields" => array( "id" => array( 'name' => 'ID', 92 'datatype' => 'integer', 93 'default' => 0, 94 'required' => true ), 95 "status_id" => array( 'name' => 'StatusID', 96 'datatype' => 'integer', 97 'default' => 0, 98 'required' => true ), 99 "name" => array( 'name' => "Name", 100 'datatype' => 'string', 101 'default' => '', 102 'required' => true ), 103 "is_active" => array( 'name' => "IsActive", 104 'datatype' => 'bool', 105 'default' => true, 106 'required' => true ) ), 107 "keys" => array( "id" ), 108 'function_attributes' => array( 'is_internal' => 'isInternal' ), 109 "increment_key" => "id", 110 "class_name" => "eZOrderStatus", 111 "name" => "ezorder_status" ); 112 } 113 114 /*! 115 \return \c true if the status is considered an internal status. 116 */ 117 function &isInternal() 118 { 119 $isInternal = $this->StatusID < EZ_ORDER_STATUS_CUSTOM; 120 return $isInternal; 121 } 122 123 /*! 124 \static 125 Flushes all global caches for the statuses. 126 */ 127 function flush() 128 { 129 unset( $GLOBALS['eZOrderStatusList'], 130 $GLOBALS['eZOrderStatusOList'], 131 $GLOBALS['eZOrderStatusMap'], 132 $GLOBALS['eZOrderStatusUndefined'] ); 133 } 134 135 /*! 136 \return the status object with the given DB ID. 137 */ 138 function fetch( $id, $asObject = true ) 139 { 140 return eZPersistentObject::fetchObject( eZOrderStatus::definition(), 141 null, 142 array( "id" => $id ), 143 $asObject ); 144 } 145 146 /*! 147 \return the status object with the given status ID. 148 \note It is safe to call this with ID 0, instead of fetching the DB 149 data it calls createUndefined() and returns that data. 150 */ 151 function fetchByStatus( $statusID, $asObject = true ) 152 { 153 if ( $statusID == 0 ) 154 return eZOrderStatus::createUndefined(); 155 return eZPersistentObject::fetchObject( eZOrderStatus::definition(), 156 null, 157 array( "status_id" => $statusID ), 158 $asObject ); 159 } 160 161 /*! 162 \static 163 \param $asObject If \c true return them as objects. 164 \param $showInactive If \c true it will include status items that are not active, default is \c false. 165 \return A list of defined orders which maps from the status ID to the object. 166 */ 167 function fetchMap( $asObject = true, $showInactive = false ) 168 { 169 $map =& $GLOBALS['eZOrderStatusMap'][$asObject][$showInactive]; 170 if ( !isset( $map ) ) 171 { 172 $conds = array(); 173 if ( !$showInactive ) 174 $conds['is_active'] = 1; 175 $list = eZPersistentObject::fetchObjectList( eZOrderStatus::definition(), 176 null, 177 $conds, 178 null, 179 null, 180 $asObject ); 181 $map = array(); 182 if ( $asObject ) 183 { 184 // Here we access member variables directly since it is of the same class 185 foreach ( $list as $statusItem ) 186 { 187 $map[$statusItem->StatusID] = $statusItem; 188 } 189 } 190 else 191 { 192 foreach ( $list as $statusItem ) 193 { 194 $map[$statusItem['status_id']] = $statusItem; 195 } 196 } 197 } 198 return $map; 199 } 200 201 /*! 202 \param $asObject If \c true return them as objects. 203 \param $showInactive If \c true it will include status items that are not active, default is \c false. 204 \return A list of defined orders sorted by status ID. 205 */ 206 function fetchList( $asObject = true, $showInactive = false ) 207 { 208 $list =& $GLOBALS['eZOrderStatusList'][$asObject][$showInactive]; 209 if ( !isset( $list ) ) 210 { 211 $conds = array(); 212 if ( !$showInactive ) 213 $conds['is_active'] = 1; 214 $list = eZPersistentObject::fetchObjectList( eZOrderStatus::definition(), 215 null, 216 $conds, 217 array( 'status_id' => false ), 218 null, 219 $asObject ); 220 } 221 return $list; 222 } 223 224 /*! 225 \param $asObject If \c true return them as objects. 226 \param $showInactive If \c true it will include status items that are not active, default is \c false. 227 \return A list of defined orders sorted by status ID. 228 */ 229 function fetchPolicyList( $showInactive = false ) 230 { 231 $db =& eZDB::instance(); 232 233 $conditionText = ''; 234 if ( !$showInactive ) 235 $conditionText = ' WHERE is_active = 1'; 236 237 $rows = $db->arrayQuery( "SELECT status_id AS id, name FROM ezorder_status$conditionText" ); 238 return $rows; 239 } 240 241 /*! 242 \param $asObject If \c true return them as objects. 243 \param $showInactive If \c true it will include status items that are not active, default is \c false. 244 \return A list of defined orders sorted by name. 245 */ 246 function fetchOrderedList( $asObject = true, $showInactive = false ) 247 { 248 $list =& $GLOBALS['eZOrderStatusOList'][$asObject][$showInactive]; 249 if ( !isset( $list ) ) 250 { 251 $conds = array(); 252 if ( !$showInactive ) 253 $conds['is_active'] = 1; 254 $list = eZPersistentObject::fetchObjectList( eZOrderStatus::definition(), 255 null, 256 $conds, 257 array( 'name' => false ), 258 null, 259 $asObject ); 260 } 261 return $list; 262 } 263 264 /*! 265 \return the number of active order statuses 266 */ 267 function orderStatusCount( $showInactive = false ) 268 { 269 $db =& eZDB::instance(); 270 271 $condText = ''; 272 if ( !$showInactive ) 273 $condText = " WHERE is_active = 1"; 274 $countArray = $db->arrayQuery( "SELECT count( * ) AS count FROM ezorder_status$condText" ); 275 return $countArray[0]['count']; 276 } 277 278 279 /*! 280 Will remove the current status from the database identifed by its DB ID. 281 \note transaction safe 282 */ 283 function remove() 284 { 285 $db =& eZDB::instance(); 286 $db->begin(); 287 288 // Set all elements using this status to 0 (undefined). 289 $statusID = (int)$this->StatusID; 290 $db->query( "UPDATE ezorder SET status_id = 0 WHERE status_id = $statusID" ); 291 $db->query( "UPDATE ezorder_status_history SET status_id = 0 WHERE status_id = $statusID" ); 292 293 $id = $this->ID; 294 eZPersistentObject::removeObject( eZOrderStatus::definition(), array( "id" => $id ) ); 295 296 $db->commit(); 297 298 eZOrderStatus::flush(); 299 } 300 301 /*! 302 \static 303 Creates a new order status and returns it. 304 */ 305 function create() 306 { 307 $row = array( 308 'id' => null, 309 'is_active' => true, 310 'name' => ezi18n( 'kernel/shop', 'Order status' ) ); 311 return new eZOrderStatus( $row ); 312 } 313 314 /*! 315 \static 316 Creates an order status which contains 'Undefined' as name and 0 as status ID. 317 This can be used whenever code expects a status object to work with. 318 \return The newly created status object. 319 */ 320 function createUndefined() 321 { 322 $obj =& $GLOBALS['eZOrderStatusUndefined']; 323 if ( !isset( $obj ) ) 324 { 325 $row = array( 326 'id' => null, 327 'status_id' => EZ_ORDER_STATUS_UNDEFINED, 328 'is_active' => true, 329 'name' => ezi18n( 'kernel/shop', 'Undefined' ) ); 330 $obj = new eZOrderStatus( $row ); 331 } 332 return $obj; 333 } 334 335 /*! 336 Stores a new custom order status. 337 If there is no status ID yet it will acquire a new unique and store it 338 with that. 339 If it already has an ID it calls store() as normally. 340 */ 341 function storeCustom() 342 { 343 if ( $this->StatusID ) 344 { 345 eZOrderStatus::flush(); 346 $this->store(); 347 } 348 else 349 { 350 // Lock the table while we find the highest number 351 $db =& eZDB::instance(); 352 $db->lock( 'ezorder_status' ); 353 354 $rows = $db->arrayQuery( "SELECT max( status_id ) as status_id FROM ezorder_status" ); 355 $statusID = $rows[0]['status_id']; 356 357 // If the max ID is below the custom one we set as the first 358 // custom ID, if not we increase it by one. 359 if ( $statusID < EZ_ORDER_STATUS_CUSTOM ) 360 { 361 $statusID = EZ_ORDER_STATUS_CUSTOM; 362 } 363 else 364 { 365 ++$statusID; 366 } 367 368 $this->StatusID = $statusID; 369 $this->store(); 370 371 $db->unlock(); 372 373 eZOrderStatus::flush(); 374 } 375 } 376 } 377 378 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sat Feb 24 10:30:04 2007 | par Balluche grâce à PHPXref 0.7 |