PageRenderTime 27ms CodeModel.GetById 16ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/halogy/helpers/smiley_helper.php

https://bitbucket.org/haloweb/halogy-1.0/
PHP | 273 lines | 135 code | 39 blank | 99 comment | 28 complexity | 336ea1e4299c1f196f98abe5e76145b7 MD5 | raw file
  1<?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
  8 * @author		ExpressionEngine Dev Team
  9 * @copyright	Copyright (c) 2008 - 2009, EllisLab, Inc.
 10 * @license		http://codeigniter.com/user_guide/license.html
 11 * @link		http://codeigniter.com
 12 * @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
 24 * @author		ExpressionEngine Dev Team
 25 * @link		http://codeigniter.com/user_guide/helpers/smiley_helper.html
 26 */
 27
 28// ------------------------------------------------------------------------
 29
 30/**
 31 * Smiley Javascript
 32 *
 33 * Returns the javascript required for the smiley insertion.  Optionally takes
 34 * an array of aliases to loosely couple the smiley array to the view.
 35 *
 36 * @access	public
 37 * @param	mixed	alias name or array of alias->field_id pairs
 38 * @param	string	field_id if alias name was passed in
 39 * @return	array
 40 */
 41if ( ! function_exists('smiley_js'))
 42{
 43	function smiley_js($alias = '', $field_id = '')
 44	{
 45		static $do_setup = TRUE;
 46
 47		$r = '';
 48	
 49		if ($alias != '' && ! is_array($alias))
 50		{
 51			$alias = array($alias => $field_id);
 52		}
 53
 54		if ($do_setup === TRUE)
 55		{
 56				$do_setup = FALSE;
 57			
 58				$m = array();
 59			
 60				if (is_array($alias))
 61				{
 62					foreach($alias as $name => $id)
 63					{
 64						$m[] = '"'.$name.'" : "'.$id.'"';
 65					}
 66				}
 67			
 68				$m = '{'.implode(',', $m).'}';
 69			
 70				$r .= <<<EOF
 71			
 72				var smiley_map = {$m};
 73
 74				function insert_smiley(smiley, field_id) {
 75					var el = document.getElementById(field_id), newStart;
 76				
 77					if ( ! el && smiley_map[field_id]) {
 78						el = document.getElementById(smiley_map[field_id]);
 79					
 80						if ( ! el)
 81							return false;
 82					}
 83				
 84					el.focus();
 85					smiley = " " + smiley;
 86
 87					if ('selectionStart' in el) {
 88						newStart = el.selectionStart + smiley.length;
 89
 90						el.value = el.value.substr(0, el.selectionStart) +
 91										smiley +
 92										el.value.substr(el.selectionEnd, el.value.length);
 93						el.setSelectionRange(newStart, newStart);
 94					}
 95					else if (document.selection) {
 96						document.selection.createRange().text = text;
 97					}
 98				}
 99EOF;
100		}
101		else
102		{
103			if (is_array($alias))
104			{
105				foreach($alias as $name => $id)
106				{
107					$r .= 'smiley_map["'.$name.'"] = "'.$id.'";'."\n";
108				}
109			}
110		}
111
112		return '<script type="text/javascript" charset="utf-8">'.$r.'</script>';
113	}
114}
115
116// ------------------------------------------------------------------------
117
118/**
119 * Get Clickable Smileys
120 *
121 * Returns an array of image tag links that can be clicked to be inserted 
122 * into a form field.  
123 *
124 * @access	public
125 * @param	string	the URL to the folder containing the smiley images
126 * @return	array
127 */
128if ( ! function_exists('get_clickable_smileys'))
129{
130	function get_clickable_smileys($image_url, $alias = '', $smileys = NULL)
131	{
132		// For backward compatibility with js_insert_smiley
133		
134		if (is_array($alias))
135		{
136			$smileys = $alias;
137		}
138		
139		if ( ! is_array($smileys))
140		{
141			if (FALSE === ($smileys = _get_smiley_array()))
142			{
143				return $smileys;
144			}
145		}
146
147		// Add a trailing slash to the file path if needed
148		$image_url = rtrim($image_url, '/').'/';
149	
150		$used = array();
151		foreach ($smileys as $key => $val)
152		{
153			// Keep duplicates from being used, which can happen if the
154			// mapping array contains multiple identical replacements.  For example:
155			// :-) and :) might be replaced with the same image so both smileys
156			// will be in the array.
157			if (isset($used[$smileys[$key][0]]))
158			{
159				continue;
160			}
161			
162			$link[] = "<a href=\"javascript:void(0);\" onClick=\"insert_smiley('".$key."', '".$alias."')\"><img src=\"".$image_url.$smileys[$key][0]."\" width=\"".$smileys[$key][1]."\" height=\"".$smileys[$key][2]."\" alt=\"".$smileys[$key][3]."\" style=\"border:0;\" /></a>";	
163	
164			$used[$smileys[$key][0]] = TRUE;
165		}
166	
167		return $link;
168	}
169}
170
171// ------------------------------------------------------------------------
172
173/**
174 * Parse Smileys
175 *
176 * Takes a string as input and swaps any contained smileys for the actual image
177 *
178 * @access	public
179 * @param	string	the text to be parsed
180 * @param	string	the URL to the folder containing the smiley images
181 * @return	string
182 */
183if ( ! function_exists('parse_smileys'))
184{
185	function parse_smileys($str = '', $image_url = '', $smileys = NULL)
186	{
187		if ($image_url == '')
188		{
189			return $str;
190		}
191
192		if ( ! is_array($smileys))
193		{
194			if (FALSE === ($smileys = _get_smiley_array()))
195			{
196				return $str;
197			}
198		}
199
200		// Add a trailing slash to the file path if needed
201		$image_url = preg_replace("/(.+?)\/*$/", "\\1/",  $image_url);
202
203		foreach ($smileys as $key => $val)
204		{
205			$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);
206		}
207
208		return $str;
209	}
210}
211
212// ------------------------------------------------------------------------
213
214/**
215 * Get Smiley Array
216 *
217 * Fetches the config/smiley.php file
218 *
219 * @access	private
220 * @return	mixed
221 */
222if ( ! function_exists('_get_smiley_array'))
223{
224	function _get_smiley_array()
225	{
226		if ( ! file_exists(APPPATH.'config/smileys'.EXT))
227		{
228			return FALSE;
229		}
230
231		include(APPPATH.'config/smileys'.EXT);
232
233		if ( ! isset($smileys) OR ! is_array($smileys))
234		{
235			return FALSE;
236		}
237
238		return $smileys;
239	}
240}
241
242// ------------------------------------------------------------------------
243
244/**
245 * JS Insert Smiley
246 *
247 * Generates the javascript function needed to insert smileys into a form field
248 *
249 * DEPRECATED as of version 1.7.2, use smiley_js instead
250 *
251 * @access	public
252 * @param	string	form name
253 * @param	string	field name
254 * @return	string
255 */
256if ( ! function_exists('js_insert_smiley'))
257{
258	function js_insert_smiley($form_name = '', $form_field = '')
259	{
260		return <<<EOF
261<script type="text/javascript">
262	function insert_smiley(smiley)
263	{
264		document.{$form_name}.{$form_field}.value += " " + smiley;
265	}
266</script>
267EOF;
268	}
269}
270
271
272/* End of file smiley_helper.php */
273/* Location: ./system/helpers/smiley_helper.php */