blob: d9a693493df5c49f4508aa1854ecc4cec01cbc70 [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
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, 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 Andreev4921fed2012-01-07 01:28:07 +0200117 return ($inline) ? '<script type="text/javascript" charset="utf-8">/*<![CDATA[ */'.$r.'// ]]></script>' : $r;
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 }
119}
Pascal Krietede8f4092009-07-29 13:46:37 +0000120
Derek Allard2067d1a2008-11-13 22:59:24 +0000121// ------------------------------------------------------------------------
122
Derek Allard2067d1a2008-11-13 22:59:24 +0000123if ( ! function_exists('get_clickable_smileys'))
124{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400125 /**
126 * Get Clickable Smileys
127 *
128 * Returns an array of image tag links that can be clicked to be inserted
129 * into a form field.
130 *
131 * @param string the URL to the folder containing the smiley images
132 * @param array
Timothy Warrenb75faa12012-04-27 12:03:32 -0400133 * @return array
134 */
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200135 function get_clickable_smileys($image_url, $alias = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 {
Pascal Krietede8f4092009-07-29 13:46:37 +0000137 // For backward compatibility with js_insert_smiley
Pascal Krietede8f4092009-07-29 13:46:37 +0000138 if (is_array($alias))
139 {
140 $smileys = $alias;
141 }
Andrey Andreev09375d72012-01-19 14:57:46 +0200142 elseif (FALSE === ($smileys = _get_smiley_array()))
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 {
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200144 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 }
146
147 // Add a trailing slash to the file path if needed
Pascal Krietede8f4092009-07-29 13:46:37 +0000148 $image_url = rtrim($image_url, '/').'/';
Barry Mienydd671972010-10-04 16:33:58 +0200149
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 $used = array();
151 foreach ($smileys as $key => $val)
152 {
153 // Keep duplicates from being used, which can happen if the
Andrey Andreev09375d72012-01-19 14:57:46 +0200154 // mapping array contains multiple identical replacements. For example:
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 // :-) and :) might be replaced with the same image so both smileys
156 // will be in the array.
157 if (isset($used[$smileys[$key][0]]))
158 {
159 continue;
160 }
Barry Mienydd671972010-10-04 16:33:58 +0200161
Andrey Andreev09375d72012-01-19 14:57:46 +0200162 $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 +0000163 $used[$smileys[$key][0]] = TRUE;
164 }
Barry Mienydd671972010-10-04 16:33:58 +0200165
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 return $link;
167 }
168}
169
170// ------------------------------------------------------------------------
171
Derek Allard2067d1a2008-11-13 22:59:24 +0000172if ( ! function_exists('parse_smileys'))
173{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400174 /**
175 * Parse Smileys
176 *
177 * Takes a string as input and swaps any contained smileys for the actual image
178 *
179 * @param string the text to be parsed
180 * @param string the URL to the folder containing the smiley images
181 * @param array
182 * @return string
183 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 function parse_smileys($str = '', $image_url = '', $smileys = NULL)
185 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100186 if ($image_url === '' OR ( ! is_array($smileys) && FALSE === ($smileys = _get_smiley_array())))
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 {
188 return $str;
189 }
190
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 // Add a trailing slash to the file path if needed
Andrey Andreev4921fed2012-01-07 01:28:07 +0200192 $image_url = rtrim($image_url, '/').'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000193
194 foreach ($smileys as $key => $val)
195 {
Andrey Andreev09375d72012-01-19 14:57:46 +0200196 $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 +0000197 }
198
199 return $str;
200 }
201}
202
203// ------------------------------------------------------------------------
204
Derek Allard2067d1a2008-11-13 22:59:24 +0000205if ( ! function_exists('_get_smiley_array'))
206{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400207 /**
208 * Get Smiley Array
209 *
210 * Fetches the config/smiley.php file
211 *
212 * @return mixed
213 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000214 function _get_smiley_array()
215 {
Andrey Andreev06879112013-01-29 15:05:02 +0200216 static $_smileys;
217
218 if ( ! is_array($smileys))
Greg Akerd96f8822011-12-27 16:23:47 -0600219 {
Andrey Andreev06879112013-01-29 15:05:02 +0200220 if (file_exists(APPPATH.'config/smileys.php'))
221 {
222 include(APPPATH.'config/smileys.php');
223 }
224
225 if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/smileys.php'))
226 {
227 include(APPPATH.'config/'.ENVIRONMENT.'/smileys.php');
228 }
229
230 if (empty($smileys) OR ! is_array($smileys))
231 {
232 $_smileys = array();
233 return FALSE;
234 }
235
236 $_smileys = $smileys;
Greg Akerd96f8822011-12-27 16:23:47 -0600237 }
Andrey Andreeve684bda2012-03-26 13:47:29 +0300238
Andrey Andreev06879112013-01-29 15:05:02 +0200239 return $_smileys;
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 }
241}
242
Derek Allard2067d1a2008-11-13 22:59:24 +0000243/* End of file smiley_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000244/* Location: ./system/helpers/smiley_helper.php */