MTrackWeb/LinkHandler.php
[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 DB_DataObject) {
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  = DB_DataOBject::Factory('mtrack_ticket');
42                 $tkt->get($no);
43                 
44                 $cache[$no] = $tkt;
45             } else {
46                 $tkt = $cache[$no];
47             }
48         } else {
49             // FIXME: hinted data from reports
50             $tkt = new stdClass;
51             $tkt->id = $no['ticket'];
52             $tkt->summary = $no['summary'];
53             $tkt->status = '';
54             
55             if (isset($no['state'])) {
56               $tkt->status = $no['state'];
57             } elseif (isset($no['status'])) {
58               $tkt->status = $no['status'];
59             } elseif (isset($no['__status__'])) {
60               $tkt->status = $no['__status__'];
61             }  
62             
63         }
64         if ($tkt == NULL) {
65             $tkt = new stdClass;
66             $tkt->id = $no;
67             $tkt->summary = 'No such ticket';
68             $tkt->status = 'No such ticket';
69         }
70         $html = "<a class='ticketlink";
71         if ($tkt->status == 'closed') {
72             $html .= ' completed';
73         }
74         if (!empty($tkt->nsident)) {
75             $ident = $tkt->nsident;
76         } else {
77             $ident = $tkt->id;
78         }
79         if (isset($extras['#'])) {
80             $anchor = '#' . $extras['#'];
81         } else {
82             $anchor = '';
83         }
84         $html .= "' href=\"{$this->baseURL}/Ticket/$ident$anchor\">";
85         if (isset($extras['display'])) {
86             $html .= htmlspecialchars($extras['display']);
87         } else {
88             $html .= '#' . htmlspecialchars($ident);
89         }
90         $html .= "</a>";
91         return $html;
92     }
93
94     function milestone(  $target, $label)
95     {
96         // silly but we assume target is urlencoded???...
97         $ms = MTrack_Milestone::loadByName($target);
98         
99         return  '<span class="milestone' .
100             (($ms->deleted || $ms->completed) ? ' completed' : '') .
101             '"><a href="'. $this->baseURL . '/Milestone/'. $target .'">'.
102                 htmlspecialchars(urldecode($target))  . 
103             '</a></span>';
104     }
105     
106     function help($target, $label, $anchor = '')  
107     {
108         // escape anchor???
109         
110         $target .= !empty($anchor) ? "#$anchor" : '';
111         
112         return '<a class="wikilink"
113             href="'.$this->baseURL . '/Help/' . htmlspecialchars($target) . 
114                 '">' . htmlspecialchars($label) . '</a>';
115     }
116     
117     function username($target, $opts=array())
118     {
119         if (is_object($target)) {
120             $person = $target;
121             $target = $person->id;
122         } else {
123             $person  = DB_DataObject::factory('Person');
124             $person->get($target);
125         }
126         
127         if (is_string($opts)) {
128             parse_str($opts, $opts);
129         }
130         
131         //print_R($person);
132         if (!$person->id) {
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($person->name) . '" 
152             href="' . 
153                 $this->baseURL .'/User/'.
154                     urlencode($person->id)  . 
155                     (empty($opts['edit']) ? '' : '?edit=1') .
156             '" 
157             class="userlink' . 
158                 (isset($opts['class']) ? ' ' . $opts['class'] : '') .
159             '">';
160         
161         $dispname = $person->name;
162         //$dispname = preg_match("/^([^+]*)(\+.*)?@(.*)$/", $target) ? $target : $person->id;
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($person->id) . '&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 = MTrack_Repo::loadByChangeSet($cs);
208         }
209         if (is_string($repo)) {
210             $repo = MTrack_Repo::loadByName($rep);
211         }
212         if (!is_a($repo, 'MTrack_Repo')) {
213             $repo = MTrack_Repo::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         return htmlspecialchars($pagename);
226         if ($pagename instanceof MTrack_Wiki_Item) {
227             $wiki = $pagename;
228         } else if (is_string($pagename)) {
229             $wiki =  new MTrack_Wiki_Item($pagename);
230             if (!$wiki->file) {
231                 $wiki = false;
232             }
233         } else {
234             // FIXME: hinted data from reports
235             throw new Exception("FIXME: wiki");
236         }
237         
238         if ($wiki) {
239             $pagename = $wiki->pagename;
240         }
241         $pagename .= empty( $extras['#']) ? '' : '#' . $extras['#'];
242         
243         return  '<a class="wikilink"  
244             href="'.$this->baseURL . '/Wiki/'. htmlspecialchars($pagename) .'">'.
245             htmlspecialchars(empty($extras['display']) ? $pagename :  $extras['display']) .     
246             '</a>';
247     }
248
249     // all these are prtty much the same..    
250     function query($target, $label) 
251     {
252         
253          return  '<a href="'. $this->baseURL . '/Query/'. htmlspecialchars($target) .'">'.
254                 htmlspecialchars($label)  . 
255             '</a>';
256     }
257     function report($target, $label) 
258     {
259         return  '<a href="'. $this->baseURL . '/Report/'. htmlspecialchars($target) .'">'.
260                 htmlspecialchars($label)  . 
261             '</a>';
262     }
263     
264     // technically not a link....
265     function date($tstring, $show_full = false)
266     {
267       /* database time is always relative to UTC */
268       $d = date_create($tstring, new DateTimeZone('UTC'));
269       if (!is_object($d)) {
270         throw new Exception("could not represent $tstring as a datetime object");
271       }
272       $iso8601 = $d->format(DateTime::W3C);
273       /* but we want to render relative to user prefs */
274       date_timezone_set($d, new DateTimeZone(date_default_timezone_get()));
275       $full = $d->format('D, M d Y H:i');
276
277       if (!$show_full) {
278             return "<abbr title=\"$iso8601\" class='timeinterval'>$full</abbr>";
279       }
280
281       return "<abbr title='$iso8601' class='timeinterval'>$full</abbr> <span class='fulldate'>$full</span>";
282     }
283     
284 }