Skip to content

Commit 18a995c

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

5 files changed

Lines changed: 27 additions & 20 deletions

File tree

cron/dailyroutine.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
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+
$now = date('Y-m-d H:i:s');
2829
while ($row = db_fetch($result)) {
2930
$row['touch'] = db_value('
3031
SELECT GREATEST(COALESCE((
@@ -48,8 +49,8 @@
4849
$row['diff'] = $date2->diff($date1)->format('%a');
4950

5051
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');
52+
db_query('UPDATE people SET deleted = :now WHERE id = :id', ['id' => $row['id'], 'now' => $now]);
53+
simpleSaveChangeHistory('people', $row['id'], 'Record deleted by daily routine', $now);
5354
db_touch('people', $row['id']);
5455
}
5556
}
@@ -61,8 +62,8 @@
6162
FROM people AS p1, people AS p2
6263
WHERE p2.parent_id = p1.id AND p1.deleted AND (NOT p2.deleted OR p2.deleted IS NULL)');
6364
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');
65+
db_query('UPDATE people SET deleted = :now WHERE id = :id', ['id' => $row['id'], 'now' => $now]);
66+
simpleSaveChangeHistory('people', $row['id'], 'Record deleted by daily routine because head of family/beneficiary was deleted', $now);
6667
db_touch('people', $row['id']);
6768
}
6869

library/ajax/deleteprofile.php

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

3-
db_transaction(function () {
3+
$now = date('Y-m-d H:i:s');
4+
db_transaction(function () use ($now) {
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']);
910
});
1011

11-
simpleSaveChangeHistory('cms_users', $_POST['cms_user_id'], 'Record deleted without undelete');
12+
simpleSaveChangeHistory('cms_users', $_POST['cms_user_id'], 'Record deleted without undelete', $now);
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: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,16 +232,18 @@ function listDeleteAction($table, $id, $count = 0, $recursive = false)
232232
// prevent deletion of deleted records
233233
$hasDeleted = db_fieldexists($table, 'deleted');
234234

235-
$query = 'UPDATE '.$table.' SET deleted = NOW(), modified = NOW(), modified_by = :user_id WHERE id = :id';
235+
$now = date('Y-m-d H:i:s');
236+
$query = 'UPDATE '.$table.' SET deleted = :now, modified = :now, modified_by = :user_id WHERE id = :id';
236237
$query .= ($hasPrevent ? ' AND NOT preventdelete' : '');
237238
$query .= ($hasDeleted ? ' AND (NOT deleted OR deleted IS NULL)' : '');
238239
$result = db_query($query, [
240+
'now' => $now,
239241
'id' => $id,
240242
'user_id' => $_SESSION['user']['id'],
241243
]);
242244
$count += $result->rowCount();
243245
if (1 === $result->rowCount()) {
244-
simpleSaveChangeHistory($table, $id, 'Record deleted');
246+
simpleSaveChangeHistory($table, $id, 'Record deleted', $now);
245247
}
246248

247249
if ($recursive) {
@@ -283,10 +285,11 @@ function listUndelete($table, $ids, $uri = false, $overwritehastree = false)
283285

284286
function listUnDeleteAction($table, $id, $count = 0, $recursive = false, $null = true)
285287
{
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']]);
288+
$now = date('Y-m-d H:i:s');
289+
$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']]);
287290
$count += $result->rowCount();
288291
if ($result->rowCount()) {
289-
simpleSaveChangeHistory($table, $id, 'Record recovered');
292+
simpleSaveChangeHistory($table, $id, 'Record recovered', $now);
290293
}
291294

292295
if ($recursive) {

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)