ultimix
search.php
Go to the documentation of this file.
1 <?php
2 
3  /*
4  * This source code is a part of the Ultimix Project.
5  * It is distributed under BSD license. All other third side source code (like tinyMCE) is distributed under
6  * it's own license wich could be found from the corresponding files or sources.
7  * This source code is provided "as is" without any warranties or garanties.
8  *
9  * Have a nice day!
10  *
11  * @url http://ultimix.sorceforge.net
12  *
13  * @author Alexey "gdever" Dodonov
14  */
15 
26  class search_1_0_0{
27 
38  var $BlockSettings = false;
39  var $CachedMultyFS = false;
40  var $Security = false;
41  var $String = false;
42 
53  var $Output = false;
54 
65  function __construct()
66  {
67  try
68  {
69  $this->BlockSettings = get_package_object( 'settings::settings' , 'last' , __FILE__ );
70  $this->CachedMultyFS = get_package( 'cached_multy_fs' , 'last' , __FILE__ );
71  $this->Security = get_package( 'security' , 'last' , __FILE__ );
72  $this->String = get_package( 'string' , 'last' , __FILE__ );
73  }
74  catch( Exception $e )
75  {
76  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
77  }
78  }
79 
99  {
100  try
101  {
102  $Config = $this->CachedMultyFS->get_config( __FILE__ , 'cf_search_access' , 'exploded' );
103  $Settigs = get_package_object( 'settings::settings' , 'last' , __FILE__ );
104  $Packages = array();
105 
106  foreach( $Config as $i => $ConfigLine )
107  {
108  $Settigs->load_settings( $ConfigLine );
109 
110  $PackageName = $Settigs->get_setting( 'package_name' );
111  $PackageVersion = $Settigs->get_setting( 'package_version' );
112 
113  $Packages [] = get_package( $PackageName , $PackageVersion , __FILE__ );
114  }
115 
116  return( $Packages );
117  }
118  catch( Exception $e )
119  {
120  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
121  }
122  }
123 
150  function get_records( &$Packages , $SearchString )
151  {
152  try
153  {
154  $Results = array();
155 
156  $Limitations = $this->Security->get_gp( 'search_output_limitations' , 'string' , '' );
157  $Limitations = explode( ',' , $Limitations );
158 
159  foreach( $Packages as $i => $Package )
160  {
161  $Results [] = call_user_func(
162  array( $Package , 'search' ) , get_field( $Limitations , $i , 0 ) , $SearchString
163  );
164  }
165 
166  return( $Results );
167  }
168  catch( Exception $e )
169  {
170  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
171  }
172  }
173 
196  function filter_results( $Results )
197  {
198  try
199  {
200  foreach( $Results as $i => $Records )
201  {
202  $Return = array();
203 
204  foreach( $Records as $j => $Record )
205  {
206  $Relevation = intval( get_field( $Record , 'relevation' ) );
207  if( $Relevation > 0 )
208  {
209  $Return [] = $Record;
210  }
211  }
212 
213  $Results[ $i ] = $Return;
214  }
215 
216  return( $Results );
217  }
218  catch( Exception $e )
219  {
220  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
221  }
222  }
223 
246  function get_maximum_relevation( $Records )
247  {
248  try
249  {
250  $MaxRelevation = 0;
251 
252  if( isset( $Records[ 0 ] ) )
253  {
254  $MaxRelevation = intval( get_field( $Records[ 0 ] , 'relevation' ) );
255 
256  foreach( $Records as $j => $Record )
257  {
258  $Tmp = intval( get_field( $Record , 'relevation' ) );
259  if( $Tmp > $MaxRelevation )
260  {
261  $MaxRelevation = $Tmp;
262  }
263  }
264  }
265 
266  return( $MaxRelevation );
267  }
268  catch( Exception $e )
269  {
270  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
271  }
272  }
273 
296  function normalize_relevation( &$Results )
297  {
298  try
299  {
300  $Return = array();
301 
302  foreach( $Results as $i => $Records )
303  {
304  $MaxRelevation = $this->get_maximum_relevation( $Records );
305 
306  foreach( $Records as $j => $Record )
307  {
308  set_field(
309  $Results[ $i ][ $j ] , 'relevation' ,
310  intval( get_field( $Results[ $i ][ $j ] , 'relevation' ) ) / $MaxRelevation
311  );
312  }
313  }
314 
315  return( $Results );
316  }
317  catch( Exception $e )
318  {
319  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
320  }
321  }
322 
345  function get_search_line( &$Results )
346  {
347  try
348  {
349  $SearchLine = array();
350 
351  /* setting fetches ids */
352  foreach( $Results as $i => $Result )
353  {
354  foreach( $Result as $j => $Record )
355  {
356  set_field( $Results[ $i ][ $j ] , 'fetch_id' , $i );
357  }
358  }
359 
360  /* composing records */
361  foreach( $Results as $i => $Result )
362  {
363  $SearchLine = array_merge( $SearchLine , $Result );
364  }
365 
366  return( array_slice( $SearchLine , 0 , 10 ) );
367  }
368  catch( Exception $e )
369  {
370  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
371  }
372  }
373 
400  private function get_preview_paraneters( &$Result , $SearchString )
401  {
402  try
403  {
404  $Position = strpos( $Text = get_field( $Result , 'record_text' ) , $SearchString );
405  $Length = strlen( $Text );
406  $PreviewSize = 512;
407  $LeftBorder = ( $Position - $PreviewSize > 0 ? $Position - $PreviewSize : 0 );
408  $RightBorder = ( $Position + $PreviewSize > $Length ? $Length : $Position + $PreviewSize );
409 
410  return( array( $Length , $LeftBorder , $RightBorder ) );
411  }
412  catch( Exception $e )
413  {
414  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
415  }
416  }
417 
444  private function compile_preview( &$Result , $SearchString )
445  {
446  try
447  {
448  list( $Length , $LeftBorder , $RightBorder ) = $this->get_preview_paraneters( $Result , $SearchString );
449 
450  $Preview = substr( $Text , $LeftBorder , $RightBorder - $LeftBorder );
451 
452  $Preview = ( $LeftBorder ? '...' : '' ).$Preview.( $RightBorder != $Length ? '...' : '' );
453  $Highlight = $this->CachedMultyFS->get_template( __FILE__ , 'search_highlight.tpl' );
454  $Hightlight = str_replace( '{search_string}' , $SearchString , $Hightlight );
455 
456  $Preview = str_replace( $SearchString , $Hightlight , $Preview );
457  }
458  catch( Exception $e )
459  {
460  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
461  }
462  }
463 
490  function prepare_for_output( $SearchLine , $SearchString )
491  {
492  try
493  {
494  foreach( $SearchLine as $i => $Result )
495  {
496  $Preview = $this->compile_preview( $Result , $SearchString );
497 
498  set_field( $SearchLine[ $i ] , 'record_text' , $Preview );
499  }
500 
501  return( $SearchLine );
502  }
503  catch( Exception $e )
504  {
505  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
506  }
507  }
508 
531  function get_next_page_limitations( &$SearchLine )
532  {
533  try
534  {
535  if( isset( $SearchLine[ 9 ] ) === false )
536  {
537  return( '' );
538  }
539 
540  $Limitations = $this->Security->get_gp( 'search_output_limitations' , 'string' , '' );
541  $Limitations = explode( ',' , $Limitations );
542 
543  foreach( $SearchLine as $i => $Record )
544  {
545  $FetchId = get_field( $Record , 'fetch_id' );
546 
547  $Limitations[ $FetchId ]++;
548  }
549 
550  return( implode( ',' , $Limitations ) );
551  }
552  catch( Exception $e )
553  {
554  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
555  }
556  }
557 
576  private function get_left_arrow()
577  {
578  try
579  {
580  $Arrows = array();
581 
582  $CurrentLimitations = $this->Security->get_gp( 'search_output_limitations' , 'string' , '' );
583  if( $CurrentLimitations != '' )
584  {
585  $Arrows [] = $this->CachedMultyFS->get_template( __FILE__ , 'search_form_prev_arrow.tpl' );;
586  }
587 
588  return( $Arrows );
589  }
590  catch( Exception $e )
591  {
592  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
593  }
594  }
595 
618  private function get_right_arrow( $NextLimitations )
619  {
620  try
621  {
622  $SearchString = $this->Security->get_gp( 'search_string' , 'string' );
623  $Page = $this->Security->get_gp( 'page' , 'integer' , 0 ) + 1;
624  $Code = $this->CachedMultyFS->get_template( __FILE__ , 'search_form_next_arrow.tpl' );
625  $Code = str_replace(
626  array( '{search_string}' , '{next_limitations}' , '{page}' ) ,
627  array( $SearchString , $NextLimitations , $Page ) , $Code
628  );
629 
630  return( $Code );
631  }
632  catch( Exception $e )
633  {
634  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
635  }
636  }
637 
660  function get_switch_page_controler( &$SearchLine )
661  {
662  try
663  {
664  $Arrows = $this->get_left_arrow();
665 
666  $NextLimitations = $this->get_next_page_limitations( $SearchLine );
667  if( $NextLimitations != '' )
668  {
669  $Arrows [] = $this->get_right_arrow( $NextLimitations );
670  }
671 
672  $Code = $this->CachedMultyFS->get_template( __FILE__ , 'search_form_controller.tpl' );
673  $Code = str_replace( '{arrows}' , implode( '&nbsp;' , $Arrows ) , $Code );
674 
675  return( $Code );
676  }
677  catch( Exception $e )
678  {
679  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
680  }
681  }
682 
701  function output_search_results( &$SearchLine )
702  {
703  try
704  {
705  $this->Output = '';
706 
707  foreach( $SearchLine as $i => $Result )
708  {
709  $TemplatePath = dirname( __FILE__ ).'/res/templates/search_item.tpl';
710  $this->Output .= $this->CachedMultyFS->file_get_contents( $TemplatePath );
711 
712  $this->Output = $this->String->print_record( $this->Output , $Result );
713  $this->Output = str_replace(
714  '{item_number}' ,
715  $this->Security->get_gp( 'page' , 'integer' , 0 ) * 10 + $i + 1 , $this->Output
716  );
717  }
718 
719  $this->Output .= $this->get_switch_page_controler( $SearchLine );
720  }
721  catch( Exception $e )
722  {
723  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
724  }
725  }
726 
741  function search_results()
742  {
743  try
744  {
745  $Packages = $this->get_list_of_packages();
746 
747  $SearchString = $this->Security->get_gp( 'search_string' , 'string' , false );
748 
749  if( $SearchString !== false )
750  {
751  $Records = $this->get_records( $Packages , $SearchString );
752 
753  $FilteredRecords = $this->filter_results( $Records );
754 
755  $NormalizedRecords = $this->normalize_relevation( $FilteredRecords );
756 
757  $SearchLine = $this->get_search_line( $NormalizedRecords );
758 
759  $SearchLine = $this->prepare_for_output( $SearchLine , $SearchString );
760 
761  $this->output_search_results( $SearchLine );
762  }
763  }
764  catch( Exception $e )
765  {
766  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
767  }
768  }
769 
792  function view( $Options )
793  {
794  try
795  {
796  $ContextSet = get_package( 'gui::context_set' , 'last' , __FILE__ );
797 
798  $ContextSet->add_context( dirname( __FILE__ ).'/conf/cfcx_search_form' );
799  $ContextSet->add_context( dirname( __FILE__ ).'/conf/cfcx_search_results' );
800 
801  if( $ContextSet->execute( $Options , $this , __FILE__ ) )return( $this->Output );
802 
803  return( $this->Output );
804  }
805  catch( Exception $e )
806  {
807  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
808  }
809  }
810 
833  function build_query_string( $Fields )
834  {
835  try
836  {
837  $QueryString = '1 = 1';
838 
839  $SearchString = $this->Security->get_gp( 'search_string' , 'string' , '' );
840 
841  if( $SearchString !== '' && isset( $Fields[ 0 ] ) )
842  {
843  $QueryString = '( '.$Fields[ 0 ]." LIKE '%".$SearchString."%'";
844 
845  for( $i = 1 ; $i < count( $Fields ) ; $i++ )
846  {
847  $QueryString .= ' OR '.$Fields[ $i ]." LIKE '%".$SearchString."%'";
848  }
849 
850  $QueryString .= ' )';
851  }
852 
853  return( $QueryString );
854  }
855  catch( Exception $e )
856  {
857  $a = func_get_args();_throw_exception_object( __METHOD__ , $a , $e );
858  }
859  }
860  }
861 ?>