Advertisement
Guest User

modeladmin example

a guest
Mar 14th, 2014
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.32 KB | None | 0 0
  1. //First File TestimonialAdmin
  2. class TestimonialAdmin extends ModelAdmin
  3.  
  4. public function getEditForm($id = null, $fields = null)
  5.     {
  6.        
  7.         $form = parent::getEditForm($id, $fields);
  8.         $gridField = $form->Fields()->fieldByName($this->sanitiseClassName($this->modelClass));
  9.             if(!sizeof($gridField->getList())){
  10.                 $t = new Testimonial();
  11.                 $t->write();
  12.             }
  13.         $config = $gridField->getConfig(); 
  14.         $config->addComponent(new GridFieldSaveBox("Testimonial Saved"));
  15.         $config->addComponent(new GridFieldAddBox("Testimonial","Testimonial Added"));
  16.         $config->addComponent(new GridFieldDeleteAction());
  17.  
  18.  
  19.              $gridField->setConfig($config);
  20.              return $form;
  21.  
  22.     }
  23.  
  24. //Second file GridFieldSaveBox
  25.  
  26. <?php
  27.  
  28. class GridFieldSaveBox implements GridField_ColumnProvider, GridField_ActionProvider
  29. {
  30.  
  31.     private $successmsg;
  32.  
  33.     public function __construct($msg=null)
  34.     {
  35.         $this->successmsg = $msg;
  36.     }
  37.  
  38.     public function augmentColumns($field, &$cols)
  39.     {
  40.         if(!in_array('Save', $cols)) $cols[] = 'Save';
  41.     }
  42.  
  43.     public function getColumnsHandled($field)
  44.     {
  45.         return array('Save');
  46.     }
  47.  
  48.     public function getColumnContent($gridfield, $record, $col)
  49.     {
  50.         if($record->canEdit()) {
  51.             $field = GridField_FormAction::create(
  52.                 $gridfield,
  53.                 "Save",
  54.                 "Save",
  55.                 'saverecords',
  56.                 array(
  57.                     'record' => $record,
  58.                 )
  59.             );
  60.             return $field->Field();
  61.         }
  62.     }
  63.  
  64.     public function getColumnAttributes($field, $record, $col)
  65.     {
  66.         return array('class' => 'col-' . preg_replace('/[^\w]/', '-', $col));
  67.     }
  68.  
  69.     public function getColumnMetadata($gridfield, $col)
  70.     {
  71.         return array('title' => 'Save');
  72.     }
  73.  
  74.     public function getActions($gridfield)
  75.     {
  76.         return array('saverecords');
  77.     }
  78.  
  79.     public function handleAction(GridField $gridfield, $actionName, $arguments, $data)
  80.     {
  81.         if($actionName == 'saverecords') {
  82.                 $msg = '';
  83.                 $cn = $arguments['record']->ClassName;
  84.                 $dos = DataObject::get($cn);
  85.                 foreach($dos as $do){
  86.                 foreach($data['returns'.$do->ID] as $d => $k)
  87.                     $do->setField($d,$k);
  88.                     $do->write();
  89.                 }
  90.             if($this->successmsg){
  91.                 $msg = $this->successmsg;
  92.             }
  93.             else {
  94.                 $msg = "Object successfully saved.";
  95.             }
  96.             Controller::curr()->getResponse()->setStatusCode(
  97.                 200,
  98.                 $msg
  99.             );
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement