Exponential Basic 2.4.0.1 Exponential Basic CMS — PHP Class Reference

ezcontentobjectstate.php
Go to the documentation of this file.
1 <?php
18 class eZContentObjectState extends eZPersistentObject
19 {
20 public $DefaultLanguageID;
21 public $GroupID;
22 const MAX_IDENTIFIER_LENGTH = 45;
23
24 static function definition()
25 {
26 static $definition = array( "fields" => array( "id" => array( "name" => "ID",
27 "datatype" => "integer",
28 "required" => true ),
29 "group_id" => array( "name" => "GroupID",
30 "datatype" => "integer",
31 "required" => true,
32 "foreign_class" => "eZContentObjectStateGroup",
33 "foreign_attribute" => "id",
34 "multiplicity" => "1..*" ),
35 "identifier" => array( "name" => "Identifier",
36 "datatype" => "string",
37 "required" => true,
38 "max_length" => self::MAX_IDENTIFIER_LENGTH ),
39 "language_mask" => array( "name" => "LanguageMask",
40 "datatype" => "integer",
41 "default" => 0,
42 "required" => true ),
43 "default_language_id" => array( "name" => "DefaultLanguageID",
44 "datatype" => "integer",
45 "required" => true ),
46 "priority" => array( "name" => "Order",
47 "datatype" => "integer",
48 "required" => true,
49 "default" => 0 ) ),
50 "keys" => array( "id" ),
51 "function_attributes" => array( "current_translation" => "currentTranslation",
52 "all_translations" => "allTranslations",
53 "translations" => "translations",
54 "languages" => "languages",
55 "available_languages" => "availableLanguages",
56 "default_language" => "defaultLanguage",
57 "object_count" => "objectCount",
58 "group" => "group" ),
59 "increment_key" => "id",
60 "class_name" => "eZContentObjectState",
61 "sort" => array( "group_id" => "asc", "priority" => "asc" ),
62 "name" => "ezcobj_state" );
63 return $definition;
64 }
65
71 public static function fetchById( $id )
72 {
73 $id = (int)$id;
74 $states = self::fetchByConditions( array( "ezcobj_state.id=$id" ), 1, 0 );
75 $state = count( $states ) > 0 ? $states[0] : false;
76 return $state;
77 }
78
87 public static function fetchByIdentifier( $identifier, $groupID )
88 {
89 $db = eZDB::instance();
90 $identifier = $db->escapeString( $identifier );
91 $states = self::fetchByConditions( array( "ezcobj_state.identifier='$identifier'", "ezcobj_state_group.id=$groupID" ), 1, 0 );
92 $state = count( $states ) > 0 ? $states[0] : false;
93 return $state;
94 }
95
107 private static function fetchByConditions( $conditions, $limit, $offset )
108 {
109 $db = eZDB::instance();
110
111 $defaultConditions = array(
112 'ezcobj_state.group_id=ezcobj_state_group.id',
113 'ezcobj_state_language.contentobject_state_id=ezcobj_state.id',
114 eZContentLanguage::languagesSQLFilter( 'ezcobj_state' ),
115 eZContentLanguage::sqlFilter( 'ezcobj_state_language', 'ezcobj_state' )
116 );
117
118 $conditions = array_merge( $conditions, $defaultConditions );
119
120 $conditionsSQL = implode( ' AND ', $conditions );
121
122 $sql = "SELECT ezcobj_state.*, ezcobj_state_language.* " .
123 "FROM ezcobj_state, ezcobj_state_group, ezcobj_state_language " .
124 "WHERE $conditionsSQL " .
125 "ORDER BY ezcobj_state.priority";
126
127 $rows = $db->arrayQuery( $sql, array( 'limit' => $limit, 'offset' => $offset ) );
128
129 $states = array();
130 foreach ( $rows as $row )
131 {
132 $state = new eZContentObjectState( $row );
133 $stateLanguage = new eZContentObjectStateLanguage( $row );
134 $state->setLanguageObject( $stateLanguage );
135 $states[] = $state;
136 }
137
138 return $states;
139 }
140
150 public static function fetchByGroup( $groupID, $limit = false, $offset = false )
151 {
152 $groupID = (int)$groupID;
153 return self::fetchByConditions( array( "ezcobj_state_group.id=$groupID" ), $limit, $offset );
154 }
155
159 private function setLanguageObject( eZContentObjectStateLanguage $stateLanguage )
160 {
161 $this->LanguageObject = $stateLanguage;
162 }
163
169 public function currentTranslation()
170 {
171 return $this->LanguageObject;
172 }
173
180 public function setCurrentLanguage( $locale )
181 {
182 $lang = eZContentLanguage::fetchByLocale( $locale );
183 $langID = $lang->attribute( 'id' );
184 foreach ( $this->translations() as $translation )
185 {
186 if ( $translation->attribute( 'language_id' ) == $langID )
187 {
188 $this->setLanguageObject( $translation );
189 return true;
190 }
191 }
192
193 return false;
194 }
195
201 public function allTranslations()
202 {
203 if ( !is_array( $this->AllTranslations ) )
204 {
205 $allTranslations = array();
206 foreach ( $this->translations() as $translation )
207 {
208 $languageID = $translation->attribute( 'language_id' ) & ~1;
209 $allTranslations[$languageID] = $translation;
210 }
211
212 $languages = eZContentLanguage::fetchList();
213 foreach ( $languages as $language )
214 {
215 $languageID = $language->attribute( 'id' );
216 if ( !array_key_exists( $languageID, $allTranslations ) )
217 {
218 $row = array( 'language_id' => $languageID );
219 if ( isset( $this->ID ) )
220 {
221 $row['contentobject_state_id'] = $this->ID;
222 }
223 $allTranslations[$languageID] = new eZContentObjectStateLanguage( $row );
224 }
225 }
226 ksort( $allTranslations );
227 // array_values is needed here to reset keys, otherwise eZHTTPPersistence::fetch() won't work
228 $this->AllTranslations = array_values( $allTranslations );
229 }
230 return $this->AllTranslations;
231 }
232
233 public function translationByLocale( $locale )
234 {
235 $languageID = eZContentLanguage::idByLocale( $locale );
236
237 if ( $languageID )
238 {
239 $translations = $this->allTranslations();
240 foreach ( $translations as $translation )
241 {
242 if ( $translation->realLanguageID() == $languageID )
243 {
244 return $translation;
245 }
246 }
247 }
248
249 return false;
250 }
251
257 public function translations()
258 {
259 if ( !isset( $this->ID ) )
260 {
261 $this->Translations = array();
262 }
263 else if ( !is_array( $this->Translations ) )
264 {
265 $this->Translations = eZContentObjectStateLanguage::fetchByState( $this->ID );
266 }
267 return $this->Translations;
268 }
269
275 public function languages()
276 {
277 return isset( $this->LanguageMask ) ? eZContentLanguage::prioritizedLanguagesByMask( $this->LanguageMask ) : array();
278 }
279
284 public function availableLanguages()
285 {
286 $languages = array();
287 $languageObjects = $this->languages();
288
289 foreach ( $languageObjects as $languageObject )
290 {
291 $languages[] = $languageObject->attribute( 'locale' );
292 }
293
294 return $languages;
295 }
296
305 public function store( $fieldFilters = null )
306 {
307 if ( $fieldFilters === null )
308 {
309 $db = eZDB::instance();
310
311 $db->begin();
312
313 $languageMask = 1;
314 // set language mask and always available bits
315 foreach ( $this->AllTranslations() as $translation )
316 {
317 if ( $translation->hasData() )
318 {
319 $languageID = $translation->attribute( 'language_id' );
320 if ( empty( $this->DefaultLanguageID ) )
321 {
322 $this->DefaultLanguageID = $languageID & ~1;
323 }
324 // if default language, set always available flag
325 if ( $languageID & $this->DefaultLanguageID )
326 {
327 $translation->setAttribute( 'language_id', $languageID | 1 );
328 }
329 // otherwise, remove always available flag if it's set
330 else if ( $languageID & 1 )
331 {
332 $translation->setAttribute( 'language_id', $languageID & ~1 );
333 }
334
335 $languageMask = $languageMask | $languageID;
336 }
337 }
338
339 $assignToObjects = false;
340 if ( !isset( $this->ID ) )
341 {
342 $rows = $db->arrayQuery( "SELECT MAX(priority) AS max_priority FROM ezcobj_state WHERE group_id=" . $this->GroupID );
343
344 if ( count( $rows ) > 0 && $rows[0]['max_priority'] !== null )
345 {
346 $this->setAttribute( 'priority', $rows[0]['max_priority'] + 1 );
347 }
348 else
349 {
350 // this is the first state created in the state group
351 // make all content objects use this state
352 $assignToObjects = true;
353 }
354 }
355
356 $this->setAttribute( 'language_mask', $languageMask );
357
358 // store state
359 eZPersistentObject::storeObject( $this, $fieldFilters );
360
361 // store or remove translations
362 foreach ( $this->AllTranslations as $translation )
363 {
364 if ( !$translation->hasData() )
365 {
366 // the name and description are empty
367 // so the translation needs to be removed if it was stored before
368 if ( $translation->attribute( 'contentobject_state_id' ) !== null )
369 {
370 $translation->remove();
371 }
372 }
373 else
374 {
375 if ( $translation->attribute( 'contentobject_state_id' ) != $this->ID )
376 {
377 $translation->setAttribute( 'contentobject_state_id', $this->ID );
378 }
379
380 $translation->store();
381 }
382 }
383
384 if ( $assignToObjects )
385 {
386 $stateID = $this->ID;
387 $db->query( "INSERT INTO ezcobj_state_link (contentobject_id, contentobject_state_id) SELECT id, $stateID FROM ezcontentobject" );
388 }
389
390 $db->commit();
391 }
392 else
393 {
394 parent::store( $fieldFilters );
395 }
396 }
397
402 public function defaultLanguage()
403 {
404 return eZContentLanguage::fetch( $this->DefaultLanguageID );
405 }
406
414 public function isValid( &$messages = array() )
415 {
416 $isValid = true;
417 // missing identifier
418 if ( !isset( $this->Identifier ) || $this->Identifier == '' )
419 {
420 $messages[] = ezpI18n::tr( 'kernel/state/edit', 'Identifier: input required' );
421 $isValid = false;
422 }
423 else
424 {
425 // make sure the identifier contains only valid characters
426 $trans = eZCharTransform::instance();
427 $validIdentifier = $trans->transformByGroup( $this->Identifier, 'identifier' );
428 if ( strcmp( $validIdentifier, $this->Identifier ) != 0 )
429 {
430 // invalid identifier
431 $messages[] = ezpI18n::tr( 'kernel/state/edit', 'Identifier: invalid, it can only consist of characters in the range a-z, 0-9 and underscore.' );
432 $isValid = false;
433 }
434 else if ( strlen( $this->Identifier ) > self::MAX_IDENTIFIER_LENGTH )
435 {
436 $messages[] = ezpI18n::tr( 'kernel/state/edit', 'Identifier: invalid, maximum %max characters allowed.',
437 null, array( '%max' => self::MAX_IDENTIFIER_LENGTH ) );
438 $isValid = false;
439 }
440 else if ( isset( $this->GroupID ) )
441 {
442 // check for existing identifier
443 $existingState = self::fetchByIdentifier( $this->Identifier, $this->GroupID );
444 if ( $existingState && ( !isset( $this->ID ) || $existingState->attribute( 'id' ) !== $this->ID ) )
445 {
446 $messages[] = ezpI18n::tr( 'kernel/state/edit', 'Identifier: a content object state group with this identifier already exists, please give another identifier' );
447 $isValid = false;
448 }
449 }
450 }
451
452 $translationsWithData = 0;
453 foreach ( $this->AllTranslations as $translation )
454 {
455 if ( $translation->hasData() )
456 {
457 $translationsWithData++;
458 if ( !$translation->isValid( $messages ) )
459 {
460 $isValid = false;
461 }
462 }
463 else if ( ( $translation->attribute( 'language_id' ) & ~1 ) == $this->DefaultLanguageID )
464 {
465 // if name nor description are set but translation is specified as main language
466 $isValid = false;
467 $messages[] = ezpI18n::tr( 'kernel/state/edit', '%language_name: this language is the default but neither name or description were provided for this language', null, array( '%language_name' => $translation->attribute( 'language' )->attribute( 'locale_object' )->attribute( 'intl_language_name' ) ) );
468 }
469 }
470
471 if ( $translationsWithData == 0 )
472 {
473 $isValid = false;
474 $messages[] = ezpI18n::tr( 'kernel/state/edit', 'Translations: you need to add at least one localization' );
475 }
476 else if ( empty( $this->DefaultLanguageID ) && $translationsWithData > 1 )
477 {
478 $isValid = false;
479 $messages[] = ezpI18n::tr( 'kernel/state/edit', 'Translations: there are multiple localizations but you did not specify which is the default one' );
480 }
481
482 return $isValid;
483 }
484
485 public function group()
486 {
487 return eZContentObjectStateGroup::fetchById( $this->GroupID );
488 }
489
495 public static function limitationList()
496 {
497 $sql = "SELECT g.identifier group_identifier, s.identifier state_identifier, s.priority, s.id " .
498 "FROM ezcobj_state s, ezcobj_state_group g " .
499 "WHERE s.group_id=g.id " .
500 "AND g.identifier NOT LIKE 'ez%' " .
501 "ORDER BY g.identifier, s.priority";
502 $db = eZDB::instance();
503 $rows = $db->arrayQuery( $sql );
504 $limitationList = array();
505 foreach ( $rows as $row )
506 {
507 $limitationList[] = array( 'name' => $row['group_identifier'] . '/' . $row['state_identifier'], 'id' => $row['id'] );
508 }
509
510 return $limitationList;
511 }
512
519 public static function defaults()
520 {
521 if ( !is_array( self::$Defaults ) )
522 {
523 self::$Defaults = eZPersistentObject::fetchObjectList( self::definition(), null, array( 'priority' => 0 ) );
524 }
525
526 return self::$Defaults;
527 }
528
532 public static function cleanDefaultsCache()
533 {
534 self::$Defaults = null;
535 }
536
544 public function fetchHTTPPersistentVariables()
545 {
546 $translations = $this->allTranslations();
547
548 $http = eZHTTPTool::instance();
549 eZHTTPPersistence::fetch( 'ContentObjectState' , eZContentObjectState::definition(), $this, $http, false );
550 eZHTTPPersistence::fetch( 'ContentObjectState' , eZContentObjectStateLanguage::definition(), $translations, $http, true );
551 }
552
560 public static function removeByID( $id )
561 {
562 $db = eZDB::instance();
563 $db->begin();
564 $db->query( "DELETE FROM ezcobj_state_link WHERE contentobject_state_id=$id" );
565 eZPersistentObject::removeObject( eZContentObjectStateLanguage::definition(), array( 'contentobject_state_id' => $id ) );
566 eZPersistentObject::removeObject( eZContentObjectState::definition(), array( 'id' => $id ) );
567 $db->commit();
568 }
569
573 public function objectCount()
574 {
575 $db = eZDB::instance();
576 $id = $this->ID;
577 $result = $db->arrayQuery( "SELECT COUNT(contentobject_id) AS object_count FROM ezcobj_state_link WHERE contentobject_state_id=$id" );
578 return $result[0]['object_count'];
579 }
580
581 private $LanguageObject;
582 private $Translations;
583 private $AllTranslations;
584 private static $Defaults = null;
585 }
586 ?>
$locale
$locale
Definition: admin/adlist.php:127
$http
$http
Definition: authorize.php:20
eZCharTransform\instance
static instance()
Definition: ezchartransform.php:435
eZContentLanguage\fetchList
static fetchList( $forceReloading=false)
Definition: ezcontentlanguage.php:164
eZContentLanguage\sqlFilter
static sqlFilter( $languageTable, $languageListTable=null, $languageAttributeName='language_id', $languageListAttributeName='language_mask', $lang=false)
Definition: ezcontentlanguage.php:693
eZContentLanguage\fetch
static fetch( $id)
Definition: ezcontentlanguage.php:252
eZContentLanguage\idByLocale
static idByLocale( $locale)
Definition: ezcontentlanguage.php:638
eZContentLanguage\prioritizedLanguagesByMask
static prioritizedLanguagesByMask( $mask)
Definition: ezcontentlanguage.php:455
eZContentLanguage\languagesSQLFilter
static languagesSQLFilter( $languageListTable, $languageListAttributeName='language_mask')
Definition: ezcontentlanguage.php:660
eZContentLanguage\fetchByLocale
static fetchByLocale( $locale, $createIfNotExist=false)
Definition: ezcontentlanguage.php:265
eZContentObjectStateGroup\fetchById
static fetchById( $id)
Definition: ezcontentobjectstategroup.php:68
eZContentObjectStateLanguage
Definition: ezcontentobjectstatelanguage.php:22
eZContentObjectStateLanguage\definition
static definition()
Definition: ezcontentobjectstatelanguage.php:25
eZContentObjectStateLanguage\fetchByState
static fetchByState( $id)
Definition: ezcontentobjectstatelanguage.php:61
eZContentObjectState
Definition: ezcontentobjectstate.php:19
eZContentObjectState\fetchByIdentifier
static fetchByIdentifier( $identifier, $groupID)
Definition: ezcontentobjectstate.php:87
eZContentObjectState\store
store( $fieldFilters=null)
Definition: ezcontentobjectstate.php:305
eZContentObjectState\$LanguageObject
$LanguageObject
Definition: ezcontentobjectstate.php:581
eZContentObjectState\$GroupID
$GroupID
Definition: ezcontentobjectstate.php:21
eZContentObjectState\defaultLanguage
defaultLanguage()
Definition: ezcontentobjectstate.php:402
eZContentObjectState\removeByID
static removeByID( $id)
Definition: ezcontentobjectstate.php:560
eZContentObjectState\$Translations
$Translations
Definition: ezcontentobjectstate.php:582
eZContentObjectState\translationByLocale
translationByLocale( $locale)
Definition: ezcontentobjectstate.php:233
eZContentObjectState\cleanDefaultsCache
static cleanDefaultsCache()
Definition: ezcontentobjectstate.php:532
eZContentObjectState\limitationList
static limitationList()
Definition: ezcontentobjectstate.php:495
eZContentObjectState\translations
translations()
Definition: ezcontentobjectstate.php:257
eZContentObjectState\availableLanguages
availableLanguages()
Definition: ezcontentobjectstate.php:284
eZContentObjectState\$AllTranslations
$AllTranslations
Definition: ezcontentobjectstate.php:583
eZContentObjectState\allTranslations
allTranslations()
Definition: ezcontentobjectstate.php:201
eZContentObjectState\fetchByGroup
static fetchByGroup( $groupID, $limit=false, $offset=false)
Definition: ezcontentobjectstate.php:150
eZContentObjectState\currentTranslation
currentTranslation()
Definition: ezcontentobjectstate.php:169
eZContentObjectState\objectCount
objectCount()
Definition: ezcontentobjectstate.php:573
eZContentObjectState\MAX_IDENTIFIER_LENGTH
const MAX_IDENTIFIER_LENGTH
Definition: ezcontentobjectstate.php:22
eZContentObjectState\fetchHTTPPersistentVariables
fetchHTTPPersistentVariables()
Definition: ezcontentobjectstate.php:544
eZContentObjectState\$DefaultLanguageID
$DefaultLanguageID
Definition: ezcontentobjectstate.php:20
eZContentObjectState\setCurrentLanguage
setCurrentLanguage( $locale)
Definition: ezcontentobjectstate.php:180
eZContentObjectState\fetchById
static fetchById( $id)
Definition: ezcontentobjectstate.php:71
eZContentObjectState\definition
static definition()
Definition: ezcontentobjectstate.php:24
eZContentObjectState\$Defaults
static $Defaults
Definition: ezcontentobjectstate.php:584
eZContentObjectState\languages
languages()
Definition: ezcontentobjectstate.php:275
eZContentObjectState\group
group()
Definition: ezcontentobjectstate.php:485
eZContentObjectState\defaults
static defaults()
Definition: ezcontentobjectstate.php:519
eZContentObjectState\setLanguageObject
setLanguageObject(eZContentObjectStateLanguage $stateLanguage)
Definition: ezcontentobjectstate.php:159
eZContentObjectState\fetchByConditions
static fetchByConditions( $conditions, $limit, $offset)
Definition: ezcontentobjectstate.php:107
eZContentObjectState\isValid
isValid(&$messages=array())
Definition: ezcontentobjectstate.php:414
eZPersistentObject
Definition: ezpersistentobject.php:34
eZPersistentObject\$ID
$ID
Definition: ezpersistentobject.php:1449
eZPersistentObject\removeObject
static removeObject( $def, $conditions=null, $extraConditions=null)
Definition: ezpersistentobject.php:272
eZPersistentObject\storeObject
static storeObject( $obj, $fieldFilters=null)
Definition: ezpersistentobject.php:339
eZPersistentObject\setAttribute
setAttribute( $attr, $val)
Definition: ezpersistentobject.php:1375
eZPersistentObject\count
static count( $def, $conds=null, $field=null)
Definition: ezpersistentobject.php:188
eZPersistentObject\fetchObjectList
static fetchObjectList( $def, $field_filters=null, $conds=null, $sorts=null, $limit=null, $asObject=true, $grouping=false, $custom_fields=null, $custom_tables=null, $custom_conds=null)
Definition: ezpersistentobject.php:807
$result
$result
Definition: companysearch.php:94
$id
$id
Definition: companytypelist.php:193
$lang
$lang
Definition: ecommerce/jscalendar/test.php:4
$db
$db
Definition: ezarticle/user/authorlist.php:53
$row
$row
Definition: ezcalendar/user/dayview.php:403
$groupID
$groupID
Definition: ezgroupeventcalendar/user/dayview.php:90
$limit
$limit
Definition: ezimagecatalogue/admin/imagelist.php:312
$offset
$offset
Definition: ezstats/admin/cron.php:51
$rows
$rows
Definition: frontpage.php:128
$messages
$messages
Definition: homepage.php:328
$language
$language
Definition: messageform.php:32
$translation
$translation
Definition: ordersendt.php:62
$states
$states
Definition: step2.php:47
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.