| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of Jitamin. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Copyright (C) Jitamin Team | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Jitamin\Http\Controllers\Manage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Jitamin\Filter\ProjectIdsFilter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Jitamin\Filter\ProjectStatusFilter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Jitamin\Filter\ProjectTypeFilter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Jitamin\Formatter\ProjectGanttFormatter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Jitamin\Http\Controllers\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Jitamin\Model\ProjectModel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * Class ProjectController. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | class ProjectController extends Controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * List of projects. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     public function index() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 31 |  | View Code Duplication |         if ($this->userSession->isAdmin()) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             $project_ids = $this->projectModel->getAllIds(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             $project_ids = $this->projectPermissionModel->getProjectIds($this->userSession->getId()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $nb_projects = count($project_ids); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $paginator = $this->paginator | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             ->setUrl('Manage/ProjectController', 'index') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             ->setMax(20) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             ->setOrder('id') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             ->setDirection('DESC') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             ->setQuery($this->projectModel->getQueryColumnStats($project_ids)) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             ->calculate(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $this->response->html($this->helper->layout->app('manage/projects', [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             'paginator'   => $paginator, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             'nb_projects' => $nb_projects, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             'title'       => t('Projects list'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         ])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * Display Gantt chart for all projects. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |     public function gantt() | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |         $project_ids = $this->projectPermissionModel->getActiveProjectIds($this->userSession->getId()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |         $filter = $this->projectQuery | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |             ->withFilter(new ProjectTypeFilter(ProjectModel::TYPE_TEAM)) | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |             ->withFilter(new ProjectStatusFilter(ProjectModel::ACTIVE)) | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |             ->withFilter(new ProjectIdsFilter($project_ids)); | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |         $filter->getQuery()->asc(ProjectModel::TABLE.'.start_date'); | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |         $this->response->html($this->helper->layout->app('manage/gantt', [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |             'projects' => $filter->format(new ProjectGanttFormatter($this->container)), | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |             'title'    => t('Projects Gantt chart'), | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |         ])); | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 72 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |  | 
            
                        
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.