php8
[web.mtrack] / MTrackWeb / Timeline.php
index 10e9cb0..80b5ebc 100644 (file)
@@ -10,171 +10,184 @@ require_once 'MTrackWeb.php';
 class MTrackWeb_Timeline extends MTrackWeb
 {
         
-    var $template = 'wiki.html';
+    
     var $title = 'Timeline';
     
      var $start_time = '-2 weeks';
-    var $limit = 50;
+    var $limit = 250;
     // fixme = this should be alot more efficient...
     
-    $events = mtrack_get_timeline('-2 weeks', $user, $limit);
-    
-    function getAuth()
-    {
-        parent::getAuth();
-        MTrackACL::requireAllRights('Timeline', 'read');
-    }
+     
     
     
     function get($only_users)
     {
         
-        if (is_string($this->start_time)) {
-           $date_limit = strtotime($this->start_time);
-        } else {
-           $date_limit = $this->start_time; // assume that it's a timestamp
-        }
-        /* round back to earlier minute (aids caching) */
-        $date_limit -= $date_limit % 60;
-        $db_date_limit = MTrackDB::unixtime($date_limit);
-        $last_date = null;
-        
-        $filter_users = null;
-        /*if (is_string($only_users)) { FIXME
-           $filter_users = array(mtrack_canon_username($only_users));
-        } else if (is_array($only_users)) { // will not happen....
-           $filter_users = array();
-           foreach ($only_users as $user) {
-               $filter_users[] = mtrack_canon_username($user);
-           }
-       } */
-    
-        $proj_by_id = array();
-        foreach (MTrackDB::q('select projid from projects')->fetchAll() as $r) {
-           $proj_by_id[$r[0]] = MTrackProject::loadById($r[0]);
-        }
-        $events = array();
-        
-        // check commits.
-        foreach (MTrackDB::q('select repoid from repos')->fetchAll() as $row) {
-            list($repoid) = $row;
-            $repo = MTrackRepo::loadById($repoid);
-            $reponame = $repo->getBrowseRootName();
-            if ($reponame == 'default/wiki') continue;
-            $checker = new MTrackCommitChecker($repo);
-    
-            $hist = $repo->history(null, $db_date_limit);
-            if (is_array($hist)) foreach ($hist as $e) {
-                if (is_array($filter_users)) {
-                   $wanted_user = false;
-                   foreach ($filter_users as $fuser) {
-                       if (mtrack_canon_username($e->changeby) === $fuser) {
-                           $wanted_user = true;
-                           break;
-                       }
-                   }
-                   if (!$wanted_user) {
-                       continue;
-                   }
-                }
-                /* we want to include changesets that do not reference tickets */
-                $pid = $repo->projectFromPath($e->files);
-                if ($pid > 1) {
-                    $proj = $proj_by_id[$pid];
-                    $e->changelog = $proj->adjust_links($e->changelog, true);
-                }
-                $actions = $checker->parseCommitMessage($e->changelog);
-                $tickets = array();
-                foreach ($actions as $act) {
-                   $tkt = $act[1];
-                   $tickets[$tkt] = $tkt;
-                   $repo_changes_by_ticket[$tkt][$reponame][$e->rev] = $e->rev;
-                }
-                if (count($tickets) == 0) {
-                    $events[] = array(
-                           'changedate' => $e->ctime,
-                           'who' => $e->changeby,
-                           'object' => "changeset:$reponame:$e->rev",
-                           'reason' => $e->changelog,
-                           );
-                }
-           }
-       }
-       // look in changes
-       foreach (MTrackDB::q("select 
-               changedate, who, object, reason from changes
-               where changedate > ?
-               order by changedate desc
-               ", $db_date_limit)->fetchAll(PDO::FETCH_ASSOC) as $row) {
-           if (is_array($filter_users)) {
-               $wanted_user = false;
-               foreach ($filter_users as $fuser) {
-                   if (mtrack_canon_username($row['who']) === $fuser) {
-                       $wanted_user = true;
-                       break;
-                   }
-               }
-               if (!$wanted_user) {
-                   continue;
-               }
-           }
-           $events[] = $row;
+        
+        if (!isset($_REQUEST['ajax_body'])) {
+            return;
         }
-    
-        usort($events, function ($a, $b) {
-                   return strcmp($b['changedate'], $a['changedate']);
-        });
-        $this->events = [];
-        $last_date  = false;
-        foreach($events as $e) {
-            $d = date_create($e['changedate'], new DateTimeZone('UTC'));
-            date_timezone_set($d, new DateTimeZone(date_default_timezone_get()));
-            $e['time'] = $d->format('H:i');
-            $day = $d->format('D, M d Y');
-            if ($last_date != $day) {
-                $this->events[] = (object) array(
-                    'day' => $day,
-                    'isDay' => 1;
-                );
-                  
-                $last_date = $day;
+        
+        
+        $this->masterTemplate = 'timeline.html';
+        // perms... 
+        $pid = $this->currentProject();
+         //DB_DataObject::debugLevel(1);
+        $isSummary = false;
+        
+        
+        $e = DB_DataObject::factory('mtrack_change');
+        $e->orderBy('changedate DESC');
+        
+        $start = empty($_REQUEST['from_date']) ? date('Y-m-01') : 
+            date('Y-m-01', strtotime($_REQUEST['from_date']));
+
+        $start_day = empty($_REQUEST['from_date']) ? date('Y-m-d') : 
+            date('Y-m-d', strtotime($_REQUEST['from_date']));
+            
+        $e->whereAdd("
+            ( ontable='mtrack_ticket' AND
+                onid IN (SELECT id FROM mtrack_ticket where project_id = $pid)
+            )
+            OR
+            ( ontable='mtrack_repos' AND
+                onid IN (SELECT id FROM mtrack_repos where project_id = $pid)
+            )
+        ");
+            
+        if (!empty($_REQUEST['viewtype']) && $_REQUEST['viewtype'] == 'summary') {
+            //DB_DataObject::debugLevel(1);
+            $isSummary = true;
+            $e->whereAdd("changedate >= '$start 00:00:00' AND changedate < '$start 00:00:00' + INTERVAL 1 MONTH");
+            $e->selectAdd();
+            $e->joinAdd(DB_DataObject::factory('core_person'), 'LEFT');
+            $e->selectAdd("
+                     DATE_FORMAT(changedate, '%Y-%m-%d')  as changeday,
+                     CONCAT(DATE_FORMAT(changedate, '%Y-%m-%d-') ,Person.id)  as id,
+                     Person.name as name,
+                     COUNT(mtrack_change.id) as nchanges
+                    ");
+            $e->groupBy("changeday,id,name");
+            $e->orderBy('changeday desc, name asc');
+            $ret = array();
+            $e->find();
+            while($e->fetch()) {
+                $ret[] = $e->toArray();
             }
+            $this->jdata($ret);
             
-            $this->events[] = (object)$e;
             
+        }  
+        $e->whereAdd("changedate >= '$start_day 00:00:00' AND
+                      changedate < '$start_day 00:00:00' + INTERVAL 1 DAY");
+         
+        //DB_DataObject::debugLevel(1);
+        
+       
+        
+        
+        
+        
+        
+        $ar = $e->fetchAll();
+        $this->events = array();
+        //$this->hist= array();
+        $last_day  =false;
+        foreach($ar as $i=>$h) {
+            $h->cls = $i % 2 ? 'odd' : '';
+        
+            $ts = strtotime($h->changedate);
+            $day = date('D, M d Y', $ts);
+            $time = date('g:ia', $ts);
+            //print_r($ent);
+            
+            $add = $h->toArray();
             
+            if ($day !== $last_day) {
+                $add['first_of_day'] = 1;
+            }
+            $last_day = $day;
+            $p = $h->person();
+            $add['person_id_name'] = $p->name;
+            $add['person_id_email'] = $p->email;
+            $add['audit'] = $h->cachedAuditToString();
+            $add['audit_ar'] = $h->cachedAuditToJSONArray();
+            // add the underlying object..
+            $add = array_merge($add, $h->objectCached()->toArray('object_%s'));
+            
+            
+            
+            
+            
+            
+    
+            $this->events[] = $add;
+             
         }
+        $total = count($this->events);
+        $extra = !$total ? array() :  array(
+                 'metaData'  => $this->meta($e, $this->events)
+        );
+             
          
+
+        $this->jdata($this->events, $total, $extra);
     
-    }
+         
+         
     
-    function  is_repo_visible($reponame)
+    }
+     /**
+      * this is a very simple version of the main one in roo.php
+      */
+    function meta($x, $data)
     {
-        static $cache = array();
-        $me = MTrackAuth::whoami();
-        if (isset($cache[$me][$reponame])) {
-            return $cache[$me][$reponame];
+        // this is not going to work on queries where the data does not match the database def..
+        // for unknown columns we send them as stirngs..
+        $lost = 0;
+        $cols  = array_keys($data[0]);
+     
+        
+        
+        
+        
+        $options = HTML_FlexyFramework::get()->DB_DataObject;
+        //echo '<PRE>';print_r($options); exit;
+        $reader = $options["ini_{$x->_database}"] .'.reader';
+        if (!file_exists( $reader )) {
+            return;
         }
-    
-        if (ctype_digit($reponame)) {
-            $oid = "repo:$reponame";
-        } else {
-            $repo = MTrackRepo::loadByName($reponame);
-            if ($repo) {
-                $oid = "repo:$repo->repoid";
-            } else {
-                $oid = null;
+        
+        $rdata = unserialize(file_get_contents($reader));
+        
+        //echo '<PRE>';print_r($rdata);exit;
+        
+        $meta = array();
+        foreach($cols as $c ) {
+            $cc = $x->tableName().'.'.$c;
+            if (      !isset($rdata[$cc]) 
+                ||    !is_array($rdata[$cc])
+            ) {
+                
+                $meta[] = $c;
+                continue;    
             }
+            $add = $rdata[$cc];
+            $add['name'] = $c;
+            $meta[] = $add;
         }
-        if ($oid) {
-            $ok = MTrackACL::hasAnyRights($oid, array(
-            'read', 'checkout'));
-        } else {
-            $ok = false;
-        }
-        $cache[$me][$reponame] = $ok;
-        return $ok;
+        
+        
+        
+        return array(
+            'totalProperty' =>  'total',
+            'successProperty' => 'success',
+            'root' => 'data',
+            'id' => 'id',
+            'fields' => $meta
+        );
+         
+        
     }
-
 }