[ Index ]
 

Code source de eZ Publish 3.9.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/kernel/classes/ -> ezcontentclass.php (source)

   1  <?php
   2  //
   3  // Definition of eZContentClass class
   4  //
   5  // Created on: <16-Apr-2002 11:08:14 amos>
   6  //
   7  // SOFTWARE NAME: eZ publish
   8  // SOFTWARE RELEASE: 3.9.0
   9  // BUILD VERSION: 17785
  10  // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS
  11  // SOFTWARE LICENSE: GNU General Public License v2.0
  12  // NOTICE: >
  13  //   This program is free software; you can redistribute it and/or
  14  //   modify it under the terms of version 2.0  of the GNU General
  15  //   Public License as published by the Free Software Foundation.
  16  //
  17  //   This program is distributed in the hope that it will be useful,
  18  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  19  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20  //   GNU General Public License for more details.
  21  //
  22  //   You should have received a copy of version 2.0 of the GNU General
  23  //   Public License along with this program; if not, write to the Free
  24  //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25  //   MA 02110-1301, USA.
  26  //
  27  //
  28  
  29  /*!
  30    \class eZContentClass ezcontentclass.php
  31    \ingroup eZKernel
  32    \brief Handles eZ publish content classes
  33  
  34    \sa eZContentObject
  35  */
  36  
  37  include_once ( "lib/ezdb/classes/ezdb.php" );
  38  include_once ( "kernel/classes/ezpersistentobject.php" );
  39  include_once ( "kernel/classes/ezcontentobject.php" );
  40  include_once ( "kernel/classes/ezcontentclassattribute.php" );
  41  include_once ( "kernel/classes/ezcontentclassclassgroup.php" );
  42  include_once ( "kernel/classes/ezcontentclassnamelist.php" );
  43  include_once ( "kernel/common/i18n.php" );
  44  
  45  define( "EZ_CLASS_VERSION_STATUS_DEFINED", 0 );
  46  define( "EZ_CLASS_VERSION_STATUS_TEMPORARY", 1 );
  47  define( "EZ_CLASS_VERSION_STATUS_MODIFED", 2 );
  48  
  49  class eZContentClass extends eZPersistentObject
  50  {
  51      function eZContentClass( $row )
  52      {
  53          if ( is_array( $row ) )
  54          {
  55              $this->eZPersistentObject( $row );
  56              $this->VersionCount = false;
  57              $this->InGroups = null;
  58              $this->AllGroups = null;
  59              if ( isset( $row["version_count"] ) )
  60                  $this->VersionCount = $row["version_count"];
  61  
  62              $this->NameList = new eZContentClassNameList();
  63              if ( isset( $row['serialized_name_list'] ) )
  64                  $this->NameList->initFromSerializedList( $row['serialized_name_list'] );
  65              else
  66                  $this->NameList->initDefault();
  67          }
  68          $this->DataMap = false;
  69      }
  70  
  71      function definition()
  72      {
  73          return array( "fields" => array( "id" => array( 'name' => 'ID',
  74                                                          'datatype' => 'integer',
  75                                                          'default' => 0,
  76                                                          'required' => true ),
  77                                           "version" => array( 'name' => 'Version',
  78                                                               'datatype' => 'integer',
  79                                                               'default' => 0,
  80                                                               'required' => true ),
  81                                           "serialized_name_list" => array( 'name' => 'SerializedNameList',
  82                                                                            'datatype' => 'string',
  83                                                                            'default' => '',
  84                                                                            'required' => true ),
  85                                           "identifier" => array( 'name' => "Identifier",
  86                                                                  'datatype' => 'string',
  87                                                                  'default' => '',
  88                                                                  'required' => true ),
  89                                           "contentobject_name" => array( 'name' => "ContentObjectName",
  90                                                                          'datatype' => 'string',
  91                                                                          'default' => '',
  92                                                                          'required' => true ),
  93                                           "creator_id" => array( 'name' => "CreatorID",
  94                                                                  'datatype' => 'integer',
  95                                                                  'default' => 0,
  96                                                                  'required' => true,
  97                                                                  'foreign_class' => 'eZUser',
  98                                                                  'foreign_attribute' => 'contentobject_id',
  99                                                                  'multiplicity' => '1..*' ),
 100                                           "modifier_id" => array( 'name' => "ModifierID",
 101                                                                   'datatype' => 'integer',
 102                                                                   'default' => 0,
 103                                                                   'required' => true,
 104                                                                   'foreign_class' => 'eZUser',
 105                                                                   'foreign_attribute' => 'contentobject_id',
 106                                                                   'multiplicity' => '1..*' ),
 107                                           "created" => array( 'name' => "Created",
 108                                                               'datatype' => 'integer',
 109                                                               'default' => 0,
 110                                                               'required' => true ),
 111                                           "remote_id" => array( 'name' => "RemoteID",
 112                                                                 'datatype' => 'string',
 113                                                                 'default' => '',
 114                                                                 'required' => true ),
 115                                           "modified" => array( 'name' => "Modified",
 116                                                                'datatype' => 'integer',
 117                                                                'default' => 0,
 118                                                                'required' => true ),
 119                                           "is_container" => array( 'name' => "IsContainer",
 120                                                                    'datatype' => 'integer',
 121                                                                    'default' => 0,
 122                                                                    'required' => true ),
 123                                           'always_available' => array( 'name' => "AlwaysAvailable",
 124                                                                        'datatype' => 'integer',
 125                                                                        'default' => 0,
 126                                                                        'required' => true ),
 127                                           'language_mask' => array( 'name' => "LanguageMask",
 128                                                                     'datatype' => 'integer',
 129                                                                     'default' => 0,
 130                                                                     'required' => true ),
 131                                           'initial_language_id' => array( 'name' => "InitialLanguageID",
 132                                                                           'datatype' => 'integer',
 133                                                                           'default' => 0,
 134                                                                           'required' => true,
 135                                                                           'foreign_class' => 'eZContentLanguage',
 136                                                                           'foreign_attribute' => 'id',
 137                                                                           'multiplicity' => '1..*' ),
 138                                           'sort_field' => array( 'name' => 'SortField',
 139                                                                  'datatype' => 'integer',
 140                                                                  'default' => 1,
 141                                                                  'required' => true ),
 142                                           'sort_order' => array( 'name' => 'SortOrder',
 143                                                                  'datatype' => 'integer',
 144                                                                  'default' => 1,
 145                                                                  'required' => true ) ),
 146                        "keys" => array( "id", "version" ),
 147                        "function_attributes" => array( "data_map" => "dataMap",
 148                                                        'object_count' => 'objectCount',
 149                                                        'version_count' => 'versionCount',
 150                                                        'version_status' => 'versionStatus',
 151                                                        'remote_id' => 'remoteID', // Note: This overrides remote_id field
 152                                                        'ingroup_list' => 'fetchGroupList',
 153                                                        'ingroup_id_list' => 'fetchGroupIDList',
 154                                                        'match_ingroup_id_list' => 'fetchMatchGroupIDList',
 155                                                        'group_list' => 'fetchAllGroups',
 156                                                        'creator' => 'creator',
 157                                                        'modifier' => 'modifier',
 158                                                        'can_instantiate_languages' => 'canInstantiateLanguages',
 159                                                        'name' => 'name',
 160                                                        'nameList' => 'nameList',
 161                                                        'languages' => 'languages',
 162                                                        'prioritized_languages' => 'prioritizedLanguages',
 163                                                        'can_create_languages' => 'canCreateLanguages',
 164                                                        'top_priority_language_locale' => 'topPriorityLanguageLocale',
 165                                                        'always_available_language' => 'alwaysAvailableLanguage' ),
 166                        'set_functions' => array( 'name' => 'setName' ),
 167                        "increment_key" => "id",
 168                        "class_name" => "eZContentClass",
 169                        "sort" => array( "id" => "asc" ),
 170                        "name" => "ezcontentclass" );
 171      }
 172  
 173      function clone()
 174      {
 175          $row = array(
 176              "id" => null,
 177              "version" => $this->attribute( 'version' ),
 178              "serialized_name_list" => $this->attribute( 'serialized_name_list' ),
 179              "identifier" => $this->attribute( 'identifier' ),
 180              "contentobject_name" => $this->attribute( 'contentobject_name' ),
 181              "creator_id" => $this->attribute( 'creator_id' ),
 182              "modifier_id" => $this->attribute( 'modifier_id' ),
 183              "created" => $this->attribute( 'created' ),
 184              "modified" => $this->attribute( 'modified' ),
 185              "is_container" => $this->attribute( 'is_container' ),
 186              "always_available" => $this->attribute( 'always_available' ),
 187              'language_mask' => $this->attribute( 'language_mask' ),
 188              'initital_language_id' => $this->attribute( 'initial_language_id' ),
 189              "sort_field" => $this->attribute( 'sort_field' ),
 190              "sort_order" => $this->attribute( 'sort_order' ) );
 191  
 192          $tmpClass = new eZContentClass( $row );
 193          return $tmpClass;
 194      }
 195  
 196      /*!
 197       Creates an 'eZContentClass' object.
 198  
 199       To specify contentclass name use either $optionalValues['serialized_name_list'] or
 200       combination of $optionalValues['name'] and/or $languageLocale.
 201  
 202       In case of conflict(when both 'serialized_name_list' and 'name' with/without $languageLocale
 203       are specified) 'serialized_name_list' has top priority. This means that 'name' and
 204       $languageLocale will be ingnored because 'serialized_name_list' already has all needed info
 205       about names and languages.
 206  
 207       If 'name' is specified then the contentclass will have a name in $languageLocale(if specified) or
 208       in default language.
 209  
 210       If neither of 'serialized_name_list' or 'name' isn't specified then the contentclass will have an empty
 211       name in 'languageLocale'(if specified) or in default language.
 212  
 213       'language_mask' and 'initial_language_id' attributes will be set according to specified(either
 214       in 'serialized_name_list' or by $languageLocale) languages.
 215  
 216       \return 'eZContentClass' object.
 217      */
 218      function create( $userID = false, $optionalValues = array(), $languageLocale = false )
 219      {
 220          $dateTime = time();
 221          if ( !$userID )
 222              $userID = eZUser::currentUserID();
 223  
 224          $nameList = new eZContentClassNameList();
 225          if ( isset( $optionalValues['serialized_name_list'] ) )
 226              $nameList->initFromSerializedList( $optionalValues['serialized_name_list'] );
 227          else if ( isset( $optionalValues['name'] ) )
 228              $nameList->initFromString( $optionalValues['name'], $languageLocale );
 229          else
 230              $nameList->initFromString( '', $languageLocale );
 231  
 232          $languageMask = $nameList->languageMask();
 233          $initialLanguageID = $nameList->alwaysAvailableLanguageID();
 234  
 235          $contentClassDefinition = eZContentClass::definition();
 236          $row = array(
 237              "id" => null,
 238              "version" => 1,
 239              "serialized_name_list" => $nameList->serializeNames(),
 240              "identifier" => "",
 241              "contentobject_name" => "",
 242              "creator_id" => $userID,
 243              "modifier_id" => $userID,
 244              "created" => $dateTime,
 245              'remote_id' => md5( (string)mt_rand() . (string)mktime() ),
 246              "modified" => $dateTime,
 247              "is_container" => $contentClassDefinition[ 'fields' ][ 'is_container' ][ 'default' ],
 248              "always_available" => $contentClassDefinition[ 'fields' ][ 'always_available' ][ 'default' ],
 249              'language_mask' => $languageMask,
 250              'initial_language_id' => $initialLanguageID,
 251              "sort_field" => $contentClassDefinition[ 'fields' ][ 'sort_field' ][ 'default' ],
 252              "sort_order" => $contentClassDefinition[ 'fields' ][ 'sort_order' ][ 'default' ] );
 253  
 254          $row = array_merge( $row, $optionalValues );
 255  
 256          $contentClass = new eZContentClass( $row );
 257  
 258          // setting 'dirtyData' to make sure the 'NameList' will be stored into db.
 259          $contentClass->NameList->setHasDirtyData( true );
 260  
 261          return $contentClass;
 262      }
 263  
 264      function instantiateIn( $lang, $userID = false, $sectionID = 0, $versionNumber = false, $versionStatus = EZ_VERSION_STATUS_INTERNAL_DRAFT )
 265      {
 266          return eZContentClass::instantiate( $userID, $sectionID, $versionNumber, $lang, $versionStatus );
 267      }
 268  
 269      /*!
 270       Creates a new content object instance and stores it.
 271  
 272       \param user ID (optional), current user if not set
 273       \param section ID (optional), 0 if not set
 274       \param version number, create initial version if not set
 275       \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
 276       the calls within a db transaction; thus within db->begin and db->commit.
 277      */
 278      function instantiate( $userID = false, $sectionID = 0, $versionNumber = false, $languageCode = false, $versionStatus = EZ_VERSION_STATUS_INTERNAL_DRAFT )
 279      {
 280          $attributes = $this->fetchAttributes();
 281  
 282          $user =& eZUser::currentUser();
 283          if ( $userID === false )
 284          {
 285              $userID =& $user->attribute( 'contentobject_id' );
 286          }
 287  
 288          if ( $languageCode == false )
 289          {
 290              $languageCode = eZContentObject::defaultLanguage();
 291          }
 292  
 293          $object = eZContentObject::create( ezi18n( "kernel/contentclass", "New %1", null, array( $this->name( $languageCode ) ) ),
 294                                             $this->attribute( "id" ),
 295                                             $userID,
 296                                             $sectionID,
 297                                             1,
 298                                             $languageCode );
 299  
 300          if ( $this->attribute( 'always_available' ) )
 301          {
 302              $object->setAttribute( 'language_mask', (int)$object->attribute( 'language_mask') | 1 );
 303          }
 304  
 305          $db =& eZDB::instance();
 306          $db->begin();
 307  
 308          $object->store();
 309          $object->setName( ezi18n( "kernel/contentclass", "New %1", null, array( $this->name( $languageCode ) ) ), false, $languageCode );
 310  
 311          if ( !$versionNumber )
 312          {
 313              $version = $object->createInitialVersion( $userID, $languageCode );
 314          }
 315          else
 316          {
 317              $version = eZContentObjectVersion::create( $object->attribute( "id" ), $userID, $versionNumber, $languageCode );
 318          }
 319          if ( $versionStatus !== false )
 320          {
 321              $version->setAttribute( 'status', $versionStatus );
 322          }
 323  
 324          $version->store();
 325  
 326          foreach ( array_keys( $attributes ) as $attributeKey )
 327          {
 328              $attribute =& $attributes[$attributeKey];
 329              $attribute->instantiate( $object->attribute( 'id' ), $languageCode );
 330          }
 331  
 332          if ( $user->isAnonymous() )
 333          {
 334              include_once ( 'kernel/classes/ezpreferences.php' );
 335              $createdObjectIDList = eZPreferences::value( 'ObjectCreationIDList' );
 336              if ( !$createdObjectIDList )
 337              {
 338                  $createdObjectIDList = array( $object->attribute( 'id' ) );
 339              }
 340              else
 341              {
 342                  $createdObjectIDList = unserialize( $createdObjectIDList );
 343                  $createdObjectIDList[] = $object->attribute( 'id' );
 344              }
 345              eZPreferences::setValue( 'ObjectCreationIDList', serialize( $createdObjectIDList ) );
 346          }
 347  
 348          $db->commit();
 349          return $object;
 350      }
 351  
 352      function canInstantiateClasses()
 353      {
 354          $ini =& eZINI::instance();
 355          $enableCaching = $ini->variable( 'RoleSettings', 'EnableCaching' );
 356  
 357          if ( $enableCaching == 'true' )
 358          {
 359              $http =& eZHTTPTool::instance();
 360  
 361              include_once ( 'lib/ezutils/classes/ezexpiryhandler.php' );
 362              $handler =& eZExpiryHandler::instance();
 363              $expiredTimeStamp = 0;
 364              if ( $handler->hasTimestamp( 'user-class-cache' ) )
 365                  $expiredTimeStamp = $handler->timestamp( 'user-class-cache' );
 366  
 367              $classesCachedForUser = $http->sessionVariable( 'CanInstantiateClassesCachedForUser' );
 368              $classesCachedTimestamp = $http->sessionVariable( 'ClassesCachedTimestamp' );
 369              $user =& eZUser::currentUser();
 370              $userID = $user->id();
 371  
 372              if ( ( $classesCachedTimestamp >= $expiredTimeStamp ) && $classesCachedForUser == $userID )
 373              {
 374                  if ( $http->hasSessionVariable( 'CanInstantiateClasses' ) )
 375                  {
 376                      return $http->sessionVariable( 'CanInstantiateClasses' );
 377                  }
 378              }
 379              else
 380              {
 381                  // store cache
 382                  $http->setSessionVariable( 'CanInstantiateClassesCachedForUser', $userID );
 383              }
 384          }
 385          $user =& eZUser::currentUser();
 386          $accessResult = $user->hasAccessTo( 'content' , 'create' );
 387          $accessWord = $accessResult['accessWord'];
 388          $canInstantiateClasses = 1;
 389          if ( $accessWord == 'no' )
 390          {
 391              $canInstantiateClasses = 0;
 392          }
 393  
 394          if ( $enableCaching == 'true' )
 395          {
 396              $http->setSessionVariable( 'CanInstantiateClasses', $canInstantiateClasses );
 397          }
 398          return $canInstantiateClasses;
 399      }
 400  
 401      // code-template::create-block: can-instantiate-class-list, group-filter, role-caching, class-policy-list, name-instantiate, object-creation, class-sql-creation
 402      // code-template::auto-generated:START can-instantiate-class-list
 403      // This code is automatically generated from templates/classcreatelist.ctpl
 404      // DO NOT EDIT THIS CODE DIRECTLY, CHANGE THE TEMPLATE FILE INSTEAD
 405  
 406      /*!
 407       \static
 408       Finds all classes that the current user can create objects from and returns.
 409       It is also possible to filter the list event more with \a $includeFilter and \a $groupList.
 410  
 411       \param $asObject If \c true then it return eZContentClass objects, if not it will
 412                        be an associative array with \c name and \c id keys.
 413       \param $includeFilter If \c true then it will include only from class groups defined in
 414                             \a $groupList, if not it will exclude those groups.
 415       \param $groupList An array with class group IDs that should be used in filtering, use
 416                         \c false if you do not wish to filter at all.
 417       \param $id A unique name for the current fetch, this must be supplied when filtering is
 418                  used if you want caching to work.
 419      */
 420      function &canInstantiateClassList( $asObject = false, $includeFilter = true, $groupList = false, $fetchID = false )
 421      {
 422          $ini =& eZINI::instance();
 423          $groupArray = array();
 424  
 425          $enableCaching = ( $ini->variable( 'RoleSettings', 'EnableCaching' ) == 'true' );
 426          if ( is_array( $groupList ) )
 427          {
 428              if ( $fetchID == false )
 429                  $enableCaching = false;
 430          }
 431  
 432          if ( $enableCaching )
 433          {
 434              $http =& eZHTTPTool::instance();
 435              include_once ( 'lib/ezutils/classes/ezexpiryhandler.php' );
 436              $handler =& eZExpiryHandler::instance();
 437              $expiredTimeStamp = 0;
 438              if ( $handler->hasTimestamp( 'user-class-cache' ) )
 439                  $expiredTimeStamp = $handler->timestamp( 'user-class-cache' );
 440  
 441              $classesCachedForUser = $http->sessionVariable( 'ClassesCachedForUser' );
 442              $classesCachedTimestamp = $http->sessionVariable( 'ClassesCachedTimestamp' );
 443  
 444              $cacheVar = 'CanInstantiateClassList';
 445              if ( is_array( $groupList ) and $fetchID !== false )
 446              {
 447                  $cacheVar = 'CanInstantiateClassListGroup';
 448              }
 449  
 450              $user =& eZUser::currentUser();
 451              $userID = $user->id();
 452              if ( ( $classesCachedTimestamp >= $expiredTimeStamp ) && $classesCachedForUser == $userID )
 453              {
 454                  if ( $http->hasSessionVariable( $cacheVar ) )
 455                  {
 456                      if ( $fetchID !== false )
 457                      {
 458                          // Check if the group contains our ID, if not we need to fetch from DB
 459                          $groupArray = $http->sessionVariable( $cacheVar );
 460                          if ( isset( $groupArray[$fetchID] ) )
 461                          {
 462                              return $groupArray[$fetchID];
 463                          }
 464                      }
 465                      else
 466                      {
 467                          return $http->sessionVariable( $cacheVar );
 468                      }
 469                  }
 470              }
 471              else
 472              {
 473                  $http->setSessionVariable( 'ClassesCachedForUser' , $userID );
 474                  $http->setSessionVariable( 'ClassesCachedTimestamp', mktime() );
 475              }
 476          }
 477  
 478          $languageCodeList = eZContentLanguage::fetchLocaleList();
 479          $allowedLanguages = array( '*' => array() );
 480  
 481          $user =& eZUser::currentUser();
 482          $accessResult = $user->hasAccessTo( 'content' , 'create' );
 483          $accessWord = $accessResult['accessWord'];
 484  
 485          $classIDArray = array();
 486          $classList = array();
 487          $fetchAll = false;
 488          if ( $accessWord == 'yes' )
 489          {
 490              $fetchAll = true;
 491              $allowedLanguages['*'] = $languageCodeList;
 492          }
 493          else if ( $accessWord == 'no' )
 494          {
 495              // Cannnot create any objects, return empty list.
 496              return $classList;
 497          }
 498          else
 499          {
 500              $policies = $accessResult['policies'];
 501              foreach ( $policies as $policyKey => $policy )
 502              {
 503                  $classIDArrayPart = '*';
 504                  if ( isset( $policy['Class'] ) )
 505                  {
 506                      $classIDArrayPart = $policy['Class'];
 507                  }
 508                  $languageCodeArrayPart = $languageCodeList;
 509                  if ( isset( $policy['Language'] ) )
 510                  {
 511                      $languageCodeArrayPart = array_intersect( $policy['Language'], $languageCodeList );
 512                  }
 513  
 514                  if ( $classIDArrayPart == '*' )
 515                  {
 516                      $fetchAll = true;
 517                      $allowedLanguages['*'] = array_unique( array_merge( $allowedLanguages['*'], $languageCodeArrayPart ) );
 518                  }
 519                  else
 520                  {
 521                      foreach( $classIDArrayPart as $class )
 522                      {
 523                          if ( isset( $allowedLanguages[$class] ) )
 524                          {
 525                              $allowedLanguages[$class] = array_unique( array_merge( $allowedLanguages[$class], $languageCodeArrayPart ) );
 526                          }
 527                          else
 528                          {
 529                              $allowedLanguages[$class] = $languageCodeArrayPart;
 530                          }
 531                      }
 532                      $classIDArray = array_merge( $classIDArray, array_diff( $classIDArrayPart, $classIDArray ) );
 533                  }
 534              }
 535          }
 536  
 537          $filterTableSQL = '';
 538          $filterSQL = '';
 539          // Create extra SQL statements for the class group filters.
 540          if ( is_array( $groupList ) )
 541          {
 542              $filterTableSQL = ', ezcontentclass_classgroup ccg';
 543              $filterSQL = ( " AND\n" .
 544                             "      cc.id = ccg.contentclass_id AND\n" .
 545                             "      ccg.group_id " );
 546              $groupText = implode( ', ', $groupList );
 547              if ( $includeFilter )
 548                  $filterSQL .= "IN ( $groupText )";
 549              else
 550                  $filterSQL .= "NOT IN ( $groupText )";
 551          }
 552  
 553          $classNameFilter = eZContentClassName::sqlFilter( 'cc' );
 554          $filterSQL .= "AND\n      cc.id=" . $classNameFilter['from'] .".contentclass_id";
 555  
 556          if ( $fetchAll )
 557          {
 558              $classList = array();
 559              $db =& eZDb::instance();
 560              // If $asObject is true we fetch all fields in class
 561              $fields = $asObject ? "cc.*, $classNameFilter[nameField]" : "cc.id, $classNameFilter[nameField]";
 562              $rows = $db->arrayQuery( "SELECT DISTINCT $fields\n" .
 563                                       "FROM ezcontentclass cc$filterTableSQL, $classNameFilter[from]\n" .
 564                                       "WHERE cc.version = " . EZ_CLASS_VERSION_STATUS_DEFINED . "$filterSQL\n" .
 565                                       "ORDER BY $classNameFilter[nameField] ASC" );
 566              $classList = eZPersistentObject::handleRows( $rows, 'ezcontentclass', $asObject );
 567          }
 568          else
 569          {
 570              // If the constrained class list is empty we are not allowed to create any class
 571              if ( count( $classIDArray ) == 0 )
 572              {
 573                  $classList = array();
 574                  return $classList;
 575              }
 576  
 577              $classList = array();
 578              $db =& eZDb::instance();
 579              $classString = implode( ',', $classIDArray );
 580              // If $asObject is true we fetch all fields in class
 581              $fields = $asObject ? "cc.*, $classNameFilter[nameField]" : "cc.id, $classNameFilter[nameField]";
 582              $rows = $db->arrayQuery( "SELECT DISTINCT $fields\n" .
 583                                       "FROM ezcontentclass cc$filterTableSQL, $classNameFilter[from]\n" .
 584                                       "WHERE cc.id IN ( $classString  ) AND\n" .
 585                                       "      cc.version = " . EZ_CLASS_VERSION_STATUS_DEFINED . "$filterSQL\n" .
 586                                       "ORDER BY $classNameFilter[nameField] ASC" );
 587              $classList = eZPersistentObject::handleRows( $rows, 'ezcontentclass', $asObject );
 588          }
 589  
 590          if ( $asObject )
 591          {
 592              foreach ( $classList as $key => $class )
 593              {
 594                  $id = $class->attribute( 'id' );
 595                  if ( isset( $allowedLanguages[$id] ) )
 596                  {
 597                      $languageCodes = array_unique( array_merge( $allowedLanguages['*'], $allowedLanguages[$id] ) );
 598                  }
 599                  else
 600                  {
 601                      $languageCodes = $allowedLanguages['*'];
 602                  }
 603                  $classList[$key]->setCanInstantiateLanguages( $languageCodes );
 604              }
 605          }
 606  
 607          eZDebugSetting::writeDebug( 'kernel-content-class', $classList, "class list fetched from db" );
 608          if ( $enableCaching )
 609          {
 610              if ( $fetchID !== false )
 611              {
 612                  $groupArray[$fetchID] = $classList;
 613                  $http->setSessionVariable( $cacheVar, $groupArray );
 614              }
 615              else
 616              {
 617                  $http->setSessionVariable( $cacheVar, $classList );
 618              }
 619          }
 620  
 621          return $classList;
 622      }
 623  
 624      // This code is automatically generated from templates/classcreatelist.ctpl
 625      // code-template::auto-generated:END can-instantiate-class-list
 626  
 627      /*!
 628       \return The creator of the class as an eZUser object by using the $CreatorID as user ID.
 629       \note The reference for the return value is required to workaround
 630             a bug with PHP references.
 631      */
 632      function &creator()
 633      {
 634          if ( isset( $this->CreatorID ) and $this->CreatorID )
 635          {
 636              include_once ( "kernel/classes/datatypes/ezuser/ezuser.php" );
 637              $user = eZUser::fetch( $this->CreatorID );
 638          }
 639          else
 640              $user = null;
 641          return $user;
 642      }
 643  
 644      /*!
 645       \return The modifier of the class as an eZUser object by using the $ModifierID as user ID.
 646       \note The reference for the return value is required to workaround
 647             a bug with PHP references.
 648      */
 649      function &modifier()
 650      {
 651          if ( isset( $this->ModifierID ) and $this->ModifierID )
 652          {
 653              include_once ( "kernel/classes/datatypes/ezuser/ezuser.php" );
 654              $user = eZUser::fetch( $this->ModifierID );
 655          }
 656          else
 657              $user = null;
 658          return $user;
 659      }
 660  
 661      /*!
 662       Find all groups the current class is placed in and returns a list of group objects.
 663       \return An array with eZContentClassGroup objects.
 664       \sa fetchGroupIDList()
 665       \note The reference for the return value is required to workaround
 666             a bug with PHP references.
 667      */
 668      function &fetchGroupList()
 669      {
 670          $this->InGroups = eZContentClassClassGroup::fetchGroupList( $this->attribute( "id" ),
 671                                                                       $this->attribute( "version" ),
 672                                                                       true );
 673          return $this->InGroups;
 674      }
 675  
 676      /*!
 677       Find all groups the current class is placed in and returns a list of group IDs.
 678       \return An array with integers (ids).
 679       \sa fetchGroupList()
 680       \note The reference for the return value is required to workaround
 681             a bug with PHP references.
 682      */
 683      function &fetchGroupIDList()
 684      {
 685          $list = eZContentClassClassGroup::fetchGroupList( $this->attribute( "id" ),
 686                                                            $this->attribute( "version" ),
 687                                                            false );
 688          $this->InGroupIDs = array();
 689          foreach ( $list as $item )
 690          {
 691              $this->InGroupIDs[] = $item['group_id'];
 692          }
 693          return $this->InGroupIDs;
 694      }
 695  
 696      /*!
 697       Returns the result from fetchGroupIDList() if class group overrides is
 698       enabled in content.ini.
 699       \return An array with eZContentClassGroup objects or \c false if disabled.
 700       \note \c EnableClassGroupOverride in group \c ContentOverrideSettings from INI file content.ini
 701             controls this behaviour.
 702       \note The reference for the return value is required to workaround
 703             a bug with PHP references.
 704      */
 705      function &fetchMatchGroupIDList()
 706      {
 707          include_once ( 'lib/ezutils/classes/ezini.php' );
 708          $contentINI =& eZINI::instance( 'content.ini' );
 709          if( $contentINI->variable( 'ContentOverrideSettings', 'EnableClassGroupOverride' ) == 'true' )
 710          {
 711              $retValue =& $this->attribute( 'ingroup_id_list' );
 712          }
 713          else
 714          {
 715              $retValue = false;
 716          }
 717          return $retValue;
 718      }
 719  
 720      /*!
 721       Finds all Classes in the system and returns them.
 722       \return An array with eZContentClass objects.
 723       \note The reference for the return value is required to workaround
 724             a bug with PHP references.
 725      */
 726      function &fetchAllClasses( $asObject = true, $includeFilter = true, $groupList = false )
 727      {
 728          if ( is_array( $groupList ) )
 729          {
 730              $filterTableSQL = ', ezcontentclass_classgroup ccg';
 731              $filterSQL = ( " AND\n" .
 732                             "      cc.id = ccg.contentclass_id AND\n" .
 733                             "      ccg.group_id " );
 734              $groupText = implode( ', ', $groupList );
 735              if ( $includeFilter )
 736                  $filterSQL .= "IN ( $groupText )";
 737              else
 738                  $filterSQL .= "NOT IN ( $groupText )";
 739          }
 740  
 741          $classNameFilter = eZContentClassName::sqlFilter( 'cc' );
 742  
 743          $classList = array();
 744          $db =& eZDb::instance();
 745          // If $asObject is true we fetch all fields in class
 746          $fields = $asObject ? "cc.*" : "cc.id, $classNameFilter[nameField]";
 747          $rows = $db->arrayQuery( "SELECT DISTINCT $fields\n" .
 748                                   "FROM ezcontentclass cc$filterTableSQL, $classNameFilter[from]\n" .
 749                                   "WHERE cc.version = " . EZ_CLASS_VERSION_STATUS_DEFINED . "$filterSQL AND $classNameFilter[where]\n" .
 750                                   "ORDER BY $classNameFilter[nameField] ASC" );
 751  
 752          $classList = eZPersistentObject::handleRows( $rows, 'ezcontentclass', $asObject );
 753          return $classList;
 754      }
 755  
 756      /*!
 757       Finds all Class groups in the system and returns them.
 758       \return An array with eZContentClassGroup objects.
 759       \sa fetchGroupList(), fetchGroupIDList()
 760       \note The reference for the return value is required to workaround
 761             a bug with PHP references.
 762      */
 763      function &fetchAllGroups()
 764      {
 765          $this->AllGroups = eZContentClassGroup::fetchList();
 766          return $this->AllGroups;
 767      }
 768  
 769      /*!
 770       \return true if the class is part of the group \a $groupID
 771      */
 772      function inGroup( $groupID )
 773      {
 774          return eZContentClassClassGroup::classInGroup( $this->attribute( 'id' ),
 775                                                         $this->attribute( 'version' ),
 776                                                         $groupID );
 777      }
 778  
 779      /*!
 780       \static
 781       Will remove all temporary classes from the database.
 782      */
 783      function removeTemporary()
 784      {
 785          $version = EZ_CLASS_VERSION_STATUS_TEMPORARY;
 786          $temporaryClasses = eZContentClass::fetchList( $version, true );
 787          $db =& eZDb::instance();
 788          $db->begin();
 789          foreach ( $temporaryClasses as $class )
 790          {
 791              $class->remove( true, $version );
 792          }
 793          eZPersistentObject::removeObject( eZContentClassAttribute::definition(),
 794                                            array( 'version' => $version ) );
 795  
 796          $db->commit();
 797      }
 798  
 799      /*!
 800       Get remote id of content node
 801      */
 802      function &remoteID()
 803      {
 804          $remoteID = eZPersistentObject::attribute( 'remote_id', true );
 805          if ( !$remoteID &&
 806               $this->Version == EZ_CLASS_VERSION_STATUS_DEFINED )
 807          {
 808              $this->setAttribute( 'remote_id', md5( (string)mt_rand() . (string)mktime() ) );
 809              $this->sync( array( 'remote_id' ) );
 810              $remoteID = eZPersistentObject::attribute( 'remote_id', true );
 811          }
 812  
 813          return $remoteID;
 814      }
 815  
 816      /*!
 817       \note If you want to remove a class with all data associated with it (objects/classMembers)
 818             you should use eZContentClassOperations::remove()
 819      */
 820      function remove( $remove_childs = false, $version = EZ_CLASS_VERSION_STATUS_DEFINED )
 821      {
 822          // If we are not allowed to remove just return false
 823          if ( $this->Version != EZ_CLASS_VERSION_STATUS_TEMPORARY && !$this->isRemovable() )
 824              return false;
 825  
 826          if ( is_array( $remove_childs ) or $remove_childs )
 827          {
 828              if ( is_array( $remove_childs ) )
 829              {
 830                  $db =& eZDb::instance();
 831                  $db->begin();
 832  
 833                  $attributes =& $remove_childs;
 834                  for ( $i = 0; $i < count( $attributes ); ++$i )
 835                  {
 836                      $attribute =& $attributes[$i];
 837                      $attribute->remove();
 838                  }
 839                  $db->commit();
 840              }
 841              else
 842              {
 843                  if ( $version == EZ_CLASS_VERSION_STATUS_DEFINED )
 844                  {
 845                      $contentClassID = $this->ID;
 846                      $version = $this->Version;
 847                      $classAttributes =& $this->fetchAttributes( );
 848  
 849                      foreach ( $classAttributes as $classAttribute )
 850                      {
 851                          $dataType = $classAttribute->dataType();
 852                          $dataType->deleteStoredClassAttribute( $classAttribute, $version );
 853                      }
 854                      eZPersistentObject::removeObject( eZContentClassAttribute::definition(),
 855                                                        array( "contentclass_id" => $contentClassID,
 856                                                               "version" => $version ) );
 857                  }
 858                  else
 859                  {
 860                      $contentClassID = $this->ID;
 861                      $version = $this->Version;
 862                      $classAttributes =& $this->fetchAttributes( );
 863  
 864                      foreach ( $classAttributes as $classAttribute )
 865                      {
 866                          $dataType = $classAttribute->dataType();
 867                          $dataType->deleteStoredClassAttribute( $classAttribute, $version );
 868                      }
 869                      eZPersistentObject::removeObject( eZContentClassAttribute::definition(),
 870                                                        array( "contentclass_id" => $contentClassID,
 871                                                               "version" => $version ) );
 872                  }
 873              }
 874          }
 875  
 876          $this->NameList->remove( $this );
 877  
 878          eZPersistentObject::remove();
 879      }
 880  
 881      /*!
 882       Checks if the class can be removed and returns \c true if it can, \c false otherwise.
 883       \sa removableInformation()
 884      */
 885      function isRemovable()
 886      {
 887          $info = $this->removableInformation( false );
 888          return count( $info['list'] ) == 0;
 889      }
 890  
 891      /*!
 892       Returns information on why the class cannot be removed,
 893       it does the same checks as in isRemovable() but generates
 894       some text in the return array.
 895       \return An array which contains:
 896               - text - Plain text description why this cannot be removed
 897               - list - An array with reasons why this failed, each entry contains:
 898                        - text - Plain text description of the reason.
 899                        - list - A sublist of reason (e.g from an attribute), is optional.
 900       \param $includeAll Controls whether the returned information will contain all
 901                          sources for not being to remove or just the first that it finds.
 902      */
 903      function removableInformation( $includeAll = true )
 904      {
 905          $result  = array( 'text' => ezi18n( 'kernel/contentclass', "Cannot remove class '%class_name':",
 906                                           null, array( '%class_name' => $this->attribute( 'name' ) ) ),
 907                         'list' => array() );
 908          $reasons =& $result['list'];
 909          $db      =& eZDB::instance();
 910  
 911          // Check top-level nodes
 912          $rows = $db->arrayQuery( "SELECT ezcot.node_id
 913  FROM ezcontentobject_tree ezcot, ezcontentobject ezco
 914  WHERE ezcot.depth = 1 AND
 915        ezco.contentclass_id = $this->ID AND
 916        ezco.id=ezcot.contentobject_id" );
 917          if ( count( $rows ) > 0 )
 918          {
 919              $reasons[] = array( 'text' => ezi18n( 'kernel/contentclass', 'The class is used by a top-level node and cannot be removed.
 920  You will need to change the class of the node by using the swap functionality.' ) );
 921              if ( !$includeAll )
 922                  return $result;
 923          }
 924  
 925          // Check class attributes
 926          $attributes =& $this->fetchAttributes();
 927          foreach ( $attributes as $key => $attribute )
 928          {
 929              $dataType = $attribute->dataType();
 930              if ( !$dataType->isClassAttributeRemovable( $attribute ) )
 931              {
 932                  $info = $dataType->classAttributeRemovableInformation( $attribute, $includeAll );
 933                  $reasons[] = $info;
 934                  if ( !$includeAll )
 935                      return $result;
 936              }
 937          }
 938  
 939          return $result;
 940      }
 941  
 942      function removeAttributes( $attributes = false, $id = false, $version = false )
 943      {
 944          if ( is_array( $attributes ) )
 945          {
 946              $db =& eZDB::instance();
 947              $db->begin();
 948              for ( $i = 0; $i < count( $attributes ); ++$i )
 949              {
 950                  $attribute =& $attributes[$i];
 951                  $attribute->remove();
 952                  $contentObject->purge();
 953              }
 954              $db->commit();
 955          }
 956          else
 957          {
 958              if ( $version === false )
 959                  $version = $this->Version;
 960              if ( $id === false )
 961                  $id = $this->ID;
 962              eZPersistentObject::removeObject( eZContentClassAttribute::definition(),
 963                                                array( "contentclass_id" => $id,
 964                                                       "version" => $version ) );
 965          }
 966      }
 967  
 968      function compareAttributes( $attr1, $attr2 )
 969      {
 970          return  ( $attr1->attribute( "placement" ) > $attr2->attribute( "placement" )  ) ? 1 : -1;
 971      }
 972  
 973      function adjustAttributePlacements( &$attributes )
 974      {
 975          if ( !is_array( $attributes ) )
 976              return;
 977          usort( $attributes, array( $this, "compareAttributes" ) );
 978          for ( $i = 0; $i < count( $attributes ); ++$i )
 979          {
 980              $attribute =& $attributes[$i];
 981              $attribute->setAttribute( "placement", $i + 1 );
 982          }
 983      }
 984  
 985      /*!
 986       \reimp
 987      */
 988      function store( $store_childs = false, $fieldFilters = null )
 989      {
 990          $db =& eZDB::instance();
 991          $db->begin();
 992  
 993          if ( is_array( $store_childs ) or $store_childs )
 994          {
 995              if ( is_array( $store_childs ) )
 996                  $attributes =& $store_childs;
 997              else
 998                  $attributes =& $this->fetchAttributes();
 999  
1000             for ( $i = 0; $i < count( $attributes ); ++$i )
1001              {
1002                  $attribute =& $attributes[$i];
1003                  if ( is_object ( $attribute ) )
1004                      $attribute->store();
1005              }
1006          }
1007  
1008          include_once ( 'lib/ezutils/classes/ezexpiryhandler.php' );
1009          $handler =& eZExpiryHandler::instance();
1010          $handler->setTimestamp( 'user-class-cache', mktime() );
1011          $handler->store();
1012  
1013          $this->setAttribute( 'serialized_name_list', $this->NameList->serializeNames() );
1014  
1015          eZPersistentObject::store( $fieldFilters );
1016  
1017          $this->NameList->store( $this );
1018  
1019          $db->commit();
1020      }
1021  
1022      /*!
1023       \reimp
1024      */
1025      function sync( $fieldFilters = null )
1026      {
1027          if ( $this->hasDirtyData() )
1028              $this->store( false, $fieldFilters );
1029      }
1030  
1031      /*!
1032       Initializes this class as a copy of \a $originalClass by
1033       creating new a new name and identifier.
1034       It will check if there are other classes already with this name
1035       in which case it will append a unique number to the name and identifier.
1036      */
1037      function initializeCopy( &$originalClass )
1038      {
1039          $name = ezi18n( 'kernel/class', 'Copy of %class_name', null,
1040                          array( '%class_name' => $originalClass->attribute( 'name' ) ) );
1041          $identifier = 'copy_of_' . $originalClass->attribute( 'identifier' );
1042          $db =& eZDB::instance();
1043          $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 ) . "%'";
1044          $rows = $db->arrayQuery( $sql );
1045          $count = $rows[0]['count'];
1046          if ( $count > 0 )
1047          {
1048              ++$count;
1049              $name .= $count;
1050              $identifier .= $count;
1051          }
1052          $this->setName( $name );
1053          $this->setAttribute( 'identifier', $identifier );
1054          $this->setAttribute( 'created', time() );
1055          include_once ( "kernel/classes/datatypes/ezuser/ezuser.php" );
1056          $user =& eZUser::currentUser();
1057          $userID = $user->attribute( "contentobject_id" );
1058          $this->setAttribute( 'creator_id', $userID );
1059      }
1060  
1061      /*!
1062       Stores the current class as a defined version, updates the contentobject_name
1063       attribute and recreates the class group entries.
1064       \note It will remove any existing temporary or defined classes before storing.
1065      */
1066      function storeDefined( &$attributes )
1067      {
1068          $db =& eZDB::instance();
1069          $db->begin();
1070  
1071          eZContentClass::removeAttributes( false, $this->attribute( "id" ), EZ_CLASS_VERSION_STATUS_DEFINED );
1072          eZContentClass::removeAttributes( false, $this->attribute( "id" ), EZ_CLASS_VERSION_STATUS_TEMPORARY );
1073          $this->remove( false );
1074          $this->setVersion( EZ_CLASS_VERSION_STATUS_DEFINED, $attributes );
1075          include_once ( "kernel/classes/datatypes/ezuser/ezuser.php" );
1076          $user =& eZUser::currentUser();
1077          $user_id = $user->attribute( "contentobject_id" );
1078          $this->setAttribute( "modifier_id", $user_id );
1079          $this->setAttribute( "modified", time() );
1080          $this->adjustAttributePlacements( $attributes );
1081  
1082          for ( $i = 0; $i < count( $attributes ); ++$i )
1083          {
1084              $attribute =& $attributes[$i];
1085              $attribute->storeDefined();
1086          }
1087  
1088          // Set contentobject_name to something sensible if it is missing
1089          if ( count( $attributes ) > 0 )
1090          {
1091              $identifier = $attributes[0]->attribute( 'identifier' );
1092              $identifier = '<' . $identifier . '>';
1093              if ( trim( $this->attribute( 'contentobject_name' ) ) == '' )
1094              {
1095                  $this->setAttribute( 'contentobject_name', $identifier );
1096              }
1097          }
1098  
1099          // Recreate class member entries
1100          eZContentClassClassGroup::removeClassMembers( $this->ID, EZ_CLASS_VERSION_STATUS_DEFINED );
1101          $classgroups = eZContentClassClassGroup::fetchGroupList( $this->ID, EZ_CLASS_VERSION_STATUS_TEMPORARY );
1102          for ( $i = 0; $i < count( $classgroups ); $i++ )
1103          {
1104              $classgroup =& $classgroups[$i];
1105              $classgroup->setAttribute( 'contentclass_version', EZ_CLASS_VERSION_STATUS_DEFINED );
1106              $classgroup->store();
1107          }
1108          eZContentClassClassGroup::removeClassMembers( $this->ID, EZ_CLASS_VERSION_STATUS_TEMPORARY );
1109  
1110          include_once ( 'lib/ezutils/classes/ezexpiryhandler.php' );
1111          $handler =& eZExpiryHandler::instance();
1112          $handler->setTimestamp( 'user-class-cache', mktime() );
1113          $handler->store();
1114  
1115          include_once ( 'kernel/classes/ezcontentcachemanager.php' );
1116          eZContentCacheManager::clearAllContentCache();
1117  
1118          $this->setAttribute( 'serialized_name_list', $this->NameList->serializeNames() );
1119          eZPersistentObject::store();
1120          $this->NameList->store( $this );
1121  
1122          $db->commit();
1123      }
1124  
1125      function setVersion( $version, $set_childs = false )
1126      {
1127          if ( is_array( $set_childs ) or $set_childs )
1128          {
1129              if ( is_array( $set_childs ) )
1130                  $attributes =& $set_childs;
1131              else
1132                  $attributes =& $this->fetchAttributes();
1133              for ( $i = 0; $i < count( $attributes ); ++$i )
1134              {
1135                  $attribute =& $attributes[$i];
1136                  $attribute->setAttribute( "version", $version );
1137              }
1138          }
1139  
1140          if ( $this->Version != $version )
1141              $this->NameList->setHasDirtyData();
1142  
1143          eZPersistentObject::setAttribute( "version", $version );
1144      }
1145  
1146      function exists( $id, $version = EZ_CLASS_VERSION_STATUS_DEFINED, $userID = false, $useIdentifier = false )
1147      {
1148          $conds = array( "version" => $version );
1149          if ( $useIdentifier )
1150              $conds["identifier"] = $id;
1151          else
1152              $conds["id"] = $id;
1153          if ( $userID !== false and is_numeric( $userID ) )
1154              $conds["creator_id"] = $userID;
1155          $version_sort = "desc";
1156          if ( $version == EZ_CLASS_VERSION_STATUS_DEFINED )
1157              $conds['version'] = $version;
1158          $rows = eZPersistentObject::fetchObjectList( eZContentClass::definition(),
1159                                                        null,
1160                                                        $conds,
1161                                                        null,
1162                                                        array( "offset" => 0,
1163                                                               "length" => 1 ),
1164                                                        false );
1165          if ( count( $rows ) > 0 )
1166              return $rows[0]['id'];
1167          return false;
1168      }
1169  
1170      function fetch( $id, $asObject = true, $version = EZ_CLASS_VERSION_STATUS_DEFINED, $user_id = false ,$parent_id = null )
1171      {
1172          $conds = array( "id" => $id,
1173                          "version" => $version );
1174  
1175          if ( $user_id !== false and is_numeric( $user_id ) )
1176              $conds["creator_id"] = $user_id;
1177  
1178          $version_sort = "desc";
1179          if ( $version == EZ_CLASS_VERSION_STATUS_DEFINED )
1180              $version_sort = "asc";
1181          $rows = eZPersistentObject::fetchObjectList( eZContentClass::definition(),
1182                                                        null,
1183                                                        $conds,
1184                                                        array( "version" => $version_sort ),
1185                                                        array( "offset" => 0,
1186                                                               "length" => 2 ),
1187                                                        false );
1188  
1189          if ( count( $rows ) == 0 )
1190          {
1191              $contentClass = null;
1192              return $contentClass;
1193          }
1194  
1195          $row =& $rows[0];
1196          $row["version_count"] = count( $rows );
1197  
1198          if ( $asObject )
1199              $contentClass = new eZContentClass( $row );
1200          else
1201              $contentClass = $row;
1202  
1203          return $contentClass;
1204      }
1205  
1206      function fetchByRemoteID( $remoteID, $asObject = true, $version = EZ_CLASS_VERSION_STATUS_DEFINED, $user_id = false ,$parent_id = null )
1207      {
1208          $conds = array( "remote_id" => $remoteID,
1209                          "version" => $version );
1210          if ( $user_id !== false and is_numeric( $user_id ) )
1211              $conds["creator_id"] = $user_id;
1212          $version_sort = "desc";
1213          if ( $version == EZ_CLASS_VERSION_STATUS_DEFINED )
1214              $version_sort = "asc";
1215          $rows = eZPersistentObject::fetchObjectList( eZContentClass::definition(),
1216                                                        null,
1217                                                        $conds,
1218                                                        array( "version" => $version_sort ),
1219                                                        array( "offset" => 0,
1220                                                               "length" => 2 ),
1221                                                        false );
1222          if ( count( $rows ) == 0 )
1223          {
1224              $contentClass = null;
1225              return $contentClass;
1226          }
1227  
1228          $row =& $rows[0];
1229          $row["version_count"] = count( $rows );
1230  
1231          if ( $asObject )
1232              $contentClass = new eZContentClass( $row );
1233          else
1234              $contentClass = $row;
1235  
1236          return $contentClass;
1237  
1238      }
1239  
1240      function fetchByIdentifier( $identifier, $asObject = true, $version = EZ_CLASS_VERSION_STATUS_DEFINED, $user_id = false ,$parent_id = null )
1241      {
1242          $conds = array( "identifier" => $identifier,
1243                          "version" => $version );
1244          if ( $user_id !== false and is_numeric( $user_id ) )
1245              $conds["creator_id"] = $user_id;
1246          $version_sort = "desc";
1247          if ( $version == EZ_CLASS_VERSION_STATUS_DEFINED )
1248              $version_sort = "asc";
1249          $rows = eZPersistentObject::fetchObjectList( eZContentClass::definition(),
1250                                                        null,
1251                                                        $conds,
1252                                                        array( "version" => $version_sort ),
1253                                                        array( "offset" => 0,
1254                                                               "length" => 2 ),
1255                                                        false );
1256          if ( count( $rows ) == 0 )
1257          {
1258              $contentClass = null;
1259              return $contentClass;
1260          }
1261  
1262          $row =& $rows[0];
1263          $row["version_count"] = count( $rows );
1264  
1265          if ( $asObject )
1266              $contentClass = new eZContentClass( $row );
1267          else
1268              $contentClass = $row;
1269  
1270          return $contentClass;
1271      }
1272  
1273      /*!
1274       \static
1275      */
1276      function fetchList( $version = EZ_CLASS_VERSION_STATUS_DEFINED, $asObject = true, $user_id = false,
1277                           $sorts = null, $fields = null, $classFilter = false, $limit = null )
1278      {
1279          $conds = array();
1280          $custom_fields = null;
1281          $custom_tables = null;
1282          $custom_conds = null;
1283  
1284          if ( is_numeric( $version ) )
1285              $conds["version"] = $version;
1286          if ( $user_id !== false and is_numeric( $user_id ) )
1287              $conds["creator_id"] = $user_id;
1288          if ( $classFilter )
1289          {
1290              $classIDCount = 0;
1291              $classIdentifierCount = 0;
1292  
1293              $classIDFilter = array();
1294              $classIdentifierFilter = array();
1295              foreach ( $classFilter as $classType )
1296              {
1297                  if ( is_numeric( $classType ) )
1298                  {
1299                      $classIDFilter[] = $classType;
1300                      $classIDCount++;
1301                  }
1302                  else
1303                  {
1304                      $classIdentifierFilter[] = $classType;
1305                      $classIdentifierCount++;
1306                  }
1307              }
1308  
1309              if ( $classIDCount > 1 )
1310                  $conds['id'] = array( $classIDFilter );
1311              else if ( $classIDCount == 1 )
1312                  $conds['id'] = $classIDFilter[0];
1313              if ( $classIdentifierCount > 1 )
1314                  $conds['identifier'] = array( $classIdentifierFilter );
1315              else if ( $classIdentifierCount == 1 )
1316                  $conds['identifier'] = $classIdentifierFilter[0];
1317          }
1318  
1319          if ( $sorts && isset( $sorts['name'] ) )
1320          {
1321              $nameFiler = eZContentClassName::sqlFilter( 'ezcontentclass' );
1322              $custom_tables = array( $nameFiler['from'] );
1323              $custom_conds = "AND " . $nameFiler['where'];
1324              $custom_fields = array( $nameFiler['nameField'] );
1325  
1326              $sorts[$nameFiler['orderBy']] = $sorts['name'];
1327              unset( $sorts['name'] );
1328          }
1329  
1330          return eZPersistentObject::fetchObjectList( eZContentClass::definition(),
1331                                                              $fields,
1332                                                              $conds,
1333                                                              $sorts,
1334                                                              $limit,
1335                                                              $asObject,
1336                                                              false,
1337                                                              $custom_fields,
1338                                                              $custom_tables,
1339                                                              $custom_conds );
1340      }
1341  
1342      /*!
1343       Returns all attributes as an associative array with the key taken from the attribute identifier.
1344      */
1345      function &dataMap()
1346      {
1347          $map =& $this->DataMap[$this->Version];
1348          if ( !isset( $map ) )
1349          {
1350              $map = array();
1351              $attributes = $this->fetchAttributes( false, true, $this->Version );
1352              foreach ( array_keys( $attributes ) as $attributeKey )
1353              {
1354                  $attribute =& $attributes[$attributeKey];
1355                  $map[$attribute->attribute( 'identifier' )] =& $attribute;
1356              }
1357          }
1358          return $map;
1359      }
1360  
1361      function &fetchAttributes( $id = false, $asObject = true, $version = EZ_CLASS_VERSION_STATUS_DEFINED )
1362      {
1363          if ( $id === false )
1364          {
1365              if ( isset( $this ) and
1366                   get_class( $this ) == "ezcontentclass" )
1367              {
1368                  $id = $this->ID;
1369                  $version = $this->Version;
1370              }
1371              else
1372              {
1373                  $attributes = null;
1374                  return $attributes;
1375              }
1376          }
1377  
1378          $filteredList =& eZContentClassAttribute::fetchFilteredList( array( "contentclass_id" => $id,
1379                                                                              "version" => $version ),
1380                                                                       $asObject );
1381          return $filteredList;
1382      }
1383  
1384      /*!
1385       Fetch class attribute by identifier, return null if none exist.
1386  
1387       \param attribute identifier.
1388  
1389       \return Class Attribute, null if none matched
1390      */
1391      function &fetchAttributeByIdentifier( $identifier, $asObject = true )
1392      {
1393          $attributeArray =& eZContentClassAttribute::fetchFilteredList( array( 'contentclass_id' => $this->ID,
1394                                                                                'version' => $this->Version,
1395                                                                                'identifier' => $identifier ), $asObject );
1396          if ( count( $attributeArray ) > 0 )
1397              return $attributeArray[0];
1398          $retValue = null;
1399          return $retValue;
1400      }
1401  
1402      function fetchSearchableAttributes( $id = false, $asObject = true, $version = EZ_CLASS_VERSION_STATUS_DEFINED )
1403      {
1404          if ( $id === false )
1405          {
1406              if ( isset( $this ) and
1407                   get_class( $this ) == "ezcontentclass" )
1408              {
1409                  $id = $this->ID;
1410                  $version = $this->Version;
1411              }
1412              else
1413                  return null;
1414          }
1415  
1416          return eZContentClassAttribute::fetchFilteredList( array( "contentclass_id" => $id,
1417                                                                    "is_searchable" => 1,
1418                                                                    "version" => $version ), $asObject );
1419      }
1420  
1421      /*!
1422       \note The reference for the return value is required to workaround
1423             a bug with PHP references.
1424      */
1425      function &versionStatus()
1426      {
1427  
1428          if ( $this->VersionCount == 1 )
1429          {
1430              if ( $this->Version == EZ_CLASS_VERSION_STATUS_TEMPORARY )
1431                  $status = EZ_CLASS_VERSION_STATUS_TEMPORARY;
1432              else
1433                  $status = EZ_CLASS_VERSION_STATUS_DEFINED;
1434          }
1435          else
1436              $status = EZ_CLASS_VERSION_STATUS_MODIFED;
1437          return $status;
1438      }
1439  
1440      /*!
1441       \deprecated
1442       \return The version count for the class if has been determined.
1443       \note The reference for the return value is required to workaround
1444             a bug with PHP references.
1445      */
1446      function &versionCount()
1447      {
1448          return $this->VersionCount;
1449      }
1450  
1451      /*!
1452       Will generate a name for the content object based on the class
1453       settings for content object.
1454      */
1455      function contentObjectName( &$contentObject, $version = false, $translation = false )
1456      {
1457  
1458          $contentObjectName = $this->ContentObjectName;
1459          $dataMap =& $contentObject->fetchDataMap( $version, $translation );
1460  
1461          eZDebugSetting::writeDebug( 'kernel-content-class', $dataMap, "data map" );
1462          preg_match_all( "/[<|\|](\(.+\))[\||>]/U",
1463                          $contentObjectName,
1464                          $subTagMatchArray );
1465  
1466          $i = 0;
1467          $tmpTagResultArray = array();
1468          foreach ( $subTagMatchArray[1]  as $subTag )
1469          {
1470              $tmpTag = 'tmptag' . $i;
1471  
1472              $contentObjectName = str_replace( $subTag, $tmpTag, $contentObjectName );
1473  
1474              $subTag = substr( $subTag, 1,strlen($subTag) - 2 );
1475              $tmpTagResultArray[$tmpTag] = eZContentClass::buildContentObjectName( $subTag, $dataMap );
1476              $i++;
1477          }
1478          $contentObjectName = eZContentClass::buildContentObjectName( $contentObjectName, $dataMap, $tmpTagResultArray );
1479          return $contentObjectName;
1480      }
1481  
1482      /*!
1483       Generates a name for the content object based on the content object name pattern
1484       and data map of an object.
1485      */
1486      function buildContentObjectName( &$contentObjectName, $dataMap, $tmpTags = false )
1487      {
1488  
1489          preg_match_all( "|<[^>]+>|U",
1490                          $contentObjectName,
1491                          $tagMatchArray );
1492  
1493          foreach ( $tagMatchArray[0] as $tag )
1494          {
1495              $tagName = str_replace( "<", "", $tag );
1496              $tagName = str_replace( ">", "", $tagName );
1497  
1498              $tagParts = explode( '|', $tagName );
1499  
1500              $namePart = "";
1501              foreach ( $tagParts as $name )
1502              {
1503                  // get the value of the attribute to use in name
1504                  if ( isset( $dataMap[$name] ) )
1505                  {
1506                      $namePart = $dataMap[$name]->title();
1507                      if ( $namePart != "" )
1508                          break;
1509                  }
1510                  elseif ( $tmpTags && isset( $tmpTags[$name] ) && $tmpTags[$name] != '' )
1511                  {
1512                      $namePart = $tmpTags[$name];
1513                      break;
1514                  }
1515  
1516              }
1517  
1518              // replace tag with object name part
1519              $contentObjectName = str_replace( $tag, $namePart, $contentObjectName );
1520          }
1521          return $contentObjectName;
1522      }
1523  
1524      /*!
1525       \return will return the number of objects published by this class.
1526      */
1527      function &objectCount()
1528      {
1529          $db =& eZDB::instance();
1530  
1531          $countRow = $db->arrayQuery( 'SELECT count(*) AS count FROM ezcontentobject '.
1532                                       'WHERE contentclass_id='.$this->ID ." and status = " . EZ_CONTENT_OBJECT_STATUS_PUBLISHED );
1533  
1534          return $countRow[0]['count'];
1535      }
1536  
1537      /*!
1538       \return Sets the languages which are allowed to be instantiated for the class.
1539       Used only for the content/ fetch function.
1540      */
1541      function setCanInstantiateLanguages( $languageCodes )
1542      {
1543          $this->CanInstantiateLanguages = $languageCodes;
1544      }
1545  
1546      function &canInstantiateLanguages()
1547      {
1548          if ( is_array( $this->CanInstantiateLanguages ) )
1549              $languageCodes = array_intersect( eZContentLanguage::prioritizedLanguageCodes(), $this->CanInstantiateLanguages );
1550          else
1551              $languageCodes = array();
1552          return $languageCodes;
1553      }
1554  
1555      /*!
1556       \static
1557       Returns a contentclass name from serialized array \a $serializedNameList using
1558       top language from siteaccess language list or 'always available' name
1559       from \a $serializedNameList.
1560  
1561       \return string with contentclass name.
1562      */
1563      function nameFromSerializedString( $serailizedNameList )
1564      {
1565          return eZContentClassNameList::nameFromSerializedString( $serailizedNameList );
1566      }
1567  
1568      function hasNameInLanguage( $languageLocale )
1569      {
1570          $hasName = $this->NameList->hasNameInLocale( $languageLocale );
1571          return $hasName;
1572      }
1573  
1574      /*!
1575       Returns a contentclass name in \a $languageLocale language.
1576       Uses siteaccess language list or 'always available' language if \a $languageLocale is 'false'.
1577  
1578       \return string with contentclass name.
1579      */
1580      function &name( $languageLocale = false )
1581      {
1582          $name = $this->NameList->name( $languageLocale );
1583          return $name;
1584      }
1585  
1586      function setName( $name, $languageLocale = false )
1587      {
1588          if ( !$languageLocale )
1589              $languageLocale = $this->topPriorityLanguageLocale();
1590          $this->NameList->setNameByLanguageLocale( $name, $languageLocale );
1591  
1592          $languageID = eZContentLanguage::idByLocale( $languageLocale );
1593          $languageMask = $this->attribute( 'language_mask' );
1594          $this->setAttribute( 'language_mask', $languageMask | $languageID );
1595      }
1596  
1597      function setAlwaysAvailableLanguageID( $languageID, $updateChilds = true )
1598      {
1599          $db =& eZDB::instance();
1600          $db->begin();
1601  
1602          $languageLocale = false;
1603          if ( $languageID )
1604          {
1605              $language = eZContentLanguage::fetch( $languageID );
1606              $languageLocale = $language->attribute( 'locale' );
1607          }
1608  
1609          if ( $languageID )
1610          {
1611              $this->setAttribute( 'language_mask', (int)$this->attribute( 'language_mask' ) | 1 );
1612              $this->NameList->setAlwaysAvailableLanguage( $languageLocale );
1613          }
1614          else
1615          {
1616              $this->setAttribute( 'language_mask', (int)$this->attribute( 'language_mask' ) & ~1 );
1617              $this->NameList->setAlwaysAvailableLanguage( false );
1618          }
1619          $this->store();
1620  
1621          $classID = $this->attribute( 'id' );
1622          $version = $this->attribute( 'version' );
1623  
1624          $attributes =& $this->fetchAttributes();
1625          foreach( array_keys( $attributes ) as $attrKey )
1626          {
1627              $attribute =& $attributes[$attrKey];
1628              $attribute->setAlwaysAvailableLanguage( $languageLocale );
1629              $attribute->store();
1630          }
1631  
1632          // reset 'always available' flag
1633          $sql = "UPDATE ezcontentclass_name SET language_id=";
1634          if ( $db->databaseName() == 'oracle' )
1635          {
1636              $sql .= "bitand( language_id, -2 )";
1637          }
1638          else
1639          {
1640              $sql .= "language_id & ~1";
1641          }
1642          $sql .= " WHERE contentclass_id = '$classID' AND contentclass_version = '$version'";
1643          $db->query( $sql );
1644  
1645          if ( $languageID != false )
1646          {
1647              $newLanguageID = $languageID | 1;
1648  
1649              $sql = "UPDATE ezcontentclass_name
1650                      SET language_id='$newLanguageID'
1651                  WHERE language_id='$languageID' AND contentclass_id = '$classID' AND contentclass_version = '$version'";
1652              $db->query( $sql );
1653          }
1654  
1655          $db->commit();
1656      }
1657  
1658      function clearAlwaysAvailableLanguageID()
1659      {
1660          $this->setAlwaysAvailableLanguageID( false );
1661      }
1662  
1663      /*!
1664       Wrapper for eZContentClassNameList::languages.
1665      */
1666      function &languages()
1667      {
1668          $languages = $this->NameList->languages();
1669          return $languages;
1670      }
1671  
1672      /*!
1673       Wrapper for eZContentClassNameList::prioritizedLanguages.
1674      */
1675      function &prioritizedLanguages()
1676      {
1677          $languages = $this->NameList->prioritizedLanguages();
1678          return $languages;
1679      }
1680  
1681      /*!
1682       Wrapper for eZContentClassNameList::untranslatedLanguages.
1683      */
1684      function &canCreateLanguages()
1685      {
1686          $languages = $this->NameList->untranslatedLanguages();
1687          return $languages;
1688      }
1689  
1690      /*!
1691       Wrapper for eZContentClassNameList::topPriorityLanguageLocale.
1692      */
1693      function &topPriorityLanguageLocale()
1694      {
1695          $languageLocale = $this->NameList->topPriorityLanguageLocale();
1696          return $languageLocale;
1697      }
1698  
1699      /*!
1700       Wrapper for eZContentClassNameList::alwaysAvailableLanguage.
1701  
1702       \return 'language' object.
1703      */
1704      function &alwaysAvailableLanguage()
1705      {
1706          $language = $this->NameList->alwaysAvailableLanguage();
1707          return $language;
1708      }
1709  
1710      /*!
1711       Wrapper for eZContentClassNameList::alwaysAvailableLanguageLocale.
1712  
1713       \return 'language' object.
1714      */
1715      function alwaysAvailableLanguageLocale()
1716      {
1717          $language = $this->NameList->alwaysAvailableLanguageLocale();
1718          return $language;
1719      }
1720  
1721      function &nameList()
1722      {
1723          $nameList = $this->NameList->nameList();
1724          return $nameList;
1725      }
1726  
1727      /*!
1728       Removes translated name for specified by \a $languageID language.
1729      */
1730      function removeTranslation( $languageID )
1731      {
1732          $language = eZContentLanguage::fetch( $languageID );
1733  
1734          if ( !$language )
1735          {
1736              return false;
1737          }
1738  
1739          // check if it is not the initial language
1740          $classInitialLanguageID = $this->attribute( 'initial_language_id' );
1741          if ( $classInitialLanguageID == $languageID )
1742          {
1743              return false;
1744          }
1745  
1746          $db =& eZDB::instance();
1747          $db->begin();
1748  
1749          $classID = $this->attribute( 'id' );
1750          $languageID = $language->attribute( 'id' );
1751  
1752          // change language_mask of the object
1753          $languageMask = (int) $this->attribute( 'language_mask' );
1754          $languageMask = (int) $languageMask & ~ (int) $languageID;
1755          $this->setAttribute( 'language_mask', $languageMask );
1756  
1757          // Remove all names in the language
1758          $db->query( "DELETE FROM ezcontentclass_name
1759                       WHERE contentclass_id='$classID'
1760                         AND language_id='$languageID'" );
1761  
1762          $languageLocale = $language->attribute( 'locale' );
1763          $this->NameList->removeName( $languageLocale );
1764  
1765          $this->store();
1766  
1767          // Remove names for attributes in the language
1768          $attributes = $this->fetchAttributes();
1769          foreach ( array_keys( $attributes ) as $attr_key )
1770          {
1771              $attribute =& $attributes[$attr_key];
1772              $attribute->removeTranslation( $languageLocale );
1773              $attribute->store();
1774              unset( $attribute );
1775          }
1776  
1777          $db->commit();
1778  
1779          return true;
1780      }
1781  
1782      /// \privatesection
1783      var $ID;
1784      // serialized array of translated class names
1785      var $SerializedNameList;
1786      // unserialized class names
1787      var $NameList;
1788      var $Identifier;
1789      var $ContentObjectName;
1790      var $Version;
1791      var $VersionCount;
1792      var $CreatorID;
1793      var $ModifierID;
1794      var $Created;
1795      var $Modified;
1796      var $InGroups;
1797      var $AllGroups;
1798      var $IsContainer;
1799      var $CanInstantiateLanguages;
1800      var $LanguageMask;
1801  }
1802  
1803  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7