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

fixed issues #26: added exta period options and moved period handling to util class

parent cc5fbdb7
Branches
Tags
No related merge requests found
......@@ -49,8 +49,8 @@ FROM civicrm_contribution WHERE contact_id = %1 AND civicrm_contribution.contrib
1 => array($contact_id, 'Positive'),
2 => array(CRM_Civirules_Utils::getContributionStatusIdWithName('Completed'), 'Positive'));
$periodStartDate = CRM_CivirulesConditions_Utils_Period::convertPeriodToStartDate($this->conditionParams['period']);
$periodEndDate = CRM_CivirulesConditions_Utils_Period::convertPeriodToEndDate($this->conditionParams['period']);
$periodStartDate = CRM_CivirulesConditions_Utils_Period::convertPeriodToStartDate($this->conditionParams);
$periodEndDate = CRM_CivirulesConditions_Utils_Period::convertPeriodToEndDate($this->conditionParams);
if ($periodStartDate) {
$query .= " AND DATE(`receive_date`) >= '".$periodStartDate->format('Y-m-d')."'";
}
......@@ -141,12 +141,7 @@ FROM civicrm_contribution WHERE contact_id = %1 AND civicrm_contribution.contrib
break;
}
$periods = CRM_CivirulesConditions_Utils_Period::Options();
if (isset($periods[$this->conditionParams['period']])) {
$period = ts('in the ').$periods[$this->conditionParams['period']];
} else {
$period = ts('all time');
}
$period = CRM_CivirulesConditions_Utils_Period::userFriendlyConditionParams($this->conditionParams);
return 'Distinct number of contributing days '.$operator.' '.$this->conditionParams['no_of_days'].' '.$period;
}
......
......@@ -20,8 +20,8 @@ class CRM_CivirulesConditions_Contribution_TotalContributedAmount extends CRM_Ci
$params[1] = array($completed_status_id, 'Integer');
$params[2] = array($contact_id, 'Integer');
$periodStartDate = CRM_CivirulesConditions_Utils_Period::convertPeriodToStartDate($this->conditionParams['period']);
$periodEndDate = CRM_CivirulesConditions_Utils_Period::convertPeriodToEndDate($this->conditionParams['period']);
$periodStartDate = CRM_CivirulesConditions_Utils_Period::convertPeriodToStartDate($this->conditionParams);
$periodEndDate = CRM_CivirulesConditions_Utils_Period::convertPeriodToEndDate($this->conditionParams);
if ($periodStartDate) {
$sql .= " AND DATE(`receive_date`) >= '".$periodStartDate->format('Y-m-d')."'";
}
......@@ -56,13 +56,7 @@ class CRM_CivirulesConditions_Contribution_TotalContributedAmount extends CRM_Ci
*/
public function userFriendlyConditionParams() {
$userFriendlyConditionParams = parent::userFriendlyConditionParams();
$periods = CRM_CivirulesConditions_Utils_Period::Options();
if (isset($periods[$this->conditionParams['period']])) {
$period = $periods[$this->conditionParams['period']];
} else {
$period = ts('all time');
}
$period = CRM_CivirulesConditions_Utils_Period::userFriendlyConditionParams($this->conditionParams);
return ts('Total amount').' '.$period.' '.$userFriendlyConditionParams;
}
......
......@@ -27,7 +27,7 @@ class CRM_CivirulesConditions_Form_Contribution_DistinctContributingDay extends
$this->addRule('no_of_days','Number of Days must be a whole number','numeric');
$this->addRule('no_of_days','Number of Days must be a whole number','nopunctuation');
$this->add('select', 'period', ts('Period'), array('' => ts('All time')) + CRM_CivirulesConditions_Utils_Period::Options());
CRM_CivirulesConditions_Utils_Period::buildQuickForm($this);
$this->addButtons(array(
array('type' => 'next', 'name' => ts('Save'), 'isDefault' => TRUE,),
......@@ -49,13 +49,17 @@ class CRM_CivirulesConditions_Form_Contribution_DistinctContributingDay extends
if (!empty($data['no_of_days'])) {
$defaultValues['no_of_days'] = $data['no_of_days'];
}
if (!empty($data['period'])) {
$defaultValues['period'] = $data['period'];
}
$defaultValues = CRM_CivirulesConditions_Utils_Period::setDefaultValues($defaultValues, $data);
return $defaultValues;
}
public function addRules()
{
CRM_CivirulesConditions_Utils_Period::addRules($this);
}
/**
* Overridden parent method to process form data after submission
*
......@@ -65,7 +69,9 @@ class CRM_CivirulesConditions_Form_Contribution_DistinctContributingDay extends
public function postProcess() {
$data['operator'] = $this->_submitValues['operator'];
$data['no_of_days'] = $this->_submitValues['no_of_days'];
$data['period'] = $this->_submitValues['period'];
$data = CRM_CivirulesConditions_Utils_Period::getConditionParams($this->_submitValues, $data);
$this->ruleCondition->condition_params = serialize($data);
$this->ruleCondition->save();
......
......@@ -10,7 +10,7 @@ class CRM_CivirulesConditions_Form_Contribution_TotalContributedAmount extends C
public function buildQuickForm() {
parent::buildQuickForm();
$this->add('select', 'period', ts('Period'), array('' => ts('All time')) + CRM_CivirulesConditions_Utils_Period::Options());
CRM_CivirulesConditions_Utils_Period::buildQuickForm($this);
}
/**
......@@ -21,13 +21,18 @@ class CRM_CivirulesConditions_Form_Contribution_TotalContributedAmount extends C
*/
public function setDefaultValues() {
$defaultValues = parent::setDefaultValues();
$data = unserialize($this->ruleCondition->condition_params);
if (!empty($data['period'])) {
$defaultValues['period'] = $data['period'];
}
$defaultValues = CRM_CivirulesConditions_Utils_Period::setDefaultValues($defaultValues, $data);
return $defaultValues;
}
public function addRules()
{
CRM_CivirulesConditions_Utils_Period::addRules($this);
}
/**
* Overridden parent method to process form data after submission
*
......@@ -37,7 +42,8 @@ class CRM_CivirulesConditions_Form_Contribution_TotalContributedAmount extends C
public function postProcess()
{
$data = unserialize($this->ruleCondition->condition_params);
$data['period'] = $this->_submitValues['period'];
$data = CRM_CivirulesConditions_Utils_Period::getConditionParams($this->_submitValues, $data);
$this->ruleCondition->condition_params = serialize($data);
$this->ruleCondition->save();
......
......@@ -8,19 +8,251 @@
*/
class CRM_CivirulesConditions_Utils_Period {
/**
* Returns the possible options
*
* @return array
*/
public static function Options() {
$definitions = self::PeriodDefinitions();
$options = array();
foreach($definitions as $key => $definition) {
$options[$key] = $definition['label'];
}
return $options;
}
/**
* Returns all possible period options and their definition
*
* A definition consist of a label of the period and eventually extra replacements
* e.g. In the period Last nnn days, the 'nnn' should be given by the user and should by
* taken into account by calculating the start and end date
*
* @return array
*/
private static function PeriodDefinitions() {
return array(
'this month' => ts('This month'),
'previous month' => ts('Previous month'),
'last 30 days' => ts('Last 30 days'),
'last 12 months' => ts('Last 12 months'),
'last 13 months' => ts('Last 13 months'),
'this year' => ts('This year'),
'previous year' => ts('Previous year'),
'this month' => array(
'label' => ts('This month'),
'replacements' => array()
),
'previous month' => array(
'label' => ts('Previous month'),
'replacements' => array()
),
'last 30 days' => array(
'label' => ts('Last 30 days'),
'replacements' => array()
),
'last 12 months' => array(
'label' => ts('Last 12 months'),
'replacements' => array()
),
'last 13 months' => array(
'label' => ts('Last 13 months'),
'replacements' => array()
),
'this year' => array(
'label' => ts('This year'),
'replacements' => array()
),
'previous year' => array(
'label' => ts('Previous year'),
'replacements' => array()
),
'last nnn days' => array(
'label' => ts('Last nnn days'),
'replacements' => array(
'nnn' => ts('days'),
)
),
'last nnn weeks' => array(
'label' => ts('Last nnn weeks'),
'replacements' => array(
'nnn' => ts('weeks'),
)
),
'last nnn months' => array(
'label' => ts('Last nnn months'),
'replacements' => array(
'nnn' => ts('months'),
)
),
'last nnn years' => array(
'label' => ts('Last nnn years'),
'replacements' => array(
'nnn' => ts('years'),
)
),
);
}
public static function convertPeriodToStartDate($period) {
/**
* Returns an array with all possible replacements
*
* The array looks like
* xxx => array(
* 'last xxx years',
* 'last xxx months',
* ...
* )
*
* @return array
*/
private static function getReplacementOptions() {
$definitions = self::PeriodDefinitions();
$replacements = array();
foreach($definitions as $key => $definition) {
foreach($definition['replacements'] as $replacement => $suffix) {
if (!isset($replacements[$replacement])) {
$replacements[$replacement] = array();
}
$replacements[$replacement][] = $key;
}
}
return $replacements;
}
/**
* Returns an array with all possible replacements
*
* The array looks like
* last xxx years => array(
* 'name' => xxx,
* 'suffix' => Days
* ...
* )
*
* @return array
*/
private static function getReplacementOptionsByPeriod() {
$definitions = self::PeriodDefinitions();
$replacements = array();
foreach($definitions as $key => $definition) {
foreach($definition['replacements'] as $replacement => $suffix) {
$replacements[$key][] = array(
'name' => $replacement,
'suffix' => $suffix,
);
}
}
return $replacements;
}
/**
* Add fields to the form for period selection
*
* @param $form
*/
public static function buildQuickForm(&$form) {
$form->add('select', 'period', ts('Period'), array('' => ts('All time')) + CRM_CivirulesConditions_Utils_Period::Options());
$replacements = self::getReplacementOptions();
foreach($replacements as $replacement_key => $replacement_periods) {
$form->add('text', $replacement_key, ts($replacement_key));
}
$form->assign('period_replacements', $replacements);
$form->assign('period_replacements_by_period', json_encode(self::getReplacementOptionsByPeriod()));
}
public static function addRules(&$form) {
$form->addFormRule(array('CRM_CivirulesConditions_Utils_Period', 'validatePeriod'));
}
public static function validatePeriod($fields) {
$definitions = self::PeriodDefinitions();
$period = $fields['period'];
if (!empty($period) && isset($definitions[$period])) {
$errors = array();
foreach($definitions[$period]['replacements'] as $key => $label) {
if (empty($fields[$key]) || !is_numeric($fields[$key])) {
$errors[$key] = ts('You should enter a valid amount');
}
}
if (count($errors)) {
return $errors;
}
}
return true;
}
/**
* Set default values for a form based on the condition params
*
* @param array $defaultValues
* @param array $condition_params
* @return array
*/
public static function setDefaultValues($defaultValues, $condition_params) {
if (!empty($condition_params['period'])) {
$defaultValues['period'] = $condition_params['period'];
}
if (isset($condition_params['replaceParameters']) && is_array($condition_params['replaceParameters'])) {
foreach($condition_params['replaceParameters'] as $key => $val) {
$defaultValues[$key] = $val;
}
}
return $defaultValues;
}
/**
* Parse submitted values from a form to the condition params
*
* @param array $submitValues
* @param array $condition_params
* @return array
*/
public static function getConditionParams($submitValues, $condition_params) {
$periods = self::PeriodDefinitions();
$period = $condition_params['period'] = $submitValues['period'];
foreach($periods[$period]['replacements'] as $key => $label) {
$condition_params['replaceParameters'][$key] = $submitValues[$key];
}
return $condition_params;
}
private static function replaceParameters($period, $key, $replacement_params) {
if (stripos($period, $key) === false) {
throw new Exception($key .' is not set for period '.$period);
}
if (empty($replacement_params[$key])) {
throw new Exception($key.' is not given for period '.$period);
}
return $replacement_params[$key];
}
public static function userFriendlyConditionParams($condition_params) {
$periods = self::PeriodDefinitions();
$p = $condition_params['period'];
if (isset($periods[$p])) {
$period = $periods[$p]['label'];
foreach($periods[$p]['replacements'] as $key => $label) {
try {
$val = self::replaceParameters($period, $key, $condition_params['replaceParameters']);
$period = str_replace($key, $val, $period);
} catch (Exception $e) {
//do nothing
}
}
} else {
$period = ts('all time');
}
return $period;
}
/**
* Returns the start date of the selected period
*
* @param $period
* @param array $replaceParameters
* @return bool|\DateTime
*/
public static function convertPeriodToStartDate($condition_params) {
$period = $condition_params['period'];
$replaceParameters = $condition_params['replaceParameters'];
$date = new DateTime();
switch ($period) {
case 'this month':
......@@ -35,6 +267,11 @@ class CRM_CivirulesConditions_Utils_Period {
$date->modify('-30 days');
return $date;
break;
case 'last nnn days':
$xxx = self::replaceParameters($period, 'nnn', $replaceParameters);
$date->modify('-'.$xxx.' days');
return $date;
break;
case 'last 12 months':
$date->modify('-12 months');
return $date;
......@@ -43,6 +280,21 @@ class CRM_CivirulesConditions_Utils_Period {
$date->modify('-13 months');
return $date;
break;
case 'last nnn months':
$xxx = self::replaceParameters($period, 'nnn', $replaceParameters);
$date->modify('-'.$xxx.' months');
return $date;
break;
case 'last nnn weeks':
$xxx = self::replaceParameters($period, 'nnn', $replaceParameters);
$date->modify('-'.$xxx.' weeks');
return $date;
break;
case 'last nnn years':
$xxx = self::replaceParameters($period, 'nnn', $replaceParameters);
$date->modify('-'.$xxx.' years');
return $date;
break;
case 'this year':
$date->modify('first day of January this year');
return $date;
......@@ -56,7 +308,16 @@ class CRM_CivirulesConditions_Utils_Period {
return false;
}
public static function convertPeriodToEndDate($period) {
/**
* Returns the end date of the selected period
*
* @param $period
* @param array $replaceParameters
* @return bool|\DateTime
*/
public static function convertPeriodToEndDate($condition_params) {
$period = $condition_params['period'];
$replaceParameters = $condition_params['replaceParameters'];
$date = new DateTime();
switch ($period) {
case 'this month':
......@@ -70,12 +331,24 @@ class CRM_CivirulesConditions_Utils_Period {
case 'last 30 days':
return $date;
break;
case 'last nnn days':
return $date;
break;
case 'last 12 months':
return $date;
break;
case 'last 13 months':
return $date;
break;
case 'last nnn months':
return $date;
break;
case 'last nnn weeks':
return $date;
break;
case 'last nnn years':
return $date;
break;
case 'this year':
$date->modify('last day of December this year');
return $date;
......
<h3>{$ruleConditionHeader}</h3>
<div class="crm-block crm-form-block crm-civirule-rule_condition-block-contribution_distinctcontributingday">
<div class="crm-section">
<div class="label">{$form.period.label}</div>
<div class="content">{$form.period.html}</div>
<div class="clear"></div>
</div>
{include file="CRM/CivirulesConditions/Form/Utils/Period.tpl"}
<div class="crm-section">
<div class="label">{$form.operator.label}</div>
<div class="content">{$form.operator.html}</div>
......
<h3>{$ruleConditionHeader}</h3>
<div class="crm-block crm-form-block crm-civirule-rule_condition-block-value-comparison">
<div class="crm-section">
<div class="label">{$form.period.label}</div>
<div class="content">{$form.period.html}</div>
<div class="clear"></div>
</div>
{include file="CRM/CivirulesConditions/Form/Utils/Period.tpl"}
<div class="crm-section">
<div class="label">{$form.operator.label}</div>
<div class="content">{$form.operator.html}</div>
......
<div class="crm-section">
<div class="label">{$form.period.label}</div>
<div class="content">{$form.period.html}
<div class="period_replacements">
{foreach from=$period_replacements item=replacement key=replacement_key}
<div class="replacement hiddenElement" id="period_replacement_{$replacement_key}">
{$form.$replacement_key.html}
<span class="suffix"></span>
</div>
{/foreach}
</div>
</div>
<div class="clear"></div>
</div>
<script type="text/javascript">
var period_replacements = {$period_replacements_by_period};
{literal}
cj(function() {
cj('select#period').change(triggerPeriodChange);
triggerPeriodChange();
});
function triggerPeriodChange() {
cj('.period_replacements .replacement').addClass('hiddenElement');
var val = cj('#period').val();
if (val) {
cj(period_replacements[val]).each(function(index, element) {
cj('#period_replacement_'+element.name).removeClass('hiddenElement');
cj('#period_replacement_'+element.name+' .suffix').html(element.suffix);
});
}
}
{/literal}
</script>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment