eZ Publish  [4.0]
ezcontentfunctions.php
Go to the documentation of this file.
00001 <?php
00002 
00003 //
00004 // Created on: <13-Nov-2006 15:00:00 dl>
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 class eZContentFunctions
00031 {
00032     static function createAndPublishObject( $params )
00033     {
00034         //include_once( 'kernel/classes/ezcontentobject.php' );
00035         //include_once( 'kernel/classes/ezcontentobjecttreenode.php' );
00036         //include_once( 'lib/ezlocale/classes/ezdatetime.php' );
00037 
00038         $parentNodeID = $params['parent_node_id'];
00039         $classIdentifier = $params['class_identifier'];
00040         $creatorID = isset( $params['creator_id'] ) ? $params['creator_id'] : false;
00041         $attributesData = isset( $params['attributes'] ) ? $params['attributes'] : false;
00042         $storageDir = isset( $params['storage_dir'] ) ? $params['storage_dir'] : '';
00043 
00044         $contentObject = false;
00045 
00046         $parentNode = eZContentObjectTreeNode::fetch( $parentNodeID, false, false );
00047 
00048         if ( is_array( $parentNode ) )
00049         {
00050             $contentClass = eZContentClass::fetchByIdentifier( $classIdentifier );
00051             if ( is_object( $contentClass ) )
00052             {
00053                 $db = eZDB::instance();
00054                 $db->begin();
00055 
00056                 $contentObject = $contentClass->instantiate( $creatorID );
00057                 $contentObject->store();
00058 
00059                 $nodeAssignment = eZNodeAssignment::create( array( 'contentobject_id' => $contentObject->attribute( 'id' ),
00060                                                                    'contentobject_version' => $contentObject->attribute( 'current_version' ),
00061                                                                    'parent_node' => $parentNodeID,
00062                                                                    'is_main' => 1,
00063                                                                    'sort_field' => $contentClass->attribute( 'sort_field' ),
00064                                                                    'sort_order' => $contentClass->attribute( 'sort_order' ) ) );
00065                 $nodeAssignment->store();
00066 
00067                 $version = $contentObject->version( 1 );
00068                 $version->setAttribute( 'modified', eZDateTime::currentTimeStamp() );
00069                 $version->setAttribute( 'status', eZContentObjectVersion::STATUS_DRAFT );
00070                 $version->store();
00071 
00072                 if ( is_array( $attributesData ) && count( $attributesData ) > 0 )
00073                 {
00074                     $attributes = $contentObject->attribute( 'contentobject_attributes' );
00075 
00076                     foreach( $attributes as $attribute )
00077                     {
00078                         $attributeIdentifier = $attribute->attribute( 'contentclass_attribute_identifier' );
00079                         if ( isset( $attributesData[$attributeIdentifier] ) )
00080                         {
00081                             $dataString = $attributesData[$attributeIdentifier];
00082                             switch ( $datatypeString = $attribute->attribute( 'data_type_string' ) )
00083                             {
00084                                 case 'ezimage':
00085                                 case 'ezbinaryfile':
00086                                 case 'ezmedia':
00087                                 {
00088                                     $dataString = $storageDir . $dataString;
00089                                     break;
00090                                 }
00091                                 default:
00092                             }
00093 
00094                             $attribute->fromString( $dataString );
00095                             $attribute->store();
00096                         }
00097                     }
00098                 }
00099 
00100                 $db->commit();
00101 
00102                 //include_once( 'lib/ezutils/classes/ezoperationhandler.php' );
00103                 $operationResult = eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $contentObject->attribute( 'id' ),
00104                                                                                              'version' => 1 ) );
00105             }
00106             else
00107             {
00108                 eZDebug::writeError( "Content class with identifier '$classIdentifier' doesn't exist.", 'eZContentFunctions::createAndPublishObject' );
00109             }
00110         }
00111         else
00112         {
00113             eZDebug::writeError( "Node with id '$parentNodeID' doesn't exist.", 'eZContentFunctions::createAndPublishObject' );
00114         }
00115 
00116         return $contentObject;
00117     }
00118 }
00119 
00120 ?>