blob: 25962eb54fb735867e99f895b8071a53b01b71ef [file] [log] [blame]
Derek Allardd2df9bc2007-04-15 17:41:17 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
Derek Allard3d879d52008-01-18 19:41:32 +00008 * @author ExpressionEngine Dev Team
Derek Allardd2df9bc2007-04-15 17:41:17 +00009 * @copyright Copyright (c) 2006, EllisLab, Inc.
Derek Jones7a9193a2008-01-21 18:39:20 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
Derek Allardd2df9bc2007-04-15 17:41:17 +000012 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * CodeIgniter Smiley Helpers
20 *
21 * @package CodeIgniter
22 * @subpackage Helpers
23 * @category Helpers
Derek Allard3d879d52008-01-18 19:41:32 +000024 * @author ExpressionEngine Dev Team
Derek Jones7a9193a2008-01-21 18:39:20 +000025 * @link http://codeigniter.com/user_guide/helpers/smiley_helper.html
Derek Allardd2df9bc2007-04-15 17:41:17 +000026 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * JS Insert Smiley
32 *
33 * Generates the javascrip function needed to insert smileys into a form field
34 *
35 * @access public
36 * @param string form name
37 * @param string field name
38 * @return string
39 */
Derek Jones269b9422008-01-28 21:00:20 +000040if (! function_exists('js_insert_smiley'))
Derek Allardd2df9bc2007-04-15 17:41:17 +000041{
Derek Jones269b9422008-01-28 21:00:20 +000042 function js_insert_smiley($form_name = '', $form_field = '')
43 {
44 return <<<EOF
Derek Allardd2df9bc2007-04-15 17:41:17 +000045<script type="text/javascript">
46 function insert_smiley(smiley)
47 {
48 document.{$form_name}.{$form_field}.value += " " + smiley;
49 }
50</script>
51EOF;
Derek Jones269b9422008-01-28 21:00:20 +000052 }
53}
Derek Allardd2df9bc2007-04-15 17:41:17 +000054// ------------------------------------------------------------------------
55
56/**
57 * Get Clickable Smileys
58 *
59 * Returns an array of image tag links that can be clicked to be inserted
60 * into a form field.
61 *
62 * @access public
63 * @param string the URL to the folder containing the smiley images
64 * @return array
65 */
Derek Jones269b9422008-01-28 21:00:20 +000066if (! function_exists('get_clickable_smileys'))
Derek Allardd2df9bc2007-04-15 17:41:17 +000067{
Derek Jones269b9422008-01-28 21:00:20 +000068 function get_clickable_smileys($image_url = '', $smileys = NULL)
Derek Allardd2df9bc2007-04-15 17:41:17 +000069 {
Derek Jones269b9422008-01-28 21:00:20 +000070 if ( ! is_array($smileys))
Derek Allardd2df9bc2007-04-15 17:41:17 +000071 {
Derek Jones269b9422008-01-28 21:00:20 +000072 if (FALSE === ($smileys = _get_smiley_array()))
73 {
Derek Allard0975bcc2008-01-29 17:21:27 +000074 return $smileys;
Derek Jones269b9422008-01-28 21:00:20 +000075 }
76 }
Derek Allardd2df9bc2007-04-15 17:41:17 +000077
Derek Jones269b9422008-01-28 21:00:20 +000078 // Add a trailing slash to the file path if needed
79 $image_url = preg_replace("/(.+?)\/*$/", "\\1/", $image_url);
Derek Allardd2df9bc2007-04-15 17:41:17 +000080
Derek Jones269b9422008-01-28 21:00:20 +000081 $used = array();
82 foreach ($smileys as $key => $val)
Derek Allardd2df9bc2007-04-15 17:41:17 +000083 {
Derek Jones269b9422008-01-28 21:00:20 +000084 // Keep duplicates from being used, which can happen if the
85 // mapping array contains multiple identical replacements. For example:
86 // :-) and :) might be replaced with the same image so both smileys
87 // will be in the array.
88 if (isset($used[$smileys[$key][0]]))
89 {
90 continue;
91 }
92
93 $link[] = "<a href=\"javascript:void(0);\" onClick=\"insert_smiley('".$key."')\"><img src=\"".$image_url.$smileys[$key][0]."\" width=\"".$smileys[$key][1]."\" height=\"".$smileys[$key][2]."\" alt=\"".$smileys[$key][3]."\" style=\"border:0;\" /></a>";
94
95 $used[$smileys[$key][0]] = TRUE;
Derek Allardd2df9bc2007-04-15 17:41:17 +000096 }
97
Derek Jones269b9422008-01-28 21:00:20 +000098 return $link;
Derek Allardd2df9bc2007-04-15 17:41:17 +000099 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000100}
101
102// ------------------------------------------------------------------------
103
104/**
105 * Parse Smileys
106 *
107 * Takes a string as input and swaps any contained smileys for the actual image
108 *
109 * @access public
110 * @param string the text to be parsed
111 * @param string the URL to the folder containing the smiley images
112 * @return string
113 */
Derek Jones269b9422008-01-28 21:00:20 +0000114if (! function_exists('parse_smileys'))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000115{
Derek Jones269b9422008-01-28 21:00:20 +0000116 function parse_smileys($str = '', $image_url = '', $smileys = NULL)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000117 {
Derek Jones269b9422008-01-28 21:00:20 +0000118 if ($image_url == '')
Derek Allardd2df9bc2007-04-15 17:41:17 +0000119 {
120 return $str;
Derek Jones269b9422008-01-28 21:00:20 +0000121 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000122
Derek Jones269b9422008-01-28 21:00:20 +0000123 if ( ! is_array($smileys))
124 {
125 if (FALSE === ($smileys = _get_smiley_array()))
126 {
127 return $str;
128 }
129 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000130
Derek Jones269b9422008-01-28 21:00:20 +0000131 // Add a trailing slash to the file path if needed
132 $image_url = preg_replace("/(.+?)\/*$/", "\\1/", $image_url);
133
134 foreach ($smileys as $key => $val)
135 {
136 $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);
137 }
138
139 return $str;
140 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000141}
142
143// ------------------------------------------------------------------------
144
145/**
146 * Get Smiley Array
147 *
148 * Fetches the config/smiley.php file
149 *
150 * @access private
151 * @return mixed
152 */
Derek Jones269b9422008-01-28 21:00:20 +0000153if (! function_exists('_get_smiley_array'))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000154{
Derek Jones269b9422008-01-28 21:00:20 +0000155 function _get_smiley_array()
Derek Allardd2df9bc2007-04-15 17:41:17 +0000156 {
Derek Jones269b9422008-01-28 21:00:20 +0000157 if ( ! file_exists(APPPATH.'config/smileys'.EXT))
158 {
159 return FALSE;
160 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000161
Derek Jones269b9422008-01-28 21:00:20 +0000162 include(APPPATH.'config/smileys'.EXT);
Derek Allardd2df9bc2007-04-15 17:41:17 +0000163
Derek Jones269b9422008-01-28 21:00:20 +0000164 if ( ! isset($smileys) OR ! is_array($smileys))
165 {
166 return FALSE;
167 }
168
169 return $smileys;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000170 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000171}
172
adminc7e59992006-10-28 05:21:35 +0000173?>