Improvement? Option to add Joomla Obj instances (use statements) #592

Closed
opened 2020-09-02 12:31:17 +00:00 by marcorensch · 3 comments
marcorensch commented 2020-09-02 12:31:17 +00:00 (Migrated from github.com)

Steps to reproduce the issue

Try to filter a query by date (like larger then current date or smaller then...).
In Joomla! You need to create a new Date instance for that. It is ok and the way to do that is to simply add
"use Joomla\CMS\Date\Date;" outside of your class(es) in the model.php then you can use something like :
$date = new Date(); inside your query before getting the items.

You cannot use the "use" statement inside of the class or function.

Expected result

A possibility to place own use statements in the model

Actual result

There is no way right?

How a model could look like:

defined('_JEXEC') or die('Restricted access');

use Joomla\Utilities\ArrayHelper;
// maybe a new field in JCB in the Custom Get  or site view PHP area?
use Joomla\CMS\Date\Date;   // <<--- HERE we should be able to add something

/**
 * Model for Events
 */
class CompModelEvents extends JModelList
{
	protected $user;
	protected $userId;
	protected $guest;
...

	/**
	 * Method to build an SQL query to load the list data.
	 */
	protected function getListQuery()
	{
		// Get the current user for authorisation checks
		$this->user = JFactory::getUser();
		$this->userId = $this->user->get('id');
		$this->guest = $this->user->get('guest');
		...
		// Get a db connection.
		$db = JFactory::getDbo();

		// Create a new query object.
		$query = $db->getQuery(true);
// Get from #__tblname as a
...
// What i've added to my query:
$datenow = new Date();             // <<-- Where i need the date
switch($appParams->get('daterange')){
    case '0':
        // Past Events
        $query->where('a.eventdate < ' . $db->quote($datenow));
        break;
    case '1':
        // Upcoming Events
        $query->where('a.eventdate > ' . $db->quote($datenow));
        break;
    case '2':
    default:
        // All Events
}

Additional comments

If there is any way to do this already please let me know, i think i've tried every CB Backend Option to add my use statement.

### Steps to reproduce the issue Try to filter a query by date (like larger then current date or smaller then...). In Joomla! You need to create a new Date instance for that. It is ok and the way to do that is to simply add "use Joomla\CMS\Date\Date;" outside of your class(es) in the model.php then you can use something like : $date = new Date(); inside your query before getting the items. You cannot use the "use" statement inside of the class or function. ### Expected result A possibility to place own use statements in the model ### Actual result There is no way right? ### How a model could look like: ``` defined('_JEXEC') or die('Restricted access'); use Joomla\Utilities\ArrayHelper; // maybe a new field in JCB in the Custom Get or site view PHP area? use Joomla\CMS\Date\Date; // <<--- HERE we should be able to add something /** * Model for Events */ class CompModelEvents extends JModelList { protected $user; protected $userId; protected $guest; ... /** * Method to build an SQL query to load the list data. */ protected function getListQuery() { // Get the current user for authorisation checks $this->user = JFactory::getUser(); $this->userId = $this->user->get('id'); $this->guest = $this->user->get('guest'); ... // Get a db connection. $db = JFactory::getDbo(); // Create a new query object. $query = $db->getQuery(true); // Get from #__tblname as a ... // What i've added to my query: $datenow = new Date(); // <<-- Where i need the date switch($appParams->get('daterange')){ case '0': // Past Events $query->where('a.eventdate < ' . $db->quote($datenow)); break; case '1': // Upcoming Events $query->where('a.eventdate > ' . $db->quote($datenow)); break; case '2': default: // All Events } ``` ### Additional comments If there is any way to do this already please let me know, i think i've tried every CB Backend Option to add my use statement.

Yes we should actually add the option to target controller, model, and view with the new name space implementation. This just makes sense.

I am working on a pro member plugin for this 👍 option to add header values.

For you to work with this for now is also simple:

$datenow = new Joomla\CMS\Date\Date();

or with JDate() check the class map;

$datenow = new JDate();

Will bump this post when I release the header editing plugin to the pro members.

Yes we should actually add the option to target controller, model, and view with the new name space implementation. This just makes sense. I am working on a pro member plugin for this :+1: option to add header values. For you to work with this for now is also simple: ```php $datenow = new Joomla\CMS\Date\Date(); ``` or with JDate() [check the class map](https://github.com/joomla/joomla-cms/blob/staging/libraries/classmap.php#L258); ```php $datenow = new JDate(); ``` Will bump this post when I release the header editing plugin to the pro members.

The option to add custom headers was asked in #548 so I will close this issue and you can follow the implementation in the initial request.

The option to add custom headers was asked in #548 so I will close this issue and you can follow the implementation in the initial request.
https://www.youtube.com/watch?v=TjoH5rj4UgM
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: joomla/Component-Builder#592
No description provided.