Keyword

itemlist query enhancement request

  • Adam Jakab
  • Adam Jakab's Avatar Topic Author
  • Offline
  • New Member
More
9 years 3 months ago #136271 by Adam Jakab
itemlist query enhancement request was created by Adam Jakab
Hi to all,

I am working on a component/plugin/module package called K2 Extra Fields Plus which enhances to extra fields by letting user through an external component to define the extra fields for an extra field group created in k2. It then injects the fields in the "extra fields" tab when modifying items so user can set the values as it was native k2 extra fields. Why I did this is because built-in k2 extra fields are quite limited. The entire component works with without making any modifications whatsoever to K2 core. (I'll soon put source on github so you can have a look)

The component also provides a search module (configurable from the component for specific categories) where user can define on which of the extra fields he wants to be able to filter. The module uses Select2 and bootstrap-slider(for numeric search/range search) plugins.
When user submits the search module form, the K2 plugin bundled with the package intercepts the "onK2BeforeSetQuery" event and modifies it by adding the custom WHERE conditions to the query. Very simple, yet there is a problem:

The K2ModelItemlist class in components/com_k2/models/itemlist.php triggers this event in two methods: getData and getTotal. These methods craft the query passed with the "onK2BeforeSetQuery" event as a string. This is quite a complex query and I think refactoring these methods by using JDatabaseQuery to build it would be a very good idea and your code would also become clearer. Surely, doing this, the two methods, which now rebuild completely the query, by using JDatabaseQuery could share the common parts of the query and add/modify only their specific stuff(like select COUNT...). It would make it much more maintainable.

The problem i have in my specific case is that to be able to modify this SQL, I am using PHPSQLParser(code.google.com/p/php-sql-parser/) to split up the query , then I re-build the JDatabaseQuery object, add my custom WHERE conditions, and finally calling the __toString() method I pass it back to K2. Even though it works well, it is a lot of extra work which could be 1000X simpler if the above stated methods would pass JDatabaseQuery object for the event listeners.

I realize that there could be lots(maybe not so many) of third party extensions which are using this event so I don't think you can modify '$dispatcher->trigger('onK2BeforeSetQuery', array(&$query));' in the near future.

My suggestion would be this:
1) rebuild the query in the by using JDatabaseQuery
2) trigger a new event '$dispatcher->trigger('onK2BeforeSetJDatabaseQuery', array(&$jDbQuery));' by passing the JDatabaseQuery object
3) convert the JDatabaseQuery object to string: $query = JDatabaseQuery->__toString();
4) then trigger the old event '$dispatcher->trigger('onK2BeforeSetQuery', array(&$query));' exactly as before

I'd love to have this mod in K2, so that I could remove the PHPSQLParser layer from my application. I volunteer to do this mod, test it and submit a Pull Request or pass you the code once it is OK.

Screenshots:
i.imgur.com/vuwpzpM.png
i.imgur.com/lj6ftXG.png
i.imgur.com/MfFHEAN.png

What do you think?
Thanks
A

Please Log in or Create an account to join the conversation.

More
9 years 3 months ago #136370 by Lefteris
Replied by Lefteris on topic itemlist query enhancement request
Hi,

I am afraid that i will have to reject your request. There is a small thing that you are missing here. K2 v2 supports also Joomla! 1.5, which of course does not include the new JDatabaseQuery . And since you can already can achieve what you need with your own code i don't see any reason for adding new events to K2 while we are working for the new K2v3 version.

Finally note, that K2v3 is using the JDatabaseQuery API so it will be easy for developers to extend K2 using the onK2BeforeSetQuery events.

JoomlaWorks Support Team
---
Please search the forum before posting a new topic :)

Please Log in or Create an account to join the conversation.

  • Adam Jakab
  • Adam Jakab's Avatar Topic Author
  • Offline
  • New Member
More
9 years 2 months ago - 9 years 2 months ago #136632 by Adam Jakab
Replied by Adam Jakab on topic itemlist query enhancement request
Dear @991-lefteris,

Thanks for the answer, that's cool I can understand your reasons for rejecting my request. I think this is the last Joomla project I am working on because I am moving towards web applications built on top of Symfony. I'd like to thank your team for K2 because it is an excellent tool and if you care please take a look at the this repo (i will update it soon) and feel free to use ideas and/or codebase for your upcoming v3. Also, this is the site I am working on using this component if you want to see a working implementation of it(it is temporary and soon it will be moved to elsewhere).

The basic idea behind making this add-on component for k2 was to have more freedom and flexibility in defining extra fields, data providers which can be shared between fields and configuration of custom search modules (based on extra fields) for having a powerful search facility for k2 items. What I could do was limited because I did not want to touch k2 codebase so I had to do quite some integration work through plugins resulting a messy code - something you would not have to worry about.

In the past I used k2 from a developer's perspective quite a lot so if you are interested I can pinpoint you some things, especially related to extra fields, which I think could be enchanced to make k2 even more versatile.

Thanks again for the excellent work

a\
Last edit: 9 years 2 months ago by Adam Jakab. Reason: added url for working presentation

Please Log in or Create an account to join the conversation.


Powered by Kunena Forum