Dynamic data source sorting
This is an open discussion with 13 replies, filed under General.
Search
Ok, found a way to dynamically modify sort and order. I forked the class.datasource.php file from the symphony2 integration branch and made a commit that can be found at: http://github.com/carsten/symphony-2/commit/4440ad610bd377e2e971c68a6742c51543eaf9e9 (I was a bit too enthousiastic and added an extra line).
Basically, add this code to /symphony/lib/toolkit/class.datasource.php
if(isset($this->dsParamORDER)) $this->dsParamORDER = $this->__processParametersInString($this->dsParamORDER, $this->_env); if(isset($this->dsParamSORT)) $this->dsParamSORT = $this->__processParametersInString($this->dsParamSORT, $this->_env);
above this line (40):
if(isset($this->dsParamSTARTPAGE)) $this->dsParamSTARTPAGE = $this->__processParametersInString($this->dsParamSTARTPAGE, $this->_env);
Then, go to your datasource file in /workspace/data-sources/data.yourdatasource.php and make sure you have the following lines at line 5-10:
public $dsParamORDER = '{$url-o}'; public $dsParamLIMIT = '{$url-l}'; public $dsParamREDIRECTONEMPTY = 'no'; public $dsParamSORT = '{$url-s}'; public $dsParamSTARTPAGE = '{$url-p}';
Go to 'public function construct' (around line 51) and make sure you add this line to the function:
$this->dsParamLIMIT = $_GET['l'] ? $this->__processParametersInString($_GET['l'], $this->_env) : '10';
Order, limit, sort and change the start page by adding url parameters such as ?p=1&s=name&o=desc&l=10.
Neato!
Please let me know if there is another way to set a default value for the dsParamLIMIT.
Great work Carsten!
Is there a potential here for a security vulnerability with the dynamic limit? Ideally there would be a maximum set (say 50 or 100) to prevent a malicious user incrementing this value and putting the database under strain.
One of my colleagues has spec'd out a new Data Source edit page which includes this functionality. We made a list of all of the edits we usually make to DS PHP files, which included this. We usually modify the PHP (thereby making a custom data source) and manually set the dsParamSort/Order
variables based on GET/POST criteria, but I like the idea of supporting this in the core.
I wold be hesitant about adding this exact code to the core, since making use of it still required an edit to the data source itself. I think it also needs the DS editor page to support the input of these variables, so that they can be chosen arbitrarily (i.e. I might already have a url-o
variable and want to use another name).
I would also like the DS Editor to support the input of these variables as it is now with the page number. The DS PHP file is not a good place to store a default limit to fall back on, it should go somewhere else in a configuration file together with a maximum result limit.
It would probably not be so hard to make changes to the core that support this functionality, but I would not recommend to use this code too (or any other code of mine for safety reasons (: ). It does the job for my current project though, and since I have a deadline coming up I will focus on other functionality first.
Being able to specify the sort field, sort order, and per-page (with a limit) would make complex data tables very easy. I also think this should go into the core. The tricky thing is how to align the UI so that you have either a drop down (for existing fields) or an input field — it would make sense to be able to define the parameter used as the value, as we do with “show page X of results” for pagination.
Has this solution been committed to the core yet? also If I used carstens solution on a 2.0.4 install, would it still work?
No idea about whether it works in 2.0.4, but you can check for yourself. Look for this file symphony/lib/toolkit/class.datasource.php
and look for Carsten’s code:
if(isset($this->dsParamORDER)) $this->dsParamORDER = $this->__processParametersInString($this->dsParamORDER, $this->_env); if(isset($this->dsParamSORT)) $this->dsParamSORT = $this->__processParametersInString($this->dsParamSORT, $this->_env);
Sweet, I was looking for a solution for this and found out that carsten’s code made it into 2.0.7.
The code still seems to be in 2.3. Is this still the preferred solution for dynamic DS-sorting, or is there some other new functionality in 2.3 making this possible?
Would this work with sessions or cookies rather then query-strings too?
Dear Core developers,
is this still on the list to be included in the DS Editor?
How would I set default sort criteria in case there is none specified in the according query-strings? Edit: ok sorry, I think I now get the idea how to set a default value. Maybe I don’t quite get it after all …
@animaux, I'd assume it would also work with session/cookie values however since those wouldn't be in the env
variable you would have to either add their values in using another custom datasource; or with the use of an extension to populate the values.
If you want a default limit in the datasource interface you can specify the following in the latest versions.
{$url-limit:10}
Meaning if your limit parameter is not set you will default to 10.
Brilliant! Thanks für your replies!
Create an account or sign in to comment.
I am using Nickdunn's pagination template and it works perfectly while limiting the size of the generated XML (and hopefully increasing speed). However, I would like to be able to sort the datasource by several attributes, either ascending or descending. Unfortunately, I can only select one way to sort the data-source. Also, it makes no sense to sort the resulting XML using XSL because the data source will only contain the maximum of results per page used for pagination. Now, I see two workarounds with the existing system:
Alternatively, I could find a way to dynamically modify $dsParamSORT and $dsParamORDER in the datasource file. It would be great to have an option for datasources to Sort by {$attribute} order by {$order} similar to show {$page} of results.
What do you think would be the best solution?