Floodlight.php
changeset 0 4d19ad17b07c
child 1 c9c49ce179e9
equal deleted inserted replaced
-1:000000000000 0:4d19ad17b07c
       
     1 <?php
       
     2 /**!info**
       
     3 {
       
     4   "Plugin Name"  : "Floodlight",
       
     5   "Plugin URI"   : "http://enanocms.org/plugin/floodlight",
       
     6   "Description"  : "A much broader search than Spotlight. Adds auto-completion to the Search sidebar block.",
       
     7   "Author"       : "Dan Fuhry",
       
     8   "Version"      : "1.1.5",
       
     9   "Author URI"   : "http://enanocms.org/"
       
    10 }
       
    11 **!*/
       
    12 
       
    13 $plugins->attachHook('sidebar_fetch_return', 'floodlight_inject_searchflags($return);');
       
    14 $plugins->attachHook('autofill_json_request', 'floodlight_perform_search($dataset);');
       
    15 $plugins->attachHook('session_started', 'floodlight_add_js_page();');
       
    16 $plugins->attachHook('common_post', 'floodlight_intercept_search();');
       
    17 $plugins->attachHook('compile_template', 'floodlight_add_js();');
       
    18 
       
    19 function floodlight_inject_searchflags(&$return)
       
    20 {
       
    21   if ( strstr($return[0], '<input name="q"') )
       
    22   {
       
    23     $hackme =& $return[0];
       
    24   }
       
    25   else if ( strstr($return[1], '<input name="q"') )
       
    26   {
       
    27     $hackme =& $return[1];
       
    28   }
       
    29   else
       
    30   {
       
    31     return;
       
    32   }
       
    33   $hackme = str_replace('<input name="q"', '<input name="q" autocomplete="off" onkeyup="this.onkeyup = null; this.className = \'autofill floodlight\'; autofill_init_element(this, {});"', $hackme);
       
    34 }
       
    35 
       
    36 function floodlight_perform_search(&$dataset)
       
    37 {
       
    38   global $db, $session, $paths, $template, $plugins; // Common objects
       
    39   if ( $_GET['type'] == 'floodlight' )
       
    40   {
       
    41     $results = perform_search($_GET['userinput'], $warnings, false, $word_list);
       
    42     if ( count($results) > 5 )
       
    43     {
       
    44       $results = array_slice($results, 0, 5);
       
    45     }
       
    46     foreach ( $results as $result )
       
    47     {
       
    48       $dataset[] = array(
       
    49           0 => "go:{$paths->nslist[$result['namespace']]}{$result['page_id']}",
       
    50           'title' => str_replace(array('<highlight>', '</highlight>'), array('<b>', '</b>'), $result['page_name']),
       
    51           'score' => $result['score'],
       
    52           'type' => $result['page_note'],
       
    53           'size' => $result['page_length'],
       
    54         );
       
    55     }
       
    56   }
       
    57 }
       
    58 
       
    59 function floodlight_add_js_page()
       
    60 {
       
    61   global $db, $session, $paths, $template, $plugins; // Common objects
       
    62   $paths->add_page(array(
       
    63       'name' => 'Floodlight Javascript',
       
    64       'urlname' => 'FloodlightJS',
       
    65       'namespace' => 'Special',
       
    66       'visible' => 0,
       
    67       'protected' => 0,
       
    68       'comments_on' => 0,
       
    69       'special' => 0
       
    70     ));
       
    71 }
       
    72 
       
    73 function floodlight_intercept_search()
       
    74 {
       
    75   global $db, $session, $paths, $template, $plugins; // Common objects
       
    76   if ( $paths->page_id == 'Search' && $paths->namespace == 'Special' )
       
    77   {
       
    78     if ( isset($_GET['q']) && preg_match('/^go:/', $_GET['q']) )
       
    79     {
       
    80       redirect(makeUrl(preg_replace('/^go:/', '', $_GET['q'])), '', '', 0);
       
    81     }
       
    82   }
       
    83 }
       
    84 
       
    85 function floodlight_add_js()
       
    86 {
       
    87   global $db, $session, $paths, $template, $plugins; // Common objects
       
    88   $template->add_header('<script type="text/javascript" src="' . makeUrlNS('Special', 'FloodlightJS', false, true) . '"></script>');
       
    89 }
       
    90 
       
    91 function page_Special_FloodlightJS()
       
    92 {
       
    93   global $db, $session, $paths, $template, $plugins; // Common objects
       
    94   header('Content-type: text/javascript');
       
    95   header('ETag: ' . sha1(__FILE__));
       
    96   
       
    97   global $aggressive_optimize_html;
       
    98   $aggressive_optimize_html = false;
       
    99   
       
   100   echo <<<EOF
       
   101 addOnloadHook(function()
       
   102 {
       
   103   var autofill_schemas = window.autofill_schemas || {};
       
   104   autofill_schemas.floodlight = {
       
   105     init: function(element, fillclass, params)
       
   106     {
       
   107       params = params || {};
       
   108       $(element).autocomplete(makeUrlNS('Special', 'Autofill', 'type=' + fillclass) + '&userinput=', {
       
   109           minChars: 3,
       
   110           formatItem: function(row, _, __)
       
   111           {
       
   112             var type = ( typeof(row.type) == 'string' ) ? row.type : '';
       
   113             var html = '<big>' + row.title + '</big> <small>' + type + '</small>';
       
   114             html += '<br /><small>' + \$lang.get('floodlight_lbl_score') + row.score + '% | ' + row.size + '</small>';
       
   115             return html;
       
   116           },
       
   117           tableHeader: '<tr><th>' + \$lang.get('floodlight_table_heading') + '</th></tr>',
       
   118           showWhenNoResults: true,
       
   119           onItemSelect: function(li)
       
   120           {
       
   121             window.location = makeUrl(li.selectValue.replace(/^go:/, ''));
       
   122           },
       
   123           width: 300,
       
   124           noResultsHTML: '<tr><td class="row1" style="font-size: smaller;">' + \$lang.get('floodlight_msg_no_results') + '</td></tr>',
       
   125       });
       
   126     }
       
   127   };
       
   128 });
       
   129 EOF;
       
   130 }
       
   131 
       
   132 /**!language**
       
   133 
       
   134 The following text up to the closing comment tag is JSON language data.
       
   135 It is not PHP code but your editor or IDE may highlight it as such. This
       
   136 data is imported when the plugin is loaded for the first time; it provides
       
   137 the strings displayed by this plugin's interface.
       
   138 
       
   139 You should copy and paste this block when you create your own plugins so
       
   140 that these comments and the basic structure of the language data is
       
   141 preserved. All language data is in the same format as the Enano core
       
   142 language files in the /language/* directories. See the Enano Localization
       
   143 Guide and Enano API Documentation for further information on the format of
       
   144 language files.
       
   145 
       
   146 The exception in plugin language file format is that multiple languages
       
   147 may be specified in the language block. This should be done by way of making
       
   148 the top-level elements each a JSON language object, with elements named
       
   149 according to the ISO-639-1 language they are representing. The path should be:
       
   150 
       
   151   root => language ID => categories array, ( strings object => category \
       
   152   objects => strings )
       
   153 
       
   154 All text leading up to first curly brace is stripped by the parser; using
       
   155 a code tag makes jEdit and other editors do automatic indentation and
       
   156 syntax highlighting on the language data. The use of the code tag is not
       
   157 necessary; it is only included as a tool for development.
       
   158 
       
   159 <code>
       
   160 {
       
   161   // english
       
   162   eng: {
       
   163     categories: [ 'meta', 'floodlight' ],
       
   164     strings: {
       
   165       meta: {
       
   166         floodlight: 'Floodlight plugin'
       
   167       },
       
   168       floodlight: {
       
   169         table_heading: 'Search results',
       
   170         msg_no_results: 'No results',
       
   171         lbl_score: 'Relevance: ',
       
   172       }
       
   173     }
       
   174   }
       
   175 }
       
   176 </code>
       
   177 **!*/