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         } else {
79             $e->whereAdd("changedate >= '$start 00:00:00' AND changedate < '$start 00:00:00' + INTERVAL 3 DAY");
80         }
81         
82         
83        
84         
85         
86         
87         
88         
89         $ar = $e->fetchAll();
90         $this->events = array();
91         //$this->hist= array();
92         $last_day  =false;
93         foreach($ar as $i=>$h) {
94             $h->cls = $i % 2 ? 'odd' : '';
95         
96             $ts = strtotime($h->changedate);
97             $day = date('D, M d Y', $ts);
98             $time = date('g:ia', $ts);
99             //print_r($ent);
100             
101             $add = $h->toArray();
102             
103             if ($day !== $last_day) {
104                 $add['first_of_day'] = 1;
105             }
106             $last_day = $day;
107             $p = $h->person();
108             $add['person_id_name'] = $p->name;
109             $add['person_id_email'] = $p->email;
110             $add['audit'] = $h->cachedAuditToString();
111             $add['audit_ar'] = $h->cachedAuditToJSONArray();
112             // add the underlying object..
113             $add = array_merge($add, $h->objectCached()->toArray('object_%s'));
114             
115             
116             
117             
118             
119             
120     
121             $this->events[] = $add;
122              
123         }
124         
125         $extra = array(
126                  'metaData'  => $this->meta($e, $this->events)
127         );
128              
129          
130
131         $this->jdata($this->events, count($this->events), $extra);
132     
133          
134          
135     
136     }
137      /**
138       * this is a very simple version of the main one in roo.php
139       */
140     function meta($x, $data)
141     {
142         // this is not going to work on queries where the data does not match the database def..
143         // for unknown columns we send them as stirngs..
144         $lost = 0;
145         $cols  = array_keys($data[0]);
146      
147         
148         
149         
150         
151         $options = HTML_FlexyFramework::get()->DB_DataObject;
152         //echo '<PRE>';print_r($options); exit;
153         $reader = $options["ini_{$x->_database}"] .'.reader';
154         if (!file_exists( $reader )) {
155             return;
156         }
157         
158         $rdata = unserialize(file_get_contents($reader));
159         
160         echo '<PRE>';print_r($rdata);exit;
161         
162         $meta = array();
163         foreach($cols as $c ) {
164             $cc = $x->tableName().'.'.$c;
165             if (      !isset($rdata[$cc]) 
166                 ||    !is_array($rdata[$cc])
167             )) {
168                 
169                 $meta[] = $c;
170                 continue;    
171             }
172             $add = $rdata[$cc];
173             $add['name'] = $c;
174             $meta[] = $add;
175         }
176         
177         
178         
179         return array(
180             'totalProperty' =>  'total',
181             'successProperty' => 'success',
182             'root' => 'data',
183             'id' => 'id',
184             'fields' => $meta
185         );
186          
187         
188     }
189 }
190