smiley_helper.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2011, 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 = '', $inline = TRUE)
  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 = smiley;
  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. if ($inline)
  94. {
  95. return '<script type="text/javascript" charset="utf-8">/*<![CDATA[ */'.$r.'// ]]></script>';
  96. }
  97. else
  98. {
  99. return $r;
  100. }
  101. }
  102. }
  103. // ------------------------------------------------------------------------
  104. /**
  105. * Get Clickable Smileys
  106. *
  107. * Returns an array of image tag links that can be clicked to be inserted
  108. * into a form field.
  109. *
  110. * @access public
  111. * @param string the URL to the folder containing the smiley images
  112. * @return array
  113. */
  114. if ( ! function_exists('get_clickable_smileys'))
  115. {
  116. function get_clickable_smileys($image_url, $alias = '', $smileys = NULL)
  117. {
  118. // For backward compatibility with js_insert_smiley
  119. if (is_array($alias))
  120. {
  121. $smileys = $alias;
  122. }
  123. if ( ! is_array($smileys))
  124. {
  125. if (FALSE === ($smileys = _get_smiley_array()))
  126. {
  127. return $smileys;
  128. }
  129. }
  130. // Add a trailing slash to the file path if needed
  131. $image_url = rtrim($image_url, '/').'/';
  132. $used = array();
  133. foreach ($smileys as $key => $val)
  134. {
  135. // Keep duplicates from being used, which can happen if the
  136. // mapping array contains multiple identical replacements. For example:
  137. // :-) and :) might be replaced with the same image so both smileys
  138. // will be in the array.
  139. if (isset($used[$smileys[$key][0]]))
  140. {
  141. continue;
  142. }
  143. $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>";
  144. $used[$smileys[$key][0]] = TRUE;
  145. }
  146. return $link;
  147. }
  148. }
  149. // ------------------------------------------------------------------------
  150. /**
  151. * Parse Smileys
  152. *
  153. * Takes a string as input and swaps any contained smileys for the actual image
  154. *
  155. * @access public
  156. * @param string the text to be parsed
  157. * @param string the URL to the folder containing the smiley images
  158. * @return string
  159. */
  160. if ( ! function_exists('parse_smileys'))
  161. {
  162. function parse_smileys($str = '', $image_url = '', $smileys = NULL)
  163. {
  164. if ($image_url == '')
  165. {
  166. return $str;
  167. }
  168. if ( ! is_array($smileys))
  169. {
  170. if (FALSE === ($smileys = _get_smiley_array()))
  171. {
  172. return $str;
  173. }
  174. }
  175. // Add a trailing slash to the file path if needed
  176. $image_url = preg_replace("/(.+?)\/*$/", "\\1/", $image_url);
  177. foreach ($smileys as $key => $val)
  178. {
  179. $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);
  180. }
  181. return $str;
  182. }
  183. }
  184. // ------------------------------------------------------------------------
  185. /**
  186. * Get Smiley Array
  187. *
  188. * Fetches the config/smiley.php file
  189. *
  190. * @access private
  191. * @return mixed
  192. */
  193. if ( ! function_exists('_get_smiley_array'))
  194. {
  195. function _get_smiley_array()
  196. {
  197. if (defined('ENVIRONMENT') AND file_exists(APPPATH.'config/'.ENVIRONMENT.'/smileys.php'))
  198. {
  199. include(APPPATH.'config/'.ENVIRONMENT.'/smileys.php');
  200. }
  201. elseif (file_exists(APPPATH.'config/smileys.php'))
  202. {
  203. include(APPPATH.'config/smileys.php');
  204. }
  205. if (isset($smileys) AND is_array($smileys))
  206. {
  207. return $smileys;
  208. }
  209. return FALSE;
  210. }
  211. }
  212. // ------------------------------------------------------------------------
  213. /**
  214. * JS Insert Smiley
  215. *
  216. * Generates the javascript function needed to insert smileys into a form field
  217. *
  218. * DEPRECATED as of version 1.7.2, use smiley_js instead
  219. *
  220. * @access public
  221. * @param string form name
  222. * @param string field name
  223. * @return string
  224. */
  225. if ( ! function_exists('js_insert_smiley'))
  226. {
  227. function js_insert_smiley($form_name = '', $form_field = '')
  228. {
  229. return <<<EOF
  230. <script type="text/javascript">
  231. function insert_smiley(smiley)
  232. {
  233. document.{$form_name}.{$form_field}.value += " " + smiley;
  234. }
  235. </script>
  236. EOF;
  237. }
  238. }
  239. /* End of file smiley_helper.php */
  240. /* Location: ./system/helpers/smiley_helper.php */