eZ Publish  [4.0]
eznotificationcollection.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZNotificationCollection class
00004 //
00005 // Created on: <09-May-2003 16:07:24 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 eznotificationcollection.php
00032 */
00033 
00034 /*!
00035   \class eZNotificationCollection eznotificationcollection.php
00036   \brief The class eZNotificationCollection does
00037 
00038 */
00039 //include_once( 'kernel/classes/notification/eznotificationcollectionitem.php' );
00040 
00041 class eZNotificationCollection extends eZPersistentObject
00042 {
00043     /*!
00044      Constructor
00045     */
00046     function eZNotificationCollection( $row = array() )
00047     {
00048         $this->eZPersistentObject( $row );
00049     }
00050 
00051     static function definition()
00052     {
00053         return array( "fields" => array( "id" => array( 'name' => 'ID',
00054                                                         'datatype' => 'integer',
00055                                                         'default' => 0,
00056                                                         'required' => true ),
00057                                          "event_id" => array( 'name' => "EventID",
00058                                                               'datatype' => 'integer',
00059                                                               'default' => 0,
00060                                                               'required' => true,
00061                                                               'foreign_class' => 'eZNotificationEvent',
00062                                                               'foreign_attribute' => 'id',
00063                                                               'multiplicity' => '1..*' ),
00064                                          "handler" => array( 'name' => "Handler",
00065                                                              'datatype' => 'string',
00066                                                              'default' => '',
00067                                                              'required' => true ),
00068                                          "transport" => array( 'name' => "Transport",
00069                                                                'datatype' => 'string',
00070                                                                'default' => '',
00071                                                                'required' => true ),
00072                                          "data_subject" => array( 'name' => "DataText1",
00073                                                                 'datatype' => 'text',
00074                                                                 'default' => '',
00075                                                                 'required' => true ),
00076                                          "data_text" => array( 'name' => "DataText2",
00077                                                                 'datatype' => 'text',
00078                                                                 'default' => '',
00079                                                                 'required' => true ) ),
00080                       "keys" => array( "id" ),
00081                       "function_attributes" => array( 'items' => 'items',
00082                                                       'items_to_send' => 'itemsToSend',
00083                                                       'item_count' => 'itemCount' ),
00084                       "increment_key" => "id",
00085                       "sort" => array( "id" => "asc" ),
00086                       "class_name" => "eZNotificationCollection",
00087                       "name" => "eznotificationcollection" );
00088     }
00089 
00090 
00091     static function create( $eventID, $handler, $transport )
00092     {
00093         return new eZNotificationCollection( array( 'event_id' => $eventID,
00094                                                     'handler' => $handler,
00095                                                     'transport' => $transport ) );
00096     }
00097 
00098     function addItem( $address, $sendDate = 0 )
00099     {
00100         $item = eZNotificationCollectionItem::create( $this->attribute( 'id' ), $this->attribute( 'event_id' ), $address, $sendDate = 0  );
00101         $item->store();
00102         return $item;
00103     }
00104 
00105     function items()
00106     {
00107         return eZPersistentObject::fetchObjectList( eZNotificationCollectionItem::definition(),
00108                                                     null, array( 'collection_id' => $this->attribute( 'id' ) ), null,null,
00109                                                     true );
00110     }
00111 
00112     function itemCount()
00113     {
00114         $result = eZPersistentObject::fetchObjectList( eZNotificationCollectionItem::definition(),
00115                                                        array(),
00116                                                        array( 'collection_id' => $this->attribute( 'id' ) ),
00117                                                        false,
00118                                                        null,
00119                                                        false,
00120                                                        false,
00121                                                        array( array( 'operation' => 'count( * )',
00122                                                                      'name' => 'count' ) ) );
00123         return $result[0]['count'];
00124     }
00125 
00126     function itemsToSend()
00127     {
00128         return eZPersistentObject::fetchObjectList( eZNotificationCollectionItem::definition(),
00129                                                     null, array( 'collection_id' => $this->attribute( 'id' ),
00130                                                                  'send_date' => 0 ),
00131                                                     null, null, true );
00132     }
00133 
00134     static function fetchForHandler( $handler, $eventID, $transport )
00135     {
00136         return eZPersistentObject::fetchObject( eZNotificationCollection::definition(), null,
00137                                                 array( 'event_id' => $eventID,
00138                                                        'handler'=> $handler,
00139                                                        'transport' => $transport ) );
00140     }
00141 
00142     static function fetchListForHandler( $handler, $eventID, $transport )
00143     {
00144         return eZPersistentObject::fetchObjectList( eZNotificationCollection::definition(), null,
00145                                                     array( 'event_id' => $eventID,
00146                                                            'handler'=> $handler,
00147                                                            'transport' => $transport ) );
00148     }
00149 
00150     /*!
00151      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
00152      the calls within a db transaction; thus within db->begin and db->commit.
00153      */
00154     static function removeEmpty()
00155     {
00156         $db = eZDB::instance();
00157         if ( $db->databaseName() == 'oracle' ) // fix for compatibility with Oracle versions prior to 9
00158             $query = 'SELECT eznotificationcollection.id FROM eznotificationcollection, eznotificationcollection_item
00159                       WHERE  eznotificationcollection.id = eznotificationcollection_item.collection_id(+) AND
00160                              eznotificationcollection_item.collection_id IS NULL';
00161         else
00162             $query = 'SELECT eznotificationcollection.id FROM eznotificationcollection
00163                       LEFT JOIN eznotificationcollection_item ON eznotificationcollection.id=eznotificationcollection_item.collection_id
00164                       WHERE eznotificationcollection_item.collection_id IS NULL';
00165 
00166         $idArray = $db->arrayQuery( $query );
00167 
00168         $db->begin();
00169         foreach ( $idArray as $id )
00170         {
00171             eZPersistentObject::removeObject( eZNotificationCollection::definition(), array( 'id' => $id['id'] ) );
00172         }
00173         $db->commit();
00174     }
00175 
00176     /*!
00177      \static
00178      Removes all notification collections.
00179      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
00180      the calls within a db transaction; thus within db->begin and db->commit.
00181     */
00182     static function cleanup()
00183     {
00184         $db = eZDB::instance();
00185         $db->begin();
00186         eZNotificationCollectionItem::cleanup();
00187         $db->query( "DELETE FROM eznotificationcollection" );
00188         $db->commit();
00189     }
00190 }
00191 
00192 ?>