b2evolution PHP Cross Reference Blogging Systems

Source: /inc/regional/model/_regional.funcs.php - 736 lines - 22516 bytes - Summary - Text - Print

Description: This file implements regional functions. This file is part of the evoCore framework - {@link http://evocore.net/} See also {@link http://sourceforge.net/projects/evocms/}.

   1  <?php
   2  /**

   3   * This file implements regional functions.

   4   *

   5   * This file is part of the evoCore framework - {@link http://evocore.net/}

   6   * See also {@link http://sourceforge.net/projects/evocms/}.

   7   *

   8   * @copyright (c)2003-2014 by Francois Planque - {@link http://fplanque.com/}

   9   * Parts of this file are copyright (c)2004-2006 by Daniel HAHLER - {@link http://thequod.de/contact}.

  10   *

  11   * {@internal License choice

  12   * - If you have received this file as part of a package, please find the license.txt file in

  13   *   the same folder or the closest folder above for complete license terms.

  14   * - If you have received this file individually (e-g: from http://evocms.cvs.sourceforge.net/)

  15   *   then you must choose one of the following licenses before using the file:

  16   *   - GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php

  17   *   - Mozilla Public License 1.1 (MPL) - http://www.opensource.org/licenses/mozilla1.1.php

  18   * }}

  19   *

  20   * {@internal Open Source relicensing agreement:

  21   * Daniel HAHLER grants Francois PLANQUE the right to license

  22   * Daniel HAHLER's contributions to this file and the b2evolution project

  23   * under any OSI approved OSS license (http://www.opensource.org/licenses/).

  24   * }}

  25   *

  26   * @package evocore

  27   *

  28   * {@internal Below is a list of authors who have contributed to design/coding of this file: }}

  29   * @author fplanque: Francois PLANQUE.

  30   *

  31   * @version $Id: _regional.funcs.php 235 2011-11-08 12:50:06Z yura $

  32   */
  33  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  34  
  35  
  36  /**

  37   * Get regional option list from given array

  38   * 

  39   * @param array rows

  40   * @param integer ID of selected row

  41   * @param array field params

  42   * @return string html tags <option>

  43   */
  44  function get_regional_option_list( $rows, $selected = 0, $params = array() )
  45  {
  46      $params = array_merge( array(
  47              'allow_none' => true,
  48              'none_option_value' => '',
  49              'none_option_text' => T_('All'),
  50              'group_preferred' => true,
  51              'group_name_preferred' => T_('Frequent'),
  52              'group_name_other' => T_('Other'),
  53          ), $params );
  54  
  55      $r = '';
  56  
  57      if( $params['allow_none'] )
  58      {    // we set current value of a country if it is sent to function
  59          $r .= '<option value="'.$params['none_option_value'].'"';
  60          if( empty($default) ) $r .= ' selected="selected"';
  61          $r .= '>'.format_to_output( $params['none_option_text'] ).'</option>'."\n";
  62      }
  63  
  64      $pref_rows = array(); //preferred countries.

  65      $rest_rows = array(); // not preffered countries (the rest)

  66  
  67      foreach( $rows as $row_Obj )
  68      {
  69          if( $params['group_preferred'] && $row_Obj->preferred == 1 )
  70          {    // if the country is preferred we add it to selected array.
  71              $pref_rows[] = $row_Obj;
  72          }
  73          $rest_rows[] = $row_Obj;
  74      }
  75  
  76      if( count( $pref_rows ) )
  77      {    // if we don't have preferred countries in this case we don't have to show optgroup
  78          // in option list

  79          $r .= '<optgroup label="'.$params['group_name_preferred'].'">';
  80          foreach( $pref_rows as $row_Obj )
  81          {
  82              $r .=  '<option value="'.$row_Obj->ID.'"';
  83              if( $row_Obj->ID == $selected ) $r .= ' selected="selected"';
  84              $r .= '>';
  85              $r .= format_to_output( $row_Obj->name, 'htmlbody' );
  86              $r .=  '</option>'."\n";
  87          }
  88          $r .= '</optgroup>';
  89  
  90          if( count( $rest_rows ) )
  91          { // if we don't have rest countries we do not create optgroup for them
  92              $r .= '<optgroup label="'.$params['group_name_other'].'">';
  93              foreach( $rest_rows as $row_Obj )
  94              {
  95                  $r .=  '<option value="'.$row_Obj->ID.'"';
  96                  if( $row_Obj->ID == $selected ) $r .= ' selected="selected"';
  97                  $r .= '>';
  98                  $r .= format_to_output( $row_Obj->name, 'htmlbody' );
  99                  $r .=  '</option>'."\n";
 100              }
 101              $r .= '</optgroup>';
 102          }
 103      }
 104      else
 105      {    // if we have only rest countries we get here
 106          foreach( $rest_rows as $row_Obj )
 107          {
 108              $r .=  '<option value="'.$row_Obj->ID.'"';
 109              if( $row_Obj->ID == $selected ) $r .= ' selected="selected"';
 110              $r .= '>';
 111              $r .= format_to_output( $row_Obj->name, 'htmlbody' );
 112              $r .=  '</option>'."\n";
 113          }
 114      }
 115  
 116      return $r;
 117  }
 118  
 119  /**

 120   * Get option list with regions by country ID

 121   * 

 122   * @param integer country ID

 123   * @param integer selected region ID

 124   * @param array field params

 125   * @return string html tags <option>

 126   */
 127  function get_regions_option_list( $country_ID, $region_ID = 0, $params = array() )
 128  {
 129      $params = array_merge( array(
 130              'group_name_preferred' => T_('Frequent regions'),
 131              'group_name_other' => T_('Other regions'),
 132          ), $params );
 133  
 134      global $DB;
 135  
 136      $regions = $DB->get_results( '
 137          SELECT rgn_ID as ID, rgn_name as name, rgn_preferred as preferred
 138              FROM T_regional__region
 139           WHERE rgn_ctry_ID = "'.$DB->escape( $country_ID ).'"
 140             AND rgn_enabled = 1
 141           ORDER BY rgn_name' );
 142  
 143      return get_regional_option_list( $regions, $region_ID, $params );
 144  }
 145  
 146  /**

 147   * Get option list with sub-regions by region ID

 148   * 

 149   * @param integer region ID

 150   * @param integer selected sub-region ID

 151   * @param array field params

 152   * @return string html tags <option>

 153   */
 154  function get_subregions_option_list( $region_ID, $subregion_ID = 0, $params = array() )
 155  {
 156      $params = array_merge( array(
 157              'group_name_preferred' => T_('Frequent sub-regions'),
 158              'group_name_other' => T_('Other sub-regions'),
 159          ), $params );
 160  
 161      global $DB;
 162  
 163      $subregions = $DB->get_results( '
 164          SELECT subrg_ID as ID, subrg_name as name, subrg_preferred as preferred
 165            FROM T_regional__subregion
 166           WHERE subrg_rgn_ID = "'.$DB->escape( $region_ID ).'"
 167             AND subrg_enabled = 1
 168           ORDER BY subrg_name' );
 169  
 170      return get_regional_option_list( $subregions, $subregion_ID, $params );
 171  }
 172  
 173  /**

 174   * Get option list with cities by country, region or subregion

 175   * 

 176   * @param integer country ID

 177   * @param integer region ID

 178   * @param integer subregion ID

 179   * @param integer selected city ID

 180   * @param array field params

 181   * @return string html tags <option>

 182   */
 183  function get_cities_option_list( $country_ID, $region_ID = 0, $subregion_ID = 0, $city_ID = 0, $params = array() )
 184  {
 185      $params = array_merge( array(
 186              'group_name_preferred' => T_('Frequent cities'),
 187              'group_name_other' => T_('Other cities'),
 188          ), $params );
 189  
 190      global $DB;
 191  
 192      $get_cities = true;
 193      $sql_where = array();
 194  
 195      // Get city by country

 196      $sql_where[] = 'city_ctry_ID = "'.$DB->escape( $country_ID ).'"';
 197  
 198      if( $region_ID > 0 )
 199      {    // Get cities by region
 200          $sql_where[] = 'city_rgn_ID = "'.$DB->escape( $region_ID ).'"';
 201      }
 202      else
 203      {    // Select all cities from current country
 204          $regions_count = $DB->get_var( '
 205              SELECT COUNT( rgn_ID )
 206                FROM T_regional__region
 207               WHERE rgn_ctry_ID = "'.$DB->escape( $country_ID ).'"
 208                 AND rgn_enabled = 1' );
 209          if( $regions_count == 0 )
 210          {    // If regions don't exist for current country we show all cities of the country
 211              $sql_where[] = 'city_rgn_ID IS NULL';
 212          }
 213          else
 214          {    // Don't get cities without selected region
 215              $get_cities = false;
 216          }
 217      }
 218  
 219      if( $region_ID > 0 )
 220      {    // Select sub-regions only when region_ID is defined
 221          if( $subregion_ID > 0 )
 222          {    // Get cities by sub-region
 223              $sql_where[] = 'city_subrg_ID = "'.$DB->escape( $subregion_ID ).'"';
 224          }
 225          else
 226          {    // Select all cities from current region
 227              $subregions_count = $DB->get_var( '
 228                  SELECT COUNT( subrg_ID )
 229                    FROM T_regional__subregion
 230                   WHERE subrg_rgn_ID = "'.$DB->escape( $region_ID ).'"
 231                   AND subrg_enabled = 1' );
 232              if( $subregions_count == 0 )
 233              {    // If sub-regions don't exist for current region we show all cities of the region
 234                  $sql_where[] = 'city_subrg_ID IS NULL';
 235              }
 236              else
 237              {    // Don't get cities without selected sub-region
 238                  $get_cities = false;
 239              }
 240          }
 241      }
 242  
 243      if( $get_cities )
 244      {    // Get cities from DB
 245          $sql_where[] = 'city_enabled = 1';
 246          $cities = $DB->get_results( '
 247              SELECT city_ID as ID, CONCAT( city_name, " (", city_postcode, ")" ) as name, city_preferred as preferred
 248                FROM T_regional__city
 249               WHERE '.implode( ' AND ', $sql_where ).'
 250               ORDER BY city_name' );
 251      }
 252      else
 253      {
 254          $cities = array();
 255      }
 256  
 257      return get_regional_option_list( $cities, $city_ID, $params );
 258  }
 259  
 260  
 261  /**

 262   * Import cities from CSV file

 263   * 

 264   * @param integer country ID

 265   * @param string 

 266   * @return array (

 267   *   'inserted' => Count of inserted cities,

 268   *   'updated'  => Count of updated cities );

 269   */
 270  function import_cities( $country_ID, $file_name )
 271  {
 272      global $DB;
 273  
 274      // Begin transaction

 275      $DB->begin();
 276  
 277      // Get all sub-regions of the current country

 278      $subregions_data = $DB->get_results( '
 279          SELECT subrg_ID, subrg_code, rgn_ID, rgn_ctry_ID
 280            FROM T_regional__subregion
 281            LEFT JOIN T_regional__region ON subrg_rgn_ID = rgn_ID
 282           WHERE rgn_ctry_ID = '.$DB->quote( $country_ID ) );
 283  
 284      $subregions = array();
 285      foreach( $subregions_data as $subregion )
 286      {
 287          $subregions[$subregion->subrg_code] = $subregion;
 288      }
 289      unset( $subregions_data );
 290  
 291  
 292      // Open file

 293      $file_handle = fopen( $file_name, 'r' );
 294  
 295      $c = 0;
 296      $cities_insert_values = array();
 297      $cities_update_values = array();
 298      while( $data = fgetcsv( $file_handle, 1024, ";" ) )
 299      {
 300          if( $c == 0 )
 301          {    // Skip first row with titles
 302              $c++;
 303              continue;
 304          }
 305  
 306          $postcode = trim( $data[0] );
 307          $name = trim( $data[1] );
 308          $subregion_code = '';
 309          if( isset( $data[2] ) )
 310          {    // Optional field
 311              $subregion_code = trim( $data[2] );
 312          }
 313  
 314          if( empty( $postcode ) && empty( $name ) )
 315          {    // Skip empty row
 316              continue;
 317          }
 318  
 319          /*if( empty( $subregion_code ) )

 320          {    // If field subregion_code is NOT defined, we get it from city postcode ( 2 first letters )

 321              $subregion_code = substr( $postcode, 0, 2 );

 322          }*/
 323  
 324          $city = array(
 325              'ctry_ID'  => $country_ID,
 326              'postcode' => $DB->quote( $postcode ),
 327              'name'     => $DB->quote( $name ),
 328          );
 329  
 330          if( empty( $subregion_code ) || ! isset( $subregions[$subregion_code] ) )
 331          {    // Subregion is not defined and not found in DB
 332              $city['rgn_ID'] = 'NULL';
 333              $city['subrg_ID'] = 'NULL';
 334          }
 335          else
 336          {    // Set region ID & subregion ID for current city
 337              $city['rgn_ID'] = $subregions[$subregion_code]->rgn_ID;
 338              $city['subrg_ID'] = $subregions[$subregion_code]->subrg_ID;
 339          }
 340  
 341          // Get city from DB with current country, postcode & name

 342          $existing_city = $DB->get_row( '
 343              SELECT city_ID, city_rgn_ID, city_subrg_ID
 344                FROM T_regional__city
 345               WHERE city_ctry_ID = '.$city['ctry_ID'].'
 346                 AND city_postcode = '.$city['postcode'].'
 347                 AND city_name = '.$city['name'] );
 348  
 349          if( !empty( $existing_city ) )
 350          {    // City already exist with such country, postcode & name
 351              if( $city['subrg_ID'] != $existing_city->city_subrg_ID )
 352              {    // Set new sub-region & region for current city
 353                  $cities_update_values[] = 'city_rgn_ID = '.$city['rgn_ID'].', city_subrg_ID = '.$city['subrg_ID'].'
 354                      WHERE city_ID = '.$existing_city->city_ID;
 355              }
 356          }
 357          else
 358          {    // Insert a new city
 359              $cities_insert_values[] = '( '.implode( ', ', $city ).' )';
 360          }
 361  
 362          $c++;
 363      }
 364  
 365      // Close file pointer

 366      fclose( $file_handle );
 367  
 368      $count_insert_cities = count( $cities_insert_values );
 369      $count_update_cities = count( $cities_update_values );
 370  
 371      if( $count_insert_cities > 0 )
 372      {    // New cities are exist to import
 373          // Split an insert data to avoid big sql queries

 374          $cities_insert_values = array_chunk( $cities_insert_values, 1000 );
 375  
 376          foreach( $cities_insert_values as $insert_values )
 377          {
 378              $insert_values = implode( ', ', $insert_values );
 379  
 380              // Insert new cities into DB

 381              $DB->query( '
 382                  INSERT INTO T_regional__city
 383                  ( city_ctry_ID, city_postcode, city_name, city_rgn_ID, city_subrg_ID )
 384                  VALUES '.$insert_values );
 385          }
 386      }
 387  
 388      if( $count_update_cities > 0 )
 389      {    // Cities to update
 390          foreach( $cities_update_values as $update_sql )
 391          {    // Update a existing city
 392              $DB->query( '
 393                  UPDATE T_regional__city
 394                  SET '.$update_sql );
 395          }
 396      }
 397  
 398      // Commit transaction

 399      $DB->commit();
 400  
 401      return array(
 402          'inserted' => $count_insert_cities,
 403          'updated'  => $count_update_cities );
 404  }
 405  
 406  
 407  /**

 408   * Initialize JavaScript for AJAX loading of regions, subregions and cities

 409   *

 410   * @param string Prefix of fields group

 411   * @param boolean TRUE if region is visible (subregion & city also are visible)

 412   */
 413  function echo_regional_js( $prefix, $region_visible )
 414  {
 415      if( !$region_visible )
 416      {    // If region is NOT visible we don't need in these ajax functions
 417          return;
 418      }
 419  ?>
 420  <script type="text/javascript">
 421  <?php /*jQuery( document ).ready( function()

 422  {

 423      if( jQuery( '#<?php echo $prefix; ?>_ctry_ID' ).val() > 0 && jQuery( '#<?php echo $prefix; ?>_rgn_ID option' ).length == 1 )

 424      {    // Preload a regions for case when country is selected as default but not saved in DB

 425          load_regions( jQuery( '#<?php echo $prefix; ?>_ctry_ID' ).val(), jQuery( '#<?php echo $prefix; ?>_rgn_ID' ).val() );

 426      }

 427  } );

 428  */ ?>
 429  jQuery( '#<?php echo $prefix; ?>_ctry_ID' ).change( function ()
 430  {    // Load option list with regions for seleted country
 431      load_regions( jQuery( this ).val(), 0 );
 432  } );
 433  
 434  jQuery( '#<?php echo $prefix; ?>_rgn_ID' ).change( function ()
 435  {    // Change option list with sub-regions
 436      load_subregions( jQuery( '#<?php echo $prefix; ?>_ctry_ID' ).val(), jQuery( this ).val() );
 437  } );
 438  
 439  jQuery( '#<?php echo $prefix; ?>_subrg_ID' ).change( function ()
 440  {    // Change option list with cities
 441      load_cities( jQuery( '#<?php echo $prefix; ?>_ctry_ID' ).val(), jQuery( '#<?php echo $prefix; ?>_rgn_ID' ).val(), jQuery( this ).val() );
 442  } );
 443  
 444  
 445  jQuery( '#button_refresh_region' ).click( function ()
 446  {    // Button - Refresh regions
 447      load_regions( jQuery( '#<?php echo $prefix; ?>_ctry_ID' ).val(), 0 );
 448      return false;
 449  } );
 450  
 451  jQuery( '#button_refresh_subregion' ).click( function ()
 452  {    // Button - Refresh sub-regions
 453      load_subregions( jQuery( '#<?php echo $prefix; ?>_ctry_ID' ).val(), jQuery( '#<?php echo $prefix; ?>_rgn_ID' ).val() );
 454      return false;
 455  } );
 456  
 457  jQuery( '#button_refresh_city' ).click( function ()
 458  {    // Button - Refresh cities
 459      load_cities( jQuery( '#<?php echo $prefix; ?>_ctry_ID' ).val(), jQuery( '#<?php echo $prefix; ?>_rgn_ID' ).val(), jQuery( '#<?php echo $prefix; ?>_subrg_ID' ).val() );
 460      return false;
 461  } );
 462  
 463  
 464  function load_regions( country_ID, region_ID )
 465  {    // Load option list with regions for seleted country
 466      jQuery( '#<?php echo $prefix; ?>_rgn_ID' ).next().find( 'button' ).hide().next().show();
 467      jQuery.ajax( {
 468      type: 'POST',
 469      url: '<?php echo get_samedomain_htsrv_url(); ?>anon_async.php',
 470      data: 'action=get_regions_option_list&page=edit&mode=load_all&ctry_id=' + country_ID + '&rgn_id=' + region_ID,
 471      success: function( result )
 472          {
 473              jQuery( '#<?php echo $prefix; ?>_rgn_ID' ).next().find( 'button' ).show().next().hide();
 474  
 475              result = ajax_debug_clear( result );
 476              var options = result.split( '-##-' );
 477  
 478              jQuery( '#<?php echo $prefix; ?>_rgn_ID' ).html( options[0] );
 479              jQuery( '#<?php echo $prefix; ?>_subrg_ID' ).html( options[1] );
 480              jQuery( '#<?php echo $prefix; ?>_city_ID' ).html( options[2] );
 481          }
 482      } );
 483  }
 484  
 485  function load_subregions( country_ID, region_ID )
 486  {    // Load option list with sub-regions for seleted region
 487      jQuery( '#<?php echo $prefix; ?>_subrg_ID' ).next().find( 'button' ).hide().next().show();
 488      jQuery.ajax( {
 489      type: 'POST',
 490      url: '<?php echo get_samedomain_htsrv_url(); ?>anon_async.php',
 491      data: 'action=get_subregions_option_list&page=edit&mode=load_all&ctry_id=' + country_ID + '&rgn_id=' + region_ID,
 492      success: function( result )
 493          {
 494              jQuery( '#<?php echo $prefix; ?>_subrg_ID' ).next().find( 'button' ).show().next().hide();
 495  
 496              result = ajax_debug_clear( result );
 497              var options = result.split( '-##-' );
 498  
 499              jQuery( '#<?php echo $prefix; ?>_subrg_ID' ).html( options[0] );
 500              jQuery( '#<?php echo $prefix; ?>_city_ID' ).html( options[1] );
 501          }
 502      } );
 503  }
 504  
 505  function load_cities( country_ID, region_ID, subregion_ID )
 506  {    // Load option list with cities for seleted region or sub-region
 507      jQuery( '#<?php echo $prefix; ?>_city_ID' ).next().find( 'button' ).hide().next().show();
 508      jQuery.ajax( {
 509      type: 'POST',
 510      url: '<?php echo get_samedomain_htsrv_url(); ?>anon_async.php',
 511      data: 'action=get_cities_option_list&page=edit&ctry_id=' + country_ID + '&rgn_id=' + region_ID + '&subrg_id=' + subregion_ID,
 512      success: function( result )
 513          {
 514              jQuery( '#<?php echo $prefix; ?>_city_ID' ).html( ajax_debug_clear( result ) );
 515              jQuery( '#<?php echo $prefix; ?>_city_ID' ).next().find( 'button' ).show().next().hide();
 516          }
 517      } );
 518  }
 519  </script>
 520  <?php
 521  }
 522  
 523  
 524  /**

 525   * Initialize JavaScript for required locations

 526   *

 527   * @param string Prefix of fields group

 528   */
 529  function echo_regional_required_js( $prefix )
 530  {
 531  ?>
 532  <script type="text/javascript">
 533  jQuery( 'input[name=<?php echo $prefix; ?>city][value=required]' ).click( function ()
 534  {    // when city is required make subregion is required
 535      set_subregion_required();
 536  } );
 537  jQuery( 'input[name=<?php echo $prefix; ?>subregion][value=required]' ).click( function ()
 538  {    // when subregion is required make region is required
 539      set_region_required();
 540  } );
 541  jQuery( 'input[name=<?php echo $prefix; ?>region][value=required]' ).click( function ()
 542  {    // when region is required make country is required
 543      set_country_required();
 544  } );
 545  
 546  function set_subregion_required()
 547  {
 548      jQuery( 'input[name=<?php echo $prefix; ?>subregion][value=required]' ).attr( 'checked', 'checked' );
 549      set_region_required();
 550  }
 551  function set_region_required()
 552  {
 553      jQuery( 'input[name=<?php echo $prefix; ?>region][value=required]' ).attr( 'checked', 'checked' );
 554      set_country_required();
 555  }
 556  function set_country_required()
 557  {
 558      jQuery( 'input[name=<?php echo $prefix; ?>country][value=required]' ).attr( 'checked', 'checked' );
 559  }
 560  </script>
 561  <?php
 562  }
 563  
 564  
 565  /**

 566   * Check the existing countries in DB

 567   *

 568   * @param integer Country ID

 569   * @return boolean TRUE - countries exist

 570   */
 571  function countries_exist()
 572  {
 573      global $DB;
 574  
 575      $countries_count = $DB->get_var( '
 576          SELECT COUNT( ctry_ID )
 577            FROM T_regional__country
 578           WHERE ctry_enabled = 1' );
 579  
 580      return $countries_count > 0;
 581  }
 582  
 583  
 584  /**

 585   * Check the existing regions for selected country

 586   *

 587   * @param integer Country ID

 588   * @param boolean TRUE - don't count regions from all countries (when $country_ID = 0)

 589   * @return boolean TRUE - regions exist

 590   */
 591  function regions_exist( $country_ID, $exact_existance = false )
 592  {
 593      global $DB;
 594  
 595      $sql_where = '';
 596      if( $country_ID > 0 )
 597      {    // Restrict regions with selected country
 598          $sql_where = ' AND rgn_ctry_ID = '.$DB->quote( $country_ID );
 599      }
 600      else if( $exact_existance )
 601      {    // Don't count regions with not selected country
 602          return false;
 603      }
 604  
 605      $regions_count = $DB->get_var( '
 606          SELECT COUNT( rgn_ID )
 607            FROM T_regional__region
 608           WHERE rgn_enabled = 1'.$sql_where );
 609  
 610      return $regions_count > 0;
 611  }
 612  
 613  
 614  /**

 615   * Check the existing subregions for selected region

 616   *

 617   * @param integer Region ID

 618   * @param boolean TRUE - don't count subregions from all regions (when $region_ID = 0)

 619   * @return boolean TRUE - subregions exist

 620   */
 621  function subregions_exist( $region_ID, $exact_existance = false )
 622  {
 623      global $DB;
 624  
 625      if( $region_ID == 0 && $exact_existance )
 626      {    // Don't count subregions with not selected region
 627          return false;
 628      }
 629  
 630      $subregions_count = $DB->get_var( '
 631          SELECT COUNT( subrg_ID )
 632            FROM T_regional__subregion
 633           WHERE subrg_enabled = 1
 634             AND subrg_rgn_ID = '.$DB->quote( $region_ID ) );
 635  
 636      return $subregions_count > 0;
 637  }
 638  
 639  
 640  /**

 641   * Check the existing cities for selected country, region & subregion

 642   *

 643   * @param integer City ID

 644   * @param boolean TRUE - don't count cities from all region (when $region_ID = 0)

 645   * @return boolean TRUE - cities exist

 646   */
 647  function cities_exist( $country_ID, $region_ID, $subregion_ID, $exact_existance = false )
 648  {
 649      global $DB;
 650  
 651      $sql_where = array();
 652      $sql_where[] = 'city_enabled = 1';
 653  
 654      if( $country_ID > 0 )
 655      {    // Restrict cities with selected country
 656          $sql_where[] = 'city_ctry_ID = '.$DB->quote( $country_ID );
 657      }
 658      else if( $exact_existance )
 659      {    // Don't count cities with not selected country
 660          return false;
 661      }
 662  
 663      if( $region_ID > 0 )
 664      {    // Restrict cities with selected region
 665          $sql_where[] = 'city_rgn_ID = '.$DB->quote( $region_ID );
 666      }
 667      else if( $exact_existance )
 668      {    // Restrict cities with not defined region
 669          $sql_where[] = 'city_rgn_ID IS NULL';
 670      }
 671  
 672      if( $subregion_ID > 0 )
 673      {    // Restrict cities with selected subregion
 674          $sql_where[] = 'city_subrg_ID = '.$DB->quote( $subregion_ID );
 675      }
 676      else if( $exact_existance )
 677      {    // Restrict cities with not defined subregion
 678          $sql_where[] = 'city_subrg_ID IS NULL';
 679      }
 680  
 681      $cities_count = $DB->get_var( '
 682          SELECT COUNT(city_ID)
 683            FROM T_regional__city
 684           WHERE '.implode( ' AND ', $sql_where ) );
 685  
 686      return $cities_count > 0;
 687  }
 688  
 689  /**

 690   * Template function: Display/Get country flag

 691   *

 692   * @todo factor with locale_flag()

 693   *

 694   * @param string country code to use

 695   * @param string country name to use

 696   * @param string collection name (subdir of img/flags)   !! OLD PARAM - NOT USED IN THE FUNCTION ANYMORE !!

 697   * @param string name of class for IMG tag   !! OLD PARAM - NOT USED IN THE FUNCTION ANYMORE !!

 698   * @param string deprecated HTML align attribute   !! OLD PARAM - NOT USED IN THE FUNCTION ANYMORE !!

 699   * @param boolean to echo or not

 700   * @param mixed use absolute url (===true) or path to flags directory   !! OLD PARAM - NOT USED IN THE FUNCTION ANYMORE !!

 701   * @param string Flag style properties

 702   * @param boolean TRUE - to display even empty flag

 703   * @return string Country flag

 704   */
 705  function country_flag( $country_code, $country_name, $collection = 'w16px', $class = 'flag', $align = '', $disp = true, $absoluteurl = true, $flag_style = '', $display_empty = true )
 706  {
 707      global $country_flags_bg;
 708  
 709      $flag_attribs = array(
 710          'class' => 'flag',
 711          'title' => $country_name,
 712          'style' => $flag_style,
 713      );
 714  
 715      if( isset( $country_flags_bg[ $country_code ] ) )
 716      {    // Set background-position from config
 717          $flag_attribs['style'] .= 'background-position:'.$country_flags_bg[ $country_code ];
 718      }
 719  
 720      if( $display_empty || isset( $country_flags_bg[ $country_code ] ) )
 721      {    // Init a country flag
 722          $r = '<span'.get_field_attribs_as_string( $flag_attribs ).'>&nbsp;</span>';
 723      }
 724      else
 725      {    // Don't display empty flag when bg-position is not defined in config by country code
 726          $r = '';
 727      }
 728  
 729      if( $disp )
 730          echo $r;   // echo it

 731      else
 732          return $r; // return it

 733  
 734  }
 735  
 736  ?>

title

Description

title

Description

title

Description

title

title

Body