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

contact has phone condition

parent 283ca507
Branches
Tags
No related merge requests found
<?php
return array (
0 =>
array (
'name' => 'Civirules:Condition.Contact.HasPhone',
'entity' => 'CiviRuleCondition',
'params' =>
array (
'version' => 3,
'name' => 'contact_has_phone',
'label' => 'Contact has phone',
'class_name' => 'CRM_CivirulesConditions_Contact_HasPhone',
'is_active' => 1
),
),
);
\ No newline at end of file
<?php
class CRM_CivirulesConditions_Contact_HasPhone 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) {
$phoneType = $this->conditionParams['phone_type'];
$apiParams['contact_id'] = $triggerData->getContactId();
if ($phoneType) {
$apiParams['phone_type_id'] = $phoneType;
}
$count = civicrm_api3('Phone', 'getCount', $apiParams);
if ($count) {
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/contact_hasphone/', '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() {
$phoneType = $this->conditionParams['phone_type'];
$phoneTypeLabel = ts('Any phone type');
if ($phoneType) {
$phoneTypeLabel = ts('Phone type is %1', array(
1 => CRM_Core_OptionGroup::getValue('phone_type', $phoneType, 'value', 'String', 'label')
));
}
$locationType = $this->conditionParams['location_type'];
$locationTypeLabel = ts('Any location');
if ($locationType) {
try {
$locationTypeLabel = ts('Location is %1', array(
1 => civicrm_api3('LocationType', 'getvalue', array('id' => $locationType, 'return' => 'display_name'))
));
} catch (Exception $e) {
//do nothing
}
}
return $phoneTypeLabel . ts(' and ').$locationTypeLabel;
}
}
\ 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_HasPhone extends CRM_CivirulesConditions_Form_Form {
protected function getPhoneTypes() {
return array_merge(
array(0 => ts(' - Any phone type -')),
CRM_Core_OptionGroup::values('phone_type', false, false, false, false, 'label', false)
);
}
protected function getLocationTypes() {
$locTypes = civicrm_api3('LocationType', 'get', array());
$return[0] = ts('- Any location -');
foreach($locTypes['values'] as $loc_type) {
$return[$loc_type['id']] = $loc_type['display_name'];
}
return $return;
}
/**
* Overridden parent method to build form
*
* @access public
*/
public function buildQuickForm() {
$this->add('hidden', 'rule_condition_id');
$this->add('select', 'location_type', ts('Location type'), $this->getLocationTypes(), true);
$this->add('select', 'phone_type', ts('Phone type'), $this->getPhoneTypes(), 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['phone_type'])) {
$defaultValues['phone_type'] = $data['phone_type'];
}
if (!empty($data['location_type'])) {
$defaultValues['location_type'] = $data['location_type'];
}
return $defaultValues;
}
/**
* Overridden parent method to process form data after submission
*
* @throws Exception when rule condition not found
* @access public
*/
public function postProcess() {
$data['phone_type'] = $this->_submitValues['phone_type'];
$data['location_type'] = $this->_submitValues['location_type'];
$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_phone">
<div class="crm-section">
<div class="label">{$form.location_type.label}</div>
<div class="content">{$form.location_type.html}</div>
<div class="clear"></div>
</div>
<div class="crm-section">
<div class="label">{$form.phone_type.label}</div>
<div class="content">{$form.phone_type.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
......@@ -133,6 +133,13 @@
<access_arguments>access CiviCRM</access_arguments>
<access_arguments>administer CiviCRM</access_arguments>
</item>
<item>
<path>civicrm/civirule/form/condition/contact_hasphone</path>
<page_callback>CRM_CivirulesConditions_Form_Contact_HasPhone</page_callback>
<title>Has Phone</title>
<access_arguments>access CiviCRM</access_arguments>
<access_arguments>administer 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