blob: b6b2afcf4c943c815f9a7b1d5312d46886c75863 [file] [log] [blame]
Andrey Andreeve684bda2012-03-26 13:47:29 +03001<?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 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
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
Pascal Krietede8f4092009-07-29 13:46:37 +000040if ( ! function_exists('smiley_js'))
Derek Allard2067d1a2008-11-13 22:59:24 +000041{
Timothy Warrenb75faa12012-04-27 12:03:32 -040042 /**
43 * Smiley Javascript
44 *
45 * Returns the javascript required for the smiley insertion. Optionally takes
46 * an array of aliases to loosely couple the smiley array to the view.
47 *
48 * @param mixed alias name or array of alias->field_id pairs
49 * @param string field_id if alias name was passed in
50 * @param bool
51 * @return array
52 */
Greg Aker60e78c72010-04-20 12:45:14 -050053 function smiley_js($alias = '', $field_id = '', $inline = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +000054 {
Pascal Krietede8f4092009-07-29 13:46:37 +000055 static $do_setup = TRUE;
Pascal Krietede8f4092009-07-29 13:46:37 +000056 $r = '';
Barry Mienydd671972010-10-04 16:33:58 +020057
Alex Bilbie773ccc32012-06-02 11:11:08 +010058 if ($alias !== '' && ! is_array($alias))
Pascal Krietede8f4092009-07-29 13:46:37 +000059 {
60 $alias = array($alias => $field_id);
61 }
62
63 if ($do_setup === TRUE)
64 {
Andrey Andreev4921fed2012-01-07 01:28:07 +020065 $do_setup = FALSE;
66 $m = array();
Barry Mienydd671972010-10-04 16:33:58 +020067
Pascal Krietede8f4092009-07-29 13:46:37 +000068 if (is_array($alias))
69 {
Pascal Kriete45e3cdf2011-02-14 13:26:20 -050070 foreach ($alias as $name => $id)
Pascal Krietede8f4092009-07-29 13:46:37 +000071 {
Andrey Andreev4921fed2012-01-07 01:28:07 +020072 $m[] = '"'.$name.'" : "'.$id.'"';
Pascal Krietede8f4092009-07-29 13:46:37 +000073 }
74 }
Andrey Andreev4921fed2012-01-07 01:28:07 +020075
76 $m = '{'.implode(',', $m).'}';
77
78 $r .= <<<EOF
79 var smiley_map = {$m};
80
81 function insert_smiley(smiley, field_id) {
82 var el = document.getElementById(field_id), newStart;
83
84 if ( ! el && smiley_map[field_id]) {
85 el = document.getElementById(smiley_map[field_id]);
86
87 if ( ! el)
88 return false;
Derek Allard2067d1a2008-11-13 22:59:24 +000089 }
90
Andrey Andreev4921fed2012-01-07 01:28:07 +020091 el.focus();
92 smiley = " " + smiley;
Derek Allard2067d1a2008-11-13 22:59:24 +000093
Andrey Andreev4921fed2012-01-07 01:28:07 +020094 if ('selectionStart' in el) {
95 newStart = el.selectionStart + smiley.length;
Derek Allard2067d1a2008-11-13 22:59:24 +000096
Andrey Andreev4921fed2012-01-07 01:28:07 +020097 el.value = el.value.substr(0, el.selectionStart) +
98 smiley +
99 el.value.substr(el.selectionEnd, el.value.length);
100 el.setSelectionRange(newStart, newStart);
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 }
Andrey Andreev4921fed2012-01-07 01:28:07 +0200102 else if (document.selection) {
103 document.selection.createRange().text = smiley;
104 }
105 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000106EOF;
Pascal Krietede8f4092009-07-29 13:46:37 +0000107 }
Andrey Andreev09375d72012-01-19 14:57:46 +0200108 elseif (is_array($alias))
Pascal Krietede8f4092009-07-29 13:46:37 +0000109 {
Andrey Andreev09375d72012-01-19 14:57:46 +0200110 foreach ($alias as $name => $id)
Pascal Krietede8f4092009-07-29 13:46:37 +0000111 {
Andrey Andreev09375d72012-01-19 14:57:46 +0200112 $r .= 'smiley_map["'.$name.'"] = "'.$id."\";\n";
Pascal Krietede8f4092009-07-29 13:46:37 +0000113 }
114 }
115
Andrey Andreev4921fed2012-01-07 01:28:07 +0200116 return ($inline) ? '<script type="text/javascript" charset="utf-8">/*<![CDATA[ */'.$r.'// ]]></script>' : $r;
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 }
118}
Pascal Krietede8f4092009-07-29 13:46:37 +0000119
Derek Allard2067d1a2008-11-13 22:59:24 +0000120// ------------------------------------------------------------------------
121
Timothy Warrenb75faa12012-04-27 12:03:32 -0400122
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 Warrenf0eb2f12012-05-17 08:32:11 -0400133 * @param array
Timothy Warrenb75faa12012-04-27 12:03:32 -0400134 * @return array
135 */
Pascal Krietede8f4092009-07-29 13:46:37 +0000136 function get_clickable_smileys($image_url, $alias = '', $smileys = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 {
Pascal Krietede8f4092009-07-29 13:46:37 +0000138 // For backward compatibility with js_insert_smiley
Pascal Krietede8f4092009-07-29 13:46:37 +0000139 if (is_array($alias))
140 {
141 $smileys = $alias;
142 }
Andrey Andreev09375d72012-01-19 14:57:46 +0200143 elseif (FALSE === ($smileys = _get_smiley_array()))
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 {
Andrey Andreev4921fed2012-01-07 01:28:07 +0200145 return $smileys;
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 }
147
148 // Add a trailing slash to the file path if needed
Pascal Krietede8f4092009-07-29 13:46:37 +0000149 $image_url = rtrim($image_url, '/').'/';
Barry Mienydd671972010-10-04 16:33:58 +0200150
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 $used = array();
152 foreach ($smileys as $key => $val)
153 {
154 // Keep duplicates from being used, which can happen if the
Andrey Andreev09375d72012-01-19 14:57:46 +0200155 // mapping array contains multiple identical replacements. For example:
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 // :-) and :) might be replaced with the same image so both smileys
157 // will be in the array.
158 if (isset($used[$smileys[$key][0]]))
159 {
160 continue;
161 }
Barry Mienydd671972010-10-04 16:33:58 +0200162
Andrey Andreev09375d72012-01-19 14:57:46 +0200163 $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 +0000164 $used[$smileys[$key][0]] = TRUE;
165 }
Barry Mienydd671972010-10-04 16:33:58 +0200166
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 return $link;
168 }
169}
170
171// ------------------------------------------------------------------------
172
Derek Allard2067d1a2008-11-13 22:59:24 +0000173if ( ! function_exists('parse_smileys'))
174{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400175 /**
176 * Parse Smileys
177 *
178 * Takes a string as input and swaps any contained smileys for the actual image
179 *
180 * @param string the text to be parsed
181 * @param string the URL to the folder containing the smiley images
182 * @param array
183 * @return string
184 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 function parse_smileys($str = '', $image_url = '', $smileys = NULL)
186 {
Alex Bilbie773ccc32012-06-02 11:11:08 +0100187 if ($image_url === '' OR ( ! is_array($smileys) && FALSE === ($smileys = _get_smiley_array())))
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 {
189 return $str;
190 }
191
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 // Add a trailing slash to the file path if needed
Andrey Andreev4921fed2012-01-07 01:28:07 +0200193 $image_url = rtrim($image_url, '/').'/';
Derek Allard2067d1a2008-11-13 22:59:24 +0000194
195 foreach ($smileys as $key => $val)
196 {
Andrey Andreev09375d72012-01-19 14:57:46 +0200197 $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 +0000198 }
199
200 return $str;
201 }
202}
203
204// ------------------------------------------------------------------------
205
Derek Allard2067d1a2008-11-13 22:59:24 +0000206if ( ! function_exists('_get_smiley_array'))
207{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400208 /**
209 * Get Smiley Array
210 *
211 * Fetches the config/smiley.php file
212 *
213 * @return mixed
214 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 function _get_smiley_array()
216 {
Andrey Andreeve684bda2012-03-26 13:47:29 +0300217 if (defined('ENVIRONMENT') && file_exists(APPPATH.'config/'.ENVIRONMENT.'/smileys.php'))
Greg Akerd96f8822011-12-27 16:23:47 -0600218 {
219 include(APPPATH.'config/'.ENVIRONMENT.'/smileys.php');
220 }
221 elseif (file_exists(APPPATH.'config/smileys.php'))
222 {
223 include(APPPATH.'config/smileys.php');
224 }
Andrey Andreeve684bda2012-03-26 13:47:29 +0300225
Andrey Andreev09375d72012-01-19 14:57:46 +0200226 return (isset($smileys) && is_array($smileys)) ? $smileys : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 }
228}
229
Derek Allard2067d1a2008-11-13 22:59:24 +0000230/* End of file smiley_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000231/* Location: ./system/helpers/smiley_helper.php */