blob: 6b4daf6d9a44d1ae8cc0c96c37efa87359d670a3 [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;
56
57 $r = '';
Barry Mienydd671972010-10-04 16:33:58 +020058
Pascal Krietede8f4092009-07-29 13:46:37 +000059 if ($alias != '' && ! is_array($alias))
60 {
61 $alias = array($alias => $field_id);
62 }
63
64 if ($do_setup === TRUE)
65 {
66 $do_setup = FALSE;
Barry Mienydd671972010-10-04 16:33:58 +020067
Pascal Krietede8f4092009-07-29 13:46:37 +000068 $m = array();
Barry Mienydd671972010-10-04 16:33:58 +020069
Pascal Krietede8f4092009-07-29 13:46:37 +000070 if (is_array($alias))
71 {
Pascal Kriete45e3cdf2011-02-14 13:26:20 -050072 foreach ($alias as $name => $id)
Pascal Krietede8f4092009-07-29 13:46:37 +000073 {
74 $m[] = '"'.$name.'" : "'.$id.'"';
75 }
76 }
Barry Mienydd671972010-10-04 16:33:58 +020077
Pascal Krietede8f4092009-07-29 13:46:37 +000078 $m = '{'.implode(',', $m).'}';
Barry Mienydd671972010-10-04 16:33:58 +020079
Pascal Krietede8f4092009-07-29 13:46:37 +000080 $r .= <<<EOF
Pascal Krietede8f4092009-07-29 13:46:37 +000081 var smiley_map = {$m};
82
83 function insert_smiley(smiley, field_id) {
84 var el = document.getElementById(field_id), newStart;
Barry Mienydd671972010-10-04 16:33:58 +020085
Pascal Krietede8f4092009-07-29 13:46:37 +000086 if ( ! el && smiley_map[field_id]) {
87 el = document.getElementById(smiley_map[field_id]);
Barry Mienydd671972010-10-04 16:33:58 +020088
Pascal Krietede8f4092009-07-29 13:46:37 +000089 if ( ! el)
90 return false;
91 }
Barry Mienydd671972010-10-04 16:33:58 +020092
Pascal Krietede8f4092009-07-29 13:46:37 +000093 el.focus();
94 smiley = " " + smiley;
95
96 if ('selectionStart' in el) {
97 newStart = el.selectionStart + smiley.length;
98
99 el.value = el.value.substr(0, el.selectionStart) +
100 smiley +
101 el.value.substr(el.selectionEnd, el.value.length);
102 el.setSelectionRange(newStart, newStart);
103 }
104 else if (document.selection) {
Pascal Kriete986e1722009-11-09 20:51:15 +0000105 document.selection.createRange().text = smiley;
Pascal Krietede8f4092009-07-29 13:46:37 +0000106 }
107 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000108EOF;
Pascal Krietede8f4092009-07-29 13:46:37 +0000109 }
110 else
111 {
112 if (is_array($alias))
113 {
Pascal Kriete45e3cdf2011-02-14 13:26:20 -0500114 foreach ($alias as $name => $id)
Pascal Krietede8f4092009-07-29 13:46:37 +0000115 {
116 $r .= 'smiley_map["'.$name.'"] = "'.$id.'";'."\n";
117 }
118 }
119 }
120
Greg Aker60e78c72010-04-20 12:45:14 -0500121 if ($inline)
122 {
Barry Mienydd671972010-10-04 16:33:58 +0200123 return '<script type="text/javascript" charset="utf-8">/*<![CDATA[ */'.$r.'// ]]></script>';
Greg Aker60e78c72010-04-20 12:45:14 -0500124 }
125 else
126 {
127 return $r;
128 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 }
130}
Pascal Krietede8f4092009-07-29 13:46:37 +0000131
Derek Allard2067d1a2008-11-13 22:59:24 +0000132// ------------------------------------------------------------------------
133
Timothy Warrenb75faa12012-04-27 12:03:32 -0400134
Derek Allard2067d1a2008-11-13 22:59:24 +0000135if ( ! function_exists('get_clickable_smileys'))
136{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400137 /**
138 * Get Clickable Smileys
139 *
140 * Returns an array of image tag links that can be clicked to be inserted
141 * into a form field.
142 *
143 * @param string the URL to the folder containing the smiley images
144 * @param array
145 * @param array
146 * @return array
147 */
Pascal Krietede8f4092009-07-29 13:46:37 +0000148 function get_clickable_smileys($image_url, $alias = '', $smileys = NULL)
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 {
Pascal Krietede8f4092009-07-29 13:46:37 +0000150 // For backward compatibility with js_insert_smiley
Barry Mienydd671972010-10-04 16:33:58 +0200151
Pascal Krietede8f4092009-07-29 13:46:37 +0000152 if (is_array($alias))
153 {
154 $smileys = $alias;
155 }
Barry Mienydd671972010-10-04 16:33:58 +0200156
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 if ( ! is_array($smileys))
158 {
159 if (FALSE === ($smileys = _get_smiley_array()))
160 {
161 return $smileys;
162 }
163 }
164
165 // Add a trailing slash to the file path if needed
Pascal Krietede8f4092009-07-29 13:46:37 +0000166 $image_url = rtrim($image_url, '/').'/';
Barry Mienydd671972010-10-04 16:33:58 +0200167
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 $used = array();
169 foreach ($smileys as $key => $val)
170 {
171 // Keep duplicates from being used, which can happen if the
Derek Jones37f4b9c2011-07-01 17:56:50 -0500172 // mapping array contains multiple identical replacements. For example:
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 // :-) and :) might be replaced with the same image so both smileys
174 // will be in the array.
175 if (isset($used[$smileys[$key][0]]))
176 {
177 continue;
178 }
Barry Mienydd671972010-10-04 16:33:58 +0200179
180 $link[] = "<a href=\"javascript:void(0);\" onclick=\"insert_smiley('".$key."', '".$alias."')\"><img src=\"".$image_url.$smileys[$key][0]."\" width=\"".$smileys[$key][1]."\" height=\"".$smileys[$key][2]."\" alt=\"".$smileys[$key][3]."\" style=\"border:0;\" /></a>";
181
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 $used[$smileys[$key][0]] = TRUE;
183 }
Barry Mienydd671972010-10-04 16:33:58 +0200184
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 return $link;
186 }
187}
188
189// ------------------------------------------------------------------------
190
Derek Allard2067d1a2008-11-13 22:59:24 +0000191if ( ! function_exists('parse_smileys'))
192{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400193 /**
194 * Parse Smileys
195 *
196 * Takes a string as input and swaps any contained smileys for the actual image
197 *
198 * @param string the text to be parsed
199 * @param string the URL to the folder containing the smiley images
200 * @param array
201 * @return string
202 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 function parse_smileys($str = '', $image_url = '', $smileys = NULL)
204 {
205 if ($image_url == '')
206 {
207 return $str;
208 }
209
210 if ( ! is_array($smileys))
211 {
212 if (FALSE === ($smileys = _get_smiley_array()))
213 {
214 return $str;
215 }
216 }
217
218 // Add a trailing slash to the file path if needed
Derek Jones37f4b9c2011-07-01 17:56:50 -0500219 $image_url = preg_replace("/(.+?)\/*$/", "\\1/", $image_url);
Derek Allard2067d1a2008-11-13 22:59:24 +0000220
221 foreach ($smileys as $key => $val)
222 {
223 $str = str_replace($key, "<img src=\"".$image_url.$smileys[$key][0]."\" width=\"".$smileys[$key][1]."\" height=\"".$smileys[$key][2]."\" alt=\"".$smileys[$key][3]."\" style=\"border:0;\" />", $str);
224 }
225
226 return $str;
227 }
228}
229
230// ------------------------------------------------------------------------
231
Derek Allard2067d1a2008-11-13 22:59:24 +0000232if ( ! function_exists('_get_smiley_array'))
233{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400234 /**
235 * Get Smiley Array
236 *
237 * Fetches the config/smiley.php file
238 *
239 * @return mixed
240 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 function _get_smiley_array()
242 {
Andrey Andreeve684bda2012-03-26 13:47:29 +0300243 if (defined('ENVIRONMENT') && file_exists(APPPATH.'config/'.ENVIRONMENT.'/smileys.php'))
Greg Akerd96f8822011-12-27 16:23:47 -0600244 {
245 include(APPPATH.'config/'.ENVIRONMENT.'/smileys.php');
246 }
247 elseif (file_exists(APPPATH.'config/smileys.php'))
248 {
249 include(APPPATH.'config/smileys.php');
250 }
Andrey Andreeve684bda2012-03-26 13:47:29 +0300251
252 if (isset($smileys) && is_array($smileys))
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 {
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100254 return $smileys;
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 }
256
Phil Sturgeon05fa6112011-04-06 22:57:43 +0100257 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 }
259}
260
Derek Allard2067d1a2008-11-13 22:59:24 +0000261/* End of file smiley_helper.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000262/* Location: ./system/helpers/smiley_helper.php */