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 | |
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 | |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 40 | if ( ! function_exists('smiley_js')) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 41 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 42 | /** |
| 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 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 | |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame^] | 58 | if ($alias !== '' && ! is_array($alias)) |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 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 | |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 68 | if (is_array($alias)) |
| 69 | { |
Pascal Kriete | 45e3cdf | 2011-02-14 13:26:20 -0500 | [diff] [blame] | 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.'"'; |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 73 | } |
| 74 | } |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 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; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 91 | el.focus(); |
| 92 | smiley = " " + smiley; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [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; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +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); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +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 | |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 122 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 123 | if ( ! function_exists('get_clickable_smileys')) |
| 124 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 125 | /** |
| 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 Warren | f0eb2f1 | 2012-05-17 08:32:11 -0400 | [diff] [blame] | 133 | * @param array |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 134 | * @return array |
| 135 | */ |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 136 | function get_clickable_smileys($image_url, $alias = '', $smileys = NULL) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 137 | { |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 138 | // For backward compatibility with js_insert_smiley |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 139 | if (is_array($alias)) |
| 140 | { |
| 141 | $smileys = $alias; |
| 142 | } |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame] | 143 | elseif (FALSE === ($smileys = _get_smiley_array())) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 144 | { |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 145 | return $smileys; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | // Add a trailing slash to the file path if needed |
Pascal Kriete | de8f409 | 2009-07-29 13:46:37 +0000 | [diff] [blame] | 149 | $image_url = rtrim($image_url, '/').'/'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 150 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 151 | $used = array(); |
| 152 | foreach ($smileys as $key => $val) |
| 153 | { |
| 154 | // Keep duplicates from being used, which can happen if the |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame] | 155 | // mapping array contains multiple identical replacements. For example: |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 156 | // :-) 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 162 | |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame] | 163 | $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] | 164 | $used[$smileys[$key][0]] = TRUE; |
| 165 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 166 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 167 | return $link; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | // ------------------------------------------------------------------------ |
| 172 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 173 | if ( ! function_exists('parse_smileys')) |
| 174 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 175 | /** |
| 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 185 | function parse_smileys($str = '', $image_url = '', $smileys = NULL) |
| 186 | { |
Alex Bilbie | 773ccc3 | 2012-06-02 11:11:08 +0100 | [diff] [blame^] | 187 | if ($image_url === '' OR ( ! is_array($smileys) && FALSE === ($smileys = _get_smiley_array()))) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 188 | { |
| 189 | return $str; |
| 190 | } |
| 191 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 192 | // Add a trailing slash to the file path if needed |
Andrey Andreev | 4921fed | 2012-01-07 01:28:07 +0200 | [diff] [blame] | 193 | $image_url = rtrim($image_url, '/').'/'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 194 | |
| 195 | foreach ($smileys as $key => $val) |
| 196 | { |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame] | 197 | $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] | 198 | } |
| 199 | |
| 200 | return $str; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | // ------------------------------------------------------------------------ |
| 205 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 206 | if ( ! function_exists('_get_smiley_array')) |
| 207 | { |
Timothy Warren | b75faa1 | 2012-04-27 12:03:32 -0400 | [diff] [blame] | 208 | /** |
| 209 | * Get Smiley Array |
| 210 | * |
| 211 | * Fetches the config/smiley.php file |
| 212 | * |
| 213 | * @return mixed |
| 214 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 215 | function _get_smiley_array() |
| 216 | { |
Andrey Andreev | e684bda | 2012-03-26 13:47:29 +0300 | [diff] [blame] | 217 | if (defined('ENVIRONMENT') && file_exists(APPPATH.'config/'.ENVIRONMENT.'/smileys.php')) |
Greg Aker | d96f882 | 2011-12-27 16:23:47 -0600 | [diff] [blame] | 218 | { |
| 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 Andreev | e684bda | 2012-03-26 13:47:29 +0300 | [diff] [blame] | 225 | |
Andrey Andreev | 09375d7 | 2012-01-19 14:57:46 +0200 | [diff] [blame] | 226 | return (isset($smileys) && is_array($smileys)) ? $smileys : FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 230 | /* End of file smiley_helper.php */ |
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 231 | /* Location: ./system/helpers/smiley_helper.php */ |