blob: 4b491758ef12e0884d56a4381033edc3ab120c52 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
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 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * CodeIgniter Smiley Helpers
31 *
32 * @package CodeIgniter
33 * @subpackage Helpers
34 * @category Helpers
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @link http://codeigniter.com/user_guide/helpers/smiley_helper.html
37 */
38
39// ------------------------------------------------------------------------
40
Pascal Krietede8f4092009-07-29 13:46:37 +000041if ( ! function_exists('smiley_js'))
Derek Allard2067d1a2008-11-13 22:59:24 +000042{
Timothy Warrenb75faa12012-04-27 12:03:32 -040043 /**
44 * Smiley Javascript
45 *
46 * Returns the javascript required for the smiley insertion. Optionally takes
47 * an array of aliases to loosely couple the smiley array to the view.
48 *
49 * @param mixed alias name or array of alias->field_id pairs
50 * @param string field_id if alias name was passed in
51 * @param bool
52 * @return array
53 */
Greg Aker60e78c72010-04-20 12:45:14 -050054 function smiley_js($alias = '', $field_id = '', $inline = TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +000055 {
Pascal Krietede8f4092009-07-29 13:46:37 +000056 static $do_setup = TRUE;
Pascal Krietede8f4092009-07-29 13:46:37 +000057 $r = '';
Barry Mienydd671972010-10-04 16:33:58 +020058
Alex Bilbie773ccc32012-06-02 11:11:08 +010059 if ($alias !== '' && ! is_array($alias))
Pascal Krietede8f4092009-07-29 13:46:37 +000060 {
61 $alias = array($alias => $field_id);
62 }
63
64 if ($do_setup === TRUE)
65 {
Andrey Andreev4921fed2012-01-07 01:28:07 +020066 $do_setup = FALSE;
67 $m = array();
Barry Mienydd671972010-10-04 16:33:58 +020068
Pascal Krietede8f4092009-07-29 13:46:37 +000069 if (is_array($alias))
70 {
Pascal Kriete45e3cdf2011-02-14 13:26:20 -050071 foreach ($alias as $name => $id)
Pascal Krietede8f4092009-07-29 13:46:37 +000072 {
Andrey Andreev4921fed2012-01-07 01:28:07 +020073 $m[] = '"'.$name.'" : "'.$id.'"';
Pascal Krietede8f4092009-07-29 13:46:37 +000074 }
75 }
Andrey Andreev4921fed2012-01-07 01:28:07 +020076
77 $m = '{'.implode(',', $m).'}';
78
79 $r .= <<<EOF
80 var smiley_map = {$m};
81
82 function insert_smiley(smiley, field_id) {
83 var el = document.getElementById(field_id), newStart;
84
85 if ( ! el && smiley_map[field_id]) {
86 el = document.getElementById(smiley_map[field_id]);
87
88 if ( ! el)
89 return false;
Derek Allard2067d1a2008-11-13 22:59:24 +000090 }
91
Andrey Andreev4921fed2012-01-07 01:28:07 +020092 el.focus();
93 smiley = " " + smiley;
Derek Allard2067d1a2008-11-13 22:59:24 +000094
Andrey Andreev4921fed2012-01-07 01:28:07 +020095 if ('selectionStart' in el) {
96 newStart = el.selectionStart + smiley.length;
Derek Allard2067d1a2008-11-13 22:59:24 +000097
Andrey Andreev4921fed2012-01-07 01:28:07 +020098 el.value = el.value.substr(0, el.selectionStart) +
99 smiley +
100 el.value.substr(el.selectionEnd, el.value.length);
101 el.setSelectionRange(newStart, newStart);
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 }
Andrey Andreev4921fed2012-01-07 01:28:07 +0200103 else if (document.selection) {
104 document.selection.createRange().text = smiley;
105 }
106 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000107EOF;
Pascal Krietede8f4092009-07-29 13:46:37 +0000108 }
Andrey Andreev09375d72012-01-19 14:57:46 +0200109 elseif (is_array($alias))
Pascal Krietede8f4092009-07-29 13:46:37 +0000110 {
Andrey Andreev09375d72012-01-19 14:57:46 +0200111 foreach ($alias as $name => $id)
Pascal Krietede8f4092009-07-29 13:46:37 +0000112 {
Andrey Andreev09375d72012-01-19 14:57:46 +0200113 $r .= 'smiley_map["'.$name.'"] = "'.$id."\";\n";
Pascal Krietede8f4092009-07-29 13:46:37 +0000114 }
115 }
116
Andrey Andreev4921fed2012-01-07 01:28:07 +0200117 return ($inline) ? '<script type="text/javascript" charset="utf-8">/*<![CDATA[ */'.$r.'// ]]></script>' : $r;
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 }
119}
Pascal Krietede8f4092009-07-29 13:46:37 +0000120
Derek Allard2067d1a2008-11-13 22:59:24 +0000121// ------------------------------------------------------------------------
122
Timothy Warrenb75faa12012-04-27 12:03:32 -0400123
Derek Allard2067d1a2008-11-13 22:59:24 +0000124if ( ! function_exists('get_clickable_smileys'))
125{
Timothy Warrenb75faa12012-04-27 12:03:32 -0400126 /**
127 * Get Clickable Smileys
128 *
129 * Returns an array of image tag links that can be clicked to be inserted
130 * into a form field.
131 *
132 * @param string the URL to the folder containing the smiley images
133 * @param array
Timothy Warrenb75faa12012-04-27 12:03:32 -0400134 * @return array
135 */
Andrey Andreev53b8ef52012-11-08 21:38:53 +0200136 function get_clickable_smileys($image_url, $alias = '')
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 Andreev53b8ef52012-11-08 21:38:53 +0200145 return FALSE;
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 */