blob: bc265e5527a1e5c83c134c2be0c2a38519d7575a [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
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
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;
Pascal Krietede8f4092009-07-29 13:46:37 +000058 $r = '';
Barry Mienydd671972010-10-04 16:33:58 +020059
Pascal Krietede8f4092009-07-29 13:46:37 +000060 if ($alias != '' && ! is_array($alias))
61 {
62 $alias = array($alias => $field_id);
63 }
64
65 if ($do_setup === TRUE)
66 {
Andrey Andreev4921fed2012-01-07 01:28:07 +020067 $do_setup = FALSE;
68 $m = array();
Barry Mienydd671972010-10-04 16:33:58 +020069
Andrey Andreev4921fed2012-01-07 01:28:07 +020070 if (is_array($alias))
71 {
72 foreach ($alias as $name => $id)
Pascal Krietede8f4092009-07-29 13:46:37 +000073 {
Andrey Andreev4921fed2012-01-07 01:28:07 +020074 $m[] = '"'.$name.'" : "'.$id.'"';
75 }
76 }
77
78 $m = '{'.implode(',', $m).'}';
79
80 $r .= <<<EOF
81 var smiley_map = {$m};
82
83 function insert_smiley(smiley, field_id) {
84 var el = document.getElementById(field_id), newStart;
85
86 if ( ! el && smiley_map[field_id]) {
87 el = document.getElementById(smiley_map[field_id]);
88
89 if ( ! el)
90 return false;
Pascal Krietede8f4092009-07-29 13:46:37 +000091 }
Barry Mienydd671972010-10-04 16:33:58 +020092
Andrey Andreev4921fed2012-01-07 01:28:07 +020093 el.focus();
94 smiley = " " + smiley;
Barry Mienydd671972010-10-04 16:33:58 +020095
Andrey Andreev4921fed2012-01-07 01:28:07 +020096 if ('selectionStart' in el) {
97 newStart = el.selectionStart + smiley.length;
Pascal Krietede8f4092009-07-29 13:46:37 +000098
Andrey Andreev4921fed2012-01-07 01:28:07 +020099 el.value = el.value.substr(0, el.selectionStart) +
100 smiley +
101 el.value.substr(el.selectionEnd, el.value.length);
102 el.setSelectionRange(newStart, newStart);
Pascal Krietede8f4092009-07-29 13:46:37 +0000103 }
Andrey Andreev4921fed2012-01-07 01:28:07 +0200104 else if (document.selection) {
105 document.selection.createRange().text = smiley;
106 }
107 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000108EOF;
Pascal Krietede8f4092009-07-29 13:46:37 +0000109 }
110 else
111 {
112 if (is_array($alias))
113 {
Pascal Kriete45e3cdf2011-02-14 13:26:20 -0500114 foreach ($alias as $name => $id)
Pascal Krietede8f4092009-07-29 13:46:37 +0000115 {
116 $r .= 'smiley_map["'.$name.'"] = "'.$id.'";'."\n";
117 }
118 }
119 }
120
Andrey Andreev4921fed2012-01-07 01:28:07 +0200121 return ($inline) ? '<script type="text/javascript" charset="utf-8">/*<![CDATA[ */'.$r.'// ]]></script>' : $r;
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 }
123}
Pascal Krietede8f4092009-07-29 13:46:37 +0000124
Derek Allard2067d1a2008-11-13 22:59:24 +0000125// ------------------------------------------------------------------------
126
127/**
128 * Get Clickable Smileys
129 *
Barry Mienydd671972010-10-04 16:33:58 +0200130 * Returns an array of image tag links that can be clicked to be inserted
131 * into a form field.
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 *
133 * @access public
134 * @param string the URL to the folder containing the smiley images
135 * @return array
136 */
137if ( ! function_exists('get_clickable_smileys'))
138{
Pascal Krietede8f4092009-07-29 13:46:37 +0000139 function get_clickable_smileys($image_url, $alias = '', $smileys = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 {
Pascal Krietede8f4092009-07-29 13:46:37 +0000141 // For backward compatibility with js_insert_smiley
Barry Mienydd671972010-10-04 16:33:58 +0200142
Pascal Krietede8f4092009-07-29 13:46:37 +0000143 if (is_array($alias))
144 {
145 $smileys = $alias;
146 }
Barry Mienydd671972010-10-04 16:33:58 +0200147
Andrey Andreev4921fed2012-01-07 01:28:07 +0200148 if ( ! is_array($smileys) && FALSE === ($smileys = _get_smiley_array()))
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200150 return $smileys;
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 }
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, '/').'/';
Barry Mienydd671972010-10-04 16:33:58 +0200155
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
Derek Jones37f4b9c2011-07-01 17:56:50 -0500160 // mapping array contains multiple identical replacements. For example:
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 // :-) 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 }
Barry Mienydd671972010-10-04 16:33:58 +0200167
168 $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>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 $used[$smileys[$key][0]] = TRUE;
170 }
Barry Mienydd671972010-10-04 16:33:58 +0200171
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 return $link;
173 }
174}
175
176// ------------------------------------------------------------------------
177
178/**
179 * Parse Smileys
180 *
181 * Takes a string as input and swaps any contained smileys for the actual image
182 *
183 * @access public
184 * @param string the text to be parsed
185 * @param string the URL to the folder containing the smiley images
186 * @return string
187 */
188if ( ! function_exists('parse_smileys'))
189{
190 function parse_smileys($str = '', $image_url = '', $smileys = NULL)
191 {
192 if ($image_url == '')
193 {
194 return $str;
195 }
196
Andrey Andreev4921fed2012-01-07 01:28:07 +0200197 if ( ! is_array($smileys) && FALSE === ($smileys = _get_smiley_array()))
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200199 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 }
201
202 // Add a trailing slash to the file path if needed
Andrey Andreev4921fed2012-01-07 01:28:07 +0200203 $image_url = rtrim($image_url, '/').'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000204
205 foreach ($smileys as $key => $val)
206 {
207 $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);
208 }
209
210 return $str;
211 }
212}
213
214// ------------------------------------------------------------------------
215
216/**
217 * Get Smiley Array
218 *
219 * Fetches the config/smiley.php file
220 *
221 * @access private
222 * @return mixed
223 */
224if ( ! function_exists('_get_smiley_array'))
225{
226 function _get_smiley_array()
227 {
Greg Akerd96f8822011-12-27 16:23:47 -0600228 if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/smileys.php'))
229 {
230 include(APPPATH.'config/'.ENVIRONMENT.'/smileys.php');
231 }
232 elseif (file_exists(APPPATH.'config/smileys.php'))
233 {
234 include(APPPATH.'config/smileys.php');
235 }
Andrey Andreev4921fed2012-01-07 01:28:07 +0200236
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100237 if (isset($smileys) AND is_array($smileys))
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 {
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100239 return $smileys;
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 }
241
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100242 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 }
244}
245
Pascal Krietede8f4092009-07-29 13:46:37 +0000246// ------------------------------------------------------------------------
247
248/**
249 * JS Insert Smiley
250 *
251 * Generates the javascript function needed to insert smileys into a form field
252 *
253 * DEPRECATED as of version 1.7.2, use smiley_js instead
254 *
255 * @access public
256 * @param string form name
257 * @param string field name
258 * @return string
259 */
260if ( ! function_exists('js_insert_smiley'))
261{
262 function js_insert_smiley($form_name = '', $form_field = '')
263 {
264 return <<<EOF
265<script type="text/javascript">
266 function insert_smiley(smiley)
267 {
268 document.{$form_name}.{$form_field}.value += " " + smiley;
269 }
270</script>
271EOF;
272 }
273}
274
Derek Allard2067d1a2008-11-13 22:59:24 +0000275/* End of file smiley_helper.php */
Andrey Andreev4921fed2012-01-07 01:28:07 +0200276/* Location: ./system/helpers/smiley_helper.php */