From b6d6336e2614171dc83f310758f9a87ef9ad9b33 Mon Sep 17 00:00:00 2001 From: Jerome Nadaud Date: Tue, 22 Oct 2013 12:12:41 +0200 Subject: [PATCH] // Add date subtitle --- admin-dev/themes/default/js/calendar.js | 2 + js/tools.js | 8 ++ modules/dashactivity/dashactivity.php | 12 +- modules/dashactivity/views/js/dashactivity.js | 107 +++++++++++++++++- .../templates/hook/dashboard_zone_one.tpl | 9 +- 5 files changed, 134 insertions(+), 4 deletions(-) diff --git a/admin-dev/themes/default/js/calendar.js b/admin-dev/themes/default/js/calendar.js index b011af369..643fa88e9 100644 --- a/admin-dev/themes/default/js/calendar.js +++ b/admin-dev/themes/default/js/calendar.js @@ -51,6 +51,7 @@ Date.parseDate = function(date, format) { switch(formatParts[i]) { case 'dd': case 'd': + case 'j': date.setDate(parseInt(parts[i], 10)||1); break; @@ -121,6 +122,7 @@ Date.prototype.format = function(format) { switch(formatParts[i]) { case 'dd': case 'd': + case 'j': result += this.getDate() + formatSeparator; break; diff --git a/js/tools.js b/js/tools.js index 4da024b8c..137318d05 100644 --- a/js/tools.js +++ b/js/tools.js @@ -294,6 +294,14 @@ function removeQuotes(value) return value; } +function sprintf(format) +{ + for(var i=1; i < arguments.length; i++) + format = format.replace(/%s/, arguments[i]); + + return format; +} + /** * Display a MessageBox * @param {string} msg diff --git a/modules/dashactivity/dashactivity.php b/modules/dashactivity/dashactivity.php index 7faa0e3c9..da93950ba 100644 --- a/modules/dashactivity/dashactivity.php +++ b/modules/dashactivity/dashactivity.php @@ -65,12 +65,22 @@ class Dashactivity extends Module public function hookDisplayBackOfficeHeader() { if (get_class($this->context->controller) == 'AdminDashboardController') + { + if (method_exists($this->context->controller, 'addJquery')) + $this->context->controller->addJquery(); + $this->context->controller->addJs($this->_path.'views/js/'.$this->name.'.js'); + $this->context->controller->addJs(_PS_JS_DIR_.'date.js'); + } } public function hookDashboardZoneOne($params) { - $this->context->smarty->assign(array_merge(array('dashactivity_config_form' => $this->renderConfigForm()), $this->getConfigFieldsValues())); + $this->context->smarty->assign(array_merge(array( + 'dashactivity_config_form' => $this->renderConfigForm(), + 'date_subtitle' => $this->l('From %s to %s'), + 'date_format' => $this->context->language->date_format_lite + ), $this->getConfigFieldsValues())); return $this->display(__FILE__, 'dashboard_zone_one.tpl'); } diff --git a/modules/dashactivity/views/js/dashactivity.js b/modules/dashactivity/views/js/dashactivity.js index b145c04b7..a09d42b2b 100644 --- a/modules/dashactivity/views/js/dashactivity.js +++ b/modules/dashactivity/views/js/dashactivity.js @@ -15,4 +15,109 @@ function pie_chart_trends(widget_name, chart_details) nv.utils.windowResize(chart.update); return chart; }); -} \ No newline at end of file +} + +//TODO unify this with other function in calenda.js and replace date.js functions +Date.parseDate = function(date, format) { + if (format === undefined) + format = 'Y-m-d'; + + var formatSeparator = format.match(/[.\/\-\s].*?/); + var formatParts = format.split(/\W+/); + var parts = date.split(formatSeparator); + var date = new Date(); + + if (parts.length === formatParts.length) { + date.setHours(0); + date.setMinutes(0); + date.setSeconds(0); + date.setMilliseconds(0); + + for (var i=0; i<=formatParts.length; i++) { + switch(formatParts[i]) { + case 'dd': + case 'd': + case 'j': + date.setDate(parseInt(parts[i], 10)||1); + break; + + case 'mm': + case 'm': + date.setMonth((parseInt(parts[i], 10)||1) - 1); + break; + + case 'yy': + case 'y': + date.setFullYear(2000 + (parseInt(parts[i], 10)||1)); + break; + + case 'yyyy': + case 'Y': + date.setFullYear(parseInt(parts[i], 10)||1); + break; + } + } + } + + return date; +}; + +Date.prototype.format = function(format) { + if (format === undefined) + return this.toString(); + + var formatSeparator = format.match(/[.\/\-\s].*?/); + var formatParts = format.split(/\W+/); + var result = ''; + + for (var i=0; i<=formatParts.length; i++) { + switch(formatParts[i]) { + case 'dd': + case 'd': + case 'j': + result += this.getDate() + formatSeparator; + break; + + case 'mm': + case 'm': + result += (this.getMonth() + 1) + formatSeparator; + break; + + case 'yy': + case 'y': + result += this.getFullYear() + formatSeparator; + break; + + case 'yyyy': + case 'Y': + result += this.getFullYear() + formatSeparator; + break; + } + } + + return result.slice(0, -1); +} + +$(document).ready(function() { + if (date_subtitle === undefined) + date_subtitle = 'From %s to %s'; + + if (date_format === undefined) + date_format = 'Y-m-d'; + + $('#date-start').change(function() { + start = Date.parseDate($('#date-start').val(), 'Y-m-d'); + end = Date.parseDate($('#date-end').val(), 'Y-m-d'); + + $('#customers-newsletters-subtitle').html(sprintf(date_subtitle, start.format(date_format), end.format(date_format))); + $('#traffic-subtitle').html(sprintf(date_subtitle, start.format(date_format), end.format(date_format))); + }); + + $('#date-end').change(function() { + start = Date.parseDate($('#date-start').val(), 'Y-m-d'); + end = Date.parseDate($('#date-end').val(), 'Y-m-d'); + + $('#customers-newsletters-subtitle').html(sprintf(date_subtitle, start.format(date_format), end.format(date_format))); + $('#traffic-subtitle').html(sprintf(date_subtitle, start.format(date_format), end.format(date_format))); + }); +}); \ No newline at end of file diff --git a/modules/dashactivity/views/templates/hook/dashboard_zone_one.tpl b/modules/dashactivity/views/templates/hook/dashboard_zone_one.tpl index 27b54688b..87690b422 100644 --- a/modules/dashactivity/views/templates/hook/dashboard_zone_one.tpl +++ b/modules/dashactivity/views/templates/hook/dashboard_zone_one.tpl @@ -126,7 +126,7 @@ {/if} {if $DASHACTIVITY_SHOW_CUSTOMERS}
-
{l s='Customers & Newsletters' mod='dashactivity'}
+
{l s='Customers & Newsletters' mod='dashactivity'}
+ + \ No newline at end of file