/kernel/classes/ezcontentobject.php
PHP | 6164 lines | 4628 code | 622 blank | 914 comment | 630 complexity | e8d5c0ce7a192f05428a2beb6a7a3abd MD5 | raw file
Possible License(s): GPL-2.0
Large files files are truncated, but you can click here to view the full file
- <?php
- //
- // Definition of eZContentObject class
- //
- // Created on: <17-Apr-2002 09:15:27 bf>
- //
- // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
- // SOFTWARE NAME: eZ Publish
- // SOFTWARE RELEASE: 4.1.x
- // COPYRIGHT NOTICE: Copyright (C) 1999-2011 eZ Systems AS
- // SOFTWARE LICENSE: GNU General Public License v2.0
- // NOTICE: >
- // This program is free software; you can redistribute it and/or
- // modify it under the terms of version 2.0 of the GNU General
- // Public License as published by the Free Software Foundation.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of version 2.0 of the GNU General
- // Public License along with this program; if not, write to the Free
- // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- // MA 02110-1301, USA.
- //
- //
- // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
- //
- /*!
- \class eZContentObject ezcontentobject.php
- \ingroup eZKernel
- \brief Handles eZ Publish content objects
- It encapsulates the data for an object and provides lots of functions
- for dealing with versions, translations and attributes.
- \sa eZContentClass
- */
- class eZContentObject extends eZPersistentObject
- {
- const STATUS_DRAFT = 0;
- const STATUS_PUBLISHED = 1;
- const STATUS_ARCHIVED = 2;
- const PACKAGE_ERROR_NO_CLASS = 1;
- const PACKAGE_ERROR_EXISTS = 2;
- const PACKAGE_ERROR_NODE_EXISTS = 3;
- const PACKAGE_ERROR_MODIFIED = 101;
- const PACKAGE_ERROR_HAS_CHILDREN = 102;
- const PACKAGE_REPLACE = 1;
- const PACKAGE_SKIP = 2;
- const PACKAGE_NEW = 3;
- const PACKAGE_UPDATE = 6;
- const PACKAGE_DELETE = 4;
- const PACKAGE_KEEP = 5;
- const RELATION_COMMON = 1;
- const RELATION_EMBED = 2;
- const RELATION_LINK = 4;
- const RELATION_ATTRIBUTE = 8;
- function eZContentObject( $row )
- {
- $this->eZPersistentObject( $row );
- $this->ClassIdentifier = false;
- if ( isset( $row['contentclass_identifier'] ) )
- $this->ClassIdentifier = $row['contentclass_identifier'];
- $this->ClassName = false;
- if ( isset( $row['contentclass_name'] ) )
- $this->ClassName = $row['contentclass_name'];
- if ( isset( $row['serialized_name_list'] ) )
- $this->ClassName = eZContentClass::nameFromSerializedString( $row['serialized_name_list'] );
- $this->CurrentLanguage = false;
- if ( isset( $row['content_translation'] ) )
- {
- $this->CurrentLanguage = $row['content_translation'];
- }
- else if ( isset( $row['real_translation'] ) )
- {
- $this->CurrentLanguage = $row['real_translation'];
- }
- else if ( isset( $row['language_mask'] ) )
- {
- $topPriorityLanguage = eZContentLanguage::topPriorityLanguageByMask( $row['language_mask'] );
- if ( $topPriorityLanguage )
- {
- $this->CurrentLanguage = $topPriorityLanguage->attribute( 'locale' );
- }
- }
- }
- static function definition()
- {
- static $definition = array( "fields" => array( "id" => array( 'name' => 'ID',
- 'datatype' => 'integer',
- 'default' => 0,
- 'required' => true ),
- "section_id" => array( 'name' => "SectionID",
- 'datatype' => 'integer',
- 'default' => 0,
- 'required' => true,
- 'foreign_class' => 'eZSection',
- 'foreign_attribute' => 'id',
- 'multiplicity' => '1..*' ),
- "owner_id" => array( 'name' => "OwnerID",
- 'datatype' => 'integer',
- 'default' => 0,
- 'required' => true,
- 'foreign_class' => 'eZUser',
- 'foreign_attribute' => 'contentobject_id',
- 'multiplicity' => '1..*'),
- "contentclass_id" => array( 'name' => "ClassID",
- 'datatype' => 'integer',
- 'default' => 0,
- 'required' => true,
- 'foreign_class' => 'eZContentClass',
- 'foreign_attribute' => 'id',
- 'multiplicity' => '1..*' ),
- "name" => array( 'name' => "Name",
- 'datatype' => 'string',
- 'default' => '',
- 'required' => true ),
- "published" => array( 'name' => "Published",
- 'datatype' => 'integer',
- 'default' => 0,
- 'required' => true ),
- "modified" => array( 'name' => "Modified",
- 'datatype' => 'integer',
- 'default' => 0,
- 'required' => true ),
- "current_version" => array( 'name' => "CurrentVersion",
- 'datatype' => 'integer',
- 'default' => 0,
- 'required' => true ),
- "status" => array( 'name' => "Status",
- 'datatype' => 'integer',
- 'default' => 0,
- 'required' => true ),
- 'remote_id' => array( 'name' => "RemoteID",
- 'datatype' => 'string',
- 'default' => '',
- 'required' => true ),
- 'language_mask' => array( 'name' => 'LanguageMask',
- 'datatype' => 'integer',
- 'default' => 0,
- 'required' => true ),
- 'initial_language_id' => array( 'name' => 'InitialLanguageID',
- 'datatype' => 'integer',
- 'default' => 0,
- 'required' => true,
- 'foreign_class' => 'eZContentLanguage',
- 'foreign_attribute' => 'id',
- 'multiplicity' => '1..*' ) ),
- "keys" => array( "id" ),
- "function_attributes" => array( "current" => "currentVersion",
- 'versions' => 'versions',
- 'author_array' => 'authorArray',
- "class_name" => "className",
- "content_class" => "contentClass",
- "contentobject_attributes" => "contentObjectAttributes",
- "owner" => "owner",
- "related_contentobject_array" => "relatedContentObjectList",
- "related_contentobject_count" => "relatedContentObjectCount",
- 'reverse_related_contentobject_array' => 'reverseRelatedObjectList',
- 'reverse_related_contentobject_count' => 'reverseRelatedObjectCount',
- "linked_contentobject_array" => "linkedContentObjectList",
- "linked_contentobject_count" => "linkedContentObjectCount",
- 'reverse_linked_contentobject_array' => 'reverseLinkedObjectList',
- 'reverse_linked_contentobject_count' => 'reverseLinkedObjectCount',
- "embedded_contentobject_array" => "embeddedContentObjectList",
- "embedded_contentobject_count" => "embeddedContentObjectCount",
- 'reverse_embedded_contentobject_array' => 'reverseEmbeddedObjectList',
- 'reverse_embedded_contentobject_count' => 'reverseEmbeddedObjectCount',
- "can_read" => "canRead",
- "can_pdf" => "canPdf",
- "can_diff" => "canDiff",
- "can_create" => "canCreate",
- "can_create_class_list" => "canCreateClassList",
- "can_edit" => "canEdit",
- "can_translate" => "canTranslate",
- "can_remove" => "canRemove",
- "can_move" => "canMoveFrom",
- "can_move_from" => "canMoveFrom",
- 'can_view_embed' => 'canViewEmbed',
- "data_map" => "dataMap",
- "grouped_data_map" => "groupedDataMap",
- "main_parent_node_id" => "mainParentNodeID",
- "assigned_nodes" => "assignedNodes",
- "parent_nodes" => "parentNodeIDArray",
- "main_node_id" => "mainNodeID",
- "main_node" => "mainNode",
- "default_language" => "defaultLanguage",
- "content_action_list" => "contentActionList",
- "class_identifier" => "contentClassIdentifier",
- 'class_group_id_list' => 'contentClassGroupIDList',
- 'name' => 'name',
- 'match_ingroup_id_list' => 'matchIngroupIDList',
- 'remote_id' => 'remoteID',
- 'current_language' => 'currentLanguage',
- 'current_language_object' => 'currentLanguageObject',
- 'initial_language' => 'initialLanguage',
- 'initial_language_code' => 'initialLanguageCode',
- 'available_languages' => 'availableLanguages',
- 'language_codes' => 'availableLanguages',
- 'language_js_array' => 'availableLanguagesJsArray',
- 'languages' => 'languages',
- 'all_languages' => 'allLanguages',
- 'can_edit_languages' => 'canEditLanguages',
- 'can_create_languages' => 'canCreateLanguages',
- 'always_available' => 'isAlwaysAvailable',
- 'allowed_assign_section_list' => 'allowedAssignSectionList',
- 'allowed_assign_state_id_list' => 'allowedAssignStateIDList',
- 'allowed_assign_state_list' => 'allowedAssignStateList',
- 'state_id_array' => 'stateIDArray',
- 'state_identifier_array' => 'stateIdentifierArray' ),
- "increment_key" => "id",
- "class_name" => "eZContentObject",
- "sort" => array( "id" => "asc" ),
- "name" => "ezcontentobject" );
- return $definition;
- }
- /*!
- Get class groups this object's class belongs to if match for class groups is enabled.
- \return array of class group ids. False if match is disabled.
- */
- function matchIngroupIDList()
- {
- $contentINI = eZINI::instance( 'content.ini' );
- $inList = false;
- if( $contentINI->variable( 'ContentOverrideSettings', 'EnableClassGroupOverride' ) == 'true' )
- {
- $contentClass = $this->contentClass();
- $inList = $contentClass->attribute( 'ingroup_id_list' );
- }
- return $inList;
- }
- /*!
- Store the object
- \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
- the calls within a db transaction; thus within db->begin and db->commit.
- */
- function store( $fieldFilters = null )
- {
- // Unset the cache
- global $eZContentObjectContentObjectCache;
- unset( $eZContentObjectContentObjectCache[$this->ID] );
- global $eZContentObjectDataMapCache;
- unset( $eZContentObjectDataMapCache[$this->ID] );
- global $eZContentObjectVersionCache;
- unset( $eZContentObjectVersionCache[$this->ID] );
- $db = eZDB::instance();
- $db->begin();
- $this->storeNodeModified();
- eZPersistentObject::store( $fieldFilters );
- $db->commit();
- }
- /*!
- Clear in-memory caches.
- \param $idArray objects to clear caches for.
- If the parameter is ommitted the caches are cleared for all objects.
- */
- static function clearCache( $idArray = array() )
- {
- if ( is_numeric( $idArray ) )
- $idArray = array( $idArray );
- // clear in-memory cache for all objects
- if ( count( $idArray ) == 0 )
- {
- unset( $GLOBALS['eZContentObjectContentObjectCache'] );
- unset( $GLOBALS['eZContentObjectDataMapCache'] );
- unset( $GLOBALS['eZContentObjectVersionCache'] );
- return;
- }
- // clear in-memory cache for specified object(s)
- global $eZContentObjectContentObjectCache;
- global $eZContentObjectDataMapCache;
- global $eZContentObjectVersionCache;
- foreach ( $idArray as $objectID )
- {
- unset( $eZContentObjectContentObjectCache[$objectID] );
- unset( $eZContentObjectDataMapCache[$objectID] );
- unset( $eZContentObjectVersionCache[$objectID] );
- }
- }
- /*!
- Update all nodes to set modified_subnode value
- \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
- the calls within a db transaction; thus within db->begin and db->commit.
- */
- function storeNodeModified()
- {
- if ( is_numeric( $this->ID ) )
- {
- $nodeArray = $this->assignedNodes();
- $db = eZDB::instance();
- $db->begin();
- foreach ( array_keys( $nodeArray ) as $key )
- {
- $nodeArray[$key]->updateAndStoreModified();
- }
- $db->commit();
- }
- }
- function name( $version = false , $lang = false )
- {
- // if the object id is null, we can't read data from the database
- // and return the locally known name
- if ( $this->attribute( 'id' ) === null )
- {
- return $this->Name;
- }
- if ( !$version )
- {
- $version = $this->attribute( 'current_version' );
- }
- if ( !$lang && $this->CurrentLanguage )
- {
- $lang = $this->CurrentLanguage;
- }
- return $this->versionLanguageName( $version, $lang );
- }
- function names()
- {
- $version = $this->attribute( 'current_version' );
- $id = $this->attribute( 'id' );
- $db = eZDB::instance();
- $rows = $db->arrayQuery( "SELECT name, real_translation FROM ezcontentobject_name WHERE contentobject_id = '$id' AND content_version='$version'" );
- $names = array();
- foreach ( $rows as $row )
- {
- $names[$row['real_translation']] = $row['name'];
- }
- return $names;
- }
- function versionLanguageName( $version, $lang = false )
- {
- $name = false;
- if ( !$version > 0 )
- {
- eZDebug::writeNotice( "There is no object name for version($version) of the content object ($contentObjectID) in language($lang)", __METHOD__ );
- return $name;
- }
- $db = eZDb::instance();
- $contentObjectID = $this->attribute( 'id' );
- if ( !$lang )
- {
- // If $lang not given we will use the initial language of the object
- $query = "SELECT initial_language_id FROM ezcontentobject WHERE id='$contentObjectID'";
- $rows = $db->arrayQuery( $query );
- if ( $rows )
- {
- $languageID = $rows[0]['initial_language_id'];
- $language = eZContentLanguage::fetch( $languageID );
- if ( $language )
- {
- $lang = $language->attribute( 'locale' );
- }
- else
- {
- return $name;
- }
- }
- else
- {
- return $name;
- }
- }
- $lang = $db->escapeString( $lang );
- $version = (int) $version;
- $languageID = $this->attribute( 'initial_language_id' );
- if ( $this->attribute( 'always_available' ) )
- {
- $languageID = (int) $languageID | 1;
- }
- $query= "SELECT name, content_translation
- FROM ezcontentobject_name
- WHERE contentobject_id = '$contentObjectID'
- AND content_version = '$version'
- AND ( content_translation = '$lang' OR language_id = '$languageID' )";
- $result = $db->arrayQuery( $query );
- $resCount = count( $result );
- if( $resCount < 1 )
- {
- eZDebug::writeNotice( "There is no object name for version($version) of the content object ($contentObjectID) in language($lang)", __METHOD__ );
- }
- else if( $resCount > 1 )
- {
- // we have name in requested language => find and return it
- foreach( $result as $row )
- {
- if( $row['content_translation'] == $lang )
- {
- $name = $row['name'];
- break;
- }
- }
- }
- else
- {
- // we don't have name in requested language(or requested language is the same as initial language) => use name in initial language
- $name = $result[0]['name'];
- }
- return $name;
- }
- /*!
- Sets the name of the object, in memory only. Use setName() to change it.
- */
- function setCachedName( $name )
- {
- $this->Name = $name;
- }
- /*!
- Sets the name of the object in all translations.
- \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
- the calls within a db transaction; thus within db->begin and db->commit.
- */
- function setName( $objectName, $versionNum = false, $languageCode = false )
- {
- $initialLanguageCode = false;
- if ( $initialLanguage = $this->initialLanguage() )
- {
- $initialLanguageCode = $initialLanguage->attribute( 'locale' );
- }
- $db = eZDB::instance();
- if ( $languageCode == false )
- {
- $languageCode = $initialLanguageCode;
- }
- $languageCode = $db->escapeString( $languageCode );
- if ( $languageCode == $initialLanguageCode )
- {
- $this->Name = $objectName;
- }
- if ( !$versionNum )
- {
- $versionNum = $this->attribute( 'current_version' );
- }
- $objectID =(int) $this->attribute( 'id' );
- $versionNum =(int) $versionNum;
- $languageID =(int) eZContentLanguage::idByLocale( $languageCode );
- $objectName = $db->escapeString( $objectName );
- $db->begin();
- // Check if name is already set before setting/changing it.
- // This helps to avoid deadlocks in mysql: a pair of DELETE/INSERT might cause deadlock here
- // in case of concurrent execution.
- $rows = $db->arrayQuery( "SELECT COUNT(*) AS count FROM ezcontentobject_name WHERE contentobject_id = '$objectID'
- AND content_version = '$versionNum' AND content_translation ='$languageCode'" );
- if ( $rows[0]['count'] )
- {
- $db->query( "UPDATE ezcontentobject_name SET name='$objectName'
- WHERE
- contentobject_id = '$objectID' AND
- content_version = '$versionNum' AND
- content_translation ='$languageCode'" );
- }
- else
- {
- $db->query( "INSERT INTO ezcontentobject_name( contentobject_id,
- name,
- content_version,
- content_translation,
- real_translation,
- language_id )
- VALUES( '$objectID',
- '$objectName',
- '$versionNum',
- '$languageCode',
- '$languageCode',
- '$languageID' )" );
- }
- $db->commit();
- }
- /*!
- \return a map with all the content object attributes where the keys are the
- attribute identifiers.
- */
- function dataMap()
- {
- return $this->fetchDataMap();
- }
- /**
- * Generates a map with all the content object attributes where the keys are
- * the attribute identifiers grouped by class attribute category.
- *
- * @note Result is not cached, so make sure you don't call this over and over.
- *
- * @return array
- */
- public function groupedDataMap()
- {
- return self::createGroupedDataMap( $this->fetchDataMap() );
- }
- /**
- * Generates a map with all the content object attributes where the keys are
- * the attribute identifiers grouped by class attribute category.
- *
- * @note Result is not cached, so make sure you don't call this over and over.
- *
- * @param array $contentObjectAttributes Array of eZContentObjectAttribute objects
- * @return array
- */
- public static function createGroupedDataMap( $contentObjectAttributes )
- {
- $groupedDataMap = array();
- $contentINI = eZINI::instance( 'content.ini' );
- $categorys = $contentINI->variable( 'ClassAttributeSettings', 'CategoryList' );
- $defaultCategory = $contentINI->variable( 'ClassAttributeSettings', 'DefaultCategory' );
- foreach( $contentObjectAttributes as $attribute )
- {
- $classAttribute = $attribute->contentClassAttribute();
- $attributeCategory = $classAttribute->attribute('category');
- $attributeIdentifier = $classAttribute->attribute( 'identifier' );
- if ( !isset( $categorys[ $attributeCategory ] ) || !$attributeCategory )
- $attributeCategory = $defaultCategory;
- if ( !isset( $groupedDataMap[ $attributeCategory ] ) )
- $groupedDataMap[ $attributeCategory ] = array();
- $groupedDataMap[ $attributeCategory ][$attributeIdentifier] = $attribute;
- }
- return $groupedDataMap;
- }
- /*!
- \return a map with all the content object attributes where the keys are the
- attribute identifiers.
- \sa eZContentObjectTreeNode::dataMap
- */
- function fetchDataMap( $version = false, $language = false )
- {
- // Global variable to cache datamaps
- global $eZContentObjectDataMapCache;
- if ( $version == false )
- $version = $this->attribute( 'current_version' );
- if ( $language == false )
- {
- $language = $this->CurrentLanguage;
- }
- if ( !$language || !isset( $eZContentObjectDataMapCache[$this->ID][$version][$language] ) )
- {
- $data = $this->contentObjectAttributes( true, $version, $language );
- if ( !$language )
- {
- $language = $this->CurrentLanguage;
- }
- // Store the attributes for later use
- $this->ContentObjectAttributeArray[$version][$language] = $data;
- $eZContentObjectDataMapCache[$this->ID][$version][$language] = $data;
- }
- else
- {
- $data = $eZContentObjectDataMapCache[$this->ID][$version][$language];
- }
- if ( !isset( $this->DataMap[$version][$language] ) )
- {
- $ret = array();
- foreach( $data as $key => $item )
- {
- $identifier = $item->contentClassAttributeIdentifier();
- $ret[$identifier] = $data[$key];
- }
- $this->DataMap[$version][$language] = $ret;
- }
- else
- {
- $ret = $this->DataMap[$version][$language];
- }
- return $ret;
- }
- function resetDataMap()
- {
- $this->ContentObjectAttributeArray = array();
- $this->ContentObjectAttributes = array();
- $this->DataMap = array();
- return $this->DataMap;
- }
- /*!
- Fetch a set of content object attributes by their class identifiers.
- */
- function fetchAttributesByIdentifier( $identifierArray, $version = false, $languageArray = false, $asObject = true )
- {
- if ( count( $identifierArray ) === 0 )
- {
- return null;
- }
- $db = eZDB::instance();
- $identifierQuotedString = array();
- foreach ( $identifierArray as $identifier )
- {
- $identifierQuotedString[] = "'$identifier'";
- }
- if ( !$version or !is_numeric( $version ) )
- {
- $version = $this->CurrentVersion;
- }
- if ( is_array( $languageArray ) )
- {
- $langCodeQuotedString = array();
- foreach ( $languageArray as $langCode )
- {
- if ( is_string( $langCode ) )
- $langCodeQuotedString[] = "'$langCode'";
- }
- if ( !empty( $langCodeQuotedString ) )
- {
- $languageText = "AND ";
- $languageText .= $db->generateSQLINStatement( $langCodeQuotedString, 'ezcontentobject_attribute.language_code' );
- }
- }
- if ( !isset( $languageText ) )
- {
- $languageText = "AND " . eZContentLanguage::sqlFilter( 'ezcontentobject_attribute', 'ezcontentobject_version' );
- }
- $versionText = "AND ezcontentobject_attribute.version = '$version'";
- $query = "SELECT ezcontentobject_attribute.*, ezcontentclass_attribute.identifier as identifier
- FROM ezcontentobject_attribute, ezcontentclass_attribute, ezcontentobject_version
- WHERE
- ezcontentclass_attribute.version = ". eZContentClass::VERSION_STATUS_DEFINED . " AND
- ezcontentclass_attribute.id = ezcontentobject_attribute.contentclassattribute_id AND
- ezcontentobject_version.contentobject_id = {$this->ID} AND
- ezcontentobject_version.version = {$version} AND
- ezcontentobject_attribute.contentobject_id = {$this->ID}
- {$languageText}
- {$versionText}
- AND
- ";
- $query .= $db->generateSQLINStatement( $identifierQuotedString, 'identifier' );
- $rows = $db->arrayQuery( $query );
- if ( count( $rows ) > 0 )
- {
- if ( $asObject )
- {
- $returnArray = array();
- foreach( $rows as $row )
- {
- $returnArray[$row['id']] = new eZContentObjectAttribute( $row );
- }
- return $returnArray;
- }
- else
- {
- return $rows;
- }
- }
- return null;
- }
- /*!
- Returns the owner of the object as a content object.
- */
- function owner()
- {
- if ( $this->OwnerID != 0 )
- {
- return eZContentObject::fetch( $this->OwnerID );
- }
- return null;
- }
- /*!
- \return the content class group identifiers for the current content object
- */
- function contentClassGroupIDList()
- {
- $contentClass = $this->contentClass();
- return $contentClass->attribute( 'ingroup_id_list' );
- }
- /*!
- \return the content class identifer for the current content object
- \note The object will cache the class name information so multiple calls will be fast.
- */
- function contentClassIdentifier()
- {
- if ( !is_numeric( $this->ClassID ) )
- {
- $retValue = null;
- return $retValue;
- }
- if ( $this->ClassIdentifier !== false )
- return $this->ClassIdentifier;
- $this->ClassIdentifier = eZContentClass::classIdentifierByID( $this->ClassID );
- return $this->ClassIdentifier;
- }
- /*!
- \return the content class for the current content object
- */
- function contentClass()
- {
- if ( !is_numeric( $this->ClassID ) )
- {
- $retValue = null;
- return $retValue;
- }
- return eZContentClass::fetch( $this->ClassID );
- }
- /*!
- Get remote id of content object
- */
- function remoteID()
- {
- $remoteID = eZPersistentObject::attribute( 'remote_id', true );
- // Ensures that we provide the correct remote_id if we have one in the database
- if ( $remoteID === null and $this->attribute( 'id' ) )
- {
- $db = eZDB::instance();
- $resultArray = $db->arrayQuery( "SELECT remote_id FROM ezcontentobject WHERE id = '" . $this->attribute( 'id' ) . "'" );
- if ( count( $resultArray ) == 1 )
- {
- $remoteID = $resultArray[0]['remote_id'];
- $this->setAttribute( 'remote_id', $remoteID );
- }
- }
- if ( !$remoteID )
- {
- $this->setAttribute( 'remote_id', eZRemoteIdUtility::generate( 'object' ) );
- if ( $this->attribute( 'id' ) !== null )
- $this->sync( array( 'remote_id' ) );
- $remoteID = eZPersistentObject::attribute( 'remote_id', true );
- }
- return $remoteID;
- }
- function mainParentNodeID()
- {
- $list = eZContentObjectTreeNode::getParentNodeIdListByContentObjectID( $this->ID, false, true );
- return isset( $list[0] ) ? $list[0] : null;
- }
- /*!
- Fetches contentobject by remote ID, returns null if none exist
- */
- static function fetchByRemoteID( $remoteID, $asObject = true )
- {
- $db = eZDB::instance();
- $remoteID =$db->escapeString( $remoteID );
- $resultArray = $db->arrayQuery( 'SELECT id FROM ezcontentobject WHERE remote_id=\'' . $remoteID . '\'' );
- if ( count( $resultArray ) != 1 )
- $object = null;
- else
- $object = eZContentObject::fetch( $resultArray[0]['id'], $asObject );
- return $object;
- }
- /**
- * Fetches a content object by ID
- * @param int $id ID of the content object to fetch
- * @param bool $asObject
- * Return the result as an object (true) or an assoc. array (false)
- *
- * @return eZContentObject
- */
- static function fetch( $id, $asObject = true )
- {
- global $eZContentObjectContentObjectCache;
- // If the object given by its id is not cached or should be returned as array
- // then we fetch it from the DB (objects are always cached as arrays).
- if ( !isset( $eZContentObjectContentObjectCache[$id] ) or $asObject === false )
- {
- $db = eZDB::instance();
- $resArray = $db->arrayQuery( eZContentObject::createFetchSQLString( $id ) );
- $objectArray = array();
- if ( count( $resArray ) == 1 && $resArray !== false )
- {
- $objectArray = $resArray[0];
- }
- else
- {
- eZDebug::writeError( "Object not found ($id)", __METHOD__ );
- $retValue = null;
- return $retValue;
- }
- if ( $asObject )
- {
- $obj = new eZContentObject( $objectArray );
- $eZContentObjectContentObjectCache[$id] = $obj;
- }
- else
- {
- return $objectArray;
- }
- return $obj;
- }
- else
- {
- return $eZContentObjectContentObjectCache[$id];
- }
- }
- /*!
- \static
- Tests for the existance of a content object by using the ID \a $id.
- \return \c true if the object exists, \c false otherwise.
- \note Uses the static function createFetchSQLString() to generate the SQL
- */
- static function exists( $id )
- {
- global $eZContentObjectContentObjectCache;
- // Check the global cache
- if ( isset( $eZContentObjectContentObjectCache[$id] ) )
- return true;
- // If the object is not cached we need to check the DB
- $db = eZDB::instance();
- $resArray = $db->arrayQuery( eZContentObject::createFetchSQLString( $id ) );
- if ( $resArray !== false and count( $resArray ) == 1 )
- {
- return true;
- }
- return false;
- }
- /*!
- \static
- Creates the SQL for fetching the object with ID \a $id and returns the string.
- */
- static function createFetchSQLString( $id )
- {
- $id = (int) $id;
- $fetchSQLString = "SELECT ezcontentobject.*,
- ezcontentclass.serialized_name_list as serialized_name_list,
- ezcontentclass.identifier as contentclass_identifier,
- ezcontentclass.is_container as is_container
- FROM
- ezcontentobject,
- ezcontentclass
- WHERE
- ezcontentobject.id='$id' AND
- ezcontentclass.id = ezcontentobject.contentclass_id AND
- ezcontentclass.version=0";
- return $fetchSQLString;
- }
- /**
- * Creates the SQL for filtering objects by visibility, used by IgnoreVisibility on some fetches.
- * The object is visible if 1 or more assigned nodes are visible.
- *
- * @static
- * @since Version 4.1
- * @param bool $IgnoreVisibility ignores visibility if true
- * @param string $ezcontentobjectTable name of ezcontentobject table used in sql
- * @return string with sql condition for node filtering by visibility
- */
- static function createFilterByVisibilitySQLString( $IgnoreVisibility = false, $ezcontentobjectTable = 'ezcontentobject' )
- {
- if ( $IgnoreVisibility )
- return '';
- return " AND ( SELECT MIN( ezct.is_invisible ) FROM ezcontentobject_tree ezct WHERE ezct.contentobject_id = $ezcontentobjectTable.id ) = 0 ";
- }
- /**
- * Fetches the contentobject which has a node with ID $nodeID
- * $nodeID can also be an array of NodeIDs. In this case, an array of content objects will be returned
- * @param int|array $nodeID Single nodeID or array of NodeIDs
- * @param bool $asObject If results have to be returned as eZContentObject instances or not
- * @return mixed Content object or array of content objects.
- * Content objects can be eZContentObject instances or array result sets
- */
- static function fetchByNodeID( $nodeID, $asObject = true )
- {
- global $eZContentObjectContentObjectCache;
- $resultAsArray = is_array( $nodeID );
- $nodeID = (array)$nodeID;
- $db = eZDB::instance();
- $resArray = $db->arrayQuery(
- "SELECT co.*, con.name as name, con.real_translation, cot.node_id
- FROM ezcontentobject co
- JOIN ezcontentobject_tree cot ON co.id = cot.contentobject_id AND co.current_version = cot.contentobject_version
- JOIN ezcontentobject_name con ON co.id = con.contentobject_id AND co.current_version = con.content_version
- WHERE " .
- $db->generateSQLINStatement( $nodeID, 'cot.node_id', false, true, 'int' ) . " AND " .
- eZContentLanguage::sqlFilter( 'con', 'co' )
- );
- if ( $resArray === false || empty( $resArray ) )
- {
- eZDebug::writeError( 'A problem occured while fetching objects with following NodeIDs : ' . implode( ', ', $nodeID ), __METHOD__ );
- return $resultAsArray ? array() : null;
- }
- $objectArray = array();
- if ( $asObject )
- {
- foreach ( $resArray as $res )
- {
- $objectArray[$res['node_id']] = $eZContentObjectContentObjectCache[$res['id']] = new self( $res );
- }
- }
- else
- {
- foreach ( $resArray as $res )
- {
- $objectArray[$res['node_id']] = $res;
- }
- }
- if ( !$resultAsArray )
- return $objectArray[$res['node_id']];
- return $objectArray;
- }
- /**
- * Fetches a content object list based on an array of content object ids
- *
- * @param array $idArray array of content object ids
- * @param bool $asObject
- * Wether to get the result as an array of eZContentObject or an
- * array of associative arrays
- *
- * @return array(contentObjectID => eZContentObject|array)
- * array of eZContentObject (if $asObject = true) or array of
- * associative arrays (if $asObject = false)
- */
- static function fetchIDArray( $idArray, $asObject = true )
- {
- global $eZContentObjectContentObjectCache;
- $db = eZDB::instance();
- $resRowArray = $db->arrayQuery(
- "SELECT ezcontentclass.serialized_name_list as class_serialized_name_list, ezcontentobject.*, ezcontentobject_name.name as name, ezcontentobject_name.real_translation
- FROM
- ezcontentclass,
- ezcontentobject,
- ezcontentobject_name
- WHERE
- ezcontentclass.id=ezcontentobject.contentclass_id AND " .
- // All elements from $idArray should be casted to (int)
- $db->generateSQLINStatement( $idArray, 'ezcontentobject.id', false, true, 'int' ) . " AND
- ezcontentobject.id = ezcontentobject_name.contentobject_id AND
- ezcontentobject.current_version = ezcontentobject_name.content_version AND " .
- eZContentLanguage::sqlFilter( 'ezcontentobject_name', 'ezcontentobject' )
- );
- $objectRetArray = array();
- foreach ( $resRowArray as $resRow )
- {
- $objectID = $resRow['id'];
- $resRow['class_name'] = eZContentClass::nameFromSerializedString( $resRow['class_serialized_name_list'] );
- if ( $asObject )
- {
- $obj = new eZContentObject( $resRow );
- $obj->ClassName = $resRow['class_name'];
- $eZContentObjectContentObjectCache[$objectID] = $obj;
- $objectRetArray[$objectID] = $obj;
- }
- else
- {
- $objectRetArray[$objectID] = $resRow;
- }
- }
- return $objectRetArray;
- }
- /*!
- \return An array with content objects.
- \param $asObject Whether to return objects or not
- \param $conditions Optional conditions to limit the fetch, set to \c null to skip it.
- \param $offset Where to start fetch from, set to \c false to skip it.
- \param $limit Maximum number of objects to fetch, set \c false to skip it.
- \sa fetchListCount
- */
- static function fetchList( $asObject = true, $conditions = null, $offset = false, $limit = false )
- {
- $limitation = null;
- if ( $offset !== false or
- $limit !== false )
- $limitation = array( 'offset' => $offset,
- 'length' => $limit );
- return eZPersistentObject::fetchObjectList( eZContentObject::definition(),
- null,
- $conditions, null, $limitation,
- $asObject );
- }
- static function fetchFilteredList( $conditions = null, $offset = false, $limit = false, $asObject = true )
- {
- $limits = null;
- if ( $offset or $limit )
- $limits = array( 'offset' => $offset,
- 'length' => $limit );
- return eZPersistentObject::fetchObjectList( eZContentObject::definition(),
- null,
- $conditions, null, $limits,
- $asObject );
- }
- /*!
- \return The number of objects in the database. Optionally \a $conditions can be used to limit the list count.
- \sa fetchList
- */
- static function fetchListCount( $conditions = null )
- {
- $rows = eZPersistentObject::fetchObjectList( eZContentObject::definition(),
- array(),
- $conditions,
- false/* we don't want any sorting when counting. Sorting leads to error on postgresql 8.x */,
- null,
- false, false,
- array( array( 'operation' => 'count( * )',
- 'name' => 'count' ) ) );
- return $rows[0]['count'];
- }
- static function fetchSameClassList( $contentClassID, $asObject = true, $offset = false, $limit = false )
- {
- $conditions = array( 'contentclass_id' => $contentClassID );
- return eZContentObject::fetchFilteredList( $conditions, $offset, $limit, $asObject );
- }
- static function fetchSameClassListCount( $contentClassID )
- {
- $result = eZPersistentObject::fetchObjectList( eZContentObject::definition(),
- array(),
- array( "contentclass_id" => $contentClassID ),
- false, null,
- false, false,
- array( array( 'operation' => 'count( * )',
- 'name' => 'count' ) ) );
- return $result[0]['count'];
- }
- /*!
- Returns the current version of this document.
- */
- function currentVersion( $asObject = true )
- {
- return eZContentObjectVersion::fetchVersion( $this->attribute( "current_version" ), $this->ID, $asObject );
- }
- /*!
- Returns the given object version. False is returned if the versions does not exist.
- */
- function version( $version, $asObject = true )
- {
- if ( $asObject )
- {
- global $eZContentObjectVersionCache;
- if ( !isset( $eZContentObjectVersionCache ) ) // prevent PHP warning below
- $eZContentObjectVersionCache = array();
- if ( isset( $eZContentObjectVersionCache[$this->ID][$version] ) )
- {
- return $eZContentObjectVersionCache[$this->ID][$version];
- }
- else
- {
- $eZContentObjectVersionCache[$this->ID][$version] = eZContentObjectVersion::fetchVersion( $version, $this->ID, $asObject );
- return $eZContentObjectVersionCache[$this->ID][$version];
- }
- }
- else
- {
- return eZContentObjectVersion::fetchVersion( $version, $this->ID, $asObject );
- }
- }
- /*!
- \return an array of versions for the current object.
- */
- function versions( $asObject = true, $parameters = array() )
- {
- $conditions = array( "contentobject_id" => $this->ID );
- if ( isset( $parameters['conditions'] ) )
- {
- if ( isset( $parameters['conditions']['status'] ) )
- $conditions['status'] = $parameters['conditions']['status'];
- if ( isset( $parameters['conditions']['creator_id'] ) )
- $conditions['creator_id'] = $parameters['conditions']['creator_id'];
- if ( isset( $parameters['conditions']['language_code'] ) )
- {
- $conditions['initial_language_id'] = eZContentLanguage::idByLocale( $parameters['conditions']['language_code'] );
- }
- if ( isset( $parameters['conditions']['initial_language_id'] ) )
- {
- $conditions['initial_language_id'] = $parameters['conditions']['initial_language_id'];
- }
- }
- return eZPersistentObject::fetchObjectList( eZContentObjectVersion::definition(),
- null, $conditions,
- null, null,
- $asObject );
- }
- /*!
- \return \c true if the object has any versions remaining.
- */
- function hasRemainingVersions()
- {
- $remainingVersions = $this->versions( false );
- if ( !is_array( $remainingVersions ) or
- count( $remainingVersions ) == 0 )
- {
- return false;
- }
- return true;
- }
- function createInitialVersion( $userID, $initialLanguageCode = false )
- {
- return eZContentObjectVersion::create( $this->attribute( "id" ), $userID, 1, $initialLanguageCode );
- }
- function createNewVersionIn( $languageCode, $copyFromLanguageCode = false, $copyFromVersion = false, $versionCheck = true, $status = eZContentObjectVersion::STATUS_DRAFT )
- {
- return $this->createNewVersion( $copyFromVersion, $versionCheck, $languageCode, $copyFromLanguageCode, $status );
- }
- /*!
- Creates a new version and returns it as an eZContentObjectVersion object.
- If version number is given as argument that version is used to create a copy.
- \param $versionCheck If \c true it will check if there are too many version and
- remove some of them to make room for a new.
- \note Transaction unsafeā¦
Large files files are truncated, but you can click here to view the full file