/kernel/classes/ezcontentobject.php
PHP | 6749 lines | 4665 code | 620 blank | 1464 comment | 641 complexity | ba99b8ac2d292de877c0dd752e7090bc MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
Large files files are truncated, but you can click here to view the full file
- <?php
- /**
- * File containing the eZContentObject class.
- *
- * @copyright Copyright (C) eZ Systems AS. All rights reserved.
- * @license For full copyright and license information view LICENSE file distributed with this source code.
- * @version //autogentag//
- * @package kernel
- */
- /**
- * Encapsulates data about and methods to work with content objects
- *
- * @package kernel
- */
- 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;
- /**
- * Initializes the object with $row.
- *
- * If $row is an integer, it will try to fetch it from the database using it as the unique ID.
- *
- * @param int|array $row
- */
- public function __construct( $row )
- {
- parent::__construct( $row );
- $this->ClassIdentifier = false;
- if ( isset( $row['contentclass_identifier'] ) )
- $this->ClassIdentifier = $row['contentclass_identifier'];
- if ( isset( $row['class_identifier'] ) )
- $this->ClassIdentifier = $row['class_identifier'];
- $this->ClassName = false;
- // Depending on how the information is retrieved, the "serialized_name_list" is sometimes available in "class_serialized_name_list" key
- if ( isset( $row['class_serialized_name_list'] ) )
- $row['serialized_name_list'] = $row['class_serialized_name_list'];
- // Depending on how the information is retrieved, the "contentclass_name" is sometimes available in "class_name" key
- if ( isset( $row['class_name'] ) )
- $row['contentclass_name'] = $row['class_name'];
- 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' );
- }
- }
- // Initialize the permission array cache
- $this->Permissions = array();
- }
- /**
- * @deprecated Use eZContentObject::__construct() instead
- * @param int|array $row
- */
- function eZContentObject( $row )
- {
- self::__construct( $row );
- }
- 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",
- "published_version" => "publishedVersion",
- '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",
- "visible_nodes" => "visibleNodes",
- "has_visible_node" => "hasVisibleNode",
- "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',
- 'section_identifier' => 'sectionIdentifier' ),
- "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, otherwise false
- *
- * @return array|bool
- */
- 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;
- }
- /**
- * Stores the object
- *
- * 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.
- *
- * @param array $fieldFilters
- */
- 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();
- parent::store( $fieldFilters );
- $db->commit();
- }
- /**
- * Clear in-memory caches.
- *
- * If $idArray is ommitted, the caches are cleared for all objects.
- *
- * @param array $idArray Objects to clear caches for.
- */
- 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
- *
- * 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();
- }
- }
- /**
- * Returns an object's name for the version given by $version in the language given by $lang
- *
- * @param int|bool $version If omitted, the current version will be used
- * @param bool $lang If omitted, the initial object language will be used
- * @return bool|string
- */
- 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 );
- }
- /**
- * Returns all translations of the current object's name
- *
- * @return string[]
- */
- 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;
- }
- /**
- * Returns the object name for version $version in the language $lang
- *
- * @param int $version
- * @param string|bool $lang If omitted, the initial language of the object is used
- * @return string|bool
- */
- 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 {@see setName()} to change it permanently
- *
- * @param string $name
- */
- function setCachedName( $name )
- {
- $this->Name = $name;
- }
- /**
- * Sets the name of the object in all translations.
- *
- * 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.
- *
- * @param string $objectName
- * @param int|bool $versionNum
- * @param int|bool $languageCode
- */
- 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.
- *
- * @return eZContentObjectAttribute[]
- */
- 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.
- *
- * 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.
- *
- * Result is not cached, so make sure you don't call this over and over.
- *
- * @param eZContentObjectAttribute[] $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;
- }
- /**
- * Returns a map with all the content object attributes where the keys are the attribute identifiers.
- *
- * @param int|bool $version
- * @param string|bool $language
- * @return eZContentObjectAttribute[]
- */
- 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();
- /* @var eZContentObjectAttribute[] $data */
- 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;
- }
- /**
- * Resets (empties) the current object's data map
- *
- * @return array
- */
- function resetDataMap()
- {
- $this->ContentObjectAttributeArray = array();
- $this->ContentObjectAttributes = array();
- $this->DataMap = array();
- return $this->DataMap;
- }
- /**
- * Fetches a set of content object attributes by their class identifiers.
- *
- * @param string[] $identifierArray
- * @param int|bool $version
- * @param string[]|bool $languageArray
- * @param bool $asObject If true, returns an array of eZContentObjectAttributes, a normal array otherwise
- *
- * @return eZContentObjectAttribute[]|array|null
- */
- 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.
- *
- * @return eZContentObject|null
- */
- function owner()
- {
- if ( $this->OwnerID != 0 )
- {
- return eZContentObject::fetch( $this->OwnerID );
- }
- return null;
- }
- /**
- * Returns the content class group identifiers for the current content object
- *
- * @return array
- */
- function contentClassGroupIDList()
- {
- $contentClass = $this->contentClass();
- return $contentClass->attribute( 'ingroup_id_list' );
- }
- /**
- * Returns the content class identifer for the current content object
- *
- * The object will cache the class name information so multiple calls will be fast.
- *
- * @return string|bool|null
- */
- 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;
- }
- /**
- * Returns the content class for the current content object
- *
- * @return eZContentClass|null
- */
- function contentClass()
- {
- if ( !is_numeric( $this->ClassID ) )
- {
- $retValue = null;
- return $retValue;
- }
- return eZContentClass::fetch( $this->ClassID );
- }
- /**
- * Returns the remote id of the current content object
- *
- * @return string
- */
- function remoteID()
- {
- $remoteID = $this->attribute( 'remote_id', true );
- if ( !$remoteID )
- {
- $this->setAttribute( 'remote_id', eZRemoteIdUtility::generate( 'object' ) );
- if ( $this->attribute( 'id' ) !== null )
- $this->sync( array( 'remote_id' ) );
- $remoteID = $this->attribute( 'remote_id', true );
- }
- return $remoteID;
- }
- /**
- * Returns the ID of the the current object's main node
- *
- * @return int|null
- */
- function mainParentNodeID()
- {
- $list = eZContentObjectTreeNode::getParentNodeIdListByContentObjectID( $this->ID, false, true );
- return isset( $list[0] ) ? $list[0] : null;
- }
- /**
- * Returns a contentobject by remote ID
- *
- * @param string $remoteID
- * @param bool $asObject
- * @return eZContentObject|array|null
- */
- 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];
- }
- }
- /**
- * Fetches a content object by ID, using SELECT ... FOR UPDATE
- *
- * Ensures the table row is locked, blocking other transactions from locking it (read or write).
- * Usage must be within a transaction, or it will be locked for the current session.
- *
- * @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|mixed|null
- */
- static function fetchForUpdate( $id, $asObject = true )
- {
- global $eZContentObjectContentObjectCache;
- $db = eZDB::instance();
- $id = (int) $id;
- // Select for update, to lock the row
- $resArray = $db->arrayQuery( "SELECT * FROM ezcontentobject WHERE id='$id' FOR UPDATE" );
- if ( !is_array( $resArray ) || count( $resArray ) !== 1 )
- {
- eZDebug::writeError( "Object not found ($id)", __METHOD__ );
- return null;
- }
- $objectArray = $resArray[0];
- $classId = $objectArray['contentclass_id'];
- $contentClassResArray = $db->arrayQuery(
- "SELECT
- ezcontentclass.serialized_name_list as serialized_name_list,
- ezcontentclass.identifier as contentclass_identifier,
- ezcontentclass.is_container as is_container
- FROM
- ezcontentclass
- WHERE
- ezcontentclass.id='$classId' AND
- ezcontentclass.version=0"
- );
- $objectArray = array_merge($objectArray, $contentClassResArray[0]);
- if ( !$asObject )
- {
- return $objectArray;
- }
- $obj = new eZContentObject( $objectArray );
- $eZContentObjectContentObjectCache[$id] = $obj;
- return $obj;
- }
- /**
- * Returns true, if a content object with the ID $id exists, false otherwise
- *
- * @param int $id
- * @return bool
- */
- 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;
- }
- /**
- * Creates the SQL for fetching the object with ID $id and returns the string.
- *
- * @param int $id
- * @return 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.
- *
- * @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 eZContentObject|eZContentObject[]|array|null 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
- * @param bool|string $lang A language code to put at the top of the prioritized
- * languages list.
- *
- * @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 , $lang = false )
- {
- 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', 'language_id', 'language_mask', $lang )
- );
- $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'];
- if ( $lang !== false )
- {
- $eZContentObjectContentObjectCache[$objectID] = $obj;
- }
- $objectRetArray[$objectID] = $obj;
- }
- else
- {
- $objectRetArray[$objectID] = $resRow;
- }
- }
- return $objectRetArray;
- }
- /**
- * Returns an array of content objects.
- *
- * @param bool $asObject Whether to return objects or not
- * @param array|null $conditions Optional conditions to limit the fetch
- * @param int|bool $offset Where to start fetch from, set to false to skip it.
- * @param int|bool $limit Maximum number of objects to fetch, set false to skip it.
- * @return eZContentObject[]|array|null
- */
- 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 );
- }
- /**
- * Returns a filtered array of content objects.
- *
- * @param array|null $conditions Optional conditions to limit the fetch
- * @param int|bool $offset Where to start fetch from, set to false to skip it.
- * @param int|bool $limit Maximum number of objects to fetch, set false to skip it.
- * @param bool $asObject Whether to return objects or not
- * @return array|eZPersistentObject[]|null
- */
- 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 );
- }
- /**
- * Returns the number of objects in the database. Optionally \a $conditions can be used to limit the list count.
- * @param array|null $conditions
- * @return int
- */
- 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'];
- }
- /**
- * Returns an array of content objects with the content class id $contentClassID
- *
- * @param int $contentClassID
- * @param bool $asObject Whether to return objects or not
- * @param int|bool $offset Where to start fetch from, set to false to skip it.
- * @param int|bool $limit Maximum number of objects to fetch, set false to skip it.
- * @return eZCā¦
Large files files are truncated, but you can click here to view the full file