php8
[web.mtrack] / MTrackWeb / Events.php
1 <?php # vim:ts=2:sw=2:et:
2 /* For licensing and copyright terms, see the file named LICENSE */
3  
4 // is this working ???
5 // partly - event.toHTML is borked... - we should use templating for this..
6 require_once 'MTrackWeb/Ticket.php';
7
8 class MTrackWeb_Events extends MTrackWeb_Ticket
9 {
10     var $id; // 0 = new
11     var $issue = false;
12     var $preview;
13     var $error;
14     var $editable;
15     var $tid = 0;
16     
17     function get($pi=0)
18     {
19         $this->id = $pi ?  $pi: (isset($_GET['id']) ? $_GET['id'] : 0);
20         $this->id  = (int) $this->id ;
21         $this->loadIssue();
22         
23         if (!$this->issue) {
24             $this->jerr( "OPPS");
25         }
26  
27         $start = isset($_REQUEST['start']) ? (int) $_REQUEST['start'] : 0;
28         $limit = isset($_REQUEST['limit']) ? (int) $_REQUEST['limit'] : 100;
29         
30         // fetch all the 
31         
32         //DB_DataObject::debugLevel(1);
33         $d = DB_DataObject::factory('mtrack_change');
34         $d->ontable = 'mtrack_ticket';
35         $d->onid = $this->id;
36         $d->orderBy('changedate ASC'); // last first?
37         $d->limit ($start, $limit);
38         $d->audit = array();
39         $d->find();
40         
41         $this->events = array();
42         while ($d->fetch()) {
43             $this->events[$d->id] = clone($d);
44         }
45         /*
46         $d = DB_DataObject::factory('mtrack_change_audit');
47         $d->whereAddIn('change_id', array_keys($this->events), 'int');
48         $d->find();
49         
50         while ($d->fetch()) {
51             $this->events[$d->change_id]->audit[] = clone($d);
52         }
53         
54         */
55         foreach($this->events as $id=>$c) {
56             echo $this->eventToHtml($c);
57         }
58         exit;
59         //$this->renderEvents();
60     }
61     
62     function eventToHtml($e)
63     {
64         
65     
66         
67         $preamble = 0;
68         
69         $cid = "comment:{$e->id}";
70         
71         // tidied up by jquery..
72         $timestamp = $this->link->date($e->changedate, false);
73          
74   
75         $comments = array();
76
77         // default is that something changed..
78         $type = 'Changed';
79         
80         $comment_body = '';
81         
82         $comment_title = '';
83         $comment_fields = array();
84         
85               
86         if (!empty($e->reason)) {
87             $comments[] = $e->reason;
88             $type = 'Comment added';
89             $comment_title = array_shift(explode("\n", $e->reason));
90         }
91         
92          
93         $comment_fields = str_replace("\n", "<BR>" , htmlspecialchars($e->cachedAuditToString()."\n"));
94             //$comment_body .= "<b>$label</b> $citem[action]<br>\n";
95             
96         
97         
98         $commit_info = array();
99         if ($comment_title && 
100             preg_match('/\(In \[changeset:([^,]+),([a-z0-9]+)\]\)(.*)$/i', $comment_title, $commit_info)) {
101             $type = 'Commit';
102             
103             
104             $comment_title = '<a class="changesetlink" href="'. $GLOBALS['ABSWEB'] . 'changeset.php/'. 
105                 $commit_info[1].'/'.$commit_info[2]. '">[' . $commit_info[2] . ']</a>'.$commit_info[3];
106             
107             
108         }
109         if ($this->id == $this->issue->created) {
110             $type = 'Created';
111             $comment_title = 'Issue Created';
112         }
113         
114         
115         
116         
117       
118         $html = '
119             <div class="ticketevent">
120                 <span class="ticketevent-expand" id="ticketevent-expand-' . $e->id . '">+ ' . $type.'</span> 
121                 <a class="pmark" href="#'.$cid.'">#'.$e->id.'</a> 
122                 
123                 <span class="ticketevent-expand" id="ticketevent-expand-title-' . $e->id . '">' .
124                 (strlen($comment_title) ? $comment_title :  $comment_fields) . '
125                 </span>
126                 
127                 <span style="float:right">' . 
128                     $this->link->username($e->person_id, array('no_image' => true, 'fullname' => true))  . '
129                     -  <a name="'.$cid.'">'.$timestamp.'</a> 
130                 </span>
131                 
132             </div>
133            ';        
134               
135               
136          /*
137         foreach ($this->effort as $eff) {
138             $exp = (float)$eff['expended'];
139             if ($eff['expended'] != 0) {
140                 $comment_body .= "<b>spent</b> $exp hours<br>\n";
141                 $preamble++;
142             }
143         }
144          */
145  
146         if ($preamble) {
147             $html .= "<br>\n";
148             $preamble = 0;
149         }
150         
151         foreach ($comments as $cid => $text) {
152             // look for changesets in the comments..
153             // and display them as expandable linsk..
154             $html .= '<pre class="showdown-render">' . htmlspecialchars($text) .
155                 "\n\n" .  $comment_fields . '</PRE>'; // MTrack_Wiki::format_to_html($text);
156         }
157         
158         
159         $html .= '</div>';
160         
161         
162         
163         if ($commit_info) {
164             // list the files that where changed...
165             //$html.= print_r($M, true);
166             $rp = '/' . $commit_info[1] . '/' . $commit_info[2];
167             $fullrp = $rp;
168             $repo = MTrackSCM::factory($rp);
169             $cd = $repo->historyWithChangelog(null, 1, 'rev', $rp);
170             //print_r($cd);exit; 
171             
172             
173             if (!is_array($cd->ent->files) || !count($cd->ent->files)) {
174                 return $html;
175             }
176             
177             $num = count(array_keys($cd->ent->files));
178             $map = array(
179                 'D' => 'DELETED',
180                 'M' => 'MODIFIED',
181                 'A' => 'ADDED'
182             );
183             
184             $file_summary = array();
185             foreach($map as $k=>$v) {
186                 $summary[$v] = 0;
187             }
188             foreach ($cd->ent->files as $id => $file) {
189                
190                 
191                 $type = isset($map[$file->status])  ? $map[$file->status] : '??? '. $file->status;
192                 
193                 if ($num > 10) 
194                 {
195                     // will bork on unknown...
196                     $summary[$type]++;
197                     continue;
198                 }
199                 
200                 
201             $html .= '
202                 <div class="ticketevent ticketevent-fileitem">
203                     <span class="ticketevent-expand" id="ticketevent-expand-' . $this->cid . '.' . $id .'">+ ' . $type.'</span> 
204                     <span title="' . $fullrp . '?part=' . $id. '"  class="ticketevent-expand" id="ticketevent-expand-title-' . $this->cid . '.' . $id . '">' .
205                     $file->nameToHtml() . '
206                     </span>
207                     <span title="' . $fullrp . '?part=' . $id. '"  class="ticketevent-expand-diff changesetlink">[View Diff]</span> 
208                     <span title="' . $fullrp . '" class="ticketevent-expand-view changesetlink">[View File]</span>
209                     <span title="' . $fullrp . '" class="ticketevent-expand-view changesetlink">[View History]</span>
210                 </div>
211                 <div class="ticketchangeinfo" id="ticketchangeinfo-' . $this->cid . '.' . $id  . '"></div>
212         ';            
213                 
214                 
215                
216             }
217             
218             if ($num>  10) {
219                 $ar = array();
220                 foreach($summary as $k=>$v) {
221                     if (empty($v)) {
222                         continue;
223                     }
224                     $ar[] = $v . ' ' . $k;
225                 }
226                 $html .= '
227                 <div class="ticketevent ticketevent-fileitem">
228                     <span class="ticketevent-expand" id="ticketevent-expand-' . $this->cid . '.0">+ MULTIPLE FILES: </span>
229                     <span title="' . $fullrp . '?part=' . 0 . '"  class="ticketevent-expand" id="ticketevent-expand-title-' . $this->cid . '.' . $id . '">' .
230                     implode(', ', $ar) . '
231                     </span>
232                     <span title="' . $fullrp . '"  class="ticketevent-expand-diff changesetlink">[View Diff]</span> 
233                     <span title="' . $fullrp . '" class="ticketevent-expand-view changesetlink">[View File]</span>
234                     <span title="' . $fullrp . '" class="ticketevent-expand-view changesetlink">[View History]</span>
235                 </div>
236                 <div class="ticketchangeinfo" id="ticketchangeinfo-' . $this->cid . '"></div>
237         ';            
238                 
239             }
240             
241             //$html .= print_r($cd, true);
242              
243         }
244         
245          
246         return $html ;
247                 
248                 
249     }
250     
251     
252     function collapse_diff($diff)
253     {
254       static $idnum = 1;
255       require_once 'MTrackWeb/Changeset.php';
256       $cs = new MTrackWeb_Changeset();
257       $id = 'diff_' . $idnum++;
258       return "<br>" .
259         "<button onclick='\$(&quot;#$id&quot;).toggle(); return false;'>Toggle diff</button>".
260         "<div id='$id' style='display:none'>" .
261             $cs->diff($diff) . "</div>";
262     }
263     
264     function diff_strings($before, $now)
265     {
266          
267         $tempdir = sys_get_temp_dir();
268         $afile = tempnam($tempdir, "mtrack");
269         $bfile = tempnam($tempdir, "mtrack");
270         file_put_contents($afile, $before);
271         file_put_contents($bfile, $now);
272         static $diff = false;
273         if (!$diff) {
274             require_once 'System.php';
275             $diff= System::which('diff');
276         }
277         if (PHP_OS == 'SunOS') {
278             // TODO: make an option to allow use of gnu diff on solaris
279            $diff = shell_exec("$diff -u $afile $bfile");
280            $diff = str_replace($afile, 'before', $diff);
281            $diff = str_replace($bfile, 'now', $diff);
282          } else {
283             $diff = shell_exec("$diff --label before --label now -u $afile $bfile");
284          }
285         unlink($afile);
286         unlink($bfile);
287         $diff = htmlentities($diff, ENT_COMPAT, 'utf-8');
288         return $diff;
289     }
290      
291     
292       
293 }