Skip to content
Snippets Groups Projects
Commit fc521dca authored by Sean Madsen's avatar Sean Madsen
Browse files

hook_civicrm_trigger_info - clean up after content import to close #83

parent 5dee4a74
No related branches found
No related tags found
No related merge requests found
# hook_civicrm_trigger_info # hook_civicrm_trigger_info
## Description
Define MYSQL Triggers. Using the hooks causes them not to clash with Define MYSQL Triggers. Using the hooks causes them not to clash with
core or other extension triggers. They are compiled into one trigger core or other extension triggers. They are compiled into one trigger
with core triggers. with core triggers.
Once the function is create, a trigger rebuild will have to be done to !!! note
create the new trigger Once the function is created, visit the following URL to rebuild triggers
and create to create the new trigger:
http//yoursite/civicrm/menu/rebuild&reset=1&triggerRebuild=1
`http://example.com/civicrm/menu/rebuild&reset=1&triggerRebuild=1`
/**
* hook_civicrm_triggerInfo() ## Definition
* ```php
hook_civicrm_triggerInfo(&$info, $tableName)
* Add trigger to update custom region field based on postcode (using a lookup table) ```
* ## Parameters
* Note that we have hard-coded a prioritisation of location types into this (since it's customer specific code * array `$info` - array of triggers to be created
* string `$tableName` - not sure how this bit works
* and unlikely to change)
*
## Returns
* @param array $info (reference) array of triggers to be created
- ??
* @param string $tableName - not sure how this bit works
* ## Example
**/ Add trigger to update custom region field based on postcode (using a lookup
table)
Note that this example uses hard-coded a prioritisation of location types
function regionfields_civicrm_triggerInfo(&$info, $tableName) { (since it was customer specific code and unlikely to change).
$table_name = 'civicrm_value_region_13'; ```php
function regionfields_civicrm_triggerInfo(&$info, $tableName) {
$customFieldID = 45; $table_name = 'civicrm_value_region_13';
$customFieldID = 45;
$columnName = 'region_45'; $columnName = 'region_45';
$sourceTable = 'civicrm_address';
$sourceTable = 'civicrm_address'; $locationPriorityOrder = '1, 3, 5, 2, 4, 6'; // hard coded prioritisation of addresses
$zipTable = 'CANYRegion';
$locationPriorityOrder = '1, 3, 5, 2, 4, 6'; // hard coded prioritisation of addresses if(civicrm_api3('custom_field', 'getcount', array('id' => $customFieldID, 'column_name' => 'region_45', 'is_active' => 1)) == 0) {
return;
$zipTable = 'CANYRegion'; }
if(civicrm_api3('custom_field', 'getcount', array('id' => $customFieldID, 'column_name' => 'region_45', 'is_active' => 1)) == 0) { $sql = "
REPLACE INTO `$table_name` (entity_id, $columnName)
return; SELECT * FROM (
SELECT contact_id, b.region
} FROM
civicrm_address a INNER JOIN $zipTable b ON a.postal_code = b.zip
WHERE a.contact_id = NEW.contact_id
ORDER BY FIELD(location_type_id, $locationPriorityOrder )
$sql = " ) as regionlist
GROUP BY contact_id;
REPLACE INTO `$table_name` (entity_id, $columnName) ";
$sql_field_parts = array();
SELECT * FROM (
$info[] = array(
SELECT contact_id, b.region 'table' => $sourceTable,
'when' => 'AFTER',
FROM 'event' => 'INSERT',
'sql' => $sql,
civicrm_address a INNER JOIN $zipTable b ON a.postal_code = b.zip );
$info[] = array(
WHERE a.contact_id = NEW.contact_id 'table' => $sourceTable,
'when' => 'AFTER',
ORDER BY FIELD(location_type_id, $locationPriorityOrder ) 'event' => 'UPDATE',
'sql' => $sql,
) as regionlist );
// For delete, we reference OLD.contact_id instead of NEW.contact_id
GROUP BY contact_id; $sql = str_replace('NEW.contact_id', 'OLD.contact_id', $sql);
$info[] = array(
"; 'table' => $sourceTable,
'when' => 'AFTER',
$sql_field_parts = array(); 'event' => 'DELETE',
'sql' => $sql,
);
}
$info[] = array( ```
'table' => $sourceTable,
'when' => 'AFTER',
'event' => 'INSERT',
'sql' => $sql,
);
$info[] = array(
'table' => $sourceTable,
'when' => 'AFTER',
'event' => 'UPDATE',
'sql' => $sql,
);
// For delete, we reference OLD.contact_id instead of NEW.contact_id
$sql = str_replace('NEW.contact_id', 'OLD.contact_id', $sql);
$info[] = array(
'table' => $sourceTable,
'when' => 'AFTER',
'event' => 'DELETE',
'sql' => $sql,
);
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment