diff --git a/workflows.php b/workflows.php index 936b309..1e098f8 100644 --- a/workflows.php +++ b/workflows.php @@ -15,6 +15,8 @@ class Workflows { private $path; private $home; private $results; + private $sortfield; + private $sortascending; /** * Description: @@ -384,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 ) ): @@ -402,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; @@ -480,4 +483,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