Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Towards supporting Smarty5 (replace addDate by datepicker) #736

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions CRM/Sepa/Form/CreateMandate.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,12 @@ public function buildQuickForm() {
$this->add('hidden', 'replace', $this->replace_id);

// add the replace date
$this->addDate(
$this->add('datepicker',
'rpl_end_date',
E::ts("Replacement Date"),
['formatType' => 'activityDate'],
$this->replace_id,
array('formatType' => 'activityDate'));
['time' => FALSE]);

// add the replacement/cancel reason
$this->add(
Expand All @@ -227,19 +228,21 @@ public function buildQuickForm() {

// add OOFF fields
// add collection date
$this->addDate(
$this->add('datepicker',
'ooff_date',
E::ts("Collection Date"),
['formatType' => 'activityDate'],
FALSE,
array('formatType' => 'activityDate'));
['time' => FALSE]);

// add RCUR fields
// add start date
$this->addDate(
$this->add('datepicker',
'rcur_start_date',
E::ts("Start Date"),
['formatType' => 'activityDate'],
FALSE,
array('formatType' => 'activityDate'));
['time' => FALSE]);

// add collection day
$this->add(
Expand All @@ -262,15 +265,15 @@ public function buildQuickForm() {
);

// add end_date
$this->addDate(
$this->add('datepicker',
'rcur_end_date',
E::ts("End Date"),
['formatType' => 'activityDate'],
FALSE,
array('formatType' => 'activityDate'));
['time' => FALSE]);

// finally, add a date field just as a converter
$this->addDate('sdd_converter', 'just for date conversion', FALSE, array('formatType' => 'activityDate'));

$this->add('datepicker', 'sdd_converter', 'just for date conversion', ['formatType' => 'activityDate'], FALSE, ['time' => FALSE]);

// inject JS logic
CRM_Core_Resources::singleton()->addScriptFile('org.project60.sepa', 'js/CreateMandate.js');
Expand Down
38 changes: 19 additions & 19 deletions js/CreateMandate.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
| written permission from the original author(s). |
+--------------------------------------------------------*/

cj(document).ready(function() {
(function($, _, ts) {
$(document).ready(function($) {
/**
* Identify and get the jQuery field for the given value
*/
Expand All @@ -24,29 +25,25 @@ cj(document).ready(function() {
* Utility function to set the date on the %^$W#$&$&% datepicker elements
* PRs welcome :)
**/
function sdd_setDate(fieldname, date) {
function sdd_setDate(fieldname, date, recalculate = true) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This recalculate param can be ignored, does not work. It was to avoid recursive issues, when sdd_setDate is called from sdd_recalculate_fields. It is not enough, because there is also an event listener for change on most of the date fields.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot @mlutfy, we'll try to pick this up after the holidays.

Happy holidays and new year!

let dp_element = cj("#sdd-create-mandate").find("[name^=" + fieldname + "].hasDatepicker");
dp_element.datepicker('setDate', date);

// flash the field a little bit to indicate change
sdd_getF(fieldname).parent().fadeOut(50).fadeIn(50);
sdd_recalculate_fields();
if (recalculate) {
sdd_recalculate_fields();
}
}

/**
* convert a Date object into a formatted string
* using the sdd_converter element
*/
function sdd_formatDate(date) {
cj("#sdd-create-mandate")
.find("[name^=sdd_converter].hasDatepicker")
.datepicker('setDate', date);

return cj("#sdd-create-mandate")
.find("[name^=sdd_converter_display]").val();
return CRM.utils.formatDate(date);
}


// logic to hide OOFF/RCUR fields
function sdd_change_type() {
let interval = sdd_getF('interval').val();
Expand Down Expand Up @@ -134,11 +131,11 @@ cj(document).ready(function() {
// parse date and overwrite only if too early
let ooff_current = Date.parse(ooff_current_value);
if (ooff_earliest > ooff_current) {
sdd_setDate('ooff_date', ooff_earliest);
sdd_setDate('ooff_date', ooff_earliest, false);
}
} else {
// no date set yet?
sdd_setDate('ooff_date', ooff_earliest);
sdd_setDate('ooff_date', ooff_earliest, false);
}
cj("#sdd_ooff_earliest")
.attr('date', ooff_earliest)
Expand All @@ -156,15 +153,16 @@ cj(document).ready(function() {
// parse date and overwrite only if too early
let rcur_current = new Date(rcur_current_value);
if (rcur_earliest > rcur_current) {
sdd_setDate('rcur_start_date', rcur_earliest);
sdd_setDate('rcur_start_date', rcur_earliest, false);
}
} else {
// no date set yet?
sdd_setDate('rcur_start_date', rcur_earliest);
sdd_setDate('rcur_start_date', rcur_earliest, false);
}

cj("#sdd_rcur_earliest")
.attr('date', rcur_earliest)
.text(ts("earliest: %1", {1: sdd_formatDate(rcur_earliest), domain: 'org.project60.sepa'}));
.data('date', rcur_earliest)
.text(ts("earliest: %1", {1: CRM.utils.formatDate(rcur_earliest)}));

// CALCULATE SUMMARY TEXT
let text = ts("<i>Not enough information</i>", {'domain':'org.project60.sepa'});
Expand Down Expand Up @@ -285,10 +283,11 @@ cj(document).ready(function() {

// attach earliest link handlers
cj("#sdd-create-mandate").find("a.sdd-earliest").click(function() {
console.log('DATE', cj(this).data('date'));
if (cj(this).attr('id') == 'sdd_rcur_earliest') {
sdd_setDate('rcur_start_date', new Date(cj(this).attr('date')));
sdd_setDate('rcur_start_date', $(this).data('date'));
} else {
sdd_setDate('ooff_date', new Date(cj(this).attr('date')));
sdd_setDate('ooff_date', $(this).data('date'));
}
});

Expand All @@ -299,4 +298,5 @@ cj(document).ready(function() {

// trigger the whole thing once
sdd_creditor_changed();
});
});
})(CRM.$, CRM._, CRM.ts('org.project60.sepa'));
14 changes: 7 additions & 7 deletions templates/CRM/Sepa/Form/CreateMandate.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

<div class="crm-section">
<div class="label">{$form.rpl_end_date.label}</div>
<div class="content">{include file="CRM/common/jcalendar.tpl" elementName='rpl_end_date'}</div>
<div class="content">{$form.rpl_end_date.html}</div>
<div class="clear"></div>
</div>

Expand Down Expand Up @@ -117,7 +117,7 @@
<div class="crm-section">
<div class="label">{$form.ooff_date.label}</div>
<div class="content">
{include file="CRM/common/jcalendar.tpl" elementName='ooff_date'}
{$form.ooff_date.html}
<a id="sdd_ooff_earliest" class="sdd-earliest"></a>
</div>
<div class="clear"></div>
Expand All @@ -129,7 +129,7 @@
<div class="crm-section">
<div class="label">{$form.rcur_start_date.label}</div>
<div class="content">
{include file="CRM/common/jcalendar.tpl" elementName='rcur_start_date'}
{$form.rcur_start_date.html}
<a id="sdd_rcur_earliest" class="sdd-earliest"></a>
</div>
<div class="clear"></div>
Expand All @@ -143,7 +143,7 @@

<div class="crm-section">
<div class="label">{$form.rcur_end_date.label}</div>
<div class="content">{include file="CRM/common/jcalendar.tpl" elementName='rcur_end_date'}</div>
<div class="content">{$form.rcur_end_date.html}</div>
<div class="clear"></div>
</div>
</div>
Expand All @@ -162,9 +162,9 @@
<div class="clear"></div>
</div>

<div class="crm-section" style="display: none;">
<div class="content">{include file="CRM/common/jcalendar.tpl" elementName='sdd_converter'}</div>
</div>
<div class="crm-section" style="display: none;">
<div class="content">{$form.sdd_converter.html}</div>
</div>
</div>


Expand Down