Skip to content
Snippets Groups Projects
Commit 02ee8b55 authored by jaapjansma's avatar jaapjansma
Browse files

added first draft of extra parameter form

parent a90b3f98
Branches
Tags
No related merge requests found
<?php
class CRM_Civirules_Conditions_AgeComparison extends CRM_Civirules_Conditions_DataComparison {
/**
* Returns value of the field
*
* @parameter CRM_Civirules_EventData_EventData $eventData
* @return mixed
*/
protected function getFieldValue(CRM_Civirules_EventData_EventData $eventData) {
$birth_date = civicrm_api3('Contact', 'getvalue', array('id' => $eventData->getContactId(), 'return' => 'birth_date'));
if ($birth_date) {
return newDateTime($birth_date)->diff(new DateTime('now'))->y;
}
return false; //undefined birth date
}
/**
* Returns the value for the data comparison
* @return mixed
*/
protected function getComparisonValue() {
throw new exception('return age value');
}
/**
* Returns an operator for comparison
*
* Valid operators are:
* - equal: =
* - not equal: !=
* - greater than: >
* - lesser than: <
* - greater than or equal: >=
* - lesser than or equal: <=
*
* @return an operator for comparison
*/
protected function getOperator() {
throw new exception('return operator');
}
}
\ No newline at end of file
......@@ -2,6 +2,25 @@
abstract class CRM_Civirules_Conditions_Condition {
private $ruleConditionId = false;
private $ruleCondition = false;
protected function __construct($ruleConditionId=false) {
$this->ruleConditionId = $ruleConditionId;
$this->ruleCondition = false;
if ($this->getRuleConditionId()) {
$ruleCondition = new CRM_Civirules_BAO_RuleCondition();
$ruleCondition->id = $this->getRuleConditionId();
if ($ruleCondition->find(true)) {
$ruleConditionData = array();
CRM_Core_DAO::storeValues($ruleCondition, $ruleConditionData);
$this->ruleCondition = $ruleConditionData;
}
}
}
/**
* This function returns true or false when an condition is valid or not
*
......@@ -10,4 +29,55 @@ abstract class CRM_Civirules_Conditions_Condition {
*/
public abstract function isConditionValid(CRM_Civirules_EventData_EventData $eventData);
/**
* Returns wether this condition has an extra input form
*
* If your condition contains a form override this method and return a CRM_Civirules_Conditions_Form_Form
* If your condition does not have/need an extra input form then return this method should return false
*
* @return bool|CRM_Civirules_Conditions_Form_Form
*/
public function getForm() {
return false;
}
/**
* Returns the extra condition data
*
* @return array
*/
public function getConditionData() {
return array();
}
/*
* Transforms condition data so that it could be stored in the database
*
* @param array $data
* @return string
*/
public function transformConditionData($data=array()) {
return '';
}
/**
* Returns the Id of the current condition, or false if not set
*
* @return int
*/
public function getRuleConditionId() {
return $this->ruleConditionId;
}
/**
* Returns the rule condition or false when not set
*
* @return array|bool
*/
public function getRuleCondition() {
return $this->ruleCondition;
}
}
\ No newline at end of file
<?php
abstract class CRM_Civirules_Conditions_DataComparison extends CRM_Civirules_Conditions_Condition {
/**
* Returns value of the field
*
* @parameter CRM_Civirules_EventData_EventData $eventData
* @return mixed
*/
abstract protected function getFieldValue(CRM_Civirules_EventData_EventData $eventData);
/**
* Returns the value for the data comparison
* @return mixed
*/
abstract protected function getComparisonValue();
/**
* Returns an operator for comparison
*
* Valid operators are:
* - equal: =
* - not equal: !=
* - greater than: >
* - lesser than: <
* - greater than or equal: >=
* - lesser than or equal: <=
*
* @return an operator for comparison
*/
abstract protected function getOperator();
public function isConditionValid(CRM_Civirules_EventData_EventData $eventData) {
$value = $this->getFieldValue($eventData);
$compareValue = $this->getComparisonValue();
return $this->compare($value, $compareValue, $this->getOperator());
}
protected function compare($leftValue, $rightValue, $operator) {
switch ($operator) {
case '=':
return ($leftValue == $rightValue) ? true : false;
break;
case '>':
return ($leftValue > $rightValue) ? true : false;
break;
case '<':
return ($leftValue < $rightValue) ? true : false;
break;
case '>=':
return ($leftValue >= $rightValue) ? true : false;
break;
case '<=':
return ($leftValue <= $rightValue) ? true : false;
break;
case '!=':
return ($leftValue != $rightValue) ? true : false;
break;
default:
return false;
break;
}
return false;
}
/**
* Returns wether this condition has an extra input form
*
* If your condition contains a form override this method and return a CRM_Civirules_Conditions_Form_Form
* If your condition does not have/need an extra input form then return this method should return false
*
* @return bool|CRM_Civirules_Conditions_Form_Form
*/
public function getForm() {
return new CRM_Civirules_Conditions_Form_ValueComparison($this);
}
/**
* Returns the extra condition data
*
* @return array
*/
public function getConditionData() {
$return = array(
'operator' => '=',
'value' => '',
);
$ruleCondition = $this->getRuleCondition();
if (is_array($ruleCondition)) {
$data = CRM_Civirules_Utils_Parameters::convertFromMultiline($ruleCondition['data']);
if (!empty($data['operator'])) {
$return['operator'] = $data['operator'];
}
if (!empty($data['value'])) {
$return['value'] = $data['value'];
}
}
return $return;
}
/*
* Transforms condition data so that it could be stored in the database
*
* @param array $data
* @return string
*/
public function transformConditionData($data=array()) {
return CRM_Civirules_Utils_Parameters::convertToMultiline($data);
}
}
\ No newline at end of file
<?php
abstract class CRM_Civirules_Conditions_Form_Form {
protected $condition;
public function __construct(CRM_Civirules_Conditions_Condition $condition) {
$this->condition = $condition;
}
/**
* You can use this function to add elements to a form
*
* @param CRM_Core_Form $form
* @return void
*/
abstract public function buildForm(CRM_Core_Form &$form);
/**
* You can use this function to set default values
*
* @param CRM_Core_Form $form
* @param array $defaultValues
* @return array
*/
abstract public function defaultValues(CRM_Core_Form &$form, $defaultValues);
/**
* You can use this to post process the form
*
* This function should return a string with the options ready for saving into the database
*
* @param CRM_Core_Form $form
* @param array $submittedValues
* @return string
*/
abstract public function postProcess(CRM_Core_Form &$form, $submittedValues);
}
\ No newline at end of file
<?php
class CRM_Civirules_Conditions_Form_ValueComparison extends CRM_Civirules_Conditions_Form_Form {
/**
* You can use this function to add elements to a form
*
* @param CRM_Core_Form $form
* @return void
*/
public function buildForm(CRM_Core_Form &$form) {
$form->addSelect('operator', ts('Operator'), array(
'=' => ts('equals'),
'!=' => ts('not equals'),
'>' => ts('greater than'),
'<' => ts('less than'),
'>=' => ts('greater than or equals'),
'<=' => ts('less than or equals'),
), true);
$this->add('text', 'value', ts('Compare value'), true);
}
/**
* You can use this function to set default values
*
* @param CRM_Core_Form $form
* @return array
*/
public function defaultValues(CRM_Core_Form &$form, $defaultValues) {
$data = $this->condition->getConditionData();
if (!empty($data['operator'])) {
$defaultValues['operator'] = $data['operator'];
}
if (!empty($data['value'])) {
$defaultValues['value'] = $data['value'];
}
return $defaultValues;
}
/**
* You can use this to post process the form
*
* This function should return a string with the options ready for saving into the database
*
* @param CRM_Core_Form $form
* @param array $submittedValues
* @return string
*/
public function postProcess(CRM_Core_Form &$form, $submittedValues) {
$data['operator'] = $submittedValues['operator'];
$data['value'] = $submittedValues['values'];
return $this->condition->transformConditionData($data);
}
}
\ No newline at end of file
......@@ -27,11 +27,41 @@ class CRM_Civirules_Utils_Parameters {
if (isset($parameter[0]) && isset($parameter[1])) {
$field = trim($parameter[0]);
$value = trim($parameter[1]);
$converted_parameters[$field] = $value;
$converted_parameters[html_entity_decode($field)] = html_entity_decode($value);
}
}
return $converted_parameters;
}
/**
* Converts an array to a multiline string
*
* E.g
* the input:
* array(
* 'group_id' => 12,
* 'contact_id' => 1,
* );
*
* the output is:
* group_id=12
* contact_id=1
*
* @param array $parameters
* @return string
*/
public static function convertToMultiline($parameters) {
$converted_parameters = '';
foreach($parameters as $key => $value) {
if (strlen($converted_parameters)) {
$converted_parameters .= "\r\n";
} else {
$converted_parameters .= "";
}
$converted_parameters .= htmlentities($key).'='.htmlentities($value);
}
return $converted_parameters;
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment