Skip to content

Consistent datetime usage

theo lem requested to merge consistent-datetime-usage into master

1 : named placeholders

Making use of DateTime object and named placeholders and for more readable queries within the expiration process. Ex : this

db_query(sprintf("
INSERT INTO framaforms_expired (nid)
(SELECT n.nid
FROM node n
INNER JOIN field_data_field_form1_expiration fex
ON fex.entity_id = n.nid
AND fex.field_form1_expiration_value <= date_trunc('day', NOW() + interval'{$expiration_period}')
AND fex.entity_id NOT IN
  (SELECT nid FROM framaforms_expired)
LIMIT 1000)
ON CONFLICT DO NOTHING;
", $expiration_period));

is now replaced by this :

db_query(
"INSERT INTO framaforms_expired (nid)
(SELECT n.nid
FROM node n
INNER JOIN field_data_field_form1_expiration fex
ON fex.entity_id = n.nid
AND fex.field_form1_expiration_value <= to_timestamp(:max_timestamp)
AND fex.entity_id NOT IN
(SELECT nid FROM framaforms_expired)
LIMIT 1000)
ON CONFLICT DO NOTHING;
",
array(':max_timestamp' => $max_expiration_time->getTimestamp())
);

($max_expiration_time being a DateTime object).

This is best practice for Drupal 7, and allows logging of the event taking place.

2. Using a hook_node_presave to handle modified nodes

When a user modified its form's expiration date, the node was handled through a cron task (update_modified_nodes) which made use of a buggy and hardly maintainable query.

The modification of such forms is now handled through an implementation of hook_node_presave, which, if the form was in it in the first place, deletes it from the framaforms_expired table.

Edited by theo lem

Merge request reports