blob: 38e2965fc9c175db651265e65a867f402e3bf960 [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * CodeIgniter Smiley Helpers
32 *
33 * @package CodeIgniter
34 * @subpackage Helpers
35 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/helpers/smiley_helper.html
38 */
39
40// ------------------------------------------------------------------------
41
42/**
Pascal Krietede8f4092009-07-29 13:46:37 +000043 * Smiley Javascript
Derek Allard2067d1a2008-11-13 22:59:24 +000044 *
Derek Jones37f4b9c2011-07-01 17:56:50 -050045 * Returns the javascript required for the smiley insertion. Optionally takes
Pascal Krietede8f4092009-07-29 13:46:37 +000046 * an array of aliases to loosely couple the smiley array to the view.
Derek Allard2067d1a2008-11-13 22:59:24 +000047 *
48 * @access public
Pascal Krietede8f4092009-07-29 13:46:37 +000049 * @param mixed alias name or array of alias->field_id pairs
50 * @param string field_id if alias name was passed in
51 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +000052 */
Pascal Krietede8f4092009-07-29 13:46:37 +000053if ( ! function_exists('smiley_js'))
Derek Allard2067d1a2008-11-13 22:59:24 +000054{
Greg Aker60e78c72010-04-20 12:45:14 -050055 function smiley_js($alias = '', $field_id = '', $inline = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +000056 {
Pascal Krietede8f4092009-07-29 13:46:37 +000057 static $do_setup = TRUE;
58
59 $r = '';
Barry Mienydd671972010-10-04 16:33:58 +020060
Pascal Krietede8f4092009-07-29 13:46:37 +000061 if ($alias != '' && ! is_array($alias))
62 {
63 $alias = array($alias => $field_id);
64 }
65
66 if ($do_setup === TRUE)
67 {
68 $do_setup = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +020069
Pascal Krietede8f4092009-07-29 13:46:37 +000070 $m = array();
Barry Mienydd671972010-10-04 16:33:58 +020071
Pascal Krietede8f4092009-07-29 13:46:37 +000072 if (is_array($alias))
73 {
Pascal Kriete45e3cdf2011-02-14 13:26:20 -050074 foreach ($alias as $name => $id)
Pascal Krietede8f4092009-07-29 13:46:37 +000075 {
76 $m[] = '"'.$name.'" : "'.$id.'"';
77 }
78 }
Barry Mienydd671972010-10-04 16:33:58 +020079
Pascal Krietede8f4092009-07-29 13:46:37 +000080 $m = '{'.implode(',', $m).'}';
Barry Mienydd671972010-10-04 16:33:58 +020081
Pascal Krietede8f4092009-07-29 13:46:37 +000082 $r .= <<<EOF
Pascal Krietede8f4092009-07-29 13:46:37 +000083 var smiley_map = {$m};
84
85 function insert_smiley(smiley, field_id) {
86 var el = document.getElementById(field_id), newStart;
Barry Mienydd671972010-10-04 16:33:58 +020087
Pascal Krietede8f4092009-07-29 13:46:37 +000088 if ( ! el && smiley_map[field_id]) {
89 el = document.getElementById(smiley_map[field_id]);
Barry Mienydd671972010-10-04 16:33:58 +020090
Pascal Krietede8f4092009-07-29 13:46:37 +000091 if ( ! el)
92 return false;
93 }
Barry Mienydd671972010-10-04 16:33:58 +020094
Pascal Krietede8f4092009-07-29 13:46:37 +000095 el.focus();
96 smiley = " " + smiley;
97
98 if ('selectionStart' in el) {
99 newStart = el.selectionStart + smiley.length;
100
101 el.value = el.value.substr(0, el.selectionStart) +
102 smiley +
103 el.value.substr(el.selectionEnd, el.value.length);
104 el.setSelectionRange(newStart, newStart);
105 }
106 else if (document.selection) {
Pascal Kriete986e1722009-11-09 20:51:15 +0000107 document.selection.createRange().text = smiley;
Pascal Krietede8f4092009-07-29 13:46:37 +0000108 }
109 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000110EOF;
Pascal Krietede8f4092009-07-29 13:46:37 +0000111 }
112 else
113 {
114 if (is_array($alias))
115 {
Pascal Kriete45e3cdf2011-02-14 13:26:20 -0500116 foreach ($alias as $name => $id)
Pascal Krietede8f4092009-07-29 13:46:37 +0000117 {
118 $r .= 'smiley_map["'.$name.'"] = "'.$id.'";'."\n";
119 }
120 }
121 }
122
Greg Aker60e78c72010-04-20 12:45:14 -0500123 if ($inline)
124 {
Barry Mienydd671972010-10-04 16:33:58 +0200125 return '<script type="text/javascript" charset="utf-8">/*<![CDATA[ */'.$r.'// ]]></script>';
Greg Aker60e78c72010-04-20 12:45:14 -0500126 }
127 else
128 {
129 return $r;
130 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 }
132}
Pascal Krietede8f4092009-07-29 13:46:37 +0000133
Derek Allard2067d1a2008-11-13 22:59:24 +0000134// ------------------------------------------------------------------------
135
136/**
137 * Get Clickable Smileys
138 *
Barry Mienydd671972010-10-04 16:33:58 +0200139 * Returns an array of image tag links that can be clicked to be inserted
140 * into a form field.
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 *
142 * @access public
143 * @param string the URL to the folder containing the smiley images
144 * @return array
145 */
146if ( ! function_exists('get_clickable_smileys'))
147{
Pascal Krietede8f4092009-07-29 13:46:37 +0000148 function get_clickable_smileys($image_url, $alias = '', $smileys = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 {
Pascal Krietede8f4092009-07-29 13:46:37 +0000150 // For backward compatibility with js_insert_smiley
Barry Mienydd671972010-10-04 16:33:58 +0200151
Pascal Krietede8f4092009-07-29 13:46:37 +0000152 if (is_array($alias))
153 {
154 $smileys = $alias;
155 }
Barry Mienydd671972010-10-04 16:33:58 +0200156
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 if ( ! is_array($smileys))
158 {
159 if (FALSE === ($smileys = _get_smiley_array()))
160 {
161 return $smileys;
162 }
163 }
164
165 // Add a trailing slash to the file path if needed
Pascal Krietede8f4092009-07-29 13:46:37 +0000166 $image_url = rtrim($image_url, '/').'/';
Barry Mienydd671972010-10-04 16:33:58 +0200167
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 $used = array();
169 foreach ($smileys as $key => $val)
170 {
171 // Keep duplicates from being used, which can happen if the
Derek Jones37f4b9c2011-07-01 17:56:50 -0500172 // mapping array contains multiple identical replacements. For example:
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 // :-) and :) might be replaced with the same image so both smileys
174 // will be in the array.
175 if (isset($used[$smileys[$key][0]]))
176 {
177 continue;
178 }
Barry Mienydd671972010-10-04 16:33:58 +0200179
180 $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>";
181
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 $used[$smileys[$key][0]] = TRUE;
183 }
Barry Mienydd671972010-10-04 16:33:58 +0200184
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 return $link;
186 }
187}
188
189// ------------------------------------------------------------------------
190
191/**
192 * Parse Smileys
193 *
194 * Takes a string as input and swaps any contained smileys for the actual image
195 *
196 * @access public
197 * @param string the text to be parsed
198 * @param string the URL to the folder containing the smiley images
199 * @return string
200 */
201if ( ! function_exists('parse_smileys'))
202{
203 function parse_smileys($str = '', $image_url = '', $smileys = NULL)
204 {
205 if ($image_url == '')
206 {
207 return $str;
208 }
209
210 if ( ! is_array($smileys))
211 {
212 if (FALSE === ($smileys = _get_smiley_array()))
213 {
214 return $str;
215 }
216 }
217
218 // Add a trailing slash to the file path if needed
Derek Jones37f4b9c2011-07-01 17:56:50 -0500219 $image_url = preg_replace("/(.+?)\/*$/", "\\1/", $image_url);
Derek Allard2067d1a2008-11-13 22:59:24 +0000220
221 foreach ($smileys as $key => $val)
222 {
223 $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);
224 }
225
226 return $str;
227 }
228}
229
230// ------------------------------------------------------------------------
231
232/**
233 * Get Smiley Array
234 *
235 * Fetches the config/smiley.php file
236 *
237 * @access private
238 * @return mixed
239 */
240if ( ! function_exists('_get_smiley_array'))
241{
242 function _get_smiley_array()
243 {
Greg Akerd96f8822011-12-27 16:23:47 -0600244 if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/smileys.php'))
245 {
246 include(APPPATH.'config/'.ENVIRONMENT.'/smileys.php');
247 }
248 elseif (file_exists(APPPATH.'config/smileys.php'))
249 {
250 include(APPPATH.'config/smileys.php');
251 }
Derek Jones37f4b9c2011-07-01 17:56:50 -0500252
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100253 if (isset($smileys) AND is_array($smileys))
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 {
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100255 return $smileys;
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 }
257
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100258 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 }
260}
261
Pascal Krietede8f4092009-07-29 13:46:37 +0000262// ------------------------------------------------------------------------
263
264/**
265 * JS Insert Smiley
266 *
267 * Generates the javascript function needed to insert smileys into a form field
268 *
269 * DEPRECATED as of version 1.7.2, use smiley_js instead
270 *
271 * @access public
272 * @param string form name
273 * @param string field name
274 * @return string
275 */
276if ( ! function_exists('js_insert_smiley'))
277{
278 function js_insert_smiley($form_name = '', $form_field = '')
279 {
280 return <<<EOF
281<script type="text/javascript">
282 function insert_smiley(smiley)
283 {
284 document.{$form_name}.{$form_field}.value += " " + smiley;
285 }
286</script>
287EOF;
288 }
289}
290
Derek Allard2067d1a2008-11-13 22:59:24 +0000291
292/* End of file smiley_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000293/* Location: ./system/helpers/smiley_helper.php */