converted to unix-style eol
[www-register-wizard.git] / helpers / smiley_helper.php
1 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2 /**
3  * CodeIgniter
4  *
5  * An open source application development framework for PHP 4.3.2 or newer
6  *
7  * @package             CodeIgniter
8  * @author              ExpressionEngine Dev Team
9  * @copyright   Copyright (c) 2008, EllisLab, Inc.
10  * @license             http://codeigniter.com/user_guide/license.html
11  * @link                http://codeigniter.com
12  * @since               Version 1.0
13  * @filesource
14  */
15
16 // ------------------------------------------------------------------------
17
18 /**
19  * CodeIgniter Smiley Helpers
20  *
21  * @package             CodeIgniter
22  * @subpackage  Helpers
23  * @category    Helpers
24  * @author              ExpressionEngine Dev Team
25  * @link                http://codeigniter.com/user_guide/helpers/smiley_helper.html
26  */
27
28 // ------------------------------------------------------------------------
29
30 /**
31  * JS Insert Smiley
32  *
33  * Generates the javascrip function needed to insert smileys into a form field
34  *
35  * @access      public
36  * @param       string  form name
37  * @param       string  field name
38  * @return      string
39  */
40 if ( ! function_exists('js_insert_smiley'))
41 {
42         function js_insert_smiley($form_name = '', $form_field = '')
43         {
44                 return <<<EOF
45 <script type="text/javascript">
46         function insert_smiley(smiley)
47         {
48                 document.{$form_name}.{$form_field}.value += " " + smiley;
49         }
50 </script>
51 EOF;
52         }
53 }
54 // ------------------------------------------------------------------------
55
56 /**
57  * Get Clickable Smileys
58  *
59  * Returns an array of image tag links that can be clicked to be inserted 
60  * into a form field.  
61  *
62  * @access      public
63  * @param       string  the URL to the folder containing the smiley images
64  * @return      array
65  */
66 if ( ! function_exists('get_clickable_smileys'))
67 {
68         function get_clickable_smileys($image_url = '', $smileys = NULL)
69         {
70                 if ( ! is_array($smileys))
71                 {
72                         if (FALSE === ($smileys = _get_smiley_array()))
73                         {
74                                 return $smileys;
75                         }
76                 }
77
78                 // Add a trailing slash to the file path if needed
79                 $image_url = preg_replace("/(.+?)\/*$/", "\\1/",  $image_url);
80
81                 $used = array();
82                 foreach ($smileys as $key => $val)
83                 {
84                         // Keep duplicates from being used, which can happen if the
85                         // mapping array contains multiple identical replacements.  For example:
86                         // :-) and :) might be replaced with the same image so both smileys
87                         // will be in the array.
88                         if (isset($used[$smileys[$key][0]]))
89                         {
90                                 continue;
91                         }
92
93                         $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>";
94
95                         $used[$smileys[$key][0]] = TRUE;
96                 }
97
98                 return $link;
99         }
100 }
101
102 // ------------------------------------------------------------------------
103
104 /**
105  * Parse Smileys
106  *
107  * Takes a string as input and swaps any contained smileys for the actual image
108  *
109  * @access      public
110  * @param       string  the text to be parsed
111  * @param       string  the URL to the folder containing the smiley images
112  * @return      string
113  */
114 if ( ! function_exists('parse_smileys'))
115 {
116         function parse_smileys($str = '', $image_url = '', $smileys = NULL)
117         {
118                 if ($image_url == '')
119                 {
120                         return $str;
121                 }
122
123                 if ( ! is_array($smileys))
124                 {
125                         if (FALSE === ($smileys = _get_smiley_array()))
126                         {
127                                 return $str;
128                         }
129                 }
130
131                 // Add a trailing slash to the file path if needed
132                 $image_url = preg_replace("/(.+?)\/*$/", "\\1/",  $image_url);
133
134                 foreach ($smileys as $key => $val)
135                 {
136                         $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);
137                 }
138
139                 return $str;
140         }
141 }
142
143 // ------------------------------------------------------------------------
144
145 /**
146  * Get Smiley Array
147  *
148  * Fetches the config/smiley.php file
149  *
150  * @access      private
151  * @return      mixed
152  */
153 if ( ! function_exists('_get_smiley_array'))
154 {
155         function _get_smiley_array()
156         {
157                 if ( ! file_exists(APPPATH.'config/smileys'.EXT))
158                 {
159                         return FALSE;
160                 }
161
162                 include(APPPATH.'config/smileys'.EXT);
163
164                 if ( ! isset($smileys) OR ! is_array($smileys))
165                 {
166                         return FALSE;
167                 }
168
169                 return $smileys;
170         }
171 }
172
173
174 /* End of file smiley_helper.php */
175 /* Location: ./system/helpers/smiley_helper.php */