r815: Everything for logging is *hopefully* implemented right, except Log::update_history
[bugdar.git] / includes / class_logging.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
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 [#]gpl[#] 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 Iris Studios, Inc.
36 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
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 * The original data to compare against
52 * @var array
53 * @access private
54 */
55 var $original = array();
56
57 /**
58 * Modified data
59 * @var array
60 * @access private
61 */
62 var $modified = array();
63
64 /**
65 * Compared/diff'd data
66 * @var array
67 * @access private
68 */
69 var $compared = array();
70
71 // ###################################################################
72 /**
73 * Sets the bug ID for the current logging instance
74 *
75 * @access public
76 *
77 * @param integer New bug ID
78 */
79 function set_bugid($newbugid)
80 {
81 $this->bugid = $newbugid;
82 }
83
84 // ###################################################################
85 /**
86 * Assigns data into the $this->original or $this->modified array based
87 * on the passed arrays of information and the fields to add (and what
88 * name to add them under), and any prefix
89 *
90 * @access public
91 *
92 * @param bool TRUE for original, FALSE for modified
93 * @param array Data array
94 * @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')
95 * @param bool If TRUE, then the list of fields is used to exclude, not include
96 * @param string Field prefix
97 */
98 function add_data($orig, $data, $fields, $exclude = false, $prefix = '')
99 {
100 $array = ($orig ? 'original' : 'modified');
101 $prefix .= '.';
102
103 if ($exclude == false)
104 {
105 foreach ($fields AS $fname => $fdisplay)
106 {
107 if (is_numeric($fname))
108 {
109 $fname = $fdisplay;
110 }
111
112 $this->{$array}["$prefix$fdisplay"] = array('name' => $fname, 'value' => $data["$fname"]);
113 }
114 }
115 else
116 {
117 foreach ($data AS $fname => $value)
118 {
119 if (!in_array($fname, $fields))
120 {
121 $this->{$array}["$prefix$fname"] = array('name' => $fname, 'value' => $value);
122 }
123 }
124 }
125 }
126
127 // ###################################################################
128 /**
129 * Populates the $this->compared array as a diff between the original
130 * and modified data. This is then used to create the databse queries.
131 *
132 * @access private
133 */
134 function compare_arrays()
135 {
136 $newfields = array_diff_assoc($this->modified, $this->original);
137 $removedfields = array_diff_assoc($this->original, $this->modified);
138
139 foreach ($this->modified AS $key => $value)
140 {
141 if ($this->original["$key"] != $value)
142 {
143 $this->compared["$key"] = array('old' => $this->original["$key"]['value'], 'new' => $this->modified["$key"]['value']);
144 }
145 }
146
147 foreach ($newfields AS $field)
148 {
149 $this->compared["$field"] = array('old' => null, 'new' => $this->modified["$field"]['value']);
150 }
151
152 foreach ($removedfields AS $field)
153 {
154 $this->compared["$field"] = array('old' => $this->original["$field"]['value'], 'new' => null);
155 }
156 }
157
158 // ###################################################################
159 /**
160 * Runs $this->compare_arrays() and then takes the result and prepares
161 * it for insertion into the history database.
162 *
163 * @access public
164 */
165 function update_history()
166 {
167 $this->compare_arrays();
168 }
169 }
170
171 /*=====================================================================*\
172 || ###################################################################
173 || # $HeadURL$
174 || # $Id$
175 || ###################################################################
176 \*=====================================================================*/
177 ?>