php8
[web.mtrack] / MTrackWeb / Timeline.php
1 <?php # vim:ts=2:sw=2:et:
2 /* For licensing and copyright terms, see the file named LICENSE */
3
4
5
6
7 require_once 'MTrack/Attachment.php';
8 require_once 'MTrackWeb.php';
9
10 class MTrackWeb_Timeline extends MTrackWeb
11 {
12         
13     
14     var $title = 'Timeline';
15     
16      var $start_time = '-2 weeks';
17     var $limit = 250;
18     // fixme = this should be alot more efficient...
19     
20      
21     
22     
23     function get($only_users)
24     {
25         
26         
27         if (!isset($_REQUEST['ajax_body'])) {
28             return;
29         }
30         
31         
32         $this->masterTemplate = 'timeline.html';
33         // perms... 
34         $pid = $this->currentProject();
35          //DB_DataObject::debugLevel(1);
36         $isSummary = false;
37         
38         
39         $e = DB_DataObject::factory('mtrack_change');
40         $e->orderBy('changedate DESC');
41         
42         $start = empty($_REQUEST['from_date']) ? date('Y-m-01') : 
43             date('Y-m-01', strtotime($_REQUEST['from_date']));
44
45         $start_day = empty($_REQUEST['from_date']) ? date('Y-m-d') : 
46             date('Y-m-d', strtotime($_REQUEST['from_date']));
47             
48         $e->whereAdd("
49             ( ontable='mtrack_ticket' AND
50                 onid IN (SELECT id FROM mtrack_ticket where project_id = $pid)
51             )
52             OR
53             ( ontable='mtrack_repos' AND
54                 onid IN (SELECT id FROM mtrack_repos where project_id = $pid)
55             )
56         ");
57             
58         if (!empty($_REQUEST['viewtype']) && $_REQUEST['viewtype'] == 'summary') {
59             //DB_DataObject::debugLevel(1);
60             $isSummary = true;
61             $e->whereAdd("changedate >= '$start 00:00:00' AND changedate < '$start 00:00:00' + INTERVAL 1 MONTH");
62             $e->selectAdd();
63             $e->joinAdd(DB_DataObject::factory('core_person'), 'LEFT');
64             $e->selectAdd("
65                      DATE_FORMAT(changedate, '%Y-%m-%d')  as changeday,
66                      CONCAT(DATE_FORMAT(changedate, '%Y-%m-%d-') ,Person.id)  as id,
67                      Person.name as name,
68                      COUNT(mtrack_change.id) as nchanges
69                     ");
70             $e->groupBy("changeday,id,name");
71             $e->orderBy('changeday desc, name asc');
72             $ret = array();
73             $e->find();
74             while($e->fetch()) {
75                 $ret[] = $e->toArray();
76             }
77             $this->jdata($ret);
78             
79             
80         }  
81         $e->whereAdd("changedate >= '$start_day 00:00:00' AND
82                       changedate < '$start_day 00:00:00' + INTERVAL 1 DAY");
83          
84         //DB_DataObject::debugLevel(1);
85         
86        
87         
88         
89         
90         
91         
92         $ar = $e->fetchAll();
93         $this->events = array();
94         //$this->hist= array();
95         $last_day  =false;
96         foreach($ar as $i=>$h) {
97             $h->cls = $i % 2 ? 'odd' : '';
98         
99             $ts = strtotime($h->changedate);
100             $day = date('D, M d Y', $ts);
101             $time = date('g:ia', $ts);
102             //print_r($ent);
103             
104             $add = $h->toArray();
105             
106             if ($day !== $last_day) {
107                 $add['first_of_day'] = 1;
108             }
109             $last_day = $day;
110             $p = $h->person();
111             $add['person_id_name'] = $p->name;
112             $add['person_id_email'] = $p->email;
113             $add['audit'] = $h->cachedAuditToString();
114             $add['audit_ar'] = $h->cachedAuditToJSONArray();
115             // add the underlying object..
116             $add = array_merge($add, $h->objectCached()->toArray('object_%s'));
117             
118             
119             
120             
121             
122             
123     
124             $this->events[] = $add;
125              
126         }
127         $total = count($this->events);
128         $extra = !$total ? array() :  array(
129                  'metaData'  => $this->meta($e, $this->events)
130         );
131              
132          
133
134         $this->jdata($this->events, $total, $extra);
135     
136          
137          
138     
139     }
140      /**
141       * this is a very simple version of the main one in roo.php
142       */
143     function meta($x, $data)
144     {
145         // this is not going to work on queries where the data does not match the database def..
146         // for unknown columns we send them as stirngs..
147         $lost = 0;
148         $cols  = array_keys($data[0]);
149      
150         
151         
152         
153         
154         $options = HTML_FlexyFramework::get()->DB_DataObject;
155         //echo '<PRE>';print_r($options); exit;
156         $reader = $options["ini_{$x->_database}"] .'.reader';
157         if (!file_exists( $reader )) {
158             return;
159         }
160         
161         $rdata = unserialize(file_get_contents($reader));
162         
163         //echo '<PRE>';print_r($rdata);exit;
164         
165         $meta = array();
166         foreach($cols as $c ) {
167             $cc = $x->tableName().'.'.$c;
168             if (      !isset($rdata[$cc]) 
169                 ||    !is_array($rdata[$cc])
170             ) {
171                 
172                 $meta[] = $c;
173                 continue;    
174             }
175             $add = $rdata[$cc];
176             $add['name'] = $c;
177             $meta[] = $add;
178         }
179         
180         
181         
182         return array(
183             'totalProperty' =>  'total',
184             'successProperty' => 'success',
185             'root' => 'data',
186             'id' => 'id',
187             'fields' => $meta
188         );
189          
190         
191     }
192 }
193