Welcome, Guest
Username: Password: Remember me

TOPIC: [CLEANED] Improvement to dom.html.grid.datetime

[CLEANED] Improvement to dom.html.grid.datetime 24 Nov 2012 11:11 #5631

  • blue-canoe
  • blue-canoe's Avatar
  • Offline
  • Senior Member
  • Posts: 57
  • Thank you received: 16
  • Karma: 7
Hello,

This is not really a bug, more an improvement, but as there is no forum category for improvements or request (maybe an idea to add one?), I decided to put it here.

This improvement is only described here for the grid.datetime.php class, but I expect the same improvements can be made to the other date fields.

For my project I wanted to have the English ordinal suffix for the day of the month and at that point I noticed it was not directly possible. When looking at the code of the grid.datetime.php class I noticed the 'toFormat()' method of the JDate object is used. This uses the format as used in PHP's strftime() function, which does not support this ordinal suffix, whilst the PHP Date() function does support this.
As the toFormat() method will possibly deprecated in the future (by Joomla) and they encourage you to use the format() method (which does uses the Date() format), I decided to fix it as follows:

In my component parameters I created a new field, in my case called: date_format_method, which only has to options: either 'new' for using the Date() format, and consequently will call the format() method to format the date. The other is 'old' which uses the strftime() format, and calls the toFormat() method.

Additionally (which is not really relevant to this issue, but I think it is a nice improvement), I added a another field for 'date format', which specifies the default format for a date (when it is not specified in the call to the DOM element).

The code for the modified grid.datetime.php is below:
<?php

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );


class JDomHtmlGridDatetime extends JDomHtmlGrid
{
	var $level = 3;			//Namespace position
	var $last = true;		//This class is last call
	var $format;
        var $format_method = "new";

	/*
	 * Constuctor
	 * 	@namespace 	: requested class
	 *  @options	: Configuration
	 * 	@dataKey	: database field name
	 * 	@dataObject	: complete object row (stdClass or Array)
	 * 	@dataValue	: value  default = dataObject->dataKey
	 * 	@num		: Num position in list
	 *
	 *	@format		: Date format  -   default='%Y-%m-%d'
	 *
	 */
	function __construct($args)
	{

		parent::__construct($args);
                $config = JComponentHelper::getParams('com_slm');

                // get the date formatting method
                $this->format_method = $config->get("date_format_method","new");
                
                // get the default date format
                $default_format = $config->get("date_format");
                if ($default_format=="") {
                    // just put some format if not specified yet
                    if ($this->format_method=="new") {
                        $default_format = 'Y-m-d H:i';
                    } else {
                        $default_format = '%Y-%m-%d %H:%M';
                    }
                } 

		$this->arg('dateFormat'			, 6, $args, $default_format);
                
	}

	function build()
	{
		$formatedDate = "";

		if ($this->dataValue
			&& ($this->dataValue != "0000-00-00")
			&& ($this->dataValue != "00:00:00")
			&& ($this->dataValue != "0000-00-00 00:00:00"))
		{
			jimport("joomla.utilities.date");
			$date = new JDate($this->dataValue);
                        // Use the correct formatting method according to the component's setting
                        if ($this->format_method=="old") {
                            $formatedDate = $date->toFormat($this->dateFormat);
                        } else {
                            $formatedDate = $date->format($this->dateFormat);
                        }
		}

		$this->addClass('grid-date');

		$html = '<span <%STYLE%><%CLASS%><%SELECTORS%>>'
			.	$formatedDate
			.	'</span>';

		return $html;
	}

}

Hopefully this will help other people as well, especially when they (like me) want to use the English ordinal suffix in date formats.

Kind regards,

Misha
The administrator has disabled public write access.
The following user(s) said Thank You: JoomGuy

Re: Improvement to dom.html.grid.datetime 24 Nov 2012 14:17 #5632

  • JoomGuy
  • JoomGuy's Avatar
  • Offline
  • Moderator
  • Joomla Enthusiast, Lover of Cooking
  • Posts: 1115
  • Thank you received: 195
  • Karma: 64
Hi Misha,

That's a nice catch!
This is not really a bug, more an improvement, but as there is no forum category for improvements or request (maybe an idea to add one?), I decided to put it here.
Actually, there is a New Functionalities section... I think that's the right place for it.

I hope you don't mind but, I'm moving the post there.

Thanks again,

Gez
Need help with your Cook/Joomla Project? . PM me to find out what I can help with. NO time wasters please!!!
Last Edit: 24 Nov 2012 14:20 by JoomGuy.
The administrator has disabled public write access.

Re: Improvement to dom.html.grid.datetime 25 Nov 2012 07:20 #5635

  • blue-canoe
  • blue-canoe's Avatar
  • Offline
  • Senior Member
  • Posts: 57
  • Thank you received: 16
  • Karma: 7
Hi Gez,

I did not spot that section yet. Thanks, I will use that from now on for these kind of things.

Misha
The administrator has disabled public write access.
The following user(s) said Thank You: JoomGuy

Re: Improvement to dom.html.grid.datetime 03 Dec 2012 12:23 #5776

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 987
  • Karma: 140
Added.

html.grid.datetime is now deprecated for html.fly.datetime.

Cook is still generating with html.grid.datetime, but will change in future.
Coding is now a piece of cake
The administrator has disabled public write access.
Time to create page: 0.059 seconds

Get Started