Andrey Andreev | e684bda | 2012-03-26 13:47:29 +0300 | [diff] [blame^] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | e684bda | 2012-03-26 13:47:29 +0300 | [diff] [blame^] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | e684bda | 2012-03-26 13:47:29 +0300 | [diff] [blame^] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
| 28 | // ------------------------------------------------------------------------ |
| 29 | |
| 30 | /** |
| 31 | * CodeIgniter Smiley Helpers |
| 32 | * |
| 33 | * @package CodeIgniter |
| 34 | * @subpackage Helpers |
| 35 | * @category Helpers |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 36 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 37 | * @link http://codeigniter.com/user_guide/helpers/smiley_helper.html |
| 38 | */ |
| 39 | |
| 40 | // ------------------------------------------------------------------------ |
| 41 | |
| 42 | /** |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 43 | * Smiley Javascript |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 44 | * |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 45 | * Returns the javascript required for the smiley insertion. Optionally takes |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 46 | * an array of aliases to loosely couple the smiley array to the view. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 47 | * |
| 48 | * @access public |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 49 | * @param mixed alias name or array of alias->field_id pairs |
| 50 | * @param string field_id if alias name was passed in |
| 51 | * @return array |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 52 | */ |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 53 | if ( ! function_exists('smiley_js')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 54 | { |
Greg Aker | 60e78c7 | 2010-04-20 12:45:14 -0500 | [diff] [blame] | 55 | function smiley_js($alias = '', $field_id = '', $inline = TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 56 | { |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 57 | static $do_setup = TRUE; |
| 58 | |
| 59 | $r = ''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 60 | |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 61 | if ($alias != '' && ! is_array($alias)) |
| 62 | { |
| 63 | $alias = array($alias => $field_id); |
| 64 | } |
| 65 | |
| 66 | if ($do_setup === TRUE) |
| 67 | { |
| 68 | $do_setup = FALSE; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 69 | |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 70 | $m = array(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 71 | |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 72 | if (is_array($alias)) |
| 73 | { |
Pascal Kriete | 45e3cdf | 2011-02-14 13:26:20 -0500 | [diff] [blame] | 74 | foreach ($alias as $name => $id) |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 75 | { |
| 76 | $m[] = '"'.$name.'" : "'.$id.'"'; |
| 77 | } |
| 78 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 79 | |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 80 | $m = '{'.implode(',', $m).'}'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 81 | |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 82 | $r .= <<<EOF |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 83 | var smiley_map = {$m}; |
| 84 | |
| 85 | function insert_smiley(smiley, field_id) { |
| 86 | var el = document.getElementById(field_id), newStart; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 87 | |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 88 | if ( ! el && smiley_map[field_id]) { |
| 89 | el = document.getElementById(smiley_map[field_id]); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 90 | |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 91 | if ( ! el) |
| 92 | return false; |
| 93 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 94 | |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 95 | el.focus(); |
| 96 | smiley = " " + smiley; |
| 97 | |
| 98 | if ('selectionStart' in el) { |
| 99 | newStart = el.selectionStart + smiley.length; |
| 100 | |
| 101 | el.value = el.value.substr(0, el.selectionStart) + |
| 102 | smiley + |
| 103 | el.value.substr(el.selectionEnd, el.value.length); |
| 104 | el.setSelectionRange(newStart, newStart); |
| 105 | } |
| 106 | else if (document.selection) { |
Pascal Kriete | 986e172 | 2009-11-09 20:51:15 +0000 | [diff] [blame] | 107 | document.selection.createRange().text = smiley; |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 108 | } |
| 109 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 110 | EOF; |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 111 | } |
| 112 | else |
| 113 | { |
| 114 | if (is_array($alias)) |
| 115 | { |
Pascal Kriete | 45e3cdf | 2011-02-14 13:26:20 -0500 | [diff] [blame] | 116 | foreach ($alias as $name => $id) |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 117 | { |
| 118 | $r .= 'smiley_map["'.$name.'"] = "'.$id.'";'."\n"; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
Greg Aker | 60e78c7 | 2010-04-20 12:45:14 -0500 | [diff] [blame] | 123 | if ($inline) |
| 124 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 125 | return '<script type="text/javascript" charset="utf-8">/*<![CDATA[ */'.$r.'// ]]></script>'; |
Greg Aker | 60e78c7 | 2010-04-20 12:45:14 -0500 | [diff] [blame] | 126 | } |
| 127 | else |
| 128 | { |
| 129 | return $r; |
| 130 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 133 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 134 | // ------------------------------------------------------------------------ |
| 135 | |
| 136 | /** |
| 137 | * Get Clickable Smileys |
| 138 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 139 | * Returns an array of image tag links that can be clicked to be inserted |
| 140 | * into a form field. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 141 | * |
| 142 | * @access public |
| 143 | * @param string the URL to the folder containing the smiley images |
| 144 | * @return array |
| 145 | */ |
| 146 | if ( ! function_exists('get_clickable_smileys')) |
| 147 | { |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 148 | function get_clickable_smileys($image_url, $alias = '', $smileys = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 149 | { |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 150 | // For backward compatibility with js_insert_smiley |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 151 | |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 152 | if (is_array($alias)) |
| 153 | { |
| 154 | $smileys = $alias; |
| 155 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 156 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 157 | 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 Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 166 | $image_url = rtrim($image_url, '/').'/'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 167 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 168 | $used = array(); |
| 169 | foreach ($smileys as $key => $val) |
| 170 | { |
| 171 | // Keep duplicates from being used, which can happen if the |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 172 | // mapping array contains multiple identical replacements. For example: |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 173 | // :-) 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 179 | |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 182 | $used[$smileys[$key][0]] = TRUE; |
| 183 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 184 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 185 | return $link; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | // ------------------------------------------------------------------------ |
| 190 | |
| 191 | /** |
| 192 | * Parse Smileys |
| 193 | * |
| 194 | * Takes a string as input and swaps any contained smileys for the actual image |
| 195 | * |
| 196 | * @access public |
| 197 | * @param string the text to be parsed |
| 198 | * @param string the URL to the folder containing the smiley images |
| 199 | * @return string |
| 200 | */ |
| 201 | if ( ! function_exists('parse_smileys')) |
| 202 | { |
| 203 | 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 Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 219 | $image_url = preg_replace("/(.+?)\/*$/", "\\1/", $image_url); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 220 | |
| 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 | |
| 232 | /** |
| 233 | * Get Smiley Array |
| 234 | * |
| 235 | * Fetches the config/smiley.php file |
| 236 | * |
| 237 | * @access private |
| 238 | * @return mixed |
| 239 | */ |
| 240 | if ( ! function_exists('_get_smiley_array')) |
| 241 | { |
| 242 | function _get_smiley_array() |
| 243 | { |
Andrey Andreev | e684bda | 2012-03-26 13:47:29 +0300 | [diff] [blame^] | 244 | if (defined('ENVIRONMENT') && file_exists(APPPATH.'config/'.ENVIRONMENT.'/smileys.php')) |
Greg Aker | d96f882 | 2011-12-27 16:23:47 -0600 | [diff] [blame] | 245 | { |
| 246 | include(APPPATH.'config/'.ENVIRONMENT.'/smileys.php'); |
| 247 | } |
| 248 | elseif (file_exists(APPPATH.'config/smileys.php')) |
| 249 | { |
| 250 | include(APPPATH.'config/smileys.php'); |
| 251 | } |
Andrey Andreev | e684bda | 2012-03-26 13:47:29 +0300 | [diff] [blame^] | 252 | |
| 253 | if (isset($smileys) && is_array($smileys)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 254 | { |
Phil Sturgeon | 05fa611 | 2011-04-06 22:57:43 +0100 | [diff] [blame] | 255 | return $smileys; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Phil Sturgeon | 05fa611 | 2011-04-06 22:57:43 +0100 | [diff] [blame] | 258 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 262 | // ------------------------------------------------------------------------ |
| 263 | |
| 264 | /** |
| 265 | * JS Insert Smiley |
| 266 | * |
| 267 | * Generates the javascript function needed to insert smileys into a form field |
| 268 | * |
| 269 | * DEPRECATED as of version 1.7.2, use smiley_js instead |
| 270 | * |
| 271 | * @access public |
| 272 | * @param string form name |
| 273 | * @param string field name |
| 274 | * @return string |
| 275 | */ |
| 276 | if ( ! function_exists('js_insert_smiley')) |
| 277 | { |
| 278 | function js_insert_smiley($form_name = '', $form_field = '') |
| 279 | { |
| 280 | return <<<EOF |
| 281 | <script type="text/javascript"> |
| 282 | function insert_smiley(smiley) |
| 283 | { |
| 284 | document.{$form_name}.{$form_field}.value += " " + smiley; |
| 285 | } |
| 286 | </script> |
| 287 | EOF; |
| 288 | } |
| 289 | } |
| 290 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 291 | /* End of file smiley_helper.php */ |
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 292 | /* Location: ./system/helpers/smiley_helper.php */ |