CustomJsCode.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\Core\UI\Traits;
  3. use \ModulesGarden\ProxmoxAddon\Core\ModuleConstants;
  4. /**
  5. * Custom Js Code(per page) related functions
  6. * View Trat
  7. *
  8. * @author Sławomir Miśkowicz <slawomir@modulesgarden.com>
  9. */
  10. trait CustomJsCode
  11. {
  12. protected $customJsPath = null;
  13. public function setCustomJsCode()
  14. {
  15. $path = $this->getCustomJsPath(debug_backtrace()[1]['class'], debug_backtrace()[1]['function']);
  16. if ($path)
  17. {
  18. $this->customJsPath = $path;
  19. }
  20. return $this;
  21. }
  22. protected function getCustomJsPath($class, $function)
  23. {
  24. $ctrlPos = stripos($class, '\App\Http\\');
  25. if ($ctrlPos)
  26. {
  27. $ctrl = substr($class, $ctrlPos + 10);
  28. $parts = explode('\\', $ctrl);
  29. $tmpType = $parts[0];
  30. unset($parts[0]);
  31. $parts[] = $function;
  32. array_walk($parts, function(&$value, $key)
  33. {
  34. $value = lcfirst($value);
  35. });
  36. array_unshift($parts, 'app', 'UI', $tmpType, 'Templates');
  37. $file = call_user_func_array([ModuleConstants::class, 'getFullPath'], $parts) . '.js';
  38. if (file_exists($file))
  39. {
  40. return $file;
  41. }
  42. return null;
  43. }
  44. return null;
  45. }
  46. /**
  47. * @return string
  48. */
  49. public function getCustomJsCode()
  50. {
  51. if ($this->customJsPath && file_exists($this->customJsPath))
  52. {
  53. return file_get_contents($this->customJsPath);
  54. }
  55. }
  56. }