includes/functions.php
changeset 756 e8cf18383425
parent 741 a216e412c439
child 766 d6690840e331
equal deleted inserted replaced
755:9b4cd3ef42f3 756:e8cf18383425
  3040       $haystack_len = strlen($haystack);
  3040       $haystack_len = strlen($haystack);
  3041       $i = $i + strlen($needle) + strlen($start_tag) + strlen($end_tag);
  3041       $i = $i + strlen($needle) + strlen($start_tag) + strlen($end_tag);
  3042     }
  3042     }
  3043   }
  3043   }
  3044   return $haystack;
  3044   return $haystack;
       
  3045 }
       
  3046 
       
  3047 /**
       
  3048  * Registers a new type of search result. Because this is so tricky to do but keep clean, this function takes an associative array as its
       
  3049  * only parameter. This array configures the function. The required keys are:
       
  3050  *  - table: the database table to search
       
  3051  *  - titlecolumn: the column that will be used as the title of the search result. This will have a weight of 1.5
       
  3052  *  - uniqueid: a TPL-format string, variables being column names, that will be unique for every result. This should contain a string that
       
  3053  *              will be specific to your *type* of search result in addition to a primary key or other unique identifier.
       
  3054  *  - linkformat: an array with the keys page_id and namespace which are where your result will link, plus the following additional options:
       
  3055  *     - append: added to the full generated URL
       
  3056  *     - query: query string without initial "?"
       
  3057  * Additional options:
       
  3058  *  - datacolumn
       
  3059  *  - additionalcolumns: additional data to select if you want to use a custom formatting callback
       
  3060  *  - formatcallback: a callback or TPL string. If a callback, it will be called with the parameters being the current result row and an
       
  3061  *                    array of words in case you want to highlight anything; the callback will be expected to return a string containing
       
  3062  *                    a fully formatted and sanitized blob of HTML. If formatcallback is a TPL string, variables will be named after table
       
  3063  *                    columns.
       
  3064  *  - additionalwhere: additional SQL to inject into WHERE clause, in the format of "AND foo = bar"
       
  3065  * @example Working example of adding users to search results:
       
  3066  <code>
       
  3067   register_search_handler(array(
       
  3068       'table' => 'users',
       
  3069       'titlecolumn' => 'username',
       
  3070       'uniqueid' => 'ns=User;cid={username}',
       
  3071       'additionalcolumns' => array('user_id'),
       
  3072       'resultnote' => '[Member]',
       
  3073       'linkformat' => array(
       
  3074           'page_id' => '{username}',
       
  3075           'namespace' => 'User'
       
  3076         ),
       
  3077       'formatcallback' => 'format_user_search_result',
       
  3078     ));
       
  3079   
       
  3080   function format_user_search_result($row)
       
  3081   {
       
  3082     global $session, $lang;
       
  3083     $rankdata = $session->get_user_rank(intval($row['user_id']));
       
  3084     $rankspan = '<span style="' . $rankdata['rank_style'] . '">' . $lang->get($rankdata['rank_title']) . '</span>';
       
  3085     if ( empty($rankdata['user_title']) )
       
  3086     {
       
  3087       return $rankspan;
       
  3088     }
       
  3089     else
       
  3090     {
       
  3091       return '"' . htmlspecialchars($rankdata['user_title']) . "\" (<b>$rankspan</b>)";
       
  3092     }
       
  3093   }
       
  3094  </code>
       
  3095  * @param array Options array - see function documentation
       
  3096  * @return null
       
  3097  */
       
  3098 
       
  3099 global $search_handlers;
       
  3100 $search_handlers = array();
       
  3101 
       
  3102 function register_search_handler($options)
       
  3103 {
       
  3104   global $search_handlers;
       
  3105   
       
  3106   $required = array('table', 'titlecolumn', 'uniqueid', 'linkformat');
       
  3107   foreach ( $required as $key )
       
  3108   {
       
  3109     if ( !isset($options[$key]) )
       
  3110     {
       
  3111       throw new Exception("Required search handler option '$key' is missing");
       
  3112     }
       
  3113   }
       
  3114   $search_handlers[] = $options;
       
  3115   return null;
  3045 }
  3116 }
  3046 
  3117 
  3047 /**
  3118 /**
  3048  * From http://us2.php.net/urldecode - decode %uXXXX
  3119  * From http://us2.php.net/urldecode - decode %uXXXX
  3049  * @param string The urlencoded string
  3120  * @param string The urlencoded string