| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Backpack\PageManager\app\Http\Controllers\Admin; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use App\UniquePages; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Backpack\PageManager\app\TraitReflections; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Backpack\CRUD\app\Http\Controllers\CrudController; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Backpack\CRUD\app\Http\Controllers\CrudFeatures\SaveActions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class UniquePageCrudController extends CrudController | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     use SaveActions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     use UniquePages; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     use TraitReflections; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     public function setup() | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |         parent::__construct(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |         $modelClass = config('backpack.pagemanager.unique_page_model_class', 'Backpack\PageManager\app\Models\Page'); | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |         $this->checkForTemplatesAndUniquePagesNotDistinct(); | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |         /* | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |         |-------------------------------------------------------------------------- | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |         | BASIC CRUD INFORMATION | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |         |-------------------------------------------------------------------------- | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |         */ | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |         $this->crud->setModel($modelClass); | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         // don't set route or entity names here. these depend on the page you are editing | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         // unique pages can not be created nor deleted | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         $this->crud->denyAccess('create'); | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         $this->crud->denyAccess('delete'); | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         if (config('backpack.pagemanager.unique_page_revisions')) { | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |             $this->crud->allowAccess('revisions'); | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * As we want to edit pages by slug we need a new edit function. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param string $slug the page slug | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @return Response | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     public function uniqueEdit($slug) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $model = $this->crud->model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $entry = $model::findBySlug($slug); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         if (! $entry) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             $entry = $this->createMissingPage($slug); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $this->uniqueSetup($entry); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         return parent::edit($entry->id); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     public function update($slug, $id) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $this->setRoute($slug); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         return parent::updateCrud(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     public function setRoute($slug) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $this->crud->setRoute(config('backpack.base.route_prefix').'/unique/'.$slug); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * Populate the update form with basic fields, that all pages need. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @param Model $page the page entity | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     public function addDefaultPageFields($page) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $this->crud->addField([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             'name' => 'template', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             'type' => 'hidden', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $this->crud->addField([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             'name' => 'name', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             'type' => 'hidden', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         $this->crud->addField([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             'name' => 'title', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             'type' => 'hidden', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $this->crud->addField([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             'name' => 'slug', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             'type' => 'hidden', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $this->crud->addField([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             'name' => 'open', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             'type' => 'custom_html', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             'value' => $this->buttons($page), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     public function buttons($page) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $openButton = $page->getOpenButton(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $revisionsButton = view('crud::buttons.revisions', ['crud' => $this->crud, 'entry' => $page]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         return $openButton.' '.$revisionsButton; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     public function createMissingPage($slug) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         $pages = collect($this->getUniquePages()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         $slugs = $pages->mapWithKeys(function ($page) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             return [str_slug($page->name) => $page->name]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         if (! $page = $slugs->pull($slug)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             abort(404); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $model = $this->crud->model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         return $model::create([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             'template' => $page, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             'name' => camel_case($page), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             'title' => camel_case($page), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             'slug' => $slug, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 134 |  | View Code Duplication |     public function uniqueRevisions($slug, $id) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         $model = $this->crud->model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         $entry = $model::findBySlugOrFail($slug); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         $this->uniqueSetup($entry); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         return parent::listRevisions($entry->id); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 144 |  | View Code Duplication |     public function restoreUniqueRevision($slug, $id) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         $model = $this->crud->model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         $entry = $model::findBySlugOrFail($slug); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         $this->uniqueSetup($entry); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         return parent::restoreRevision($id); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     protected function uniqueSetup($entry) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         $this->setRoute($entry->slug); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         $this->addDefaultPageFields($entry); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         $this->crud->setEntityNameStrings($this->crud->makeLabel($entry->template), ''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         $this->{$entry->template}(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     |-------------------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |     | SaveActions overrides | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     |-------------------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |     */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |      * Overrides trait version to remove. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |     public function getSaveAction() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |         $saveCurrent = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |             'value' => $this->getSaveActionButtonName('save_and_back'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |             'label' => $this->getSaveActionButtonName('save_and_back'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |         return [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |             'active' => $saveCurrent, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |             'options' => [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |     public function setSaveAction($forceSaveAction = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |         // do nothing to preserve session value for other crud | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 190 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 191 |  |  |  | 
            
                        
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()method in theSoncalls the wrong method in the parent class.