eZ Publish  [trunk]
ezcontentfunctions.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * File containing the eZContentFunctions class.
00004  *
00005  * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
00006  * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
00007  * @version //autogentag//
00008  * @package kernel
00009  */
00010 
00011 class eZContentFunctions
00012 {
00013     /**
00014      * Creates and publishes a new content object.
00015      *
00016      * This function takes all the variables passes in the $params
00017      * argument and creates a new content object out of it.
00018      *
00019      * Here is an example
00020      * <code>
00021      * <?php
00022      *
00023      * // admin user
00024      * $creatorID    = 14;
00025      *
00026      * // folder content class
00027      * $classIdentifier = 'folder';
00028      *
00029      * // root node
00030      * $parentNodeID = 2;
00031      *
00032      * // have a look at the folder content class' definition ;)
00033      * // basically the array is the following :
00034      * // key : attribute identifier ( not attribute ID !! )
00035      * // value : value for this attribute
00036      * //
00037      * // Please refer to each fromString/toString function to see
00038      * // how to organize your data
00039      *
00040      * $xmlDeclaration = '<?xml version="1.0" encoding="utf-8"?>
00041      *                    <section xmlns:image="http://ez.no/namespaces/ezpublish3/image/"
00042      *                             xmlns:xhtml="http://ez.no/namespaces/ezpublish3/xhtml/"
00043      *                             xmlns:custom="http://ez.no/namespaces/ezpublish3/custom/">';
00044      *
00045      * $attributeList = array( 'name'              => 'A newly created folder object',
00046      *                         'short_name'        => 'A new folder',
00047      *                         'short_description' => $xmlDeclaration .'<paragraph>This is the short description</paragraph></section>',
00048      *                         'description'       => $xmlDeclaration . '<section><section><header>Some header</header><paragraph>Some paragraph
00049      *                                                                   with a <link target="_blank" url_id="1">link</link></paragraph>
00050      *                                                                   </section></section></section>',
00051      *                         'show_children'     => true);
00052      *
00053      * // Creates the data import array
00054      * $params                     = array();
00055      * $params['creator_id']       = $creatorID;
00056      * $params['class_identifier'] = $classIdentifier;
00057      * $params['parent_node_id']   = $parentNodeID;
00058      * $params['attributes']       = $attributeList;
00059      *
00060      * $contentObject = eZContentFunctions::createAndPublishObject( $params );
00061      *
00062      * if( $contentObject )
00063      * {
00064      *     // do anything you want here
00065      * }
00066      *
00067      * ?>
00068      * </code>
00069      *
00070      * @param array $params An array with all the informations to store.
00071      *                      This array must contains a strict list of key/value pairs.
00072      *                      The possible keys are the following :
00073      *                      - 'parent_node_id'   : The parentNodeID for this new object.
00074      *                      - 'class_identifier' : The classIdentifier for this new object.
00075      *                                             using the classID is not possible.
00076      *                      - 'creator_id'       : The eZUser::contentObjectID to use as creator
00077      *                                             of this new eZContentObject, uses current user
00078      *                                             and stores object id in current session if not set
00079      *                      - 'attributes'       : The list of attributes to store, in order to now
00080      *                                             which values you can use for this key, you have to
00081      *                                             read the code of the fromString and toString functions
00082      *                                             of the attribute's datatype you use
00083      *                      - 'storage_dir'      :
00084      *                      - 'remote_id'        : The value for the remoteID  (optional)
00085      *                      - 'section_id'       : The value for the sectionID (optional)
00086      * @return eZContentObject|false An eZContentObject object if success, false otherwise
00087      */
00088     static function createAndPublishObject( $params )
00089     {
00090         $parentNodeID = $params['parent_node_id'];
00091         $classIdentifier = $params['class_identifier'];
00092         $creatorID = isset( $params['creator_id'] ) ? $params['creator_id'] : false;
00093         $attributesData = isset( $params['attributes'] ) ? $params['attributes'] : false;
00094         $storageDir = isset( $params['storage_dir'] ) ? $params['storage_dir'] : '';
00095 
00096         $contentObject = false;
00097 
00098         $parentNode = eZContentObjectTreeNode::fetch( $parentNodeID, false, false );
00099 
00100         if ( is_array( $parentNode ) )
00101         {
00102             $contentClass = eZContentClass::fetchByIdentifier( $classIdentifier );
00103             if ( $contentClass instanceof eZContentClass )
00104             {
00105                 $db = eZDB::instance();
00106                 $db->begin();
00107 
00108                 $contentObject = $contentClass->instantiate( $creatorID );
00109 
00110                 if ( array_key_exists( 'remote_id', $params ) )
00111                     $contentObject->setAttribute( 'remote_id', $params['remote_id'] );
00112 
00113                 if ( array_key_exists( 'section_id', $params ) )
00114                     $contentObject->setAttribute( 'section_id', $params['section_id'] );
00115 
00116                 $contentObject->store();
00117 
00118                 $nodeAssignment = eZNodeAssignment::create( array( 'contentobject_id' => $contentObject->attribute( 'id' ),
00119                                                                    'contentobject_version' => $contentObject->attribute( 'current_version' ),
00120                                                                    'parent_node' => $parentNodeID,
00121                                                                    'is_main' => 1,
00122                                                                    'sort_field' => $contentClass->attribute( 'sort_field' ),
00123                                                                    'sort_order' => $contentClass->attribute( 'sort_order' ) ) );
00124                 $nodeAssignment->store();
00125 
00126                 $version = $contentObject->version( 1 );
00127                 $version->setAttribute( 'modified', eZDateTime::currentTimeStamp() );
00128                 $version->setAttribute( 'status', eZContentObjectVersion::STATUS_DRAFT );
00129                 $version->store();
00130 
00131                 if ( is_array( $attributesData ) && !empty( $attributesData ) )
00132                 {
00133                     $attributes = $contentObject->attribute( 'contentobject_attributes' );
00134 
00135                     foreach( $attributes as $attribute )
00136                     {
00137                         $attributeIdentifier = $attribute->attribute( 'contentclass_attribute_identifier' );
00138                         if ( isset( $attributesData[$attributeIdentifier] ) )
00139                         {
00140                             $dataString = $attributesData[$attributeIdentifier];
00141                             switch ( $datatypeString = $attribute->attribute( 'data_type_string' ) )
00142                             {
00143                                 case 'ezimage':
00144                                 case 'ezbinaryfile':
00145                                 case 'ezmedia':
00146                                 {
00147                                     $dataString = $storageDir . $dataString;
00148                                     break;
00149                                 }
00150                                 default:
00151                             }
00152 
00153                             $attribute->fromString( $dataString );
00154                             $attribute->store();
00155                         }
00156                     }
00157                 }
00158 
00159                 $db->commit();
00160 
00161                 $operationResult = eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $contentObject->attribute( 'id' ),
00162                                                                                              'version' => 1 ) );
00163             }
00164             else
00165             {
00166                 eZDebug::writeError( "Content class with identifier '$classIdentifier' doesn't exist.", __METHOD__ );
00167             }
00168         }
00169         else
00170         {
00171             eZDebug::writeError( "Node with id '$parentNodeID' doesn't exist.", __METHOD__ );
00172         }
00173 
00174         return $contentObject;
00175     }
00176 
00177     /**
00178      * Updates an existing content object.
00179      *
00180      * This function works like createAndPublishObject
00181      *
00182      * Here is an example
00183      * <code>
00184      *
00185      * <?php
00186      * $contentObjectID = 1;
00187      * $contentObject = eZContentObject::fetch( $contentObjectID );
00188      *
00189      * if( $contentObject instanceof eZContentObject )
00190      * {
00191      *     $xmlDeclaration = '<?xml version="1.0" encoding="utf-8"?>
00192      *                         <section xmlns:image="http://ez.no/namespaces/ezpublish3/image/"
00193      *                                  xmlns:xhtml="http://ez.no/namespaces/ezpublish3/xhtml/"
00194      *                                  xmlns:custom="http://ez.no/namespaces/ezpublish3/custom/">';
00195      *
00196      *     $now = $now = date( 'Y/m/d H:i:s', time() );
00197      *     $xmlDeclaration = '<?xml version="1.0" encoding="utf-8"?>
00198      *                     <section xmlns:image="http://ez.no/namespaces/ezpublish3/image/"
00199      *                                 xmlns:xhtml="http://ez.no/namespaces/ezpublish3/xhtml/"
00200      *                                 xmlns:custom="http://ez.no/namespaces/ezpublish3/custom/">';
00201      *
00202      *     $attributeList = array( 'name'              => 'Name ' . $now,
00203      *                             'short_name'        => 'Short name ' . $now,
00204      *                             'short_description' => $xmlDeclaration . '<paragraph>Short description '. $now . '</paragraph></section>',
00205      *                             'description'       => $xmlDeclaration . '<paragraph>Description '. $now . '</paragraph></section>',
00206      *                             'show_children'     => false);
00207      *
00208      *     $params = array();
00209      *     $params['attributes'] = $attributeList;
00210      *     // $params['remote_id'] = $now;
00211      *     // $params['section_id'] = 3;
00212      *     // $params['language']  = 'ger-DE';
00213      *
00214      *     $result = eZContentFunctions::updateAndPublishObject( $contentObject, $params );
00215      *
00216      *     if( $result )
00217      *         print( 'Update OK' );
00218      *     else
00219      *         print( 'Failed' );
00220      * }
00221      * ?>
00222      * </code>
00223      * @param eZContentObject an eZContentObject object
00224      * @param array an array with the attributes to update
00225      * @static
00226      * @return bool true if the object has been successfully updated, false otherwise
00227      */
00228     public static function updateAndPublishObject( eZContentObject $object, array $params )
00229     {
00230         if ( !array_key_exists( 'attributes', $params ) and !is_array( $params['attributes'] ) and count( $params['attributes'] ) > 0 )
00231         {
00232             eZDebug::writeError( 'No attributes specified for object' . $object->attribute( 'id' ), __METHOD__ );
00233             return false;
00234         }
00235 
00236         $storageDir   = '';
00237         $languageCode = false;
00238         $mustStore    = false;
00239 
00240         if ( array_key_exists( 'remote_id', $params ) )
00241         {
00242             $object->setAttribute( 'remote_id', $params['remote_id'] );
00243             $mustStore = true;
00244         }
00245 
00246         if ( array_key_exists( 'section_id', $params ) )
00247         {
00248             $object->setAttribute( 'section_id', $params['section_id'] );
00249             $mustStore = true;
00250         }
00251 
00252         if ( $mustStore )
00253             $object->store();
00254 
00255         if ( array_key_exists( 'storage_dir', $params ) )
00256             $storageDir = $params['storage_dir'];
00257 
00258         if ( array_key_exists( 'language', $params ) and $params['language'] != false )
00259         {
00260             $languageCode = $params['language'];
00261         }
00262         else
00263         {
00264             $initialLanguageID = $object->attribute( 'initial_language_id' );
00265             $language = eZContentLanguage::fetch( $initialLanguageID );
00266             $languageCode = $language->attribute( 'locale' );
00267         }
00268 
00269         $db = eZDB::instance();
00270         $db->begin();
00271 
00272         $newVersion = $object->createNewVersion( false, true, $languageCode );
00273 
00274         if ( !$newVersion instanceof eZContentObjectVersion )
00275         {
00276             eZDebug::writeError( 'Unable to create a new version for object ' . $object->attribute( 'id' ), __METHOD__ );
00277 
00278             $db->rollback();
00279 
00280             return false;
00281         }
00282 
00283         $newVersion->setAttribute( 'modified', time() );
00284         $newVersion->store();
00285 
00286         $attributeList = $newVersion->attribute( 'contentobject_attributes' );
00287 
00288         $attributesData = $params['attributes'];
00289 
00290         foreach( $attributeList as $attribute )
00291         {
00292             $attributeIdentifier = $attribute->attribute( 'contentclass_attribute_identifier' );
00293             if ( array_key_exists( $attributeIdentifier, $attributesData ) )
00294             {
00295                 $dataString = $attributesData[$attributeIdentifier];
00296                 switch ( $datatypeString = $attribute->attribute( 'data_type_string' ) )
00297                 {
00298                     case 'ezimage':
00299                     case 'ezbinaryfile':
00300                     case 'ezmedia':
00301                     {
00302                         $dataString = $storageDir . $dataString;
00303                         break;
00304                     }
00305                     default:
00306                 }
00307 
00308                 $attribute->fromString( $dataString );
00309                 $attribute->store();
00310             }
00311         }
00312 
00313         $db->commit();
00314 
00315         $operationResult = eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $newVersion->attribute( 'contentobject_id' ),
00316                                                                                      'version'   => $newVersion->attribute( 'version' ) ) );
00317 
00318         if( $operationResult['status'] == eZModuleOperationInfo::STATUS_CONTINUE )
00319             return true;
00320 
00321         return false;
00322     }
00323 }
00324 
00325 ?>