blob: 8dba74e7332b5b320438bdf65165e6ff1247b5b0 [file] [log] [blame]
Andrey Andreeve684bda2012-03-26 13:47:29 +03001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreeve684bda2012-03-26 13:47:29 +03008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreeve684bda2012-03-26 13:47:29 +030010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @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
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * CodeIgniter Smiley Helpers
30 *
31 * @package CodeIgniter
32 * @subpackage Helpers
33 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000035 * @link http://codeigniter.com/user_guide/helpers/smiley_helper.html
36 */
37
38// ------------------------------------------------------------------------
39
40/**
Pascal Krietede8f4092009-07-29 13:46:37 +000041 * Smiley Javascript
Derek Allard2067d1a2008-11-13 22:59:24 +000042 *
Derek Jones37f4b9c2011-07-01 17:56:50 -050043 * Returns the javascript required for the smiley insertion. Optionally takes
Pascal Krietede8f4092009-07-29 13:46:37 +000044 * an array of aliases to loosely couple the smiley array to the view.
Derek Allard2067d1a2008-11-13 22:59:24 +000045 *
Pascal Krietede8f4092009-07-29 13:46:37 +000046 * @param mixed alias name or array of alias->field_id pairs
47 * @param string field_id if alias name was passed in
48 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +000049 */
Pascal Krietede8f4092009-07-29 13:46:37 +000050if ( ! function_exists('smiley_js'))
Derek Allard2067d1a2008-11-13 22:59:24 +000051{
Greg Aker60e78c72010-04-20 12:45:14 -050052 function smiley_js($alias = '', $field_id = '', $inline = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +000053 {
Pascal Krietede8f4092009-07-29 13:46:37 +000054 static $do_setup = TRUE;
55
56 $r = '';
Barry Mienydd671972010-10-04 16:33:58 +020057
Pascal Krietede8f4092009-07-29 13:46:37 +000058 if ($alias != '' && ! is_array($alias))
59 {
60 $alias = array($alias => $field_id);
61 }
62
63 if ($do_setup === TRUE)
64 {
65 $do_setup = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +020066
Pascal Krietede8f4092009-07-29 13:46:37 +000067 $m = array();
Barry Mienydd671972010-10-04 16:33:58 +020068
Pascal Krietede8f4092009-07-29 13:46:37 +000069 if (is_array($alias))
70 {
Pascal Kriete45e3cdf2011-02-14 13:26:20 -050071 foreach ($alias as $name => $id)
Pascal Krietede8f4092009-07-29 13:46:37 +000072 {
73 $m[] = '"'.$name.'" : "'.$id.'"';
74 }
75 }
Barry Mienydd671972010-10-04 16:33:58 +020076
Pascal Krietede8f4092009-07-29 13:46:37 +000077 $m = '{'.implode(',', $m).'}';
Barry Mienydd671972010-10-04 16:33:58 +020078
Pascal Krietede8f4092009-07-29 13:46:37 +000079 $r .= <<<EOF
Pascal Krietede8f4092009-07-29 13:46:37 +000080 var smiley_map = {$m};
81
82 function insert_smiley(smiley, field_id) {
83 var el = document.getElementById(field_id), newStart;
Barry Mienydd671972010-10-04 16:33:58 +020084
Pascal Krietede8f4092009-07-29 13:46:37 +000085 if ( ! el && smiley_map[field_id]) {
86 el = document.getElementById(smiley_map[field_id]);
Barry Mienydd671972010-10-04 16:33:58 +020087
Pascal Krietede8f4092009-07-29 13:46:37 +000088 if ( ! el)
89 return false;
90 }
Barry Mienydd671972010-10-04 16:33:58 +020091
Pascal Krietede8f4092009-07-29 13:46:37 +000092 el.focus();
93 smiley = " " + smiley;
94
95 if ('selectionStart' in el) {
96 newStart = el.selectionStart + smiley.length;
97
98 el.value = el.value.substr(0, el.selectionStart) +
99 smiley +
100 el.value.substr(el.selectionEnd, el.value.length);
101 el.setSelectionRange(newStart, newStart);
102 }
103 else if (document.selection) {
Pascal Kriete986e1722009-11-09 20:51:15 +0000104 document.selection.createRange().text = smiley;
Pascal Krietede8f4092009-07-29 13:46:37 +0000105 }
106 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000107EOF;
Pascal Krietede8f4092009-07-29 13:46:37 +0000108 }
109 else
110 {
111 if (is_array($alias))
112 {
Pascal Kriete45e3cdf2011-02-14 13:26:20 -0500113 foreach ($alias as $name => $id)
Pascal Krietede8f4092009-07-29 13:46:37 +0000114 {
115 $r .= 'smiley_map["'.$name.'"] = "'.$id.'";'."\n";
116 }
117 }
118 }
119
Greg Aker60e78c72010-04-20 12:45:14 -0500120 if ($inline)
121 {
Barry Mienydd671972010-10-04 16:33:58 +0200122 return '<script type="text/javascript" charset="utf-8">/*<![CDATA[ */'.$r.'// ]]></script>';
Greg Aker60e78c72010-04-20 12:45:14 -0500123 }
124 else
125 {
126 return $r;
127 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 }
129}
Pascal Krietede8f4092009-07-29 13:46:37 +0000130
Derek Allard2067d1a2008-11-13 22:59:24 +0000131// ------------------------------------------------------------------------
132
133/**
134 * Get Clickable Smileys
135 *
Barry Mienydd671972010-10-04 16:33:58 +0200136 * Returns an array of image tag links that can be clicked to be inserted
137 * into a form field.
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 * @param string the URL to the folder containing the smiley images
140 * @return array
141 */
142if ( ! function_exists('get_clickable_smileys'))
143{
Pascal Krietede8f4092009-07-29 13:46:37 +0000144 function get_clickable_smileys($image_url, $alias = '', $smileys = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 {
Pascal Krietede8f4092009-07-29 13:46:37 +0000146 // For backward compatibility with js_insert_smiley
Barry Mienydd671972010-10-04 16:33:58 +0200147
Pascal Krietede8f4092009-07-29 13:46:37 +0000148 if (is_array($alias))
149 {
150 $smileys = $alias;
151 }
Barry Mienydd671972010-10-04 16:33:58 +0200152
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 if ( ! is_array($smileys))
154 {
155 if (FALSE === ($smileys = _get_smiley_array()))
156 {
157 return $smileys;
158 }
159 }
160
161 // Add a trailing slash to the file path if needed
Pascal Krietede8f4092009-07-29 13:46:37 +0000162 $image_url = rtrim($image_url, '/').'/';
Barry Mienydd671972010-10-04 16:33:58 +0200163
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 $used = array();
165 foreach ($smileys as $key => $val)
166 {
167 // Keep duplicates from being used, which can happen if the
Derek Jones37f4b9c2011-07-01 17:56:50 -0500168 // mapping array contains multiple identical replacements. For example:
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 // :-) and :) might be replaced with the same image so both smileys
170 // will be in the array.
171 if (isset($used[$smileys[$key][0]]))
172 {
173 continue;
174 }
Barry Mienydd671972010-10-04 16:33:58 +0200175
176 $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>";
177
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 $used[$smileys[$key][0]] = TRUE;
179 }
Barry Mienydd671972010-10-04 16:33:58 +0200180
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 return $link;
182 }
183}
184
185// ------------------------------------------------------------------------
186
187/**
188 * Parse Smileys
189 *
190 * Takes a string as input and swaps any contained smileys for the actual image
191 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 * @param string the text to be parsed
193 * @param string the URL to the folder containing the smiley images
194 * @return string
195 */
196if ( ! function_exists('parse_smileys'))
197{
198 function parse_smileys($str = '', $image_url = '', $smileys = NULL)
199 {
200 if ($image_url == '')
201 {
202 return $str;
203 }
204
205 if ( ! is_array($smileys))
206 {
207 if (FALSE === ($smileys = _get_smiley_array()))
208 {
209 return $str;
210 }
211 }
212
213 // Add a trailing slash to the file path if needed
Derek Jones37f4b9c2011-07-01 17:56:50 -0500214 $image_url = preg_replace("/(.+?)\/*$/", "\\1/", $image_url);
Derek Allard2067d1a2008-11-13 22:59:24 +0000215
216 foreach ($smileys as $key => $val)
217 {
218 $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);
219 }
220
221 return $str;
222 }
223}
224
225// ------------------------------------------------------------------------
226
227/**
228 * Get Smiley Array
229 *
230 * Fetches the config/smiley.php file
231 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 * @return mixed
233 */
234if ( ! function_exists('_get_smiley_array'))
235{
236 function _get_smiley_array()
237 {
Andrey Andreeve684bda2012-03-26 13:47:29 +0300238 if (defined('ENVIRONMENT') && file_exists(APPPATH.'config/'.ENVIRONMENT.'/smileys.php'))
Greg Akerd96f8822011-12-27 16:23:47 -0600239 {
240 include(APPPATH.'config/'.ENVIRONMENT.'/smileys.php');
241 }
242 elseif (file_exists(APPPATH.'config/smileys.php'))
243 {
244 include(APPPATH.'config/smileys.php');
245 }
Andrey Andreeve684bda2012-03-26 13:47:29 +0300246
247 if (isset($smileys) && is_array($smileys))
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 {
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100249 return $smileys;
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 }
251
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100252 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 }
254}
255
Pascal Krietede8f4092009-07-29 13:46:37 +0000256// ------------------------------------------------------------------------
257
258/**
259 * JS Insert Smiley
260 *
261 * Generates the javascript function needed to insert smileys into a form field
262 *
263 * DEPRECATED as of version 1.7.2, use smiley_js instead
264 *
Pascal Krietede8f4092009-07-29 13:46:37 +0000265 * @param string form name
266 * @param string field name
267 * @return string
268 */
269if ( ! function_exists('js_insert_smiley'))
270{
271 function js_insert_smiley($form_name = '', $form_field = '')
272 {
273 return <<<EOF
274<script type="text/javascript">
275 function insert_smiley(smiley)
276 {
277 document.{$form_name}.{$form_field}.value += " " + smiley;
278 }
279</script>
280EOF;
281 }
282}
283
Derek Allard2067d1a2008-11-13 22:59:24 +0000284/* End of file smiley_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000285/* Location: ./system/helpers/smiley_helper.php */