Commit 72bf3ed5 authored by jaapjansma's avatar jaapjansma

Initial commit

parents
<?php
/**
* @author Jaap Jansma <jaap.jansma@civicoop.org>
* @license AGPL-3.0
*/
/**
* Form controller class
*
* @see http://wiki.civicrm.org/confluence/display/CRMDOC43/QuickForm+Reference
*/
class CRM_WebformCivirules_Condition_Form_IsNthContactOnWebform extends CRM_CivirulesConditions_Form_Form {
public function preProcess() {
parent::preProcess();
}
/**
* Overridden parent method to build the form
*
* @access public
*/
public function buildQuickForm() {
$this->add('hidden', 'rule_condition_id');
$nodes= array();
$this->add('text', 'contact_num', ts('Contact number on webform'), true);
$this->addButtons(array(
array('type' => 'next', 'name' => ts('Save'), 'isDefault' => TRUE,),
array('type' => 'cancel', 'name' => ts('Cancel'))));
parent::buildQuickForm();
}
/**
* Overridden parent method to set default values
*
* @return array $defaultValues
* @access public
*/
public function setDefaultValues() {
$defaultValues = parent::setDefaultValues();
$data = unserialize($this->ruleCondition->condition_params);
if (!empty($data['contact_num'])) {
$defaultValues['contact_num'] = $data['contact_num'];
} else {
$defaultValues['contact_num'] = 1;
}
return $defaultValues;
}
/**
* Overridden parent method to perform data processing once form is submitted
*
* @access public
*/
public function postProcess() {
$data['contact_num'] = $this->_submitValues['contact_num'];
$this->ruleCondition->condition_params = serialize($data);
$this->ruleCondition->save();
parent::postProcess();
}
}
\ No newline at end of file
<?php
/**
* @author Jaap Jansma <jaap.jansma@civicoop.org>
* @license AGPL-3.0
*/
/**
* Form controller class
*
* @see http://wiki.civicrm.org/confluence/display/CRMDOC43/QuickForm+Reference
*/
class CRM_WebformCivirules_Condition_Form_WebformIs extends CRM_CivirulesConditions_Form_Form {
public function preProcess() {
parent::preProcess();
}
/**
* Overridden parent method to build the form
*
* @access public
*/
public function buildQuickForm() {
$this->add('hidden', 'rule_condition_id');
$nodes= array();
$query = db_query("SELECT n.nid, n.title FROM webform_civicrm_forms INNER JOIN node n ON webform_civicrm_forms.nid = n.nid");
$result = $query->fetchAllAssoc('nid');
foreach($result as $node) {
$nodes[$node->nid] = $node->title . '(nid: '.$node->nid.')';
}
$nodes = array('- select -') + $nodes;
asort($nodes);
$this->add('select', 'node_ids', ts('Node'), $nodes, true,
array('id' => 'node_ids', 'multiple' => 'multiple','class' => 'crm-select2'));
$this->add('select', 'operator', ts('Operator'), array('is one of' => ts('Is one of'), 'is not one of' => ts('Is NOT one of')), true);
$this->addButtons(array(
array('type' => 'next', 'name' => ts('Save'), 'isDefault' => TRUE,),
array('type' => 'cancel', 'name' => ts('Cancel'))));
parent::buildQuickForm();
}
/**
* Overridden parent method to set default values
*
* @return array $defaultValues
* @access public
*/
public function setDefaultValues() {
$defaultValues = parent::setDefaultValues();
$data = unserialize($this->ruleCondition->condition_params);
if (!empty($data['node_ids'])) {
$defaultValues['node_ids'] = $data['node_ids'];
}
if (!empty($data['operator'])) {
$defaultValues['operator'] = $data['operator'];
}
return $defaultValues;
}
/**
* Overridden parent method to perform data processing once form is submitted
*
* @access public
*/
public function postProcess() {
$data['operator'] = $this->_submitValues['operator'];
$data['node_ids'] = $this->_submitValues['node_ids'];
$this->ruleCondition->condition_params = serialize($data);
$this->ruleCondition->save();
parent::postProcess();
}
}
\ No newline at end of file
<?php
/**
* @author Jaap Jansma <jaap.jansma@civicoop.org>
* @license AGPL-3.0
*/
class CRM_WebformCivirules_Condition_IsNthContactOnWebform extends CRM_Civirules_Condition {
private $conditionParams = array();
/**
* Method to set the Rule Condition data
*
* @param array $ruleCondition
* @access public
*/
public function setRuleConditionData($ruleCondition) {
parent::setRuleConditionData($ruleCondition);
$this->conditionParams = array();
if (!empty($this->ruleCondition['condition_params'])) {
$this->conditionParams = unserialize($this->ruleCondition['condition_params']);
}
}
/**
* This method returns true or false when an condition is valid or not
*
* @param CRM_Civirules_TriggerData_TriggerData $triggerData
* @return bool
* @access public
* @abstract
*/
public function isConditionValid(CRM_Civirules_TriggerData_TriggerData $triggerData) {
if (!$triggerData instanceof CRM_WebformCivirules_TriggerData) {
return false;
}
// The current contact number in the trigger data starts counting with zero.
// The user enters the contact number assuming it counts from one.
$current_contact_num = $triggerData->getContactNumber() + 1;
if ($this->conditionParams['contact_num'] == $current_contact_num) {
return true;
}
return false;
}
/**
* Returns a redirect url to extra data input from the user after adding a condition
*
* Return false if you do not need extra data input
*
* @param int $ruleConditionId
* @return bool|string
* @access public
* @abstract
*/
public function getExtraDataInputUrl($ruleConditionId) {
return CRM_Utils_System::url('civicrm/civirules/conditions/webform_civirules_contactisnthcontactonwebform', 'rule_condition_id='.$ruleConditionId);
}
/**
* Returns a user friendly text explaining the condition params
* e.g. 'Older than 65'
*
* @return string
* @access public
*/
public function userFriendlyConditionParams() {
return ts('Contact is contact number %1 on the webform', array(1 => $this->conditionParams['contact_num']));
}
/**
* This function validates whether this condition works with the selected trigger.
*
* This function could be overriden in child classes to provide additional validation
* whether a condition is possible in the current setup. E.g. we could have a condition
* which works on contribution or on contributionRecur then this function could do
* this kind of validation and return false/true
*
* @param CRM_Civirules_Trigger $trigger
* @param CRM_Civirules_BAO_Rule $rule
* @return bool
*/
public function doesWorkWithTrigger(CRM_Civirules_Trigger $trigger, CRM_Civirules_BAO_Rule $rule) {
if ($trigger instanceof CRM_WebformCivirules_Trigger) {
return TRUE;
}
return FALSE;
}
}
\ No newline at end of file
<?php
/**
* @author Jaap Jansma <jaap.jansma@civicoop.org>
* @license AGPL-3.0
*/
class CRM_WebformCivirules_Condition_WebformIs extends CRM_Civirules_Condition {
private $conditionParams = array();
/**
* Method to set the Rule Condition data
*
* @param array $ruleCondition
* @access public
*/
public function setRuleConditionData($ruleCondition) {
parent::setRuleConditionData($ruleCondition);
$this->conditionParams = array();
if (!empty($this->ruleCondition['condition_params'])) {
$this->conditionParams = unserialize($this->ruleCondition['condition_params']);
}
}
/**
* This method returns true or false when an condition is valid or not
*
* @param CRM_Civirules_TriggerData_TriggerData $triggerData
* @return bool
* @access public
* @abstract
*/
public function isConditionValid(CRM_Civirules_TriggerData_TriggerData $triggerData) {
if (!$triggerData instanceof CRM_WebformCivirules_TriggerData) {
return false;
}
if ($this->conditionParams['is not one of']) {
if (!in_array($triggerData->getNodeId(), $this->conditionParams['node_ids'])) {
return true;
}
} else {
if (in_array($triggerData->getNodeId(), $this->conditionParams['node_ids'])) {
return true;
}
}
return false;
}
/**
* Returns a redirect url to extra data input from the user after adding a condition
*
* Return false if you do not need extra data input
*
* @param int $ruleConditionId
* @return bool|string
* @access public
* @abstract
*/
public function getExtraDataInputUrl($ruleConditionId) {
return CRM_Utils_System::url('civicrm/civirules/conditions/webform_civirules_webform_is', 'rule_condition_id='.$ruleConditionId);
}
/**
* Returns a user friendly text explaining the condition params
* e.g. 'Older than 65'
*
* @return string
* @access public
*/
public function userFriendlyConditionParams() {
$nodes = node_load_multiple($this->conditionParams['node_ids']);
$title = 'Webform is one of %1';
if ($this->conditionParams['operator'] == 'is not one of') {
$title = 'Webform is not one of %1';
}
$nodeTitles = array();
foreach($nodes as $node) {
$nodeTitles[] = "'".$node->title ." (nid: ".$node->nid.")'";
}
return ts($title, array(1 => implode(', ', $nodeTitles)));
}
/**
* This function validates whether this condition works with the selected trigger.
*
* This function could be overriden in child classes to provide additional validation
* whether a condition is possible in the current setup. E.g. we could have a condition
* which works on contribution or on contributionRecur then this function could do
* this kind of validation and return false/true
*
* @param CRM_Civirules_Trigger $trigger
* @param CRM_Civirules_BAO_Rule $rule
* @return bool
*/
public function doesWorkWithTrigger(CRM_Civirules_Trigger $trigger, CRM_Civirules_BAO_Rule $rule) {
if ($trigger instanceof CRM_WebformCivirules_Trigger) {
return TRUE;
}
return FALSE;
}
}
\ No newline at end of file
<?php
/**
* @author Jaap Jansma <jaap.jansma@civicoop.org>
* @license AGPL-3.0
*/
class CRM_WebformCivirules_Trigger extends CRM_Civirules_Trigger {
/**
* Returns an array of entities on which the trigger reacts
*
* @return CRM_Civirules_TriggerData_EntityDefinition
*/
protected function reactOnEntity() {
return new CRM_Civirules_TriggerData_EntityDefinition('webform_civirules_submission', 'webform_civirules_submission', null, 'webform_civirules_submission');
}
public function triggerTrigger($contact_id, $contact_num, $data, $contacts, $submission_id, $node_id) {
$triggerData = new CRM_WebformCivirules_TriggerData($contact_id, $contact_num, $data, $contacts, $submission_id, $node_id);
$this->alterTriggerData($triggerData);
$triggers = CRM_Civirules_BAO_Rule::findRulesByClassname(get_class($this));
foreach ($triggers as $trigger) {
CRM_Civirules_Engine::triggerRule($trigger, $triggerData);
}
}
}
\ No newline at end of file
<?php
/**
* @author Jaap Jansma <jaap.jansma@civicoop.org>
* @license AGPL-3.0
*/
class CRM_WebformCivirules_TriggerData extends CRM_Civirules_TriggerData_TriggerData {
protected $submission_id;
protected $node_id;
protected $contact_num;
protected $contacts;
public function __construct($contact_id, $contact_num, $data, $contacts, $submission_id, $node_id) {
$this->submission_id = $submission_id;
$this->node_id = $node_id;
$this->contact_num = $contact_num;
foreach($data as $entity => $datasets) {
foreach ($datasets as $rec_num => $record) {
if ($rec_num == 1) {
$this->setEntityData($entity, $record);
}
$this->setEntityData($entity . '_' . $rec_num, $record);
}
}
$this->contact_id = $contact_id;
$this->contacts = $contacts;
parent::__construct();
}
public function getSubmissionId() {
return $this->submission_id;
}
public function getNodeId() {
return $this->node_id;
}
public function getContactNumber() {
return $this->contact_num;
}
public function getContacts() {
$return = array();
foreach($this->contacts as $c => $cid) {
if ($cid) {
$return[$c] = $cid;
}
}
return $return;
}
}
\ No newline at end of file
# Webform CiviRules
This is a drupal module which triggers civirules after a webform submission. CiviRules is triggered for each contact on the webform.
This module also contains the following conditions:
* **Webform is**
* **Is Nth contact on the webform**
**This module is an improvement of [org.civicoop.civiruleswebform](https://github.com/CiviCooP/org.civicoop.civiruleswebform)**
<h3>{$ruleConditionHeader}</h3>
<div class="crm-block crm-form-block crm-civirule-rule_condition-block-isnthcontactonwebform">
<div class="crm-section">
<div class="label">{$form.contact_num.label}</div>
<div class="content">{$form.contact_num.html}</div>
<div class="clear"></div>
</div>
</div>
<div class="crm-submit-buttons">
{include file="CRM/common/formButtons.tpl" location="bottom"}
</div>
\ No newline at end of file
<h3>{$ruleConditionHeader}</h3>
<div class="crm-block crm-form-block crm-civirule-rule_condition-block-webform_is">
<div class="crm-section">
<div class="label">{$form.operator.label}</div>
<div class="content">{$form.operator.html}</div>
<div class="clear"></div>
</div>
<div class="crm-section sector-section">
<div class="label">
<label for="webform-select">{ts}Webform(s){/ts}</label>
</div>
<div class="content crm-select-container" id="webform_block">
{$form.node_ids.html}
</div>
<div class="clear"></div>
</div>
</div>
<div class="crm-submit-buttons">
{include file="CRM/common/formButtons.tpl" location="bottom"}
</div>
\ No newline at end of file
name = CiviRules Webform CiviCRM Submission Trigger
description = Fire civirules trigger engine on webform submission
package = CiviCRM
core = 7.x
version = 7.x-2.0
dependencies[] = civicrm (>=4.7)
dependencies[] = webform (>=7.x-4.15)
files[] = CRM/WebformCivirules/Trigger.php
files[] = CRM/WebformCivirules/TriggerData.php
files[] = CRM/WebformCivirules/Condition/WebformIs.php
files[] = CRM/WebformCivirules/Condition/IsNthContactOnWebform.php
files[] = CRM/WebformCivirules/Condition/Form/WebformIs.php
files[] = CRM/WebformCivirules/Condition/Form/IsNthContactOnWebform.php
\ No newline at end of file
<?php
function webform_civirules_disable() {
if (_webform_civirules_is_civirules_installed()) {
try {
$triggerId = civicrm_api3('CiviRuleTrigger', 'getvalue', array('name' => 'webform_civirules_submission', 'return' => 'id'));
civicrm_api3('CiviRuleTrigger', 'delete', array('id' => $triggerId));
} catch (CiviCRM_API3_Exception $ex) {}
}
}
\ No newline at end of file
<?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) {
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) {
$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);
}
}
}
}
}
/**
* 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;
}
}
/**
* 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;
}
\ No newline at end of file
<?xml version="1.0"?>
<menu>
<item>
<path>civicrm/civirules/conditions/webform_civirules_webform_is</path>
<page_callback>CRM_WebformCivirules_Condition_Form_WebformIs</page_callback>
<title>Webform is</title>
<access_arguments>access CiviCRM</access_arguments>
<access_arguments>administer CiviCRM</access_arguments>
</item>
<item>
<path>civicrm/civirules/conditions/webform_civirules_contactisnthcontactonwebform</path>
<page_callback>CRM_WebformCivirules_Condition_Form_IsNthContactOnWebform</page_callback>
<title>Contact is nth contact on the webform</title>
<access_arguments>access CiviCRM</access_arguments>
<access_arguments>administer CiviCRM</access_arguments>
</item>
</menu>
\ 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