Version: current

     1 <?php
     2
     3 class PaymentAdmin extends ModelAdmin{
     4
     5     static $url_segment = 'payments';
     6     static $menu_title = 'Payments';
     7     static $menu_priority = 0;
     8
     9     static $managed_models = array(
    10         'PaymentMethod' => array(
    11             'collection_controller' => 'PaymentAdmin_CollectionController'
    12         )
    13     );
    14
    15     public static $model_importers = array();
    16
    17 }
    18
    19 class PaymentAdmin_CollectionController extends ModelAdmin_CollectionController{
    20
    21     function CreateForm(){
    22         $form = parent::CreateForm();
    23         $options = ClassInfo::subclassesFor("PaymentMethod");
    24         unset($options[0]);
    25         foreach ($options as $f) {
    26             if (singleton($f)->canCreate()) {
    27                 $options[$f] = singleton($f)->i18n_singular_name();
    28             } else {
    29                 unset($options[$f]);
    30             }
    31         }
    32         $form->Fields()->push(new DropdownField("CustomModelClass", _t('PaymentMethod.PaymentTypeName', 'Type'), $options));
    33         return $form;
    34     }
    35
    36     //hacks to allow creating a different class of model within the same collection controller
    37         //get correct CMSFields -> AddForm
    38         //create correct model -> doCreate
    39
    40     function __construct($parent, $model){
    41         parent::__construct($parent, $model);
    42         $this->backupModelClass = $this->modelClass;
    43     }
    44
    45     function add($request){
    46         $this->storeCustomModelClass();
    47         return parent::add($request);
    48     }
    49
    50     //pass selected model class into form
    51     function AddForm(){
    52         if($customclass = $this->retrieveCustomModelClass()){
    53             $this->modelClass = $customclass;
    54         }
    55         $form = parent::AddForm();
    56         $this->modelClass = $this->backupModelClass;
    57         return $form;
    58     }
    59
    60     function doCreate($data, $form, $request){
    61         if($customclass = $this->retrieveCustomModelClass()){
    62             $this->modelClass = $customclass;
    63         }
    64         $output = parent::doCreate($data, $form, $request);
    65         $this->clearCustomModelClass();
    66         return $output;
    67     }
    68
    69     protected function storeCustomModelClass(){
    70         if(isset($_REQUEST['CustomModelClass']) && ClassInfo::is_subclass_of($_REQUEST['CustomModelClass'], $this->modelClass)){
    71             Session::set($this->class.".CustomModelClass",$_REQUEST['CustomModelClass']);
    72         }
    73     }
    74     protected function retrieveCustomModelClass(){
    75         return Session::get($this->class.".CustomModelClass");
    76     }
    77     protected function clearCustomModelClass(){
    78         Session::clear($this->class.".CustomModelClass");
    79         $this->modelClass = $this->backupModelClass;
    80     }
    81
    82 }
    83
    84

[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. -