blob: 05ffb29b2000a504ecf9c558194a729c60786338 [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
25 * @link http://www.codeigniter.com/user_guide/helpers/download_helper.html
26 */
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 */
64function get_clickable_smileys($image_url = '')
65{
66 if (FALSE === ($smileys = _get_smiley_array()))
67 {
68 return array();
69 }
70
71 // Add a trailing slash to the file path if needed
72 $image_url = preg_replace("/(.+?)\/*$/", "\\1/", $image_url);
73
74 $used = array();
75 foreach ($smileys as $key => $val)
76 {
77 // Keep duplicates from being used, which can happen if the
78 // mapping array contains multiple identical replacements. For example:
79 // :-) and :) might be replaced with the same image so both smileys
80 // will be in the array.
81 if (isset($used[$smileys[$key]['0']]))
82 {
83 continue;
84 }
85
86 $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>";
87
88 $used[$smileys[$key]['0']] = TRUE;
89 }
90
91 return $link;
92}
93
94// ------------------------------------------------------------------------
95
96/**
97 * Parse Smileys
98 *
99 * Takes a string as input and swaps any contained smileys for the actual image
100 *
101 * @access public
102 * @param string the text to be parsed
103 * @param string the URL to the folder containing the smiley images
104 * @return string
105 */
106function parse_smileys($str, $image_url)
107{
108 if (FALSE === ($smileys = _get_smiley_array()))
109 {
110 return $str;
111 }
112
113 // Add a trailing slash to the file path if needed
114 $image_url = preg_replace("/(.+?)\/*$/", "\\1/", $image_url);
115
116 foreach ($smileys as $key => $val)
117 {
118 $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);
119 }
120
121 return $str;
122}
123
124// ------------------------------------------------------------------------
125
126/**
127 * Get Smiley Array
128 *
129 * Fetches the config/smiley.php file
130 *
131 * @access private
132 * @return mixed
133 */
134function _get_smiley_array()
135{
136 if ( ! file_exists(APPPATH.'config/smileys'.EXT))
137 {
138 return FALSE;
139 }
140
141 include_once(APPPATH.'config/smileys'.EXT);
142
143 if ( ! isset($smileys) OR ! is_array($smileys))
144 {
145 return FALSE;
146 }
147
148 return $smileys;
149}
150
151
152
153
154?>