Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [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 | * |
Greg Aker | 741de1c | 2010-11-10 14:52:57 -0600 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.1.6 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 | 4921fed | 2012-01-07 01:28:07 +0200 | [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 | 4921fed | 2012-01-07 01:28:07 +0200 | [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 |
Andrey Andreev | c5a1f93 | 2012-01-24 15:29:02 +0200 | [diff] [blame^] | 12 | * bundled with this package in the files license.txt / license.rst. It is |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 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 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | /** |
| 29 | * CodeIgniter Smiley Helpers |
| 30 | * |
| 31 | * @package CodeIgniter |
| 32 | * @subpackage Helpers |
| 33 | * @category Helpers |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 34 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 35 | * @link http://codeigniter.com/user_guide/helpers/smiley_helper.html |
| 36 | */ |
| 37 | |
| 38 | // ------------------------------------------------------------------------ |
| 39 | |
| 40 | /** |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 41 | * Smiley Javascript |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 42 | * |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame] | 43 | * Returns the javascript required for the smiley insertion. Optionally takes |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 44 | * 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] | 45 | * |
| 46 | * @access public |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 47 | * @param mixed alias name or array of alias->field_id pairs |
| 48 | * @param string field_id if alias name was passed in |
| 49 | * @return array |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 50 | */ |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 51 | if ( ! function_exists('smiley_js')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 52 | { |
Greg Aker | 60e78c7 | 2010-04-20 12:45:14 -0500 | [diff] [blame] | 53 | function smiley_js($alias = '', $field_id = '', $inline = TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 54 | { |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 55 | static $do_setup = TRUE; |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 56 | $r = ''; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 57 | |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 58 | if ($alias != '' && ! is_array($alias)) |
| 59 | { |
| 60 | $alias = array($alias => $field_id); |
| 61 | } |
| 62 | |
| 63 | if ($do_setup === TRUE) |
| 64 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 65 | $do_setup = FALSE; |
| 66 | $m = array(); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 67 | |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 68 | if (is_array($alias)) |
| 69 | { |
| 70 | foreach ($alias as $name => $id) |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 71 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 72 | $m[] = '"'.$name.'" : "'.$id.'"'; |
| 73 | } |
| 74 | } |
| 75 | |
| 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; |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 89 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 90 | |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 91 | el.focus(); |
| 92 | smiley = " " + smiley; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 93 | |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 94 | if ('selectionStart' in el) { |
| 95 | newStart = el.selectionStart + smiley.length; |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 96 | |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 97 | el.value = el.value.substr(0, el.selectionStart) + |
| 98 | smiley + |
| 99 | el.value.substr(el.selectionEnd, el.value.length); |
| 100 | el.setSelectionRange(newStart, newStart); |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 101 | } |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 102 | else if (document.selection) { |
| 103 | document.selection.createRange().text = smiley; |
| 104 | } |
| 105 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 106 | EOF; |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 107 | } |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame] | 108 | elseif (is_array($alias)) |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 109 | { |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame] | 110 | foreach ($alias as $name => $id) |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 111 | { |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame] | 112 | $r .= 'smiley_map["'.$name.'"] = "'.$id."\";\n"; |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 116 | return ($inline) ? '<script type="text/javascript" charset="utf-8">/*<![CDATA[ */'.$r.'// ]]></script>' : $r; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 119 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 120 | // ------------------------------------------------------------------------ |
| 121 | |
| 122 | /** |
| 123 | * Get Clickable Smileys |
| 124 | * |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 125 | * Returns an array of image tag links that can be clicked to be inserted |
| 126 | * into a form field. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 127 | * |
| 128 | * @access public |
| 129 | * @param string the URL to the folder containing the smiley images |
| 130 | * @return array |
| 131 | */ |
| 132 | if ( ! function_exists('get_clickable_smileys')) |
| 133 | { |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 134 | function get_clickable_smileys($image_url, $alias = '', $smileys = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 135 | { |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 136 | // For backward compatibility with js_insert_smiley |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 137 | if (is_array($alias)) |
| 138 | { |
| 139 | $smileys = $alias; |
| 140 | } |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame] | 141 | elseif (FALSE === ($smileys = _get_smiley_array())) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 142 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 143 | return $smileys; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | // Add a trailing slash to the file path if needed |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 147 | $image_url = rtrim($image_url, '/').'/'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 148 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 149 | $used = array(); |
| 150 | foreach ($smileys as $key => $val) |
| 151 | { |
| 152 | // Keep duplicates from being used, which can happen if the |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame] | 153 | // mapping array contains multiple identical replacements. For example: |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 154 | // :-) and :) might be replaced with the same image so both smileys |
| 155 | // will be in the array. |
| 156 | if (isset($used[$smileys[$key][0]])) |
| 157 | { |
| 158 | continue; |
| 159 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 160 | |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame] | 161 | $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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 162 | $used[$smileys[$key][0]] = TRUE; |
| 163 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 164 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 165 | return $link; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | // ------------------------------------------------------------------------ |
| 170 | |
| 171 | /** |
| 172 | * Parse Smileys |
| 173 | * |
| 174 | * Takes a string as input and swaps any contained smileys for the actual image |
| 175 | * |
| 176 | * @access public |
| 177 | * @param string the text to be parsed |
| 178 | * @param string the URL to the folder containing the smiley images |
| 179 | * @return string |
| 180 | */ |
| 181 | if ( ! function_exists('parse_smileys')) |
| 182 | { |
| 183 | function parse_smileys($str = '', $image_url = '', $smileys = NULL) |
| 184 | { |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame] | 185 | if ($image_url == '' OR ( ! is_array($smileys) && FALSE === ($smileys = _get_smiley_array()))) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 186 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 187 | return $str; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | // Add a trailing slash to the file path if needed |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 191 | $image_url = rtrim($image_url, '/').'/'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 192 | |
| 193 | foreach ($smileys as $key => $val) |
| 194 | { |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame] | 195 | $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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | return $str; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | // ------------------------------------------------------------------------ |
| 203 | |
| 204 | /** |
| 205 | * Get Smiley Array |
| 206 | * |
| 207 | * Fetches the config/smiley.php file |
| 208 | * |
| 209 | * @access private |
| 210 | * @return mixed |
| 211 | */ |
| 212 | if ( ! function_exists('_get_smiley_array')) |
| 213 | { |
| 214 | function _get_smiley_array() |
| 215 | { |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame] | 216 | if (defined('ENVIRONMENT') && file_exists(APPPATH.'config/'.ENVIRONMENT.'/smileys.php')) |
Greg Aker | d96f882 | 2011-12-27 16:23:47 -0600 | [diff] [blame] | 217 | { |
| 218 | include(APPPATH.'config/'.ENVIRONMENT.'/smileys.php'); |
| 219 | } |
| 220 | elseif (file_exists(APPPATH.'config/smileys.php')) |
| 221 | { |
| 222 | include(APPPATH.'config/smileys.php'); |
| 223 | } |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 224 | |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame] | 225 | return (isset($smileys) && is_array($smileys)) ? $smileys : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 229 | // ------------------------------------------------------------------------ |
| 230 | |
| 231 | /** |
| 232 | * JS Insert Smiley |
| 233 | * |
| 234 | * Generates the javascript function needed to insert smileys into a form field |
| 235 | * |
| 236 | * DEPRECATED as of version 1.7.2, use smiley_js instead |
| 237 | * |
| 238 | * @access public |
| 239 | * @param string form name |
| 240 | * @param string field name |
| 241 | * @return string |
| 242 | */ |
| 243 | if ( ! function_exists('js_insert_smiley')) |
| 244 | { |
| 245 | function js_insert_smiley($form_name = '', $form_field = '') |
| 246 | { |
| 247 | return <<<EOF |
| 248 | <script type="text/javascript"> |
| 249 | function insert_smiley(smiley) |
| 250 | { |
| 251 | document.{$form_name}.{$form_field}.value += " " + smiley; |
| 252 | } |
| 253 | </script> |
| 254 | EOF; |
| 255 | } |
| 256 | } |
| 257 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 258 | /* End of file smiley_helper.php */ |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 259 | /* Location: ./system/helpers/smiley_helper.php */ |