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