00001 <?php 00002 00003 namespace Habari; 00004 00008 class FormControlFacet extends FormControl 00009 { 00010 static $outpre; 00011 00015 public function _extend() 00016 { 00017 Stack::add('template_header_javascript', 'visualsearch' ); 00018 Stack::add('template_stylesheet', 'visualsearch-css'); 00019 Stack::add('template_stylesheet', 'visualsearch-datauri-css'); 00020 00021 00022 $config = new \stdClass(); 00023 $config->facets = array('type', 'status', 'author', 'from', 'to', 'tag'); 00024 $config->values = array( 00025 'type' => array( 00026 'entry', 00027 'page', 00028 ), 00029 'status' => array( 00030 'draft', 00031 'published', 00032 'scheduled', 00033 ), 00034 'author' => array( 00035 'admin' 00036 ), 00037 'tag' => array( 00038 'habari', 00039 'exploding', 00040 'sausages' 00041 )); 00042 $this->properties['data-facet-config'] = $config; 00043 $this->add_template_class('div', 'facet_ui'); 00044 } 00045 00050 public function pre_out() 00051 { 00052 $out = ''; 00053 if ( !self::$outpre ) { 00054 self::$outpre = true; 00055 $out = <<< CUSTOM_AUTOCOMPLETE_JS 00056 <script type="text/javascript"> 00057 controls.init(function(){ 00058 $('.facet_ui').each(function(){ 00059 var self = $(this); 00060 var target = $('#' + self.data('target')); 00061 var facet_config = target.data('facet-config'); 00062 self.data('visualsearch', VS.init({ 00063 container: self, 00064 query: '', 00065 showFacets: false, 00066 callbacks: { 00067 search: function(query, searchCollection) { 00068 console.log(query, searchCollection); 00069 }, 00070 facetMatches: function(callback) { 00071 if(facet_config.facetsURL != undefined) { 00072 $.post( 00073 facet_config.facetsURL, 00074 {}, 00075 function(response) { 00076 callback(response); 00077 } 00078 ) 00079 } 00080 else { 00081 callback(facet_config.facets) 00082 } 00083 }, 00084 valueMatches: function(facet, searchTerm, callback) { 00085 if(facet_config.valuesURL != undefined) { 00086 $.post( 00087 facet_config.valuesURL, 00088 { 00089 facet: facet, 00090 q: searchTerm 00091 }, 00092 function(response) { 00093 callback(response); 00094 } 00095 ) 00096 } 00097 else { 00098 if(facet_config.values[facet]!=undefined){ 00099 callback(facet_config.values[facet]); 00100 } 00101 } 00102 } 00103 } 00104 })); 00105 self.closest('form').on('submit', function(){ 00106 target.val(self.data('visualsearch').searchBox.value()); 00107 }); 00108 }); 00109 00110 }); 00111 </script> 00112 CUSTOM_AUTOCOMPLETE_JS; 00113 } 00114 return $this->controls_js($out); 00115 } 00116 00124 public function set_ajax($url, $ishtml = false) { 00125 $this->set_settings( 00126 array( 00127 'ajax_url' => $url, 00128 'ajax_ishtml' => $ishtml, 00129 ), 00130 false 00131 ); 00132 return $this; 00133 } 00134 00135 00136 public function get(Theme $theme) 00137 { 00138 $this->properties['type'] = 'hidden'; 00139 $this->properties['data-facet-config'] = json_encode($this->properties['data-facet-config'] ); 00140 $this->set_template_properties('div', array( 00141 'id' => $this->get_visualizer(), 00142 'data-target' => $this->get_id(), 00143 )); 00144 return parent::get($theme); 00145 } 00146 00150 public function get_visualizer() { 00151 return $this->get_id() . '_visualizer'; 00152 } 00153 00161 public function parse() { 00162 preg_match_all('/(\w+):\s*"([^"]+)"|(\w+):\s*([\S]+)|"([\w\s]+(?!:))"|([\S]+(?!:))/im', $this->value, $matches, PREG_SET_ORDER); 00163 $results = array(); 00164 foreach($matches as $match) { 00165 if(!empty($match[1])) { 00166 $results[$match[1]][] = trim($match[2], '"'); 00167 } 00168 if(!empty($match[3])) { 00169 $results[$match[3]][] = trim($match[4], '"'); 00170 } 00171 if(!empty($match[5])) { 00172 $results['text'][] = trim($match[5], '"'); 00173 } 00174 if(!empty($match[6])) { 00175 $results['text'][] = trim($match[6], '"'); 00176 } 00177 } 00178 return $results; 00179 } 00180 } 00181 00182 ?>