Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //First File TestimonialAdmin
- class TestimonialAdmin extends ModelAdmin
- public function getEditForm($id = null, $fields = null)
- {
- $form = parent::getEditForm($id, $fields);
- $gridField = $form->Fields()->fieldByName($this->sanitiseClassName($this->modelClass));
- if(!sizeof($gridField->getList())){
- $t = new Testimonial();
- $t->write();
- }
- $config = $gridField->getConfig();
- $config->addComponent(new GridFieldSaveBox("Testimonial Saved"));
- $config->addComponent(new GridFieldAddBox("Testimonial","Testimonial Added"));
- $config->addComponent(new GridFieldDeleteAction());
- $gridField->setConfig($config);
- return $form;
- }
- //Second file GridFieldSaveBox
- <?php
- class GridFieldSaveBox implements GridField_ColumnProvider, GridField_ActionProvider
- {
- private $successmsg;
- public function __construct($msg=null)
- {
- $this->successmsg = $msg;
- }
- public function augmentColumns($field, &$cols)
- {
- if(!in_array('Save', $cols)) $cols[] = 'Save';
- }
- public function getColumnsHandled($field)
- {
- return array('Save');
- }
- public function getColumnContent($gridfield, $record, $col)
- {
- if($record->canEdit()) {
- $field = GridField_FormAction::create(
- $gridfield,
- "Save",
- "Save",
- 'saverecords',
- array(
- 'record' => $record,
- )
- );
- return $field->Field();
- }
- }
- public function getColumnAttributes($field, $record, $col)
- {
- return array('class' => 'col-' . preg_replace('/[^\w]/', '-', $col));
- }
- public function getColumnMetadata($gridfield, $col)
- {
- return array('title' => 'Save');
- }
- public function getActions($gridfield)
- {
- return array('saverecords');
- }
- public function handleAction(GridField $gridfield, $actionName, $arguments, $data)
- {
- if($actionName == 'saverecords') {
- $msg = '';
- $cn = $arguments['record']->ClassName;
- $dos = DataObject::get($cn);
- foreach($dos as $do){
- foreach($data['returns'.$do->ID] as $d => $k)
- $do->setField($d,$k);
- $do->write();
- }
- if($this->successmsg){
- $msg = $this->successmsg;
- }
- else {
- $msg = "Object successfully saved.";
- }
- Controller::curr()->getResponse()->setStatusCode(
- 200,
- $msg
- );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement