blob: 775cc1b99b5ba664672423e61e68e990872cc495 [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{
Pascal Krietede8f4092009-07-29 13:46:37 +000043 function smiley_js($alias = '', $field_id = '')
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
Derek Jones48822fa2010-03-05 10:23:39 -0600111 return '<script type="text/javascript" charset="utf-8">/*<![CDATA[ */'.$r.'// ]]></script>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 }
113}
Pascal Krietede8f4092009-07-29 13:46:37 +0000114
Derek Allard2067d1a2008-11-13 22:59:24 +0000115// ------------------------------------------------------------------------
116
117/**
118 * Get Clickable Smileys
119 *
120 * Returns an array of image tag links that can be clicked to be inserted
121 * into a form field.
122 *
123 * @access public
124 * @param string the URL to the folder containing the smiley images
125 * @return array
126 */
127if ( ! function_exists('get_clickable_smileys'))
128{
Pascal Krietede8f4092009-07-29 13:46:37 +0000129 function get_clickable_smileys($image_url, $alias = '', $smileys = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 {
Pascal Krietede8f4092009-07-29 13:46:37 +0000131 // For backward compatibility with js_insert_smiley
132
133 if (is_array($alias))
134 {
135 $smileys = $alias;
136 }
137
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 if ( ! is_array($smileys))
139 {
140 if (FALSE === ($smileys = _get_smiley_array()))
141 {
142 return $smileys;
143 }
144 }
145
146 // Add a trailing slash to the file path if needed
Pascal Krietede8f4092009-07-29 13:46:37 +0000147 $image_url = rtrim($image_url, '/').'/';
148
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 $used = array();
150 foreach ($smileys as $key => $val)
151 {
152 // Keep duplicates from being used, which can happen if the
153 // mapping array contains multiple identical replacements. For example:
154 // :-) and :) might be replaced with the same image so both smileys
155 // will be in the array.
156 if (isset($used[$smileys[$key][0]]))
157 {
158 continue;
159 }
Pascal Krietede8f4092009-07-29 13:46:37 +0000160
Derek Jones48822fa2010-03-05 10:23:39 -0600161 $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 +0000162
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 $used[$smileys[$key][0]] = TRUE;
164 }
Pascal Krietede8f4092009-07-29 13:46:37 +0000165
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 return $link;
167 }
168}
169
170// ------------------------------------------------------------------------
171
172/**
173 * Parse Smileys
174 *
175 * Takes a string as input and swaps any contained smileys for the actual image
176 *
177 * @access public
178 * @param string the text to be parsed
179 * @param string the URL to the folder containing the smiley images
180 * @return string
181 */
182if ( ! function_exists('parse_smileys'))
183{
184 function parse_smileys($str = '', $image_url = '', $smileys = NULL)
185 {
186 if ($image_url == '')
187 {
188 return $str;
189 }
190
191 if ( ! is_array($smileys))
192 {
193 if (FALSE === ($smileys = _get_smiley_array()))
194 {
195 return $str;
196 }
197 }
198
199 // Add a trailing slash to the file path if needed
200 $image_url = preg_replace("/(.+?)\/*$/", "\\1/", $image_url);
201
202 foreach ($smileys as $key => $val)
203 {
204 $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);
205 }
206
207 return $str;
208 }
209}
210
211// ------------------------------------------------------------------------
212
213/**
214 * Get Smiley Array
215 *
216 * Fetches the config/smiley.php file
217 *
218 * @access private
219 * @return mixed
220 */
221if ( ! function_exists('_get_smiley_array'))
222{
223 function _get_smiley_array()
224 {
225 if ( ! file_exists(APPPATH.'config/smileys'.EXT))
226 {
227 return FALSE;
228 }
229
230 include(APPPATH.'config/smileys'.EXT);
231
232 if ( ! isset($smileys) OR ! is_array($smileys))
233 {
234 return FALSE;
235 }
236
237 return $smileys;
238 }
239}
240
Pascal Krietede8f4092009-07-29 13:46:37 +0000241// ------------------------------------------------------------------------
242
243/**
244 * JS Insert Smiley
245 *
246 * Generates the javascript function needed to insert smileys into a form field
247 *
248 * DEPRECATED as of version 1.7.2, use smiley_js instead
249 *
250 * @access public
251 * @param string form name
252 * @param string field name
253 * @return string
254 */
255if ( ! function_exists('js_insert_smiley'))
256{
257 function js_insert_smiley($form_name = '', $form_field = '')
258 {
259 return <<<EOF
260<script type="text/javascript">
261 function insert_smiley(smiley)
262 {
263 document.{$form_name}.{$form_field}.value += " " + smiley;
264 }
265</script>
266EOF;
267 }
268}
269
Derek Allard2067d1a2008-11-13 22:59:24 +0000270
271/* End of file smiley_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000272/* Location: ./system/helpers/smiley_helper.php */