b2evolution PHP Cross Reference Blogging Systems

Source: /inc/regional/cities.ctrl.php - 381 lines - 12450 bytes - Text - Print

Description: 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 is part of the evoCore framework - {@link http://evocore.net/}

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

   5   *

   6   * @copyright (c)2009-2014 by Francois PLANQUE - {@link http://fplanque.net/}

   7   * Parts of this file are copyright (c)2009 by The Evo Factory - {@link http://www.evofactory.com/}.

   8   *

   9   * {@internal License choice

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

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

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

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

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

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

  16   * }}

  17   *

  18   * {@internal Open Source relicensing agreement:

  19   * The Evo Factory grants Francois PLANQUE the right to license

  20   * The Evo Factory's contributions to this file and the b2evolution project

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

  22   * }}

  23   *

  24   * @package evocore

  25   *

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

  27   * @author efy-maxim: Evo Factory / Maxim.

  28   * @author fplanque: Francois Planque.

  29   *

  30   * @version $Id: cities.ctrl.php 88 2011-10-26 18:45:27Z yura $

  31   */
  32  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  33  
  34  // Load City class (PHP4):

  35  load_class( 'regional/model/_city.class.php', 'City' );
  36  load_funcs( 'regional/model/_regional.funcs.php' );
  37  
  38  /**

  39   * @var User

  40   */
  41  global $current_User;
  42  
  43  // Check minimum permission:

  44  $current_User->check_perm( 'options', 'view', true );
  45  
  46  // Memorize this as the last "tab" used in the Global Settings:

  47  $UserSettings->set( 'pref_glob_settings_tab', $ctrl );
  48  $UserSettings->set( 'pref_glob_regional_tab', $ctrl );
  49  $UserSettings->dbupdate();
  50  
  51  // Set options path:

  52  $AdminUI->set_path( 'options', 'regional', 'cities' );
  53  
  54  // Get action parameter from request:

  55  param_action();
  56  
  57  if( param( 'city_ID', 'integer', '', true) )
  58  {// Load city from cache:
  59      $CityCache = & get_CityCache();
  60      if( ($edited_City = & $CityCache->get_by_ID( $city_ID, false )) === false )
  61      {    unset( $edited_City );
  62          forget_param( 'city_ID' );
  63          $Messages->add( sprintf( T_('Requested &laquo;%s&raquo; object does not exist any longer.'), T_('City') ), 'error' );
  64          $action = 'nil';
  65      }
  66  }
  67  
  68  switch( $action )
  69  {
  70      case 'disable_city':
  71      case 'enable_city':
  72          // Check that this action request is not a CSRF hacked request:

  73          $Session->assert_received_crumb( 'city' );
  74  
  75          // Disable a city only if it is enabled, and user has edit access.

  76          $current_User->check_perm( 'options', 'edit', true );
  77  
  78          // Make sure the city information was loaded. If not, just exit with error.

  79          if( empty($edited_City) )
  80          {
  81              $Messages->add( sprintf( 'The city with ID %d could not be instantiated.', $city_ID ), 'error' );
  82              break;
  83          }
  84  
  85          if ( $action == 'disable_city' )
  86          {    // Disable this city by setting flag to false.
  87              $edited_City->set( 'enabled', 0 );
  88              $Messages->add( sprintf( T_('Disabled city (%s, #%d).'), $edited_City->name, $edited_City->ID ), 'success' );
  89          }
  90          elseif ( $action == 'enable_city' )
  91          {    // Enable city by setting flag to true.
  92              $edited_City->set( 'enabled', 1 );
  93              $Messages->add( sprintf( T_('Enabled city (%s, #%d).'), $edited_City->name, $edited_City->ID ), 'success' );
  94          }
  95  
  96          // Update db with new flag value.

  97          $edited_City->dbupdate();
  98  
  99          param( 'results_city_page', 'integer', '', true );
 100          param( 'results_city_order', 'string', '', true );
 101  
 102          // Redirect so that a reload doesn't write to the DB twice:

 103          header_redirect( regenerate_url( '', '', '', '&' ), 303 ); // Will EXIT

 104          // We have EXITed already at this point!!

 105          break;
 106  
 107      case 'enable_city_pref':
 108      case 'disable_city_pref':
 109  
 110          // Check that this action request is not a CSRF hacked request:

 111          $Session->assert_received_crumb( 'city' );
 112  
 113          // Disable a city only if it is enabled, and user has edit access.

 114          $current_User->check_perm( 'options', 'edit', true );
 115  
 116          // Make sure the city information was loaded. If not, just exit with error.

 117          if( empty($edited_City) )
 118          {
 119              $Messages->add( sprintf( 'The city with ID %d could not be instantiated.', $city_ID ), 'error' );
 120              break;
 121          }
 122  
 123          if ( $action == 'disable_city_pref' )
 124          {    // Disable this city by setting flag to false.
 125              $edited_City->set( 'preferred', 0 );
 126              $Messages->add( sprintf( T_('Removed from preferred cities (%s, #%d).'), $edited_City->name, $edited_City->ID ), 'success' );
 127          }
 128          elseif ( $action == 'enable_city_pref' )
 129          {    // Enable city by setting flag to true.
 130              $edited_City->set( 'preferred', 1 );
 131              $Messages->add( sprintf( T_('Added to preferred cities (%s, #%d).'), $edited_City->name, $edited_City->ID ), 'success' );
 132          }
 133  
 134          // Update db with new flag value.

 135          $edited_City->dbupdate();
 136  
 137          param( 'results_city_page', 'integer', '', true );
 138          param( 'results_city_order', 'string', '', true );
 139  
 140          // Redirect so that a reload doesn't write to the DB twice:

 141          header_redirect( regenerate_url( '', '', '', '&' ), 303 ); // Will EXIT

 142          // We have EXITed already at this point!!

 143          break;
 144  
 145      case 'new':
 146          // Check permission:

 147          $current_User->check_perm( 'options', 'edit', true );
 148  
 149          if( ! isset($edited_City) )
 150          {    // We don't have a model to use, start with blank object:
 151              $edited_City = new City();
 152          }
 153          else
 154          {    // Duplicate object in order no to mess with the cache:
 155              $edited_City = duplicate( $edited_City ); // PHP4/5 abstraction

 156              $edited_City->ID = 0;
 157          }
 158          break;
 159  
 160      case 'csv':
 161          // Check permission:

 162          $current_User->check_perm( 'options', 'edit', true );
 163          break;
 164  
 165      case 'edit':
 166          // Check permission:

 167          $current_User->check_perm( 'options', 'edit', true );
 168  
 169          // Make sure we got an city_ID:

 170          param( 'city_ID', 'integer', true );
 171          break;
 172  
 173      case 'create': // Record new city
 174      case 'create_new': // Record city and create new
 175      case 'create_copy': // Record city and create similar
 176          // Insert new city:

 177          $edited_City = new City();
 178  
 179          // Check that this action request is not a CSRF hacked request:

 180          $Session->assert_received_crumb( 'city' );
 181  
 182          // Check permission:

 183          $current_User->check_perm( 'options', 'edit', true );
 184  
 185          // Load data from request

 186          if( $edited_City->load_from_Request() )
 187          {    // We could load data from form without errors:
 188  
 189              // Insert in DB:

 190              $DB->begin();
 191              $edited_City->dbinsert();
 192              $Messages->add( T_('New city created.'), 'success' );
 193              $DB->commit();
 194  
 195              if( empty($q) )
 196              {    // What next?
 197  
 198                  switch( $action )
 199                  {
 200                      case 'create_copy':
 201                          // Redirect so that a reload doesn't write to the DB twice:

 202                          header_redirect( '?ctrl=cities&action=new&city_ID='.$edited_City->ID, 303 ); // Will EXIT

 203                          // We have EXITed already at this point!!

 204                          break;
 205                      case 'create_new':
 206                          // Redirect so that a reload doesn't write to the DB twice:

 207                          header_redirect( '?ctrl=cities&action=new', 303 ); // Will EXIT

 208                          // We have EXITed already at this point!!

 209                          break;
 210                      case 'create':
 211                          // Redirect so that a reload doesn't write to the DB twice:

 212                          header_redirect( '?ctrl=cities', 303 ); // Will EXIT

 213                          // We have EXITed already at this point!!

 214                          break;
 215                  }
 216              }
 217          }
 218          break;
 219  
 220      case 'update':
 221          // Edit city form:

 222  
 223          // Check that this action request is not a CSRF hacked request:

 224          $Session->assert_received_crumb( 'city' );
 225  
 226          // Check permission:

 227          $current_User->check_perm( 'options', 'edit', true );
 228  
 229          // Make sure we got an city_ID:

 230          param( 'city_ID', 'integer', true );
 231  
 232          // load data from request

 233          if( $edited_City->load_from_Request() )
 234          {    // We could load data from form without errors:
 235  
 236              // Update in DB:

 237              $DB->begin();
 238              $edited_City->dbupdate();
 239              $Messages->add( T_('City updated.'), 'success' );
 240              $DB->commit();
 241  
 242              if( empty($q) )
 243              {    // If no error, Redirect so that a reload doesn't write to the DB twice:
 244                  header_redirect( '?ctrl=cities', 303 ); // Will EXIT

 245                  // We have EXITed already at this point!!

 246              }
 247          }
 248          break;
 249  
 250      case 'delete':
 251          // Delete city:

 252  
 253          // Check that this action request is not a CSRF hacked request:

 254          $Session->assert_received_crumb( 'city' );
 255  
 256          // Check permission:

 257          $current_User->check_perm( 'options', 'edit', true );
 258  
 259          // Make sure we got an city_ID:

 260          param( 'city_ID', 'integer', true );
 261  
 262          if( param( 'confirm', 'integer', 0 ) )
 263          { // confirmed, Delete from DB:
 264              $msg = sprintf( T_('City &laquo;%s&raquo; deleted.'), $edited_City->dget('name') );
 265              $edited_City->dbdelete( true );
 266              unset( $edited_City );
 267              forget_param( 'city_ID' );
 268              $Messages->add( $msg, 'success' );
 269              // Redirect so that a reload doesn't write to the DB twice:

 270              header_redirect( '?ctrl=cities', 303 ); // Will EXIT

 271              // We have EXITed already at this point!!

 272          }
 273          else
 274          {    // not confirmed, Check for restrictions:
 275              if( ! $edited_City->check_delete( sprintf( T_('Cannot delete city &laquo;%s&raquo;'), $edited_City->dget('name') ) ) )
 276              {    // There are restrictions:
 277                  $action = 'view';
 278              }
 279          }
 280          break;
 281  
 282      case 'import': // Import new cities
 283          // Check that this action request is not a CSRF hacked request:

 284          $Session->assert_received_crumb( 'city' );
 285  
 286          // Check permission:

 287          $current_User->check_perm( 'options', 'edit', true );
 288  
 289          // Country Id

 290          param( 'ctry_ID', 'integer', true );
 291          param_check_number( 'ctry_ID', T_('Please select a country'), true );
 292  
 293          // CSV File

 294          $csv = $_FILES['csv'];
 295          if( $csv['size'] == 0 )
 296          {    // File is empty
 297              $Messages->add( T_('Please select a CSV file to import.'), 'error' );
 298          }
 299          else if( !preg_match( '/\.csv$/i', $csv['name'] ) )
 300          {    // Extension is incorrect
 301              $Messages->add( sprintf( T_('&laquo;%s&raquo; has an unrecognized extension.'), $csv['name'] ), 'error' );
 302          }
 303  
 304          if( param_errors_detected() )
 305          {    // Some errors are exist, Stop the importing
 306              $action = 'csv';
 307              break;
 308          }
 309  
 310          // Import a new cities from CSV file

 311          $count_cities = import_cities( $ctry_ID, $csv['tmp_name'] );
 312  
 313          load_class( 'regional/model/_country.class.php', 'Country' );
 314          $CountryCache = & get_CountryCache();
 315          $Country = $CountryCache->get_by_ID( $ctry_ID );
 316  
 317          $Messages->add( sprintf( T_('%s cities added and %s cities updated for country %s.'), $count_cities['inserted'], $count_cities['updated'], $Country->get_name() ), 'success' );
 318          // Redirect so that a reload doesn't write to the DB twice:

 319          header_redirect( '?ctrl=cities', 303 ); // Will EXIT

 320          break;
 321  
 322  }
 323  
 324  
 325  $AdminUI->breadcrumbpath_init( false );
 326  $AdminUI->breadcrumbpath_add( T_('System'), '?ctrl=system',
 327          T_('Global settings are shared between all blogs; see Blog settings for more granular settings.') );
 328  $AdminUI->breadcrumbpath_add( T_('Regional settings'), '?ctrl=locales' );
 329  $AdminUI->breadcrumbpath_add( T_('Cities'), '?ctrl=cities' );
 330  
 331  
 332  // Display <html><head>...</head> section! (Note: should be done early if actions do not redirect)

 333  $AdminUI->disp_html_head();
 334  
 335  // Display title, menu, messages, etc. (Note: messages MUST be displayed AFTER the actions)

 336  $AdminUI->disp_body_top();
 337  
 338  $AdminUI->disp_payload_begin();
 339  
 340  /**

 341   * Display payload:

 342   */
 343  switch( $action )
 344  {
 345      case 'nil':
 346          // Do nothing

 347          break;
 348  
 349      case 'delete':
 350          // We need to ask for confirmation:

 351          $edited_City->confirm_delete(
 352                  sprintf( T_('Delete city &laquo;%s&raquo;?'), $edited_City->dget('name') ),
 353                  'city', $action, get_memorized( 'action' ) );
 354      case 'new':
 355      case 'create':
 356      case 'create_new':
 357      case 'create_copy':
 358      case 'edit':
 359      case 'update':
 360          $AdminUI->disp_view( 'regional/views/_city.form.php' );
 361          break;
 362  
 363      case 'csv':
 364          $AdminUI->disp_view( 'regional/views/_city_import.form.php' );
 365          break;
 366  
 367      default:
 368          // No specific request, list all cities:

 369          // Cleanup context:

 370          forget_param( 'city_ID' );
 371          // Display cities list:

 372          $AdminUI->disp_view( 'regional/views/_city_list.view.php' );
 373          break;
 374  }
 375  
 376  $AdminUI->disp_payload_end();
 377  
 378  // Display body bottom, debug info and close </html>:

 379  $AdminUI->disp_global_footer();
 380  
 381  ?>

title

Description

title

Description

title

Description

title

title

Body