eZ Publish  [4.0]
ezimagefile.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZImageFile class
00004 //
00005 // Created on: <30-Apr-2002 16:47:08 bf>
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 /*!
00032   \class eZImageFile ezimagefile.php
00033   \ingroup eZDatatype
00034   \brief The class eZImageFile handles registered images
00035 
00036 */
00037 
00038 //include_once( 'lib/ezdb/classes/ezdb.php' );
00039 //include_once( 'kernel/classes/ezpersistentobject.php' );
00040 
00041 class eZImageFile extends eZPersistentObject
00042 {
00043     function eZImageFile( $row )
00044     {
00045         $this->eZPersistentObject( $row );
00046     }
00047 
00048     static function definition()
00049     {
00050         return array( 'fields' => array( 'id' => array( 'name' => 'id',
00051                                                         'datatype' => 'integer',
00052                                                         'default' => 0,
00053                                                         'required' => true ),
00054                                          'contentobject_attribute_id' => array( 'name' => 'ContentObjectAttributeID',
00055                                                                                 'datatype' => 'integer',
00056                                                                                 'default' => 0,
00057                                                                                 'required' => true,
00058                                                                                 'foreign_class' => 'eZContentObjectAttribute',
00059                                                                                 'foreign_attribute' => 'id',
00060                                                                                 'multiplicity' => '1..*' ),
00061                                          'filepath' => array( 'name' => 'Filepath',
00062                                                               'datatype' => 'string',
00063                                                               'default' => '',
00064                                                               'required' => true ) ),
00065                       'keys' => array( 'id' ),
00066                       'class_name' => 'eZImageFile',
00067                       'name' => 'ezimagefile' );
00068     }
00069 
00070     static function create( $contentObjectAttributeID, $filepath  )
00071     {
00072         $row = array( "contentobject_attribute_id" => $contentObjectAttributeID,
00073                       "filepath" => $filepath );
00074         return new eZImageFile( $row );
00075     }
00076 
00077     static function fetchForContentObjectAttribute( $contentObjectAttributeID, $asObject = false )
00078     {
00079         $rows = eZPersistentObject::fetchObjectList( eZImageFile::definition(),
00080                                                       null,
00081                                                       array( "contentobject_attribute_id" => $contentObjectAttributeID ),
00082                                                       null,
00083                                                       null,
00084                                                       $asObject );
00085         if ( !$asObject )
00086         {
00087             $files = array();
00088             foreach ( $rows as $row )
00089             {
00090                 $files[] = $row['filepath'];
00091             }
00092             return array_unique( $files );
00093         }
00094         else
00095             return $rows;
00096     }
00097 
00098     /**
00099      * Looks up ezcontentobjectattribute entries matching an image filepath and
00100      * a contentobjectattribute ID
00101      *
00102      * @param string $filePath file path to look up as URL in the XML string
00103      * @param int $contentObjectAttributeID
00104      *
00105      * @return array An array of content object attribute ids and versions of
00106      *               image files where the url is referenced
00107      *
00108      * @todo Rewrite ! A where data_text LIKE '%xxx%' is a resource hog !
00109      **/
00110     static function fetchImageAttributesByFilepath( $filepath, $contentObjectAttributeID )
00111     {
00112         $db = eZDB::instance();
00113         $contentObjectAttributeID = (int) $contentObjectAttributeID;
00114         $query = "SELECT contentobject_id, contentclassattribute_id
00115                   FROM   ezcontentobject_attribute
00116                   WHERE  id = $contentObjectAttributeID
00117                   LIMIT 1";
00118         $rows = $db->arrayQuery( $query );
00119         if ( count( $rows ) != 1 )
00120             return array();
00121 
00122         $contentObjectID = (int)( $rows[0]['contentobject_id'] );
00123         $contentClassAttributeID = (int)( $rows[0]['contentclassattribute_id'] );
00124         $filepath = $db->escapeString( $filepath );
00125         $query = "SELECT id, version
00126                   FROM   ezcontentobject_attribute
00127                   WHERE  contentobject_id = $contentObjectID and
00128                          contentclassattribute_id = $contentClassAttributeID and
00129                          data_text like '%url=\"$filepath\"%'";
00130         $rows = $db->arrayQuery( $query );
00131         return $rows;
00132     }
00133 
00134     static function fetchByFilepath( $contentObjectAttributeID, $filepath, $asObject = true )
00135     {
00136         // Fetch by file path without $contentObjectAttributeID
00137         if ( $contentObjectAttributeID === false )
00138             return eZPersistentObject::fetchObject( eZImageFile::definition(),
00139                                                     null,
00140                                                     array( 'filepath' => $filepath ),
00141                                                     $asObject );
00142 
00143         return eZPersistentObject::fetchObject( eZImageFile::definition(),
00144                                                 null,
00145                                                 array( 'contentobject_attribute_id' => $contentObjectAttributeID,
00146                                                        'filepath' => $filepath ),
00147                                                 $asObject );
00148     }
00149 
00150     static function moveFilepath( $contentObjectAttributeID, $oldFilepath, $newFilepath )
00151     {
00152         $db = eZDB::instance();
00153         $db->begin();
00154 
00155         eZImageFile::removeFilepath( $contentObjectAttributeID, $oldFilepath );
00156         $result = eZImageFile::appendFilepath( $contentObjectAttributeID, $newFilepath );
00157 
00158         $db->commit();
00159         return $result;
00160     }
00161 
00162     static function appendFilepath( $contentObjectAttributeID, $filepath, $ignoreUnique = false )
00163     {
00164         if ( empty( $filepath ) )
00165             return false;
00166 
00167         if ( !$ignoreUnique )
00168         {
00169             // Fetch ezimagefile objects having the $filepath
00170             $imageFiles = eZImageFile::fetchByFilePath( false, $filepath, false );
00171             // Checking If the filePath already exists in ezimagefile table
00172             if ( isset( $imageFiles[ 'contentobject_attribute_id' ] ) )
00173                 return false;
00174         }
00175         $fileObject = eZImageFile::fetchByFilePath( $contentObjectAttributeID, $filepath );
00176         if ( $fileObject )
00177             return false;
00178         $fileObject = eZImageFile::create( $contentObjectAttributeID, $filepath );
00179         $fileObject->store();
00180         return true;
00181     }
00182 
00183     static function removeFilepath( $contentObjectAttributeID, $filepath )
00184     {
00185         if ( empty( $filepath ) )
00186             return false;
00187         $fileObject = eZImageFile::fetchByFilePath( $contentObjectAttributeID, $filepath );
00188         if ( !$fileObject )
00189             return false;
00190         $fileObject->remove();
00191         return true;
00192     }
00193 
00194     static function removeForContentObjectAttribute( $contentObjectAttributeID )
00195     {
00196         eZPersistentObject::removeObject( eZImageFile::definition(), array( 'contentobject_attribute_id' => $contentObjectAttributeID ) );
00197     }
00198 
00199 
00200     /// \privatesection
00201     public $ID;
00202     public $ContentObjectAttributeID;
00203     public $Filepath;
00204 }
00205 
00206 ?>