3 * Table Definition for mtrack_change
5 class_exists('DB_DataObject') ? '' : require_once 'DB/DataObject.php';
7 class Pman_MTrack_DataObjects_Mtrack_change extends DB_DataObject
10 /* the code below is auto generated do not remove the above tag */
12 public $__table = 'mtrack_change'; // table name
13 public $id; // int(11) not_null primary_key auto_increment
14 public $person_id; // int(11)
15 public $ontable; // string(128)
16 public $onid; // int(11) not_null
17 public $changedate; // datetime(19) not_null binary
18 public $reason; // blob(65535) blob
21 /* the code above is auto generated do not remove the tag below */
25 function objectCached()
28 if (empty($this->ontable) || empty($this->onid)) {
32 if (isset($cache[$this->ontable.':'. $this->onid])) {
33 $cache[$this->ontable.':'. $this->onid];
35 $o = DB_DataObject::factory($this->ontable);
37 $cache[$this->ontable.':'. $this->onid] = $o;
43 //checkPerm('S'/'E'/'A', $authuser) - can
44 function checkPerm($perm, $au)
47 if ($au && $au->company()->comptype == 'OWNER') {
48 // owner can do anything..????
52 if ($perm == 'E' || $perm == 'D' ) { // Edit and delete ..not allowed..
56 if (!$au && $perm != 'S') {
57 // non-authenticated users can only list stuff..
62 //DB_DataObject::debugLevel(1);
63 $obj = $this->objectCached();
67 if (!method_exists($obj, 'checkPerm')) {
72 return $obj->checkPerm($perm, $au);
79 function applyFilters($q, $au, $roo)
82 if (!empty($q['timeline'])) {
83 return $this->applyFilterTimeline($q,$au, $roo);
86 $obj = $this->objectCached();
88 // global searching on non-object...
89 // needed for timelime, but not ready yet...
92 $this->whereAdd('1=0');
96 if (!method_exists($obj, 'checkPerm')) {
97 $this->whereAdd('1=0');
101 if (!$au && !$obj->checkPerm('S', $au)) {
102 $this->whereAdd('1=0');
110 // this is the query for timelines..
111 function applyFilterTimeline($q, $au, $roo)
115 $start = empty($q['on_date']) ? date('Y-m-d') :
116 date('Y-m-d', strtotime($q['on_date']));
119 //DB_DataObject::debugLevel(1);
122 $pd = DB_DataObject::factory('ProjectDirectory');
123 $pd->whereAdd("role != ''");
124 $pids = $pd->projects($au);
126 $roo->jerr("User is not a member of any projects");
129 if (!empty($q['project_id'])) {
130 if (!in_array($q['project_id'], $pids)) {
131 $roo->jerr("Selected project not in your permitted project list.");
133 $pids = array($q['project_id']);
136 $pids = implode(',', $pids);
140 ( ontable='mtrack_ticket' AND
141 onid IN (SELECT id FROM mtrack_ticket where project_id IN ( $pids ))
144 ( ontable='mtrack_repos' AND
145 onid IN (SELECT id FROM mtrack_repos where project_id IN ( $pids ))
148 $this->orderBy('changedate DESC');
150 if (!empty($q['viewtype']) && $q['viewtype'] == 'summary') {
151 //DB_DataObject::debugLevel(1);
154 changedate >= '$start 00:00:00'
156 changedate < '$start 00:00:00' + INTERVAL 1 DAY
161 //$this->joinAdd(DB_DataObject::factory('Person'), 'LEFT');
163 DATE_FORMAT(changedate, '%Y-%m-%d') as changeday,
164 join_person_id_id.id as person_id,
165 join_person_id_id.name as person_name,
166 Projects.id as project_id,
167 mtrack_repos.shortname as repo_name,
168 mtrack_repos.id as repo_id,
170 Projects.code as project_code,
172 COUNT(mtrack_change.id) as nchanges
175 LEFT JOIN Projects ON
176 Projects.id = IF (ontable='mtrack_repos',
177 (SELECT project_id FROM mtrack_repos where onid = mtrack_repos.id ),
178 (SELECT project_id FROM mtrack_ticket where onid = mtrack_ticket.id )
180 LEFT JOIN mtrack_repos ON
181 mtrack_repos.id = IF (ontable='mtrack_repos', onid, 0)
186 $this->groupBy("changeday,
187 join_person_id_id.id,
188 join_person_id_id.name,
191 mtrack_repos.shortname,
193 $this->orderBy('project_code asc, repo_name ASC, person_name asc');
195 $roo->jdata($this->fetchAll(false,false,'toArray'));
199 $this->whereAdd("changedate >= '$start_day 00:00:00' AND
200 changedate < '$start_day 00:00:00' + INTERVAL 1 DAY");
202 //DB_DataObject::debugLevel(1);
208 // do not accept use input for this..
209 function autoJoinObject($tbl)
213 $d = DB_DataObject::Factory($tbl);
214 $ji = $d->autoJoin();
215 //echo '<PRE>';print_R($ji);
217 foreach($ji['join_names'] as $cname=>$fname) {
218 $this->selectAdd($fname . ' as ontable_id_' . $cname );
221 //$this->selectAdd($d->_query['data_select']); -- this will cause the same dataIndex...
223 LEFT JOIN {$d->tableName()} ON {$this->tableName()}.onid = {$d->tableName()}.id
226 $this->selectAs($d, 'ontable_%s');
237 static $cache = array();
238 if (isset($cache[$this->person_id])) {
239 return $cache[$this->person_id];
241 $p = DB_DataObject::factory('core_person');
242 $p->get($this->person_id);
243 $cache[$this->person_id] = $p;
248 function getCommit($repo, $hash, $runhistory=true)
250 $this->object = $repo->toIdString(); // should be repo:1
252 if ($this->find(true)) {
257 throw new Exception("Can not find revision");
259 $this->refreshHistory($repo);
260 if ( $this->find(true)) {
263 }; /// yes I know we say you should not run this twice...
269 *@param {TrackCommitHookChangeEvent} ce the change event..
271 function createFromCommit($ce, $checker, $ticket = false)
273 $rev = substr(array_pop(explode(',', $ce->rev)), 0, -1);
276 $tc->ontable = 'mtrack_repos';
277 $tc->onid = $checker->repo->id;
279 $tc->branch = $ce->branch;
280 if ($tc->count()) { // if we have already tracked this..
292 $tc->ontable = $ticket->tableName();
293 $tc->onid = $ticket->id;
294 $tc->changedate = date('Y-m-d H:i:s', $ce->ctime);
295 $tc->reason = $ce->changelog;
296 $tc->person_id = $ce->changeby_id;
298 $tcid = $tc->insert();
305 $this->ontable = 'mtrack_repos';
306 $this->onid = $checker->repo->id;
307 $this->changedate = date('Y-m-d H:i:s', $ce->ctime);
308 $this->reason = $ce->changelog;
309 $this->person_id = $ce->changeby_id;
310 $this->branch = $ce->branch;
314 // add all the chanedge files..
315 foreach($ce->fileActions as $f => $cg) {
317 $au = DB_DataObject::factory('mtrack_change_audit');
318 $au->change_id = $this->id;
319 $au->ticket_change_id = $tcid;
320 $au->fieldname = $rev;
321 $au->action = 'commit-'.$cg;
326 $this->onInsert(array(), HTML_FlexyFramework::get()->page);
328 // finnaly trigger watchers
335 // audit should contain files changed..
338 function onInsert($request,$roo)
341 // for a commit ? - ontable = repos, onid = the repo id...
342 // for a change... ontable - will be the ticked, and onid will be the ticket id..
344 $obj = $this->objectCached();
346 if ($obj->tableName() == 'mtrack_ticket') {
347 $obj = $obj->project();
349 $obj = empty($this->branch) ? $obj : $obj->branchObject($this->branch);
353 $core_watch = DB_DataObject::Factory('core_watch');
357 "medium='ENDOFDAYMAIL'",
359 date('Y-m-d 23:59:59')
366 * scans repo history and imports it... ??? does not know about branches yet...
368 function refreshHistory($repo)
370 $c = DB_DataObject::factory('mtrack_changes');
371 $c->object = $repo->toIdString();
372 $c->orderBy('changedate DESC');
373 $c->whereAdd("rev != ''");
375 if ($c->find(true)) {
378 // history returns an array of scmEvents... - should actually return an array of this object...
379 $recs = $repo->history('', null, $last ? 'rev' : null, $last ? $last :null);
381 foreach($recs as $r) {
382 // see if we have a copy of it already.
383 $c = DB_DataObject::factory('mtrack_change');
384 $c->object = $repo->toIdString();
385 $c->changedate != $r->ctime;
387 $c->whereAdd("rev != ''");
388 if (!$c->find(true)) {
389 // not found, create a new one..
394 $cr->reason = $r->changelog;
395 $cr->who = $r->changeby;
397 foreach($cr->files as $f) {
398 // we could do with adding 'X' lines changed on this
399 $ca = DB_DataObject::factory('change_audit');
401 $ca->fieldname = $cr->object. ':file';
402 $ca->action = $f->status . ($f->added ? ' +' . $f->added : '') . ($f->removed ? ' -' . $f->removed : '') ;
403 $ca->value = $f->name;
414 function beginChange($object, $reason = '', $when = null)
416 // $db->beginTransaction();
417 //$this->query('BEGIN');
418 $pg = HTML_FlexyFramework::get()->page;
421 $this->person_id = $au->id;
422 $this->ontable = $object->tableName(); // string(128)
423 $this->onid = $object->id; // should use keys( really..)
424 $this->reason = $reason;
425 //$d = date_create("@" . (empty($when) ? time() : $when), new DateTimeZone('UTC'));
426 $this->changedate = date('Y-m-d H:i:s'); // everythign at server time..
427 //$d->format('Y-m-d\TH:i:s.u\Z');
429 $this->onInsert(array(), $pg);
435 if ($this->count == 0) {
436 // throw new Exception("no changes were made as part of this changeset");
438 if (self::$use_txn) {
439 //$this->query('COMMIT');
444 function addentry($fieldname, $action, $old, $value = null)
447 // final sanity check..
448 $o = DB_DataObject::factory($this->ontable);
450 $col = array_pop(explode(':',$fieldname));
451 if (!isset($info[$col])) {
454 if ($info[$col] & DB_DATAOBJECT_INT) {
455 if (((int) $old) == ((int) $value)) {
462 $ca = DB_DataObject::factory('mtrack_change_audit');
464 'change_id' => $this->id,
465 'fieldname' => $fieldname,
478 * add('xxx:yyy:zzz' , new , old)
479 * add('xxx:yyy', new_do, old_do) // two dataobjects.. (will diff the two..
480 * add($new_do, $old_do)
484 function add($fieldname, $new, $old = false)
486 //print_r(array("ADD", $fieldname, $new, $old));
487 //echo "check: $fieldname ($old) => ($new)\n";
489 if (is_object($fieldname)) {
490 return $this->add($this->ontable.':'. $this->onid , $fieldname, $new );
492 if (is_object($new)) {
493 // should check keys() - so it does not log primary key addition.
495 $cols = array_keys($new->table());
496 $keys = $new->keys();
497 $links = $new->links();
499 foreach($cols as $k) {
500 if (in_array($k, $keys)) {
503 if (isset($links[$k]) && $this->tablename() == array_shift(explode(':', $links[$k]))) {
506 $ret += $this->add($fieldname .':'. $k, $new->{$k}, !$old ? '' : $old->{$k});
515 if (!$old === false) {
516 return $this->addentry($fieldname, 'set', $old, $new);
519 if ($new === false) {
520 return $this->addentry($fieldname, 'deleted', $old, $new);
523 return $this->addentry($fieldname, 'changed', $old, $new);
532 function cachedAudit()
534 static $cache = array();
536 if (!isset($cache[$this->id])) {
537 $cache[$this->id]= $this->audit();
539 return $cache[$this->id];
541 function cachedAuditToJSONArray()
544 $ar = $this->cachedAudit();
550 $add = $a->toJSONArray($this);
561 function relatedCommits()
563 if (empty($this->rev)) {
566 // occurs on a ticket... when listing...
567 $mc = DB_DataObject::Factory('mtrack_change');
568 $mc->rev = $this->rev;
569 $mc->ontable = 'mtrack_repos';
570 if (!$mc->find(true)) {
573 // got the repo.. and the commit..
575 $repo = $mc->objectCached();
577 return $repo->historyToSummary(
578 $repo->impl()->history('.', 1, 'rev', $this->rev),
590 $a = DB_DataObject::Factory('mtrack_change_audit');
592 {$a->tableName()}.change_id = {$this->id} OR
593 {$a->tableName()}.ticket_change_id = {$this->id}
595 // var_dump($a->find());exit;
597 return $a->fetchAll();
600 function changesSince($object,$since)
602 $q = DB_DataObject::factory('mtrack_change');
603 $q->onid = $object->id;
604 $q->ontable = $object->tableName();
605 $q->whereAdd("changedate > '$since'");
606 $q->orderBy('changedate ASC');
608 // we are going to end up with a list of objects that have changed.
609 // eg. a ticket or a repo..
611 return $q->fetchAll();
617 function cachedAuditToString()
621 $ar = $this->cachedAudit();
622 foreach($ar as $audit) {
623 $add = $audit->toAuditString($this);
624 if ($add === false) {
630 return implode("\n",$body);
633 function toRooArray()
635 $ret = $this->toArray();
636 $ret['audit'] = $this->cachedAuditToJSONArray();
637 $ret['commit'] = $this->relatedCommits();
643 function beforeInsert($req, $roo)
646 $roo->jerr("no direct access");
648 if (empty($roo->authUser)) {
649 $roo->jerr("Invalid user inserting");
651 $this->person_id = $roo->authUser->id;
653 $obj = $this->objectCached();
654 if (!$obj || !$obj->checkPerm('E', $roo->authUser)) {
655 $this->jerr("Invalid object / permission denied");
658 $this->changedate = $this->sqlValue('NOW()');
662 function beforeUpdate($old,$req,$roo)
664 $roo->jerr("update not allowed by user interface.");
669 function changedate($format)
671 return date($format, strtotime($this->changedate));
673 function postListFilter($data, $authUser, $q)
675 if (empty($q['ontable']) || empty($q['onid']) || $q['ontable'] != 'mtrack_ticket') {
678 //DB_DataObject::debugLevel(1);
679 // look up in the accounting system what time was spent on the ticket..
680 $ie = DB_DataObject::factory('cash_invoice_entry');
681 $ie->ticket_id = $q['onid'];
682 $ie->orderBy('entered_dt ASC'); // doesnt matter really - we will sort it..
686 cash_invoice_entry.id * -1 as id,
687 staff_id as person_id,
688 join_staff_id_id.name as person_id_name,
689 entered_dt as changedate,
690 CONCAT(qtyvalue , ' Hours worked: ', cash_invoice_entry.description) as reason,
694 $hours = $ie->fetchAll(false,false,'toArray');
696 $ret = array_merge($data , $hours);
698 usort($ret, function($a,$b) {
699 $aa = $a['changedate'];
700 $bb = $b['changedate'];
704 return strtotime($aa) < strtotime($bb) ? -1 : 1;