{"action":"created","comment":{"url":"https://api.github.com/repos/ding2/bpi/pulls/comments/39490366","id":39490366,"diff_hunk":"@@ -332,149 +454,181 @@ function bpi_preprocess_bpi_search_results(&$variables) {\n }\n \n /**\n- * Create key/value pairs from the query string.\n+ * Get link for sorting by a field.\n  *\n- * This is a helper function for drupal core l() function.\n- * Munges the query string into key/value pairs array, so it is accepted\n- * by the l() function.\n- * Ex. admin/bpi?key1=value1&key2=value2\n+ * @param string $text\n+ *   The text on the link.\n+ * @param string $field\n+ *   The field to sort by.\n+ * @param string $default_direction\n+ *   The sorting direction to use if not currently sorting of this field.\n  *\n- * @param string $key\n- *   Parameter key.\n- * @param string $value\n- *   Parameter value.\n- * @param string $ignore\n- *   Key to be ignored. Used in removing filters.\n- *\n- * @return array\n- *   An array, in structure:\n- *   - key: value\n+ * @return string\n+ *   The link for sorting.\n  */\n-function _bpi_build_query($key = '', $value = '', $ignore = NULL) {\n-  $destination = drupal_get_destination();\n-  $current_path = parse_url(reset($destination));\n-  $query = isset($current_path['query']) ? $current_path['query'] : '';\n-\n-  $new_query_pairs = array();\n-\n-  if (!empty($query)) {\n-    // Get the parameters array.\n-    $components = explode('&', $query);\n-\n-    // Iterate and rearrange them.\n-    foreach ($components as $component) {\n-      $temp = explode('=', $component);\n-\n-      // Ignore a key from query string.\n-      if ($temp[0] == $ignore) {\n-        continue;\n-      }\n-\n-      // It's required to decode some values here, since Drupal's url()\n-      // encodes it weird, munging the 'space' entity value (%20).\n-      $new_query_pairs[$temp[0]] = urldecode($temp[1]);\n-    }\n+function bpi_get_sort_link($text, $field, $default_direction = 'asc') {\n+  $current_sorting = _bpi_get_current_sorting($field);\n+  if ($current_sorting) {\n+    $sort_direction = $current_sorting == 'asc' ? 'desc' : 'asc';\n+  }\n+  else {\n+    $sort_direction = $default_direction;\n   }\n+  $query = _bpi_build_query(array(\n+    BPI_QUERY_SORT_NAME => array(\n+      $field => $sort_direction,\n+    ),\n+  ), array(BPI_QUERY_SORT_NAME));\n+  $image = $current_sorting ? theme('tablesort_indicator', array('style' => $sort_direction)) : '';\n+  return l($text . $image, current_path(), array('query' => $query, 'html' => TRUE));\n+}\n \n-  // Append the new key/value pair to existing ones.\n-  if (!empty($key) && !empty($value) && $key != $ignore) {\n-    $new_query_pairs[$key] = $value;\n+/**\n+ * Get current sorting or direction.\n+ *\n+ * @param string $field\n+ *   Field to get sorting direction for.\n+ *\n+ * @return string|array\n+ *   The current sorting (field => direction) or the direction for a specified\n+ *   field.\n+ */\n+function _bpi_get_current_sorting($field = NULL) {\n+  $sorting = _bpi_get_query_parameter(BPI_QUERY_SORT_NAME);\n+  if ($field) {\n+    return isset($sorting[$field]) ? $sorting[$field] : NULL;\n   }\n+  return $sorting;\n+}\n \n-  return $new_query_pairs;\n+/**\n+ * Get current search.\n+ *\n+ * @return array\n+ *   The search.\n+ */\n+function _bpi_get_current_search_query() {\n+  $text = _bpi_get_query_parameter('text', NULL);\n+  $amount = _bpi_get_query_parameter('amount');\n+  $query = array();\n+  if ($text) {\n+    $query['text'] = $text;\n+  }\n+  if ($amount) {\n+    $query['amount'] = $amount;\n+  }\n+  return $query;\n }\n \n /**\n- * Used to retrieve the searched phrase after submit is clicked.\n+ * Get current filter.\n  *\n- * @see bpi_syndicate_page_form()\n+ * @return array\n+ *   The filter.\n+ */\n+function _bpi_get_current_filter() {\n+  $value = _bpi_get_query_parameter('filter', array());\n+  // @TODO: Make sure that only valid filter parameters are present.\n+  if (isset($value['agency_internal'])) {\n+    $value['agencyInternal'] = $value['agency_internal'];\n+    unset($value['agency_internal']);\n+  }\n+  return $value;\n+}\n+\n+/**\n+ * Decide if a filter is currently applied.\n  *\n- * @return string\n- *   Searched string.\n+ * @return bool\n+ *   Whether a filter is applied or not.\n  */\n-function _bpi_current_search_phrase() {\n-  return isset($_GET[BPI_SEARCH_PHRASE_KEY]) ? check_plain($_GET[BPI_SEARCH_PHRASE_KEY]) : '';\n+function _bpi_is_filter_applied() {\n+  $filter = _bpi_get_current_filter();\n+  // Filter is applied if something apart from logicalOperator is set.\n+  unset($filter['logicalOperator']);\n+  return !empty($filter);\n }\n \n /**\n- * List of filters that are available.\n+ * Get query for applying a filter.\n  *\n- * Basically that's limited to what is to be showed in filtering section.\n+ * @param string $field\n+ *   The field to filter on.\n+ * @param string $value\n+ *   The value match.\n  *\n  * @return array\n- *   Array of filter identifiers.\n+ *   The filter query.\n  */\n-function bpi_available_filters() {\n-  return array(\n-    BPI_SEARCH_PHRASE_KEY,\n-    'author',\n-    'category',\n-    'agency',\n-    'audience',\n+function _bpi_get_filter_query($field, $value, $clear = TRUE) {\n+  $query = array(\n+    'filter' => array(\n+      $field => array($value),\n+    ),\n   );\n+  return _bpi_build_query($query, $clear ? array('filter') : FALSE);\n }\n \n /**\n- * Creates filters markup, based on current query string.\n+ * Merge a bpi query into the current query string.\n  *\n- * @return string\n- *   HTMl markup.\n+ * @param array $query\n+ *   The new query.\n+ * @param bool|array $clear\n+ *   Clear all or some parameters.\n  *\n- * @ingroup themeable\n+ * @return array\n+ *   The merged query.\n  */\n-function bpi_build_filters() {\n-  $components = _bpi_build_query();\n-  $filters_markup = array();\n-  $enabled_filters = bpi_available_filters();\n-\n-  foreach ($components as $key => $value) {\n-    // Limit the filtering options.\n-    if (!in_array($key, $enabled_filters)) {\n-      continue;\n+function _bpi_build_query(array $query, $clear = FALSE) {\n+  $current_query = $clear === TRUE ? array() : _bpi_get_query_parameter();","path":"bpi.syndicate.inc","position":577,"original_position":577,"commit_id":"3e6bacfbb3161ed9bd7b6645449ff89c3449fe2b","original_commit_id":"3e6bacfbb3161ed9bd7b6645449ff89c3449fe2b","user":{"login":"cableman","id":111397,"avatar_url":"https://avatars.githubusercontent.com/u/111397?v=3","gravatar_id":"","url":"https://api.github.com/users/cableman","html_url":"https://github.com/cableman","followers_url":"https://api.github.com/users/cableman/followers","following_url":"https://api.github.com/users/cableman/following{/other_user}","gists_url":"https://api.github.com/users/cableman/gists{/gist_id}","starred_url":"https://api.github.com/users/cableman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cableman/subscriptions","organizations_url":"https://api.github.com/users/cableman/orgs","repos_url":"https://api.github.com/users/cableman/repos","events_url":"https://api.github.com/users/cableman/events{/privacy}","received_events_url":"https://api.github.com/users/cableman/received_events","type":"User","site_admin":false},"body":"This seams complex, could it be rewritten to a more simple statement.","created_at":"2015-09-15T09:26:53Z","updated_at":"2015-09-15T09:26:53Z","html_url":"https://github.com/ding2/bpi/pull/5#discussion_r39490366","pull_request_url":"https://api.github.com/repos/ding2/bpi/pulls/5","_links":{"self":{"href":"https://api.github.com/repos/ding2/bpi/pulls/comments/39490366"},"html":{"href":"https://github.com/ding2/bpi/pull/5#discussion_r39490366"},"pull_request":{"href":"https://api.github.com/repos/ding2/bpi/pulls/5"}}},"pull_request":{"url":"https://api.github.com/repos/ding2/bpi/pulls/5","id":44902085,"html_url":"https://github.com/ding2/bpi/pull/5","diff_url":"https://github.com/ding2/bpi/pull/5.diff","patch_url":"https://github.com/ding2/bpi/pull/5.patch","issue_url":"https://api.github.com/repos/ding2/bpi/issues/5","number":5,"state":"open","locked":false,"title":"Implement facet filtering in bpi ui","user":{"login":"rimi-itk","id":11267554,"avatar_url":"https://avatars.githubusercontent.com/u/11267554?v=3","gravatar_id":"","url":"https://api.github.com/users/rimi-itk","html_url":"https://github.com/rimi-itk","followers_url":"https://api.github.com/users/rimi-itk/followers","following_url":"https://api.github.com/users/rimi-itk/following{/other_user}","gists_url":"https://api.github.com/users/rimi-itk/gists{/gist_id}","starred_url":"https://api.github.com/users/rimi-itk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rimi-itk/subscriptions","organizations_url":"https://api.github.com/users/rimi-itk/orgs","repos_url":"https://api.github.com/users/rimi-itk/repos","events_url":"https://api.github.com/users/rimi-itk/events{/privacy}","received_events_url":"https://api.github.com/users/rimi-itk/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-09-14T08:01:03Z","updated_at":"2015-09-15T09:26:53Z","closed_at":null,"merged_at":null,"merge_commit_sha":"486527557482014d816b0c4348d3e61808b5b744","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/ding2/bpi/pulls/5/commits","review_comments_url":"https://api.github.com/repos/ding2/bpi/pulls/5/comments","review_comment_url":"https://api.github.com/repos/ding2/bpi/pulls/comments{/number}","comments_url":"https://api.github.com/repos/ding2/bpi/issues/5/comments","statuses_url":"https://api.github.com/repos/ding2/bpi/statuses/3e6bacfbb3161ed9bd7b6645449ff89c3449fe2b","head":{"label":"aakb:develop","ref":"develop","sha":"3e6bacfbb3161ed9bd7b6645449ff89c3449fe2b","user":{"login":"aakb","id":111706,"avatar_url":"https://avatars.githubusercontent.com/u/111706?v=3","gravatar_id":"","url":"https://api.github.com/users/aakb","html_url":"https://github.com/aakb","followers_url":"https://api.github.com/users/aakb/followers","following_url":"https://api.github.com/users/aakb/following{/other_user}","gists_url":"https://api.github.com/users/aakb/gists{/gist_id}","starred_url":"https://api.github.com/users/aakb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aakb/subscriptions","organizations_url":"https://api.github.com/users/aakb/orgs","repos_url":"https://api.github.com/users/aakb/repos","events_url":"https://api.github.com/users/aakb/events{/privacy}","received_events_url":"https://api.github.com/users/aakb/received_events","type":"Organization","site_admin":false},"repo":{"id":38877362,"name":"bpi","full_name":"aakb/bpi","owner":{"login":"aakb","id":111706,"avatar_url":"https://avatars.githubusercontent.com/u/111706?v=3","gravatar_id":"","url":"https://api.github.com/users/aakb","html_url":"https://github.com/aakb","followers_url":"https://api.github.com/users/aakb/followers","following_url":"https://api.github.com/users/aakb/following{/other_user}","gists_url":"https://api.github.com/users/aakb/gists{/gist_id}","starred_url":"https://api.github.com/users/aakb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aakb/subscriptions","organizations_url":"https://api.github.com/users/aakb/orgs","repos_url":"https://api.github.com/users/aakb/repos","events_url":"https://api.github.com/users/aakb/events{/privacy}","received_events_url":"https://api.github.com/users/aakb/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/aakb/bpi","description":"","fork":true,"url":"https://api.github.com/repos/aakb/bpi","forks_url":"https://api.github.com/repos/aakb/bpi/forks","keys_url":"https://api.github.com/repos/aakb/bpi/keys{/key_id}","collaborators_url":"https://api.github.com/repos/aakb/bpi/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/aakb/bpi/teams","hooks_url":"https://api.github.com/repos/aakb/bpi/hooks","issue_events_url":"https://api.github.com/repos/aakb/bpi/issues/events{/number}","events_url":"https://api.github.com/repos/aakb/bpi/events","assignees_url":"https://api.github.com/repos/aakb/bpi/assignees{/user}","branches_url":"https://api.github.com/repos/aakb/bpi/branches{/branch}","tags_url":"https://api.github.com/repos/aakb/bpi/tags","blobs_url":"https://api.github.com/repos/aakb/bpi/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/aakb/bpi/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/aakb/bpi/git/refs{/sha}","trees_url":"https://api.github.com/repos/aakb/bpi/git/trees{/sha}","statuses_url":"https://api.github.com/repos/aakb/bpi/statuses/{sha}","languages_url":"https://api.github.com/repos/aakb/bpi/languages","stargazers_url":"https://api.github.com/repos/aakb/bpi/stargazers","contributors_url":"https://api.github.com/repos/aakb/bpi/contributors","subscribers_url":"https://api.github.com/repos/aakb/bpi/subscribers","subscription_url":"https://api.github.com/repos/aakb/bpi/subscription","commits_url":"https://api.github.com/repos/aakb/bpi/commits{/sha}","git_commits_url":"https://api.github.com/repos/aakb/bpi/git/commits{/sha}","comments_url":"https://api.github.com/repos/aakb/bpi/comments{/number}","issue_comment_url":"https://api.github.com/repos/aakb/bpi/issues/comments{/number}","contents_url":"https://api.github.com/repos/aakb/bpi/contents/{+path}","compare_url":"https://api.github.com/repos/aakb/bpi/compare/{base}...{head}","merges_url":"https://api.github.com/repos/aakb/bpi/merges","archive_url":"https://api.github.com/repos/aakb/bpi/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/aakb/bpi/downloads","issues_url":"https://api.github.com/repos/aakb/bpi/issues{/number}","pulls_url":"https://api.github.com/repos/aakb/bpi/pulls{/number}","milestones_url":"https://api.github.com/repos/aakb/bpi/milestones{/number}","notifications_url":"https://api.github.com/repos/aakb/bpi/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/aakb/bpi/labels{/name}","releases_url":"https://api.github.com/repos/aakb/bpi/releases{/id}","created_at":"2015-07-10T11:34:53Z","updated_at":"2015-07-10T11:34:54Z","pushed_at":"2015-09-07T13:27:25Z","git_url":"git://github.com/aakb/bpi.git","ssh_url":"git@github.com:aakb/bpi.git","clone_url":"https://github.com/aakb/bpi.git","svn_url":"https://github.com/aakb/bpi","homepage":null,"size":426,"stargazers_count":0,"watchers_count":0,"language":"PHP","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"ding2:master","ref":"master","sha":"de8c4d23c0f78338aeeb19f5a31c1cf2084f3073","user":{"login":"ding2","id":564698,"avatar_url":"https://avatars.githubusercontent.com/u/564698?v=3","gravatar_id":"","url":"https://api.github.com/users/ding2","html_url":"https://github.com/ding2","followers_url":"https://api.github.com/users/ding2/followers","following_url":"https://api.github.com/users/ding2/following{/other_user}","gists_url":"https://api.github.com/users/ding2/gists{/gist_id}","starred_url":"https://api.github.com/users/ding2/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ding2/subscriptions","organizations_url":"https://api.github.com/users/ding2/orgs","repos_url":"https://api.github.com/users/ding2/repos","events_url":"https://api.github.com/users/ding2/events{/privacy}","received_events_url":"https://api.github.com/users/ding2/received_events","type":"Organization","site_admin":false},"repo":{"id":28138629,"name":"bpi","full_name":"ding2/bpi","owner":{"login":"ding2","id":564698,"avatar_url":"https://avatars.githubusercontent.com/u/564698?v=3","gravatar_id":"","url":"https://api.github.com/users/ding2","html_url":"https://github.com/ding2","followers_url":"https://api.github.com/users/ding2/followers","following_url":"https://api.github.com/users/ding2/following{/other_user}","gists_url":"https://api.github.com/users/ding2/gists{/gist_id}","starred_url":"https://api.github.com/users/ding2/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ding2/subscriptions","organizations_url":"https://api.github.com/users/ding2/orgs","repos_url":"https://api.github.com/users/ding2/repos","events_url":"https://api.github.com/users/ding2/events{/privacy}","received_events_url":"https://api.github.com/users/ding2/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/ding2/bpi","description":"","fork":true,"url":"https://api.github.com/repos/ding2/bpi","forks_url":"https://api.github.com/repos/ding2/bpi/forks","keys_url":"https://api.github.com/repos/ding2/bpi/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ding2/bpi/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ding2/bpi/teams","hooks_url":"https://api.github.com/repos/ding2/bpi/hooks","issue_events_url":"https://api.github.com/repos/ding2/bpi/issues/events{/number}","events_url":"https://api.github.com/repos/ding2/bpi/events","assignees_url":"https://api.github.com/repos/ding2/bpi/assignees{/user}","branches_url":"https://api.github.com/repos/ding2/bpi/branches{/branch}","tags_url":"https://api.github.com/repos/ding2/bpi/tags","blobs_url":"https://api.github.com/repos/ding2/bpi/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ding2/bpi/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ding2/bpi/git/refs{/sha}","trees_url":"https://api.github.com/repos/ding2/bpi/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ding2/bpi/statuses/{sha}","languages_url":"https://api.github.com/repos/ding2/bpi/languages","stargazers_url":"https://api.github.com/repos/ding2/bpi/stargazers","contributors_url":"https://api.github.com/repos/ding2/bpi/contributors","subscribers_url":"https://api.github.com/repos/ding2/bpi/subscribers","subscription_url":"https://api.github.com/repos/ding2/bpi/subscription","commits_url":"https://api.github.com/repos/ding2/bpi/commits{/sha}","git_commits_url":"https://api.github.com/repos/ding2/bpi/git/commits{/sha}","comments_url":"https://api.github.com/repos/ding2/bpi/comments{/number}","issue_comment_url":"https://api.github.com/repos/ding2/bpi/issues/comments{/number}","contents_url":"https://api.github.com/repos/ding2/bpi/contents/{+path}","compare_url":"https://api.github.com/repos/ding2/bpi/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ding2/bpi/merges","archive_url":"https://api.github.com/repos/ding2/bpi/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ding2/bpi/downloads","issues_url":"https://api.github.com/repos/ding2/bpi/issues{/number}","pulls_url":"https://api.github.com/repos/ding2/bpi/pulls{/number}","milestones_url":"https://api.github.com/repos/ding2/bpi/milestones{/number}","notifications_url":"https://api.github.com/repos/ding2/bpi/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ding2/bpi/labels{/name}","releases_url":"https://api.github.com/repos/ding2/bpi/releases{/id}","created_at":"2014-12-17T13:34:18Z","updated_at":"2015-03-04T09:30:32Z","pushed_at":"2015-09-14T13:27:32Z","git_url":"git://github.com/ding2/bpi.git","ssh_url":"git@github.com:ding2/bpi.git","clone_url":"https://github.com/ding2/bpi.git","svn_url":"https://github.com/ding2/bpi","homepage":null,"size":424,"stargazers_count":1,"watchers_count":1,"language":"PHP","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":4,"mirror_url":null,"open_issues_count":3,"forks":4,"open_issues":3,"watchers":1,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/ding2/bpi/pulls/5"},"html":{"href":"https://github.com/ding2/bpi/pull/5"},"issue":{"href":"https://api.github.com/repos/ding2/bpi/issues/5"},"comments":{"href":"https://api.github.com/repos/ding2/bpi/issues/5/comments"},"review_comments":{"href":"https://api.github.com/repos/ding2/bpi/pulls/5/comments"},"review_comment":{"href":"https://api.github.com/repos/ding2/bpi/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/ding2/bpi/pulls/5/commits"},"statuses":{"href":"https://api.github.com/repos/ding2/bpi/statuses/3e6bacfbb3161ed9bd7b6645449ff89c3449fe2b"}}}}