Happy 2009! Updating copyright years.
[bugdar.git] / includes / class_logging.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar
5 || # Copyright (c)2004-2009 Blue Static
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version 2 of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 /*
23 A USEFUL QUERY:
24
25 DELETE FROM history WHERE field IN ('lastposttime', 'lastpostby', 'hiddenlastposttime', 'hiddenlastpostby')
26
27 */
28
29 /**
30 * Bug Change Logging
31 *
32 * This class is used to record changes in a bug's fields, comments, and
33 * attachments.
34 *
35 * @author Blue Static
36 * @copyright Copyright (c)2004 - 2009, Blue Static
37 * @package Bugdar
38 *
39 */
40 class Logging
41 {
42 /**
43 * Bug ID
44 * @var integer
45 */
46 private $bugid = 0;
47
48 /**
49 * Attachment ID
50 * @var integer
51 */
52 private $attachmentid = 0;
53
54 /**
55 * Comment ID
56 * @var integer
57 */
58 private $commentid = 0;
59
60 /**
61 * The original data to compare against
62 * @var array
63 */
64 private $original = array();
65
66 /**
67 * Modified data
68 * @var array
69 */
70 private $modified = array();
71
72 /**
73 * Compared/diff'd data
74 * @var array
75 */
76 private $compared = array();
77
78 /**
79 * Sets the bug ID for the current logging instance
80 *
81 * @param integer New bug ID
82 */
83 public function setBugId($id)
84 {
85 $this->bugid = BSApp::$input->clean($id, TYPE_UINT);
86 }
87
88 /**
89 * Sets the attachment ID for the current logging instance
90 *
91 * @param integer New attachment ID
92 */
93 public function setAttachmentId($id)
94 {
95 $this->attachmentid = BSApp::$input->clean($id, TYPE_UINT);
96 }
97
98 /**
99 * Sets the current comment ID to be logged
100 *
101 * @param integer New comment ID
102 */
103 public function setCommentId($id)
104 {
105 $this->commentid = BSApp::$input->clean($id, TYPE_UINT);
106 }
107
108 /**
109 * Assigns data into the $this->original or $this->modified array based
110 * on the passed arrays of information and the fields to add (and what
111 * name to add them under), and any prefix
112 *
113 * @param bool TRUE for original, FALSE for modified
114 * @param array Data array
115 * @param array List of fields in the data array to add; in format of array('field name' => 'display name', 'display name 2', 'display name 3')
116 * @param bool If TRUE, then the list of fields is used to exclude, not include
117 * @param string Field prefix
118 */
119 public function addData($orig, $data, $fields, $exclude = false, $prefix = '')
120 {
121 $array = ($orig ? 'original' : 'modified');
122 $prefix .= '.';
123
124 if ($exclude == false)
125 {
126 foreach ($fields as $fname => $fdisplay)
127 {
128 if (is_numeric($fname))
129 {
130 $fname = $fdisplay;
131 }
132
133 $this->{$array}["$prefix$fdisplay"] = array('name' => $fname, 'value' => $data["$fname"]);
134 }
135 }
136 else
137 {
138 foreach ($data as $fname => $value)
139 {
140 if (!in_array($fname, $fields))
141 {
142 $this->{$array}["$prefix$fname"] = array('name' => $fname, 'value' => $value);
143 }
144 }
145 }
146 }
147
148 /**
149 * Populates the $this->compared array as a diff between the original
150 * and modified data. This is then used to create the databse queries.
151 */
152 private function _compareArrays()
153 {
154 foreach ($this->modified AS $key => $value)
155 {
156 if ($this->original["$key"] != $value && !($value['value'] == '' && $this->original["$key"]['value'] == '0') && !($this->original["$key"]['value'] == '' && $value['value'] == '0'))
157 {
158 $this->compared["$key"] = array('old' => $this->original["$key"]['value'], 'new' => $this->modified["$key"]['value']);
159 }
160 }
161 }
162
163 /**
164 * Runs $this->_compareArrays() and then takes the result and prepares
165 * it for insertion into the history database.
166 */
167 public function updateHistory()
168 {
169 $this->_compareArrays();
170
171 foreach ($this->compared as $field => $values)
172 {
173 BSApp::$db->query("
174 INSERT INTO " . TABLE_PREFIX . "history
175 (bugid, attachmentid, commentid, dateline, userid, field, original, changed)
176 VALUES
177 (" . BSApp::$input->clean($this->bugid, TYPE_UINT) . ", " . BSApp::$input->clean($this->attachmentid, TYPE_UINT) . ",
178 " . BSApp::$input->clean($this->commentid, TYPE_UINT) . ", " . TIMENOW . ", " . bugdar::$userinfo['userid'] . ",
179 '" . BSApp::$db->escapeString($field) . "', '" . BSApp::$db->escapeString($values['old']) . "',
180 '" . BSApp::$db->escapeString($values['new']) . "'
181 )
182 ");
183 }
184 }
185
186 /**
187 * Returns an array of the fields commonly ignored
188 *
189 * @return array Fields ignored in logging
190 */
191 public function getCommonFields()
192 {
193 return array(
194 'bugid',
195 'lastposttime',
196 'lastpostby',
197 'lastpostbyname',
198 'hiddenlastposttime',
199 'hiddenlastpostby',
200 'hiddenlastpostbyname'
201 );
202 }
203 }
204
205 ?>