eZ Publish  [4.0]
ezcontentbrowserecent.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZContentBrowseRecent class
00004 //
00005 // Created on: <30-Apr-2003 13:04:11 sp>
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 ezcontentbrowserecent.php
00032 */
00033 
00034 /*!
00035   \class eZContentBrowseRecent ezcontentbrowserecent.php
00036   \brief Handles recent nodes for users
00037 
00038   Allows the creation and fetching of recent lists for users.
00039   The recent list is used in the browse page to allow quick navigation and selection.
00040 
00041   Creating a new recent item is done with
00042 \code
00043 $userID = eZUser::currentUserID();
00044 $nodeID = 2;
00045 $nodeName = 'Node';
00046 eZContentBrowseRecent::createNew( $userID, $nodeID, $nodeName )
00047 \endcode
00048 
00049   Fetching the list is done with
00050 \code
00051 $userID = eZUser::currentUserID();
00052 eZContentBrowseRecent::fetchListForUser( $userID )
00053 \endcode
00054 
00055 */
00056 
00057 //include_once( "lib/ezdb/classes/ezdb.php" );
00058 require_once( "lib/ezutils/classes/ezdebug.php" );
00059 //include_once( "kernel/classes/ezpersistentobject.php" );
00060 
00061 class eZContentBrowseRecent extends eZPersistentObject
00062 {
00063     /*!
00064      \reimp
00065     */
00066     function eZContentBrowseRecent( $row )
00067     {
00068         $this->eZPersistentObject( $row );
00069     }
00070 
00071     /*!
00072      \reimp
00073     */
00074     static function definition()
00075     {
00076         return array( "fields" => array( "id" => array( 'name' => 'ID',
00077                                                         'datatype' => 'integer',
00078                                                         'default' => 0,
00079                                                         'required' => true ),
00080                                          "user_id" => array( 'name' => 'UserID',
00081                                                              'datatype' => 'integer',
00082                                                              'default' => 0,
00083                                                              'required' => true,
00084                                                              'foreign_class' => 'eZUser',
00085                                                              'foreign_attribute' => 'contentobject_id',
00086                                                              'multiplicity' => '1..*' ),
00087                                          "node_id" => array( 'name' => "NodeID",
00088                                                              'datatype' => 'integer',
00089                                                              'default' => 0,
00090                                                              'required' => true,
00091                                                              'foreign_class' => 'eZContentObjectTreeNode',
00092                                                              'foreign_attribute' => 'node_id',
00093                                                              'multiplicity' => '1..*' ),
00094                                          "created" => array( 'name' => 'Created',
00095                                                              'datatype' => 'integer',
00096                                                              'default' => 0,
00097                                                              'required' => true ),
00098                                          "name" => array( 'name' => "Name",
00099                                                           'datatype' => 'string',
00100                                                           'default' => '',
00101                                                           'required' => true ) ),
00102                       "keys" => array( "id" ),
00103                       "function_attributes" => array( 'node' => 'fetchNode',
00104                                                       'contentobject_id' => 'contentObjectID' ),
00105                       "increment_key" => "id",
00106                       "sort" => array( "id" => "asc" ),
00107                       "class_name" => "eZContentBrowseRecent",
00108                       "name" => "ezcontentbrowserecent" );
00109 
00110     }
00111 
00112     /*!
00113      \static
00114      \return the recent item \a $recentID
00115     */
00116     static function fetch( $recentID )
00117     {
00118         return eZPersistentObject::fetchObject( eZContentBrowseRecent::definition(),
00119                                                 null, array( 'id' => $recentID ), true );
00120     }
00121 
00122     /*!
00123      \static
00124      \return the recent list for the user identifier by \a $userID.
00125     */
00126     static function fetchListForUser( $userID )
00127     {
00128         return eZPersistentObject::fetchObjectList( eZContentBrowseRecent::definition(),
00129                                                     null, array( 'user_id' => $userID ),
00130                                                     array( 'created' => 'desc' ), null, true );
00131     }
00132 
00133     /*!
00134      \static
00135      \return the maximum number of recent items for user \a $userID.
00136      The default value is read from MaximumRecentItems from group BrowseSettings in browse.ini.
00137      \note Currently all users get the same default maximum amount
00138     */
00139     static function maximumRecentItems( $userID )
00140     {
00141         //include_once( 'lib/ezutils/classes/ezini.php' );
00142         $ini = eZINI::instance( 'browse.ini' );
00143         $maximum = $ini->variable( 'BrowseSettings', 'MaximumRecentItems' );
00144         return $maximum;
00145     }
00146 
00147     /*!
00148      \static
00149      Tries to create a new recent item and returns it.
00150      If the node ID \a $nodeID already exists as a recent item nothing is done and the old item is returned.
00151 
00152      It will also remove items when the maximum number of items for the user \a $userID is exceeded.
00153      \sa maximumRecentItems
00154      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
00155      the calls within a db transaction; thus within db->begin and db->commit.
00156     */
00157     static function createNew( $userID, $nodeID, $nodeName )
00158     {
00159         $recentCountList = eZPersistentObject::fetchObjectList( eZContentBrowseRecent::definition(),
00160                                                                 array(),
00161                                                                 array( 'user_id' => $userID ),
00162                                                                 false,
00163                                                                 null,
00164                                                                 false,
00165                                                                 false,
00166                                                                 array( array( 'operation' => 'count( * )',
00167                                                                               'name' => 'count' ) ) );
00168         $matchingRecentList = eZPersistentObject::fetchObjectList( eZContentBrowseRecent::definition(),
00169                                                                    null,
00170                                                                    array( 'user_id' => $userID,
00171                                                                           'node_id' => $nodeID ),
00172                                                                    null,
00173                                                                    null,
00174                                                                    true );
00175         // If we already have the node in the list just return
00176         if ( count( $matchingRecentList ) > 0 )
00177         {
00178             $oldItem = $matchingRecentList[0];
00179             $oldItem->setAttribute( 'created', time() );
00180             $oldItem->store();
00181             return $oldItem;
00182         }
00183         $recentCount = 0;
00184         if ( isset( $recentCountList[0] ) and count( $recentCountList[0] ) > 0 )
00185             $recentCount = $recentCountList[0]['count'];
00186         $maximumCount = eZContentBrowseRecent::maximumRecentItems( $userID );
00187         // Remove oldest item
00188 
00189         $db = eZDB::instance();
00190         $db->begin();
00191         if ( $recentCount > $maximumCount )
00192         {
00193             $recentCountList = eZPersistentObject::fetchObjectList( eZContentBrowseRecent::definition(),
00194                                                                     null,
00195                                                                     array( 'user_id' => $userID ),
00196                                                                     array( 'created' => 'asc' ),
00197                                                                     array( 'length' => ( $recentCount - $maximumCount ), 'offset' => 0 ),
00198                                                                     true );
00199             foreach($recentCountList as $countList)
00200             {
00201                  $eldest = $countList;
00202                  $eldest->remove();
00203             }
00204 
00205         }
00206 
00207         $recent = new eZContentBrowseRecent( array( 'user_id' => $userID,
00208                                                     'node_id' => $nodeID,
00209                                                     'name' => $nodeName,
00210                                                     'created' => time() ) );
00211         $recent->store();
00212         $db->commit();
00213         return $recent;
00214     }
00215 
00216     /*!
00217      \return the tree node which this item refers to.
00218     */
00219     function fetchNode()
00220     {
00221         return eZContentObjectTreeNode::fetch( $this->attribute( 'node_id' ) );
00222     }
00223 
00224     /*!
00225      \return the content object ID of the tree node which this item refers to.
00226     */
00227     function contentObjectID()
00228     {
00229         $node = $this->fetchNode();
00230         if ( $node )
00231         {
00232             return $node->attribute( 'contentobject_id' );
00233         }
00234 
00235         return false;
00236     }
00237 
00238     /*!
00239      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
00240      the calls within a db transaction; thus within db->begin and db->commit.
00241      */
00242     static function removeRecentByNodeID( $nodeID )
00243     {
00244         $db = eZDB::instance();
00245         $nodeID =(int) $nodeID;
00246         $db->query( "DELETE FROM ezcontentbrowserecent WHERE node_id=$nodeID" );
00247     }
00248 
00249     /*!
00250      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
00251      the calls within a db transaction; thus within db->begin and db->commit.
00252      */
00253     static function updateNodeID( $oldNodeID, $newNodeID )
00254     {
00255         $db = eZDB::instance();
00256         $oldNodeID =(int) $oldNodeID;
00257         $newNodeID =(int) $newNodeID;
00258         $db->query( "UPDATE ezcontentbrowserecent SET node_id=$newNodeID WHERE node_id=$oldNodeID" );
00259     }
00260 
00261     /*!
00262      \static
00263      Removes all recent entries for all users.
00264      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
00265      the calls within a db transaction; thus within db->begin and db->commit.
00266     */
00267     static function cleanup()
00268     {
00269         $db = eZDB::instance();
00270         $db->query( "DELETE FROM ezcontentbrowserecent" );
00271     }
00272 }
00273 
00274 ?>