blob: b9eb0a23e4e9a5afc54d1726c9d3ac2e6a5765ee [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<?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
Derek Jones7f3719f2010-01-05 13:35:37 +00009 * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @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/**
Pascal Krietede8f4092009-07-29 13:46:37 +000031 * Smiley Javascript
Derek Allard2067d1a2008-11-13 22:59:24 +000032 *
Pascal Krietede8f4092009-07-29 13:46:37 +000033 * Returns the javascript required for the smiley insertion. Optionally takes
34 * an array of aliases to loosely couple the smiley array to the view.
Derek Allard2067d1a2008-11-13 22:59:24 +000035 *
36 * @access public
Pascal Krietede8f4092009-07-29 13:46:37 +000037 * @param mixed alias name or array of alias->field_id pairs
38 * @param string field_id if alias name was passed in
39 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +000040 */
Pascal Krietede8f4092009-07-29 13:46:37 +000041if ( ! function_exists('smiley_js'))
Derek Allard2067d1a2008-11-13 22:59:24 +000042{
Greg Aker60e78c72010-04-20 12:45:14 -050043 function smiley_js($alias = '', $field_id = '', $inline = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +000044 {
Pascal Krietede8f4092009-07-29 13:46:37 +000045 static $do_setup = TRUE;
46
47 $r = '';
48
49 if ($alias != '' && ! is_array($alias))
50 {
51 $alias = array($alias => $field_id);
52 }
53
54 if ($do_setup === TRUE)
55 {
56 $do_setup = FALSE;
57
58 $m = array();
59
60 if (is_array($alias))
61 {
62 foreach($alias as $name => $id)
63 {
64 $m[] = '"'.$name.'" : "'.$id.'"';
65 }
66 }
67
68 $m = '{'.implode(',', $m).'}';
69
70 $r .= <<<EOF
Pascal Krietede8f4092009-07-29 13:46:37 +000071 var smiley_map = {$m};
72
73 function insert_smiley(smiley, field_id) {
74 var el = document.getElementById(field_id), newStart;
75
76 if ( ! el && smiley_map[field_id]) {
77 el = document.getElementById(smiley_map[field_id]);
78
79 if ( ! el)
80 return false;
81 }
82
83 el.focus();
84 smiley = " " + smiley;
85
86 if ('selectionStart' in el) {
87 newStart = el.selectionStart + smiley.length;
88
89 el.value = el.value.substr(0, el.selectionStart) +
90 smiley +
91 el.value.substr(el.selectionEnd, el.value.length);
92 el.setSelectionRange(newStart, newStart);
93 }
94 else if (document.selection) {
Pascal Kriete986e1722009-11-09 20:51:15 +000095 document.selection.createRange().text = smiley;
Pascal Krietede8f4092009-07-29 13:46:37 +000096 }
97 }
Derek Allard2067d1a2008-11-13 22:59:24 +000098EOF;
Pascal Krietede8f4092009-07-29 13:46:37 +000099 }
100 else
101 {
102 if (is_array($alias))
103 {
104 foreach($alias as $name => $id)
105 {
106 $r .= 'smiley_map["'.$name.'"] = "'.$id.'";'."\n";
107 }
108 }
109 }
110
Greg Aker60e78c72010-04-20 12:45:14 -0500111 if ($inline)
112 {
113 return '<script type="text/javascript" charset="utf-8">/*<![CDATA[ */'.$r.'// ]]></script>';
114 }
115 else
116 {
117 return $r;
118 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 }
120}
Pascal Krietede8f4092009-07-29 13:46:37 +0000121
Derek Allard2067d1a2008-11-13 22:59:24 +0000122// ------------------------------------------------------------------------
123
124/**
125 * Get Clickable Smileys
126 *
127 * Returns an array of image tag links that can be clicked to be inserted
128 * into a form field.
129 *
130 * @access public
131 * @param string the URL to the folder containing the smiley images
132 * @return array
133 */
134if ( ! function_exists('get_clickable_smileys'))
135{
Pascal Krietede8f4092009-07-29 13:46:37 +0000136 function get_clickable_smileys($image_url, $alias = '', $smileys = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 {
Pascal Krietede8f4092009-07-29 13:46:37 +0000138 // For backward compatibility with js_insert_smiley
139
140 if (is_array($alias))
141 {
142 $smileys = $alias;
143 }
144
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 if ( ! is_array($smileys))
146 {
147 if (FALSE === ($smileys = _get_smiley_array()))
148 {
149 return $smileys;
150 }
151 }
152
153 // Add a trailing slash to the file path if needed
Pascal Krietede8f4092009-07-29 13:46:37 +0000154 $image_url = rtrim($image_url, '/').'/';
155
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 $used = array();
157 foreach ($smileys as $key => $val)
158 {
159 // Keep duplicates from being used, which can happen if the
160 // mapping array contains multiple identical replacements. For example:
161 // :-) and :) might be replaced with the same image so both smileys
162 // will be in the array.
163 if (isset($used[$smileys[$key][0]]))
164 {
165 continue;
166 }
Pascal Krietede8f4092009-07-29 13:46:37 +0000167
Derek Jones48822fa2010-03-05 10:23:39 -0600168 $link[] = "<a href=\"javascript:void(0);\" onclick=\"insert_smiley('".$key."', '".$alias."')\"><img src=\"".$image_url.$smileys[$key][0]."\" width=\"".$smileys[$key][1]."\" height=\"".$smileys[$key][2]."\" alt=\"".$smileys[$key][3]."\" style=\"border:0;\" /></a>";
Pascal Krietede8f4092009-07-29 13:46:37 +0000169
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 $used[$smileys[$key][0]] = TRUE;
171 }
Pascal Krietede8f4092009-07-29 13:46:37 +0000172
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 return $link;
174 }
175}
176
177// ------------------------------------------------------------------------
178
179/**
180 * Parse Smileys
181 *
182 * Takes a string as input and swaps any contained smileys for the actual image
183 *
184 * @access public
185 * @param string the text to be parsed
186 * @param string the URL to the folder containing the smiley images
187 * @return string
188 */
189if ( ! function_exists('parse_smileys'))
190{
191 function parse_smileys($str = '', $image_url = '', $smileys = NULL)
192 {
193 if ($image_url == '')
194 {
195 return $str;
196 }
197
198 if ( ! is_array($smileys))
199 {
200 if (FALSE === ($smileys = _get_smiley_array()))
201 {
202 return $str;
203 }
204 }
205
206 // Add a trailing slash to the file path if needed
207 $image_url = preg_replace("/(.+?)\/*$/", "\\1/", $image_url);
208
209 foreach ($smileys as $key => $val)
210 {
211 $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);
212 }
213
214 return $str;
215 }
216}
217
218// ------------------------------------------------------------------------
219
220/**
221 * Get Smiley Array
222 *
223 * Fetches the config/smiley.php file
224 *
225 * @access private
226 * @return mixed
227 */
228if ( ! function_exists('_get_smiley_array'))
229{
230 function _get_smiley_array()
231 {
232 if ( ! file_exists(APPPATH.'config/smileys'.EXT))
233 {
234 return FALSE;
235 }
236
237 include(APPPATH.'config/smileys'.EXT);
238
239 if ( ! isset($smileys) OR ! is_array($smileys))
240 {
241 return FALSE;
242 }
243
244 return $smileys;
245 }
246}
247
Pascal Krietede8f4092009-07-29 13:46:37 +0000248// ------------------------------------------------------------------------
249
250/**
251 * JS Insert Smiley
252 *
253 * Generates the javascript function needed to insert smileys into a form field
254 *
255 * DEPRECATED as of version 1.7.2, use smiley_js instead
256 *
257 * @access public
258 * @param string form name
259 * @param string field name
260 * @return string
261 */
262if ( ! function_exists('js_insert_smiley'))
263{
264 function js_insert_smiley($form_name = '', $form_field = '')
265 {
266 return <<<EOF
267<script type="text/javascript">
268 function insert_smiley(smiley)
269 {
270 document.{$form_name}.{$form_field}.value += " " + smiley;
271 }
272</script>
273EOF;
274 }
275}
276
Derek Allard2067d1a2008-11-13 22:59:24 +0000277
278/* End of file smiley_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000279/* Location: ./system/helpers/smiley_helper.php */