Version: current

     1 <?php
     2 /**
     3  * Used by ModelAdmin scaffolding, to manage many-many relationships.
     4  *
     5  * @package forms
     6  * @subpackage fields-relational
     7  */
     8 class ScaffoldingComplexTableField_Popup extends ComplexTableField_Popup {
     9
    10     public static $allowed_actions = array(
    11         'filter', 'record', 'httpSubmission', 'handleAction', 'handleField'
    12     );
    13
    14     function __construct($controller, $name, $fields, $validator, $readonly, $dataObject) {
    15         $this->dataObject = $dataObject;
    16
    17         Requirements::clear();
    18
    19         $actions = new FieldSet();
    20         if(!$readonly) {
    21             $actions->push(
    22                 $saveAction = new FormAction("saveComplexTableField", "Save")
    23             );
    24             $saveAction->addExtraClass('save');
    25         }
    26
    27         $fields->push(new HiddenField("ComplexTableField_Path", Director::absoluteBaseURL()));
    28
    29         parent::__construct($controller, $name, $fields, $validator, $readonly, $dataObject);
    30     }
    31
    32     /**
    33      * Handle a generic action passed in by the URL mapping.
    34      *
    35      * @param SS_HTTPRequest $request
    36      */
    37     public function handleAction($request) {
    38         $action = str_replace("-","_",$request->param('Action'));
    39         if(!$this->action) $this->action = 'index';
    40
    41         if($this->checkAccessAction($action)) {
    42             if($this->hasMethod($action)) {
    43                 $result = $this->$action($request);
    44
    45                 // Method returns an array, that is used to customise the object before rendering with a template
    46                 if(is_array($result)) {
    47                     return $this->getViewer($action)->process($this->customise($result));
    48
    49                 // Method returns a string / object, in which case we just return that
    50                 } else {
    51                     return $result;
    52                 }
    53
    54             // There is no method, in which case we just render this object using a (possibly alternate) template
    55             } else {
    56                 return $this->getViewer($action)->process($this);
    57             }
    58         } else {
    59             return $this->httpError(403, "Action '$action' isn't allowed on class $this->class");
    60         }
    61     }
    62
    63     /**
    64      * Action to render results for an autocomplete filter.
    65      *
    66      * @param SS_HTTPRequest $request
    67      * @return void
    68      */
    69     function filter($request) {
    70         //$model = singleton($this->modelClass);
    71         $context = $this->dataObject->getDefaultSearchContext();
    72         $value = $request->getVar('q');
    73         $results = $context->getResults(array("Name"=>$value));
    74         header("Content-Type: text/plain");
    75         foreach($results as $result) {
    76             echo $result->Name . "\n";
    77         }
    78     }
    79
    80     /**
    81      * Action to populate edit box with a single data object via Ajax query
    82      */
    83     function record($request) {
    84         $type = $request->getVar('type');
    85         $value = $request->getVar('value');
    86         if ($type && $value) {
    87             $record = DataObject::get_one($this->dataObject->class, "\"$type\" = '$value'");
    88             header("Content-Type: text/plain");
    89             echo json_encode(array("record"=>$record->toMap()));
    90         }
    91     }
    92
    93 }
    94 ?>

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