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
338 if (!$ticket && preg_match('/_T([0-9]+)_/', $ce->branch, $matches)) {
339 $ticket = DB_DataObject::factory('mtrack_ticket');
340 if (!$ticket->get($matches[1])) {
345 $ts = DB_DataObject::Factory('cash_invoice_entry');
346 $ts->updateFromCommit($ce, $ticket);
354 // audit should contain files changed..
357 function onInsert($request,$roo)
360 // for a commit ? - ontable = repos, onid = the repo id...
361 // for a change... ontable - will be the ticked, and onid will be the ticket id..
363 $obj = $this->objectCached();
365 if ($obj->tableName() == 'mtrack_ticket') {
366 $obj = $obj->project();
369 $target = empty($this->branch) ? $obj : $obj->branchObject($this->branch);
373 $core_watch = DB_DataObject::Factory('core_watch');
377 "medium='ENDOFDAYMAIL'",
379 date('Y-m-d 23:59:59'),
380 $target->tableName(),
388 * scans repo history and imports it... ??? does not know about branches yet...
390 function refreshHistory($repo)
392 $c = DB_DataObject::factory('mtrack_changes');
393 $c->object = $repo->toIdString();
394 $c->orderBy('changedate DESC');
395 $c->whereAdd("rev != ''");
397 if ($c->find(true)) {
400 // history returns an array of scmEvents... - should actually return an array of this object...
401 $recs = $repo->history('', null, $last ? 'rev' : null, $last ? $last :null);
403 foreach($recs as $r) {
404 // see if we have a copy of it already.
405 $c = DB_DataObject::factory('mtrack_change');
406 $c->object = $repo->toIdString();
407 $c->changedate != $r->ctime;
409 $c->whereAdd("rev != ''");
410 if (!$c->find(true)) {
411 // not found, create a new one..
416 $cr->reason = $r->changelog;
417 $cr->who = $r->changeby;
419 foreach($cr->files as $f) {
420 // we could do with adding 'X' lines changed on this
421 $ca = DB_DataObject::factory('change_audit');
423 $ca->fieldname = $cr->object. ':file';
424 $ca->action = $f->status . ($f->added ? ' +' . $f->added : '') . ($f->removed ? ' -' . $f->removed : '') ;
425 $ca->value = $f->name;
436 function beginChange($object, $reason = '', $when = null)
438 // $db->beginTransaction();
439 //$this->query('BEGIN');
440 $pg = HTML_FlexyFramework::get()->page;
443 $this->person_id = $au->id;
444 $this->ontable = $object->tableName(); // string(128)
445 $this->onid = $object->id; // should use keys( really..)
446 $this->reason = $reason;
447 //$d = date_create("@" . (empty($when) ? time() : $when), new DateTimeZone('UTC'));
448 $this->changedate = date('Y-m-d H:i:s'); // everythign at server time..
449 //$d->format('Y-m-d\TH:i:s.u\Z');
450 $this->cgtype = $reason == 'Changed' ? 'CHANGE' : 'COMMENT';
452 $this->onInsert(array(), $pg);
458 if ($this->count == 0) {
459 // throw new Exception("no changes were made as part of this changeset");
461 if (self::$use_txn) {
462 //$this->query('COMMIT');
467 function addentry($fieldname, $action, $old, $value = null)
470 // final sanity check..
471 $o = DB_DataObject::factory($this->ontable);
472 $info = $o->tableColumns();
473 $far = explode(':',$fieldname);
474 $col = array_pop($far);
475 if (!isset($info[$col])) {
480 if ($info[$col] & (defined('DB_DATAOBJECT_INT') ? DB_DATAOBJECT_INT : PDO_DataObject::INT)) {
481 if (((int) $old) == ((int) $value)) {
488 $ca = DB_DataObject::factory('mtrack_change_audit');
490 'change_id' => $this->id,
491 'fieldname' => $fieldname,
504 * add('xxx:yyy:zzz' , new , old)
505 * add('xxx:yyy', new_do, old_do) // two dataobjects.. (will diff the two..
506 * add($new_do, $old_do)
510 function add($fieldname, $new, $old = false)
512 //print_r(array("ADD", $fieldname, $new, $old));
513 //echo "check: $fieldname ($old) => ($new)\n";
515 if (is_object($fieldname)) {
516 return $this->add($this->ontable.':'. $this->onid , $fieldname, $new );
518 if (is_object($new)) {
519 // should check keys() - so it does not log primary key addition.
521 $cols = array_keys($new->tableColumns());
522 $keys = $new->keys();
523 $links = $new->links();
525 foreach($cols as $k) {
526 if (in_array($k, $keys)) {
529 $alinks = isset($links[$k]) ? explode(':', $links[$k]) : false;
530 if ($alinks && $this->tablename() == array_shift($alinks)) {
533 if (!$old && !isset($new->{$k})) {
537 $ret += $this->add($fieldname .':'. $k, $new->{$k}, !$old ? '' : $old->{$k});
546 if ($old !== false) {
547 return $this->addentry($fieldname, 'set', $old, $new);
550 if ($new === false) {
551 return $this->addentry($fieldname, 'deleted', $old, $new);
554 return $this->addentry($fieldname, 'changed', $old, $new);
563 function cachedAudit()
565 static $cache = array();
567 if (!isset($cache[$this->id])) {
568 $cache[$this->id]= $this->audit();
570 return $cache[$this->id];
572 function cachedAuditToJSONArray()
575 $ar = $this->cachedAudit();
581 $add = $a->toJSONArray($this);
592 function relatedCommits()
594 if (empty($this->rev)) {
597 // occurs on a ticket... when listing...
598 $mc = DB_DataObject::Factory('mtrack_change');
599 $mc->rev = $this->rev;
600 $mc->ontable = 'mtrack_repos';
601 if (!$mc->find(true)) {
604 // got the repo.. and the commit..
606 $repo = $mc->objectCached();
608 return $repo->historyToSummary(
609 $repo->impl()->history('.', 1, 'rev', $this->rev),
621 $a = DB_DataObject::Factory('mtrack_change_audit');
623 {$a->tableName()}.change_id = {$this->id} OR
624 {$a->tableName()}.ticket_change_id = {$this->id}
626 // var_dump($a->find());exit;
628 return $a->fetchAll();
631 function changesSince($object,$since)
633 $q = DB_DataObject::factory('mtrack_change');
634 $q->onid = $object->id;
635 $q->ontable = $object->tableName();
636 $q->whereAdd("changedate > '$since'");
637 $q->orderBy('changedate ASC');
639 // we are going to end up with a list of objects that have changed.
640 // eg. a ticket or a repo..
642 return $q->fetchAll();
648 function cachedAuditToString()
652 $ar = $this->cachedAudit();
653 foreach($ar as $audit) {
654 $add = $audit->toAuditString($this);
655 if ($add === false) {
661 return implode("\n",$body);
664 function toRooArray()
666 $ret = $this->toArray();
667 $ret['audit'] = $this->cachedAuditToJSONArray();
668 $ret['commit'] = $this->relatedCommits();
674 function beforeInsert($req, $roo)
676 // originally this was blocked - we are going to try and use it now???
677 if (empty($roo->authUser)) {
678 $roo->jerr("Invalid user inserting");
680 $this->person_id = $roo->authUser->id;
683 $obj = $this->objectCached();
685 if (!$obj || !$obj->checkPerm('E', $roo->authUser)) {
686 $roo->jerr("Invalid object / permission denied");
689 $this->changedate = $this->sqlValue('NOW()');
692 function beforeUpdate($old,$req,$roo)
694 $roo->jerr("update not allowed by user interface.");
699 function changedate($format)
701 return date($format, strtotime($this->changedate));
703 function postListFilter($data, $authUser, $q)
705 if (empty($q['ontable']) || empty($q['onid']) || $q['ontable'] != 'mtrack_ticket') {
708 //DB_DataObject::debugLevel(1);
709 // look up in the accounting system what time was spent on the ticket..
710 $ie = DB_DataObject::factory('cash_invoice_entry');
711 $ie->ticket_id = $q['onid'];
712 $ie->orderBy('entered_dt ASC'); // doesnt matter really - we will sort it..
716 cash_invoice_entry.id * -1 as id,
717 staff_id as person_id,
718 join_staff_id_id.name as person_id_name,
719 entered_dt as changedate,
720 'TIMESHEET' as cgtype ,
721 CONCAT(qtyvalue , ' Hours worked: ', cash_invoice_entry.description) as reason,
725 $hours = $ie->fetchAll(false,false,'toArray');
727 $ret = array_merge($data , $hours);
729 usort($ret, function ($a,$b) use($q) {
730 $aa = $a['changedate'];
731 $bb = $b['changedate'];
735 if ($q['dir'] == 'ASC') {
736 return strtotime($aa) < strtotime($bb) ? -1 : 1;
738 return strtotime($aa) > strtotime($bb) ? -1 : 1;