webform_civirules.module 6.3 KB
Newer Older
jaapjansma's avatar
jaapjansma committed
1 2 3 4 5 6 7 8 9
<?php

/**
 * Implements hook_menu_get_item_alter().
 *
 * Make sure our classes get loaded when viewing the condition settings form.
 * This hack is needed because we are not a native civicrm extension.
 */
function webform_civirules_menu_get_item_alter(&$router_item, $path, $original_map) {
10
  civicrm_initialize();
jaapjansma's avatar
jaapjansma committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
  if ($path == "civicrm/civirules/conditions/webform_civirules_webform_is") {
    set_include_path(get_include_path() . PATH_SEPARATOR . __DIR__);
    $smarty = CRM_Core_Smarty::singleton();
    $smarty->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) {
30 31 32 33 34 35 36 37 38 39 40 41 42 43
  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();
jaapjansma's avatar
jaapjansma committed
44 45 46
  }
}

47 48 49 50 51 52 53 54 55
/**
 * 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();
}

jaapjansma's avatar
jaapjansma committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
/**
 * 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;
  }
}

82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
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();
}

jaapjansma's avatar
jaapjansma committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
/**
 * 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;
}