eZ Publish  [4.0]
ezpdfexport.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZPDFExport class
00004 //
00005 // Created on: <21-Nov-2003 15:59:56 kk>
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 ezpdfexport.php
00032 */
00033 
00034 /*!
00035   \class eZPDFExport ezpdfexport.php
00036   \brief class for storing PDF exports
00037 
00038   eZPDFExport is used to create PDF exports from published content. See kernel/pdf for more files.
00039 */
00040 
00041 //include_once( 'kernel/classes/ezpersistentobject.php' );
00042 
00043 class eZPDFExport extends eZPersistentObject
00044 {
00045     const VERSION_VALID = 0;
00046     const VERSION_DRAFT = 1;
00047 
00048     const CREATE_ONCE = 1;
00049     const CREATE_ONFLY = 2;
00050 
00051     /*!
00052      Initializes a new eZPDFExport.
00053     */
00054     function eZPDFExport( $row )
00055     {
00056         $this->eZPersistentObject( $row );
00057     }
00058 
00059     /*!
00060      \reimp
00061     */
00062     static function definition()
00063     {
00064         return array( 'fields' => array( 'id' => array( 'name' => 'ID',
00065                                                         'datatype' => 'integer',
00066                                                         'default' => 0,
00067                                                         'required' => true ),
00068                                          'title' => array( 'name' => 'Title',
00069                                                            'datatype' => 'string',
00070                                                            'default' => ezi18n( 'kernel/pdfexport', 'New PDF Export' ),
00071                                                            'required' => true ),
00072                                          'show_frontpage' => array( 'name' => 'DisplayFrontpage',
00073                                                                        'datatype' => 'integer',
00074                                                                        'default' => 1,
00075                                                                        'required' => true ),
00076                                          'intro_text' => array( 'name' => 'IntroText',
00077                                                                 'datatype' => 'text',
00078                                                                 'default' => '',
00079                                                                 'required' => false ),
00080                                          'sub_text' => array( 'name' => 'SubText',
00081                                                               'datatype' => 'text',
00082                                                               'default' => '',
00083                                                               'required' => false ),
00084                                          'source_node_id' => array( 'name' => 'SourceNodeID',
00085                                                                     'datatype' => 'int',
00086                                                                     'default' => '',
00087                                                                     'required' => true,
00088                                                                     'foreign_class' => 'eZContentObjectTreeNode',
00089                                                                     'foreign_attribute' => 'node_id',
00090                                                                     'multiplicity' => '1..*' ),
00091                                          'site_access' => array( 'name' => 'SiteAccess',
00092                                                                  'datatype' => 'string',
00093                                                                  'default' => '',
00094                                                                  'required' => true ),
00095                                          'modified' => array( 'name' => 'Modified',
00096                                                               'datatype' => 'integer',
00097                                                               'default' => 0,
00098                                                               'required' => true ),
00099                                          'modifier_id' => array( 'name' => 'ModifierID',
00100                                                                  'datatype' => 'integer',
00101                                                                  'default' => 0,
00102                                                                  'required' => true,
00103                                                                  'foreign_class' => 'eZUser',
00104                                                                  'foreign_attribute' => 'contentobject_id',
00105                                                                  'multiplicity' => '1..*' ),
00106                                          'created' => array( 'name' => 'Created',
00107                                                              'datatype' => 'integer',
00108                                                              'default' => 0,
00109                                                              'required' => true ),
00110                                          'creator_id' => array( 'name' => 'CreatorID',
00111                                                                 'datatype' => 'integer',
00112                                                                 'default' => 0,
00113                                                                 'required' => true,
00114                                                                 'foreign_class' => 'eZUser',
00115                                                                 'foreign_attribute' => 'contentobject_id',
00116                                                                 'multiplicity' => '1..*' ),
00117                                          'export_structure' => array( 'name' => 'ExportStructure',
00118                                                                       'datatype' => 'string',
00119                                                                       'default' => 'tree',
00120                                                                       'required' => false ),
00121                                          'export_classes' => array( 'name' => 'ExportClasses',
00122                                                                     'datatype' => 'string',
00123                                                                     'default' => 0,
00124                                                                     'required' => false ),
00125                                          'pdf_filename' => array( 'name' => 'PDFFileName',
00126                                                                    'datatype' => 'string',
00127                                                                    'default' => 'file.pdf',
00128                                                                    'required' => true ),
00129                                          'status' => array( 'name' => 'Status',
00130                                                             'datatype' => 'integer',
00131                                                             'default' => eZPDFExport::CREATE_ONCE,
00132                                                             'required' => true ),
00133                                          'version' => array( 'name' => 'Version',
00134                                                              'datatype' => 'integer',
00135                                                              'default' => 0,
00136                                                              'required' => true ) ),
00137                       'keys' => array( 'id', 'version' ),
00138                       'function_attributes' => array ( 'modifier' => 'modifier',
00139                                                        'source_node' => 'sourceNode',
00140                                                        'filepath' => 'filepath',
00141                                                        'export_classes_array' => 'exportClassesArray' ),
00142                       'increment_key' => 'id',
00143                       'sort' => array( 'title' => 'asc' ),
00144                       'class_name' => 'eZPDFExport',
00145                       'name' => 'ezpdf_export' );
00146     }
00147 
00148     /*!
00149      \static
00150      Creates a new PDF Export
00151      \param User ID
00152     */
00153     static function create( $user_id )
00154     {
00155         $config = eZINI::instance( 'site.ini' );
00156         $dateTime = time();
00157         $row = array( 'id' => null,
00158                       'title' => ezi18n( 'kernel/pdfexport', 'New PDF Export' ),
00159                       'show_frontpage' => 1,
00160                       'intro_text' => '',
00161                       'sub_text' => '',
00162                       'source_node_id' => 0,
00163                       'export_structure' => 'tree',
00164                       'export_classes' => '',
00165                       'site_access' => '',
00166                       'pdf_filename' => 'file.pdf',
00167                       'modifier_id' => $user_id,
00168                       'modified' => $dateTime,
00169                       'creator_id' => $user_id,
00170                       'created' => $dateTime,
00171                       'status' => 0,
00172                       'version' => 1 );
00173         return new eZPDFExport( $row );
00174     }
00175 
00176     /*!
00177      Store Object to database
00178      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
00179      the calls within a db transaction; thus within db->begin and db->commit.
00180     */
00181     function store( $publish = false )
00182     {
00183         //include_once( 'kernel/classes/datatypes/ezuser/ezuser.php' );
00184 
00185         if ( $publish )
00186         {
00187             $originalVersion = $this->attribute( 'version' );
00188             $this->setAttribute( 'version', eZPDFExport::VERSION_VALID );
00189         }
00190         $user = eZUser::currentUser();
00191         $this->setAttribute( 'modified', time() );
00192         $this->setAttribute( 'modifier_id', $user->attribute( 'contentobject_id' ) );
00193 
00194         $db = eZDB::instance();
00195         $db->begin();
00196         eZPersistentObject::store();
00197         if ( $publish )
00198         {
00199             $this->setAttribute( 'version', eZPDFExport::VERSION_DRAFT );
00200             $this->remove();
00201             $this->setAttribute( 'version', $originalVersion );
00202         }
00203         $db->commit();
00204     }
00205 
00206     /*!
00207      \static
00208       Fetches the PDF Export by ID.
00209 
00210      \param PDF Export ID
00211     */
00212     static function fetch( $id, $asObject = true, $version = eZPDFExport::VERSION_VALID )
00213     {
00214         return eZPersistentObject::fetchObject( eZPDFExport::definition(),
00215                                                 null,
00216                                                 array( 'id' => $id,
00217                                                        'version' => $version ),
00218                                                 $asObject );
00219     }
00220 
00221     /*!
00222      \reimp
00223      \transaction unsafe.
00224     */
00225     function remove( $conditions = null, $extraConditions = null )
00226     {
00227         if ( $this->attribute( 'version' ) == eZPDFExport::VERSION_VALID &&
00228              $this->attribute( 'status' ) != eZPDFExport::CREATE_ONFLY )
00229         {
00230             $sys = eZSys::instance();
00231             $storage_dir = $sys->storageDirectory();
00232 
00233             $filename = $storage_dir . '/pdf/' . $this->attribute( 'pdf_filename' );
00234             if ( file_exists( $filename ) )
00235             {
00236                 unlink( $filename );
00237             }
00238         }
00239         eZPersistentObject::remove( $conditions, $extraConditions);
00240     }
00241 
00242     /*!
00243      \static
00244       Fetches complete list of PDF Exports.
00245     */
00246     static function fetchList( $asObject = true )
00247     {
00248         return eZPersistentObject::fetchObjectList( eZPDFExport::definition(),
00249                                                     null,
00250                                                     array( 'version' => eZPDFExport::VERSION_VALID ),
00251                                                     null,
00252                                                     null,
00253                                                     $asObject );
00254     }
00255 
00256     function modifier()
00257     {
00258         if ( isset( $this->ModifierID ) and $this->ModifierID )
00259         {
00260             //include_once( 'kernel/classes/datatypes/ezuser/ezuser.php' );
00261             return eZUser::fetch( $this->ModifierID );
00262         }
00263 
00264         return null;
00265     }
00266 
00267     function sourceNode()
00268     {
00269         if ( isset( $this->SourceNodeID ) and $this->SourceNodeID )
00270         {
00271             //include_once( 'kernel/classes/ezcontentobjecttreenode.php' );
00272             return eZContentObjectTreeNode::fetch( $this->SourceNodeID );
00273         }
00274 
00275         return null;
00276     }
00277 
00278     function filepath()
00279     {
00280         $sys = eZSys::instance();
00281         $storage_dir = $sys->storageDirectory();
00282         return $storage_dir . '/pdf/' . $this->attribute( 'pdf_filename' );
00283     }
00284 
00285     function exportClassesArray()
00286     {
00287         return explode( ':',  eZPersistentObject::attribute( 'export_classes' ) );
00288     }
00289 
00290     function countGeneratingOnceExports( $filename = '' )
00291     {
00292         $conditions = array( 'version' => eZPDFExport::VERSION_VALID,
00293                              'status' =>  eZPDFExport::CREATE_ONCE,
00294                              'pdf_filename' => $filename );
00295 
00296         if ( $filename === '' && isset( $this ) )
00297         {
00298             $conditions['pdf_filename'] = $this->attribute( 'pdf_filename' );
00299             $conditions['id'] = array( '<>', $this->attribute( 'id' ) );
00300         }
00301 
00302         $queryResult = eZPersistentObject::fetchObjectList( eZPDFExport::definition(),
00303                                                             array(),
00304                                                             $conditions,
00305                                                             false,
00306                                                             null,
00307                                                             false,
00308                                                             null,
00309                                                             array( array( 'operation' => 'count( * )',
00310                                                                           'name' => 'count' ) ) );
00311         if ( isset( $queryResult[0]['count'] ) )
00312         {
00313             return ( int ) $queryResult[0]['count'];
00314         }
00315         return 0;
00316 
00317     }
00318 
00319 }
00320 
00321 ?>