b2evolution PHP Cross Reference Blogging Systems

Source: /inc/items/item_types.ctrl.php - 322 lines - 10134 bytes - Text - Print

Description: This file implements the controller for item types management. 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 the controller for item types management.
   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   *
  10   * {@internal License choice
  11   * - If you have received this file as part of a package, please find the license.txt file in
  12   *   the same folder or the closest folder above for complete license terms.
  13   * - If you have received this file individually (e-g: from http://evocms.cvs.sourceforge.net/)
  14   *   then you must choose one of the following licenses before using the file:
  15   *   - GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php
  16   *   - Mozilla Public License 1.1 (MPL) - http://www.opensource.org/licenses/mozilla1.1.php
  17   * }}
  18   *
  19   * {@internal Open Source relicensing agreement:
  20   * The Evo Factory grants Francois PLANQUE the right to license
  21   * The Evo Factory's contributions to this file and the b2evolution project
  22   * under any OSI approved OSS license (http://www.opensource.org/licenses/).
  23   * }}
  24   *
  25   * @package admin
  26   *
  27   * {@internal Below is a list of authors who have contributed to design/coding of this file: }}
  28   * @author efy-sergey: Evo Factory / Sergey.
  29   * @author fplanque: Francois PLANQUE.
  30   *
  31   * @version $Id: item_types.ctrl.php 6136 2014-03-08 07:59:48Z manuel $
  32   */
  33  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  34  
  35  // Load Itemtype class:
  36  load_class( 'items/model/_itemtype.class.php', 'ItemType' );
  37  
  38  /**
  39   * @var AdminUI
  40   */
  41  global $AdminUI;
  42  
  43  /**
  44   * @var User
  45   */
  46  global $current_User;
  47  
  48  global $dispatcher;
  49  
  50  // get reserved ids
  51  global $reserved_ids;
  52  $reserved_ids = ItemType::get_reserved_ids();
  53  
  54  // Check minimum permission:
  55  $current_User->check_perm( 'options', 'view', true );
  56  
  57  $tab = param( 'tab', 'string', 'settings', true );
  58  
  59  $tab3 = param( 'tab3', 'string', 'types', true );
  60  
  61  /**
  62   * We need make this call to build menu for all modules
  63   */
  64  $AdminUI->set_path( 'items' );
  65  
  66  /*
  67   * Add sub menu entries:
  68   * We do this here instead of _header because we need to include all filter params into regenerate_url()
  69   */
  70  attach_browse_tabs();
  71  
  72  $AdminUI->set_path( 'items', $tab, $tab3 );
  73  
  74  // Get action parameter from request:
  75  param_action();
  76  
  77  if( param( 'ptyp_ID', 'integer', '', true) )
  78  {// Load itemtype from cache:
  79      $ItemtypeCache = & get_ItemTypeCache();
  80      if( ($edited_Itemtype = & $ItemtypeCache->get_by_ID( $ptyp_ID, false )) === false )
  81      {    // We could not find the item type to edit:
  82          unset( $edited_Itemtype );
  83          forget_param( 'ptyp_ID' );
  84          $Messages->add( sprintf( T_('Requested &laquo;%s&raquo; object does not exist any longer.'), 'Itemtype' ), 'error' );
  85          $action = 'nil';
  86      }
  87  }
  88  
  89  switch( $action )
  90  {
  91  
  92      case 'new':
  93          // Check permission:
  94          $current_User->check_perm( 'options', 'edit', true );
  95  
  96          if( ! isset($edited_Itemtype) )
  97          {    // We don't have a model to use, start with blank object:
  98              $edited_Itemtype = new ItemType();
  99          }
 100          else
 101          {    // Duplicate object in order no to mess with the cache:
 102              $edited_Itemtype = duplicate( $edited_Itemtype ); // PHP4/5 abstraction
 103              $edited_Itemtype->ID = 0;
 104          }
 105          break;
 106  
 107      case 'edit':
 108          // Check permission:
 109          $current_User->check_perm( 'options', 'edit', true );
 110  
 111          // Make sure we got an ptyp_ID:
 112          param( 'ptyp_ID', 'integer', true );
 113           break;
 114  
 115       case 'create': // Record new Itemtype
 116      case 'create_new': // Record Itemtype and create new
 117      case 'create_copy': // Record Itemtype and create similar
 118          // Insert new item type...:
 119          
 120          // Check that this action request is not a CSRF hacked request:
 121          $Session->assert_received_crumb( 'itemtype' );
 122          
 123          $edited_Itemtype = new ItemType();
 124  
 125          // Check permission:
 126          $current_User->check_perm( 'options', 'edit', true );
 127  
 128          // load data from request
 129          if( $edited_Itemtype->load_from_Request() )
 130          {    // We could load data from form without errors:
 131  
 132              if( ($edited_Itemtype->ID > $reserved_ids[0]) && ($edited_Itemtype->ID < $reserved_ids[1]) )
 133              { // is reserved item type
 134                  param_error( 'ptyp_ID',
 135                      sprintf( T_('Item types with ID from %d to %d are reserved. Please use another ID.' ), $reserved_ids[0], $reserved_ids[1] ) );
 136              }
 137              else
 138              { // ID is good
 139  
 140                  // While inserting into DB, ID property of Userfield object will be set to autogenerated ID
 141                  // So far as we set ID manualy, we need to preserve this value
 142                  // When assignment of wrong value will be fixed, we can skip this
 143                  $entered_itemtype_id = $edited_Itemtype->ID;
 144  
 145                  // Insert in DB:
 146                  $DB->begin();
 147                  // because of manual assigning ID,
 148                  // member function ItemType::dbexists() is overloaded for proper functionality
 149                  $q = $edited_Itemtype->dbexists();
 150                  if($q)
 151                  {    // We have a duplicate entry:
 152  
 153                      param_error( 'ptyp_ID',
 154                          sprintf( T_('This item type already exists. Do you want to <a %s>edit the existing item type</a>?'),
 155                              'href="?ctrl=itemtypes&amp;tab='.$tab.'&amp;tab3='.$tab3.'&amp;action=edit&amp;ptyp_ID='.$q.'"' ) );
 156                  }
 157                  else
 158                  {
 159                      $edited_Itemtype->dbinsert();
 160                      $Messages->add( T_('New item type created.'), 'success' );
 161                  }
 162                  $DB->commit();
 163  
 164                  if( empty($q) )
 165                  {    // What next?
 166                      switch( $action )
 167                      {
 168                          case 'create_copy':
 169                              // Redirect so that a reload doesn't write to the DB twice:
 170                              header_redirect( '?ctrl=itemtypes&tab='.$tab.'&tab3='.$tab3.'&action=new&ptyp_ID='.$entered_itemtype_id, 303 ); // Will EXIT
 171                              // We have EXITed already at this point!!
 172                              break;
 173                          case 'create_new':
 174                              // Redirect so that a reload doesn't write to the DB twice:
 175                              header_redirect( '?ctrl=itemtypes&tab='.$tab.'&tab3='.$tab3.'&action=new', 303 ); // Will EXIT
 176                              // We have EXITed already at this point!!
 177                              break;
 178                          case 'create':
 179                              // Redirect so that a reload doesn't write to the DB twice:
 180                              header_redirect( '?ctrl=itemtypes&tab='.$tab.'&tab3='.$tab3.'', 303 ); // Will EXIT
 181                              // We have EXITed already at this point!!
 182                              break;
 183                      }
 184                  }
 185              }
 186          }
 187          break;
 188  
 189      case 'update':
 190          // Edit item type form...:
 191  
 192          // Check that this action request is not a CSRF hacked request:
 193          $Session->assert_received_crumb( 'itemtype' );
 194  
 195          // Check permission:
 196          $current_User->check_perm( 'options', 'edit', true );
 197  
 198          // Make sure we got an ptyp_ID:
 199          param( 'ptyp_ID', 'integer', true );
 200  
 201          // load data from request
 202          if( $edited_Itemtype->load_from_Request() )
 203          {    // We could load data from form without errors:
 204  
 205              if( ($edited_Itemtype->ID > $reserved_ids[0]) && ($edited_Itemtype->ID < $reserved_ids[1]) )
 206              { // is reserved item type
 207                  param_error( 'ptyp_ID',
 208                      sprintf( T_('Item types with ID from %d to %d are reserved. You can not edit this item type.' ), $reserved_ids[0], $reserved_ids[1] ) );
 209              }
 210              else
 211              { // ID is good
 212                  // Update in DB:
 213                  $DB->begin();
 214  
 215                  $edited_Itemtype->dbupdate();
 216                  $Messages->add( T_('Item type updated.'), 'success' );
 217  
 218                  $DB->commit();
 219  
 220                  header_redirect( '?ctrl=itemtypes&amp;tab='.$tab.'&amp;tab3='.$tab3.'', 303 ); // Will EXIT
 221                  // We have EXITed already at this point!!
 222              }
 223          }
 224          break;
 225  
 226      case 'delete':
 227          // Delete item type:
 228  
 229          // Check that this action request is not a CSRF hacked request:
 230          $Session->assert_received_crumb( 'itemtype' );
 231  
 232          // Check permission:
 233          $current_User->check_perm( 'options', 'edit', true );
 234  
 235          // Make sure we got an ptyp_ID:
 236          param( 'ptyp_ID', 'integer', true );
 237  
 238          if( ($edited_Itemtype->ID > $reserved_ids[0]) && ($edited_Itemtype->ID < $reserved_ids[1]) )
 239          { // is reserved item type
 240              param_error( 'ptyp_ID',
 241                  sprintf( T_('Item types with ID from %d to %d are reserved. You can not delete this item type' ), $reserved_ids[0], $reserved_ids[1] ) );
 242          }
 243          else
 244          { // ID is good
 245              if( param( 'confirm', 'integer', 0 ) )
 246              { // confirmed, Delete from DB:
 247                  $msg = sprintf( T_('Item type &laquo;%s&raquo; deleted.'), $edited_Itemtype->dget('name') );
 248                  $edited_Itemtype->dbdelete( true );
 249                  unset( $edited_Itemtype );
 250                  forget_param( 'ptyp_ID' );
 251                  $Messages->add( $msg, 'success' );
 252                  // Redirect so that a reload doesn't write to the DB twice:
 253                  header_redirect( '?ctrl=itemtypes&amp;tab='.$tab.'&amp;tab3='.$tab3.'', 303 ); // Will EXIT
 254                  // We have EXITed already at this point!!
 255              }
 256              else
 257              {    // not confirmed, Check for restrictions:
 258                  if( ! $edited_Itemtype->check_delete( sprintf( T_('Cannot delete item type &laquo;%s&raquo;'), $edited_Itemtype->dget('name') ) ) )
 259                  {    // There are restrictions:
 260                      $action = 'view';
 261                  }
 262              }
 263          }
 264          break;
 265  
 266  }
 267  
 268  $AdminUI->breadcrumbpath_init();
 269  $AdminUI->breadcrumbpath_add( T_('Contents'), '?ctrl=items&amp;blog=$blog$&amp;tab=full&amp;filter=restore' );
 270  $AdminUI->breadcrumbpath_add( T_('Settings'), '?ctrl=itemtypes&amp;blog=$blog$&amp;tab=settings' );
 271  $AdminUI->breadcrumbpath_add( T_('Post types'), '?ctrl=itemtypes&amp;blog=$blog$&amp;tab=settings&amp;tab3=types' );
 272  
 273  // Display <html><head>...</head> section! (Note: should be done early if actions do not redirect)
 274  $AdminUI->disp_html_head();
 275  
 276  // Display title, menu, messages, etc. (Note: messages MUST be displayed AFTER the actions)
 277  $AdminUI->disp_body_top();
 278  
 279  $AdminUI->disp_payload_begin();
 280  
 281  /**
 282   * Display payload:
 283   */
 284  switch( $action )
 285  {
 286      case 'nil':
 287          // Do nothing
 288          break;
 289  
 290  
 291      case 'delete':
 292          // We need to ask for confirmation:
 293          $edited_Itemtype->confirm_delete(
 294                  sprintf( T_('Delete item type &laquo;%s&raquo;?'),  $edited_Itemtype->dget('name') ),
 295                  'itemtype', $action, get_memorized( 'action' ) );
 296          /* no break */
 297      case 'new':
 298      case 'create':
 299      case 'create_new':
 300      case 'create_copy':
 301      case 'edit':
 302      case 'update':    // we return in this state after a validation error
 303          $AdminUI->disp_view( 'items/views/_itemtype.form.php' );
 304          break;
 305  
 306  
 307      default:
 308          // No specific request, list all item types:
 309          // Cleanup context:
 310          forget_param( 'ptyp_ID' );
 311          // Display item types list:
 312          $AdminUI->disp_view( 'items/views/_itemtypes.view.php' );
 313          break;
 314  
 315  }
 316  
 317  $AdminUI->disp_payload_end();
 318  
 319  // Display body bottom, debug info and close </html>:
 320  $AdminUI->disp_global_footer();
 321  
 322  ?>

title

Description

title

Description

title

Description

title

title

Body