|
eZ Publish
[4.0]
|
00001 <?php 00002 // 00003 // Definition of eZCountryType class 00004 // 00005 // Created on: <20-Feb-2006 11:11:19 vs> 00006 // 00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ## 00008 // SOFTWARE NAME: eZ Publish 00009 // SOFTWARE RELEASE: 4.0.x 00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS 00011 // SOFTWARE LICENSE: GNU General Public License v2.0 00012 // NOTICE: > 00013 // This program is free software; you can redistribute it and/or 00014 // modify it under the terms of version 2.0 of the GNU General 00015 // Public License as published by the Free Software Foundation. 00016 // 00017 // This program is distributed in the hope that it will be useful, 00018 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00020 // GNU General Public License for more details. 00021 // 00022 // You should have received a copy of version 2.0 of the GNU General 00023 // Public License along with this program; if not, write to the Free 00024 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 00025 // MA 02110-1301, USA. 00026 // 00027 // 00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ## 00029 // 00030 00031 /*! 00032 \class eZCountryType ezcountrytype.php 00033 \ingroup eZDatatype 00034 \brief A content datatype that contains country. 00035 00036 The list of countries is fetched from contenet.ini. 00037 Country is stored as text string. 00038 */ 00039 00040 //include_once( 'kernel/classes/ezdatatype.php' ); 00041 //include_once( 'lib/ezutils/classes/ezintegervalidator.php' ); 00042 require_once( 'kernel/common/i18n.php' ); 00043 00044 class eZCountryType extends eZDataType 00045 { 00046 const DATA_TYPE_STRING = 'ezcountry'; 00047 00048 const DEFAULT_LIST_FIELD = 'data_text5'; 00049 00050 const MULTIPLE_CHOICE_FIELD = 'data_int1'; 00051 00052 function eZCountryType() 00053 { 00054 $this->eZDataType( self::DATA_TYPE_STRING, ezi18n( 'kernel/classes/datatypes', 'Country', 'Datatype name' ), 00055 array( 'serialize_supported' => true, 00056 'object_serialize_map' => array( 'data_text' => 'country' ) ) ); 00057 } 00058 00059 /*! 00060 Fetches country list from ini. 00061 */ 00062 static function fetchCountryList() 00063 { 00064 if ( isset( $GLOBALS['CountryList'] ) ) 00065 return $GLOBALS['CountryList']; 00066 00067 $ini = eZINI::instance( 'country.ini' ); 00068 $countries = $ini->getNamedArray(); 00069 eZCountryType::fetchTranslatedNames( $countries ); 00070 $GLOBALS['CountryList'] = $countries; 00071 return $countries; 00072 } 00073 00074 /*! 00075 Fetches translated country names from locale 00076 \a $countries will be updated. 00077 */ 00078 static function fetchTranslatedNames( &$countries ) 00079 { 00080 //include_once( "lib/ezlocale/classes/ezlocale.php" ); 00081 $locale = eZLocale::instance(); 00082 $translatedCountryNames = $locale->translatedCountryNames(); 00083 foreach ( array_keys( $countries ) as $countryKey ) 00084 { 00085 $translatedName = isset( $translatedCountryNames[$countryKey] ) ? $translatedCountryNames[$countryKey] : false; 00086 if ( $translatedName ) 00087 $countries[$countryKey]['Name'] = $translatedName; 00088 } 00089 } 00090 00091 /*! 00092 Fetches country by \a $fetchBy. 00093 if \a $fetchBy is false country name will be used. 00094 */ 00095 static function fetchCountry( $value, $fetchBy = false ) 00096 { 00097 $fetchBy = !$fetchBy ? 'Name' : $fetchBy; 00098 00099 $allCountries = eZCountryType::fetchCountryList(); 00100 $result = false; 00101 if ( $fetchBy == 'Alpha2' and isset( $allCountries[strtoupper( $value )] ) ) 00102 { 00103 $result = $allCountries[$value]; 00104 return $result; 00105 } 00106 00107 foreach ( $allCountries as $country ) 00108 { 00109 if ( isset( $country[$fetchBy] ) and $country[$fetchBy] == $value ) 00110 { 00111 $result = $country; 00112 break; 00113 } 00114 } 00115 00116 return $result; 00117 } 00118 00119 /*! 00120 \reimp 00121 */ 00122 function fetchClassAttributeHTTPInput( $http, $base, $classAttribute ) 00123 { 00124 $classAttributeID = $classAttribute->attribute( 'id' ); 00125 $content = $classAttribute->content(); 00126 00127 if ( $http->hasPostVariable( $base . '_ezcountry_multiple_choice_value_' . $classAttribute->attribute( 'id' ) . '_exists' ) ) 00128 { 00129 $content['multiple_choice'] = $http->hasPostVariable( $base . "_ezcountry_ismultiple_value_" . $classAttributeID ) ? 1 : 0; 00130 } 00131 00132 if ( $http->hasPostVariable( $base . '_ezcountry_default_selection_value_' . $classAttribute->attribute( 'id' ) . '_exists' ) ) 00133 { 00134 if ( $http->hasPostVariable( $base . "_ezcountry_default_country_list_". $classAttributeID ) ) 00135 { 00136 $defaultValues = $http->postVariable( $base . "_ezcountry_default_country_list_". $classAttributeID ); 00137 $defaultList = array(); 00138 foreach ( $defaultValues as $alpha2 ) 00139 { 00140 if ( trim( $alpha2 ) == '' ) 00141 continue; 00142 // Fetch ezcountry by aplha2 code (as reserved in iso-3166 code list) 00143 $eZCountry = eZCountryType::fetchCountry( $alpha2, 'Alpha2' ); 00144 if ( $eZCountry ) 00145 $defaultList[$alpha2] = $eZCountry; 00146 } 00147 $content['default_countries'] = $defaultList; 00148 } 00149 else 00150 { 00151 $content['default_countries'] = array(); 00152 } 00153 } 00154 $classAttribute->setContent( $content ); 00155 $classAttribute->store(); 00156 return true; 00157 } 00158 00159 /*! 00160 \reimp 00161 */ 00162 function preStoreClassAttribute( $classAttribute, $version ) 00163 { 00164 $content = $classAttribute->content(); 00165 return eZCountryType::storeClassAttributeContent( $classAttribute, $content ); 00166 } 00167 00168 function storeClassAttributeContent( $classAttribute, $content ) 00169 { 00170 if ( is_array( $content ) ) 00171 { 00172 $multipleChoice = $content['multiple_choice']; 00173 $defaultCountryList = $content['default_countries']; 00174 $defaultCountry = implode( ',', array_keys( $defaultCountryList ) ); 00175 00176 $classAttribute->setAttribute( self::DEFAULT_LIST_FIELD, $defaultCountry ); 00177 $classAttribute->setAttribute( self::MULTIPLE_CHOICE_FIELD, $multipleChoice ); 00178 } 00179 return false; 00180 } 00181 00182 /*! 00183 Sets the default value. 00184 */ 00185 function initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute ) 00186 { 00187 if ( $currentVersion != false ) 00188 { 00189 $dataText = $originalContentObjectAttribute->content(); 00190 $contentObjectAttribute->setContent( $dataText ); 00191 } 00192 else 00193 { 00194 $default = array( 'value' => array() ); 00195 $contentObjectAttribute->setContent( $default ); 00196 } 00197 } 00198 00199 /*! 00200 \reimp 00201 */ 00202 function validateObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute ) 00203 { 00204 if ( !$contentObjectAttribute->validateIsRequired() ) 00205 return eZInputValidator::STATE_ACCEPTED; 00206 00207 if ( $http->hasPostVariable( $base . '_country_' . $contentObjectAttribute->attribute( 'id' ) ) ) 00208 { 00209 $data = $http->postVariable( $base . '_country_' . $contentObjectAttribute->attribute( 'id' ) ); 00210 00211 if ( count( $data ) > 0 and $data[0] != '' ) 00212 return eZInputValidator::STATE_ACCEPTED; 00213 } 00214 00215 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes', 00216 'Input required.' ) ); 00217 return eZInputValidator::STATE_INVALID; 00218 } 00219 00220 /*! 00221 \reimp 00222 */ 00223 function validateCollectionAttributeHTTPInput( $http, $base, $contentObjectAttribute ) 00224 { 00225 if ( !$contentObjectAttribute->validateIsRequired() ) 00226 return eZInputValidator::STATE_ACCEPTED; 00227 00228 if ( $http->hasPostVariable( $base . '_country_' . $contentObjectAttribute->attribute( 'id' ) ) ) 00229 { 00230 $data = $http->postVariable( $base . '_country_' . $contentObjectAttribute->attribute( 'id' ) ); 00231 00232 if ( count( $data ) > 0 and $data[0] != '' ) 00233 return eZInputValidator::STATE_ACCEPTED; 00234 } 00235 00236 $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes', 00237 'Input required.' ) ); 00238 return eZInputValidator::STATE_INVALID; 00239 } 00240 00241 /*! 00242 Fetches the http post var and stores it in the data instance. 00243 */ 00244 function fetchObjectAttributeHTTPInput( $http, $base, $contentObjectAttribute ) 00245 { 00246 if ( $http->hasPostVariable( $base . '_country_' . $contentObjectAttribute->attribute( 'id' ) ) ) 00247 { 00248 $data = $http->postVariable( $base . '_country_' . $contentObjectAttribute->attribute( 'id' ) ); 00249 $defaultList = array(); 00250 if ( is_array( $data ) ) 00251 { 00252 foreach ( $data as $alpha2 ) 00253 { 00254 if ( trim( $alpha2 ) == '' ) 00255 continue; 00256 00257 $eZCountry = eZCountryType::fetchCountry( $alpha2, 'Alpha2' ); 00258 if ( $eZCountry ) 00259 $defaultList[$alpha2] = $eZCountry; 00260 } 00261 } 00262 else 00263 { 00264 $countries = eZCountryType::fetchCountryList(); 00265 foreach ( $countries as $country ) 00266 { 00267 if ( $country['Name'] == $data ) 00268 { 00269 $defaultList[$country['Alpha2']] = $country['Name']; 00270 } 00271 } 00272 } 00273 $content = array( 'value' => $defaultList ); 00274 00275 $contentObjectAttribute->setContent( $content ); 00276 } 00277 else 00278 { 00279 $content = array( 'value' => array() ); 00280 $contentObjectAttribute->setContent( $content ); 00281 } 00282 return true; 00283 } 00284 00285 /*! 00286 Fetches the http post variables for collected information 00287 */ 00288 function fetchCollectionAttributeHTTPInput( $collection, $collectionAttribute, $http, $base, $contentObjectAttribute ) 00289 { 00290 if ( $http->hasPostVariable( $base . "_country_" . $contentObjectAttribute->attribute( "id" ) ) ) 00291 { 00292 $dataText = $http->postVariable( $base . "_country_" . $contentObjectAttribute->attribute( "id" ) ); 00293 00294 $value = implode( ',', $dataText ); 00295 $collectionAttribute->setAttribute( 'data_text', $value ); 00296 return true; 00297 } 00298 return false; 00299 } 00300 00301 /*! 00302 \reimp 00303 */ 00304 function storeObjectAttribute( $contentObjectAttribute ) 00305 { 00306 $content = $contentObjectAttribute->content(); 00307 00308 $valueArray = $content['value']; 00309 $value = is_array( $valueArray ) ? implode( ',', array_keys( $valueArray ) ) : $valueArray; 00310 00311 $contentObjectAttribute->setAttribute( "data_text", $value ); 00312 } 00313 00314 /*! 00315 \reimp 00316 Simple string insertion is supported. 00317 */ 00318 function isSimpleStringInsertionSupported() 00319 { 00320 return true; 00321 } 00322 00323 /*! 00324 \reimp 00325 */ 00326 function insertSimpleString( $object, $objectVersion, $objectLanguage, 00327 $objectAttribute, $string, 00328 &$result ) 00329 { 00330 $result = array( 'errors' => array(), 00331 'require_storage' => true ); 00332 $content = array( 'value' => $string ); 00333 $objectAttribute->setContent( $content ); 00334 return true; 00335 } 00336 00337 /*! 00338 Returns the content. 00339 */ 00340 function objectAttributeContent( $contentObjectAttribute ) 00341 { 00342 $value = $contentObjectAttribute->attribute( 'data_text' ); 00343 00344 $countryList = explode( ',', $value ); 00345 $resultList = array(); 00346 foreach ( $countryList as $alpha2 ) 00347 { 00348 $eZCountry = eZCountryType::fetchCountry( $alpha2, 'Alpha2' ); 00349 $resultList[$alpha2] = $eZCountry ? $eZCountry : ''; 00350 } 00351 // Supporting of previous version format. 00352 // For backwards compatability. 00353 if ( count( $resultList ) == 1 and $resultList[$value] == '' ) 00354 $resultList = $value; 00355 00356 $content = array( 'value' => $resultList ); 00357 return $content; 00358 } 00359 00360 /*! 00361 \reimp 00362 */ 00363 function classAttributeContent( $classAttribute ) 00364 { 00365 $defaultCountry = $classAttribute->attribute( self::DEFAULT_LIST_FIELD ); 00366 $multipleChoice = $classAttribute->attribute( self::MULTIPLE_CHOICE_FIELD ); 00367 $defaultCountryList = explode( ',', $defaultCountry ); 00368 $resultList = array(); 00369 foreach ( $defaultCountryList as $alpha2 ) 00370 { 00371 $eZCountry = eZCountryType::fetchCountry( $alpha2, 'Alpha2' ); 00372 if ( $eZCountry ) 00373 $resultList[$alpha2] = $eZCountry; 00374 } 00375 $content = array( 'default_countries' => $resultList, 00376 'multiple_choice' => $multipleChoice ); 00377 00378 return $content; 00379 } 00380 00381 /*! 00382 Returns the meta data used for storing search indeces. 00383 */ 00384 function metaData( $contentObjectAttribute ) 00385 { 00386 $content = $contentObjectAttribute->content(); 00387 if ( is_array( $content['value'] ) ) 00388 { 00389 $imploded = ''; 00390 foreach ( $content['value'] as $country ) 00391 { 00392 $countryName = $country['Name']; 00393 if ( $imploded == '' ) 00394 $imploded = $countryName; 00395 else 00396 $imploded .= ',' . $countryName; 00397 } 00398 $content['value'] = $imploded; 00399 } 00400 return $content['value']; 00401 } 00402 00403 /*! 00404 \return string representation of an contentobjectattribute data for simplified export 00405 */ 00406 function toString( $contentObjectAttribute ) 00407 { 00408 return $contentObjectAttribute->attribute( 'data_text' ); 00409 } 00410 00411 function fromString( $contentObjectAttribute, $string ) 00412 { 00413 return $contentObjectAttribute->setAttribute( 'data_text', $string ); 00414 } 00415 00416 /*! 00417 Returns the country for use as a title 00418 */ 00419 function title( $contentObjectAttribute, $name = null ) 00420 { 00421 $content = $contentObjectAttribute->content(); 00422 if ( is_array( $content['value'] ) ) 00423 { 00424 $imploded = ''; 00425 foreach ( $content['value'] as $country ) 00426 { 00427 $countryName = $country['Name']; 00428 if ( $imploded == '' ) 00429 $imploded = $countryName; 00430 else 00431 $imploded .= ',' . $countryName; 00432 } 00433 $content['value'] = $imploded; 00434 } 00435 return $content['value']; 00436 } 00437 00438 function hasObjectAttributeContent( $contentObjectAttribute ) 00439 { 00440 $content = $contentObjectAttribute->content(); 00441 $result = ( ( !is_array( $content['value'] ) and trim( $content['value'] ) != '' ) or ( is_array( $content['value'] ) and count( $content['value'] ) > 0 ) ); 00442 return $result; 00443 } 00444 00445 /*! 00446 \reimp 00447 */ 00448 function isIndexable() 00449 { 00450 return true; 00451 } 00452 00453 /*! 00454 \reimp 00455 */ 00456 function isInformationCollector() 00457 { 00458 return true; 00459 } 00460 00461 /*! 00462 \reimp 00463 */ 00464 function sortKey( $contentObjectAttribute ) 00465 { 00466 //include_once( 'lib/ezi18n/classes/ezchartransform.php' ); 00467 $trans = eZCharTransform::instance(); 00468 $content = $contentObjectAttribute->content(); 00469 if ( is_array( $content['value'] ) ) 00470 { 00471 $imploded = ''; 00472 foreach ( $content['value'] as $country ) 00473 { 00474 $countryName = $country['Name']; 00475 00476 if ( $imploded == '' ) 00477 $imploded = $countryName; 00478 else 00479 $imploded .= ',' . $countryName; 00480 } 00481 $content['value'] = $imploded; 00482 } 00483 return $trans->transformByGroup( $content['value'], 'lowercase' ); 00484 } 00485 00486 /*! 00487 \reimp 00488 */ 00489 function sortKeyType() 00490 { 00491 return 'string'; 00492 } 00493 00494 /*! 00495 \reimp 00496 */ 00497 function diff( $old, $new, $options = false ) 00498 { 00499 return null; 00500 } 00501 } 00502 00503 eZDataType::register( eZCountryType::DATA_TYPE_STRING, 'ezcountrytype' ); 00504 00505 ?>