MTrackWeb/Timeline.php
[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
46         $e->whereAdd("
47             ( ontable='mtrack_ticket' AND
48                 onid IN (SELECT id FROM mtrack_ticket where project_id = $pid)
49             )
50             OR
51             ( ontable='mtrack_repos' AND
52                 onid IN (SELECT id FROM mtrack_repos where project_id = $pid)
53             )
54         ");
55             
56         if (!empty($_REQUEST['viewtype']) && $_REQUEST['viewtype'] == 'summary') {
57             //DB_DataObject::debugLevel(1);
58             $isSummary = true;
59             $e->whereAdd("changedate >= '$start 00:00:00' AND changedate < '$start 00:00:00' + INTERVAL 1 MONTH");
60             $e->selectAdd();
61             $e->joinAdd(DB_DataObject::factory('Person'), 'LEFT');
62             $e->selectAdd("
63                      DATE_FORMAT(changedate, '%Y-%m-%d')  as changeday,
64                      CONCAT(DATE_FORMAT(changedate, '%Y-%m-%d-') ,Person.id)  as id,
65                      Person.name as name,
66                      COUNT(mtrack_change.id) as nchanges
67                     ");
68             $e->groupBy("changeday,id,name");
69             $e->orderBy('changeday desc, name asc');
70             $ret = array();
71             $e->find();
72             while($e->fetch()) {
73                 $ret[] = $e->toArray();
74             }
75             $this->jdata($ret);
76             
77             
78         }  
79         $e->whereAdd("changedate >= '$start 00:00:00' AND
80                       changedate < '$start 00:00:00' + INTERVAL 1 DAY");
81          
82         DB_DataObject::debugLevel(1);
83         
84        
85         
86         
87         
88         
89         
90         $ar = $e->fetchAll();
91         $this->events = array();
92         //$this->hist= array();
93         $last_day  =false;
94         foreach($ar as $i=>$h) {
95             $h->cls = $i % 2 ? 'odd' : '';
96         
97             $ts = strtotime($h->changedate);
98             $day = date('D, M d Y', $ts);
99             $time = date('g:ia', $ts);
100             //print_r($ent);
101             
102             $add = $h->toArray();
103             
104             if ($day !== $last_day) {
105                 $add['first_of_day'] = 1;
106             }
107             $last_day = $day;
108             $p = $h->person();
109             $add['person_id_name'] = $p->name;
110             $add['person_id_email'] = $p->email;
111             $add['audit'] = $h->cachedAuditToString();
112             $add['audit_ar'] = $h->cachedAuditToJSONArray();
113             // add the underlying object..
114             $add = array_merge($add, $h->objectCached()->toArray('object_%s'));
115             
116             
117             
118             
119             
120             
121     
122             $this->events[] = $add;
123              
124         }
125         
126         $extra = array(
127                  'metaData'  => $this->meta($e, $this->events)
128         );
129              
130          
131
132         $this->jdata($this->events, count($this->events), $extra);
133     
134          
135          
136     
137     }
138      /**
139       * this is a very simple version of the main one in roo.php
140       */
141     function meta($x, $data)
142     {
143         // this is not going to work on queries where the data does not match the database def..
144         // for unknown columns we send them as stirngs..
145         $lost = 0;
146         $cols  = array_keys($data[0]);
147      
148         
149         
150         
151         
152         $options = HTML_FlexyFramework::get()->DB_DataObject;
153         //echo '<PRE>';print_r($options); exit;
154         $reader = $options["ini_{$x->_database}"] .'.reader';
155         if (!file_exists( $reader )) {
156             return;
157         }
158         
159         $rdata = unserialize(file_get_contents($reader));
160         
161         //echo '<PRE>';print_r($rdata);exit;
162         
163         $meta = array();
164         foreach($cols as $c ) {
165             $cc = $x->tableName().'.'.$c;
166             if (      !isset($rdata[$cc]) 
167                 ||    !is_array($rdata[$cc])
168             ) {
169                 
170                 $meta[] = $c;
171                 continue;    
172             }
173             $add = $rdata[$cc];
174             $add['name'] = $c;
175             $meta[] = $add;
176         }
177         
178         
179         
180         return array(
181             'totalProperty' =>  'total',
182             'successProperty' => 'success',
183             'root' => 'data',
184             'id' => 'id',
185             'fields' => $meta
186         );
187          
188         
189     }
190 }
191