addTemplateDir( __DIR__.'/templates'); } if ($path == "civicrm/civirules/conditions/webform_civirules_contactisnthcontactonwebform") { set_include_path(get_include_path() . PATH_SEPARATOR . __DIR__); $smarty = CRM_Core_Smarty::singleton(); $smarty->addTemplateDir( __DIR__.'/templates'); } } /** * Implementation of webform_submission_insert for CiviRules Webform Submission Trigger * * @param $node * @param $submission */ function webform_civirules_webform_submission_insert($node, $submission) { 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. * * @param $module_list * @param $context */ function webform_civirules_module_implements_alter(&$module_list, $context) { switch ($context) { case 'webform_submission_insert': $old_module_list = $module_list; $module_list = array(); // Walk trhough the old list and add the hook to the new list. Unles the module // is this module. Then don't add this module but only add this module after webform_civicrm. foreach ($old_module_list as $module => $hook) { if ($module != 'webform_civirules') { $module_list[$module] = $hook; } if ($module == 'webform_civicrm') { $module_list['webform_civirules'] = $old_module_list['webform_civirules']; } } break; } } 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 * * Generate a list of entities to create/deactivate/delete when this module * is installed, disabled, uninstalled. * * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_managed */ function webform_civirules_civicrm_managed(&$entities) { if (_webform_civirules_is_civirules_installed()) { $select = "SELECT COUNT(*) FROM civirule_trigger WHERE `name` = 'webform_civirules_submission'"; $count = CRM_Core_DAO::singleValueQuery($select); if ($count == 0) { $nowDate = new DateTime(); try { civicrm_api3('CiviRuleTrigger', 'create', [ 'name' => 'webform_civirules_submission', 'label' => 'Webform is submitted', 'class_name' => 'CRM_WebformCivirules_Trigger', 'is_active' => 1, 'created_date' => $nowDate->format('Y-m-d') ]); } catch (CiviCRM_API3_Exception $ex) { throw new Exception('Could not create required trigger for webform submission in ' . __METHOD__ . ', contact your system administrator. Error from API CiviRulesTrigger create: ' . $ex->getMessage()); } } $select = "SELECT COUNT(*) FROM civirule_condition WHERE `name` = 'webform_civirules_webform_is'"; $count = CRM_Core_DAO::singleValueQuery($select); if ($count == 0) { CRM_Core_DAO::executeQuery("INSERT INTO civirule_condition (name, label, class_name, is_active) VALUES('webform_civirules_webform_is', 'Webform is', 'CRM_WebformCivirules_Condition_WebformIs', 1);"); } $select = "SELECT COUNT(*) FROM civirule_condition WHERE `name` = 'webform_civirules_isnthcontactonwebform'"; $count = CRM_Core_DAO::singleValueQuery($select); if ($count == 0) { CRM_Core_DAO::executeQuery("INSERT INTO civirule_condition (name, label, class_name, is_active) VALUES('webform_civirules_isnthcontactonwebform', 'Is nth contact on webform', 'CRM_WebformCivirules_Condition_IsNthContactOnWebform', 1);"); } } } function webform_civirules_civicrm_xmlMenu( &$files ) { $files[] = dirname(__FILE__).'/xml/Menu.xml'; } /** * Helper function to check whether CiviRules is installed * * @return bool * @throws \CiviCRM_API3_Exception */ function _webform_civirules_is_civirules_installed() { civicrm_initialize(); if (civicrm_api3('Extension', 'get', ['key' => 'civirules', 'status' => 'installed'])['count']) { return true; } elseif (civicrm_api3('Extension', 'get', ['key' => 'org.civicoop.civirules', 'status' => 'installed'])['count']) { return true; } return false; }