blob: 0f426d609f75696e0f9d42eeed0f6a09c562d240 [file] [log] [blame]
adminc7e59992006-10-28 05:21:35 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * Code Igniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author Rick Ellis
9 * @copyright Copyright (c) 2006, pMachine, Inc.
10 * @license http://www.codeignitor.com/user_guide/license.html
11 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Code Igniter Smiley Helpers
20 *
21 * @package CodeIgniter
22 * @subpackage Helpers
23 * @category Helpers
24 * @author Rick Ellis
adminbd56abb2006-10-28 18:00:37 +000025 * @link http://www.codeigniter.com/user_guide/helpers/smiley_helper.html
adminc7e59992006-10-28 05:21:35 +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 */
40function js_insert_smiley($form_name = '', $form_field = '')
41{
42return <<<EOF
43<script type="text/javascript">
44 function insert_smiley(smiley)
45 {
46 document.{$form_name}.{$form_field}.value += " " + smiley;
47 }
48</script>
49EOF;
50}
51
52// ------------------------------------------------------------------------
53
54/**
55 * Get Clickable Smileys
56 *
57 * Returns an array of image tag links that can be clicked to be inserted
58 * into a form field.
59 *
60 * @access public
61 * @param string the URL to the folder containing the smiley images
62 * @return array
63 */
adminbd56abb2006-10-28 18:00:37 +000064function get_clickable_smileys($image_url = '', $smileys = NULL)
adminc7e59992006-10-28 05:21:35 +000065{
adminbd56abb2006-10-28 18:00:37 +000066 if ( ! is_array($smileys))
adminc7e59992006-10-28 05:21:35 +000067 {
adminbd56abb2006-10-28 18:00:37 +000068 if (FALSE === ($smileys = _get_smiley_array()))
69 {
70 return $str;
71 }
72 }
adminc7e59992006-10-28 05:21:35 +000073
74 // Add a trailing slash to the file path if needed
75 $image_url = preg_replace("/(.+?)\/*$/", "\\1/", $image_url);
76
77 $used = array();
78 foreach ($smileys as $key => $val)
79 {
80 // Keep duplicates from being used, which can happen if the
81 // mapping array contains multiple identical replacements. For example:
82 // :-) and :) might be replaced with the same image so both smileys
83 // will be in the array.
adminbd56abb2006-10-28 18:00:37 +000084 if (isset($used[$smileys[$key][0]]))
adminc7e59992006-10-28 05:21:35 +000085 {
86 continue;
87 }
88
adminbd56abb2006-10-28 18:00:37 +000089 $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>";
adminc7e59992006-10-28 05:21:35 +000090
adminbd56abb2006-10-28 18:00:37 +000091 $used[$smileys[$key][0]] = TRUE;
adminc7e59992006-10-28 05:21:35 +000092 }
93
94 return $link;
95}
96
97// ------------------------------------------------------------------------
98
99/**
100 * Parse Smileys
101 *
102 * Takes a string as input and swaps any contained smileys for the actual image
103 *
104 * @access public
105 * @param string the text to be parsed
106 * @param string the URL to the folder containing the smiley images
107 * @return string
108 */
adminbd56abb2006-10-28 18:00:37 +0000109function parse_smileys($str = '', $image_url = '', $smileys = NULL)
adminc7e59992006-10-28 05:21:35 +0000110{
adminbd56abb2006-10-28 18:00:37 +0000111 if ($image_url == '')
adminc7e59992006-10-28 05:21:35 +0000112 {
113 return $str;
adminbd56abb2006-10-28 18:00:37 +0000114 }
adminc7e59992006-10-28 05:21:35 +0000115
adminbd56abb2006-10-28 18:00:37 +0000116 if ( ! is_array($smileys))
117 {
118 if (FALSE === ($smileys = _get_smiley_array()))
119 {
120 return $str;
121 }
122 }
123
adminc7e59992006-10-28 05:21:35 +0000124 // Add a trailing slash to the file path if needed
125 $image_url = preg_replace("/(.+?)\/*$/", "\\1/", $image_url);
126
127 foreach ($smileys as $key => $val)
128 {
adminbd56abb2006-10-28 18:00:37 +0000129 $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);
adminc7e59992006-10-28 05:21:35 +0000130 }
131
132 return $str;
133}
134
135// ------------------------------------------------------------------------
136
137/**
138 * Get Smiley Array
139 *
140 * Fetches the config/smiley.php file
141 *
142 * @access private
143 * @return mixed
144 */
145function _get_smiley_array()
146{
147 if ( ! file_exists(APPPATH.'config/smileys'.EXT))
148 {
149 return FALSE;
150 }
151
152 include_once(APPPATH.'config/smileys'.EXT);
153
154 if ( ! isset($smileys) OR ! is_array($smileys))
155 {
156 return FALSE;
157 }
158
159 return $smileys;
160}
161
162
163
164
165?>