blob: f67890c5bca80ee207d7f2618c55cd112076c0b5 [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 *
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
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, 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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * CodeIgniter Smiley Helpers
31 *
32 * @package CodeIgniter
33 * @subpackage Helpers
34 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @link http://codeigniter.com/user_guide/helpers/smiley_helper.html
37 */
38
39// ------------------------------------------------------------------------
40
Pascal Krietede8f4092009-07-29 13:46:37 +000041if ( ! function_exists('smiley_js'))
Derek Allard2067d1a2008-11-13 22:59:24 +000042{
Timothy Warrenb75faa12012-04-27 12:03:32 -040043 /**
44 * Smiley Javascript
45 *
46 * Returns the javascript required for the smiley insertion. Optionally takes
47 * an array of aliases to loosely couple the smiley array to the view.
48 *
49 * @param mixed alias name or array of alias->field_id pairs
50 * @param string field_id if alias name was passed in
51 * @param bool
52 * @return array
53 */
Greg Aker60e78c72010-04-20 12:45:14 -050054 function smiley_js($alias = '', $field_id = '', $inline = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +000055 {
Pascal Krietede8f4092009-07-29 13:46:37 +000056 static $do_setup = TRUE;
Pascal Krietede8f4092009-07-29 13:46:37 +000057 $r = '';
Barry Mienydd671972010-10-04 16:33:58 +020058
Alex Bilbie773ccc32012-06-02 11:11:08 +010059 if ($alias !== '' && ! is_array($alias))
Pascal Krietede8f4092009-07-29 13:46:37 +000060 {
61 $alias = array($alias => $field_id);
62 }
63
64 if ($do_setup === TRUE)
65 {
Andrey Andreev4921fed2012-01-07 01:28:07 +020066 $do_setup = FALSE;
67 $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 {
Andrey Andreev4921fed2012-01-07 01:28:07 +020073 $m[] = '"'.$name.'" : "'.$id.'"';
Pascal Krietede8f4092009-07-29 13:46:37 +000074 }
75 }
Andrey Andreev4921fed2012-01-07 01:28:07 +020076
77 $m = '{'.implode(',', $m).'}';
78
79 $r .= <<<EOF
80 var smiley_map = {$m};
81
82 function insert_smiley(smiley, field_id) {
83 var el = document.getElementById(field_id), newStart;
84
85 if ( ! el && smiley_map[field_id]) {
86 el = document.getElementById(smiley_map[field_id]);
87
88 if ( ! el)
89 return false;
Derek Allard2067d1a2008-11-13 22:59:24 +000090 }
91
Andrey Andreev4921fed2012-01-07 01:28:07 +020092 el.focus();
93 smiley = " " + smiley;
Derek Allard2067d1a2008-11-13 22:59:24 +000094
Andrey Andreev4921fed2012-01-07 01:28:07 +020095 if ('selectionStart' in el) {
96 newStart = el.selectionStart + smiley.length;
Derek Allard2067d1a2008-11-13 22:59:24 +000097
Andrey Andreev4921fed2012-01-07 01:28:07 +020098 el.value = el.value.substr(0, el.selectionStart) +
99 smiley +
100 el.value.substr(el.selectionEnd, el.value.length);
101 el.setSelectionRange(newStart, newStart);
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 }
Andrey Andreev4921fed2012-01-07 01:28:07 +0200103 else if (document.selection) {
104 document.selection.createRange().text = smiley;
105 }
106 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000107EOF;
Pascal Krietede8f4092009-07-29 13:46:37 +0000108 }
Andrey Andreev09375d72012-01-19 14:57:46 +0200109 elseif (is_array($alias))
Pascal Krietede8f4092009-07-29 13:46:37 +0000110 {
Andrey Andreev09375d72012-01-19 14:57:46 +0200111 foreach ($alias as $name => $id)
Pascal Krietede8f4092009-07-29 13:46:37 +0000112 {
Andrey Andreev09375d72012-01-19 14:57:46 +0200113 $r .= 'smiley_map["'.$name.'"] = "'.$id."\";\n";
Pascal Krietede8f4092009-07-29 13:46:37 +0000114 }
115 }
116
Andrey Andreevea41c8a2014-02-26 18:31:02 +0200117 return ($inline)
118 ? '<script type="text/javascript" charset="utf-8">/*<![CDATA[ */'.$r.'// ]]></script>'
119 : $r;
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 }
121}
Pascal Krietede8f4092009-07-29 13:46:37 +0000122
Derek Allard2067d1a2008-11-13 22:59:24 +0000123// ------------------------------------------------------------------------
124
Derek Allard2067d1a2008-11-13 22:59:24 +0000125if ( ! function_exists('get_clickable_smileys'))
126{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400127 /**
128 * Get Clickable Smileys
129 *
130 * Returns an array of image tag links that can be clicked to be inserted
131 * into a form field.
132 *
133 * @param string the URL to the folder containing the smiley images
134 * @param array
Timothy Warrenb75faa12012-04-27 12:03:32 -0400135 * @return array
136 */
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200137 function get_clickable_smileys($image_url, $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 {
Pascal Krietede8f4092009-07-29 13:46:37 +0000139 // For backward compatibility with js_insert_smiley
Pascal Krietede8f4092009-07-29 13:46:37 +0000140 if (is_array($alias))
141 {
142 $smileys = $alias;
143 }
Andrey Andreev09375d72012-01-19 14:57:46 +0200144 elseif (FALSE === ($smileys = _get_smiley_array()))
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 {
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200146 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 }
148
149 // Add a trailing slash to the file path if needed
Pascal Krietede8f4092009-07-29 13:46:37 +0000150 $image_url = rtrim($image_url, '/').'/';
Barry Mienydd671972010-10-04 16:33:58 +0200151
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 $used = array();
153 foreach ($smileys as $key => $val)
154 {
155 // Keep duplicates from being used, which can happen if the
Andrey Andreev09375d72012-01-19 14:57:46 +0200156 // mapping array contains multiple identical replacements. For example:
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 // :-) and :) might be replaced with the same image so both smileys
158 // will be in the array.
159 if (isset($used[$smileys[$key][0]]))
160 {
161 continue;
162 }
Barry Mienydd671972010-10-04 16:33:58 +0200163
Andrey Andreev09375d72012-01-19 14:57:46 +0200164 $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 +0000165 $used[$smileys[$key][0]] = TRUE;
166 }
Barry Mienydd671972010-10-04 16:33:58 +0200167
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 return $link;
169 }
170}
171
172// ------------------------------------------------------------------------
173
Derek Allard2067d1a2008-11-13 22:59:24 +0000174if ( ! function_exists('parse_smileys'))
175{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400176 /**
177 * Parse Smileys
178 *
179 * Takes a string as input and swaps any contained smileys for the actual image
180 *
181 * @param string the text to be parsed
182 * @param string the URL to the folder containing the smiley images
183 * @param array
184 * @return string
185 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 function parse_smileys($str = '', $image_url = '', $smileys = NULL)
187 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100188 if ($image_url === '' OR ( ! is_array($smileys) && FALSE === ($smileys = _get_smiley_array())))
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 {
190 return $str;
191 }
192
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 // Add a trailing slash to the file path if needed
Andrey Andreev4921fed2012-01-07 01:28:07 +0200194 $image_url = rtrim($image_url, '/').'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000195
196 foreach ($smileys as $key => $val)
197 {
Andrey Andreev09375d72012-01-19 14:57:46 +0200198 $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 +0000199 }
200
201 return $str;
202 }
203}
204
205// ------------------------------------------------------------------------
206
Derek Allard2067d1a2008-11-13 22:59:24 +0000207if ( ! function_exists('_get_smiley_array'))
208{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400209 /**
210 * Get Smiley Array
211 *
212 * Fetches the config/smiley.php file
213 *
214 * @return mixed
215 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 function _get_smiley_array()
217 {
Andrey Andreev06879112013-01-29 15:05:02 +0200218 static $_smileys;
219
220 if ( ! is_array($smileys))
Greg Akerd96f8822011-12-27 16:23:47 -0600221 {
Andrey Andreev06879112013-01-29 15:05:02 +0200222 if (file_exists(APPPATH.'config/smileys.php'))
223 {
224 include(APPPATH.'config/smileys.php');
225 }
226
227 if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/smileys.php'))
228 {
229 include(APPPATH.'config/'.ENVIRONMENT.'/smileys.php');
230 }
231
232 if (empty($smileys) OR ! is_array($smileys))
233 {
234 $_smileys = array();
235 return FALSE;
236 }
237
238 $_smileys = $smileys;
Greg Akerd96f8822011-12-27 16:23:47 -0600239 }
Andrey Andreeve684bda2012-03-26 13:47:29 +0300240
Andrey Andreev06879112013-01-29 15:05:02 +0200241 return $_smileys;
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 }
243}
244
Derek Allard2067d1a2008-11-13 22:59:24 +0000245/* End of file smiley_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000246/* Location: ./system/helpers/smiley_helper.php */