| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SilverStripe\GridFieldAddOns; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use SilverStripe\Forms\Form; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use SilverStripe\Forms\FieldList; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SilverStripe\Forms\FormAction; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SilverStripe\Control\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SilverStripe\Control\RequestHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use SilverStripe\ORM\ValidationException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use SilverStripe\Control\PjaxResponseNegotiator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use SilverStripe\GridFieldAddOns\GridFieldExpandableForm; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class GridFieldExpandableForm_ItemRequest extends RequestHandler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     private static $url_handlers = array( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         '$Action!' => '$Action', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         '' => 'edit', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private static $allowed_actions = array( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         'edit', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         'ExpandableForm' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     protected $gridfield; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     protected $component; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     protected $record; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     protected $controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     protected $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     protected $formorfields; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     protected $template = GridFieldExpandableForm::class; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     public function __construct($gridfield, $component, $record, $controller, $name, $formorfields) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $this->gridfield = $gridfield; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $this->component = $component; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $this->record = $record; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $this->controller = $controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $this->name = $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $this->formorfields = $formorfields; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         parent::__construct(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     public function edit($request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $form = $this->ExpandableForm($this->gridField, $request); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             ->customise(['ExpandableForm' => $form]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             ->renderWith($this->template); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * Generate a form to allow editing of a reord | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @return \SilverStripe\Forms\Form | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     public function ExpandableForm() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $record = $this->record; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         if (!$record->canView()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             $controller = $this->getToplevelController(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             return $controller->httpError(403); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         if ($this->formorfields instanceof FieldList) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             $fields = $this->formorfields; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } elseif ($this->formorfields instanceof ViewableData) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             $form = $this->formorfields; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } elseif ($this->record->hasMethod('getExandableForm')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             $form = $this->record->getExandableForm($this, __FUNCTION__); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             $this->record->extend('updateExandableForm', $form); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         } elseif ($this->record->hasMethod('getExandableFormFields')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             $fields = $this->record->getExandableFormFields(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             $this->record->extend('updateExandableFormFields', $fields); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             $fields = $this->record->scaffoldFormFields(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             $this->record->extend('updateExandableFormFields', $fields); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         if (empty($form)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             $actions = new FieldList(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             $actions->push( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 FormAction::create('doSave', _t('GridFieldDetailForm.Save', 'Save')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                     ->setUseButtonTag(true) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                     ->addExtraClass('ss-ui-action-constructive btn-primary font-icon-save') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                     ->setAttribute('data-icon', 'accept') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                     ->setAttribute('data-action-type', 'default') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             $form = new Form( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                 $this, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 'ExpandableForm', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                 $fields, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 $actions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         if ($this->validator) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             $form->setValidator($this->validator); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $form->loadDataFrom($this->record, Form::MERGE_DEFAULT); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         $form->IncludeFormTag = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         // Ensure form is made readonly if editing not allowed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         if (!$record->canEdit()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             $form->makeReadonly(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         return $form; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     public function doSave($data, $form) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         // Check permission | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         if (!$this->record->canEdit()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             $controller = $this->getToplevelController(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             return $controller->httpError(403); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             $form->saveInto($this->record); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             $this->record->write(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             $list = $this->gridfield->getList(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             if ($list instanceof ManyManyList) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |                 $extradata = array_intersect_key($data, $list->getField('extraFields')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |                 $list->add($this->record, $extradata); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |                 $list->add($this->record); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         } catch (ValidationException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |             $form->sessionMessage($e->getResult()->message(), 'bad'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             $responseNegotiator = new PjaxResponseNegotiator(array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |                 'CurrentForm' => function () use (&$form) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |                     return $form->forTemplate(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |                 }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |                 'default' => function () use (&$controller) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |                     return $controller->redirectBack(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |             )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |             if ($controller->getRequest()->isAjax()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |                 $controller->getRequest()->addHeader('X-Pjax', 'CurrentForm'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             return $responseNegotiator->respond($controller->getRequest()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         return $this->customise(array('ExpandableForm' => $form))->renderWith($this->template); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     public function doDelete($data, $form) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |             if (!$this->record->canDelete()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |                 throw new ValidationException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |                     _t('GridFieldDetailForm.DeletePermissionsFailure', "No delete permissions"), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |                     0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |             $this->record->delete(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         } catch (ValidationException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |             $form->sessionMessage($e->getResult()->message(), 'bad'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |             return Controller::curr()->redirectBack(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |         return 'deleted'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 171 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 172 |  |  |     protected function getToplevelController() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 173 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 174 |  |  |         $c = $this->popupController; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 175 |  |  |         while ($c && $c instanceof GridFieldExpandableForm_ItemRequest) { | 
            
                                                                        
                            
            
                                    
            
            
                | 176 |  |  |             $c = $c->getController(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 177 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 178 |  |  |         return $c; | 
            
                                                                        
                            
            
                                    
            
            
                | 179 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |     public function Link($action = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |         return Controller::join_links( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |             $this->gridfield->Link('expand'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |             $this->record->ID ? $this->record->ID : 'new', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |             $action | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 189 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 190 |  |  |  |