Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezserializedobjectnamelist.php
Go to the documentation of this file.
1 <?php
11 class eZSerializedObjectNameList
12 {
13 public $nameList;
14 const ALWAYS_AVAILABLE_STR = 'always-available';
15
16 public function __construct( $serializedNamesString = false )
17 {
18 $this->DefaultLanguage = null;
19
20 if ( $serializedNamesString )
21 $this->initFromSerializedList( $serializedNamesString );
22 }
23
24 function initFromSerializedList( $serializedNamesString )
25 {
26 $this->HasDirtyData = false;
27 $this->unserializeNames( $serializedNamesString );
28 }
29
30 function initFromString( $nameString, $languageLocale = false )
31 {
32 if ( !$languageLocale )
33 $languageLocale = $this->defaultLanguageLocale();
34
35 $serializedNameList = serialize( array( $languageLocale => $nameString,
36 eZSerializedObjectNameList::ALWAYS_AVAILABLE_STR => $languageLocale ) );
37 $this->initFromSerializedList( $serializedNameList );
38 }
39
40 function initDefault()
41 {
42 $this->initFromString( '' );
43 }
44
45 function create( $serializedNamesString = false )
46 {
47 $object = new eZSerializedObjectNameList( $serializedNamesString );
48 return $object;
49 }
50
51 function __clone()
52 {
53 }
54
55 function copy( $serializedObjectNameListObject )
56 {
57 $serializedObjectNameListObject->setNameList( $this->nameList() );
58 $serializedObjectNameListObject->setHasDirtyData( $this->hasDirtyData() );
59 $serializedObjectNameListObject->setDefaultLanguage( $this->defaultLanguage() );
60 }
61
62 function mergeNameList( $inNameList )
63 {
64 foreach ( $inNameList as $languageLocale => $name )
65 $this->setNameByLanguageLocale( $name, $languageLocale );
66
67 $this->setHasDirtyData();
68 }
69
70 function serializeNames()
71 {
72 return serialize( $this->NameList );
73 }
74
75 function isEmpty()
76 {
77 return ( count( $this->NameList ) == 0 );
78 }
79
80 function unserializeNames( $serializedNamesString )
81 {
82 $this->NameList = array();
83 if ( $serializedNamesString )
84 {
85 $this->NameList = @unserialize( $serializedNamesString );
86 if ( $this->NameList === false || !is_array( $this->NameList ) )
87 $this->NameList = array();
88 }
89
90 $this->setHasDirtyData( false );
91 }
92
93 function alwaysAvailableLanguageID()
94 {
95 $languageLocale = $this->alwaysAvailableLanguageLocale();
96 $languageID = $languageLocale ? eZContentLanguage::idByLocale( $languageLocale ) : false;
97
98 return $languageID;
99 }
100
101 function alwaysAvailableLanguageLocale()
102 {
103 $languageLocale = isset( $this->NameList[eZSerializedObjectNameList::ALWAYS_AVAILABLE_STR] ) ? $this->NameList[eZSerializedObjectNameList::ALWAYS_AVAILABLE_STR] : false;
104 return $languageLocale;
105 }
106
107 function alwaysAvailableLanguage()
108 {
109 $language = false;
110 if ( isset( $this->NameList[eZSerializedObjectNameList::ALWAYS_AVAILABLE_STR] ) )
111 $language = eZContentLanguage::fetchByLocale( $this->NameList[eZSerializedObjectNameList::ALWAYS_AVAILABLE_STR] );
112
113 return $language;
114 }
115
116 function languageMask()
117 {
118 $mask = 0;
119 foreach ( $this->NameList as $languageLocale => $name )
120 {
121 if ( $languageLocale == eZSerializedObjectNameList::ALWAYS_AVAILABLE_STR )
122 {
123 $mask += 1;
124 }
125 else
126 {
127 $languageID = eZContentLanguage::idByLocale( $languageLocale );
128 $mask += $languageID;
129 }
130 }
131
132 return $mask;
133 }
134
135 function name( $languageLocale = false )
136 {
137 return ( ( $languageLocale === false ) ? $this->nameByPrioritizedLanguages() : $this->nameByLanguageLocale( $languageLocale ) );
138 }
139
140 function nameByPrioritizedLanguages()
141 {
142 $name = $this->alwaysAvailableName();
143 $languageList = eZContentLanguage::prioritizedLanguages();
144 foreach ( $languageList as $language )
145 {
146 if ( $this->hasNameInLocale( $language->attribute( 'locale' ) ) )
147 {
148 $name = $this->nameByLanguageLocale( $language->attribute( 'locale' ) );
149 break;
150 }
151 }
152
153 return $name;
154 }
155
156 function nameByLanguageLocale( $languageLocale )
157 {
158 $name = '';
159 if ( $this->hasNameInLocale( $languageLocale ) )
160 $name = $this->NameList[$languageLocale];
161
162 return $name;
163 }
164
165 function alwaysAvailableName()
166 {
167 $name = '';
168 if ( isset( $this->NameList[eZSerializedObjectNameList::ALWAYS_AVAILABLE_STR] ) )
169 {
170 $name = $this->nameByLanguageLocale( $this->NameList[eZSerializedObjectNameList::ALWAYS_AVAILABLE_STR] );
171 }
172
173 return $name;
174 }
175
176 function setAlwaysAvailableLanguage( $languageLocale )
177 {
178 if ( $languageLocale )
179 {
180 $this->NameList[eZSerializedObjectNameList::ALWAYS_AVAILABLE_STR] = $languageLocale;
181 }
182 else
183 {
184 unset( $this->NameList[eZSerializedObjectNameList::ALWAYS_AVAILABLE_STR] );
185 }
186
187 $this->setHasDirtyData();
188 }
189
190 function updateAlwaysAvailable( $alwaysAvailableLocale = false )
191 {
192 if ( !$alwaysAvailableLocale )
193 $alwaysAvailableLocale = $this->alwaysAvailableLanguageLocale();
194
195 if ( !$this->hasNameInLocale( $alwaysAvailableLocale ) )
196 {
197 $languageLocaleList = array_keys( $this->nameList() );
198 $alwaysAvailableLocale = $languageLocaleList[0];
199 }
200
201 $this->setAlwaysAvailableLanguage( $alwaysAvailableLocale );
202 }
203
204 function hasNameInLocale( $languageLocale )
205 {
206 $hasName = false;
207 if ( is_array ( $this->NameList ) )
208 {
209 if ( $languageLocale && isset( $this->NameList[$languageLocale] ) )
210 {
211 $hasName = true;
212 }
213 }
214 else
215 {
216 eZDebug::writeWarning( "Trying to get name for language '$languageLocale' without initialized name list.", __METHOD__ );
217 }
218
219 return $hasName;
220 }
221
222 function setName( $name, $languageLocale = false )
223 {
224 $oldName = $this->name( $languageLocale );
225 if ( !$languageLocale )
226 $languageLocale = $this->topPriorityLanguageLocale();
227
228 $this->setNameByLanguageLocale( $name, $languageLocale );
229 return $oldName;
230 }
231
232 function setNameByLanguageLocale( $name, $languageLocale )
233 {
234 if ( is_array( $this->NameList ) )
235 {
236 if ( $languageLocale )
237 {
238 $this->NameList[$languageLocale] = $name;
239 $this->setHasDirtyData();
240 }
241 else
242 {
243 eZDebug::writeWarning( "Language locale is not specified while setting name '$name'", __METHOD__ );
244 }
245 }
246 else
247 {
248 eZDebug::writeWarning( "Trying to set name '$name' for language '$languageLocale' without initialized name list.", __METHOD__ );
249 }
250 }
251
255 function appendGroupName( $appendString )
256 {
257 foreach ( array_keys( $this->NameList ) as $languageLocale )
258 {
259 if ( $languageLocale != eZSerializedObjectNameList::ALWAYS_AVAILABLE_STR )
260 $this->NameList[$languageLocale] .= $appendString;
261 }
262 }
263
267 static function nameFromSerializedString( $serializedNames, $languageLocale = false )
268 {
269 $nameList = new eZSerializedObjectNameList( $serializedNames );
270 return $nameList->name( $languageLocale );
271 }
272
273
277 function hasDirtyData()
278 {
279 return $this->HasDirtyData;
280 }
281
286 function setHasDirtyData( $hasDirtyData = true )
287 {
288 $this->HasDirtyData = $hasDirtyData;
289 }
290
291 function nameList()
292 {
293 return $this->NameList;
294 }
295
299 function cleanNameList()
300 {
301 $nameList = $this->nameList();
302 unset( $nameList[eZSerializedObjectNameList::ALWAYS_AVAILABLE_STR] );
303 return $nameList;
304 }
305
306 function nameListCount()
307 {
308 return count( $this->nameList );
309 }
310
311 function setNameList( $nameListArray )
312 {
313 $this->NameList = $nameListArray;
314 }
315
316 function resetNameList()
317 {
318 $this->setNameList( array() );
319 }
320
321 function removeName( $languageLocale )
322 {
323 if ( isset( $this->NameList[$languageLocale] ) )
324 {
325 unset( $this->NameList[$languageLocale] );
326 $this->setHasDirtyData();
327 }
328 }
329
330 function defaultLanguageLocale()
331 {
332 $languageLocale = false;
333 $language = $this->defaultLanguage();
334
335 if ( is_object( $language ) )
336 $languageLocale = $language->attribute( 'locale' );
337
338 return $languageLocale;
339 }
340
341 function defaultLanguage()
342 {
343 if ( !is_object( $this->DefaultLanguage ) )
344 {
345 $this->DefaultLanguage = eZContentLanguage::topPriorityLanguage();
346 }
347
348 return $this->DefaultLanguage;
349 }
350
351 function setDefaultLanguage( $language )
352 {
353 $this->DefaultLanguage = $language;
354 }
355
356 function setDefaultLanguageByLocale( $languageLocale, $createIfNotExist = true )
357 {
358 $language = eZContentLanguage::fetchByLocale( $languageLocale, $createIfNotExist );
359
360 if ( is_object( $language ) )
361 {
362 $this->setDefaultLanguage( $language );
363 }
364 else
365 {
366 eZDebug::writeWarning( "Can't set '$languageLocale' as default language. '$languageLocale' language doesn't exist in system", __METHOD__ );
367 }
368
369 return $language;
370 }
371
377 function topPriorityLanguageLocale()
378 {
379 $languageLocale = false;
380
381 $language = $this->topPriorityLanguage();
382 if ( $language )
383 $languageLocale = $language->attribute( 'locale' );
384
385 return $languageLocale;
386 }
387
396 function topPriorityLanguage()
397 {
398 $language = false;
399
400 $languageLocaleList = $this->languageLocaleList();
401
402 $language = eZContentLanguage::topPriorityLanguageByLocaleList( $languageLocaleList );
403
404 if ( !$language )
405 $language = $this->alwaysAvailableLanguage();
406
407 return $language;
408 }
409
418 function prioritizedLanguages()
419 {
420 $languageLocaleList = $this->languageLocaleList();
421 $languages = eZContentLanguage::prioritizedLanguagesByLocaleList( $languageLocaleList );
422
423 $alwaysAvailableLanguage = $this->alwaysAvailableLanguage();
424 if ( $alwaysAvailableLanguage )
425 {
426 $alwaysAvailableLanguageLocale = $alwaysAvailableLanguage->attribute( 'locale' );
427 if ( !isset( $languages[$alwaysAvailableLanguageLocale] ) )
428 {
429 $languages[$alwaysAvailableLanguageLocale] = $alwaysAvailableLanguage;
430 }
431 }
432
433 return $languages;
434 }
435
436 function prioritizedLanguagesJsArray()
437 {
438 $langList = array();
439 $languages = $this->prioritizedLanguages();
440 foreach ( $languages as $key => $language )
441 {
442 $langList[] = array( 'locale' => $language->attribute( 'locale' ),
443 'name' => $language->attribute( 'name' ) );
444 }
445
446 if ( $langList )
447 return json_encode( $langList );
448
449 return '[]';
450 }
451
452 function languageLocaleList()
453 {
454 $languageLocaleList = array();
455
456 if ( is_array( $this->NameList ) && count( $this->NameList ) > 0 )
457 {
458 foreach ( array_keys( $this->NameList ) as $languageLocale )
459 {
460 if ( $languageLocale != eZSerializedObjectNameList::ALWAYS_AVAILABLE_STR )
461 $languageLocaleList[] = $languageLocale;
462 }
463 }
464
465 return $languageLocaleList;
466 }
467
473 function languages()
474 {
475 $languages = array();
476
477 $languageLocaleList = $this->languageLocaleList();
478 foreach( $languageLocaleList as $languageLocale )
479 $languages[$languageLocale] = eZContentLanguage::fetchByLocale( $languageLocale );
480
481 return $languages;
482 }
483
489 function untranslatedLanguages()
490 {
491 $availableLanguages = $this->prioritizedLanguages();
492 $availableLanguagesCodes = array_keys( $availableLanguages );
493
494 $languages = array();
495 foreach ( eZContentLanguage::prioritizedLanguages() as $language )
496 {
497 $languageCode = $language->attribute( 'locale' );
498 if ( !in_array( $languageCode, $availableLanguagesCodes ) )
499 {
500 $languages[$languageCode] = $language;
501 }
502 }
503
504 return $languages;
505 }
506
516 function normalize( $languageInfo )
517 {
518 if ( is_array( $languageInfo ) && isset( $languageInfo['map_table'] ) )
519 {
520 // do normalization on new 'nameList' to avoid dependence on the order
521 // of <lang>s in 'map_table'. Normailzation on $this->nameList directly
522 // can lead to unwanted behaviour, like
523 // 'map_table' => array( 'ger-DE' => 'skip_language',
524 // 'eng-GB' => 'ger-DE' )
525 // and
526 // 'map_table' => array( 'eng-GB' => 'get-DE',
527 // 'ger-DE' => 'skip_language' )
528 // will produce different results.
529 $nameList = clone $this;
530 $this->resetNameList();
531
532 foreach ( $languageInfo['map_table'] as $fromLanguageLocale => $toLanguageLocale )
533 {
534 $name = $nameList->nameByLanguageLocale( $fromLanguageLocale );
535
536 if ( $toLanguageLocale == 'skip_language' )
537 {
538 // do nothing;
539 }
540 else
541 {
542 $this->setNameByLanguageLocale( $name, $toLanguageLocale );
543 }
544
545 // exclude 'processed' name.
546 $nameList->removeName( $fromLanguageLocale );
547 }
548
549 // copy names which were not transformed
550 $this->mergeNameList( $nameList );
551
552 // update always-available(probably original 'always-available' was skiped)
553 $this->updateAlwaysAvailable();
554 }
555 }
556
568 function validate( $param = true )
569 {
570 $languageMap = ( is_array( $param ) && (count( $param ) > 0) ) ? $param : false;
571 $createLanguageIfNotExist = ( $param === true ) ? true : false;
572 $nameList = $this->nameList();
573 foreach ( $nameList as $nameLanguageLocale => $name )
574 {
575 if ( $nameLanguageLocale != eZSerializedObjectNameList::ALWAYS_AVAILABLE_STR )
576 {
577 $language = false;
578
579 if( $createLanguageIfNotExist )
580 {
581 $language = eZContentLanguage::fetchByLocale( $nameLanguageLocale, true );
582 }
583 else
584 {
585 if ( is_array( $languageMap ) )
586 {
587 $languageLocale = isset( $languageMap[$nameLanguageLocale] ) ? $languageMap[$nameLanguageLocale] : false;
588
589 if( $languageLocale && $languageLocale != 'skip' )
590 {
591 $language = eZContentLanguage::fetchByLocale( $languageLocale, true );
592 }
593 }
594 else
595 {
596 // just check '$nameLanguageLocale' language if '$languageMap' is not specified.
597 $language = eZContentLanguage::fetchByLocale( $nameLanguageLocale, false );
598 }
599 }
600
601 $languageLocale = is_object( $language ) ? $language->attribute( 'locale' ) : false;
602
603 if( $languageLocale != $nameLanguageLocale )
604 {
605 if( $languageLocale )
606 {
607 // map name's language.
608 $this->removeName( $nameLanguageLocale );
609 $this->setName( $name, $languageLocale );
610 }
611 else
612 {
613 $this->removeName( $nameLanguageLocale );
614 }
615 }
616 }
617 }
618 // update always-available(probably original 'always-available' was skiped)
619 $this->updateAlwaysAvailable();
620 }
621
622 public $NameList;
623 public $HasDirtyData;
624 public $DefaultLanguage;
625 }
626
627 ?>
eZContentLanguage\topPriorityLanguageByLocaleList
static topPriorityLanguageByLocaleList( $languageLocaleList)
Definition: ezcontentlanguage.php:522
eZContentLanguage\prioritizedLanguagesByLocaleList
static prioritizedLanguagesByLocaleList( $languageLocaleList)
Definition: ezcontentlanguage.php:477
eZContentLanguage\topPriorityLanguage
static topPriorityLanguage()
Definition: ezcontentlanguage.php:404
eZContentLanguage\prioritizedLanguages
static prioritizedLanguages( $languageList=false)
Definition: ezcontentlanguage.php:294
eZContentLanguage\idByLocale
static idByLocale( $locale)
Definition: ezcontentlanguage.php:637
eZContentLanguage\fetchByLocale
static fetchByLocale( $locale, $createIfNotExist=false)
Definition: ezcontentlanguage.php:264
eZDebug\writeWarning
static writeWarning( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:502
eZSerializedObjectNameList
Definition: ezserializedobjectnamelist.php:12
eZSerializedObjectNameList\nameFromSerializedString
static nameFromSerializedString( $serializedNames, $languageLocale=false)
Definition: ezserializedobjectnamelist.php:267
eZSerializedObjectNameList\updateAlwaysAvailable
updateAlwaysAvailable( $alwaysAvailableLocale=false)
Definition: ezserializedobjectnamelist.php:190
eZSerializedObjectNameList\$nameList
$nameList
Definition: ezserializedobjectnamelist.php:13
eZSerializedObjectNameList\topPriorityLanguage
topPriorityLanguage()
Definition: ezserializedobjectnamelist.php:396
eZSerializedObjectNameList\nameByLanguageLocale
nameByLanguageLocale( $languageLocale)
Definition: ezserializedobjectnamelist.php:156
eZSerializedObjectNameList\alwaysAvailableLanguageLocale
alwaysAvailableLanguageLocale()
Definition: ezserializedobjectnamelist.php:101
eZSerializedObjectNameList\$NameList
$NameList
Definition: ezserializedobjectnamelist.php:622
eZSerializedObjectNameList\nameByPrioritizedLanguages
nameByPrioritizedLanguages()
Definition: ezserializedobjectnamelist.php:140
eZSerializedObjectNameList\removeName
removeName( $languageLocale)
Definition: ezserializedobjectnamelist.php:321
eZSerializedObjectNameList\defaultLanguageLocale
defaultLanguageLocale()
Definition: ezserializedobjectnamelist.php:330
eZSerializedObjectNameList\nameList
nameList()
Definition: ezserializedobjectnamelist.php:291
eZSerializedObjectNameList\name
name( $languageLocale=false)
Definition: ezserializedobjectnamelist.php:135
eZSerializedObjectNameList\setDefaultLanguageByLocale
setDefaultLanguageByLocale( $languageLocale, $createIfNotExist=true)
Definition: ezserializedobjectnamelist.php:356
eZSerializedObjectNameList\unserializeNames
unserializeNames( $serializedNamesString)
Definition: ezserializedobjectnamelist.php:80
eZSerializedObjectNameList\create
create( $serializedNamesString=false)
Definition: ezserializedobjectnamelist.php:45
eZSerializedObjectNameList\initFromString
initFromString( $nameString, $languageLocale=false)
Definition: ezserializedobjectnamelist.php:30
eZSerializedObjectNameList\setName
setName( $name, $languageLocale=false)
Definition: ezserializedobjectnamelist.php:222
eZSerializedObjectNameList\setNameByLanguageLocale
setNameByLanguageLocale( $name, $languageLocale)
Definition: ezserializedobjectnamelist.php:232
eZSerializedObjectNameList\initFromSerializedList
initFromSerializedList( $serializedNamesString)
Definition: ezserializedobjectnamelist.php:24
eZSerializedObjectNameList\alwaysAvailableLanguage
alwaysAvailableLanguage()
Definition: ezserializedobjectnamelist.php:107
eZSerializedObjectNameList\$HasDirtyData
$HasDirtyData
Definition: ezserializedobjectnamelist.php:623
eZSerializedObjectNameList\ALWAYS_AVAILABLE_STR
const ALWAYS_AVAILABLE_STR
Definition: ezserializedobjectnamelist.php:14
eZSerializedObjectNameList\languageMask
languageMask()
Definition: ezserializedobjectnamelist.php:116
eZSerializedObjectNameList\normalize
normalize( $languageInfo)
Definition: ezserializedobjectnamelist.php:516
eZSerializedObjectNameList\setDefaultLanguage
setDefaultLanguage( $language)
Definition: ezserializedobjectnamelist.php:351
eZSerializedObjectNameList\mergeNameList
mergeNameList( $inNameList)
Definition: ezserializedobjectnamelist.php:62
eZSerializedObjectNameList\alwaysAvailableName
alwaysAvailableName()
Definition: ezserializedobjectnamelist.php:165
eZSerializedObjectNameList\resetNameList
resetNameList()
Definition: ezserializedobjectnamelist.php:316
eZSerializedObjectNameList\hasNameInLocale
hasNameInLocale( $languageLocale)
Definition: ezserializedobjectnamelist.php:204
eZSerializedObjectNameList\__clone
__clone()
Definition: ezserializedobjectnamelist.php:51
eZSerializedObjectNameList\hasDirtyData
hasDirtyData()
Definition: ezserializedobjectnamelist.php:277
eZSerializedObjectNameList\prioritizedLanguagesJsArray
prioritizedLanguagesJsArray()
Definition: ezserializedobjectnamelist.php:436
eZSerializedObjectNameList\isEmpty
isEmpty()
Definition: ezserializedobjectnamelist.php:75
eZSerializedObjectNameList\languageLocaleList
languageLocaleList()
Definition: ezserializedobjectnamelist.php:452
eZSerializedObjectNameList\$DefaultLanguage
$DefaultLanguage
Definition: ezserializedobjectnamelist.php:624
eZSerializedObjectNameList\cleanNameList
cleanNameList()
Definition: ezserializedobjectnamelist.php:299
eZSerializedObjectNameList\prioritizedLanguages
prioritizedLanguages()
Definition: ezserializedobjectnamelist.php:418
eZSerializedObjectNameList\serializeNames
serializeNames()
Definition: ezserializedobjectnamelist.php:70
eZSerializedObjectNameList\alwaysAvailableLanguageID
alwaysAvailableLanguageID()
Definition: ezserializedobjectnamelist.php:93
eZSerializedObjectNameList\appendGroupName
appendGroupName( $appendString)
Definition: ezserializedobjectnamelist.php:255
eZSerializedObjectNameList\topPriorityLanguageLocale
topPriorityLanguageLocale()
Definition: ezserializedobjectnamelist.php:377
eZSerializedObjectNameList\defaultLanguage
defaultLanguage()
Definition: ezserializedobjectnamelist.php:341
eZSerializedObjectNameList\__construct
__construct( $serializedNamesString=false)
Definition: ezserializedobjectnamelist.php:16
eZSerializedObjectNameList\setAlwaysAvailableLanguage
setAlwaysAvailableLanguage( $languageLocale)
Definition: ezserializedobjectnamelist.php:176
eZSerializedObjectNameList\setNameList
setNameList( $nameListArray)
Definition: ezserializedobjectnamelist.php:311
eZSerializedObjectNameList\initDefault
initDefault()
Definition: ezserializedobjectnamelist.php:40
eZSerializedObjectNameList\validate
validate( $param=true)
Definition: ezserializedobjectnamelist.php:568
eZSerializedObjectNameList\copy
copy( $serializedObjectNameListObject)
Definition: ezserializedobjectnamelist.php:55
eZSerializedObjectNameList\setHasDirtyData
setHasDirtyData( $hasDirtyData=true)
Definition: ezserializedobjectnamelist.php:286
eZSerializedObjectNameList\nameListCount
nameListCount()
Definition: ezserializedobjectnamelist.php:306
eZSerializedObjectNameList\untranslatedLanguages
untranslatedLanguages()
Definition: ezserializedobjectnamelist.php:489
eZSerializedObjectNameList\languages
languages()
Definition: ezserializedobjectnamelist.php:473
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.