Skip to content
Snippets Groups Projects
Commit 2db55b3b authored by jaapjansma's avatar jaapjansma
Browse files

Fixed #87 condition for active membership

parent 47a290cb
No related branches found
No related tags found
No related merge requests found
<?php
/**
* Class for CiviRules Condition Membership Type Form
*
* @author Erik Hommel (CiviCooP) <erik.hommel@civicoop.org>
* @license AGPL-3.0
*/
class CRM_CivirulesConditions_Form_Membership_ActiveMembership extends CRM_CivirulesConditions_Form_Form {
/**
* Overridden parent method to build form
*
* @access public
*/
public function buildQuickForm() {
$this->add('hidden', 'rule_condition_id');
$membershipTypes = CRM_Civirules_Utils::getMembershipTypes();
$membershipTypes[0] = ts('- select -');
asort($membershipTypes);
$this->add('select', 'membership_type_id', ts('Membership Type'), $membershipTypes, 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['membership_type_id'])) {
$defaultValues['membership_type_id'] = $data['membership_type_id'];
}
return $defaultValues;
}
/**
* Overridden parent method to process form data after submission
*
* @throws Exception when rule condition not found
* @access public
*/
public function postProcess() {
$data['membership_type_id'] = $this->_submitValues['membership_type_id'];
$this->ruleCondition->condition_params = serialize($data);
$this->ruleCondition->save();
parent::postProcess();
}
}
\ No newline at end of file
<?php
return array (
0 =>
array (
'name' => 'Civirules:Condition.MembershipActiveMembership',
'entity' => 'CiviRuleCondition',
'params' =>
array (
'version' => 3,
'name' => 'activemembership_type',
'label' => 'Contact has active membership',
'class_name' => 'CRM_CivirulesConditions_Membership_ActiveMembership',
'is_active' => 1
),
),
);
\ No newline at end of file
<?php
class CRM_CivirulesConditions_Membership_ActiveMembership extends CRM_Civirules_Condition {
/**
* 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) {
$params['membership_type_id'] = $this->conditionParams['membership_type_id'];
$params['contact_id'] = $triggerData->getContactId();
$params['active_only'] = 1;
$memberships = civicrm_api3('Membership', 'get', $params);
if (isset($memberships['values']) && count($memberships['values']) > 0) {
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/civirule/form/condition/activemembershiptype', '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() {
try {
$membershipTypes = civicrm_api3('MembershipType', 'Get', array('is_active' => 1));
$operator = 'equals';
foreach ($membershipTypes['values'] as $membershipType) {
if ($membershipType['id'] == $this->conditionParams['membership_type_id']) {
return "Membership Type ".$operator." ".$membershipType['name'];
}
}
} catch (CiviCRM_API3_Exception $ex) {}
return '';
}
}
\ No newline at end of file
<h3>{$ruleConditionHeader}</h3>
<div class="crm-block crm-form-block crm-civirule-rule_condition-block-membership_activemembership">
<div class="crm-section">
<div class="label">{$form.membership_type_id.label}</div>
<div class="content">{$form.membership_type_id.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
......@@ -77,6 +77,13 @@
<access_arguments>access CiviCRM</access_arguments>
<access_arguments>administer CiviCRM</access_arguments>
</item>
<item>
<path>civicrm/civirule/form/condition/activemembershiptype</path>
<page_callback>CRM_CivirulesConditions_Form_Membership_ActiveMembership</page_callback>
<title>membership type</title>
<access_arguments>access CiviCRM</access_arguments>
<access_arguments>administer CiviCRM</access_arguments>
</item>
<item>
<path>civicrm/civirule/form/condition/membershipstatus</path>
<page_callback>CRM_CivirulesConditions_Form_Membership_Status</page_callback>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment