PageRenderTime 1384ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/_add-ons/redactor/hooks.redactor.php

https://bitbucket.org/sirestudios/fortis-wellness
PHP | 165 lines | 113 code | 47 blank | 5 comment | 28 complexity | 8d4bec69bf5424665b80001005934208 MD5 | raw file
Possible License(s): JSON
  1. <?php
  2. class Hooks_redactor extends Hooks
  3. {
  4. public function control_panel__add_to_head()
  5. {
  6. if (URL::getCurrent(false) == '/publish') {
  7. return $this->css->link(array('redactor.css', 'override.css'));
  8. }
  9. }
  10. public function control_panel__add_to_foot()
  11. {
  12. if (URL::getCurrent(false) == '/publish') {
  13. $html = $this->js->link(array('fullscreen.js', 'redactor.min.js'));
  14. $options = $this->getConfig();
  15. # Load image browser folder
  16. if (class_exists('Fieldtype_redactor') && method_exists('Fieldtype_redactor', 'get_field_settings')) {
  17. $field_settings = Fieldtype_redactor::get_field_settings();
  18. $field_config = array_get($field_settings, 'field_config', $field_settings);
  19. if (array_get($field_config, 'image_dir', false)) {
  20. $image_path = Path::tidy($field_config['image_dir'].'/');
  21. $options['imageGetJson'] = Config::getSiteRoot()."TRIGGER/redactor/fetch_images?path={$image_path}";
  22. $options['imageUpload'] = Config::getSiteRoot()."TRIGGER/redactor/upload?path={$image_path}";
  23. }
  24. if (array_get($field_config, 'file_dir', false)) {
  25. $file_path = Path::tidy($field_config['file_dir'].'/');
  26. $options['fileUpload'] = Config::getSiteRoot()."TRIGGER/redactor/upload?path={$file_path}";
  27. }
  28. if (isset($field_config['image_dir_append_slug'])) {
  29. $options['uploadFields'] = array(
  30. 'subfolder' => '#publish-slug'
  31. );
  32. }
  33. }
  34. $redactor_options = json_encode($options, true);
  35. $html .= "<script>
  36. var redactor_options = $redactor_options;
  37. $(document).ready(
  38. function() {
  39. $.extend(redactor_options, {'imageUploadErrorCallback': callback});
  40. $('.redactor-container textarea').redactor(redactor_options);
  41. }
  42. );
  43. $('body').on('addRow', '.grid', function() {
  44. $.extend(redactor_options, {'imageUploadErrorCallback': callback});
  45. $('.redactor-container textarea').redactor(redactor_options);
  46. });
  47. function callback(obj, json) {
  48. alert(json.error);
  49. }
  50. </script>";
  51. return $html;
  52. }
  53. }
  54. public function redactor__upload()
  55. {
  56. if ( ! Statamic_Auth::get_current_user()) {
  57. exit("Invalid Request");
  58. }
  59. $path = Request::get('path');
  60. if (isset($path)) {
  61. $dir = Path::tidy(ltrim($path, '/').'/');
  62. if (isset($_POST['subfolder'])) {
  63. $dir .= $_POST['subfolder'] . '/';
  64. }
  65. Folder::make($dir);
  66. $_FILES['file']['type'] = strtolower($_FILES['file']['type']);
  67. if ($_FILES['file']['type'] == 'image/png'
  68. || $_FILES['file']['type'] == 'image/jpg'
  69. || $_FILES['file']['type'] == 'image/gif'
  70. || $_FILES['file']['type'] == 'image/jpeg'
  71. || $_FILES['file']['type'] == 'application/pdf') {
  72. $file_info = pathinfo($_FILES['file']['name']);
  73. // pull out the filename bits
  74. $filename = $file_info['filename'];
  75. $ext = $file_info['extension'];
  76. // build filename
  77. $file = $dir.$filename.'.'.$ext;
  78. // check for dupes
  79. if (File::exists($file)) {
  80. $file = $dir.$filename.'-'.date('YmdHis').'.'.$ext;
  81. }
  82. if ( ! Folder::isWritable($dir)) {
  83. Log::error('Upload failed. Directory "' . $dir . '" is not writable.', 'redactor');
  84. echo json_encode(array('error' => "Redactor: Upload directory not writable."));
  85. die();
  86. }
  87. copy($_FILES['file']['tmp_name'], $file);
  88. // display file
  89. $array = array(
  90. 'filelink' => Config::getSiteRoot().$file
  91. );
  92. echo stripslashes(json_encode($array));
  93. } else {
  94. echo json_encode(array('error' => "Redactor: Could not find directory: '$dir'"));
  95. }
  96. } else {
  97. echo json_encode(array('error' => "Redactor: Upload directory not set."));
  98. }
  99. }
  100. public function redactor__fetch_images()
  101. {
  102. if ( ! Statamic_Auth::get_current_user()) {
  103. exit("Invalid Request");
  104. }
  105. $dir = Path::tidy(ltrim(Request::get('path'), '/').'/');
  106. $image_list = glob($dir."*.{jpg,jpeg,gif,png}", GLOB_BRACE);
  107. $images = array();
  108. if (count($image_list) > 0) {
  109. foreach ($image_list as $image) {
  110. $images[] = array(
  111. 'thumb' => Config::getSiteRoot().$image,
  112. 'image' => Config::getSiteRoot().$image
  113. );
  114. }
  115. }
  116. echo json_encode($images);
  117. }
  118. }