replacing the cli cmds with variables and configuration in config.php
[gwvp-mini.git] / gwvpmini / gwvpmini_activity.php
1 <?php
2 global $HOME_PAGE_PROVIDERS;\r
3 \r
4 \r
5 $HOME_PAGE_PROVIDERS["10activity"] = "gwvpmini_HomePageActivityLog";\r
6
7
8 if($IS_WEB_REQUEST) {
9         $reg = gwvpmini_getConfigVal("activityloglength");\r
10         \r
11         $activity_log_length = false;\r
12         if($reg == null) {\r
13                 gwvpmini_setConfigVal("activityloglength", "100");\r
14         } else if($reg == 1) {\r
15                 $activityloglength = true;\r
16         } else {\r
17                 $activityloglength = false;\r
18         }
19         
20         global $activity_log_length;\r
21 }       \r
22
23
24 // this will add a repo activity (a commit for eg)
25 // and auto populate the field in the activity
26 // log
27 function gwvpmini_AddCommitActivityForRepo($reponame, $byusername, $commitid, $desc, $branch)
28 {
29         // gwvpmini_AddActivityLog($type, $userid, $repoid, $commitid, $commitlog, $visibleto="a")
30         $rid = gwvpmini_GetRepoId($reponame);
31         $uid = gwvpmini_GetUserId($byusername);
32         
33         if($rid < 1 || $uid < 1) return false;
34         
35         $vis = gwvpmini_GetVisibilityForRepo($rid);
36         
37         gwvpmini_AddActivityLog("commit", $uid, $rid, "$branch:$commitid", $desc, $vis);
38 }
39
40 function gwvpmini_AddRefActivityForRepo($reponame, $byusername, $branchname, $acttype="branch")
41 {
42         $rid = gwvpmini_GetRepoId($reponame);\r
43         $uid = gwvpmini_GetUserId($byusername);\r
44         \r
45         if($rid < 1 || $uid < 1) return false;
46         
47         $vis = gwvpmini_GetVisibilityForRepo($rid);
48         \r
49         gwvpmini_AddActivityLog("refs", $uid, $rid, "$acttype:$branchname", "", $vis);
50 }
51
52 function gwvpmini_GetVisibilityForRepo($repoid)
53 {
54         $perms = gwvpmini_GetRepoPerms($repoid);
55         $dets = gwvpmini_getRepo($repoid);
56         $oid = $dets["ownerid"];
57         
58         if($perms["b"] == "a") return "a";
59         if($perms["b"] == "r") return "r";
60         
61         $st = ":$oid:";
62         foreach($perms as $key => $val) {
63                 if($key!="b") $st .= "$key:";
64         }
65         return $st;
66 }
67
68 // gets the activity log as it would be viewed by
69 // the user id of "$forid"
70 function gwvpmini_HomePageActivityLog()
71 {
72         global $BASE_URL;
73         
74         $id = -1;
75         if(isset($_SESSION["id"])) $id = $_SESSION["id"];
76         
77         if($id < 0) {
78                 $ents = gwvpmini_GetActivityLog();
79         } else { 
80                 $ents = gwvpmini_GetActivityLog(20, $id);
81         }
82         
83         echo "<h2>News</h2>";
84         echo "<table border=\"1\">";
85         if($ents != null) foreach($ents as $vals) {
86                 /*
87                  *              $ret[$nent]["type"] = $vals["activity_type"];
88                 $ret[$nent]["date"] = $vals["activity_date"];
89                 $ret[$nent]["userid"] = $vals["activity_user"];
90                 $ret[$nent]["repoid"] = $vals["activity_repo"];
91                 $ret[$nent]["commitid"] = $vals["activity_commitid"];
92                 $ret[$nent]["commitlog"] = $vals["activitiy_commitlog"];
93
94                  */
95                 $type = $vals["type"];
96                 //$rest = $vals["date"].", ".$vals["userid"].", ".$vals["repoid"].", ".$vals["commitid"].", ".$vals["commitlog"];
97                 
98                 if($vals["type"] == "commit") {
99                         $udets = gwvpmini_getUser(null, null, $vals["userid"]);
100                         $rdets = gwvpmini_getRepo(null, null, $vals["repoid"]);
101                         $reponame = $rdets["name"];
102                         $uname = $udets["username"];
103                         $fname = $udets["fullname"];
104                         $br_spl = explode(":", $vals["commitid"]);
105                         $branch = $br_spl[0];
106                         $cid = $br_spl[1];
107                         $compressedcid = gwvpmini_CompressCommitId($cid);
108                         $log = $vals["commitlog"];
109                         $tdiff = gwvpmini_TimeDiffText($vals["date"]);
110                         $col1 = "<font size=\"+1\"><a href=\"$BASE_URL/view/$reponame\">$reponame</a></font><br>".gwvpmini_HtmlGravatar($udets["email"], 30, "<br>")."<a href=\"$BASE_URL/user/$uname\">$uname</a>";
111                         $col2 = $tdiff."<br>Commited change <b>$compressedcid</b><br><table border=\"1\"><tr><td bgcolor=\"#eeeeee\"><pre>$log</pre></td></tr></table><br>";
112                 } else if($vals["type"] == "refs") {
113                         $udets = gwvpmini_getUser(null, null, $vals["userid"]);\r
114                         $rdets = gwvpmini_getRepo(null, null, $vals["repoid"]);\r
115                         $reponame = $rdets["name"];\r
116                         $uname = $udets["username"];\r
117                         $fname = $udets["fullname"];
118                         $tdiff = gwvpmini_TimeDiffText($vals["date"]);
119                         $tp_spl = explode(":", $vals["commitid"]);
120                         $col1 = "<font size=\"+1\"><a href=\"$BASE_URL/view/$reponame\">$reponame</a></font><br>".gwvpmini_HtmlGravatar($udets["email"], 30, "<br>")."<a href=\"$BASE_URL/user/$uname\">$uname</a>";
121                         if($tp_spl[0] == "tagcreate") {
122                                 $colapp = "$tdiff<br>Created Tag <b>".$tp_spl[1]."</b>";
123                         } else if($tp_spl[0] == "branchcreate") {
124                                 $colapp = "$tdiff<br>Created Branch <b>".$tp_spl[1]."</b>";
125                         } else {
126                                 $colapp = "$tdiff<br>Performed some unknown action.";
127                         }
128                         $col2 = $colapp;
129                         
130                 }
131                 
132                 echo "<tr><td>$col1</td><td>$col2</td></tr>";
133         }
134         echo "</table>";
135 }
136
137 function gwvpmini_TimeDiffText($time)
138 {
139         $tdiff = time() - $time;
140         
141         if($tdiff < 10) return "Now";
142         if($tdiff < 60) return "$tdiff Seconds Ago";
143         if($tdiff < 3600) return "".(int)($tdiff/60)." Minutes Ago";
144         if($tdiff < 86400) return "".(int)($tdiff/3600)." Hours Ago";
145         if($tdiff < 2592000) {
146                 return "".(int)($tdiff/86400)." Days Ago";
147         }
148         if($tdiff < 31536000) {
149                 $months = (int)($tdiff/2592000);
150                 $days = (int)(($tdiff%2592000)/86400);
151                 
152                 $txt = "$months Month";
153                 if($months > 1) $txt .= "s";
154                 if($days > 0) $txt .= " and $days Day";
155                 if($days > 1) $txt .= "s";
156                 return $txt." ago";
157         }
158         
159         $years = (int)($tdiff/31536000);
160         $months = (int)(($tdiff%31536000)/2592000);
161         $txt = "$years Year";
162         if($years > 1) $txt .= "s";
163         if($months > 0) $txt .= " and $months Month";
164         if($months > 1) $txt .= "s";
165         
166         return $txt." ago";\r
167         
168 }
169
170 ?>