}
}
+ /**
+ * Generate triggers to update the timestamp on the corresponding civicrm_contact row,
+ * on insert/update/delete to a table that extends civicrm_contact.
+ * Don't regenerate triggers for all such tables if only asked for one table.
+ *
+ * @param array $info
+ * Reference to the array where generated trigger information is being stored
+ * @param string|null $reqTableName
+ * Name of the table for which triggers are being generated, or NULL if all tables
+ * @param array $relatedTableNames
+ * Array of all core or all custom table names extending civicrm_contact
+ * @param string $contactRefColumn
+ * 'contact_id' if processing core tables, 'entity_id' if processing custom tables
+ * @return void
+ *
+ * @link https://issues.civicrm.org/jira/browse/CRM-15602
+ * @see triggerInfo
+ * @access public
+ * @static
+ */
+ static function generateTimestampTriggers(&$info, $reqTableName, $relatedTableNames, $contactRefColumn) {
+ // Safety
+ $contactRefColumn = CRM_Core_DAO::escapeString($contactRefColumn);
+ // If specific related table requested, just process that one
+ if (in_array($reqTableName, $relatedTableNames)) {
+ $relatedTableNames = array($reqTableName);
+ }
+
+ // If no specific table requested (include all related tables),
+ // or a specific related table requested (as matched above)
+ if (empty($reqTableName) || in_array($reqTableName, $relatedTableNames)) {
+ $info[] = array(
+ 'table' => $relatedTableNames,
+ 'when' => 'AFTER',
+ 'event' => array('INSERT', 'UPDATE'),
+ 'sql' => "\nUPDATE civicrm_contact SET modified_date = CURRENT_TIMESTAMP WHERE id = NEW.$contactRefColumn;\n",
+ );
+ $info[] = array(
+ 'table' => $relatedTableNames,
+ 'when' => 'AFTER',
+ 'event' => array('DELETE'),
+ 'sql' => "\nUPDATE civicrm_contact SET modified_date = CURRENT_TIMESTAMP WHERE id = OLD.$contactRefColumn;\n",
+ );
+ }
+ }
+
/**
* Get a list of triggers for the contact table
*
}
}
+ // Update timestamp for civicrm_contact itself
if ($tableName == NULL || $tableName == self::getTableName()) {
$info[] = array(
'table' => array(self::getTableName()),
'civicrm_phone',
'civicrm_website',
);
- $info[] = array(
- 'table' => $relatedTables,
- 'when' => 'AFTER',
- 'event' => array('INSERT', 'UPDATE'),
- 'sql' => "\nUPDATE civicrm_contact SET modified_date = CURRENT_TIMESTAMP WHERE id = NEW.contact_id;\n",
- );
- $info[] = array(
- 'table' => $relatedTables,
- 'when' => 'AFTER',
- 'event' => array('DELETE'),
- 'sql' => "\nUPDATE civicrm_contact SET modified_date = CURRENT_TIMESTAMP WHERE id = OLD.contact_id;\n",
- );
+ self::generateTimestampTriggers($info, $tableName, $relatedTables, 'contact_id');
// Update timestamp when modifying related custom-data tables
$customGroupTables = array();
$customGroupTables[] = $customGroupDAO->table_name;
}
if (!empty($customGroupTables)) {
- $info[] = array(
- 'table' => $customGroupTables,
- 'when' => 'AFTER',
- 'event' => array('INSERT', 'UPDATE'),
- 'sql' => "\nUPDATE civicrm_contact SET modified_date = CURRENT_TIMESTAMP WHERE id = NEW.entity_id;\n",
- );
- $info[] = array(
- 'table' => $customGroupTables,
- 'when' => 'AFTER',
- 'event' => array('DELETE'),
- 'sql' => "\nUPDATE civicrm_contact SET modified_date = CURRENT_TIMESTAMP WHERE id = OLD.entity_id;\n",
- );
+ self::generateTimestampTriggers($info, $tableName, $customGroupTables, 'entity_id');
}
// Update phone table to populate phone_numeric field