Last active
January 3, 2017 08:09
-
-
Save jeremyboggs/3319ce091a751912101943dc1f73c8c6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class CustomItemsBrowsePlugin extends Omeka_Plugin_AbstractPlugin | |
{ | |
protected $_hooks = array( | |
'items_browse_sql' | |
); | |
public function hookItemsBrowseSql($args) { | |
$select = $args['select']; | |
$sort_string = "CASE WHEN et_sort.text REGEXP '<[^>]*>' = 1 THEN TRIM(SUBSTR(et_sort.text, INSTR(et_sort.text ,' '))) ELSE et_sort.text END ASC"; | |
$select->reset(Zend_Db_Select::ORDER); | |
$sort_order = new Zend_Db_Expr($sort_string); | |
$select->order($sort_order); | |
get_db()->getTable('Item')->applySorting($select, 'Dublin Core,Title', 'ASC'); | |
} | |
} |
@luku Thanks! For the correction, and for the suggestion on $sortDir
. Yeah, as written this would fail on nested HTML, which is entirely possible to have. And agreed about the usefulness of a sort_key field.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I guess you meant:
TRIM(SUBSTR(et_sort.text, INSTR(et_sort.text,'>')+1))
Or did I misunderstand?
It's pretty clever way of stripping HTML from sorting, if the whole string is inside HTML but it could still fail if you have nested tags, for example
<em><sup>190</sup> Oranges</em>
, not sure which markup tynimce produces...Note: I wrote about similar problem with natural sorting in your issue #735. This could be another example that can utilize
sort_key
field inelement_texts
(it would store the text without html tags, even nested tags). Though, in your scenario the inline SQL performs very well, so it's not as urgent, as for my case.As for sort_dir, you could simply use:
$sortDir = isset($args['params'][Omeka_Db_Table::SORT_DIR_PARAM]) && $args['params'][Omeka_Db_Table::SORT_DIR_PARAM] == 'd' ? DESC : 'ASC';