smiley_helper.php

Go to the documentation of this file.
00001 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002 /**
00003  * CodeIgniter
00004  *
00005  * An open source application development framework for PHP 4.3.2 or newer
00006  *
00007  * @package             CodeIgniter
00008  * @author              ExpressionEngine Dev Team
00009  * @copyright   Copyright (c) 2008, EllisLab, Inc.
00010  * @license             http://codeigniter.com/user_guide/license.html
00011  * @link                http://codeigniter.com
00012  * @since               Version 1.0
00013  * @filesource
00014  */
00015 
00016 // ------------------------------------------------------------------------
00017 
00018 /**
00019  * CodeIgniter Smiley Helpers
00020  *
00021  * @package             CodeIgniter
00022  * @subpackage  Helpers
00023  * @category    Helpers
00024  * @author              ExpressionEngine Dev Team
00025  * @link                http://codeigniter.com/user_guide/helpers/smiley_helper.html
00026  */
00027 
00028 // ------------------------------------------------------------------------
00029 
00030 /**
00031  * JS Insert Smiley
00032  *
00033  * Generates the javascrip function needed to insert smileys into a form field
00034  *
00035  * @access      public
00036  * @param       string  form name
00037  * @param       string  field name
00038  * @return      string
00039  */
00040 if ( ! function_exists('js_insert_smiley'))
00041 {
00042         function js_insert_smiley($form_name = '', $form_field = '')
00043         {
00044                 return <<<EOF
00045 <script type="text/javascript">
00046         function insert_smiley(smiley)
00047         {
00048                 document.{$form_name}.{$form_field}.value += " " + smiley;
00049         }
00050 </script>
00051 EOF;
00052         }
00053 }
00054 // ------------------------------------------------------------------------
00055 
00056 /**
00057  * Get Clickable Smileys
00058  *
00059  * Returns an array of image tag links that can be clicked to be inserted 
00060  * into a form field.  
00061  *
00062  * @access      public
00063  * @param       string  the URL to the folder containing the smiley images
00064  * @return      array
00065  */
00066 if ( ! function_exists('get_clickable_smileys'))
00067 {
00068         function get_clickable_smileys($image_url = '', $smileys = NULL)
00069         {
00070                 if ( ! is_array($smileys))
00071                 {
00072                         if (FALSE === ($smileys = _get_smiley_array()))
00073                         {
00074                                 return $smileys;
00075                         }
00076                 }
00077 
00078                 // Add a trailing slash to the file path if needed
00079                 $image_url = preg_replace("/(.+?)\/*$/", "\\1/",  $image_url);
00080 
00081                 $used = array();
00082                 foreach ($smileys as $key => $val)
00083                 {
00084                         // Keep duplicates from being used, which can happen if the
00085                         // mapping array contains multiple identical replacements.  For example:
00086                         // :-) and :) might be replaced with the same image so both smileys
00087                         // will be in the array.
00088                         if (isset($used[$smileys[$key][0]]))
00089                         {
00090                                 continue;
00091                         }
00092 
00093                         $link[] = "<a href=\"javascript:void(0);\" onClick=\"insert_smiley('".$key."')\"><img src=\"".$image_url.$smileys[$key][0]."\" width=\"".$smileys[$key][1]."\" height=\"".$smileys[$key][2]."\" alt=\"".$smileys[$key][3]."\" style=\"border:0;\" /></a>";
00094 
00095                         $used[$smileys[$key][0]] = TRUE;
00096                 }
00097 
00098                 return $link;
00099         }
00100 }
00101 
00102 // ------------------------------------------------------------------------
00103 
00104 /**
00105  * Parse Smileys
00106  *
00107  * Takes a string as input and swaps any contained smileys for the actual image
00108  *
00109  * @access      public
00110  * @param       string  the text to be parsed
00111  * @param       string  the URL to the folder containing the smiley images
00112  * @return      string
00113  */
00114 if ( ! function_exists('parse_smileys'))
00115 {
00116         function parse_smileys($str = '', $image_url = '', $smileys = NULL)
00117         {
00118                 if ($image_url == '')
00119                 {
00120                         return $str;
00121                 }
00122 
00123                 if ( ! is_array($smileys))
00124                 {
00125                         if (FALSE === ($smileys = _get_smiley_array()))
00126                         {
00127                                 return $str;
00128                         }
00129                 }
00130 
00131                 // Add a trailing slash to the file path if needed
00132                 $image_url = preg_replace("/(.+?)\/*$/", "\\1/",  $image_url);
00133 
00134                 foreach ($smileys as $key => $val)
00135                 {
00136                         $str = str_replace($key, "<img src=\"".$image_url.$smileys[$key][0]."\" width=\"".$smileys[$key][1]."\" height=\"".$smileys[$key][2]."\" alt=\"".$smileys[$key][3]."\" style=\"border:0;\" />", $str);
00137                 }
00138 
00139                 return $str;
00140         }
00141 }
00142 
00143 // ------------------------------------------------------------------------
00144 
00145 /**
00146  * Get Smiley Array
00147  *
00148  * Fetches the config/smiley.php file
00149  *
00150  * @access      private
00151  * @return      mixed
00152  */
00153 if ( ! function_exists('_get_smiley_array'))
00154 {
00155         function _get_smiley_array()
00156         {
00157                 if ( ! file_exists(APPPATH.'config/smileys'.EXT))
00158                 {
00159                         return FALSE;
00160                 }
00161 
00162                 include(APPPATH.'config/smileys'.EXT);
00163 
00164                 if ( ! isset($smileys) OR ! is_array($smileys))
00165                 {
00166                         return FALSE;
00167                 }
00168 
00169                 return $smileys;
00170         }
00171 }
00172 
00173 
00174 /* End of file smiley_helper.php */
00175 /* Location: ./system/helpers/smiley_helper.php */