| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Backpack\CRUD\app\Http\Controllers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Backpack\CRUD\app\Http\Controllers\Contracts\CrudControllerContract; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Backpack\CRUD\app\Library\Attributes\DeprecatedIgnoreOnRuntime; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Backpack\CRUD\app\Library\CrudPanel\CrudPanel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Backpack\CRUD\app\Library\CrudPanel\Hooks\Facades\LifecycleHook; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Backpack\CRUD\Backpack; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Illuminate\Foundation\Bus\DispatchesJobs; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Illuminate\Foundation\Validation\ValidatesRequests; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Illuminate\Routing\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Illuminate\Support\Str; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * Class CrudController. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @property-read CrudPanel $crud | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @property array $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | class CrudController extends Controller implements CrudControllerContract | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     use DispatchesJobs, ValidatesRequests; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     //public $crud; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     public $data = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     public $initialized = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     public function __construct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         // --------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         // Create the CrudPanel object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         // --------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         // Used by developers inside their ProductCrudControllers as | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         // $this->crud or using the CRUD facade. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         // | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         // It's done inside a middleware closure in order to have | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         // the complete request inside the CrudPanel object. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $this->middleware(function ($request, $next) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             if (! Backpack::hasCrudController(get_class($this))) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |                 $this->initializeCrud($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                 return $next($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             $this->triggerControllerHooks(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             $this->initialized = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             Backpack::crud($this)->setRequest($request); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             return $next($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     public function initializeCrud($request, $crudPanel = null, $operation = null): CrudPanel | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $crudPanel ??= Backpack::crud($this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         if ($crudPanel->isInitialized()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             $crudPanel->setRequest($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             return $crudPanel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $crudPanel->initialized = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $crudPanel->setRequest($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $this->triggerControllerHooks(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         return $crudPanel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |     private function triggerControllerHooks() | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |         LifecycleHook::trigger('crud:before_setup_defaults', [$this]); | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |         $this->setupDefaults(); | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |         LifecycleHook::trigger('crud:after_setup_defaults', [$this]); | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |         LifecycleHook::trigger('crud:before_setup', [$this]); | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |         $this->setup(); | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |         LifecycleHook::trigger('crud:after_setup', [$this]); | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |         $this->setupConfigurationForCurrentOperation(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * Allow developers to set their configuration options for a CrudPanel. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     public function setup() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * Load routes for all operations. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * Allow developers to load extra routes by creating a method that looks like setupOperationNameRoutes. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * @param  string  $segment  Name of the current entity (singular). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * @param  string  $routeName  Route name prefix (ends with .). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @param  string  $controller  Name of the current controller. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     #[DeprecatedIgnoreOnRuntime('we dont call this method anymore unless you had it overwritten in your CrudController')] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     public function setupRoutes($segment, $routeName, $controller) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         preg_match_all('/(?<=^|;)setup([^;]+?)Routes(;|$)/', implode(';', get_class_methods($this)), $matches); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         if (count($matches[1])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             foreach ($matches[1] as $methodName) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |                 $this->{'setup'.$methodName.'Routes'}($segment, $routeName, $controller); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * Load defaults for all operations. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * Allow developers to insert default settings by creating a method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * that looks like setupOperationNameDefaults. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     protected function setupDefaults() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         preg_match_all('/(?<=^|;)setup([^;]+?)Defaults(;|$)/', implode(';', get_class_methods($this)), $matches); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         if (count($matches[1])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             foreach ($matches[1] as $methodName) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                 $this->{'setup'.$methodName.'Defaults'}(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * Load configurations for the current operation. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      * Allow developers to insert default settings by creating a method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * that looks like setupOperationNameOperation (aka setupXxxOperation). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     protected function setupConfigurationForCurrentOperation(?string $operation = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         $operationName = $operation ?? $this->crud->getCurrentOperation(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         if (! $operationName) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         $setupClassName = 'setup'.Str::studly($operationName).'Operation'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |          * FIRST, run all Operation Closures for this operation. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |          * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |          * It's preferred for this to run closures first, because | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |          * (1) setup() is usually higher in a controller than any other method, so it's more intuitive, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |          * since the first thing you write is the first thing that is being run; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |          * (2) operations use operation closures themselves, inside their setupXxxDefaults(), and | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |          * you'd like the defaults to be applied before anything you write. That way, anything you | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |          * write is done after the default, so you can remove default settings, etc; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         LifecycleHook::trigger($operationName.':before_setup', [$this]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         $this->crud->applyConfigurationFromSettings($operationName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |          * THEN, run the corresponding setupXxxOperation if it exists. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         if (method_exists($this, $setupClassName)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |             $this->{$setupClassName}(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         LifecycleHook::trigger($operationName.':after_setup', [$this]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |     public function __get($name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         if ($name === 'crud') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |             return Backpack::getControllerCrud(get_class($this)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |         return $this->{$name}; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 176 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 177 |  |  |  |