smiley_helper.php 6.4 KB

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