import
[web.mtrack] / inc / changeset.php
1 <?php # vim:ts=2:sw=2:et:
2 /* For licensing and copyright terms, see the file named LICENSE */
3
4 class MTrackChangeset {
5   public $cid = null;
6   public $who = null;
7   public $object = null;
8   public $reason = null;
9   public $when = null;
10   private $count = 0;
11
12   /* used by the import script to allow batching */
13   static $use_txn = true;
14
15   static function get($cid) {
16     foreach (MTrackDB::q('select * from changes where cid = ?', $cid)
17         ->fetchAll() as $row) {
18       $CS = new MTrackChangeset;
19       $CS->cid = $cid;
20       $CS->who = $row['who'];
21       $CS->object = $row['object'];
22       $CS->reason = $row['reason'];
23       $CS->when = $row['changedate'];
24       return $CS;
25     }
26     throw new Exception("invalid changeset id $cid");
27   }
28
29   static function begin($object, $reason = '', $when = null) {
30     $CS = new MTrackChangeset;
31
32     $db = MTrackDB::get();
33     if (self::$use_txn) {
34       $db->beginTransaction();
35     }
36
37     $CS->who = MTrackAuth::whoami();
38     $CS->object = $object;
39     $CS->reason = $reason;
40
41     if ($when === null) {
42       $CS->when = MTrackDB::unixtime(time());
43       $q = MTrackDB::q(
44         "INSERT INTO changes (who, object, reason, changedate)
45           values (?,?,?,?)",
46         $CS->who, $CS->object, $CS->reason, $CS->when);
47     } else {
48       $CS->when = MTrackDB::unixtime($when);
49       $q = MTrackDB::q(
50         "INSERT INTO changes (who, object, reason, changedate)
51         values (?,?,?,?)",
52         $CS->who, $CS->object, $CS->reason, $CS->when);
53     }
54
55     $CS->cid = MTrackDB::lastInsertId('changes', 'cid');
56
57     return $CS;
58   }
59
60   function commit()
61   {
62     if ($this->count == 0) {
63 //      throw new Exception("no changes were made as part of this changeset");
64     }
65     if (self::$use_txn) {
66       $db = MTrackDB::get();
67       $db->commit();
68     }
69   }
70
71   function addentry($fieldname, $action, $old, $value = null)
72   {
73     MTrackDB::q("INSERT INTO change_audit 
74       (cid, fieldname, action, oldvalue, value)
75       VALUES (?, ?, ?, ?, ?)",
76       $this->cid, $fieldname, $action, $old, $value);
77     $this->count++;
78   }
79
80   function add($fieldname, $old, $new)
81   {
82     if ($old == $new) {
83       return;
84     }
85     if (!strlen($old)) {
86       $this->addentry($fieldname, 'set', $old, $new);
87       return;
88     }
89     if (!strlen($new)) {
90       $this->addentry($fieldname, 'deleted', $old, $new);
91       return;
92     }
93     $this->addentry($fieldname, 'changed', $old, $new);
94   }
95
96   function setObject($object)
97   {
98     $this->object = $object;
99     MTrackDB::q('update changes set object = ? where cid = ?',
100       $this->object, $this->cid);
101   }
102
103   function setReason($reason)
104   {
105     $this->reason = $reason;
106     MTrackDB::q('update changes set reason = ? where cid = ?',
107       $this->reason, $this->cid);
108   }
109
110 }