Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezcountrytype.php
Go to the documentation of this file.
1 <?php
22 class eZCountryType extends eZDataType
23 {
24 const DATA_TYPE_STRING = 'ezcountry';
25
26 const DEFAULT_LIST_FIELD = 'data_text5';
27
28 const MULTIPLE_CHOICE_FIELD = 'data_int1';
29
30 public function __construct()
31 {
32 parent::__construct( self::DATA_TYPE_STRING, ezpI18n::tr( 'kernel/classes/datatypes', 'Country', 'Datatype name' ),
33 array( 'serialize_supported' => true,
34 'object_serialize_map' => array( 'data_text' => 'country' ) ) );
35 }
36
40 static function fetchCountryList()
41 {
42 if ( isset( $GLOBALS['CountryList'] ) )
43 return $GLOBALS['CountryList'];
44
45 $ini = eZINI::instance( 'country.ini' );
46 $countries = $ini->getNamedArray();
47 eZCountryType::fetchTranslatedNames( $countries );
48 $GLOBALS['CountryList'] = $countries;
49 return $countries;
50 }
51
56 static function fetchTranslatedNames( &$countries )
57 {
58 $locale = eZLocale::instance();
59 $translatedCountryNames = $locale->translatedCountryNames();
60 foreach ( array_keys( $countries ) as $countryKey )
61 {
62 $translatedName = isset( $translatedCountryNames[$countryKey] ) ? $translatedCountryNames[$countryKey] : false;
63 if ( $translatedName )
64 $countries[$countryKey]['Name'] = $translatedName;
65 }
66 usort( $countries, array( 'eZCountryType', 'compareCountryNames' ) );
67 }
68
76 protected static function compareCountryNames( $a, $b )
77 {
78 return strcoll( $a["Name"], $b["Name"] );
79 }
80
85 static function fetchCountry( $value, $fetchBy = false )
86 {
87 $fetchBy = !$fetchBy ? 'Name' : $fetchBy;
88
89 $allCountries = eZCountryType::fetchCountryList();
90 $result = false;
91 if ( $fetchBy == 'Alpha2' and isset( $allCountries[strtoupper( $value )] ) )
92 {
93 $result = $allCountries[$value];
94 return $result;
95 }
96
97 foreach ( $allCountries as $country )
98 {
99 if ( isset( $country[$fetchBy] ) and $country[$fetchBy] == $value )
100 {
101 $result = $country;
102 break;
103 }
104 }
105
106 return $result;
107 }
108
109 function fetchClassAttributeHTTPInput( $http, $base, $classAttribute )
110 {
111 $classAttributeID = $classAttribute->attribute( 'id' );
112 $content = $classAttribute->content();
113
114 if ( $http->hasPostVariable( $base . '_ezcountry_multiple_choice_value_' . $classAttribute->attribute( 'id' ) . '_exists' ) )
115 {
116 $content['multiple_choice'] = $http->hasPostVariable( $base . "_ezcountry_ismultiple_value_" . $classAttributeID ) ? 1 : 0;
117 }
118
119 if ( $http->hasPostVariable( $base . '_ezcountry_default_selection_value_' . $classAttribute->attribute( 'id' ) . '_exists' ) )
120 {
121 if ( $http->hasPostVariable( $base . "_ezcountry_default_country_list_". $classAttributeID ) )
122 {
123 $defaultValues = $http->postVariable( $base . "_ezcountry_default_country_list_". $classAttributeID );
124 $defaultList = array();
125 foreach ( $defaultValues as $alpha2 )
126 {
127 if ( trim( $alpha2 ) == '' )
128 continue;
129 // Fetch ezcountry by aplha2 code (as reserved in iso-3166 code list)
130 $eZCountry = eZCountryType::fetchCountry( $alpha2, 'Alpha2' );
131 if ( $eZCountry )
132 $defaultList[$alpha2] = $eZCountry;
133 }
134 $content['default_countries'] = $defaultList;
135 }
136 else
137 {
138 $content['default_countries'] = array();
139 }
140 }
141 $classAttribute->setContent( $content );
142 $classAttribute->store();
143 return true;
144 }
145
146 function preStoreClassAttribute( $classAttribute, $version )
147 {
148 $content = $classAttribute->content();
149 return eZCountryType::storeClassAttributeContent( $classAttribute, $content );
150 }
151
152 function storeClassAttributeContent( $classAttribute, $content )
153 {
154 if ( is_array( $content ) )
155 {
156 $multipleChoice = $content['multiple_choice'];
157 $defaultCountryList = $content['default_countries'];
158 $defaultCountry = implode( ',', array_keys( $defaultCountryList ) );
159
160 $classAttribute->setAttribute( self::DEFAULT_LIST_FIELD, $defaultCountry );
161 $classAttribute->setAttribute( self::MULTIPLE_CHOICE_FIELD, $multipleChoice );
162 }
163 return false;
164 }
165
169 function initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute )
170 {
171 if ( $currentVersion != false )
172 {
173 $dataText = $originalContentObjectAttribute->content();
174 $contentObjectAttribute->setContent( $dataText );
175 }
176 else
177 {
178 $default = array( 'value' => array() );
179 $contentObjectAttribute->setContent( $default );
180 }
181 }
182
183 function validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
184 {
185 if ( !$contentObjectAttribute->validateIsRequired() )
186 return eZInputValidator::STATE_ACCEPTED;
187
188 if ( $http->hasPostVariable( $base . '_country_' . $contentObjectAttribute->attribute( 'id' ) ) )
189 {
190 $data = $http->postVariable( $base . '_country_' . $contentObjectAttribute->attribute( 'id' ) );
191
192 if ( count( $data ) > 0 and $data[0] != '' )
193 return eZInputValidator::STATE_ACCEPTED;
194 }
195
196 $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
197 'Input required.' ) );
198 return eZInputValidator::STATE_INVALID;
199 }
200
201 function validateCollectionAttributeHTTPInput( $http, $base, $contentObjectAttribute )
202 {
203 if ( !$contentObjectAttribute->validateIsRequired() )
204 return eZInputValidator::STATE_ACCEPTED;
205
206 if ( $http->hasPostVariable( $base . '_country_' . $contentObjectAttribute->attribute( 'id' ) ) )
207 {
208 $data = $http->postVariable( $base . '_country_' . $contentObjectAttribute->attribute( 'id' ) );
209
210 if ( count( $data ) > 0 and $data[0] != '' )
211 return eZInputValidator::STATE_ACCEPTED;
212 }
213
214 $contentObjectAttribute->setValidationError( ezpI18n::tr( 'kernel/classes/datatypes',
215 'Input required.' ) );
216 return eZInputValidator::STATE_INVALID;
217 }
218
222 function fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute )
223 {
224 if ( $http->hasPostVariable( $base . '_country_' . $contentObjectAttribute->attribute( 'id' ) ) )
225 {
226 $data = $http->postVariable( $base . '_country_' . $contentObjectAttribute->attribute( 'id' ) );
227 $defaultList = array();
228 if ( is_array( $data ) )
229 {
230 foreach ( $data as $alpha2 )
231 {
232 if ( trim( $alpha2 ) == '' )
233 continue;
234
235 $eZCountry = eZCountryType::fetchCountry( $alpha2, 'Alpha2' );
236 if ( $eZCountry )
237 $defaultList[$alpha2] = $eZCountry;
238 }
239 }
240 else
241 {
242 $countries = eZCountryType::fetchCountryList();
243 foreach ( $countries as $country )
244 {
245 if ( $country['Name'] == $data )
246 {
247 $defaultList[$country['Alpha2']] = $country['Name'];
248 }
249 }
250 }
251 $content = array( 'value' => $defaultList );
252
253 $contentObjectAttribute->setContent( $content );
254 }
255 else
256 {
257 $content = array( 'value' => array() );
258 $contentObjectAttribute->setContent( $content );
259 }
260 return true;
261 }
262
266 function fetchCollectionAttributeHTTPInput( $collection, $collectionAttribute, $http, $base, $contentObjectAttribute )
267 {
268 if ( $http->hasPostVariable( $base . "_country_" . $contentObjectAttribute->attribute( "id" ) ) )
269 {
270 $dataText = $http->postVariable( $base . "_country_" . $contentObjectAttribute->attribute( "id" ) );
271
272 $value = implode( ',', $dataText );
273 $collectionAttribute->setAttribute( 'data_text', $value );
274 return true;
275 }
276 return false;
277 }
278
279 function storeObjectAttribute( $contentObjectAttribute )
280 {
281 $content = $contentObjectAttribute->content();
282
283 $valueArray = $content['value'];
284 $value = is_array( $valueArray ) ? implode( ',', array_keys( $valueArray ) ) : $valueArray;
285
286 $contentObjectAttribute->setAttribute( "data_text", $value );
287 }
288
292 function isSimpleStringInsertionSupported()
293 {
294 return true;
295 }
296
297 function insertSimpleString( $object, $objectVersion, $objectLanguage,
298 $objectAttribute, $string,
299 &$result )
300 {
301 $result = array( 'errors' => array(),
302 'require_storage' => true );
303 $content = array( 'value' => $string );
304 $objectAttribute->setContent( $content );
305 return true;
306 }
307
311 function objectAttributeContent( $contentObjectAttribute )
312 {
313 $value = $contentObjectAttribute->attribute( 'data_text' );
314
315 $countryList = explode( ',', $value );
316 $resultList = array();
317 foreach ( $countryList as $alpha2 )
318 {
319 $eZCountry = eZCountryType::fetchCountry( $alpha2, 'Alpha2' );
320 $resultList[$alpha2] = $eZCountry ? $eZCountry : '';
321 }
322 // Supporting of previous version format.
323 // For backwards compatibility.
324 if ( count( $resultList ) == 1 and $resultList[$value] == '' )
325 $resultList = $value;
326
327 $content = array( 'value' => $resultList );
328 return $content;
329 }
330
331 function classAttributeContent( $classAttribute )
332 {
333 $defaultCountry = $classAttribute->attribute( self::DEFAULT_LIST_FIELD );
334 $multipleChoice = $classAttribute->attribute( self::MULTIPLE_CHOICE_FIELD );
335 $defaultCountryList = explode( ',', $defaultCountry );
336 $resultList = array();
337 foreach ( $defaultCountryList as $alpha2 )
338 {
339 $eZCountry = eZCountryType::fetchCountry( $alpha2, 'Alpha2' );
340 if ( $eZCountry )
341 $resultList[$alpha2] = $eZCountry;
342 }
343 $content = array( 'default_countries' => $resultList,
344 'multiple_choice' => $multipleChoice );
345
346 return $content;
347 }
348
352 function metaData( $contentObjectAttribute )
353 {
354 $content = $contentObjectAttribute->content();
355 if ( is_array( $content['value'] ) )
356 {
357 $imploded = '';
358 foreach ( $content['value'] as $country )
359 {
360 $countryName = $country['Name'];
361 if ( $imploded == '' )
362 $imploded = $countryName;
363 else
364 $imploded .= ',' . $countryName;
365 }
366 $content['value'] = $imploded;
367 }
368 return $content['value'];
369 }
370
374 function toString( $contentObjectAttribute )
375 {
376 return $contentObjectAttribute->attribute( 'data_text' );
377 }
378
379 function fromString( $contentObjectAttribute, $string )
380 {
381 return $contentObjectAttribute->setAttribute( 'data_text', $string );
382 }
383
387 function title( $contentObjectAttribute, $name = null )
388 {
389 $content = $contentObjectAttribute->content();
390 if ( is_array( $content['value'] ) )
391 {
392 $imploded = '';
393 foreach ( $content['value'] as $country )
394 {
395 $countryName = $country['Name'];
396 if ( $imploded == '' )
397 $imploded = $countryName;
398 else
399 $imploded .= ',' . $countryName;
400 }
401 $content['value'] = $imploded;
402 }
403 return $content['value'];
404 }
405
406 function hasObjectAttributeContent( $contentObjectAttribute )
407 {
408 $content = $contentObjectAttribute->content();
409 $result = ( ( !is_array( $content['value'] ) and trim( $content['value'] ) != '' ) or ( is_array( $content['value'] ) and count( $content['value'] ) > 0 ) );
410 return $result;
411 }
412
413 function isIndexable()
414 {
415 return true;
416 }
417
418 function isInformationCollector()
419 {
420 return true;
421 }
422
423 function sortKey( $contentObjectAttribute )
424 {
425 $trans = eZCharTransform::instance();
426 $content = $contentObjectAttribute->content();
427 if ( is_array( $content['value'] ) )
428 {
429 $imploded = '';
430 foreach ( $content['value'] as $country )
431 {
432 $countryName = $country['Name'];
433
434 if ( $imploded == '' )
435 $imploded = $countryName;
436 else
437 $imploded .= ',' . $countryName;
438 }
439 $content['value'] = $imploded;
440 }
441 return $trans->transformByGroup( $content['value'], 'lowercase' );
442 }
443
444 function sortKeyType()
445 {
446 return 'string';
447 }
448
449 function diff( $old, $new, $options = false )
450 {
451 return null;
452 }
453
454 function supportsBatchInitializeObjectAttribute()
455 {
456 return true;
457 }
458 }
459
460 eZDataType::register( eZCountryType::DATA_TYPE_STRING, 'ezcountrytype' );
461
462 ?>
eZCharTransform\instance
static instance()
Definition: ezchartransform.php:434
eZCountryType
A content datatype that contains country.
Definition: ezcountrytype.php:23
eZCountryType\metaData
metaData( $contentObjectAttribute)
Definition: ezcountrytype.php:352
eZCountryType\classAttributeContent
classAttributeContent( $classAttribute)
Definition: ezcountrytype.php:331
eZCountryType\fetchCollectionAttributeHTTPInput
fetchCollectionAttributeHTTPInput( $collection, $collectionAttribute, $http, $base, $contentObjectAttribute)
Definition: ezcountrytype.php:266
eZCountryType\sortKey
sortKey( $contentObjectAttribute)
Definition: ezcountrytype.php:423
eZCountryType\toString
toString( $contentObjectAttribute)
Definition: ezcountrytype.php:374
eZCountryType\fetchCountry
static fetchCountry( $value, $fetchBy=false)
Definition: ezcountrytype.php:85
eZCountryType\preStoreClassAttribute
preStoreClassAttribute( $classAttribute, $version)
Definition: ezcountrytype.php:146
eZCountryType\sortKeyType
sortKeyType()
Definition: ezcountrytype.php:444
eZCountryType\MULTIPLE_CHOICE_FIELD
const MULTIPLE_CHOICE_FIELD
Definition: ezcountrytype.php:28
eZCountryType\fetchObjectAttributeHTTPInput
fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute)
Definition: ezcountrytype.php:222
eZCountryType\objectAttributeContent
objectAttributeContent( $contentObjectAttribute)
Definition: ezcountrytype.php:311
eZCountryType\insertSimpleString
insertSimpleString( $object, $objectVersion, $objectLanguage, $objectAttribute, $string, &$result)
Definition: ezcountrytype.php:297
eZCountryType\storeObjectAttribute
storeObjectAttribute( $contentObjectAttribute)
Definition: ezcountrytype.php:279
eZCountryType\validateCollectionAttributeHTTPInput
validateCollectionAttributeHTTPInput( $http, $base, $contentObjectAttribute)
Definition: ezcountrytype.php:201
eZCountryType\DATA_TYPE_STRING
const DATA_TYPE_STRING
Definition: ezcountrytype.php:24
eZCountryType\fetchTranslatedNames
static fetchTranslatedNames(&$countries)
Definition: ezcountrytype.php:56
eZCountryType\fetchCountryList
static fetchCountryList()
Definition: ezcountrytype.php:40
eZCountryType\title
title( $contentObjectAttribute, $name=null)
Definition: ezcountrytype.php:387
eZCountryType\validateObjectAttributeHTTPInput
validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute)
Definition: ezcountrytype.php:183
eZCountryType\diff
diff( $old, $new, $options=false)
Definition: ezcountrytype.php:449
eZCountryType\isSimpleStringInsertionSupported
isSimpleStringInsertionSupported()
Definition: ezcountrytype.php:292
eZCountryType\__construct
__construct()
Definition: ezcountrytype.php:30
eZCountryType\fetchClassAttributeHTTPInput
fetchClassAttributeHTTPInput( $http, $base, $classAttribute)
Definition: ezcountrytype.php:109
eZCountryType\compareCountryNames
static compareCountryNames( $a, $b)
Definition: ezcountrytype.php:76
eZCountryType\DEFAULT_LIST_FIELD
const DEFAULT_LIST_FIELD
Definition: ezcountrytype.php:26
eZCountryType\storeClassAttributeContent
storeClassAttributeContent( $classAttribute, $content)
Definition: ezcountrytype.php:152
eZCountryType\supportsBatchInitializeObjectAttribute
supportsBatchInitializeObjectAttribute()
Definition: ezcountrytype.php:454
eZCountryType\hasObjectAttributeContent
hasObjectAttributeContent( $contentObjectAttribute)
Definition: ezcountrytype.php:406
eZCountryType\initializeObjectAttribute
initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute)
Definition: ezcountrytype.php:169
eZCountryType\isIndexable
isIndexable()
Definition: ezcountrytype.php:413
eZCountryType\isInformationCollector
isInformationCollector()
Definition: ezcountrytype.php:418
eZCountryType\fromString
fromString( $contentObjectAttribute, $string)
Definition: ezcountrytype.php:379
eZDataType
Base class for content datatypes.
Definition: ezdatatype.php:46
eZDataType\register
static register( $dataTypeString, $className)
Definition: ezdatatype.php:203
eZInputValidator\STATE_ACCEPTED
const STATE_ACCEPTED
Definition: ezinputvalidator.php:79
eZInputValidator\STATE_INVALID
const STATE_INVALID
Definition: ezinputvalidator.php:81
eZLocale\instance
static instance( $localeString=false)
Definition: ezlocale.php:1536
ezpI18n\tr
static tr( $context, $source, $comment=null, $arguments=null)
Definition: ezpi18n.php:86
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.