| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace App\Controller\Admin; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use App\Controller\AppController; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Cake\Cache\Cache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Cake\Core\Configure; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Cake\Event\Event; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Cake\Network\Exception\NotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class SettingsController extends AppController | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      * Display all settings. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 18 |  | View Code Duplication |     public function index() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         $this->paginate = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |             'maxLimit' => 20 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $settings = $this->Settings | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |             ->find() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             ->contain([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |                 'LastUpdatedUser' => function ($q) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |                     return $q->find('medium'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             ]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             ->order([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |                 'Settings.name' => 'ASC' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $settings = $this->paginate($settings); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $this->set(compact('settings')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * Create a setting. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @return \Cake\Network\Response|void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     public function create() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $setting = $this->Settings->newEntity($this->request->data); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         if ($this->request->is('post')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             $setting->last_updated_user_id = $this->Auth->user('id'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 52 |  | View Code Duplication |             if ($this->Settings->save($setting)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 Cache::delete('settings', 'database'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |                 $this->Flash->success(__d('admin', 'This setting has been created successfully !')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |                 return $this->redirect(['action' => 'index']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $this->set(compact('setting')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * Edit a setting. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @return \Cake\Network\Response|void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     public function edit() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $setting = $this->Settings | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             ->find() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             ->where([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 'Settings.id' => $this->request->id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             ]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             ->first(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 77 |  | View Code Duplication |         if (is_null($setting)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             $this->Flash->error(__d('admin', 'This setting doesn\'t exist or has been deleted.')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             return $this->redirect(['action' => 'index']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         if ($this->request->is(['post', 'put'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             $this->Settings->patchEntity($setting, $this->request->data); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             $setting->last_updated_user_id = $this->Auth->user('id'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 88 |  | View Code Duplication |             if ($this->Settings->save($setting)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                 Cache::delete('settings', 'database'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                 $this->Flash->success(__d('admin', 'This setting has been edited successfully !')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                 return $this->redirect(['action' => 'index']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $this->set(compact('setting')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * Delete a setting. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @return \Cake\Network\Response|void | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 103 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |     public function delete() | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |         $setting = $this->Settings | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 107 |  |  |             ->find() | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |             ->where([ | 
            
                                                        
            
                                    
            
            
                | 109 |  |  |                 'Settings.id' => $this->request->id | 
            
                                                        
            
                                    
            
            
                | 110 |  |  |             ]) | 
            
                                                        
            
                                    
            
            
                | 111 |  |  |             ->first(); | 
            
                                                        
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                        
            
                                                                    
                                                                                                        
            
            
                | 113 |  | View Code Duplication |         if (is_null($setting)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 114 |  |  |             $this->Flash->error(__d('admin', 'This setting doesn\'t exist or has been deleted.')); | 
            
                                                        
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 116 |  |  |             return $this->redirect(['action' => 'index']); | 
            
                                                        
            
                                    
            
            
                | 117 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                        
            
                                                                    
                                                                                                        
            
            
                | 119 |  | View Code Duplication |         if ($this->Settings->delete($setting)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 120 |  |  |             $this->Flash->success(__d('admin', 'This setting has been deleted successfully !')); | 
            
                                                        
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 122 |  |  |             return $this->redirect(['action' => 'index']); | 
            
                                                        
            
                                    
            
            
                | 123 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 125 |  |  |         return $this->redirect(['action' => 'index']); | 
            
                                                        
            
                                    
            
            
                | 126 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 127 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 128 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.