1 | <?php |
---|
2 | /**************************************************************************\ |
---|
3 | * eGroupWare API - Record history logging * |
---|
4 | * This file written by Joseph Engo <jengo@phpgroupware.org> * |
---|
5 | * Copyright (C) 2001 Joseph Engo * |
---|
6 | * -------------------------------------------------------------------------* |
---|
7 | * This library is part of the eGroupWare API * |
---|
8 | * http://www.egroupware.org/api * |
---|
9 | * ------------------------------------------------------------------------ * |
---|
10 | * This library is free software; you can redistribute it and/or modify it * |
---|
11 | * under the terms of the GNU Lesser General Public License as published by * |
---|
12 | * the Free Software Foundation; either version 2.1 of the License, * |
---|
13 | * or any later version. * |
---|
14 | * This library is distributed in the hope that it will be useful, but * |
---|
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * |
---|
17 | * See the GNU Lesser General Public License for more details. * |
---|
18 | * You should have received a copy of the GNU Lesser General Public License * |
---|
19 | * along with this library; if not, write to the Free Software Foundation, * |
---|
20 | * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
---|
21 | \**************************************************************************/ |
---|
22 | |
---|
23 | |
---|
24 | class historylog |
---|
25 | { |
---|
26 | var $db; |
---|
27 | var $appname; |
---|
28 | var $template; |
---|
29 | var $nextmatchs; |
---|
30 | var $types = array( |
---|
31 | 'C' => 'Created', |
---|
32 | 'D' => 'Deleted', |
---|
33 | 'E' => 'Edited' |
---|
34 | ); |
---|
35 | var $alternate_handlers = array(); |
---|
36 | |
---|
37 | function historylog($appname='') |
---|
38 | { |
---|
39 | if (! $appname) |
---|
40 | { |
---|
41 | $appname = $GLOBALS['phpgw_info']['flags']['currentapp']; |
---|
42 | } |
---|
43 | |
---|
44 | $this->appname = $appname; |
---|
45 | $this->db = $GLOBALS['phpgw']->db; |
---|
46 | } |
---|
47 | |
---|
48 | function delete($record_id) |
---|
49 | { |
---|
50 | $appname = (int)$record_id ? $this->appname : $record_id; |
---|
51 | $record_id = (int)$record_id; |
---|
52 | $this->db->query('DELETE FROM phpgw_history_log WHERE' |
---|
53 | . ($record_id ? " history_record_id='$record_id' AND" : '') |
---|
54 | . " history_appname='$appname'",__LINE__,__FILE__); |
---|
55 | |
---|
56 | return $this->db->affected_rows(); |
---|
57 | } |
---|
58 | |
---|
59 | function add($status,$record_id,$new_value,$old_value) |
---|
60 | { |
---|
61 | if ($new_value != $old_value) |
---|
62 | { |
---|
63 | $this->db->query("insert into phpgw_history_log (history_record_id," |
---|
64 | . "history_appname,history_owner,history_status,history_new_value,history_old_value,history_timestamp) " |
---|
65 | . "values ('".(int)$record_id."','" . $this->appname . "','" |
---|
66 | . $GLOBALS['phpgw_info']['user']['account_id'] . "','$status','" |
---|
67 | . addslashes($new_value) . "','" . addslashes($old_value) . "','" . $this->db->to_timestamp(time()) |
---|
68 | . "')",__LINE__,__FILE__); |
---|
69 | } |
---|
70 | } |
---|
71 | |
---|
72 | // array $filter_out |
---|
73 | function return_array($filter_out,$only_show,$_orderby = '',$sort = '', $record_id) |
---|
74 | { |
---|
75 | |
---|
76 | if (! $sort || ! $_orderby) |
---|
77 | { |
---|
78 | $orderby = 'order by history_timestamp,history_id'; |
---|
79 | } |
---|
80 | else |
---|
81 | { |
---|
82 | $orderby = "order by $_orderby $sort"; |
---|
83 | } |
---|
84 | |
---|
85 | while (is_array($filter_out) && list(,$_filter) = each($filter_out)) |
---|
86 | { |
---|
87 | $filtered[] = "history_status != '$_filter'"; |
---|
88 | } |
---|
89 | |
---|
90 | if (is_array($filtered)) |
---|
91 | { |
---|
92 | $filter = ' and ' . implode(' and ',$filtered); |
---|
93 | } |
---|
94 | |
---|
95 | while (is_array($only_show) && list(,$_filter) = each($only_show)) |
---|
96 | { |
---|
97 | $_only_show[] = "history_status='$_filter'"; |
---|
98 | } |
---|
99 | |
---|
100 | if (is_array($_only_show)) |
---|
101 | { |
---|
102 | $only_show_filter = ' and (' . implode(' or ',$_only_show). ')'; |
---|
103 | } |
---|
104 | |
---|
105 | $this->db->query("select * from phpgw_history_log where history_appname='" |
---|
106 | . $this->appname . "' and history_record_id='".(int)$record_id."' $filter $only_show_filter " |
---|
107 | . "$orderby",__LINE__,__FILE__); |
---|
108 | while ($this->db->next_record()) |
---|
109 | { |
---|
110 | $return_values[] = array( |
---|
111 | 'id' => $this->db->f('history_id'), |
---|
112 | 'record_id' => $this->db->f('history_record_id'), |
---|
113 | 'owner' => $GLOBALS['phpgw']->accounts->id2name($this->db->f('history_owner')), |
---|
114 | // 'status' => lang($this->types[$this->db->f('history_status')]), |
---|
115 | 'status' => str_replace(' ','',$this->db->f('history_status')), |
---|
116 | 'new_value' => $this->db->f('history_new_value'), |
---|
117 | 'old_value' => $this->db->f('history_old_value'), |
---|
118 | 'datetime' => $this->db->from_timestamp($this->db->f('history_timestamp')) |
---|
119 | ); |
---|
120 | } |
---|
121 | return $return_values; |
---|
122 | } |
---|
123 | |
---|
124 | function return_html($filter_out,$orderby = '',$sort = '', $record_id) |
---|
125 | { |
---|
126 | $this->template = createobject('phpgwapi.Template',PHPGW_TEMPLATE_DIR); |
---|
127 | $this->nextmatchs = createobject('phpgwapi.nextmatchs'); |
---|
128 | |
---|
129 | $this->template->set_file('_history','history_list.tpl'); |
---|
130 | |
---|
131 | $this->template->set_block('_history','row_no_history'); |
---|
132 | $this->template->set_block('_history','list'); |
---|
133 | $this->template->set_block('_history','row'); |
---|
134 | |
---|
135 | $this->template->set_var('lang_user',lang('User')); |
---|
136 | $this->template->set_var('lang_date',lang('Date')); |
---|
137 | $this->template->set_var('lang_action',lang('Action')); |
---|
138 | $this->template->set_var('lang_new_value',lang('New Value')); |
---|
139 | |
---|
140 | $this->template->set_var('th_bg',$GLOBALS['phpgw_info']['theme']['th_bg']); |
---|
141 | $this->template->set_var('sort_date',lang('Date')); |
---|
142 | $this->template->set_var('sort_owner',lang('User')); |
---|
143 | $this->template->set_var('sort_status',lang('Status')); |
---|
144 | $this->template->set_var('sort_new_value',lang('New value')); |
---|
145 | $this->template->set_var('sort_old_value',lang('Old value')); |
---|
146 | |
---|
147 | $values = $this->return_array($filter_out,array(),$orderby,$sort,$record_id); |
---|
148 | |
---|
149 | if (! is_array($values)) |
---|
150 | { |
---|
151 | $this->template->set_var('tr_color',$GLOBALS['phpgw_info']['theme']['row_off']); |
---|
152 | $this->template->set_var('lang_no_history',lang('No history for this record')); |
---|
153 | $this->template->fp('rows','row_no_history'); |
---|
154 | return $this->template->fp('out','list'); |
---|
155 | } |
---|
156 | |
---|
157 | while (list(,$value) = each($values)) |
---|
158 | { |
---|
159 | $this->nextmatchs->template_alternate_row_color($this->template); |
---|
160 | |
---|
161 | $this->template->set_var('row_date',$GLOBALS['phpgw']->common->show_date($value['datetime'])); |
---|
162 | $this->template->set_var('row_owner',$value['owner']); |
---|
163 | |
---|
164 | if ($this->alternate_handlers[$value['status']]) |
---|
165 | { |
---|
166 | eval('\$s = ' . $this->alternate_handlers[$value['status']] . '(' . $value['new_value'] . ');'); |
---|
167 | $this->template->set_var('row_new_value',$s); |
---|
168 | unset($s); |
---|
169 | |
---|
170 | eval('\$s = ' . $this->alternate_handlers[$value['status']] . '(' . $value['old_value'] . ');'); |
---|
171 | $this->template->set_var('row_old_value',$s); |
---|
172 | unset($s); |
---|
173 | } |
---|
174 | else |
---|
175 | { |
---|
176 | $this->template->set_var('row_new_value',$value['new_value']); |
---|
177 | $this->template->set_var('row_old_value',$value['old_value']); |
---|
178 | } |
---|
179 | |
---|
180 | $this->template->set_var('row_status',$this->types[$value['status']]); |
---|
181 | |
---|
182 | $this->template->fp('rows','row',True); |
---|
183 | } |
---|
184 | return $this->template->fp('out','list'); |
---|
185 | } |
---|
186 | } |
---|