Version: current

     1 <?php
     2
     3 /**
     4  *
     5  *
     6  *
     7  * @package cart_shipping
     8  * @author menedem, dvp
     9  */
    10 class ShippingMethodAdmin extends ModelAdmin {
    11
    12     static $url_segment = "shipping";
    13     static $menu_title = "Shipping";
    14     static $menu_priority = 0;
    15
    16     static $managed_models = array(
    17         'ShippingMethod' => array(
    18             'collection_controller' => 'ShippingMethodAdmin_CollectionController'
    19         )
    20     );
    21
    22     public static $model_importers = array();
    23 }
    24
    25 class ShippingMethodAdmin_CollectionController extends ModelAdmin_CollectionController {
    26
    27     function CreateForm(){
    28         $form = parent::CreateForm();
    29
    30         $options = array();
    31         foreach (ShippingMethod::get_supported_methods() as $f) {
    32             $options[$f] = singleton($f)->i18n_singular_name();
    33         }
    34         $form->Fields()->push(new DropdownField("CustomModelClass", _t('ShippingMethodAdmin.Type', 'Type'), $options));
    35         return $form;
    36     }
    37
    38     function __construct($parent, $model){
    39         parent::__construct($parent, $model);
    40         $this->backupModelClass = $this->modelClass;
    41     }
    42
    43     function add($request){
    44         $this->storeCustomModelClass();
    45         return parent::add($request);
    46     }
    47
    48     //pass selected model class into form
    49     function AddForm(){
    50         if ($customclass = $this->retrieveCustomModelClass()) {
    51             $this->modelClass = $customclass;
    52         }
    53         $form = parent::AddForm();
    54         $this->modelClass = $this->backupModelClass;
    55         return $form;
    56     }
    57
    58     function doCreate($data, $form, $request){
    59         if ($customclass = $this->retrieveCustomModelClass()) {
    60             $this->modelClass = $customclass;
    61         }
    62         $output = parent::doCreate($data, $form, $request);
    63         $this->clearCustomModelClass();
    64         $this->modelClass = $this->backupModelClass;
    65         return $output;
    66     }
    67
    68     protected function storeCustomModelClass(){
    69         if (isset($_REQUEST['CustomModelClass']) && ShippingMethod::method_is_supported($_REQUEST['CustomModelClass'])) {
    70             Session::set($this->class.".CustomModelClass", $_REQUEST['CustomModelClass']);
    71         }
    72     }
    73     protected function retrieveCustomModelClass(){
    74         return Session::get($this->class.".CustomModelClass");
    75     }
    76     protected function clearCustomModelClass(){
    77         Session::clear($this->class.".CustomModelClass");
    78     }
    79 }

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -