/kernel/classes/ezcontentclass.php
PHP | 2017 lines | 1429 code | 221 blank | 367 comment | 175 complexity | f66e314d69358035ce6717cc0c434652 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 eZContentClass class
- //
- // Created on: <16-Apr-2002 11:08:14 amos>
- //
- // ## 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 eZContentClass ezcontentclass.php
- \ingroup eZKernel
- \brief Handles eZ Publish content classes
- \sa eZContentObject
- */
- class eZContentClass extends eZPersistentObject
- {
- const VERSION_STATUS_DEFINED = 0;
- const VERSION_STATUS_TEMPORARY = 1;
- const VERSION_STATUS_MODIFIED = 2;
- /**
- * Max length of content object name.
- * @var int
- */
- const CONTENT_OBJECT_NAME_MAX_LENGTH = 255;
- function eZContentClass( $row )
- {
- if ( is_array( $row ) )
- {
- $this->eZPersistentObject( $row );
- $this->VersionCount = false;
- $this->InGroups = null;
- $this->AllGroups = null;
- if ( isset( $row["version_count"] ) )
- $this->VersionCount = $row["version_count"];
- $this->NameList = new eZContentClassNameList();
- if ( isset( $row['serialized_name_list'] ) )
- $this->NameList->initFromSerializedList( $row['serialized_name_list'] );
- else
- $this->NameList->initDefault();
- $this->DescriptionList = new eZSerializedObjectNameList();
- if ( isset( $row['serialized_description_list'] ) )
- $this->DescriptionList->initFromSerializedList( $row['serialized_description_list'] );
- else
- $this->DescriptionList->initDefault();
- }
- $this->DataMap = false;
- }
- static function definition()
- {
- static $definition = array( "fields" => array( "id" => array( 'name' => 'ID',
- 'datatype' => 'integer',
- 'default' => 0,
- 'required' => true ),
- "version" => array( 'name' => 'Version',
- 'datatype' => 'integer',
- 'default' => 0,
- 'required' => true ),
- "serialized_name_list" => array( 'name' => 'SerializedNameList',
- 'datatype' => 'string',
- 'default' => '',
- 'required' => true ),
- 'serialized_description_list' => array( 'name' => 'SerializedDescriptionList',
- 'datatype' => 'string',
- 'default' => '',
- 'required' => true ),
- "identifier" => array( 'name' => "Identifier",
- 'datatype' => 'string',
- 'default' => '',
- 'required' => true ),
- "contentobject_name" => array( 'name' => "ContentObjectName",
- 'datatype' => 'string',
- 'default' => '',
- 'required' => true ),
- "url_alias_name" => array( 'name' => "URLAliasName",
- 'datatype' => 'string',
- 'default' => '',
- 'required' => false ),
- "creator_id" => array( 'name' => "CreatorID",
- 'datatype' => 'integer',
- 'default' => 0,
- 'required' => true,
- 'foreign_class' => 'eZUser',
- 'foreign_attribute' => 'contentobject_id',
- 'multiplicity' => '1..*' ),
- "modifier_id" => array( 'name' => "ModifierID",
- 'datatype' => 'integer',
- 'default' => 0,
- 'required' => true,
- 'foreign_class' => 'eZUser',
- 'foreign_attribute' => 'contentobject_id',
- 'multiplicity' => '1..*' ),
- "created" => array( 'name' => "Created",
- 'datatype' => 'integer',
- 'default' => 0,
- 'required' => true ),
- "remote_id" => array( 'name' => "RemoteID",
- 'datatype' => 'string',
- 'default' => '',
- 'required' => true ),
- "modified" => array( 'name' => "Modified",
- 'datatype' => 'integer',
- 'default' => 0,
- 'required' => true ),
- "is_container" => array( 'name' => "IsContainer",
- 'datatype' => 'integer',
- 'default' => 0,
- 'required' => true ),
- 'always_available' => array( 'name' => "AlwaysAvailable",
- 'datatype' => 'integer',
- 'default' => 0,
- '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..*' ),
- 'sort_field' => array( 'name' => 'SortField',
- 'datatype' => 'integer',
- 'default' => 1,
- 'required' => true ),
- 'sort_order' => array( 'name' => 'SortOrder',
- 'datatype' => 'integer',
- 'default' => 1,
- 'required' => true ) ),
- "keys" => array( "id", "version" ),
- "function_attributes" => array( "data_map" => "dataMap",
- 'object_count' => 'objectCount',
- 'object_list' => 'objectList',
- 'version_count' => 'versionCount',
- 'version_status' => 'versionStatus',
- 'remote_id' => 'remoteID', // Note: This overrides remote_id field
- 'ingroup_list' => 'fetchGroupList',
- 'ingroup_id_list' => 'fetchGroupIDList',
- 'match_ingroup_id_list' => 'fetchMatchGroupIDList',
- 'group_list' => 'fetchAllGroups',
- 'creator' => 'creator',
- 'modifier' => 'modifier',
- 'can_instantiate_languages' => 'canInstantiateLanguages',
- 'name' => 'name',
- 'nameList' => 'nameList',
- 'description' => 'description',
- 'descriptionList' => 'descriptionList',
- 'languages' => 'languages',
- 'prioritized_languages' => 'prioritizedLanguages',
- 'prioritized_languages_js_array' => 'prioritizedLanguagesJsArray',
- 'can_create_languages' => 'canCreateLanguages',
- 'top_priority_language_locale' => 'topPriorityLanguageLocale',
- 'always_available_language' => 'alwaysAvailableLanguage' ),
- 'set_functions' => array( 'name' => 'setName' ),
- "increment_key" => "id",
- "class_name" => "eZContentClass",
- "sort" => array( "id" => "asc" ),
- "name" => "ezcontentclass" );
- return $definition;
- }
- function __clone()
- {
- unset( $this->Version );
- unset( $this->InGroups );
- unset( $this->AllGroups );
- unset( $this->CanInstantiateLanguages );
- unset( $this->VersionCount );
- $this->ID = null;
- $this->RemoteID = eZRemoteIdUtility::generate( 'class' );
- }
- /*!
- Creates an 'eZContentClass' object.
- To specify contentclass name use either $optionalValues['serialized_name_list'] or
- combination of $optionalValues['name'] and/or $languageLocale.
- In case of conflict(when both 'serialized_name_list' and 'name' with/without $languageLocale
- are specified) 'serialized_name_list' has top priority. This means that 'name' and
- $languageLocale will be ingnored because 'serialized_name_list' already has all needed info
- about names and languages.
- If 'name' is specified then the contentclass will have a name in $languageLocale(if specified) or
- in default language.
- If neither of 'serialized_name_list' or 'name' isn't specified then the contentclass will have an empty
- name in 'languageLocale'(if specified) or in default language.
- 'language_mask' and 'initial_language_id' attributes will be set according to specified(either
- in 'serialized_name_list' or by $languageLocale) languages.
- \return 'eZContentClass' object.
- */
- static function create( $userID = false, $optionalValues = array(), $languageLocale = false )
- {
- $dateTime = time();
- if ( !$userID )
- $userID = eZUser::currentUserID();
- $nameList = new eZContentClassNameList();
- if ( isset( $optionalValues['serialized_name_list'] ) )
- $nameList->initFromSerializedList( $optionalValues['serialized_name_list'] );
- else if ( isset( $optionalValues['name'] ) )
- $nameList->initFromString( $optionalValues['name'], $languageLocale );
- else
- $nameList->initFromString( '', $languageLocale );
- $descriptionList = new eZSerializedObjectNameList();
- if ( isset( $optionalValues['serialized_description_list'] ) )
- $descriptionList->initFromSerializedList( $optionalValues['serialized_description_list'] );
- else if ( isset( $optionalValues['description'] ) )
- $descriptionList->initFromString( $optionalValues['description'], $languageLocale );
- else
- $descriptionList->initFromString( '', $languageLocale );
- $languageMask = $nameList->languageMask();
- $initialLanguageID = $nameList->alwaysAvailableLanguageID();
- $contentClassDefinition = eZContentClass::definition();
- $row = array(
- "id" => null,
- "version" => 1,
- "serialized_name_list" => $nameList->serializeNames(),
- 'serialized_description_list' => $descriptionList->serializeNames(),
- "identifier" => "",
- "contentobject_name" => "",
- "creator_id" => $userID,
- "modifier_id" => $userID,
- "created" => $dateTime,
- 'remote_id' => eZRemoteIdUtility::generate( 'class' ),
- "modified" => $dateTime,
- "is_container" => $contentClassDefinition[ 'fields' ][ 'is_container' ][ 'default' ],
- "always_available" => $contentClassDefinition[ 'fields' ][ 'always_available' ][ 'default' ],
- 'language_mask' => $languageMask,
- 'initial_language_id' => $initialLanguageID,
- "sort_field" => $contentClassDefinition[ 'fields' ][ 'sort_field' ][ 'default' ],
- "sort_order" => $contentClassDefinition[ 'fields' ][ 'sort_order' ][ 'default' ] );
- $row = array_merge( $row, $optionalValues );
- $contentClass = new eZContentClass( $row );
- // setting 'dirtyData' to make sure the 'NameList' will be stored into db.
- $contentClass->NameList->setHasDirtyData( true );
- return $contentClass;
- }
- function instantiateIn( $lang, $userID = false, $sectionID = 1, $versionNumber = false, $versionStatus = eZContentObjectVersion::STATUS_INTERNAL_DRAFT )
- {
- return eZContentClass::instantiate( $userID, $sectionID, $versionNumber, $lang, $versionStatus );
- }
- /*!
- Creates a new content object instance and stores it.
- \param userID user ID (optional), current user if not set (also store object id in session if $userID = false)
- \param sectionID section ID (optional), 1 if not set (Standard section)
- \param versionNumber version number, create initial version if not set
- \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 instantiate( $userID = false, $sectionID = 1, $versionNumber = false, $languageCode = false, $versionStatus = eZContentObjectVersion::STATUS_INTERNAL_DRAFT )
- {
- $attributes = $this->fetchAttributes();
- if ( $userID === false )
- {
- $user = eZUser::currentUser();
- $userID = $user->attribute( 'contentobject_id' );
- }
- if ( $languageCode == false )
- {
- $languageCode = eZContentObject::defaultLanguage();
- }
- $object = eZContentObject::create( ezpI18n::tr( "kernel/contentclass", "New %1", null, array( $this->name( $languageCode ) ) ),
- $this->attribute( "id" ),
- $userID,
- $sectionID,
- 1,
- $languageCode );
- if ( $this->attribute( 'always_available' ) )
- {
- $object->setAttribute( 'language_mask', (int)$object->attribute( 'language_mask') | 1 );
- }
- $db = eZDB::instance();
- $db->begin();
- $object->store();
- $object->assignDefaultStates();
- $object->setName( ezpI18n::tr( "kernel/contentclass", "New %1", null, array( $this->name( $languageCode ) ) ), false, $languageCode );
- if ( !$versionNumber )
- {
- $version = $object->createInitialVersion( $userID, $languageCode );
- }
- else
- {
- $version = eZContentObjectVersion::create( $object->attribute( "id" ), $userID, $versionNumber, $languageCode );
- }
- if ( $versionStatus !== false )
- {
- $version->setAttribute( 'status', $versionStatus );
- }
- $version->store();
- foreach ( $attributes as $attribute )
- {
- $attribute->instantiate( $object->attribute( 'id' ), $languageCode );
- }
- if ( isset( $user ) && $user instanceof eZUser && $user->isAnonymous() )
- {
- $createdObjectIDList = eZPreferences::value( 'ObjectCreationIDList' );
- if ( !$createdObjectIDList )
- {
- $createdObjectIDList = array( $object->attribute( 'id' ) );
- }
- else
- {
- $createdObjectIDList = unserialize( $createdObjectIDList );
- $createdObjectIDList[] = $object->attribute( 'id' );
- }
- eZPreferences::setValue( 'ObjectCreationIDList', serialize( $createdObjectIDList ) );
- }
- $db->commit();
- return $object;
- }
- function canInstantiateClasses()
- {
- $accessResult = eZUser::currentUser()->hasAccessTo( 'content' , 'create' );
- $canInstantiateClasses = 1;
- if ( $accessResult['accessWord'] == 'no' )
- {
- $canInstantiateClasses = 0;
- }
- return $canInstantiateClasses;
- }
- // code-template::create-block: can-instantiate-class-list, group-filter, role-caching, class-policy-list, name-instantiate, object-creation, class-sql-creation, static-method
- // code-template::auto-generated:START can-instantiate-class-list
- // This code is automatically generated from templates/classcreatelist.ctpl
- // DO NOT EDIT THIS CODE DIRECTLY, CHANGE THE TEMPLATE FILE INSTEAD
- /*!
- \static
- Finds all classes that the current user can create objects from and returns.
- It is also possible to filter the list event more with \a $includeFilter and \a $groupList.
- \param $asObject If \c true then it return eZContentClass objects, if not it will
- be an associative array with \c name and \c id keys.
- \param $includeFilter If \c true then it will include only from class groups defined in
- \a $groupList, if not it will exclude those groups.
- \param $groupList An array with class group IDs that should be used in filtering, use
- \c false if you do not wish to filter at all.
- \param $fetchID A unique name for the current fetch, this must be supplied when filtering is
- used if you want caching to work.
- */
- static function canInstantiateClassList( $asObject = false, $includeFilter = true, $groupList = false, $fetchID = false )
- {
- $ini = eZINI::instance();
- $groupArray = array();
- $enableCaching = ( $ini->variable( 'RoleSettings', 'EnableCaching' ) == 'true' );
- if ( is_array( $groupList ) )
- {
- if ( $fetchID == false )
- $enableCaching = false;
- }
- if ( $enableCaching )
- {
- $http = eZHTTPTool::instance();
- eZExpiryHandler::registerShutdownFunction();
- $handler = eZExpiryHandler::instance();
- $expiredTimeStamp = 0;
- if ( $handler->hasTimestamp( 'user-class-cache' ) )
- $expiredTimeStamp = $handler->timestamp( 'user-class-cache' );
- $classesCachedForUser = $http->sessionVariable( 'ClassesCachedForUser' );
- $classesCachedTimestamp = $http->sessionVariable( 'ClassesCachedTimestamp' );
- $cacheVar = 'CanInstantiateClassList';
- if ( is_array( $groupList ) and $fetchID !== false )
- {
- $cacheVar = 'CanInstantiateClassListGroup';
- }
- $user = eZUser::currentUser();
- $userID = $user->id();
- if ( ( $classesCachedTimestamp >= $expiredTimeStamp ) && $classesCachedForUser == $userID )
- {
- if ( $http->hasSessionVariable( $cacheVar ) )
- {
- if ( $fetchID !== false )
- {
- // Check if the group contains our ID, if not we need to fetch from DB
- $groupArray = $http->sessionVariable( $cacheVar );
- if ( isset( $groupArray[$fetchID] ) )
- {
- return $groupArray[$fetchID];
- }
- }
- else
- {
- return $http->sessionVariable( $cacheVar );
- }
- }
- }
- else
- {
- $http->setSessionVariable( 'ClassesCachedForUser' , $userID );
- $http->setSessionVariable( 'ClassesCachedTimestamp', time() );
- }
- }
- $languageCodeList = eZContentLanguage::fetchLocaleList();
- $allowedLanguages = array( '*' => array() );
- $user = eZUser::currentUser();
- $accessResult = $user->hasAccessTo( 'content' , 'create' );
- $accessWord = $accessResult['accessWord'];
- $classIDArray = array();
- $classList = array();
- $fetchAll = false;
- if ( $accessWord == 'yes' )
- {
- $fetchAll = true;
- $allowedLanguages['*'] = $languageCodeList;
- }
- else if ( $accessWord == 'no' )
- {
- // Cannot create any objects, return empty list.
- return $classList;
- }
- else
- {
- $policies = $accessResult['policies'];
- foreach ( $policies as $policyKey => $policy )
- {
- $classIDArrayPart = '*';
- if ( isset( $policy['Class'] ) )
- {
- $classIDArrayPart = $policy['Class'];
- }
- $languageCodeArrayPart = $languageCodeList;
- if ( isset( $policy['Language'] ) )
- {
- $languageCodeArrayPart = array_intersect( $policy['Language'], $languageCodeList );
- }
- if ( $classIDArrayPart == '*' )
- {
- $fetchAll = true;
- $allowedLanguages['*'] = array_unique( array_merge( $allowedLanguages['*'], $languageCodeArrayPart ) );
- }
- else
- {
- foreach( $classIDArrayPart as $class )
- {
- if ( isset( $allowedLanguages[$class] ) )
- {
- $allowedLanguages[$class] = array_unique( array_merge( $allowedLanguages[$class], $languageCodeArrayPart ) );
- }
- else
- {
- $allowedLanguages[$class] = $languageCodeArrayPart;
- }
- }
- $classIDArray = array_merge( $classIDArray, array_diff( $classIDArrayPart, $classIDArray ) );
- }
- }
- }
- $db = eZDB::instance();
- $filterTableSQL = '';
- $filterSQL = '';
- // Create extra SQL statements for the class group filters.
- if ( is_array( $groupList ) )
- {
- if ( count( $groupList ) == 0 )
- {
- return $classList;
- }
- $filterTableSQL = ', ezcontentclass_classgroup ccg';
- $filterSQL = ( " AND" .
- " cc.id = ccg.contentclass_id AND" .
- " " );
- $filterSQL .= $db->generateSQLINStatement( $groupList, 'ccg.group_id', !$includeFilter, true, 'int' );
- }
- $classNameFilter = eZContentClassName::sqlFilter( 'cc' );
- $filterSQL .= " AND cc.id=" . $classNameFilter['from'] . ".contentclass_id";
- if ( $fetchAll )
- {
- // If $asObject is true we fetch all fields in class
- $fields = $asObject ? "cc.*, $classNameFilter[nameField]" : "cc.id, $classNameFilter[nameField]";
- $rows = $db->arrayQuery( "SELECT DISTINCT $fields " .
- "FROM ezcontentclass cc$filterTableSQL, $classNameFilter[from] " .
- "WHERE cc.version = " . eZContentClass::VERSION_STATUS_DEFINED . " $filterSQL " .
- "ORDER BY $classNameFilter[nameField] ASC" );
- $classList = eZPersistentObject::handleRows( $rows, 'eZContentClass', $asObject );
- }
- else
- {
- // If the constrained class list is empty we are not allowed to create any class
- if ( count( $classIDArray ) == 0 )
- {
- return $classList;
- }
- $classIDCondition = $db->generateSQLINStatement( $classIDArray, 'cc.id' );
- // If $asObject is true we fetch all fields in class
- $fields = $asObject ? "cc.*, $classNameFilter[nameField]" : "cc.id, $classNameFilter[nameField]";
- $rows = $db->arrayQuery( "SELECT DISTINCT $fields " .
- "FROM ezcontentclass cc$filterTableSQL, $classNameFilter[from] " .
- "WHERE $classIDCondition AND" .
- " cc.version = " . eZContentClass::VERSION_STATUS_DEFINED . " $filterSQL " .
- "ORDER BY $classNameFilter[nameField] ASC" );
- $classList = eZPersistentObject::handleRows( $rows, 'eZContentClass', $asObject );
- }
- if ( $asObject )
- {
- foreach ( $classList as $key => $class )
- {
- $id = $class->attribute( 'id' );
- if ( isset( $allowedLanguages[$id] ) )
- {
- $languageCodes = array_unique( array_merge( $allowedLanguages['*'], $allowedLanguages[$id] ) );
- }
- else
- {
- $languageCodes = $allowedLanguages['*'];
- }
- $classList[$key]->setCanInstantiateLanguages( $languageCodes );
- }
- }
- eZDebugSetting::writeDebug( 'kernel-content-class', $classList, "class list fetched from db" );
- if ( $enableCaching )
- {
- if ( $fetchID !== false )
- {
- $groupArray[$fetchID] = $classList;
- $http->setSessionVariable( $cacheVar, $groupArray );
- }
- else
- {
- $http->setSessionVariable( $cacheVar, $classList );
- }
- }
- return $classList;
- }
- // This code is automatically generated from templates/classcreatelist.ctpl
- // code-template::auto-generated:END can-instantiate-class-list
- /*!
- \return The creator of the class as an eZUser object by using the $CreatorID as user ID.
- */
- function creator()
- {
- if ( isset( $this->CreatorID ) and $this->CreatorID )
- {
- return eZUser::fetch( $this->CreatorID );
- }
- return null;
- }
- /*!
- \return The modifier of the class as an eZUser object by using the $ModifierID as user ID.
- */
- function modifier()
- {
- if ( isset( $this->ModifierID ) and $this->ModifierID )
- {
- return eZUser::fetch( $this->ModifierID );
- }
- return null;
- }
- /*!
- Find all groups the current class is placed in and returns a list of group objects.
- \return An array with eZContentClassGroup objects.
- \sa fetchGroupIDList()
- */
- function fetchGroupList()
- {
- $this->InGroups = eZContentClassClassGroup::fetchGroupList( $this->attribute( "id" ),
- $this->attribute( "version" ),
- true );
- return $this->InGroups;
- }
- /*!
- Find all groups the current class is placed in and returns a list of group IDs.
- \return An array with integers (ids).
- \sa fetchGroupList()
- */
- function fetchGroupIDList()
- {
- $list = eZContentClassClassGroup::fetchGroupList( $this->attribute( "id" ),
- $this->attribute( "version" ),
- false );
- $this->InGroupIDs = array();
- foreach ( $list as $item )
- {
- $this->InGroupIDs[] = $item['group_id'];
- }
- return $this->InGroupIDs;
- }
- /*!
- Returns the result from fetchGroupIDList() if class group overrides is
- enabled in content.ini.
- \return An array with eZContentClassGroup objects or \c false if disabled.
- \note \c EnableClassGroupOverride in group \c ContentOverrideSettings from INI file content.ini
- controls this behaviour.
- */
- function fetchMatchGroupIDList()
- {
- $contentINI = eZINI::instance( 'content.ini' );
- if( $contentINI->variable( 'ContentOverrideSettings', 'EnableClassGroupOverride' ) == 'true' )
- {
- return $this->attribute( 'ingroup_id_list' );
- }
- return false;
- }
- /*!
- Finds all Classes in the system and returns them.
- \return An array with eZContentClass objects.
- */
- static function fetchAllClasses( $asObject = true, $includeFilter = true, $groupList = false )
- {
- $filterTableSQL = '';
- $filterSQL = '';
- if ( is_array( $groupList ) )
- {
- $filterTableSQL = ', ezcontentclass_classgroup ccg';
- $filterSQL = ( " AND" .
- " cc.id = ccg.contentclass_id AND" .
- " ccg.group_id " );
- $groupText = implode( ', ', $groupList );
- if ( $includeFilter )
- $filterSQL .= "IN ( $groupText )";
- else
- $filterSQL .= "NOT IN ( $groupText )";
- }
- $classNameFilter = eZContentClassName::sqlFilter( 'cc' );
- $classList = array();
- $db = eZDb::instance();
- // If $asObject is true we fetch all fields in class
- $fields = $asObject ? "cc.*" : "cc.id, $classNameFilter[nameField]";
- $rows = $db->arrayQuery( "SELECT DISTINCT $fields " .
- "FROM ezcontentclass cc$filterTableSQL, $classNameFilter[from] " .
- "WHERE cc.version = " . eZContentClass::VERSION_STATUS_DEFINED . "$filterSQL AND $classNameFilter[where]" .
- "ORDER BY $classNameFilter[nameField] ASC" );
- $classList = eZPersistentObject::handleRows( $rows, 'eZContentClass', $asObject );
- return $classList;
- }
- /*!
- Finds all Class groups in the system and returns them.
- \return An array with eZContentClassGroup objects.
- \sa fetchGroupList(), fetchGroupIDList()
- */
- function fetchAllGroups()
- {
- $this->AllGroups = eZContentClassGroup::fetchList();
- return $this->AllGroups;
- }
- /*!
- \return true if the class is part of the group \a $groupID
- */
- function inGroup( $groupID )
- {
- return eZContentClassClassGroup::classInGroup( $this->attribute( 'id' ),
- $this->attribute( 'version' ),
- $groupID );
- }
- /*!
- \static
- Will remove all temporary classes from the database.
- */
- static function removeTemporary()
- {
- $version = eZContentClass::VERSION_STATUS_TEMPORARY;
- $temporaryClasses = eZContentClass::fetchList( $version, true );
- $db = eZDB::instance();
- $db->begin();
- foreach ( $temporaryClasses as $class )
- {
- $class->remove( true, $version );
- }
- eZPersistentObject::removeObject( eZContentClassAttribute::definition(),
- array( 'version' => $version ) );
- $db->commit();
- }
- /*!
- Get remote id of content node
- */
- function remoteID()
- {
- $remoteID = eZPersistentObject::attribute( 'remote_id', true );
- if ( !$remoteID &&
- $this->Version == eZContentClass::VERSION_STATUS_DEFINED )
- {
- $this->setAttribute( 'remote_id', eZRemoteIdUtility::generate( 'class' ) );
- $this->sync( array( 'remote_id' ) );
- $remoteID = eZPersistentObject::attribute( 'remote_id', true );
- }
- return $remoteID;
- }
- /*!
- \note If you want to remove a class with all data associated with it (objects/classMembers)
- you should use eZContentClassOperations::remove()
- */
- function remove( $removeAttributes = false, $version = eZContentClass::VERSION_STATUS_DEFINED )
- {
- // If we are not allowed to remove just return false
- if ( $this->Version == eZContentClass::VERSION_STATUS_DEFINED && !$this->isRemovable() )
- return false;
- if ( is_array( $removeAttributes ) or $removeAttributes )
- $this->removeAttributes( $removeAttributes );
- $this->NameList->remove( $this );
- eZPersistentObject::remove();
- }
- /*!
- Checks if the class can be removed and returns \c true if it can, \c false otherwise.
- \sa removableInformation()
- */
- function isRemovable()
- {
- $info = $this->removableInformation( false );
- return count( $info['list'] ) == 0;
- }
- /*!
- Returns information on why the class cannot be removed,
- it does the same checks as in isRemovable() but generates
- some text in the return array.
- \return An array which contains:
- - text - Plain text description why this cannot be removed
- - list - An array with reasons why this failed, each entry contains:
- - text - Plain text description of the reason.
- - list - A sublist of reason (e.g from an attribute), is optional.
- \param $includeAll Controls whether the returned information will contain all
- sources for not being to remove or just the first that it finds.
- */
- function removableInformation( $includeAll = true )
- {
- $result = array( 'text' => ezpI18n::tr( 'kernel/contentclass', "Cannot remove class '%class_name':",
- null, array( '%class_name' => $this->attribute( 'name' ) ) ),
- 'list' => array() );
- $db = eZDB::instance();
- // Check top-level nodes
- $rows = $db->arrayQuery( "SELECT ezcot.node_id
- FROM ezcontentobject_tree ezcot, ezcontentobject ezco
- WHERE ezcot.depth = 1 AND
- ezco.contentclass_id = $this->ID AND
- ezco.id=ezcot.contentobject_id" );
- if ( count( $rows ) > 0 )
- {
- $result['list'][] = array( 'text' => ezpI18n::tr( 'kernel/contentclass', 'The class is used by a top-level node and cannot be removed.
- You will need to change the class of the node by using the swap functionality.' ) );
- if ( !$includeAll )
- return $result;
- }
- // Check class attributes
- foreach ( $this->fetchAttributes() as $attribute )
- {
- $dataType = $attribute->dataType();
- if ( !$dataType->isClassAttributeRemovable( $attribute ) )
- {
- $info = $dataType->classAttributeRemovableInformation( $attribute, $includeAll );
- $result['list'][] = $info;
- if ( !$includeAll )
- return $result;
- }
- }
- return $result;
- }
- /*!
- \note Removes class attributes
- \param removeAttributes Array of attributes to remove
- \param version Version to remove( optional )
- */
- function removeAttributes( $removeAttributes = false, $version = false )
- {
- if ( is_array( $removeAttributes ) )
- {
- $db = eZDB::instance();
- $db->begin();
- foreach( $removeAttributes as $attribute )
- {
- $attribute->removeThis();
- }
- $db->commit();
- }
- else
- {
- $contentClassID = $this->ID;
- if ( $version === false )
- {
- $version = $this->Version;
- }
- $classAttributes = $this->fetchAttributes( );
- $db = eZDB::instance();
- $db->begin();
- foreach ( $classAttributes as $classAttribute )
- {
- $dataType = $classAttribute->dataType();
- $dataType->deleteStoredClassAttribute( $classAttribute, $version );
- }
- eZPersistentObject::removeObject( eZContentClassAttribute::definition(),
- array( 'contentclass_id' => $contentClassID,
- 'version' => $version ) );
- $db->commit();
- }
- }
- function compareAttributes( $attr1, $attr2 )
- {
- return ( $attr1->attribute( "placement" ) > $attr2->attribute( "placement" ) ) ? 1 : -1;
- }
- function adjustAttributePlacements( $attributes )
- {
- if ( !is_array( $attributes ) )
- return;
- usort( $attributes, array( $this, "compareAttributes" ) );
- $i = 0;
- foreach( $attributes as $attribute )
- {
- $attribute->setAttribute( "placement", ++$i );
- }
- }
- function store( $store_childs = false, $fieldFilters = null )
- {
- self::expireCache();
- $db = eZDB::instance();
- $db->begin();
- if ( is_array( $store_childs ) ||
- $store_childs )
- {
- if ( is_array( $store_childs ) )
- {
- $attributes = $store_childs;
- }
- else
- {
- $attributes = $this->fetchAttributes();
- }
- foreach( $attributes as $attribute )
- {
- if ( is_object ( $attribute ) )
- $attribute->store();
- }
- }
- eZExpiryHandler::registerShutdownFunction();
- $handler = eZExpiryHandler::instance();
- $handler->setTimestamp( 'user-class-cache', time() );
- $handler->store();
- $this->setAttribute( 'serialized_name_list', $this->NameList->serializeNames() );
- $this->setAttribute( 'serialized_description_list', $this->DescriptionList->serializeNames() );
- eZPersistentObject::store( $fieldFilters );
- $this->NameList->store( $this );
- $db->commit();
- }
- function sync( $fieldFilters = null )
- {
- if ( $this->hasDirtyData() )
- $this->store( false, $fieldFilters );
- }
- /*!
- Initializes this class as a copy of \a $originalClass by
- creating new a new name and identifier.
- It will check if there are other classes already with this name
- in which case it will append a unique number to the name and identifier.
- */
- function initializeCopy( &$originalClass )
- {
- $name = ezpI18n::tr( 'kernel/class', 'Copy of %class_name', null,
- array( '%class_name' => $originalClass->attribute( 'name' ) ) );
- $identifier = 'copy_of_' . $originalClass->attribute( 'identifier' );
- $db = eZDB::instance();
- $sql = "SELECT count( ezcontentclass_name.name ) AS count FROM ezcontentclass, ezcontentclass_name WHERE ezcontentclass.id = ezcontentclass_name.contentclass_id AND ezcontentclass_name.name like '" . $db->escapeString( $name ) . "%'";
- $rows = $db->arrayQuery( $sql );
- $count = $rows[0]['count'];
- if ( $count > 0 )
- {
- ++$count;
- $name .= $count;
- $identifier .= $count;
- }
- $this->setName( $name );
- $this->setAttribute( 'identifier', $identifier );
- $this->setAttribute( 'created', time() );
- $user = eZUser::currentUser();
- $userID = $user->attribute( "contentobject_id" );
- $this->setAttribute( 'creator_id', $userID );
- }
- /**
- * Stores the current class as a defined version, updates the contentobject_name
- * attribute and recreates the class group entries.
- *
- * @note It will remove any existing temporary or defined classes before storing.
- *
- * @param array $attributes array of attributes of the content class
- */
- public function storeDefined( $attributes )
- {
- $this->storeVersioned( $attributes, self::VERSION_STATUS_DEFINED );
- }
- /**
- * Stores the current class as a modified version, updates the contentobject_name
- * attribute and recreates the class group entries.
- *
- * @note It will remove classes in the previous and specified version before storing.
- *
- * @param array $attributes array of attributes
- * @param int $version version status
- * @since Version 4.3
- */
- public function storeVersioned( $attributes, $version )
- {
- $previousVersion = $this->attribute( 'version' );
- $db = eZDB::instance();
- $db->begin();
- $this->removeAttributes( false, $version );
- $this->removeAttributes( false, $previousVersion );
- $this->remove( false );
- $this->setVersion( $version, $attributes );
- $this->setAttribute( "modifier_id", eZUser::currentUser()->attribute( "contentobject_id" ) );
- $this->setAttribute( "modified", time() );
- $this->adjustAttributePlacements( $attributes );
- foreach( $attributes as $attribute )
- {
- $attribute->storeVersioned( $version );
- }
- // Set contentobject_name to something sensible if it is missing
- if ( count( $attributes ) > 0 && trim( $this->attribute( 'contentobject_name' ) ) == '' )
- {
- $this->setAttribute( 'contentobject_name', '<' . $attributes[0]->attribute( 'identifier' ) . '>' );
- }
- // Recreate class member entries
- eZContentClassClassGroup::removeClassMembers( $this->ID, $version );
- foreach( eZContentClassClassGroup::fetchGroupList( $this->ID, $previousVersion ) as $classgroup )
- {
- $classgroup->setAttribute( 'contentclass_version', $version );
- $classgroup->store();
- }
- eZContentClassClassGroup::removeClassMembers( $this->ID, $previousVersion );
- eZExpiryHandler::registerShutdownFunction();
- $handler = eZExpiryHandler::instance();
- $time = time();
- $handler->setTimestamp( 'user-class-cache', $time );
- $handler->setTimestamp( 'class-identifier-cache', $time );
- $handler->setTimestamp( 'sort-key-cache', $time );
- $handler->store();
- eZContentCacheManager::clearAllContentCache();
- $this->setAttribute( 'serialized_name_list', $this->NameList->serializeNames() );
- $this->setAttribute( 'serialized_description_list', $this->DescriptionList->serializeNames() );
- eZPersistentObject::store();
- $this->NameList->store( $this );
- $db->commit();
- }
- function setVersion( $version, $set_childs = false )
- {
- if ( is_array( $set_childs ) or $set_childs )
- {
- if ( is_array( $set_childs ) )
- {
- $attributes = $set_childs;
- }
- else
- {
- $attributes = $this->fetchAttributes();
- }
- foreach( $attributes as $attribute )
- {
- $attribute->setAttribute( "version", $version );
- }
- }
- if ( $this->Version != $version )
- $this->NameList->setHasDirtyData();
- $this->setAttribute( "version", $version );
- }
- static function exists( $id, $version = eZContentClass::VERSION_STATUS_DEFINED, $userID = false, $useIdentifier = false )
- {
- $conds = array( "version" => $version );
- if ( $useIdentifier )
- $conds["identifier"] = $id;
- else
- $conds["id"] = $id;
- if ( $userID !== false and is_numeric( $userID ) )
- $conds["creator_id"] = $userID;
- $version_sort = "desc";
- if ( $version == eZContentClass::VERSION_STATUS_DEFINED )
- $conds['version'] = $version;
- $rows = eZPersistentObject::fetchObjectList( eZContentClass::definition(),
- null,
- $conds,
- null,
- array( "offset" => 0,
- "length" => 1 ),
- false );
- if ( count( $rows ) > 0 )
- return $rows[0]['id'];
- return false;
- }
- static function fetch( $id, $asObject = true, $version = eZContentClass::VERSION_STATUS_DEFINED, $user_id = false ,$parent_id = null )
- {
- global $eZContentClassObjectCache;
- // 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( $eZContentClassObjectCache[$id] ) or $asObject === false or $version != eZContentClass::VERSION_STATUS_DEFINED )
- {
- $conds = array( "id" => $id,
- "version" => $version );
- if ( $user_id !== false and is_numeric( $user_id ) )
- $conds["creator_id"] = $user_id;
- $version_sort = "desc";
- if ( $version == eZContentClass::VERSION_STATUS_DEFINED )
- $version_sort = "asc";
- $rows = eZPersistentObject::fetchObjectList( eZContentClass::definition(),
- null,
- $conds,
- array( "version" => $version_sort ),
- array( "offset" => 0,
- "length" => 2 ),
- false );
- if ( count( $rows ) == 0 )
- {
- $contentClass = null;
- return $contentClass;
- }
- $row = $rows[0];
- $row["version_count"] = count( $rows );
- if ( $asObject )
- {
- $contentClass = new eZContentClass( $row );
- if ( $version == eZContentClass::VERSION_STATUS_DEFINED )
- {
- $eZContentClassObjectCache[$id] = $contentClass;
- }
- return $contentClass;
- }
- else
- $contentClass = $row;
- }
- else
- {
- $contentClass = $eZContentClassObjectCache[$id];
- }
- return $contentClass;
- }
- static function fetchByRemoteID( $remoteID, $asObject = true, $version = eZContentClass::VERSION_STATUS_DEFINED, $user_id = false ,$parent_id = null )
- {
- $conds = array( "remote_id" => $remoteID,
- …
Large files files are truncated, but you can click here to view the full file