| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace DNADesign\Elemental\Controllers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use DNADesign\Elemental\Forms\EditFormFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use DNADesign\Elemental\Models\BaseElement; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SilverStripe\Admin\LeftAndMain; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SilverStripe\Control\HTTPRequest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SilverStripe\Control\HTTPResponse_Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use SilverStripe\Core\Injector\Injector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use SilverStripe\Forms\Form; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * Controller for "ElementalArea" - handles loading and saving of in-line edit forms in an elemental area in admin | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class ElementalAreaController extends LeftAndMain | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     private static $url_segment = 'elemental-area'; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     private static $ignore_menuitem = true; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private static $allowed_actions = array( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         'elementForm', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         'schema', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     ); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     public function getClientConfig() | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |         $clientConfig = parent::getClientConfig(); | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         $clientConfig['form']['elementForm'] = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |             'schemaUrl' => $this->Link('schema/elementForm'), | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         return $clientConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param HTTPRequest|null $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @return Form | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @throws HTTPResponse_Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     public function elementForm(HTTPRequest $request = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         // Get ID either from posted back value, or url parameter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         if (!$request) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             $this->jsonError(400); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $id = $request->param('ID'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         if (!$id) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             $this->jsonError(400); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         return $this->getElementForm($id) ?: $this->jsonError(404); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @param int $elementID | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @return Form|null Returns null if no element exists for the given ID | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     public function getElementForm($elementID) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $scaffolder = Injector::inst()->get(EditFormFactory::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $element = BaseElement::get()->byID($elementID); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         if (!$element) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         /** @var Form $form */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $form = $scaffolder->getForm( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             $this, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             'ElementForm_'.$elementID, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             ['Record' => $element] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         return $form; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 78 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |  |