blob: a529c4537354aa4f0bc7d015efae2b2e93919e1a [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreeve684bda2012-03-26 13:47:29 +03008 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02009 * Copyright (c) 2014, British Columbia Institute of Technology
Andrey Andreeve684bda2012-03-26 13:47:29 +030010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020032 * @copyright Copyright (c) 2014, British Columbia Institute of Technology (http://bcit.ca/)
33 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * CodeIgniter Smiley Helpers
42 *
43 * @package CodeIgniter
44 * @subpackage Helpers
45 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050046 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000047 * @link http://codeigniter.com/user_guide/helpers/smiley_helper.html
48 */
49
50// ------------------------------------------------------------------------
51
Pascal Krietede8f4092009-07-29 13:46:37 +000052if ( ! function_exists('smiley_js'))
Derek Allard2067d1a2008-11-13 22:59:24 +000053{
Timothy Warrenb75faa12012-04-27 12:03:32 -040054 /**
55 * Smiley Javascript
56 *
57 * Returns the javascript required for the smiley insertion. Optionally takes
58 * an array of aliases to loosely couple the smiley array to the view.
59 *
60 * @param mixed alias name or array of alias->field_id pairs
61 * @param string field_id if alias name was passed in
62 * @param bool
63 * @return array
64 */
Greg Aker60e78c72010-04-20 12:45:14 -050065 function smiley_js($alias = '', $field_id = '', $inline = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +000066 {
Pascal Krietede8f4092009-07-29 13:46:37 +000067 static $do_setup = TRUE;
Pascal Krietede8f4092009-07-29 13:46:37 +000068 $r = '';
Barry Mienydd671972010-10-04 16:33:58 +020069
Alex Bilbie773ccc32012-06-02 11:11:08 +010070 if ($alias !== '' && ! is_array($alias))
Pascal Krietede8f4092009-07-29 13:46:37 +000071 {
72 $alias = array($alias => $field_id);
73 }
74
75 if ($do_setup === TRUE)
76 {
Andrey Andreev4921fed2012-01-07 01:28:07 +020077 $do_setup = FALSE;
78 $m = array();
Barry Mienydd671972010-10-04 16:33:58 +020079
Pascal Krietede8f4092009-07-29 13:46:37 +000080 if (is_array($alias))
81 {
Pascal Kriete45e3cdf2011-02-14 13:26:20 -050082 foreach ($alias as $name => $id)
Pascal Krietede8f4092009-07-29 13:46:37 +000083 {
Andrey Andreev4921fed2012-01-07 01:28:07 +020084 $m[] = '"'.$name.'" : "'.$id.'"';
Pascal Krietede8f4092009-07-29 13:46:37 +000085 }
86 }
Andrey Andreev4921fed2012-01-07 01:28:07 +020087
88 $m = '{'.implode(',', $m).'}';
89
90 $r .= <<<EOF
91 var smiley_map = {$m};
92
93 function insert_smiley(smiley, field_id) {
94 var el = document.getElementById(field_id), newStart;
95
96 if ( ! el && smiley_map[field_id]) {
97 el = document.getElementById(smiley_map[field_id]);
98
99 if ( ! el)
100 return false;
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 }
102
Andrey Andreev4921fed2012-01-07 01:28:07 +0200103 el.focus();
104 smiley = " " + smiley;
Derek Allard2067d1a2008-11-13 22:59:24 +0000105
Andrey Andreev4921fed2012-01-07 01:28:07 +0200106 if ('selectionStart' in el) {
107 newStart = el.selectionStart + smiley.length;
Derek Allard2067d1a2008-11-13 22:59:24 +0000108
Andrey Andreev4921fed2012-01-07 01:28:07 +0200109 el.value = el.value.substr(0, el.selectionStart) +
110 smiley +
111 el.value.substr(el.selectionEnd, el.value.length);
112 el.setSelectionRange(newStart, newStart);
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 }
Andrey Andreev4921fed2012-01-07 01:28:07 +0200114 else if (document.selection) {
115 document.selection.createRange().text = smiley;
116 }
117 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000118EOF;
Pascal Krietede8f4092009-07-29 13:46:37 +0000119 }
Andrey Andreev09375d72012-01-19 14:57:46 +0200120 elseif (is_array($alias))
Pascal Krietede8f4092009-07-29 13:46:37 +0000121 {
Andrey Andreev09375d72012-01-19 14:57:46 +0200122 foreach ($alias as $name => $id)
Pascal Krietede8f4092009-07-29 13:46:37 +0000123 {
Andrey Andreev09375d72012-01-19 14:57:46 +0200124 $r .= 'smiley_map["'.$name.'"] = "'.$id."\";\n";
Pascal Krietede8f4092009-07-29 13:46:37 +0000125 }
126 }
127
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200128 return ($inline)
129 ? '<script type="text/javascript" charset="utf-8">/*<![CDATA[ */'.$r.'// ]]></script>'
130 : $r;
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
Derek Allard2067d1a2008-11-13 22:59:24 +0000136if ( ! function_exists('get_clickable_smileys'))
137{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400138 /**
139 * Get Clickable Smileys
140 *
141 * Returns an array of image tag links that can be clicked to be inserted
142 * into a form field.
143 *
144 * @param string the URL to the folder containing the smiley images
145 * @param array
Timothy Warrenb75faa12012-04-27 12:03:32 -0400146 * @return array
147 */
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200148 function get_clickable_smileys($image_url, $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 {
Pascal Krietede8f4092009-07-29 13:46:37 +0000150 // For backward compatibility with js_insert_smiley
Pascal Krietede8f4092009-07-29 13:46:37 +0000151 if (is_array($alias))
152 {
153 $smileys = $alias;
154 }
Andrey Andreev09375d72012-01-19 14:57:46 +0200155 elseif (FALSE === ($smileys = _get_smiley_array()))
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 {
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200157 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 }
159
160 // Add a trailing slash to the file path if needed
Pascal Krietede8f4092009-07-29 13:46:37 +0000161 $image_url = rtrim($image_url, '/').'/';
Barry Mienydd671972010-10-04 16:33:58 +0200162
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 $used = array();
164 foreach ($smileys as $key => $val)
165 {
166 // Keep duplicates from being used, which can happen if the
Andrey Andreev09375d72012-01-19 14:57:46 +0200167 // mapping array contains multiple identical replacements. For example:
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 // :-) and :) might be replaced with the same image so both smileys
169 // will be in the array.
170 if (isset($used[$smileys[$key][0]]))
171 {
172 continue;
173 }
Barry Mienydd671972010-10-04 16:33:58 +0200174
Andrey Andreev09375d72012-01-19 14:57:46 +0200175 $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 +0000176 $used[$smileys[$key][0]] = TRUE;
177 }
Barry Mienydd671972010-10-04 16:33:58 +0200178
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 return $link;
180 }
181}
182
183// ------------------------------------------------------------------------
184
Derek Allard2067d1a2008-11-13 22:59:24 +0000185if ( ! function_exists('parse_smileys'))
186{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400187 /**
188 * Parse Smileys
189 *
190 * Takes a string as input and swaps any contained smileys for the actual image
191 *
192 * @param string the text to be parsed
193 * @param string the URL to the folder containing the smiley images
194 * @param array
195 * @return string
196 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 function parse_smileys($str = '', $image_url = '', $smileys = NULL)
198 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100199 if ($image_url === '' OR ( ! is_array($smileys) && FALSE === ($smileys = _get_smiley_array())))
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 {
201 return $str;
202 }
203
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 // Add a trailing slash to the file path if needed
Andrey Andreev4921fed2012-01-07 01:28:07 +0200205 $image_url = rtrim($image_url, '/').'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000206
207 foreach ($smileys as $key => $val)
208 {
Andrey Andreev09375d72012-01-19 14:57:46 +0200209 $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 +0000210 }
211
212 return $str;
213 }
214}
215
216// ------------------------------------------------------------------------
217
Derek Allard2067d1a2008-11-13 22:59:24 +0000218if ( ! function_exists('_get_smiley_array'))
219{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400220 /**
221 * Get Smiley Array
222 *
223 * Fetches the config/smiley.php file
224 *
225 * @return mixed
226 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 function _get_smiley_array()
228 {
Andrey Andreev06879112013-01-29 15:05:02 +0200229 static $_smileys;
230
231 if ( ! is_array($smileys))
Greg Akerd96f8822011-12-27 16:23:47 -0600232 {
Andrey Andreev06879112013-01-29 15:05:02 +0200233 if (file_exists(APPPATH.'config/smileys.php'))
234 {
235 include(APPPATH.'config/smileys.php');
236 }
237
238 if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/smileys.php'))
239 {
240 include(APPPATH.'config/'.ENVIRONMENT.'/smileys.php');
241 }
242
243 if (empty($smileys) OR ! is_array($smileys))
244 {
245 $_smileys = array();
246 return FALSE;
247 }
248
249 $_smileys = $smileys;
Greg Akerd96f8822011-12-27 16:23:47 -0600250 }
Andrey Andreeve684bda2012-03-26 13:47:29 +0300251
Andrey Andreev06879112013-01-29 15:05:02 +0200252 return $_smileys;
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 }
254}
255
Derek Allard2067d1a2008-11-13 22:59:24 +0000256/* End of file smiley_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000257/* Location: ./system/helpers/smiley_helper.php */