blob: 638100e9cdc7e4be8b0be1cad775d8f90ec554ee [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 *
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 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 *
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;
Pascal Krietede8f4092009-07-29 13:46:37 +000055 $r = '';
Barry Mienydd671972010-10-04 16:33:58 +020056
Pascal Krietede8f4092009-07-29 13:46:37 +000057 if ($alias != '' && ! is_array($alias))
58 {
59 $alias = array($alias => $field_id);
60 }
61
62 if ($do_setup === TRUE)
63 {
Andrey Andreev4921fed2012-01-07 01:28:07 +020064 $do_setup = FALSE;
65 $m = array();
Barry Mienydd671972010-10-04 16:33:58 +020066
Andrey Andreev4921fed2012-01-07 01:28:07 +020067 if (is_array($alias))
68 {
69 foreach ($alias as $name => $id)
Pascal Krietede8f4092009-07-29 13:46:37 +000070 {
Andrey Andreev4921fed2012-01-07 01:28:07 +020071 $m[] = '"'.$name.'" : "'.$id.'"';
72 }
73 }
74
75 $m = '{'.implode(',', $m).'}';
76
77 $r .= <<<EOF
78 var smiley_map = {$m};
79
80 function insert_smiley(smiley, field_id) {
81 var el = document.getElementById(field_id), newStart;
82
83 if ( ! el && smiley_map[field_id]) {
84 el = document.getElementById(smiley_map[field_id]);
85
86 if ( ! el)
87 return false;
Pascal Krietede8f4092009-07-29 13:46:37 +000088 }
Barry Mienydd671972010-10-04 16:33:58 +020089
Andrey Andreev4921fed2012-01-07 01:28:07 +020090 el.focus();
91 smiley = " " + smiley;
Barry Mienydd671972010-10-04 16:33:58 +020092
Andrey Andreev4921fed2012-01-07 01:28:07 +020093 if ('selectionStart' in el) {
94 newStart = el.selectionStart + smiley.length;
Pascal Krietede8f4092009-07-29 13:46:37 +000095
Andrey Andreev4921fed2012-01-07 01:28:07 +020096 el.value = el.value.substr(0, el.selectionStart) +
97 smiley +
98 el.value.substr(el.selectionEnd, el.value.length);
99 el.setSelectionRange(newStart, newStart);
Pascal Krietede8f4092009-07-29 13:46:37 +0000100 }
Andrey Andreev4921fed2012-01-07 01:28:07 +0200101 else if (document.selection) {
102 document.selection.createRange().text = smiley;
103 }
104 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000105EOF;
Pascal Krietede8f4092009-07-29 13:46:37 +0000106 }
Andrey Andreev09375d72012-01-19 14:57:46 +0200107 elseif (is_array($alias))
Pascal Krietede8f4092009-07-29 13:46:37 +0000108 {
Andrey Andreev09375d72012-01-19 14:57:46 +0200109 foreach ($alias as $name => $id)
Pascal Krietede8f4092009-07-29 13:46:37 +0000110 {
Andrey Andreev09375d72012-01-19 14:57:46 +0200111 $r .= 'smiley_map["'.$name.'"] = "'.$id."\";\n";
Pascal Krietede8f4092009-07-29 13:46:37 +0000112 }
113 }
114
Andrey Andreev4921fed2012-01-07 01:28:07 +0200115 return ($inline) ? '<script type="text/javascript" charset="utf-8">/*<![CDATA[ */'.$r.'// ]]></script>' : $r;
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 }
117}
Pascal Krietede8f4092009-07-29 13:46:37 +0000118
Derek Allard2067d1a2008-11-13 22:59:24 +0000119// ------------------------------------------------------------------------
120
121/**
122 * Get Clickable Smileys
123 *
Barry Mienydd671972010-10-04 16:33:58 +0200124 * Returns an array of image tag links that can be clicked to be inserted
125 * into a form field.
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 * @param string the URL to the folder containing the smiley images
128 * @return array
129 */
130if ( ! function_exists('get_clickable_smileys'))
131{
Pascal Krietede8f4092009-07-29 13:46:37 +0000132 function get_clickable_smileys($image_url, $alias = '', $smileys = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 {
Pascal Krietede8f4092009-07-29 13:46:37 +0000134 // For backward compatibility with js_insert_smiley
Pascal Krietede8f4092009-07-29 13:46:37 +0000135 if (is_array($alias))
136 {
137 $smileys = $alias;
138 }
Andrey Andreev09375d72012-01-19 14:57:46 +0200139 elseif (FALSE === ($smileys = _get_smiley_array()))
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200141 return $smileys;
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 }
143
144 // Add a trailing slash to the file path if needed
Pascal Krietede8f4092009-07-29 13:46:37 +0000145 $image_url = rtrim($image_url, '/').'/';
Barry Mienydd671972010-10-04 16:33:58 +0200146
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 $used = array();
148 foreach ($smileys as $key => $val)
149 {
150 // Keep duplicates from being used, which can happen if the
Andrey Andreev09375d72012-01-19 14:57:46 +0200151 // mapping array contains multiple identical replacements. For example:
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 // :-) and :) might be replaced with the same image so both smileys
153 // will be in the array.
154 if (isset($used[$smileys[$key][0]]))
155 {
156 continue;
157 }
Barry Mienydd671972010-10-04 16:33:58 +0200158
Andrey Andreev09375d72012-01-19 14:57:46 +0200159 $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 +0000160 $used[$smileys[$key][0]] = TRUE;
161 }
Barry Mienydd671972010-10-04 16:33:58 +0200162
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 return $link;
164 }
165}
166
167// ------------------------------------------------------------------------
168
169/**
170 * Parse Smileys
171 *
172 * Takes a string as input and swaps any contained smileys for the actual image
173 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 * @param string the text to be parsed
175 * @param string the URL to the folder containing the smiley images
176 * @return string
177 */
178if ( ! function_exists('parse_smileys'))
179{
180 function parse_smileys($str = '', $image_url = '', $smileys = NULL)
181 {
Andrey Andreev09375d72012-01-19 14:57:46 +0200182 if ($image_url == '' OR ( ! is_array($smileys) && FALSE === ($smileys = _get_smiley_array())))
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200184 return $str;
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 }
186
187 // Add a trailing slash to the file path if needed
Andrey Andreev4921fed2012-01-07 01:28:07 +0200188 $image_url = rtrim($image_url, '/').'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000189
190 foreach ($smileys as $key => $val)
191 {
Andrey Andreev09375d72012-01-19 14:57:46 +0200192 $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 +0000193 }
194
195 return $str;
196 }
197}
198
199// ------------------------------------------------------------------------
200
201/**
202 * Get Smiley Array
203 *
204 * Fetches the config/smiley.php file
205 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 * @return mixed
207 */
208if ( ! function_exists('_get_smiley_array'))
209{
210 function _get_smiley_array()
211 {
Andrey Andreev09375d72012-01-19 14:57:46 +0200212 if (defined('ENVIRONMENT') && file_exists(APPPATH.'config/'.ENVIRONMENT.'/smileys.php'))
Greg Akerd96f8822011-12-27 16:23:47 -0600213 {
214 include(APPPATH.'config/'.ENVIRONMENT.'/smileys.php');
215 }
216 elseif (file_exists(APPPATH.'config/smileys.php'))
217 {
218 include(APPPATH.'config/smileys.php');
219 }
Andrey Andreev4921fed2012-01-07 01:28:07 +0200220
Andrey Andreev09375d72012-01-19 14:57:46 +0200221 return (isset($smileys) && is_array($smileys)) ? $smileys : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 }
223}
224
Pascal Krietede8f4092009-07-29 13:46:37 +0000225// ------------------------------------------------------------------------
226
227/**
228 * JS Insert Smiley
229 *
230 * Generates the javascript function needed to insert smileys into a form field
231 *
232 * DEPRECATED as of version 1.7.2, use smiley_js instead
233 *
Pascal Krietede8f4092009-07-29 13:46:37 +0000234 * @param string form name
235 * @param string field name
236 * @return string
237 */
238if ( ! function_exists('js_insert_smiley'))
239{
240 function js_insert_smiley($form_name = '', $form_field = '')
241 {
242 return <<<EOF
243<script type="text/javascript">
244 function insert_smiley(smiley)
245 {
246 document.{$form_name}.{$form_field}.value += " " + smiley;
247 }
248</script>
249EOF;
250 }
251}
252
Derek Allard2067d1a2008-11-13 22:59:24 +0000253/* End of file smiley_helper.php */
Andrey Andreeve79a3b12012-03-20 15:41:22 +0200254/* Location: ./system/helpers/smiley_helper.php */