Advertisement
Guest User

ckeditor helper

a guest
Aug 5th, 2012
16,241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. <?php
  2. if(!defined('BASEPATH')) exit('No direct script access allowed');
  3.  
  4. /*
  5. * CKEditor helper for CodeIgniter
  6. *
  7. * @author Samuel Sanchez <samuel.sanchez.work@gmail.com> - http://kromack.com/
  8. * @package CodeIgniter
  9. * @license http://creativecommons.org/licenses/by-nc-sa/3.0/us/
  10. * @tutorial http://kromack.com/developpement-php/codeigniter/ckeditor-helper-for-codeigniter/
  11. * @see http://codeigniter.com/forums/viewthread/127374/
  12. * @version 2010-08-28
  13. *
  14. */
  15.  
  16. /**
  17. * This function adds once the CKEditor's config vars
  18. * @author Samuel Sanchez
  19. * @access private
  20. * @param array $data (default: array())
  21. * @return string
  22. */
  23. function cke_initialize($data = array()) {
  24.  
  25. $return = '';
  26.  
  27. if(!defined('CI_CKEDITOR_HELPER_LOADED')) {
  28.  
  29. define('CI_CKEDITOR_HELPER_LOADED', TRUE);
  30. $return = '<script type="text/javascript" src="'.base_url(). $data['path'] . '/ckeditor.js"></script>';
  31. $return .= "<script type=\"text/javascript\">CKEDITOR_BASEPATH = '" . base_url() . $data['path'] . "/';</script>";
  32. }
  33.  
  34. return $return;
  35.  
  36. }
  37.  
  38. /**
  39. * This function create JavaScript instances of CKEditor
  40. * @author Samuel Sanchez
  41. * @access private
  42. * @param array $data (default: array())
  43. * @return string
  44. */
  45. function cke_create_instance($data = array()) {
  46.  
  47. $return = "<script type=\"text/javascript\">
  48. CKEDITOR.replace('" . $data['id'] . "', {";
  49.  
  50. //Adding config values
  51. if(isset($data['config'])) {
  52. foreach($data['config'] as $k=>$v) {
  53.  
  54. // Support for extra config parameters
  55. if (is_array($v)) {
  56. $return .= $k . " : [";
  57. $return .= config_data($v);
  58. $return .= "]";
  59.  
  60. }
  61. else {
  62. $return .= $k . " : '" . $v . "'";
  63. }
  64.  
  65. if($k !== end(array_keys($data['config']))) {
  66. $return .= ",";
  67. }
  68. }
  69. }
  70.  
  71. $return .= '});</script>';
  72.  
  73. return $return;
  74.  
  75. }
  76.  
  77. /**
  78. * This function displays an instance of CKEditor inside a view
  79. * @author Samuel Sanchez
  80. * @access public
  81. * @param array $data (default: array())
  82. * @return string
  83. */
  84. function display_ckeditor($data = array())
  85. {
  86. // Initialization
  87. $return = cke_initialize($data);
  88.  
  89. // Creating a Ckeditor instance
  90. $return .= cke_create_instance($data);
  91.  
  92.  
  93. // Adding styles values
  94. if(isset($data['styles'])) {
  95.  
  96. $return .= "<script type=\"text/javascript\">CKEDITOR.addStylesSet( 'my_styles_" . $data['id'] . "', [";
  97.  
  98.  
  99. foreach($data['styles'] as $k=>$v) {
  100.  
  101. $return .= "{ name : '" . $k . "', element : '" . $v['element'] . "', styles : { ";
  102.  
  103. if(isset($v['styles'])) {
  104. foreach($v['styles'] as $k2=>$v2) {
  105.  
  106. $return .= "'" . $k2 . "' : '" . $v2 . "'";
  107.  
  108. if($k2 !== end(array_keys($v['styles']))) {
  109. $return .= ",";
  110. }
  111. }
  112. }
  113.  
  114. $return .= '} }';
  115.  
  116. if($k !== end(array_keys($data['styles']))) {
  117. $return .= ',';
  118. }
  119.  
  120.  
  121. }
  122.  
  123. $return .= ']);';
  124.  
  125. $return .= "CKEDITOR.instances['" . $data['id'] . "'].config.stylesCombo_stylesSet = 'my_styles_" . $data['id'] . "';
  126. </script>";
  127. }
  128.  
  129. return $return;
  130. }
  131.  
  132. /**
  133. * config_data function.
  134. * This function look for extra config data
  135. *
  136. * @author ronan
  137. * @link http://kromack.com/developpement-php/codeigniter/ckeditor-helper-for-codeigniter/comment-page-5/#comment-545
  138. * @access public
  139. * @param array $data. (default: array())
  140. * @return String
  141. */
  142. function config_data($data = array())
  143. {
  144. $return = '';
  145. foreach ($data as $key)
  146. {
  147. if (is_array($key)) {
  148. $return .= "[";
  149. foreach ($key as $string) {
  150. $return .= "'" . $string . "'";
  151. if ($string != end(array_values($key))) $return .= ",";
  152. }
  153. $return .= "]";
  154. }
  155. else {
  156. $return .= "'".$key."'";
  157. }
  158. if ($key != end(array_values($data))) $return .= ",";
  159.  
  160. }
  161. return $return;
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement