Skip to content
Snippets Groups Projects
Commit 0a333eac authored by jaapjansma's avatar jaapjansma
Browse files

has tag condition

parent 7a56616f
Branches
Tags
No related merge requests found
<?php
return array (
0 =>
array (
'name' => 'Civirules:Condition.Contact.HasTag',
'entity' => 'CiviRuleCondition',
'params' =>
array (
'version' => 3,
'name' => 'contact_has_tag',
'label' => 'Contact has (not) tag',
'class_name' => 'CRM_CivirulesConditions_Contact_HasTag',
'is_active' => 1
),
),
);
\ No newline at end of file
<?php
/**
* Class for CiviRules AgeComparison (extending generic ValueComparison)
*
* @author Jaap Jansma (CiviCooP) <jaap.jansma@civicoop.org>
* @license AGPL-3.0
*/
class CRM_CivirulesConditions_Contact_HasTag 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_EventData_EventData $eventData
* @return bool
* @access public
* @abstract
*/
public function isConditionValid(CRM_Civirules_EventData_EventData $eventData) {
$isConditionValid = false;
$contact_id = $eventData->getContactId();
switch($this->conditionParams['operator']) {
case 'in one of':
$isConditionValid = $this->contactHasOneOfTags($contact_id, $this->conditionParams['tag_ids']);
break;
case 'in all of':
$isConditionValid = $this->contactHasAllTags($contact_id, $this->conditionParams['tag_ids']);
break;
case 'not in':
$isConditionValid = $this->contactHasNotTag($contact_id, $this->conditionParams['tag_ids']);
break;
}
return $isConditionValid;
}
protected function contactHasNotTag($contact_id, $tag_ids) {
$isValid = true;
$tags = CRM_Core_BAO_EntityTag::getTag($contact_id);
foreach($tag_ids as $tag_id) {
if (in_array($tag_id, $tags)) {
$isValid = false;
}
}
return $isValid;
}
protected function contactHasAllTags($contact_id, $tag_ids) {
$isValid = 0;
$tags = CRM_Core_BAO_EntityTag::getTag($contact_id);
foreach($tag_ids as $tag_id) {
if (in_array($tag_id, $tags)) {
$isValid++;
}
}
if (count($tag_ids) == $isValid && count($tag_ids) > 0) {
return true;
}
return false;
}
protected function contactHasOneOfTags($contact_id, $tag_ids) {
$isValid = false;
$tags = CRM_Core_BAO_EntityTag::getTag($contact_id);
foreach($tag_ids as $tag_id) {
if (in_array($tag_id, $tags)) {
$isValid = true;
break;
}
}
return $isValid;
}
/**
* 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/civirule/form/condition/contact_hastag/', '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() {
$operators = CRM_CivirulesConditions_Contact_InGroup::getOperatorOptions();
$operator = $this->conditionParams['operator'];
$operatorLabel = ts('unknown');
if (isset($operators[$operator])) {
$operatorLabel = $operators[$operator];
}
$tags = '';
foreach($this->conditionParams['tag_ids'] as $tid) {
if (strlen($tags)) {
$tags .= ', ';
}
$tags .= civicrm_api3('Tag', 'getvalue', array('return' => 'name', 'id' => $tid));
}
return $operatorLabel.' tags ('.$tags.')';
}
/**
* Method to get operators
*
* @return array
* @access protected
*/
public static function getOperatorOptions() {
return array(
'in one of' => ts('In one of selected'),
'in all of' => ts('In all selected'),
'not in' => ts('Not in selected'),
);
}
}
\ No newline at end of file
<?php
/**
* Class for CiviRules Condition Contribution has contact a tag
*
* @author Jaap Jansma (CiviCooP) <jaap.jansma@civicoop.org>
* @license AGPL-3.0
*/
class CRM_CivirulesConditions_Form_Contact_HasTag extends CRM_CivirulesConditions_Form_Form {
/**
* Method to get groups
*
* @return array
* @access protected
*/
protected function getTags() {
return CRM_Core_BAO_Tag::getTags();
}
/**
* Method to get operators
*
* @return array
* @access protected
*/
protected function getOperators() {
return CRM_CivirulesConditions_Contact_HasTag::getOperatorOptions();
}
/**
* Overridden parent method to build form
*
* @access public
*/
public function buildQuickForm() {
$this->add('hidden', 'rule_condition_id');
$tag = $this->add('select', 'tag_ids', ts('Tags'), $this->getTags(), true);
$tag->setMultiple(TRUE);
$this->add('select', 'operator', ts('Operator'), $this->getOperators(), true);
$this->addButtons(array(
array('type' => 'next', 'name' => ts('Save'), 'isDefault' => TRUE,),
array('type' => 'cancel', 'name' => ts('Cancel'))));
}
/**
* 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['tag_ids'])) {
$defaultValues['tag_ids'] = $data['tag_ids'];
}
if (!empty($data['operator'])) {
$defaultValues['operator'] = $data['operator'];
}
return $defaultValues;
}
/**
* Overridden parent method to process form data after submission
*
* @throws Exception when rule condition not found
* @access public
*/
public function postProcess() {
$data['tag_ids'] = $this->_submitValues['tag_ids'];
$data['operator'] = $this->_submitValues['operator'];
$this->ruleCondition->condition_params = serialize($data);
$this->ruleCondition->save();
parent::postProcess();
}
}
\ No newline at end of file
<h3>{$ruleConditionHeader}</h3>
<div class="crm-block crm-form-block crm-civirule-rule_condition-block-contact_has_tag">
<div class="crm-section">
<div class="label">{$form.operator.html}</div>
<div class="content">{$form.tag_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
......@@ -60,6 +60,12 @@
<title>In Group</title>
<access_arguments>access CiviCRM</access_arguments>
</item>
<item>
<path>civicrm/civirule/form/condition/contact_hastag</path>
<page_callback>CRM_CivirulesConditions_Form_Contact_HasTag</page_callback>
<title>Has Tag</title>
<access_arguments>access CiviCRM</access_arguments>
</item>
<item>
<path>civicrm/civirule/form/condition/contribution_paidby</path>
<page_callback>CRM_CivirulesConditions_Form_Contribution_PaidBy</page_callback>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment