From 196ed6ba60c07624a8cba8d3dfb2cffa2cfa4ed0 Mon Sep 17 00:00:00 2001 From: timbooo Date: Wed, 18 Dec 2013 17:45:57 +0100 Subject: [PATCH 1/2] added function sortresults --- workflows.php | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/workflows.php b/workflows.php index 936b309..7942806 100644 --- a/workflows.php +++ b/workflows.php @@ -15,6 +15,8 @@ class Workflows { private $path; private $home; private $results; + private $sortfield; + private $sortascending; /** * Description: @@ -480,4 +482,43 @@ public function result( $uid, $arg, $title, $sub, $icon, $valid='yes', $auto=nul return $temp; } + /** + * Description: + * Helper function that sorts the results by given field and sort direction. + * Possible fields: uid, arg, title, subtitle, icon, valid, autocomplete, type + * + * @param $field - the field that will be used to sort the current array of results, defaults to 'title' + * @param $ascending - sort direction, defaults to true + * @return array - sorted array or false in case the given field is not allowed + */ + public function sortresults( $field='title', $ascending=true ) + { + // abort in case the field is not allowed + $allowed = array("uid", "arg", "title", "subtitle", "icon", "valid", "autocomplete", "type"); + if (!in_array($field, $allowed)): + return false; + endif; + + $this->sortfield = $field; + $this->sortascending = $ascending; + uasort($this->results, function ($a, $b) { + $c = $a[$this->sortfield]; + $d = $b[$this->sortfield]; + + if ( $c === $d ) { + return 0; + } + if ($this->sortascending) { + return ($c > $d) ? 1 : -1; + } else { + return ($c > $d) ? -1 : 1; + } + }); + + unset($this->sortfield); + unset($this->sortascending); + + return $this->results; + } + } \ No newline at end of file From a12f6daccd4f6f5e4c6525b3f85e979edb4ad88c Mon Sep 17 00:00:00 2001 From: timbooo Date: Sun, 23 Feb 2014 19:23:12 +0100 Subject: [PATCH 2/2] added optional file flags parameter to method write (e.g. to support appending to a file) --- workflows.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/workflows.php b/workflows.php index 7942806..1e098f8 100644 --- a/workflows.php +++ b/workflows.php @@ -386,9 +386,10 @@ public function mdfind( $query ) * * @param array - data to save to file * @param file - filename to write the cache data to + * @param flags - file flags used for 'file_put_contents' * @return none */ - public function write( $a, $b ) + public function write( $a, $b, $flags = 0 ) { if ( file_exists( $b ) ): if ( file_exists( $this->path.'/'.$b ) ): @@ -404,10 +405,10 @@ public function write( $a, $b ) if ( is_array( $a ) ): $a = json_encode( $a ); - file_put_contents( $b, $a ); + file_put_contents( $b, $a, $flags ); return true; elseif ( is_string( $a ) ): - file_put_contents( $b, $a ); + file_put_contents( $b, $a, $flags ); return true; else: return false;