SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/RefineBar.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace Sprout\Helpers;
  15.  
  16.  
  17. /**
  18.  * Provides the search refinement options in the admin UI
  19.  */
  20. class RefineBar
  21. {
  22. private $widgets = array();
  23.  
  24. private $curr_group = 'General';
  25. private $groups = array();
  26.  
  27. private $field_ops = [];
  28.  
  29.  
  30. /**
  31.   * Add a refine widget
  32.   *
  33.   * @param RefineWidget $widget Refinement widget to add
  34.   * @param string $operator Operator for WHERE clause, e.g. '=' or 'CONTAINS'
  35.   * The available values are the operators for {@see Pdb::buildClause}
  36.   * The value null indicates "auto-guess", which is CONTAINS for strings and = for integers
  37.   * @return void
  38.   */
  39. public function addWidget(RefineWidget $widget, $operator = null)
  40. {
  41. $this->widgets[] = $widget;
  42. $this->groups[$this->curr_group][] = $widget;
  43. if (!empty($operator)) {
  44. $this->field_ops[$widget->getName()] = $operator;
  45. }
  46. }
  47.  
  48. public function setGroup($name)
  49. {
  50. $this->curr_group = Enc::html($name);
  51. }
  52.  
  53.  
  54. /**
  55.   * Gets the bar
  56.   **/
  57. public function get()
  58. {
  59. $out = '';
  60.  
  61. $get_fields = $_GET;
  62. unset ($get_fields['page']);
  63. foreach ($this->widgets as $widget) {
  64. unset ($get_fields[$widget->getName()]);
  65. }
  66.  
  67. foreach ($get_fields as $name => $val) {
  68. $name = Enc::html($name);
  69. $val = Enc::html($val);
  70. $out .= "<input type=\"hidden\" name=\"{$name}\" value=\"{$val}\">";
  71. }
  72.  
  73. $out .= "<div class=\"refine-bar -clearfix\">";
  74. $out .= "<form action=\"\" method=\"get\">";
  75. $out .= "<h3>Search</h3>";
  76.  
  77. $out .= "<div class=\"refine-list -clearfix\">";
  78. $index = 0;
  79. foreach ($this->widgets as $widget) {
  80. $html = $widget->render();
  81. $label = Enc::html($widget->getLabel());
  82.  
  83. if ($html && $index < 4) {
  84. $filterLevel = "main";
  85. $out .= '<div class="refine-list-item refine-list-main">';
  86. $out .= $html;
  87. $out .= '</div>';
  88. } else if($html) {
  89. $out .= '<div class="refine-list-item refine-list-advanced">';
  90. $out .= $html;
  91. $out .= '</div>';
  92. }
  93. $index++;
  94. }
  95. $out .= "</div>";
  96.  
  97. $out .= "<div class=\"refine-submit\">";
  98. $out .= "<button type=\"submit\" class=\"refine-submit button button-green button-small\">Update</button>";
  99. $out .= "</div>";
  100.  
  101. if($index >= 4) {
  102. $out .= "<button type=\"button\" class=\"refine-advanced-button icon-link-button icon-before icon-keyboard_arrow_down\">Advanced</button>";
  103. }
  104.  
  105. $out .= "</form>";
  106. $out .= "</div>";
  107.  
  108. return $out;
  109. }
  110.  
  111. /**
  112.   * Renders the bar
  113.   **/
  114. public function render()
  115. {
  116. echo $this->get();
  117. }
  118.  
  119.  
  120. /**
  121.   * Returns true if any of the refine bar widgets refer to the specified field
  122.   **/
  123. public function hasField($field_name)
  124. {
  125. foreach ($this->widgets as $w) {
  126. if ($w->getName() == $field_name) return true;
  127. }
  128.  
  129. return false;
  130. }
  131.  
  132.  
  133. /**
  134.   * Return the operator to use in WHERE clauses, for a given field
  135.   *
  136.   * @param string $field_name Field, e.g. 'first_name'
  137.   * @return string Operator, e.g. 'CONTAINS'
  138.   * @return null Operator should be auto-detected
  139.   */
  140. public function getOperator($field_name)
  141. {
  142. return @$this->field_ops[$field_name];
  143. }
  144.  
  145.  
  146. /**
  147.   * Return a list of search widgets, in groups
  148.   **/
  149. public function getGroups()
  150. {
  151. return $this->groups;
  152. }
  153. }
  154.  
  155.  
  156.