/**
  * Set the container form. If the list is an instance of StatefulGridFieldList and the state is empty it tries to restore the state from the session.
  * @param {Form} $form Form to be used
  */
 public function setForm($form)
 {
     $return = parent::setForm($form);
     if ($this->list instanceof StatefulGridFieldList) {
         $stateValue = $this->state->getData()->toArray();
         if (empty($stateValue) && !empty($this->form)) {
             $this->state->restoreFromSession();
         }
     }
     return $return;
 }
 /**
  * Tests that the appropriate sortable headers are generated
  */
 public function testRenderHeaders()
 {
     // Generate sortable header and extract HTML
     $list = new DataList('GridFieldSortableHeaderTest_Team');
     $config = new GridFieldConfig_RecordEditor();
     $form = new Form(Controller::curr(), 'Form', new FieldList(), new FieldList());
     $gridField = new GridField('testfield', 'testfield', $list, $config);
     $gridField->setForm($form);
     $compontent = $gridField->getConfig()->getComponentByType('GridFieldSortableHeader');
     $htmlFragment = $compontent->getHTMLFragments($gridField);
     // Check that the output shows name and hat as sortable fields, but not city
     $this->assertContains('<span class="non-sortable">City</span>', $htmlFragment['header']);
     $this->assertContains('value="Name" class="action ss-gridfield-sort" id="action_SetOrderName"', $htmlFragment['header']);
     $this->assertContains('value="Cheerleader Hat" class="action ss-gridfield-sort" id="action_SetOrderCheerleader-Hat-Colour"', $htmlFragment['header']);
     // Check inverse of above
     $this->assertNotContains('value="City" class="action ss-gridfield-sort" id="action_SetOrderCity"', $htmlFragment['header']);
     $this->assertNotContains('<span class="non-sortable">Name</span>', $htmlFragment['header']);
     $this->assertNotContains('<span class="non-sortable">Cheerleader Hat</span>', $htmlFragment['header']);
 }
 public function getEditForm($id = null, $fields = null)
 {
     $form = parent::getEditForm($id, $fields);
     $filter = $this->jobQueue->getJobListFilter(null, 300);
     $list = DataList::create('QueuedJobDescriptor');
     $list = $list->where($filter);
     $grid = new GridField('QueuedJobDescriptor', _t('QueuedJobs.JobsFieldTitle', 'Jobs'), $list);
     $grid->setForm($form);
     $form->Fields()->replaceField('QueuedJobDescriptor', $grid);
     $grid->getConfig()->addComponent(new GridFieldQueuedJobExecute());
     $grid->getConfig()->addComponent(new GridFieldQueuedJobExecute('pause', function ($record) {
         return $record->JobStatus == QueuedJob::STATUS_WAIT || $record->JobStatus == QueuedJob::STATUS_RUN;
     }));
     $grid->getConfig()->addComponent(new GridFieldQueuedJobExecute('resume', function ($record) {
         return $record->JobStatus == QueuedJob::STATUS_PAUSED || $record->JobStatus == QueuedJob::STATUS_BROKEN;
     }));
     $grid->getConfig()->addComponent(new GridFieldDeleteAction());
     $formatting = array('Messages' => function ($val, $obj) {
         return "<div style='max-width: 300px; max-height: 200px; overflow: auto;'>{$obj->Messages}</div>";
     });
     $grid->getConfig()->getComponentByType('GridFieldDataColumns')->setFieldFormatting($formatting);
     return $form;
 }
 /**
  * @param int $id
  * @param FieldList $fields
  * @return Form
  */
 public function getEditForm($id = null, $fields = null)
 {
     $form = parent::getEditForm($id, $fields);
     $filter = $this->jobQueue->getJobListFilter(null, 300);
     $list = DataList::create('QueuedJobDescriptor');
     $list = $list->where($filter)->sort('Created', 'DESC');
     $gridFieldConfig = GridFieldConfig_RecordEditor::create()->addComponent(new GridFieldQueuedJobExecute('execute'))->addComponent(new GridFieldQueuedJobExecute('pause', function ($record) {
         return $record->JobStatus == QueuedJob::STATUS_WAIT || $record->JobStatus == QueuedJob::STATUS_RUN;
     }))->addComponent(new GridFieldQueuedJobExecute('resume', function ($record) {
         return $record->JobStatus == QueuedJob::STATUS_PAUSED || $record->JobStatus == QueuedJob::STATUS_BROKEN;
     }))->removeComponentsByType('GridFieldAddNewButton');
     // Set messages to HTML display format
     $formatting = array('Messages' => function ($val, $obj) {
         return "<div style='max-width: 300px; max-height: 200px; overflow: auto;'>{$obj->Messages}</div>";
     });
     $gridFieldConfig->getComponentByType('GridFieldDataColumns')->setFieldFormatting($formatting);
     // Replace gridfield
     $grid = new GridField('QueuedJobDescriptor', _t('QueuedJobs.JobsFieldTitle', 'Jobs'), $list, $gridFieldConfig);
     $grid->setForm($form);
     $form->Fields()->replaceField('QueuedJobDescriptor', $grid);
     if (Permission::check('ADMIN')) {
         $types = ClassInfo::subclassesFor('AbstractQueuedJob');
         $types = array_combine($types, $types);
         unset($types['AbstractQueuedJob']);
         $jobType = DropdownField::create('JobType', _t('QueuedJobs.CREATE_JOB_TYPE', 'Create job of type'), $types);
         $jobType->setEmptyString('(select job to create)');
         $form->Fields()->push($jobType);
         $jobParams = MultiValueTextField::create('JobParams', _t('QueuedJobs.JOB_TYPE_PARAMS', 'Constructor parameters for job creation'));
         $form->Fields()->push($jobParams);
         $form->Fields()->push($dt = DatetimeField::create('JobStart', _t('QueuedJobs.START_JOB_TIME', 'Start job at')));
         $dt->getDateField()->setConfig('showcalendar', true);
         $actions = $form->Actions();
         $actions->push(FormAction::create('createjob', _t('QueuedJobs.CREATE_NEW_JOB', 'Create new job')));
     }
     $this->extend('updateEditForm', $form);
     return $form;
 }