Skip to content

Commit b1f8332

Browse files
committed
Add optional now-timestamp parameter to simpleBulkSaveChangeHistory
1 parent bd094b7 commit b1f8332

3 files changed

Lines changed: 15 additions & 10 deletions

File tree

include/people.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -514,14 +514,15 @@ function () use ($cmsmain, $data) {
514514
case 'touch':
515515
$ids = explode(',', (string) $_POST['ids']);
516516
$userId = $_SESSION['user']['id'];
517+
$now = date('Y-m-d H:i:s');
517518
// Query speed optimised for 500 records from 6.2 seconds to 0.54 seconds using transaction blocks over UPDATE and bulk inserts
518-
db_transaction(function () use ($ids, $userId) {
519+
db_transaction(function () use ($ids, $userId, $now) {
519520
foreach ($ids as $id) {
520-
db_query('UPDATE people SET modified = NOW(), modified_by = :user WHERE id = :id', ['id' => $id, 'user' => $userId]);
521+
db_query('UPDATE people SET modified = :now, modified_by = :user WHERE id = :id', ['id' => $id, 'user' => $userId, 'now' => $now]);
521522
}
522523
});
523524
// Bulk insert used to insert into history table
524-
simpleBulkSaveChangeHistory('people', $ids, 'Touched');
525+
simpleBulkSaveChangeHistory('people', $ids, 'Touched', $now);
525526

526527
$success = true;
527528
$message = 'Selected people have been touched';

library/lib/list.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -326,10 +326,11 @@ function listBulkUndelete($table, $ids, $uri = false, $overwritehastree = false)
326326

327327
function listBulkUnDeleteAction($table, $ids, $count = 0, $hasTree = false)
328328
{
329-
[$finalIds, $count] = db_transaction(function () use ($table, $ids, $count, $hasTree) {
329+
$now = date('Y-m-d H:i:s');
330+
[$finalIds, $count] = db_transaction(function () use ($table, $ids, $count, $hasTree, $now) {
330331
$finalIds = [];
331332
foreach ($ids as $id) {
332-
$result = db_query('UPDATE '.$table.' SET deleted = 0, modified = NOW(), modified_by = :user_id WHERE id = :id', ['id' => $id, 'user_id' => $_SESSION['user']['id']]);
333+
$result = db_query('UPDATE '.$table.' SET deleted = 0, modified = :now, modified_by = :user_id WHERE id = :id', ['id' => $id, 'user_id' => $_SESSION['user']['id'], 'now' => $now]);
333334
$count += $result->rowCount();
334335
if ($result->rowCount()) {
335336
$finalIds[] = $id;
@@ -338,7 +339,7 @@ function listBulkUnDeleteAction($table, $ids, $count = 0, $hasTree = false)
338339
if ($hasTree) {
339340
$childs = db_array('SELECT id FROM '.$table.' WHERE parent_id = :id', ['id' => $id]);
340341
foreach ($childs as $child) {
341-
$result = db_query('UPDATE '.$table.' SET deleted = 0, modified = NOW(), modified_by = :user_id WHERE id = :id', ['id' => $child['id'], 'user_id' => $_SESSION['user']['id']]);
342+
$result = db_query('UPDATE '.$table.' SET deleted = 0, modified = :now, modified_by = :user_id WHERE id = :id', ['id' => $child['id'], 'user_id' => $_SESSION['user']['id'], 'now' => $now]);
342343
$count += $result->rowCount();
343344
if ($result->rowCount()) {
344345
$finalIds[] = $child['id'];
@@ -350,7 +351,7 @@ function listBulkUnDeleteAction($table, $ids, $count = 0, $hasTree = false)
350351
return [$finalIds, $count];
351352
});
352353

353-
simpleBulkSaveChangeHistory($table, $finalIds, 'Record recovered');
354+
simpleBulkSaveChangeHistory($table, $finalIds, 'Record recovered', $now);
354355

355356
return $count;
356357
}

library/lib/tools.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,17 +298,20 @@ function simpleSaveChangeHistory($table, $record, $changes, $now = null, $from =
298298
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']]);
299299
}
300300

301-
function simpleBulkSaveChangeHistory($table, $records, $changes)
301+
function simpleBulkSaveChangeHistory($table, $records, $changes, $now = null)
302302
{
303303
if (!db_tableexists('history')) {
304304
return;
305305
}
306+
if (null === $now) {
307+
$now = date('Y-m-d H:i:s');
308+
}
306309
$query = '';
307310
$params = [];
308311
if (is_iterable($records)) {
309312
for ($i = 0; $i < sizeof($records); ++$i) {
310-
$query .= "(:table{$i},:id{$i},:change{$i},:user_id{$i},:ip{$i},NOW())";
311-
$params = array_merge($params, ['table'.$i => $table, 'id'.$i => $records[$i], 'change'.$i => $changes, 'user_id'.$i => $_SESSION['user']['id'], 'ip'.$i => $_SERVER['REMOTE_ADDR']]);
313+
$query .= "(:table{$i},:id{$i},:change{$i},:user_id{$i},:ip{$i},:now)";
314+
$params = array_merge($params, ['table'.$i => $table, 'id'.$i => $records[$i], 'change'.$i => $changes, 'user_id'.$i => $_SESSION['user']['id'], 'ip'.$i => $_SERVER['REMOTE_ADDR'], 'now' => $now]);
312315
if ($i !== sizeof($records) - 1) {
313316
$query .= ',';
314317
}

0 commit comments

Comments
 (0)