Skip to content

Commit c18de18

Browse files
committed
Add now-timestamp parameter to simpleSaveChangeHistory
1 parent f813b5d commit c18de18

5 files changed

Lines changed: 32 additions & 21 deletions

File tree

cron/dailyroutine.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
FROM people AS p
2626
LEFT OUTER JOIN camps AS c ON c.id = p.camp_id
2727
WHERE (NOT p.deleted OR p.deleted IS NULL) AND p.parent_id IS NULL');
28+
29+
// All deletions are logged with the same timestamp
30+
$now = date('Y-m-d H:i:s');
2831
while ($row = db_fetch($result)) {
2932
$row['touch'] = db_value('
3033
SELECT GREATEST(COALESCE((
@@ -48,8 +51,8 @@
4851
$row['diff'] = $date2->diff($date1)->format('%a');
4952

5053
if ($row['diff'] > $row['treshold']) {
51-
db_query('UPDATE people SET deleted = NOW() WHERE id = :id', ['id' => $row['id']]);
52-
simpleSaveChangeHistory('people', $row['id'], 'Record deleted by daily routine');
54+
db_query('UPDATE people SET deleted = :now WHERE id = :id', ['id' => $row['id'], 'now' => $now]);
55+
simpleSaveChangeHistory('people', $row['id'], 'Record deleted by daily routine', $now);
5356
db_touch('people', $row['id']);
5457
}
5558
}
@@ -61,8 +64,8 @@
6164
FROM people AS p1, people AS p2
6265
WHERE p2.parent_id = p1.id AND p1.deleted AND (NOT p2.deleted OR p2.deleted IS NULL)');
6366
while ($row = db_fetch($result)) {
64-
db_query('UPDATE people SET deleted = NOW() WHERE id = :id', ['id' => $row['id']]);
65-
simpleSaveChangeHistory('people', $row['id'], 'Record deleted by daily routine because head of family/beneficiary was deleted');
67+
db_query('UPDATE people SET deleted = :now WHERE id = :id', ['id' => $row['id'], 'now' => $now]);
68+
simpleSaveChangeHistory('people', $row['id'], 'Record deleted by daily routine because head of family/beneficiary was deleted', $now);
6669
db_touch('people', $row['id']);
6770
}
6871

library/ajax/deleteprofile.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
<?php
22

33
db_transaction(function () {
4+
$now = date('Y-m-d H:i:s');
45
// Only append .deleted suffix if the email doesn't already have it
56
// This prevents double-deletion if the query is somehow executed twice
67
// Pattern checks for .deleted.<digits> after the @ symbol (e.g., user@domain.com.deleted.123)
7-
db_query('UPDATE cms_users SET deleted = NOW(), email = CONCAT(email,".deleted.",id) WHERE id = :id AND (NOT deleted OR deleted IS NULL) AND email NOT REGEXP "@.*\.deleted\.[0-9]+$"', ['id' => $_POST['cms_user_id']]);
8+
db_query('UPDATE cms_users SET deleted = :now, email = CONCAT(email,".deleted.",id) WHERE id = :id AND (NOT deleted OR deleted IS NULL) AND email NOT REGEXP "@.*\.deleted\.[0-9]+$"', ['now' => $now, 'id' => $_POST['cms_user_id']]);
89
updateAuth0UserFromDb($_POST['cms_user_id']);
10+
simpleSaveChangeHistory('cms_users', $_POST['cms_user_id'], 'Record deleted without undelete', $now);
911
});
1012

11-
simpleSaveChangeHistory('cms_users', $_POST['cms_user_id'], 'Record deleted without undelete');
1213
// when a user deactive its account we need to ensure that user logged out immediately and then redirected to Auth0 login page
1314
global $settings;
1415
logout();

library/functions.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ function move_boxes($ids, $newlocationid, $mobile = false)
272272
{
273273
[$count, $action_label, $mobile_message] = db_transaction(function () use ($ids, $newlocationid) {
274274
$count = 0;
275+
$now = date('Y-m-d H:i:s');
275276
foreach ($ids as $id) {
276277
$box = db_row('
277278
SELECT
@@ -311,23 +312,24 @@ function move_boxes($ids, $newlocationid, $mobile = false)
311312
'
312313
UPDATE stock
313314
SET
314-
modified = NOW(),
315+
modified = :now,
315316
modified_by = :user_id ,
316317
location_id = :location
317318
WHERE id = :id',
318-
['location' => $newlocationid, 'id' => $id, 'user_id' => $_SESSION['user']['id']]
319+
['location' => $newlocationid, 'id' => $id, 'now' => $now, 'user_id' => $_SESSION['user']['id']]
319320
);
320321

321322
$from['int'] = $box['location_id'];
322323
$to['int'] = $newlocationid;
323-
simpleSaveChangeHistory('stock', $id, 'location_id', $from, $to);
324+
simpleSaveChangeHistory('stock', $id, 'location_id', $now, $from, $to);
324325
db_query(
325326
'
326327
INSERT INTO itemsout (product_id, size_id, count, movedate, from_location, to_location)
327-
VALUES (:product_id, :size_id, :count, NOW(), :from_location, :to_location)',
328+
VALUES (:product_id, :size_id, :count, :now, :from_location, :to_location)',
328329
['product_id' => $box['product_id'],
329330
'size_id' => $box['size_id'],
330331
'count' => $box['items'],
332+
'now' => $now,
331333
'from_location' => $box['location_id'],
332334
'to_location' => $newlocationid, ]
333335
);
@@ -343,12 +345,12 @@ function move_boxes($ids, $newlocationid, $mobile = false)
343345
UPDATE stock
344346
SET
345347
box_state_id = :box_state_id,
346-
modified = NOW(),
348+
modified = :now,
347349
modified_by = :user_id
348350
WHERE id = :id',
349-
['box_state_id' => $newlocation['box_state_id'], 'id' => $id, 'user_id' => $_SESSION['user']['id']]
351+
['box_state_id' => $newlocation['box_state_id'], 'id' => $id, 'now' => $now, 'user_id' => $_SESSION['user']['id']]
350352
);
351-
simpleSaveChangeHistory('stock', $id, 'box_state_id', $from, $to);
353+
simpleSaveChangeHistory('stock', $id, 'box_state_id', $now, $from, $to);
352354
$mobile_message .= ' and its state changed to '.$newlocation['box_state_name'];
353355
}
354356

library/lib/list.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,12 @@ function listRealDelete($table, $ids, $uri = false)
9898
$hasPrevent = db_fieldexists($table, 'preventdelete');
9999
$hasTree = db_fieldexists($table, 'parent_id');
100100
$count = 0;
101+
$now = date('Y-m-d H:i:s');
101102
foreach ($ids as $id) {
102103
$result = db_query('DELETE FROM '.$table.' WHERE id = :id'.($hasPrevent ? ' AND NOT preventdelete' : ''), ['id' => $id]);
103104
$count += $result->rowCount();
104105
if ($result->rowCount()) {
105-
simpleSaveChangeHistory($table, $id, 'Record deleted without undelete');
106+
simpleSaveChangeHistory($table, $id, 'Record deleted without undelete', $now);
106107
}
107108
}
108109

@@ -232,16 +233,18 @@ function listDeleteAction($table, $id, $count = 0, $recursive = false)
232233
// prevent deletion of deleted records
233234
$hasDeleted = db_fieldexists($table, 'deleted');
234235

235-
$query = 'UPDATE '.$table.' SET deleted = NOW(), modified = NOW(), modified_by = :user_id WHERE id = :id';
236+
$now = date('Y-m-d H:i:s');
237+
$query = 'UPDATE '.$table.' SET deleted = :now, modified = :now, modified_by = :user_id WHERE id = :id';
236238
$query .= ($hasPrevent ? ' AND NOT preventdelete' : '');
237239
$query .= ($hasDeleted ? ' AND (NOT deleted OR deleted IS NULL)' : '');
238240
$result = db_query($query, [
241+
'now' => $now,
239242
'id' => $id,
240243
'user_id' => $_SESSION['user']['id'],
241244
]);
242245
$count += $result->rowCount();
243246
if (1 === $result->rowCount()) {
244-
simpleSaveChangeHistory($table, $id, 'Record deleted');
247+
simpleSaveChangeHistory($table, $id, 'Record deleted', $now);
245248
}
246249

247250
if ($recursive) {
@@ -283,10 +286,11 @@ function listUndelete($table, $ids, $uri = false, $overwritehastree = false)
283286

284287
function listUnDeleteAction($table, $id, $count = 0, $recursive = false, $null = true)
285288
{
286-
$result = db_query('UPDATE '.$table.' SET deleted = '.($null ? 'NULL' : '0').', modified = NOW(), modified_by = :user_id WHERE id = :id', ['id' => $id, 'user_id' => $_SESSION['user']['id']]);
289+
$now = date('Y-m-d H:i:s');
290+
$result = db_query('UPDATE '.$table.' SET deleted = '.($null ? 'NULL' : '0').', modified = :now, modified_by = :user_id WHERE id = :id', ['now' => $now, 'id' => $id, 'user_id' => $_SESSION['user']['id']]);
287291
$count += $result->rowCount();
288292
if ($result->rowCount()) {
289-
simpleSaveChangeHistory($table, $id, 'Record recovered');
293+
simpleSaveChangeHistory($table, $id, 'Record recovered', $now);
290294
}
291295

292296
if ($recursive) {
@@ -387,9 +391,10 @@ function listExtendAction($table, $id, $period)
387391
};
388392
$result = db_query($extendQuery, ['id' => $id]);
389393

394+
$now = date('Y-m-d H:i:s');
390395
$count += $result->rowCount();
391396
if ($count) {
392-
simpleSaveChangeHistory($table, $id, 'Record extended');
397+
simpleSaveChangeHistory($table, $id, 'Record extended', $now);
393398
}
394399

395400
return $count;

library/lib/tools.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,13 +286,13 @@ function utf8_decode_array($array)
286286
return $array;
287287
}
288288

289-
function simpleSaveChangeHistory($table, $record, $changes, $from = [], $to = [])
289+
function simpleSaveChangeHistory($table, $record, $changes, $now, $from = [], $to = [])
290290
{
291291
// from and to variable must be arrays with entry 'int' or 'float'
292292
if (!db_tableexists('history')) {
293293
return;
294294
}
295-
db_query('INSERT INTO history (tablename, record_id, changes, user_id, ip, changedate, from_int, from_float, to_int, to_float) VALUES (:table,:id,:change,:user_id,:ip,NOW(), :from_int, :from_float, :to_int, :to_float)', ['table' => $table, 'id' => $record, 'change' => $changes, 'user_id' => $_SESSION['user']['id'], 'ip' => $_SERVER['REMOTE_ADDR'], 'from_int' => $from['int'], 'from_float' => $from['float'], 'to_int' => $to['int'], 'to_float' => $to['float']]);
295+
db_query('INSERT INTO history (tablename, record_id, changes, user_id, ip, changedate, from_int, from_float, to_int, to_float) VALUES (:table,:id,:change,:user_id,:ip,:now, :from_int, :from_float, :to_int, :to_float)', ['table' => $table, 'id' => $record, 'change' => $changes, 'user_id' => $_SESSION['user']['id'], 'ip' => $_SERVER['REMOTE_ADDR'], 'now' => $now, 'from_int' => $from['int'], 'from_float' => $from['float'], 'to_int' => $to['int'], 'to_float' => $to['float']]);
296296
}
297297

298298
function simpleBulkSaveChangeHistory($table, $records, $changes, $from = [], $to = [])

0 commit comments

Comments
 (0)