Commit 5aa93d25 authored by jaapjansma's avatar jaapjansma

Updated also trigger civirules when webform is submitted for online payment within webform civicrm.

parent afb92e63
......@@ -6,6 +6,7 @@ version = 7.x-2.0
dependencies[] = civicrm (>=4.7)
dependencies[] = webform (>=7.x-4.15)
files[] = webform_civirules_trigger.inc
files[] = CRM/WebformCivirules/Trigger.php
files[] = CRM/WebformCivirules/TriggerData.php
files[] = CRM/WebformCivirules/Condition/WebformIs.php
......
......@@ -8,3 +8,37 @@ function webform_civirules_disable() {
} catch (CiviCRM_API3_Exception $ex) {}
}
}
function webform_civirules_schema() {
$schema['webform_civicrm_submissions_civirules_trigger'] = array(
'description' => 'Link between form submissions and CiviCRM data.',
'fields' => array(
'sid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => 'Webform Submission ID',
),
'trigger_fired' => array(
'type' => 'int',
'not null' => TRUE,
'default' => '0',
'description' => 'Whether we triggered civirules for this submission',
),
),
'primary key' => array('sid'),
);
return $schema;
}
/**
* Add the webform_civicrm_submissions_civirules_triggers table.
* This table is ued to determine whether a certain submission did already trigger civirules.
*
* @return null|string
*/
function webform_civirules_update_1() {
$schema = webform_civirules_schema();
db_create_table('webform_civicrm_submissions_civirules_trigger', $schema['webform_civicrm_submissions_civirules_trigger']);
}
......@@ -26,24 +26,32 @@ function webform_civirules_menu_get_item_alter(&$router_item, $path, $original_m
* @param $submission
*/
function webform_civirules_webform_submission_insert($node, $submission) {
$db = db_query('SELECT * FROM {webform_civicrm_submissions} WHERE sid = :sid', array(':sid' => $submission->sid));
if ($row = $db->fetchAssoc()) {
$data = unserialize($row['civicrm_data']);
$contacts = array();
if ($row['contact_id']) {
foreach (explode('-', trim($row['contact_id'], '-')) as $c => $cid) {
$contacts[$c] = $cid;
}
foreach($contacts as $c => $cid) {
if ($cid) {
$trigger = new CRM_WebformCivirules_Trigger();
$trigger->triggerTrigger($cid, $c, $data, $contacts, $submission->sid, $submission->nid);
}
}
if (!empty($node->webform_civicrm)) {
webform_civirules_trigger::getSingleton()->setSubmission($submission)->trigger();
}
}
/**
* Implementation of webform_submission_insert for CiviRules Webform Submission Trigger
*
* @param $node
* @param $submission
*/
function webform_civirules_webform_submission_update($node, $submission) {
if (!empty($node->webform_civicrm)) {
webform_civirules_trigger::getSingleton()->setSubmission($submission)->trigger();
}
}
/**
* Implements hook_webform_submission_delete().
*/
function webform_civirules_webform_submission_delete($node, $submission) {
db_delete('webform_civicrm_submissions_civirules_trigger')
->condition('sid', $submission->sid)
->execute();
}
/**
* Make sure that the hooks gets executed after webform_civicrm has done its processing.
*
......@@ -70,6 +78,36 @@ function webform_civirules_module_implements_alter(&$module_list, $context) {
}
}
function webform_civirules_webform_submission_presave($node, &$submission) {
if (!$node->webform_civicrm) {
return; // is not a webform civicrm submission.
}
// Store the submission object so we can use it later on
// when the sid is set.
// As submission is an object and object are storeed by reference we can retrieve the new submission id.
webform_civirules_trigger::getSingleton()->setSubmission($submission);
}
/**
* Implements hook_civicrm_alterPaymentProcessorParams().
*
* In this hook we check whether we do an online payment from a webform.
* If so we find out what the submission id is and and trigger civirules from that submission.
* Why do we do this? Because the webform_civicrm redirects to the pyament processor page and this
* levaes our hook webform_submission_insert uncalled and webform_submission_update would never be called afterwards.
*
* @param $paymentObj
* @param $rawParams
* @param $cookedParams
*/
function webform_civirules_civicrm_alterPaymentProcessorParams($paymentObj,&$rawParams, &$cookedParams) {
if (!isset($rawParams['webform_redirect_success'])) {
return; // this is not a webform submission
}
webform_civirules_trigger::getSingleton()->trigger();
}
/**
* Implementation of hook_civicrm_managed
*
......
<?php
class webform_civirules_trigger {
private static $singleton;
private $submission;
private function __construct() {
}
/**
* @return \webform_civirules_trigger
*/
public static function getSingleton() {
if (!self::$singleton) {
self::$singleton = new webform_civirules_trigger();
}
return self::$singleton;
}
/**
* @return \webform_civirules_trigger
*/
public function setSubmission($submission) {
$this->submission = $submission;
return $this;
}
public function trigger() {
if (!$this->submission || !$this->submission->sid || !$this->submission->nid) {
return;
}
$db = db_query('SELECT * FROM {webform_civicrm_submissions_civirules_trigger} WHERE sid = :sid', array(':sid' => $this->submission->sid));
if (($row = $db->fetchAssoc()) && $row->trigger_fired) {
// This submission did already fire a civirules trigger.
return;
}
$db = db_query('SELECT * FROM {webform_civicrm_submissions} WHERE sid = :sid', array(':sid' => $this->submission->sid));
if ($row = $db->fetchAssoc()) {
// First update webform_civicrm_submissions_civirules_trigger so that we do not trigger again on this submission.
$webform_civicrm_submissions_civirules_trigger = array(
'sid' => $this->submission->sid,
'trigger_fired' => 1,
);
drupal_write_record('webform_civicrm_submissions_civirules_trigger',$webform_civicrm_submissions_civirules_trigger);
$data = unserialize($row['civicrm_data']);
$contacts = array();
if ($row['contact_id']) {
foreach (explode('-', trim($row['contact_id'], '-')) as $c => $cid) {
$contacts[$c] = $cid;
}
foreach($contacts as $c => $cid) {
if ($cid) {
$trigger = new CRM_WebformCivirules_Trigger();
$trigger->triggerTrigger($cid, $c, $data, $contacts, $this->submission->sid, $this->submission->nid);
}
}
}
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment