00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 include_once( 'kernel/classes/ezdatatype.php' );
00041 include_once( 'lib/ezutils/classes/ezintegervalidator.php' );
00042 include_once( 'kernel/common/i18n.php' );
00043
00044 define( 'EZ_DATATYPESTRING_COUNTRY', 'ezcountry' );
00045 define( 'EZ_DATATYPESTRING_COUNTRY_DEFAULT_LIST_FIELD', 'data_text5' );
00046 define( 'EZ_DATATYPESTRING_COUNTRY_MULTIPLE_CHOICE_FIELD', 'data_int1' );
00047
00048 class eZCountryType extends eZDataType
00049 {
00050 function eZCountryType()
00051 {
00052 $this->eZDataType( EZ_DATATYPESTRING_COUNTRY, ezi18n( 'kernel/classes/datatypes', 'Country', 'Datatype name' ),
00053 array( 'serialize_supported' => true,
00054 'object_serialize_map' => array( 'data_text' => 'country' ) ) );
00055 }
00056
00057
00058
00059
00060 function fetchCountryList()
00061 {
00062 if ( isset( $GLOBALS['CountryList'] ) )
00063 return $GLOBALS['CountryList'];
00064
00065 $ini =& eZINI::instance( 'country.ini' );
00066 $countries = $ini->getNamedArray();
00067 eZCountryType::fetchTranslatedNames( $countries );
00068 $GLOBALS['CountryList'] = $countries;
00069 return $countries;
00070 }
00071
00072
00073
00074
00075
00076 function fetchTranslatedNames( &$countries )
00077 {
00078 include_once( "lib/ezlocale/classes/ezlocale.php" );
00079 $locale =& eZLocale::instance();
00080 $translatedCountryNames = $locale->translatedCountryNames();
00081 foreach ( array_keys( $countries ) as $countryKey )
00082 {
00083 $translatedName = isset( $translatedCountryNames[$countryKey] ) ? $translatedCountryNames[$countryKey] : false;
00084 if ( $translatedName )
00085 $countries[$countryKey]['Name'] = $translatedName;
00086 }
00087 }
00088
00089
00090
00091
00092
00093 function fetchCountry( $value, $fetchBy = false )
00094 {
00095 $fetchBy = !$fetchBy ? 'Name' : $fetchBy;
00096
00097 $allCountries = eZCountryType::fetchCountryList();
00098 $result = false;
00099 if ( $fetchBy == 'Alpha2' and isset( $allCountries[strtoupper( $value )] ) )
00100 {
00101 $result = $allCountries[$value];
00102 return $result;
00103 }
00104
00105 foreach ( $allCountries as $country )
00106 {
00107 if ( isset( $country[$fetchBy] ) and $country[$fetchBy] == $value )
00108 {
00109 $result = $country;
00110 break;
00111 }
00112 }
00113
00114 return $result;
00115 }
00116
00117
00118
00119
00120 function fetchClassAttributeHTTPInput( &$http, $base, &$classAttribute )
00121 {
00122 $classAttributeID = $classAttribute->attribute( 'id' );
00123 $content = $classAttribute->content();
00124
00125 if ( $http->hasPostVariable( $base . '_ezcountry_multiple_choice_value_' . $classAttribute->attribute( 'id' ) . '_exists' ) )
00126 {
00127 $content['multiple_choice'] = $http->hasPostVariable( $base . "_ezcountry_ismultiple_value_" . $classAttributeID ) ? 1 : 0;
00128 }
00129
00130 if ( $http->hasPostVariable( $base . '_ezcountry_default_selection_value_' . $classAttribute->attribute( 'id' ) . '_exists' ) )
00131 {
00132 if ( $http->hasPostVariable( $base . "_ezcountry_default_country_list_". $classAttributeID ) )
00133 {
00134 $defaultValues = $http->postVariable( $base . "_ezcountry_default_country_list_". $classAttributeID );
00135 $defaultList = array();
00136 foreach ( $defaultValues as $alpha2 )
00137 {
00138 if ( trim( $alpha2 ) == '' )
00139 continue;
00140
00141 $eZCountry = eZCountryType::fetchCountry( $alpha2, 'Alpha2' );
00142 if ( $eZCountry )
00143 $defaultList[$alpha2] = $eZCountry;
00144 }
00145 $content['default_countries'] = $defaultList;
00146 }
00147 else
00148 {
00149 $content['default_countries'] = array();
00150 }
00151 }
00152 $classAttribute->setContent( $content );
00153 $classAttribute->store();
00154 return true;
00155 }
00156
00157
00158
00159
00160 function preStoreClassAttribute( &$classAttribute, $version )
00161 {
00162 $content = $classAttribute->content();
00163 return eZCountryType::storeClassAttributeContent( $classAttribute, $content );
00164 }
00165
00166 function storeClassAttributeContent( &$classAttribute, $content )
00167 {
00168 if ( is_array( $content ) )
00169 {
00170 $multipleChoice = $content['multiple_choice'];
00171 $defaultCountryList = $content['default_countries'];
00172 $defaultCountry = implode( ',', array_keys( $defaultCountryList ) );
00173
00174 $classAttribute->setAttribute( EZ_DATATYPESTRING_COUNTRY_DEFAULT_LIST_FIELD, $defaultCountry );
00175 $classAttribute->setAttribute( EZ_DATATYPESTRING_COUNTRY_MULTIPLE_CHOICE_FIELD, $multipleChoice );
00176 }
00177 return false;
00178 }
00179
00180
00181
00182
00183 function initializeObjectAttribute( &$contentObjectAttribute, $currentVersion, &$originalContentObjectAttribute )
00184 {
00185 if ( $currentVersion != false )
00186 {
00187 $dataText = $originalContentObjectAttribute->content();
00188 $contentObjectAttribute->setContent( $dataText );
00189 }
00190 else
00191 {
00192 $default = array( 'value' => array() );
00193 $contentObjectAttribute->setContent( $default );
00194 }
00195 }
00196
00197
00198
00199
00200 function validateObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
00201 {
00202 if ( !$contentObjectAttribute->validateIsRequired() )
00203 return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
00204
00205 if ( $http->hasPostVariable( $base . '_country_' . $contentObjectAttribute->attribute( 'id' ) ) )
00206 {
00207 $data = $http->postVariable( $base . '_country_' . $contentObjectAttribute->attribute( 'id' ) );
00208
00209 if ( count( $data ) > 0 and $data[0] != '' )
00210 return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
00211 }
00212
00213 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00214 'Input required.' ) );
00215 return EZ_INPUT_VALIDATOR_STATE_INVALID;
00216 }
00217
00218
00219
00220
00221 function validateCollectionAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
00222 {
00223 if ( !$contentObjectAttribute->validateIsRequired() )
00224 return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
00225
00226 if ( $http->hasPostVariable( $base . '_country_' . $contentObjectAttribute->attribute( 'id' ) ) )
00227 {
00228 $data = $http->postVariable( $base . '_country_' . $contentObjectAttribute->attribute( 'id' ) );
00229
00230 if ( count( $data ) > 0 and $data[0] != '' )
00231 return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
00232 }
00233
00234 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
00235 'Input required.' ) );
00236 return EZ_INPUT_VALIDATOR_STATE_INVALID;
00237 }
00238
00239
00240
00241
00242 function fetchObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
00243 {
00244 if ( $http->hasPostVariable( $base . '_country_' . $contentObjectAttribute->attribute( 'id' ) ) )
00245 {
00246 $data = $http->postVariable( $base . '_country_' . $contentObjectAttribute->attribute( 'id' ) );
00247 $defaultList = array();
00248 if ( is_array( $data ) )
00249 {
00250 foreach ( $data as $alpha2 )
00251 {
00252 if ( trim( $alpha2 ) == '' )
00253 continue;
00254
00255 $eZCountry = eZCountryType::fetchCountry( $alpha2, 'Alpha2' );
00256 if ( $eZCountry )
00257 $defaultList[$alpha2] = $eZCountry;
00258 }
00259 }
00260 else
00261 {
00262 $countries = eZCountryType::fetchCountryList();
00263 foreach ( $countries as $country )
00264 {
00265 if ( $country['Name'] == $data )
00266 {
00267 $defaultList[$country['Alpha2']] = $country['Name'];
00268 }
00269 }
00270 }
00271 $content = array( 'value' => $defaultList );
00272
00273 $contentObjectAttribute->setContent( $content );
00274 }
00275 else
00276 {
00277 $content = array( 'value' => array() );
00278 $contentObjectAttribute->setContent( $content );
00279 }
00280 return true;
00281 }
00282
00283
00284
00285
00286 function fetchCollectionAttributeHTTPInput( &$collection, &$collectionAttribute, &$http, $base, &$contentObjectAttribute )
00287 {
00288 if ( $http->hasPostVariable( $base . "_country_" . $contentObjectAttribute->attribute( "id" ) ) )
00289 {
00290 $dataText = $http->postVariable( $base . "_country_" . $contentObjectAttribute->attribute( "id" ) );
00291
00292 $value = implode( ',', $dataText );
00293 $collectionAttribute->setAttribute( 'data_text', $value );
00294 return true;
00295 }
00296 return false;
00297 }
00298
00299
00300
00301
00302 function storeObjectAttribute( &$contentObjectAttribute )
00303 {
00304 $content = $contentObjectAttribute->content();
00305
00306 $valueArray = $content['value'];
00307 $value = is_array( $valueArray ) ? implode( ',', array_keys( $valueArray ) ) : $valueArray;
00308
00309 $contentObjectAttribute->setAttribute( "data_text", $value );
00310 }
00311
00312
00313
00314
00315
00316 function isSimpleStringInsertionSupported()
00317 {
00318 return true;
00319 }
00320
00321
00322
00323
00324 function insertSimpleString( &$object, $objectVersion, $objectLanguage,
00325 &$objectAttribute, $string,
00326 &$result )
00327 {
00328 $result = array( 'errors' => array(),
00329 'require_storage' => true );
00330 $content = array( 'value' => $string );
00331 $objectAttribute->setContent( $content );
00332 return true;
00333 }
00334
00335
00336
00337
00338 function &objectAttributeContent( &$contentObjectAttribute )
00339 {
00340 $value = $contentObjectAttribute->attribute( 'data_text' );
00341
00342 $countryList = explode( ',', $value );
00343 $resultList = array();
00344 foreach ( $countryList as $alpha2 )
00345 {
00346 $eZCountry = eZCountryType::fetchCountry( $alpha2, 'Alpha2' );
00347 $resultList[$alpha2] = $eZCountry ? $eZCountry : '';
00348 }
00349
00350
00351 if ( count( $resultList ) == 1 and $resultList[$value] == '' )
00352 $resultList = $value;
00353
00354 $content = array( 'value' => $resultList );
00355 return $content;
00356 }
00357
00358
00359
00360
00361 function &classAttributeContent( &$classAttribute )
00362 {
00363 $defaultCountry = $classAttribute->attribute( EZ_DATATYPESTRING_COUNTRY_DEFAULT_LIST_FIELD );
00364 $multipleChoice = $classAttribute->attribute( EZ_DATATYPESTRING_COUNTRY_MULTIPLE_CHOICE_FIELD );
00365 $defaultCountryList = explode( ',', $defaultCountry );
00366 $resultList = array();
00367 foreach ( $defaultCountryList as $alpha2 )
00368 {
00369 $eZCountry = eZCountryType::fetchCountry( $alpha2, 'Alpha2' );
00370 if ( $eZCountry )
00371 $resultList[$alpha2] = $eZCountry;
00372 }
00373 $content = array( 'default_countries' => $resultList,
00374 'multiple_choice' => $multipleChoice );
00375
00376 return $content;
00377 }
00378
00379
00380
00381
00382 function metaData( &$contentObjectAttribute )
00383 {
00384 $content = $contentObjectAttribute->content();
00385 if ( is_array( $content['value'] ) )
00386 {
00387 $imploded = '';
00388 foreach ( $content['value'] as $country )
00389 {
00390 $countryName = $country['Name'];
00391 if ( $imploded == '' )
00392 $imploded = $countryName;
00393 else
00394 $imploded .= ',' . $countryName;
00395 }
00396 $content['value'] = $imploded;
00397 }
00398 return $content['value'];
00399 }
00400
00401
00402
00403
00404 function toString( $contentObjectAttribute )
00405 {
00406 return $contentObjectAttribute->attribute( 'data_text' );
00407 }
00408
00409 function fromString( &$contentObjectAttribute, $string )
00410 {
00411 return $contentObjectAttribute->setAttribute( 'data_text', $string );
00412 }
00413
00414
00415
00416
00417 function title( &$contentObjectAttribute )
00418 {
00419 $content = $contentObjectAttribute->content();
00420 if ( is_array( $content['value'] ) )
00421 {
00422 $imploded = '';
00423 foreach ( $content['value'] as $country )
00424 {
00425 $countryName = $country['Name'];
00426 if ( $imploded == '' )
00427 $imploded = $countryName;
00428 else
00429 $imploded .= ',' . $countryName;
00430 }
00431 $content['value'] = $imploded;
00432 }
00433 return $content['value'];
00434 }
00435
00436 function hasObjectAttributeContent( &$contentObjectAttribute )
00437 {
00438 $content = $contentObjectAttribute->content();
00439 $result = ( ( !is_array( $content['value'] ) and trim( $content['value'] ) != '' ) or ( is_array( $content['value'] ) and count( $content['value'] ) > 0 ) );
00440 return $result;
00441 }
00442
00443
00444
00445
00446 function isIndexable()
00447 {
00448 return true;
00449 }
00450
00451
00452
00453
00454 function isInformationCollector()
00455 {
00456 return true;
00457 }
00458
00459
00460
00461
00462 function sortKey( &$contentObjectAttribute )
00463 {
00464 include_once( 'lib/ezi18n/classes/ezchartransform.php' );
00465 $trans =& eZCharTransform::instance();
00466 $content = $contentObjectAttribute->content();
00467 if ( is_array( $content['value'] ) )
00468 {
00469 $imploded = '';
00470 foreach ( $content['value'] as $country )
00471 {
00472 $countryName = $country['Name'];
00473
00474 if ( $imploded == '' )
00475 $imploded = $countryName;
00476 else
00477 $imploded .= ',' . $countryName;
00478 }
00479 $content['value'] = $imploded;
00480 }
00481 return $trans->transformByGroup( $content['value'], 'lowercase' );
00482 }
00483
00484
00485
00486
00487 function sortKeyType()
00488 {
00489 return 'string';
00490 }
00491
00492
00493
00494
00495 function diff( $old, $new, $options = false )
00496 {
00497 return null;
00498 }
00499 }
00500
00501 eZDataType::register( EZ_DATATYPESTRING_COUNTRY, 'ezcountrytype' );
00502
00503 ?>