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