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