Skip to content

Instantly share code, notes, and snippets.

@jeremyboggs
Last active January 3, 2017 08:09
Show Gist options
  • Select an option

  • Save jeremyboggs/3319ce091a751912101943dc1f73c8c6 to your computer and use it in GitHub Desktop.

Select an option

Save jeremyboggs/3319ce091a751912101943dc1f73c8c6 to your computer and use it in GitHub Desktop.
<?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

luku commented Aug 31, 2016

Copy link
Copy Markdown

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 in element_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';

@jeremyboggs

Copy link
Copy Markdown
Author

@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