PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/liguelista/library/shortcodes/search-form/search-form.php

https://gitlab.com/oxidigitaluser/liguelista
PHP | 259 lines | 208 code | 34 blank | 17 comment | 16 complexity | 0da2466610c608c1e5edcfd73573f93b MD5 | raw file
  1. <?php
  2. class javo_search_form {
  3. public function __construct() {
  4. add_shortcode('javo_search_form', Array($this, 'javo_search_form_callback'));
  5. add_action('wp_ajax_nopriv_get_categories_autocomplete', Array($this, 'get_categories_autocomplete_callback'));
  6. add_action('wp_ajax_get_categories_autocomplete', Array($this, 'get_categories_autocomplete_callback'));
  7. }
  8. public function get_categories_autocomplete_callback() {
  9. $autoComplete = array();
  10. $listCategories = get_terms("item_category", Array("hide_empty" => 0));
  11. foreach ($listCategories as $category) {
  12. array_push($autoComplete, array("value" => $category->name, "data" => $category->term_id));
  13. }
  14. asort($autoComplete);
  15. echo json_encode(Array('result' => $autoComplete));
  16. exit;
  17. }
  18. public function javo_search_form_callback($atts, $content = '') {
  19. extract(shortcode_atts(Array(
  20. 'action' => 'home'
  21. , 'hide_field' => Array()
  22. ), $atts));
  23. $hide_field = @explode(',', $hide_field);
  24. if (!empty($hide_field)) {
  25. foreach ($hide_field as $idx => $item) {
  26. $hide_field[$item] = 'hide';
  27. unset($hide_field[$idx]);
  28. }
  29. }
  30. $javo_this_onoff = new javo_ARRAY($hide_field);
  31. if ($action == 'home') {
  32. $javo_this_form_action = home_url('/');
  33. } else {
  34. $javo_this_form_action = get_permalink((int) $action);
  35. }
  36. $javo_query = new javo_ARRAY($_GET);
  37. ob_start();
  38. ?>
  39. <div class="container search-type-a-wrap">
  40. <form role="form" data-javo-search-form>
  41. <div class="search-type-a-inner">
  42. <div class="search-box-inline<?php echo $javo_this_onoff->get('keyword', null) == 'hide' ? ' hidden' : ''; ?>">
  43. <input title="Digite ao menos 3 caracteres para pesquisar!" type="text" class="search-a-items" name="s" placeholder="<?php _e('Keyword', 'javo_fr'); ?>" value="<?php echo $javo_query->get('s', null); ?>">
  44. </div>
  45. <div class="search-box-inline<?php echo $javo_this_onoff->get('category', null) == 'hide'? ' hidden':'';?>" style="display: none;">
  46. <select name="filter[item_category]" class="form-control">
  47. <option value=""><?php _e('Category', 'javo_fr');?></option>
  48. <?php echo apply_filters('javo_get_selbox_child_term_lists', 'item_category', null, 'select', $javo_query->get('category', 0), 0, 0 );?>
  49. </select>
  50. </div>
  51. <div class="search-box-inline<?php echo $javo_this_onoff->get('location', null) == 'hide' ? ' hidden' : ''; ?>">
  52. <select name="filter[item_location]" class="form-control">
  53. <option value=""><?php _e('Location', 'javo_fr'); ?></option>
  54. <?php echo apply_filters('javo_get_selbox_child_term_lists', 'item_location', null, 'select-sigla', $javo_query->get('location', 0), 0, 0, "-"); ?>
  55. </select>
  56. </div><!-- /.search-box-inline -->
  57. <div class="search-box-inline hidden<?php //echo $javo_this_onoff->get('dropdown', null) == 'hide' || is_archive() || is_search() ? ' hidden' : ''; ?>">
  58. <select class="form-control" data-javo-search-form-action-type>
  59. <option value="data-javo-patch-form-for-result"><?php _e('Search Result Page', 'javo_fr'); ?></option>
  60. <option value="data-javo-patch-form-for-template"><?php _e('Search on the Map', 'javo_fr'); ?></option>
  61. </select>
  62. </div>
  63. <div class="search-box-inline">
  64. <input type="submit" class="btn btn-block btn-primary admin-color-setting" title="Pesquisar" alt="<?php _e('Search', 'javo_fr'); ?>" >
  65. </div><!-- /.col-md-2 -->
  66. </div> <!-- search-type-a-inner -->
  67. </form>
  68. <fieldset>
  69. <input type="hidden" value="<?php echo (int) $action > 0 ? get_permalink($action) : null; ?>" data-javo-search-action-template-url>
  70. </fieldset>
  71. <!-- Search Result Page -->
  72. <form action="<?php echo home_url('/'); ?>" method="get" data-javo-patch-form-for-result>
  73. <input type="hidden" name="post_type" value="item">
  74. <input type="hidden" name="category" value="<?php echo $javo_query->get('category'); ?>" data-javo-sf-category>
  75. <input type="hidden" name="location" value="<?php echo $javo_query->get('location'); ?>" data-javo-sf-location>
  76. <input type="hidden" name="s" data-javo-sf-keyword>
  77. </form><!-- /data-javo-patch-form-for-result : Go to Archive Page -->
  78. <!-- Javo Map Template -->
  79. <form action="<?php echo $javo_this_form_action; ?>" method="post" data-javo-patch-form-for-template>
  80. <input type="hidden" name="filter[item_category]" value="<?php echo $javo_query->get('category'); ?>" data-javo-sf-category>
  81. <input type="hidden" name="filter[item_location]" value="<?php echo $javo_query->get('location'); ?>" data-javo-sf-location>
  82. <input type="hidden" name="keyword" data-javo-sf-keyword>
  83. </form><!-- /data-javo-patch-form-for-template : Go to Map -->
  84. </div> <!-- container search-type-a-wrap -->
  85. <script type="text/javascript">
  86. jQuery(function($){
  87. jQuery('.search-box-inline').find('input[type=submit]').click(function(){
  88. var hasSearch = (jQuery('.search-a-items').val().length >= 4);
  89. var hasCategory = (jQuery('[name="filter[item_category]"]').val() !== "");
  90. if (!hasSearch && !hasCategory) {
  91. jQuery('.search-box-inline:nth-child(2)').showBalloon();
  92. jQuery('.search-box-inline:nth-child(3)').showBalloon();
  93. return false;
  94. }
  95. if(jQuery('[name="filter[item_location]"]').val() === ""){
  96. jQuery('.search-box-inline:nth-child(3)').showBalloon();
  97. return false;
  98. }
  99. /*
  100. if(jQuery('.search-a-items').val().length < 2) {
  101. jQuery('.search-a-items').showBalloon();
  102. return false;
  103. } else if(jQuery('[name="filter[item_category]"]').val() === ""){
  104. jQuery('.search-box-inline:nth-child(2)').showBalloon();
  105. return false;
  106. }
  107. */
  108. });
  109. /*jQuery('.search-a-items').keypress(function(){
  110. var regex = new RegExp("^[0-9A-Za-záàâãéèêíïóôõöúçñÁÀÂÃÉÈÍÏÓÔÕÖÚÇÑ'\" ]+$");
  111. var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
  112. if (!regex.test(key) && event.keyCode != 13) {
  113. event.preventDefault();
  114. return false;
  115. }
  116. });*/
  117. var javo_search_bar_script = {
  118. el:{
  119. origin: '[data-javo-search-form]'
  120. , result: '[data-javo-patch-form-for-result]'
  121. , template: '[data-javo-patch-form-for-template]'
  122. , type: '[data-javo-search-form-action-type]'
  123. }
  124. , template_url: $('[data-javo-search-action-template-url]').val()
  125. , msg: function(str){ $.javo_msg({content:str, delay:10000}); }
  126. , init: function(){
  127. jQuery('.search-a-items').balloon();
  128. jQuery('.search-box-inline:nth-child(2)').attr("title", "Selecione uma categoria!");
  129. jQuery('.search-box-inline:nth-child(2)').balloon();
  130. jQuery('.search-box-inline:nth-child(3)').attr("title", "Selecione uma cidade ou estado!");
  131. jQuery('.search-box-inline:nth-child(3)').balloon();
  132. var $cat = this.setAutoComplete('[name="filter[item_category]"]');
  133. var $loc = this.setAutoComplete('[name="filter[item_location]"]');
  134. $('body').on('change', '[name="s"]', function () {
  135. $('[name="filter[item_category]"]').val("");
  136. });
  137. var categories = new Array();
  138. $.ajax({
  139. url:'<?php echo admin_url("admin-ajax.php"); ?>'
  140. , type: 'post'
  141. , dataType: 'json'
  142. , async: false
  143. , data:{ action:'get_categories_autocomplete' }
  144. , success:function(d){
  145. categories = d.result;
  146. }
  147. , error: function(e){
  148. console.log(e.responseText);
  149. }
  150. });
  151. var categoriesArray = Object.keys(categories).map(function(k) { return categories[k] });
  152. var formatRegexp = function(q) {
  153. q = q.replace(/e/i,'[eéèêëEÉÈÊË]');
  154. q = q.replace(/a/i,'[aàáâãäAÀÁÂÃÄÅÆ]');
  155. q = q.replace(/c/i,'[cçÇC]');
  156. q = q.replace(/i/i,'[íìiïîÍÌIÌÍÎÏ]');
  157. q = q.replace(/o/i,'[oóòôöÒÓÔÕÖ]');
  158. q = q.replace(/u/i,'[uúùüûUÜÛÙÚ]');
  159. q = q.replace(/y/i,'[ýyYÿ^yÝ]');
  160. q = q.replace(/n/i,'[nñNÑ]');
  161. return q;
  162. };
  163. var _autocompleteLookup = function (suggestion, originalQuery, queryLowerCase) {
  164. var pattern = '(\\b|^)('+$.Autocomplete.utils.escapeRegExChars(queryLowerCase)+')';
  165. pattern = formatRegexp(pattern);
  166. var matcher = new RegExp(pattern);
  167. var ret = suggestion.value.toLowerCase().match(matcher);
  168. return ret;
  169. };
  170. var _autocompleteFormatResult = function (suggestion, currentValue) {
  171. var pattern = '(\\b|^)('+$.Autocomplete.utils.escapeRegExChars(currentValue)+')';
  172. pattern = formatRegexp(pattern);
  173. return suggestion.value.replace(new RegExp(pattern, 'gi'), '<strong>$1$2<\/strong>');
  174. };
  175. jQuery('[name="s"]').autocomplete({
  176. lookup: categoriesArray,
  177. dataType: 'json',
  178. minChars: 3,
  179. type: 'GET',
  180. onSelect: function (suggestion) {
  181. $('[name="filter[item_category]"]').val(suggestion.data);
  182. },
  183. onInvalidateSelection: function () {
  184. $('[name="filter[item_category]"]').val("");
  185. },
  186. lookupFilter: _autocompleteLookup,
  187. formatResult: _autocompleteFormatResult
  188. });
  189. $(document)
  190. .on('submit', this.el.origin, this.submit)
  191. .on('change', '[data-javo-search-form-action-type]', this.swapType);
  192. }
  193. , setAutoComplete: function(el){
  194. $(this.el.origin).find(el).chosen({ search_contains: 1 });
  195. return $(this.el.origin).find(el);
  196. }
  197. , swapType: function(){
  198. var o = javo_search_bar_script;
  199. if( o.template_url == ""){
  200. o.msg('<?php _e("The map template (to show result page) is not setup or was deleted. Please contact to webmaster.", "javo_fr"); ?>');
  201. $(this).get(0).selectedIndex = 0;
  202. return false;
  203. };
  204. }
  205. , submit: function(e){
  206. e.preventDefault();
  207. var o = javo_search_bar_script;
  208. var r = $( o.el.origin );
  209. var t = $('[' + $(o.el.type).val() + ']');
  210. t.find('[data-javo-sf-category]').val(r.find('[name="filter[item_category]"]').val());
  211. t.find('[data-javo-sf-location]').val(r.find('[name="filter[item_location]"]').val());
  212. if (r.find('[name="filter[item_category]"]').val() == undefined || r.find('[name="filter[item_category]"]').val() == "") {
  213. t.find('[data-javo-sf-keyword]').val(r.find('input[name="s"]').val());
  214. }
  215. t.submit();
  216. }
  217. }
  218. javo_search_bar_script.init();
  219. });
  220. </script>
  221. <?php
  222. return ob_get_clean();
  223. }
  224. }
  225. new javo_search_form();