ade8eb61b5839a503d5413671a329da19bb18657
[phpgitweb.git] / htdocs / pages / projects.class.php
1 <?php
2 /* projects.class.php - phpgitweb
3  * Copyright (C) 2011-2012  Philipp Kreil (pk910)
4  * 
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License 
16  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
17  */
18
19 class page_projects {
20     private $page, $phpgitweb;
21         
22     public function main($phpgitweb, $project) {
23                 $this->phpgitweb = $phpgitweb;
24                 $this->page = new ContentProvider('projects', 'main');
25                 
26                 $project_loader = $phpgitweb->get_project_loader();
27                 $project_counter = 0;
28                 $project_list = $project_loader->getProjectList();
29                 
30                 foreach($project_list as $projectid => &$project) {
31                         //load some additional information
32                         $project['last_change'] = $project_loader->getLastChange($project);
33                 }
34                 unset($project);
35                 
36                 if(array_key_exists('o', $_GET))
37                         $order = $_GET['o'];
38                 else
39                         $order = 'project';
40                 
41                 $this->page->set('header_project',     new ContentProvider('projects', ($order == 'project' ? 'head_order_active' : 'head_order_link'), array('name' => "Project",     'tag' => "project")));
42                 $this->page->set('header_description', new ContentProvider('projects', ($order == 'descr'   ? 'head_order_active' : 'head_order_link'), array('name' => "Description", 'tag' => "descr")));
43                 $this->page->set('header_owner',       new ContentProvider('projects', ($order == 'owner'   ? 'head_order_active' : 'head_order_link'), array('name' => "Owner",       'tag' => "owner")));
44                 $this->page->set('header_age',         new ContentProvider('projects', ($order == 'age'     ? 'head_order_active' : 'head_order_link'), array('name' => "Last Change", 'tag' => "age")));
45                 
46                 switch($order) {
47                 case 'project':
48                         usort($project_list, array($this, "sort_by_project"));
49                         break;
50                 case 'descr':
51                         usort($project_list, array($this, "sort_by_description"));
52                         break;
53                 case 'owner':
54                         usort($project_list, array($this, "sort_by_owner"));
55                         break;
56                 case 'age':
57                         usort($project_list, array($this, "sort_by_age"));
58                         break;
59                 }
60                 
61                 foreach($project_list as $projectid => $project) {
62                         $project_counter++;
63                         $project_entry = $this->project(($project_counter % 2 ? 'dark' : 'light'), $project);
64                         $this->page->append('projects', $project_entry);
65                 }
66                 
67                 return $this->page;
68         }
69         
70         private function sort_by_project($a, $b) {
71                 return strcmp($a['name'], $b['name']);
72         }
73         private function sort_by_description($a, $b) {
74                 $ret = strcmp($a['description'], $b['description']);
75                 if($ret == 0)
76                         $ret = strcmp($a['name'], $b['name']);
77                 return $ret;
78         }
79         private function sort_by_owner($a, $b) {
80                 $ret = strcmp($a['owner'], $b['owner']);
81                 if($ret == 0)
82                         $ret = strcmp($a['name'], $b['name']);
83                 return $ret;
84         }
85         private function sort_by_age($a, $b) {
86                 $ret = $a['last_change'] - $b['last_change'];
87                 if($ret == 0)
88                         $ret = strcmp($a['name'], $b['name']);
89                 return $ret;
90         }
91     
92         private function project($class, $project) {
93                 $entry = new ContentProvider('projects', 'project');
94                 $entry->set('class', $class);
95                 $entry->set('project', $project['name']);
96                 $entry->set('name', htmlentities($project['name']));
97                 $entry->set('description', htmlentities($project['description']));
98                 $entry->set('owner', htmlentities($project['owner']));
99                 
100                 $age = Tools::age_calculate($project['last_change']);
101                 
102                 $entry->set('ageclass', $age['age_class']);
103                 $entry->set('age', $age['age_str']);
104                 return $entry;
105         }
106         
107 }
108
109 ?>