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)
83 if (!empty($q['timeline'])) {
84 return $this->applyFilterTimeline($q,$au, $roo);
87 if (!empty($q['_is_update_request'])) {
94 $obj = $this->objectCached();
97 // global searching on non-object...
98 // needed for timelime, but not ready yet...
101 $this->whereAdd('1=0');
105 if (!method_exists($obj, 'checkPerm')) {
106 $this->whereAdd('1=0');
110 if (!$au || (!$obj->checkPerm('S', $au) || !$obj->checkPerm('E', $au)) ) {
111 $this->whereAdd('1=0');
119 // this is the query for timelines..
120 function applyFilterTimeline($q, $au, $roo)
124 $start = empty($q['on_date']) ? date('Y-m-d') :
125 date('Y-m-d', strtotime($q['on_date']));
128 //DB_DataObject::debugLevel(1);
131 $pd = DB_DataObject::factory('ProjectDirectory');
132 $pd->whereAdd("role != ''");
133 $pids = $pd->projects($au);
135 $roo->jerr("User is not a member of any projects");
138 if (!empty($q['project_id'])) {
139 if (!in_array($q['project_id'], $pids)) {
140 $roo->jerr("Selected project not in your permitted project list.");
142 $pids = array($q['project_id']);
145 $pids = implode(',', $pids);
149 ( ontable='mtrack_ticket' AND
150 onid IN (SELECT id FROM mtrack_ticket where project_id IN ( $pids ))
153 ( ontable='mtrack_repos' AND
154 onid IN (SELECT id FROM mtrack_repos where project_id IN ( $pids ))
157 $this->orderBy('changedate DESC');
159 if (!empty($q['viewtype']) && $q['viewtype'] == 'summary') {
160 //DB_DataObject::debugLevel(1);
163 changedate >= '$start 00:00:00'
165 changedate < '$start 00:00:00' + INTERVAL 1 DAY
170 //$this->joinAdd(DB_DataObject::factory('Person'), 'LEFT');
172 DATE_FORMAT(changedate, '%Y-%m-%d') as changeday,
173 join_person_id_id.id as person_id,
174 join_person_id_id.name as person_name,
175 Projects.id as project_id,
176 mtrack_repos.shortname as repo_name,
177 mtrack_repos.id as repo_id,
179 Projects.code as project_code,
181 COUNT(mtrack_change.id) as nchanges
184 LEFT JOIN Projects ON
185 Projects.id = IF (ontable='mtrack_repos',
186 (SELECT project_id FROM mtrack_repos where onid = mtrack_repos.id ),
187 (SELECT project_id FROM mtrack_ticket where onid = mtrack_ticket.id )
189 LEFT JOIN mtrack_repos ON
190 mtrack_repos.id = IF (ontable='mtrack_repos', onid, 0)
195 $this->groupBy("changeday,
196 join_person_id_id.id,
197 join_person_id_id.name,
200 mtrack_repos.shortname,
202 $this->orderBy('project_code asc, repo_name ASC, person_name asc');
204 $roo->jdata($this->fetchAll(false,false,'toArray'));
208 $this->whereAdd("changedate >= '$start_day 00:00:00' AND
209 changedate < '$start_day 00:00:00' + INTERVAL 1 DAY");
211 //DB_DataObject::debugLevel(1);
217 // do not accept use input for this..
218 function autoJoinObject($tbl)
222 $d = DB_DataObject::Factory($tbl);
223 $ji = $d->autoJoin();
224 //echo '<PRE>';print_R($ji);
226 foreach($ji['join_names'] as $cname=>$fname) {
227 $this->selectAdd($fname . ' as ontable_id_' . $cname );
230 //$this->selectAdd($d->_query['data_select']); -- this will cause the same dataIndex...
232 LEFT JOIN {$d->tableName()} ON {$this->tableName()}.onid = {$d->tableName()}.id
235 $this->selectAs($d, 'ontable_%s');
246 static $cache = array();
247 if (isset($cache[$this->person_id])) {
248 return $cache[$this->person_id];
250 $p = DB_DataObject::factory('core_person');
251 $p->get($this->person_id);
252 $cache[$this->person_id] = $p;
257 function getCommit($repo, $hash, $runhistory=true)
259 $this->object = $repo->toIdString(); // should be repo:1
261 if ($this->find(true)) {
266 throw new Exception("Can not find revision");
268 $this->refreshHistory($repo);
269 if ( $this->find(true)) {
272 }; /// yes I know we say you should not run this twice...
278 *@param {TrackCommitHookChangeEvent} ce the change event..
280 function createFromCommit($ce, $checker, $ticket = false)
282 $rev = substr(array_pop(explode(',', $ce->rev)), 0, -1);
285 $tc->ontable = 'mtrack_repos';
286 $tc->onid = $checker->repo->id;
288 $tc->cgtype = 'COMMIT';
289 $tc->branch = $ce->branch;
290 if ($tc->count()) { // if we have already tracked this..
302 $tc->ontable = $ticket->tableName();
303 $tc->onid = $ticket->id;
304 $tc->changedate = date('Y-m-d H:i:s', $ce->ctime);
305 $tc->reason = $ce->changelog;
306 $tc->person_id = $ce->changeby_id;
308 $tcid = $tc->insert();
313 $this->ontable = 'mtrack_repos';
314 $this->onid = $checker->repo->id;
315 $this->changedate = date('Y-m-d H:i:s', $ce->ctime);
316 $this->reason = $ce->changelog;
317 $this->person_id = $ce->changeby_id;
318 $this->branch = $ce->branch;
322 // add all the chanedge files..
323 foreach($ce->fileActions as $f => $cg) {
325 $au = DB_DataObject::factory('mtrack_change_audit');
326 $au->change_id = $this->id;
327 $au->ticket_change_id = $tcid;
328 $au->fieldname = $rev;
329 $au->action = 'commit-'.$cg;
334 $this->onInsert(array(), HTML_FlexyFramework::get()->page);
336 // finnaly trigger watchers
343 // audit should contain files changed..
346 function onInsert($request,$roo)
349 // for a commit ? - ontable = repos, onid = the repo id...
350 // for a change... ontable - will be the ticked, and onid will be the ticket id..
352 $obj = $this->objectCached();
354 if ($obj->tableName() == 'mtrack_ticket') {
355 $obj = $obj->project();
358 $target = empty($this->branch) ? $obj : $obj->branchObject($this->branch);
362 $core_watch = DB_DataObject::Factory('core_watch');
366 "medium='ENDOFDAYMAIL'",
368 date('Y-m-d 23:59:59'),
369 $target->tableName(),
377 * scans repo history and imports it... ??? does not know about branches yet...
379 function refreshHistory($repo)
381 $c = DB_DataObject::factory('mtrack_changes');
382 $c->object = $repo->toIdString();
383 $c->orderBy('changedate DESC');
384 $c->whereAdd("rev != ''");
386 if ($c->find(true)) {
389 // history returns an array of scmEvents... - should actually return an array of this object...
390 $recs = $repo->history('', null, $last ? 'rev' : null, $last ? $last :null);
392 foreach($recs as $r) {
393 // see if we have a copy of it already.
394 $c = DB_DataObject::factory('mtrack_change');
395 $c->object = $repo->toIdString();
396 $c->changedate != $r->ctime;
398 $c->whereAdd("rev != ''");
399 if (!$c->find(true)) {
400 // not found, create a new one..
405 $cr->reason = $r->changelog;
406 $cr->who = $r->changeby;
408 foreach($cr->files as $f) {
409 // we could do with adding 'X' lines changed on this
410 $ca = DB_DataObject::factory('change_audit');
412 $ca->fieldname = $cr->object. ':file';
413 $ca->action = $f->status . ($f->added ? ' +' . $f->added : '') . ($f->removed ? ' -' . $f->removed : '') ;
414 $ca->value = $f->name;
425 function beginChange($object, $reason = '', $when = null)
427 // $db->beginTransaction();
428 //$this->query('BEGIN');
429 $pg = HTML_FlexyFramework::get()->page;
432 $this->person_id = $au->id;
433 $this->ontable = $object->tableName(); // string(128)
434 $this->onid = $object->id; // should use keys( really..)
435 $this->reason = $reason;
436 //$d = date_create("@" . (empty($when) ? time() : $when), new DateTimeZone('UTC'));
437 $this->changedate = date('Y-m-d H:i:s'); // everythign at server time..
438 //$d->format('Y-m-d\TH:i:s.u\Z');
439 $this->cgtype = $reason == 'Changed' ? 'CHANGE' : 'COMMENT';
441 $this->onInsert(array(), $pg);
447 if ($this->count == 0) {
448 // throw new Exception("no changes were made as part of this changeset");
450 if (self::$use_txn) {
451 //$this->query('COMMIT');
456 function addentry($fieldname, $action, $old, $value = null)
459 // final sanity check..
460 $o = DB_DataObject::factory($this->ontable);
461 $info = $o->tableColumns();
462 $far = explode(':',$fieldname);
463 $col = array_pop($far);
464 if (!isset($info[$col])) {
469 if ($info[$col] & (defined('DB_DATAOBJECT_INT') ? DB_DATAOBJECT_INT : PDO_DataObject::INT)) {
470 if (((int) $old) == ((int) $value)) {
477 $ca = DB_DataObject::factory('mtrack_change_audit');
479 'change_id' => $this->id,
480 'fieldname' => $fieldname,
493 * add('xxx:yyy:zzz' , new , old)
494 * add('xxx:yyy', new_do, old_do) // two dataobjects.. (will diff the two..
495 * add($new_do, $old_do)
499 function add($fieldname, $new, $old = false)
501 //print_r(array("ADD", $fieldname, $new, $old));
502 //echo "check: $fieldname ($old) => ($new)\n";
504 if (is_object($fieldname)) {
505 return $this->add($this->ontable.':'. $this->onid , $fieldname, $new );
507 if (is_object($new)) {
508 // should check keys() - so it does not log primary key addition.
510 $cols = array_keys($new->tableColumns());
511 $keys = $new->keys();
512 $links = $new->links();
514 foreach($cols as $k) {
515 if (in_array($k, $keys)) {
518 $alinks = isset($links[$k]) ? explode(':', $links[$k]) : false;
519 if ($alinks && $this->tablename() == array_shift($alinks)) {
522 $ret += $this->add($fieldname .':'. $k, $new->{$k}, !$old ? '' : $old->{$k});
531 if ($old !== false) {
532 return $this->addentry($fieldname, 'set', $old, $new);
535 if ($new === false) {
536 return $this->addentry($fieldname, 'deleted', $old, $new);
539 return $this->addentry($fieldname, 'changed', $old, $new);
548 function cachedAudit()
550 static $cache = array();
552 if (!isset($cache[$this->id])) {
553 $cache[$this->id]= $this->audit();
555 return $cache[$this->id];
557 function cachedAuditToJSONArray()
560 $ar = $this->cachedAudit();
566 $add = $a->toJSONArray($this);
577 function relatedCommits()
579 if (empty($this->rev)) {
582 // occurs on a ticket... when listing...
583 $mc = DB_DataObject::Factory('mtrack_change');
584 $mc->rev = $this->rev;
585 $mc->ontable = 'mtrack_repos';
586 if (!$mc->find(true)) {
589 // got the repo.. and the commit..
591 $repo = $mc->objectCached();
593 return $repo->historyToSummary(
594 $repo->impl()->history('.', 1, 'rev', $this->rev),
606 $a = DB_DataObject::Factory('mtrack_change_audit');
608 {$a->tableName()}.change_id = {$this->id} OR
609 {$a->tableName()}.ticket_change_id = {$this->id}
611 // var_dump($a->find());exit;
613 return $a->fetchAll();
616 function changesSince($object,$since)
618 $q = DB_DataObject::factory('mtrack_change');
619 $q->onid = $object->id;
620 $q->ontable = $object->tableName();
621 $q->whereAdd("changedate > '$since'");
622 $q->orderBy('changedate ASC');
624 // we are going to end up with a list of objects that have changed.
625 // eg. a ticket or a repo..
627 return $q->fetchAll();
633 function cachedAuditToString()
637 $ar = $this->cachedAudit();
638 foreach($ar as $audit) {
639 $add = $audit->toAuditString($this);
640 if ($add === false) {
646 return implode("\n",$body);
649 function toRooArray()
651 $ret = $this->toArray();
652 $ret['audit'] = $this->cachedAuditToJSONArray();
653 $ret['commit'] = $this->relatedCommits();
659 function beforeInsert($req, $roo)
661 // originally this was blocked - we are going to try and use it now???
662 if (empty($roo->authUser)) {
663 $roo->jerr("Invalid user inserting");
665 $this->person_id = $roo->authUser->id;
668 $obj = $this->objectCached();
670 if (!$obj || !$obj->checkPerm('E', $roo->authUser)) {
671 $roo->jerr("Invalid object / permission denied");
674 $this->changedate = $this->sqlValue('NOW()');
677 function beforeUpdate($old,$req,$roo)
679 $roo->jerr("update not allowed by user interface.");
684 function changedate($format)
686 return date($format, strtotime($this->changedate));
688 function postListFilter($data, $authUser, $q)
690 if (empty($q['ontable']) || empty($q['onid']) || $q['ontable'] != 'mtrack_ticket') {
693 //DB_DataObject::debugLevel(1);
694 // look up in the accounting system what time was spent on the ticket..
695 $ie = DB_DataObject::factory('cash_invoice_entry');
696 $ie->ticket_id = $q['onid'];
697 $ie->orderBy('entered_dt ASC'); // doesnt matter really - we will sort it..
701 cash_invoice_entry.id * -1 as id,
702 staff_id as person_id,
703 join_staff_id_id.name as person_id_name,
704 entered_dt as changedate,
705 'TIMESHEET' as cgtype ,
706 CONCAT(qtyvalue , ' Hours worked: ', cash_invoice_entry.description) as reason,
710 $hours = $ie->fetchAll(false,false,'toArray');
712 $ret = array_merge($data , $hours);
714 usort($ret, function ($a,$b) use($q) {
715 $aa = $a['changedate'];
716 $bb = $b['changedate'];
720 if ($q['dir'] == 'ASC') {
721 return strtotime($aa) < strtotime($bb) ? -1 : 1;
723 return strtotime($aa) > strtotime($bb) ? -1 : 1;