final move of files
[web.mtrack] / MTrackWeb / LinkHandler.php
1 <?php
2
3 /**
4  * This handles link creation for the wiki..
5  * 
6  * it get's registered by MTrackWeb as the link handler..
7  * 
8  */
9 require_once 'MTrack/Interface/WikiLinkHandler.php';
10
11
12 class MTrackWeb_LinkHandler implements MTrack_Interface_WikiLinkHandler
13 {
14     function MTrackWeb_LinkHandler()
15     {
16         $ff = HTML_FlexyFramework::get();
17         
18         $this->baseURL = $ff->cli ? $ff->MTrackCLI['baseURL'] : $ff->page->baseURL;
19         
20         
21     }
22     
23     /**
24      * opts
25      *  [display] = text to display
26      * 
27      */
28     function ticket($no, $extras=array())
29     {
30         
31         if ($no instanceof MTrackIssue) {
32             $tkt = $no;
33         } else if (is_string($no) || is_int($no)) {
34             static $cache = array();
35
36             if ($no[0] == '#') {
37                 $no = substr($no, 1);
38             }
39
40             if (!isset($cache[$no])) {
41                 $tkt = MTrackIssue::loadByNSIdent($no);
42                 if (!$tkt) {
43                     $tkt = MTrackIssue::loadById($no);
44                 }
45                 $cache[$no] = $tkt;
46             } else {
47                 $tkt = $cache[$no];
48             }
49         } else {
50             // FIXME: hinted data from reports
51             $tkt = new stdClass;
52             $tkt->tid = $no['ticket'];
53             $tkt->summary = $no['summary'];
54             $tkt->status = '';
55             
56             if (isset($no['state'])) {
57               $tkt->status = $no['state'];
58             } elseif (isset($no['status'])) {
59               $tkt->status = $no['status'];
60             } elseif (isset($no['__status__'])) {
61               $tkt->status = $no['__status__'];
62             }  
63             
64         }
65         if ($tkt == NULL) {
66             $tkt = new stdClass;
67             $tkt->tid = $no;
68             $tkt->summary = 'No such ticket';
69             $tkt->status = 'No such ticket';
70         }
71         $html = "<a class='ticketlink";
72         if ($tkt->status == 'closed') {
73             $html .= ' completed';
74         }
75         if (!empty($tkt->nsident)) {
76             $ident = $tkt->nsident;
77         } else {
78             $ident = $tkt->tid;
79         }
80         if (isset($extras['#'])) {
81             $anchor = '#' . $extras['#'];
82         } else {
83             $anchor = '';
84         }
85         $html .= "' href=\"{$this->baseURL}/Ticket/$ident$anchor\">";
86         if (isset($extras['display'])) {
87             $html .= htmlspecialchars($extras['display']);
88         } else {
89             $html .= '#' . htmlspecialchars($ident);
90         }
91         $html .= "</a>";
92         return $html;
93     }
94
95     function milestone(  $target, $label)
96     {
97         // silly but we assume target is urlencoded???...
98         $ms = MTrack_Milestone::loadByName($target);
99         
100         return  '<span class="milestone' .
101             (($ms->deleted || $ms->completed) ? ' completed' : '') .
102             '"><a href="'. $this->baseURL . '/Milestone/'. $target .'">'.
103                 htmlspecialchars(urldecode($target))  . 
104             '</a></span>';
105     }
106     
107     function help($target, $label, $anchor = '')  
108     {
109         // escape anchor???
110         
111         $target .= !empty($anchor) ? "#$anchor" : '';
112         
113         return '<a class="wikilink"
114             href="'.$this->baseURL . '/Help/' . htmlspecialchars($target) . 
115                 '">' . htmlspecialchars($label) . '</a>';
116     }
117     
118     function username($target, $opts=array())
119     {
120         if (is_object($target)) {
121             $userinfo  = $target;
122             $target = $userinfo->userid;
123         } else {
124             $userinfo  = MTrack_DataObjects_Userinfo::findString($target);
125         }
126         
127         if (is_string($opts)) {
128             parse_str($opts, $opts);
129         }
130         
131         //print_R($userinfo);
132         if (!$userinfo) {
133             // we did not find the ysers...
134             // we currently ignore all the formating....
135             return '<a 
136                 href="'. $this->baseURL .'/User/'. urlencode($target) . '"  class="userlink">' . 
137                 htmlspecialchars($target). 
138                 '</a>';
139         }
140          /* WHY?
141           if (!ctype_alnum($username)) {
142             $target = "{$ABSWEB}user.php?user=" . urlencode($username);
143             if (isset($opts['edit'])) {
144               $target .= '&edit=1';
145             }
146           } 
147           */  
148         
149         
150         $open_a = 
151             '<a  title="' . htmlspecialchars($userinfo->fullname) . '" 
152             href="' . 
153                 $this->baseURL .'/User/'.
154                     urlencode($userinfo->userid)  . 
155                     (empty($opts['edit']) ? '' : '?edit=1') .
156             '" 
157             class="userlink' . 
158                 (isset($opts['class']) ? ' ' . $opts['class'] : '') .
159             '">';
160         
161         $dispname = $userinfo->fullname;
162         //$dispname = preg_match("/^([^+]*)(\+.*)?@(.*)$/", $target) ? $target : $userinfo->userid;
163         
164         
165         $size = empty($opts['size']) ? 24 : (int) $opts['size'];
166        // var_dump($opts);
167         $ret = 
168             (!empty($opts['no_image']) ? '' :  ($open_a . 
169                 '<img class="gravatar"
170                     width="' . $size . '" height="' . $size . '" 
171                     src="' . $this->baseURL . '/Avatar.png?u=' . 
172                         urlencode($userinfo->userid) . '&amp;s='. $size . '">
173                 </a>')) .
174             
175             // at this point the old code tries to chop it...
176             // I'm not sure that is a great idea....
177             (!empty($opts['no_name']) ? '' :  ($open_a .  htmlspecialchars($dispname) . '</a>')); 
178         //var_dump( htmlspecialchars($ret));
179         return $ret;
180     }
181                 
182     function browse($target, $label)
183     {
184         return  '<a href="'. $this->baseURL . '/Browse/'. htmlspecialchars($target) .'">'.
185                 htmlspecialchars($label)  . 
186             '</a></span>';
187     }
188     function log($target, $label) // what does this do...
189     {
190         return  '<a href="'. $this->baseURL . '/Log/'. htmlspecialchars($target) .'">'.
191                 htmlspecialchars($label)  . 
192             '</a>';
193     }
194     
195     function branch($branch, $repo = null)
196     {
197       return "<span class='branchname'>" . htmlspecialchars($branch) . "</span>";
198     }
199
200         
201       
202     
203     function changeset($cs, $repo = null)
204     {
205         $display =  substr($cs, 0, 12); 
206         if (empty($repo)) {
207             $repo = MTrackRepo::loadByChangeSet($cs);
208         }
209         if (is_string($repo)) {
210             $repo = MTrackRepo::loadByName($rep);
211         }
212         if (!is_a($repo, 'MTrackRepo')) {
213             $repo = MTrackRepo::defaultRepo();
214         }
215         $p = $repo->getBrowseRootName() . '/'. $cs;
216         
217         
218         return '<a class="changesetlink"
219             href="'. $this->baseURL . '/Changeset/'. htmlspecialchars($p) .'">[' .  substr($cs, 0, 12) . ']</a>';
220     }
221         
222         
223     function wiki($pagename, $extras = array())
224     {
225         
226         if ($pagename instanceof MTrack_Wiki_Item) {
227             $wiki = $pagename;
228         } else if (is_string($pagename)) {
229             $wiki = null;//MTrackWikiItem::loadByPageName($pagename);
230         } else {
231             // FIXME: hinted data from reports
232             throw new Exception("FIXME: wiki");
233         }
234         
235         if ($wiki) {
236             $pagename = $wiki->pagename;
237         }
238         $pagename .= empty( $extras['#']) ? '' : '#' . $extras['#'];
239         
240         return  '<a class="wikilink"  
241             href="'.$this->baseURL . '/Wiki/'. htmlspecialchars($pagename) .'">'.
242             htmlspecialchars(empty($extras['display']) ? $pagename :  $extras['display']) .     
243             '</a>';
244     }
245
246     // all these are prtty much the same..    
247     function query($target, $label) 
248     {
249         
250          return  '<a href="'. $this->baseURL . '/Query/'. htmlspecialchars($target) .'">'.
251                 htmlspecialchars($label)  . 
252             '</a>';
253     }
254     function report($target, $label) 
255     {
256         return  '<a href="'. $this->baseURL . '/Report/'. htmlspecialchars($target) .'">'.
257                 htmlspecialchars($label)  . 
258             '</a>';
259     }
260     
261     // technically not a link....
262     function date($tstring, $show_full = false)
263     {
264       /* database time is always relative to UTC */
265       $d = date_create($tstring, new DateTimeZone('UTC'));
266       if (!is_object($d)) {
267         throw new Exception("could not represent $tstring as a datetime object");
268       }
269       $iso8601 = $d->format(DateTime::W3C);
270       /* but we want to render relative to user prefs */
271       date_timezone_set($d, new DateTimeZone(date_default_timezone_get()));
272       $full = $d->format('D, M d Y H:i');
273
274       if (!$show_full) {
275             return "<abbr title=\"$iso8601\" class='timeinterval'>$full</abbr>";
276       }
277
278       return "<abbr title='$iso8601' class='timeinterval'>$full</abbr> <span class='fulldate'>$full</span>";
279     }
280     
281 }