[ Index ]
 

Code source de eZ Publish 3.9.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/kernel/classes/datatypes/ezcountry/ -> ezcountrytype.php (source)

   1  <?php
   2  //
   3  // Definition of eZCountryType class
   4  //
   5  // Created on: <20-Feb-2006 11:11:19 vs>
   6  //
   7  // SOFTWARE NAME: eZ publish
   8  // SOFTWARE RELEASE: 3.9.0
   9  // BUILD VERSION: 17785
  10  // COPYRIGHT NOTICE: Copyright (C) 1999-2006 eZ systems AS
  11  // SOFTWARE LICENSE: GNU General Public License v2.0
  12  // NOTICE: >
  13  //   This program is free software; you can redistribute it and/or
  14  //   modify it under the terms of version 2.0  of the GNU General
  15  //   Public License as published by the Free Software Foundation.
  16  //
  17  //   This program is distributed in the hope that it will be useful,
  18  //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  19  //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20  //   GNU General Public License for more details.
  21  //
  22  //   You should have received a copy of version 2.0 of the GNU General
  23  //   Public License along with this program; if not, write to the Free
  24  //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25  //   MA 02110-1301, USA.
  26  //
  27  //
  28  
  29  /*!
  30    \class eZCountryType ezcountrytype.php
  31    \ingroup eZDatatype
  32    \brief A content datatype that contains country.
  33  
  34    The list of countries is fetched from contenet.ini.
  35    Country is stored as text string.
  36  */
  37  
  38  include_once ( 'kernel/classes/ezdatatype.php' );
  39  include_once ( 'lib/ezutils/classes/ezintegervalidator.php' );
  40  include_once ( 'kernel/common/i18n.php' );
  41  
  42  define( 'EZ_DATATYPESTRING_COUNTRY', 'ezcountry' );
  43  define( 'EZ_DATATYPESTRING_COUNTRY_DEFAULT_LIST_FIELD', 'data_text5' );
  44  define( 'EZ_DATATYPESTRING_COUNTRY_MULTIPLE_CHOICE_FIELD', 'data_int1' );
  45  
  46  class eZCountryType extends eZDataType
  47  {
  48      function eZCountryType()
  49      {
  50          $this->eZDataType( EZ_DATATYPESTRING_COUNTRY, ezi18n( 'kernel/classes/datatypes', 'Country', 'Datatype name' ),
  51                             array( 'serialize_supported' => true,
  52                                    'object_serialize_map' => array( 'data_text' => 'country' ) ) );
  53      }
  54  
  55      /*!
  56       Fetches country list from ini.
  57      */
  58      function fetchCountryList()
  59      {
  60          if ( isset( $GLOBALS['CountryList'] ) )
  61              return $GLOBALS['CountryList'];
  62  
  63          $ini =& eZINI::instance( 'country.ini' );
  64          $countries = $ini->getNamedArray();
  65          eZCountryType::fetchTranslatedNames( $countries );
  66          $GLOBALS['CountryList'] = $countries;
  67          return $countries;
  68      }
  69  
  70      /*!
  71        Fetches translated country names from locale
  72        \a $countries will be updated.
  73      */
  74      function fetchTranslatedNames( &$countries )
  75      {
  76          include_once ( "lib/ezlocale/classes/ezlocale.php" );
  77          $locale =& eZLocale::instance();
  78          $translatedCountryNames = $locale->translatedCountryNames();
  79          foreach ( array_keys( $countries ) as $countryKey )
  80          {
  81              $translatedName = isset( $translatedCountryNames[$countryKey] ) ? $translatedCountryNames[$countryKey] : false;
  82              if ( $translatedName )
  83                  $countries[$countryKey]['Name'] = $translatedName;
  84          }
  85      }
  86  
  87      /*!
  88        Fetches country by \a $fetchBy.
  89        if \a $fetchBy is false country name will be used.
  90      */
  91      function fetchCountry( $value, $fetchBy = false )
  92      {
  93          $fetchBy = !$fetchBy ? 'Name' : $fetchBy;
  94  
  95          $allCountries = eZCountryType::fetchCountryList();
  96          $result = false;
  97          if ( $fetchBy == 'Alpha2' and isset( $allCountries[strtoupper( $value )] ) )
  98          {
  99              $result = $allCountries[$value];
 100              return $result;
 101          }
 102  
 103          foreach ( $allCountries as $country )
 104          {
 105              if ( isset( $country[$fetchBy] ) and $country[$fetchBy] == $value )
 106              {
 107                  $result = $country;
 108                  break;
 109              }
 110          }
 111  
 112          return $result;
 113      }
 114  
 115      /*!
 116       \reimp
 117      */
 118      function fetchClassAttributeHTTPInput( &$http, $base, &$classAttribute )
 119      {
 120          $classAttributeID = $classAttribute->attribute( 'id' );
 121          $content = $classAttribute->content();
 122  
 123          if ( $http->hasPostVariable( $base . '_ezcountry_multiple_choice_value_' . $classAttribute->attribute( 'id' ) . '_exists' ) )
 124          {
 125               $content['multiple_choice'] = $http->hasPostVariable( $base . "_ezcountry_ismultiple_value_" . $classAttributeID ) ? 1 : 0;
 126          }
 127  
 128          if ( $http->hasPostVariable( $base . '_ezcountry_default_selection_value_' . $classAttribute->attribute( 'id' ) . '_exists' ) )
 129          {
 130              if ( $http->hasPostVariable( $base . "_ezcountry_default_country_list_". $classAttributeID ) )
 131              {
 132                  $defaultValues = $http->postVariable( $base . "_ezcountry_default_country_list_". $classAttributeID );
 133                  $defaultList = array();
 134                  foreach ( $defaultValues as $alpha2 )
 135                  {
 136                      if ( trim( $alpha2 ) == '' )
 137                          continue;
 138                      // Fetch ezcountry by aplha2 code (as reserved in iso-3166 code list)
 139                      $eZCountry = eZCountryType::fetchCountry( $alpha2, 'Alpha2' );
 140                      if ( $eZCountry )
 141                          $defaultList[$alpha2] = $eZCountry;
 142                  }
 143                  $content['default_countries'] = $defaultList;
 144              }
 145              else
 146              {
 147                  $content['default_countries'] = array();
 148              }
 149          }
 150          $classAttribute->setContent( $content );
 151          $classAttribute->store();
 152          return true;
 153      }
 154  
 155      /*!
 156       \reimp
 157      */
 158      function preStoreClassAttribute( &$classAttribute, $version )
 159      {
 160          $content = $classAttribute->content();
 161          return eZCountryType::storeClassAttributeContent( $classAttribute, $content );
 162      }
 163  
 164      function storeClassAttributeContent( &$classAttribute, $content )
 165      {
 166          if ( is_array( $content ) )
 167          {
 168              $multipleChoice = $content['multiple_choice'];
 169              $defaultCountryList = $content['default_countries'];
 170              $defaultCountry = implode( ',', array_keys( $defaultCountryList ) );
 171  
 172              $classAttribute->setAttribute( EZ_DATATYPESTRING_COUNTRY_DEFAULT_LIST_FIELD, $defaultCountry );
 173              $classAttribute->setAttribute( EZ_DATATYPESTRING_COUNTRY_MULTIPLE_CHOICE_FIELD, $multipleChoice );
 174          }
 175          return false;
 176      }
 177  
 178      /*!
 179       Sets the default value.
 180      */
 181      function initializeObjectAttribute( &$contentObjectAttribute, $currentVersion, &$originalContentObjectAttribute )
 182      {
 183          if ( $currentVersion != false )
 184          {
 185              $dataText = $originalContentObjectAttribute->content();
 186              $contentObjectAttribute->setContent( $dataText );
 187          }
 188          else
 189          {
 190              $default = array( 'value' => array() );
 191              $contentObjectAttribute->setContent( $default );
 192          }
 193      }
 194  
 195      /*!
 196       \reimp
 197      */
 198      function validateObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
 199      {
 200          if ( !$contentObjectAttribute->validateIsRequired() )
 201              return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
 202  
 203          if ( $http->hasPostVariable( $base . '_country_' . $contentObjectAttribute->attribute( 'id' ) ) )
 204          {
 205              $data = $http->postVariable( $base . '_country_' . $contentObjectAttribute->attribute( 'id' ) );
 206  
 207              if ( count( $data ) > 0 and $data[0] != '' )
 208                  return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
 209          }
 210  
 211          $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
 212                                                               'Input required.' ) );
 213          return EZ_INPUT_VALIDATOR_STATE_INVALID;
 214      }
 215  
 216      /*!
 217       \reimp
 218      */
 219      function validateCollectionAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
 220      {
 221          if ( !$contentObjectAttribute->validateIsRequired() )
 222              return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
 223  
 224          if ( $http->hasPostVariable( $base . '_country_' . $contentObjectAttribute->attribute( 'id' ) ) )
 225          {
 226              $data = $http->postVariable( $base . '_country_' . $contentObjectAttribute->attribute( 'id' ) );
 227  
 228              if ( count( $data ) > 0 and $data[0] != '' )
 229                  return EZ_INPUT_VALIDATOR_STATE_ACCEPTED;
 230          }
 231  
 232          $contentObjectAttribute->setValidationError( ezi18n( 'kernel/classes/datatypes',
 233                                                               'Input required.' ) );
 234          return EZ_INPUT_VALIDATOR_STATE_INVALID;
 235      }
 236  
 237      /*!
 238       Fetches the http post var and stores it in the data instance.
 239      */
 240      function fetchObjectAttributeHTTPInput( &$http, $base, &$contentObjectAttribute )
 241      {
 242          if ( $http->hasPostVariable( $base . '_country_' . $contentObjectAttribute->attribute( 'id' ) ) )
 243          {
 244              $data = $http->postVariable( $base . '_country_' . $contentObjectAttribute->attribute( 'id' ) );
 245              $defaultList = array();
 246              if ( is_array( $data ) )
 247              {
 248                  foreach ( $data as $alpha2 )
 249                  {
 250                      if ( trim( $alpha2 ) == '' )
 251                          continue;
 252  
 253                      $eZCountry = eZCountryType::fetchCountry( $alpha2, 'Alpha2' );
 254                      if ( $eZCountry )
 255                          $defaultList[$alpha2] = $eZCountry;
 256                  }
 257              }
 258              else
 259              {
 260                  $countries = eZCountryType::fetchCountryList();
 261                  foreach ( $countries as $country )
 262                  {
 263                      if ( $country['Name'] == $data )
 264                      {
 265                          $defaultList[$country['Alpha2']] = $country['Name'];
 266                      }
 267                  }
 268              }
 269              $content = array( 'value' => $defaultList );
 270  
 271              $contentObjectAttribute->setContent( $content );
 272          }
 273          else
 274          {
 275              $content = array( 'value' => array() );
 276              $contentObjectAttribute->setContent( $content );
 277          }
 278          return true;
 279      }
 280  
 281      /*!
 282       Fetches the http post variables for collected information
 283      */
 284      function fetchCollectionAttributeHTTPInput( &$collection, &$collectionAttribute, &$http, $base, &$contentObjectAttribute )
 285      {
 286          if ( $http->hasPostVariable( $base . "_country_" . $contentObjectAttribute->attribute( "id" ) ) )
 287          {
 288              $dataText = $http->postVariable( $base . "_country_" . $contentObjectAttribute->attribute( "id" ) );
 289  
 290              $value = implode( ',', $dataText );
 291              $collectionAttribute->setAttribute( 'data_text', $value );
 292              return true;
 293          }
 294          return false;
 295      }
 296  
 297      /*!
 298       \reimp
 299      */
 300      function storeObjectAttribute( &$contentObjectAttribute )
 301      {
 302          $content = $contentObjectAttribute->content();
 303  
 304          $valueArray = $content['value'];
 305          $value = is_array( $valueArray ) ? implode( ',', array_keys( $valueArray ) ) : $valueArray;
 306  
 307          $contentObjectAttribute->setAttribute( "data_text", $value );
 308      }
 309  
 310      /*!
 311       \reimp
 312       Simple string insertion is supported.
 313      */
 314      function isSimpleStringInsertionSupported()
 315      {
 316          return true;
 317      }
 318  
 319      /*!
 320       \reimp
 321      */
 322      function insertSimpleString( &$object, $objectVersion, $objectLanguage,
 323                                   &$objectAttribute, $string,
 324                                   &$result )
 325      {
 326          $result = array( 'errors' => array(),
 327                           'require_storage' => true );
 328          $content = array( 'value' => $string );
 329          $objectAttribute->setContent( $content );
 330          return true;
 331      }
 332  
 333      /*!
 334       Returns the content.
 335      */
 336      function &objectAttributeContent( &$contentObjectAttribute )
 337      {
 338          $value = $contentObjectAttribute->attribute( 'data_text' );
 339  
 340          $countryList = explode( ',', $value );
 341          $resultList = array();
 342          foreach ( $countryList as $alpha2 )
 343          {
 344              $eZCountry = eZCountryType::fetchCountry( $alpha2, 'Alpha2' );
 345              $resultList[$alpha2] = $eZCountry ? $eZCountry : '';
 346          }
 347          // Supporting of previous version format.
 348          // For backwards compatability.
 349          if ( count( $resultList ) == 1 and $resultList[$value] == '' )
 350              $resultList = $value;
 351  
 352          $content = array( 'value' => $resultList );
 353          return $content;
 354      }
 355  
 356      /*!
 357       \reimp
 358      */
 359      function &classAttributeContent( &$classAttribute )
 360      {
 361          $defaultCountry = $classAttribute->attribute( EZ_DATATYPESTRING_COUNTRY_DEFAULT_LIST_FIELD );
 362          $multipleChoice = $classAttribute->attribute( EZ_DATATYPESTRING_COUNTRY_MULTIPLE_CHOICE_FIELD );
 363          $defaultCountryList = explode( ',', $defaultCountry );
 364          $resultList = array();
 365          foreach ( $defaultCountryList as $alpha2 )
 366          {
 367              $eZCountry = eZCountryType::fetchCountry( $alpha2, 'Alpha2' );
 368              if ( $eZCountry )
 369                  $resultList[$alpha2] = $eZCountry;
 370          }
 371          $content = array( 'default_countries' => $resultList,
 372                            'multiple_choice' => $multipleChoice );
 373  
 374          return $content;
 375      }
 376  
 377      /*!
 378       Returns the meta data used for storing search indeces.
 379      */
 380      function metaData( &$contentObjectAttribute )
 381      {
 382          $content = $contentObjectAttribute->content();
 383          if ( is_array( $content['value'] ) )
 384          {
 385              $imploded = '';
 386              foreach ( $content['value'] as $country )
 387              {
 388                  $countryName = $country['Name'];
 389                  if ( $imploded == '' )
 390                      $imploded = $countryName;
 391                  else
 392                      $imploded .= ',' . $countryName;
 393              }
 394              $content['value'] = $imploded;
 395          }
 396          return $content['value'];
 397      }
 398  
 399      /*!
 400       \return string representation of an contentobjectattribute data for simplified export
 401      */
 402      function toString( $contentObjectAttribute )
 403      {
 404          return $contentObjectAttribute->attribute( 'data_text' );
 405      }
 406  
 407      function fromString( &$contentObjectAttribute, $string )
 408      {
 409          return $contentObjectAttribute->setAttribute( 'data_text', $string );
 410      }
 411  
 412      /*!
 413       Returns the country for use as a title
 414      */
 415      function title( &$contentObjectAttribute )
 416      {
 417          $content = $contentObjectAttribute->content();
 418          if ( is_array( $content['value'] ) )
 419          {
 420              $imploded = '';
 421              foreach ( $content['value'] as $country )
 422              {
 423                  $countryName = $country['Name'];
 424                  if ( $imploded == '' )
 425                      $imploded = $countryName;
 426                  else
 427                      $imploded .= ',' . $countryName;
 428              }
 429              $content['value'] = $imploded;
 430          }
 431          return $content['value'];
 432      }
 433  
 434      function hasObjectAttributeContent( &$contentObjectAttribute )
 435      {
 436          $content = $contentObjectAttribute->content();
 437          $result = ( ( !is_array( $content['value'] ) and trim( $content['value'] ) != '' ) or ( is_array( $content['value'] ) and count( $content['value'] ) > 0 ) );
 438          return $result;
 439      }
 440  
 441      /*!
 442       \reimp
 443      */
 444      function isIndexable()
 445      {
 446          return true;
 447      }
 448  
 449      /*!
 450       \reimp
 451      */
 452      function isInformationCollector()
 453      {
 454          return true;
 455      }
 456  
 457      /*!
 458       \reimp
 459      */
 460      function sortKey( &$contentObjectAttribute )
 461      {
 462          include_once ( 'lib/ezi18n/classes/ezchartransform.php' );
 463          $trans =& eZCharTransform::instance();
 464          $content = $contentObjectAttribute->content();
 465          if ( is_array( $content['value'] ) )
 466          {
 467              $imploded = '';
 468              foreach ( $content['value'] as $country )
 469              {
 470                  $countryName = $country['Name'];
 471  
 472                  if ( $imploded == '' )
 473                      $imploded = $countryName;
 474                  else
 475                      $imploded .= ',' . $countryName;
 476              }
 477              $content['value'] = $imploded;
 478          }
 479          return $trans->transformByGroup( $content['value'], 'lowercase' );
 480      }
 481  
 482      /*!
 483       \reimp
 484      */
 485      function sortKeyType()
 486      {
 487          return 'string';
 488      }
 489  
 490      /*!
 491        \reimp
 492      */
 493      function diff( $old, $new, $options = false )
 494      {
 495          return null;
 496      }
 497  }
 498  
 499  eZDataType::register( EZ_DATATYPESTRING_COUNTRY, 'ezcountrytype' );
 500  
 501  ?>


Généré le : Sat Feb 24 10:30:04 2007 par Balluche grâce à PHPXref 0.7