blob: cb902114e8be3151b6d21c0dee184734affdd761 [file] [log] [blame]
Andrey Andreev4921fed2012-01-07 01:28:07 +02001<?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
Andrey Andreev4921fed2012-01-07 01:28:07 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev4921fed2012-01-07 01:28:07 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
Andrey Andreevc5a1f932012-01-24 15:29:02 +020012 * bundled with this package in the files license.txt / license.rst. It is
Derek Jonesf4a4bd82011-10-20 12:18:42 -050013 * 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 *
Andrey Andreev09375d72012-01-19 14:57:46 +020043 * 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 *
46 * @access public
Pascal Krietede8f4092009-07-29 13:46:37 +000047 * @param mixed alias name or array of alias->field_id pairs
48 * @param string field_id if alias name was passed in
49 * @return array
Derek Allard2067d1a2008-11-13 22:59:24 +000050 */
Pascal Krietede8f4092009-07-29 13:46:37 +000051if ( ! function_exists('smiley_js'))
Derek Allard2067d1a2008-11-13 22:59:24 +000052{
Greg Aker60e78c72010-04-20 12:45:14 -050053 function smiley_js($alias = '', $field_id = '', $inline = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +000054 {
Pascal Krietede8f4092009-07-29 13:46:37 +000055 static $do_setup = TRUE;
Pascal Krietede8f4092009-07-29 13:46:37 +000056 $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 {
Andrey Andreev4921fed2012-01-07 01:28:07 +020065 $do_setup = FALSE;
66 $m = array();
Barry Mienydd671972010-10-04 16:33:58 +020067
Andrey Andreev4921fed2012-01-07 01:28:07 +020068 if (is_array($alias))
69 {
70 foreach ($alias as $name => $id)
Pascal Krietede8f4092009-07-29 13:46:37 +000071 {
Andrey Andreev4921fed2012-01-07 01:28:07 +020072 $m[] = '"'.$name.'" : "'.$id.'"';
73 }
74 }
75
76 $m = '{'.implode(',', $m).'}';
77
78 $r .= <<<EOF
79 var smiley_map = {$m};
80
81 function insert_smiley(smiley, field_id) {
82 var el = document.getElementById(field_id), newStart;
83
84 if ( ! el && smiley_map[field_id]) {
85 el = document.getElementById(smiley_map[field_id]);
86
87 if ( ! el)
88 return false;
Pascal Krietede8f4092009-07-29 13:46:37 +000089 }
Barry Mienydd671972010-10-04 16:33:58 +020090
Andrey Andreev4921fed2012-01-07 01:28:07 +020091 el.focus();
92 smiley = " " + smiley;
Barry Mienydd671972010-10-04 16:33:58 +020093
Andrey Andreev4921fed2012-01-07 01:28:07 +020094 if ('selectionStart' in el) {
95 newStart = el.selectionStart + smiley.length;
Pascal Krietede8f4092009-07-29 13:46:37 +000096
Andrey Andreev4921fed2012-01-07 01:28:07 +020097 el.value = el.value.substr(0, el.selectionStart) +
98 smiley +
99 el.value.substr(el.selectionEnd, el.value.length);
100 el.setSelectionRange(newStart, newStart);
Pascal Krietede8f4092009-07-29 13:46:37 +0000101 }
Andrey Andreev4921fed2012-01-07 01:28:07 +0200102 else if (document.selection) {
103 document.selection.createRange().text = smiley;
104 }
105 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000106EOF;
Pascal Krietede8f4092009-07-29 13:46:37 +0000107 }
Andrey Andreev09375d72012-01-19 14:57:46 +0200108 elseif (is_array($alias))
Pascal Krietede8f4092009-07-29 13:46:37 +0000109 {
Andrey Andreev09375d72012-01-19 14:57:46 +0200110 foreach ($alias as $name => $id)
Pascal Krietede8f4092009-07-29 13:46:37 +0000111 {
Andrey Andreev09375d72012-01-19 14:57:46 +0200112 $r .= 'smiley_map["'.$name.'"] = "'.$id."\";\n";
Pascal Krietede8f4092009-07-29 13:46:37 +0000113 }
114 }
115
Andrey Andreev4921fed2012-01-07 01:28:07 +0200116 return ($inline) ? '<script type="text/javascript" charset="utf-8">/*<![CDATA[ */'.$r.'// ]]></script>' : $r;
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 }
118}
Pascal Krietede8f4092009-07-29 13:46:37 +0000119
Derek Allard2067d1a2008-11-13 22:59:24 +0000120// ------------------------------------------------------------------------
121
122/**
123 * Get Clickable Smileys
124 *
Barry Mienydd671972010-10-04 16:33:58 +0200125 * Returns an array of image tag links that can be clicked to be inserted
126 * into a form field.
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 *
128 * @access public
129 * @param string the URL to the folder containing the smiley images
130 * @return array
131 */
132if ( ! function_exists('get_clickable_smileys'))
133{
Pascal Krietede8f4092009-07-29 13:46:37 +0000134 function get_clickable_smileys($image_url, $alias = '', $smileys = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 {
Pascal Krietede8f4092009-07-29 13:46:37 +0000136 // For backward compatibility with js_insert_smiley
Pascal Krietede8f4092009-07-29 13:46:37 +0000137 if (is_array($alias))
138 {
139 $smileys = $alias;
140 }
Andrey Andreev09375d72012-01-19 14:57:46 +0200141 elseif (FALSE === ($smileys = _get_smiley_array()))
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200143 return $smileys;
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 }
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, '/').'/';
Barry Mienydd671972010-10-04 16:33:58 +0200148
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
Andrey Andreev09375d72012-01-19 14:57:46 +0200153 // mapping array contains multiple identical replacements. For example:
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 // :-) 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 }
Barry Mienydd671972010-10-04 16:33:58 +0200160
Andrey Andreev09375d72012-01-19 14:57:46 +0200161 $link[] = '<a href="javascript:void(0);" onclick="insert_smiley(\''.$key.'\', \''.$alias.'\')"><img src="'.$image_url.$smileys[$key][0].'" alt="'.$smileys[$key][3].'" style="width: '.$smileys[$key][1].'; height: '.$smileys[$key][2].'; border: 0;" /></a>';
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 $used[$smileys[$key][0]] = TRUE;
163 }
Barry Mienydd671972010-10-04 16:33:58 +0200164
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 return $link;
166 }
167}
168
169// ------------------------------------------------------------------------
170
171/**
172 * Parse Smileys
173 *
174 * Takes a string as input and swaps any contained smileys for the actual image
175 *
176 * @access public
177 * @param string the text to be parsed
178 * @param string the URL to the folder containing the smiley images
179 * @return string
180 */
181if ( ! function_exists('parse_smileys'))
182{
183 function parse_smileys($str = '', $image_url = '', $smileys = NULL)
184 {
Andrey Andreev09375d72012-01-19 14:57:46 +0200185 if ($image_url == '' OR ( ! is_array($smileys) && FALSE === ($smileys = _get_smiley_array())))
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200187 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 }
189
190 // Add a trailing slash to the file path if needed
Andrey Andreev4921fed2012-01-07 01:28:07 +0200191 $image_url = rtrim($image_url, '/').'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000192
193 foreach ($smileys as $key => $val)
194 {
Andrey Andreev09375d72012-01-19 14:57:46 +0200195 $str = str_replace($key, '<img src="'.$image_url.$smileys[$key][0].'" alt="'.$smileys[$key][3].'" style="width: '.$smileys[$key][1].'; height: '.$smileys[$key][2].'; border: 0;" />', $str);
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 }
197
198 return $str;
199 }
200}
201
202// ------------------------------------------------------------------------
203
204/**
205 * Get Smiley Array
206 *
207 * Fetches the config/smiley.php file
208 *
209 * @access private
210 * @return mixed
211 */
212if ( ! function_exists('_get_smiley_array'))
213{
214 function _get_smiley_array()
215 {
Andrey Andreev09375d72012-01-19 14:57:46 +0200216 if (defined('ENVIRONMENT') && file_exists(APPPATH.'config/'.ENVIRONMENT.'/smileys.php'))
Greg Akerd96f8822011-12-27 16:23:47 -0600217 {
218 include(APPPATH.'config/'.ENVIRONMENT.'/smileys.php');
219 }
220 elseif (file_exists(APPPATH.'config/smileys.php'))
221 {
222 include(APPPATH.'config/smileys.php');
223 }
Andrey Andreev4921fed2012-01-07 01:28:07 +0200224
Andrey Andreev09375d72012-01-19 14:57:46 +0200225 return (isset($smileys) && is_array($smileys)) ? $smileys : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 }
227}
228
Pascal Krietede8f4092009-07-29 13:46:37 +0000229// ------------------------------------------------------------------------
230
231/**
232 * JS Insert Smiley
233 *
234 * Generates the javascript function needed to insert smileys into a form field
235 *
236 * DEPRECATED as of version 1.7.2, use smiley_js instead
237 *
238 * @access public
239 * @param string form name
240 * @param string field name
241 * @return string
242 */
243if ( ! function_exists('js_insert_smiley'))
244{
245 function js_insert_smiley($form_name = '', $form_field = '')
246 {
247 return <<<EOF
248<script type="text/javascript">
249 function insert_smiley(smiley)
250 {
251 document.{$form_name}.{$form_field}.value += " " + smiley;
252 }
253</script>
254EOF;
255 }
256}
257
Derek Allard2067d1a2008-11-13 22:59:24 +0000258/* End of file smiley_helper.php */
Andrey Andreev4921fed2012-01-07 01:28:07 +0200259/* Location: ./system/helpers/smiley_helper.php */