Update class_logging.php
[bugdar.git] / includes / class_logging.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar
5 || # Copyright ©2002-2007 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 ©2002 - 2007, Blue Static
37 * @version $Revision$
38 * @package Bugdar
39 *
40 */
41 class Logging
42 {
43 /**
44 * Bug ID
45 * @var integer
46 */
47 private $bugid = 0;
48
49 /**
50 * Attachment ID
51 * @var integer
52 */
53 private $attachmentid = 0;
54
55 /**
56 * Comment ID
57 * @var integer
58 */
59 private $commentid = 0;
60
61 /**
62 * The original data to compare against
63 * @var array
64 */
65 private $original = array();
66
67 /**
68 * Modified data
69 * @var array
70 */
71 private $modified = array();
72
73 /**
74 * Compared/diff'd data
75 * @var array
76 */
77 private $compared = array();
78
79 /**
80 * Sets the bug ID for the current logging instance
81 *
82 * @param integer New bug ID
83 */
84 public function setBugId($id)
85 {
86 $this->bugid = BSApp::$input->clean($id, TYPE_UINT);
87 }
88
89 /**
90 * Sets the attachment ID for the current logging instance
91 *
92 * @param integer New attachment ID
93 */
94 public function setAttachmentId($id)
95 {
96 $this->attachmentid = BSApp::$input->clean($id, TYPE_UINT);
97 }
98
99 /**
100 * Sets the current comment ID to be logged
101 *
102 * @param integer New comment ID
103 */
104 public function setCommentId($id)
105 {
106 $this->commentid = BSApp::$input->clean($id, TYPE_UINT);
107 }
108
109 /**
110 * Assigns data into the $this->original or $this->modified array based
111 * on the passed arrays of information and the fields to add (and what
112 * name to add them under), and any prefix
113 *
114 * @param bool TRUE for original, FALSE for modified
115 * @param array Data array
116 * @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')
117 * @param bool If TRUE, then the list of fields is used to exclude, not include
118 * @param string Field prefix
119 */
120 public function addData($orig, $data, $fields, $exclude = false, $prefix = '')
121 {
122 $array = ($orig ? 'original' : 'modified');
123 $prefix .= '.';
124
125 if ($exclude == false)
126 {
127 foreach ($fields as $fname => $fdisplay)
128 {
129 if (is_numeric($fname))
130 {
131 $fname = $fdisplay;
132 }
133
134 $this->{$array}["$prefix$fdisplay"] = array('name' => $fname, 'value' => $data["$fname"]);
135 }
136 }
137 else
138 {
139 foreach ($data as $fname => $value)
140 {
141 if (!in_array($fname, $fields))
142 {
143 $this->{$array}["$prefix$fname"] = array('name' => $fname, 'value' => $value);
144 }
145 }
146 }
147 }
148
149 /**
150 * Populates the $this->compared array as a diff between the original
151 * and modified data. This is then used to create the databse queries.
152 */
153 private function _compareArrays()
154 {
155 foreach ($this->modified AS $key => $value)
156 {
157 if ($this->original["$key"] != $value && !($value['value'] == '' && $this->original["$key"]['value'] == '0') && !($this->original["$key"]['value'] == '' && $value['value'] == '0'))
158 {
159 $this->compared["$key"] = array('old' => $this->original["$key"]['value'], 'new' => $this->modified["$key"]['value']);
160 }
161 }
162 }
163
164 /**
165 * Runs $this->_compareArrays() and then takes the result and prepares
166 * it for insertion into the history database.
167 */
168 public function updateHistory()
169 {
170 $this->_compareArrays();
171
172 foreach ($this->compared as $field => $values)
173 {
174 BSApp::$db->query("
175 INSERT INTO " . TABLE_PREFIX . "history
176 (bugid, attachmentid, commentid, dateline, userid, field, original, changed)
177 VALUES
178 (" . BSApp::$input->clean($this->bugid, TYPE_UINT) . ", " . BSApp::$input->clean($this->attachmentid, TYPE_UINT) . ",
179 " . BSApp::$input->clean($this->commentid, TYPE_UINT) . ", " . TIMENOW . ", " . bugdar::$userinfo['userid'] . ",
180 '" . BSApp::$db->escapeString($field) . "', '" . BSApp::$db->escapeString($values['old']) . "',
181 '" . BSApp::$db->escapeString($values['new']) . "'
182 )
183 ");
184 }
185 }
186
187 /**
188 * Returns an array of the fields commonly ignored
189 *
190 * @return array Fields ignored in logging
191 */
192 public function getCommonFields()
193 {
194 return array(
195 'bugid',
196 'lastposttime',
197 'lastpostby',
198 'lastpostbyname',
199 'hiddenlastposttime',
200 'hiddenlastpostby',
201 'hiddenlastpostbyname'
202 );
203 }
204 }
205
206 /*=====================================================================*\
207 || ###################################################################
208 || # $HeadURL$
209 || # $Id$
210 || ###################################################################
211 \*=====================================================================*/
212 ?>