eZ Publish  [4.0]
ezviewcounter.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZViewCounter class
00004 //
00005 //
00006 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00007 // SOFTWARE NAME: eZ Publish
00008 // SOFTWARE RELEASE: 4.0.x
00009 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00010 // SOFTWARE LICENSE: GNU General Public License v2.0
00011 // NOTICE: >
00012 //   This program is free software; you can redistribute it and/or
00013 //   modify it under the terms of version 2.0  of the GNU General
00014 //   Public License as published by the Free Software Foundation.
00015 //
00016 //   This program is distributed in the hope that it will be useful,
00017 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 //   GNU General Public License for more details.
00020 //
00021 //   You should have received a copy of version 2.0 of the GNU General
00022 //   Public License along with this program; if not, write to the Free
00023 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00024 //   MA 02110-1301, USA.
00025 //
00026 //
00027 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00028 //
00029 
00030 //!! eZKernel
00031 //! The class eZViewCounter
00032 /*!
00033 
00034 */
00035 
00036 //include_once( "lib/ezdb/classes/ezdb.php" );
00037 //include_once( "kernel/classes/ezpersistentobject.php" );
00038 //include_once( "kernel/classes/ezcontentclassgroup.php" );
00039 
00040 class eZViewCounter extends eZPersistentObject
00041 {
00042     function eZViewCounter( $row )
00043     {
00044         $this->eZPersistentObject( $row );
00045     }
00046 
00047     static function definition()
00048     {
00049         return array( "fields" => array( "node_id" => array( 'name' => "NodeID",
00050                                                              'datatype' => 'integer',
00051                                                              'default' => 0,
00052                                                              'required' => true,
00053                                                              'foreign_class' => 'eZContentObjectTreeNode',
00054                                                              'foreign_attribute' => 'node_id',
00055                                                              'multiplicity' => '1..*' ),
00056                                          "count" => array( 'name' => "Count",
00057                                                            'datatype' => 'integer',
00058                                                            'default' => 0,
00059                                                            'required' => true ) ),
00060                       "keys" => array( "node_id" ),
00061                       'relations' => array( 'node_id' => array( 'class' => 'eZContentObjectTreeNode',
00062                                                                 'field' => 'node_id' ) ),
00063                       "class_name" => "eZViewCounter",
00064                       "sort" => array( "count" => "desc" ),
00065                       "name" => "ezview_counter" );
00066     }
00067 
00068     static function create( $node_id )
00069     {
00070         $row = array("node_id" => $node_id,
00071                      "count" => 0 );
00072         return new eZViewCounter( $row );
00073     }
00074 
00075     /*!
00076      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
00077      the calls within a db transaction; thus within db->begin and db->commit.
00078      */
00079     public static function removeCounter( $node_id )
00080     {
00081         eZPersistentObject::removeObject( eZViewCounter::definition(),
00082                                           array("node_id" => $node_id ) );
00083     }
00084 
00085     /*!
00086      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
00087      the calls within a db transaction; thus within db->begin and db->commit.
00088      */
00089     static function clear( $node_id )
00090     {
00091         $counter = eZViewCounter::fetch( $node_id );
00092         if ( $counter != null )
00093         {
00094             $counter->setAttribute( 'count', 0 );
00095             $counter->store();
00096         }
00097     }
00098 
00099     /*!
00100      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
00101      the calls within a db transaction; thus within db->begin and db->commit.
00102      */
00103     function increase()
00104     {
00105         $currentCount = $this->attribute( 'count' );
00106         $newCount = $currentCount + 1;
00107         $this->setAttribute( 'count', $newCount );
00108         $this->store();
00109     }
00110 
00111     static function fetch( $node_id, $asObject = true )
00112     {
00113         return eZPersistentObject::fetchObject( eZViewCounter::definition(),
00114                                                 null,
00115                                                 array("node_id" => $node_id ),
00116                                                 $asObject );
00117     }
00118 
00119     static function fetchTopList( $classID = false, $sectionID = false, $offset = false, $limit = false )
00120     {
00121         if ( !$classID && !$sectionID )
00122         {
00123 
00124             return  eZPersistentObject::fetchObjectList( eZViewCounter::definition(),
00125                                                          null,
00126                                                          null,
00127                                                          null,
00128                                                          array( 'length' => $limit, 'offset' => $offset ),
00129                                                          false );
00130         }
00131 
00132         $queryPart = "";
00133         if ( $classID != false )
00134         {
00135             $classID = (int)$classID;
00136             $queryPart .= "ezcontentobject.contentclass_id=$classID AND ";
00137         }
00138 
00139         if ( $sectionID != false )
00140         {
00141             $sectionID = (int)$sectionID;
00142             $queryPart .= "ezcontentobject.section_id=$sectionID AND ";
00143         }
00144 
00145         $db = eZDB::instance();
00146         $query = "SELECT ezview_counter.*
00147                   FROM
00148                          ezcontentobject_tree,
00149                          ezcontentobject,
00150                          ezview_counter
00151                   WHERE
00152                          ezview_counter.node_id=ezcontentobject_tree.node_id AND
00153                          $queryPart
00154                          ezcontentobject_tree.contentobject_id=ezcontentobject.id
00155                   ORDER BY ezview_counter.count DESC";
00156 
00157         if ( !$offset && !$limit )
00158         {
00159             $countListArray = $db->arrayQuery( $query );
00160         }
00161         else
00162         {
00163             $countListArray = $db->arrayQuery( $query, array( "offset" => $offset,
00164                                                                "limit" => $limit ) );
00165         }
00166         return $countListArray;
00167     }
00168 
00169     /// \privatesection
00170     public $NodeID;
00171     public $Count;
00172 }
00173 
00174 ?>