Skip to content

Commit

Permalink
CRM-12642 add time to membership receive_date fields, move date_time …
Browse files Browse the repository at this point in the history
…handling to Form for re-usability
  • Loading branch information
eileenmcnaughton committed May 20, 2013
1 parent 64071bf commit 5d86176
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 20 deletions.
43 changes: 43 additions & 0 deletions CRM/Core/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,21 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
*/
protected $_renderer;

/**
* An array to hold a list of datefields on the form
* so that they can be converted to ISO in a consistent manner
*
* @var array
*
* e.g on a form declare $_dateFields = array(
* 'receive_date' => array('default' => 'now'),
* );
* then in postProcess call $this->convertDateFieldsToMySQL($formValues)
* to have the time field re-incorporated into the field & 'now' set if
* no value has been passed in
*/
protected $_dateFields = array();

/**
* cache the smarty template for efficiency reasons
*
Expand Down Expand Up @@ -1218,6 +1233,34 @@ function addCurrency($name = 'currency',
$this->setDefaults(array($name => $defaultCurrency));
}

/**
* Convert all date fields within the params to mysql date ready for the
* BAO layer. In this case fields are checked against the $_datefields defined for the form
* and if time is defined it is incorporated
*
* @param array $params input params from the form
*
* @todo it would probably be better to work on $this->_params than a passed array
* @todo standardise the format which dates are passed to the BAO layer in & remove date
* handling from BAO
*/
function convertDateFieldsToMySQL(&$params){
foreach ($this->_dateFields as $fieldName => $specs){
if(!empty($params[$fieldName])){
$params[$fieldName] = CRM_Utils_Date::isoToMysql(
CRM_Utils_Date::processDate(
$params[$fieldName],
CRM_Utils_Array::value("{$fieldName}_time", $params), TRUE)
);
}
else{
if(isset($specs['default'])){
$params[$fieldName] = date('YmdHis', strtotime($specs['default']));
}
}
}
}

function removeFileRequiredRules($elementName) {
$this->_required = array_diff($this->_required, array($elementName));
if (isset($this->_rules[$elementName])) {
Expand Down
28 changes: 15 additions & 13 deletions CRM/Member/Form/Membership.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ class CRM_Member_Form_Membership extends CRM_Member_Form {
*/
protected $_membershipIDs = array( );

/**
* An array to hold a list of datefields on the form
* so that they can be converted to ISO in a consistent manner
*
* @var array
*/
protected $_dateFields = array(
'receive_date' => array('default' => 'now'),
);

public function preProcess() {
//custom data related code
$this->_cdType = CRM_Utils_Array::value('type', $_GET);
Expand Down Expand Up @@ -235,7 +245,7 @@ public function preProcess() {
$resources->addScriptFile('civicrm', 'templates/CRM/Member/Form/Membership.js');
}
}
else {
else {
$resources = CRM_Core_Resources::singleton();
$resources->addScriptFile('civicrm', 'templates/CRM/Member/Form/MembershipStandalone.js');
$statuses = array();
Expand Down Expand Up @@ -719,7 +729,7 @@ public function buildQuickForm() {
$this->add('text', 'total_amount', ts('Amount'));
$this->addRule('total_amount', ts('Please enter a valid amount.'), 'money');

$this->addDate('receive_date', ts('Received'), FALSE, array('formatType' => 'activityDate'));
$this->addDate('receive_date', ts('Received'), FALSE, array('formatType' => 'activityDateTime'));

$this->add('select', 'payment_instrument_id',
ts('Paid By'),
Expand Down Expand Up @@ -1063,6 +1073,7 @@ public function postProcess() {
$config = CRM_Core_Config::singleton();
// get the submitted form values.
$this->_params = $formValues = $this->controller->exportValues($this->_name);
$this->convertDateFieldsToMySQL($formValues);

$params = $ids = array();

Expand Down Expand Up @@ -1168,12 +1179,6 @@ public function postProcess() {
$$dateVariable = CRM_Utils_Date::processDate($formValues[$dateField]);
}

$dates = array(
'join_date',
'start_date',
'end_date',
);

$num_terms = CRM_Utils_Array::value('num_terms', $formValues, 1);

$calcDates = array();
Expand All @@ -1185,7 +1190,7 @@ public function postProcess() {
}

foreach ($calcDates as $memType => $calcDate) {
foreach ($dates as $d) {
foreach (array_keys($dateTypes) as $d) {
//first give priority to form values then calDates.
$date = CRM_Utils_Array::value($d, $formValues);
if (!$date) {
Expand Down Expand Up @@ -1276,11 +1281,8 @@ public function postProcess() {
$this->assign('is_pay_later', 1);
}

if (CRM_Utils_Array::value('receive_date', $params)) {
$formValues['receive_date'] = $params['receive_date'] = CRM_Utils_Date::processDate($params['receive_date']);
}
if (CRM_Utils_Array::value('send_receipt', $formValues)) {
$params['receipt_date'] = CRM_Utils_Array::value('receive_date', $params);
$params['receipt_date'] = CRM_Utils_Array::value('receive_date', $formValues);
}

//insert financial type name in receipt.
Expand Down
19 changes: 12 additions & 7 deletions CRM/Member/Form/MembershipRenewal.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ class CRM_Member_Form_MembershipRenewal extends CRM_Member_Form {
*/
protected $_context;

/**
* An array to hold a list of datefields on the form
* so that they can be converted to ISO in a consistent manner
*
* @var array
*/
protected $_dateFields = array(
'receive_date' => array('default' => 'now'),
);

public function preProcess() {
//custom data related code
$this->_cdType = CRM_Utils_Array::value('type', $_GET);
Expand Down Expand Up @@ -436,7 +446,7 @@ public function buildQuickForm() {
$this->add('text', 'total_amount', ts('Amount'));
$this->addRule('total_amount', ts('Please enter a valid amount.'), 'money');

$this->addDate('receive_date', ts('Received'), FALSE, array('formatType' => 'activityDate'));
$this->addDate('receive_date', ts('Received'), FALSE, array('formatType' => 'activityDateTime'));

$this->add('text', 'num_terms', ts('Extend Membership by'), array('onchange' => "setPaymentBlock();"), TRUE);
$this->addRule('num_terms', ts('Please enter a whole number for how many periods to renew.'), 'integer');
Expand Down Expand Up @@ -563,12 +573,7 @@ public function postProcess() {
}

$now = CRM_Utils_Date::getToday( null, 'YmdHis');
if (CRM_Utils_Array::value('receive_date', $this->_params)) {
$formValues['receive_date'] = CRM_Utils_Date::processDate($this->_params['receive_date']);
}
else {
$formValues['receive_date'] = $now;
}
$this->convertDateFieldsToMySQL($formValues);
$this->assign('receive_date', $formValues['receive_date']);

if (CRM_Utils_Array::value('send_receipt', $this->_params)) {
Expand Down

0 comments on commit 5d86176

Please sign in to comment.