b2evolution PHP Cross Reference Blogging Systems

Source: /inc/regional/countries.ctrl.php - 352 lines - 11348 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: countries.ctrl.php 6136 2014-03-08 07:59:48Z manuel $
  31   */
  32  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  33  
  34  // Load Country class (PHP4):
  35  load_class( 'regional/model/_country.class.php', 'Country' );
  36  
  37  /**
  38   * @var User
  39   */
  40  global $current_User;
  41  
  42  // Check minimum permission:
  43  $current_User->check_perm( 'options', 'view', true );
  44  
  45  // Memorize this as the last "tab" used in the Global Settings:
  46  $UserSettings->set( 'pref_glob_settings_tab', $ctrl );
  47  $UserSettings->set( 'pref_glob_regional_tab', $ctrl );
  48  $UserSettings->dbupdate();
  49  
  50  // Set options path:
  51  $AdminUI->set_path( 'options', 'regional', 'countries' );
  52  
  53  // Get action parameter from request:
  54  param_action();
  55  
  56  if( param( 'ctry_ID', 'integer', '', true) )
  57  {// Load country from cache:
  58      $CountryCache = & get_CountryCache();
  59      if( ($edited_Country = & $CountryCache->get_by_ID( $ctry_ID, false )) === false )
  60      {    unset( $edited_Country );
  61          forget_param( 'ctry_ID' );
  62          $Messages->add( sprintf( T_('Requested &laquo;%s&raquo; object does not exist any longer.'), T_('Country') ), 'error' );
  63          $action = 'nil';
  64      }
  65  }
  66  
  67  switch( $action )
  68  {
  69      case 'disable_country':
  70      case 'enable_country':
  71          // Check that this action request is not a CSRF hacked request:
  72          $Session->assert_received_crumb( 'country' );
  73  
  74          // Disable a country only if it is enabled, and user has edit access.
  75          $current_User->check_perm( 'options', 'edit', true );
  76  
  77          // Make sure the country information was loaded. If not, just exit with error.
  78          if( empty($edited_Country) )
  79          {
  80              $Messages->add( sprintf( 'The country with ID %d could not be instantiated.', $ctry_ID ), 'error' );
  81              break;
  82          }
  83  
  84          if ( $action == 'disable_country' )
  85          {    // Disable this country by setting flag to false.
  86              $edited_Country->set( 'enabled', 0 );
  87              $Messages->add( sprintf( T_('Disabled country (%s, #%d).'), $edited_Country->name, $edited_Country->ID ), 'success' );
  88          }
  89          elseif ( $action == 'enable_country' )
  90          {    // Enable country by setting flag to true.
  91              $edited_Country->set( 'enabled', 1 );
  92              $Messages->add( sprintf( T_('Enabled country (%s, #%d).'), $edited_Country->name, $edited_Country->ID ), 'success' );
  93          }
  94  
  95          // Update db with new flag value.
  96          $edited_Country->dbupdate();
  97  
  98          param( 'results_ctry_page', 'integer', '', true );
  99          param( 'results_ctry_order', 'string', '', true );
 100  
 101          // Redirect so that a reload doesn't write to the DB twice:
 102          header_redirect( regenerate_url( '', '', '', '&' ), 303 ); // Will EXIT
 103          // We have EXITed already at this point!!
 104          break;
 105  
 106      case 'enable_country_pref':
 107      case 'disable_country_pref':
 108  
 109          // Check that this action request is not a CSRF hacked request:
 110          $Session->assert_received_crumb( 'country' );
 111  
 112          // Disable a country only if it is enabled, and user has edit access.
 113          $current_User->check_perm( 'options', 'edit', true );
 114  
 115          // Make sure the country information was loaded. If not, just exit with error.
 116          if( empty($edited_Country) )
 117          {
 118              $Messages->add( sprintf( 'The country with ID %d could not be instantiated.', $ctry_ID ), 'error' );
 119              break;
 120          }
 121  
 122          if ( $action == 'disable_country_pref' )
 123          {    // Disable this country by setting flag to false.
 124              $edited_Country->set( 'preferred', 0 );
 125              $Messages->add( sprintf( T_('Removed from preferred countries (%s, #%d).'), $edited_Country->name, $edited_Country->ID ), 'success' );
 126          }
 127          elseif ( $action == 'enable_country_pref' )
 128          {    // Enable country by setting flag to true.
 129              $edited_Country->set( 'preferred', 1 );
 130              $Messages->add( sprintf( T_('Added to preferred countries (%s, #%d).'), $edited_Country->name, $edited_Country->ID ), 'success' );
 131          }
 132  
 133          // Update db with new flag value.
 134          $edited_Country->dbupdate();
 135  
 136          param( 'results_ctry_page', 'integer', '', true );
 137          param( 'results_ctry_order', 'string', '', true );
 138  
 139          // Redirect so that a reload doesn't write to the DB twice:
 140          header_redirect( regenerate_url( '', '', '', '&' ), 303 ); // Will EXIT
 141          // We have EXITed already at this point!!
 142          break;
 143  
 144      case 'new':
 145          // Check permission:
 146          $current_User->check_perm( 'options', 'edit', true );
 147  
 148          if( ! isset($edited_Country) )
 149          {    // We don't have a model to use, start with blank object:
 150              $edited_Country = new Country();
 151          }
 152          else
 153          {    // Duplicate object in order no to mess with the cache:
 154              $edited_Country = duplicate( $edited_Country ); // PHP4/5 abstraction
 155              $edited_Country->ID = 0;
 156          }
 157          break;
 158  
 159      case 'edit':
 160          // Check permission:
 161          $current_User->check_perm( 'options', 'edit', true );
 162  
 163          // Make sure we got an ctry_ID:
 164          param( 'ctry_ID', 'integer', true );
 165           break;
 166  
 167      case 'create': // Record new country
 168      case 'create_new': // Record country and create new
 169      case 'create_copy': // Record country and create similar
 170          // Insert new country:
 171          $edited_Country = new Country();
 172  
 173          // Check that this action request is not a CSRF hacked request:
 174          $Session->assert_received_crumb( 'country' );
 175  
 176          // Check permission:
 177          $current_User->check_perm( 'options', 'edit', true );
 178  
 179          // Load data from request
 180          if( $edited_Country->load_from_Request() )
 181          {    // We could load data from form without errors:
 182  
 183              // Insert in DB:
 184              $DB->begin();
 185              $q = $edited_Country->dbexists();
 186              if($q)
 187              {    // We have a duplicate entry:
 188  
 189                  param_error( 'ctry_code',
 190                      sprintf( T_('This country already exists. Do you want to <a %s>edit the existing country</a>?'),
 191                          'href="?ctrl=countries&amp;action=edit&amp;ctry_ID='.$q.'"' ) );
 192              }
 193              else
 194              {
 195                  $edited_Country->dbinsert();
 196                  $Messages->add( T_('New country created.'), 'success' );
 197              }
 198              $DB->commit();
 199  
 200              if( empty($q) )
 201              {    // What next?
 202  
 203                  switch( $action )
 204                  {
 205                      case 'create_copy':
 206                          // Redirect so that a reload doesn't write to the DB twice:
 207                          header_redirect( '?ctrl=countries&action=new&ctry_ID='.$edited_Country->ID, 303 ); // Will EXIT
 208                          // We have EXITed already at this point!!
 209                          break;
 210                      case 'create_new':
 211                          // Redirect so that a reload doesn't write to the DB twice:
 212                          header_redirect( '?ctrl=countries&action=new', 303 ); // Will EXIT
 213                          // We have EXITed already at this point!!
 214                          break;
 215                      case 'create':
 216                          // Redirect so that a reload doesn't write to the DB twice:
 217                          header_redirect( '?ctrl=countries', 303 ); // Will EXIT
 218                          // We have EXITed already at this point!!
 219                          break;
 220                  }
 221              }
 222          }
 223          break;
 224  
 225      case 'update':
 226          // Edit country form:
 227  
 228          // Check that this action request is not a CSRF hacked request:
 229          $Session->assert_received_crumb( 'country' );
 230  
 231          // Check permission:
 232          $current_User->check_perm( 'options', 'edit', true );
 233  
 234          // Make sure we got an ctry_ID:
 235          param( 'ctry_ID', 'integer', true );
 236  
 237          // load data from request
 238          if( $edited_Country->load_from_Request() )
 239          {    // We could load data from form without errors:
 240  
 241              // Update in DB:
 242              $DB->begin();
 243              $q = $edited_Country->dbexists();
 244              if($q)
 245              {     // We have a duplicate entry:
 246                  param_error( 'ctry_code',
 247                      sprintf( T_('This country already exists. Do you want to <a %s>edit the existing country</a>?'),
 248                          'href="?ctrl=countries&amp;action=edit&amp;ctry_ID='.$q.'"' ) );
 249              }
 250              else
 251              {
 252                  $edited_Country->dbupdate();
 253                  $Messages->add( T_('Country updated.'), 'success' );
 254              }
 255              $DB->commit();
 256  
 257              if( empty($q) )
 258              {    // If no error, Redirect so that a reload doesn't write to the DB twice:
 259                  header_redirect( '?ctrl=countries', 303 ); // Will EXIT
 260                  // We have EXITed already at this point!!
 261              }
 262          }
 263          break;
 264  
 265      case 'delete':
 266          // Delete country:
 267  
 268          // Check that this action request is not a CSRF hacked request:
 269          $Session->assert_received_crumb( 'country' );
 270  
 271          // Check permission:
 272          $current_User->check_perm( 'options', 'edit', true );
 273  
 274          // Make sure we got an ctry_ID:
 275          param( 'ctry_ID', 'integer', true );
 276  
 277          if( param( 'confirm', 'integer', 0 ) )
 278          { // confirmed, Delete from DB:
 279              $msg = sprintf( T_('Country &laquo;%s&raquo; deleted.'), $edited_Country->dget('name') );
 280              $edited_Country->dbdelete( true );
 281              unset( $edited_Country );
 282              forget_param( 'ctry_ID' );
 283              $Messages->add( $msg, 'success' );
 284              // Redirect so that a reload doesn't write to the DB twice:
 285              header_redirect( '?ctrl=countries', 303 ); // Will EXIT
 286              // We have EXITed already at this point!!
 287          }
 288          else
 289          {    // not confirmed, Check for restrictions:
 290              if( ! $edited_Country->check_delete( sprintf( T_('Cannot delete country &laquo;%s&raquo;'), $edited_Country->dget('name') ) ) )
 291              {    // There are restrictions:
 292                  $action = 'view';
 293              }
 294          }
 295          break;
 296  
 297  }
 298  
 299  
 300  $AdminUI->breadcrumbpath_init( false );
 301  $AdminUI->breadcrumbpath_add( T_('System'), '?ctrl=system',
 302          T_('Global settings are shared between all blogs; see Blog settings for more granular settings.') );
 303  $AdminUI->breadcrumbpath_add( T_('Regional settings'), '?ctrl=locales' );
 304  $AdminUI->breadcrumbpath_add( T_('Countries'), '?ctrl=countries' );
 305  
 306  
 307  // Display <html><head>...</head> section! (Note: should be done early if actions do not redirect)
 308  $AdminUI->disp_html_head();
 309  
 310  // Display title, menu, messages, etc. (Note: messages MUST be displayed AFTER the actions)
 311  $AdminUI->disp_body_top();
 312  
 313  $AdminUI->disp_payload_begin();
 314  
 315  /**
 316   * Display payload:
 317   */
 318  switch( $action )
 319  {
 320      case 'nil':
 321          // Do nothing
 322          break;
 323  
 324      case 'delete':
 325          // We need to ask for confirmation:
 326          $edited_Country->confirm_delete(
 327                  sprintf( T_('Delete country &laquo;%s&raquo;?'), $edited_Country->dget('name') ),
 328                  'country', $action, get_memorized( 'action' ) );
 329      case 'new':
 330      case 'create':
 331      case 'create_new':
 332      case 'create_copy':
 333      case 'edit':
 334      case 'update':
 335          $AdminUI->disp_view( 'regional/views/_country.form.php' );
 336          break;
 337  
 338      default:
 339          // No specific request, list all countries:
 340          // Cleanup context:
 341          forget_param( 'ctry_ID' );
 342          // Display country list:
 343          $AdminUI->disp_view( 'regional/views/_country_list.view.php' );
 344          break;
 345  }
 346  
 347  $AdminUI->disp_payload_end();
 348  
 349  // Display body bottom, debug info and close </html>:
 350  $AdminUI->disp_global_footer();
 351  
 352  ?>

title

Description

title

Description

title

Description

title

title

Body