CustomJsCode.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\Core\UI\Traits;
  3. use \ThurData\Servers\KerioEmail\Core\ModuleConstants;
  4. /**
  5. * Custom Js Code(per page) related functions
  6. * View Trait
  7. *
  8. * @autor ThurData <info@thrudata.ch>
  9. */
  10. trait CustomJsCode
  11. {
  12. protected $customJsPath = null;
  13. protected $customCssPath = null;
  14. protected function getCustomAssetPath($class, $function, $assetType = 'js', $fileType = 'js', $forceReturn = false)
  15. {
  16. $ctrlPos = stripos($class, '\App\Http\\');
  17. if ($ctrlPos)
  18. {
  19. $ctrl = substr($class, $ctrlPos + 10);
  20. $parts = explode('\\', $ctrl);
  21. $tmpType = $parts[0];
  22. unset($parts[0]);
  23. $parts[] = $function;
  24. array_walk($parts, function(&$value, $key){
  25. $value = lcfirst($value);
  26. });
  27. array_unshift($parts, 'app', 'UI', $tmpType, 'Templates', 'assets', $assetType);
  28. $file = call_user_func_array([ModuleConstants::class, 'getFullPath'], $parts) . '.' . $fileType;
  29. if (file_exists($file) || $forceReturn === true)
  30. {
  31. return $file;
  32. }
  33. return null;
  34. }
  35. return null;
  36. }
  37. /**
  38. * @return string
  39. */
  40. public function getCustomJsCode ()
  41. {
  42. if ($this->customJsPath && file_exists($this->customJsPath))
  43. {
  44. if (!$this->isDebugOn())
  45. {
  46. return file_get_contents($this->customJsPath);
  47. }
  48. return PHP_EOL . '/*' . PHP_EOL . ' * File: ' . $this->trimPath($this->customJsPath) . PHP_EOL . '*/' . PHP_EOL .
  49. file_get_contents($this->customJsPath) . PHP_EOL .
  50. '/*' . PHP_EOL . ' * End of ' . $this->trimPath($this->customJsPath) . PHP_EOL . '*/' . PHP_EOL;
  51. }
  52. }
  53. /**
  54. * @return string
  55. */
  56. public function getCustomCssCode ()
  57. {
  58. if ($this->customCssPath && file_exists($this->customCssPath))
  59. {
  60. if (!$this->isDebugOn())
  61. {
  62. return file_get_contents($this->customCssPath);
  63. }
  64. return PHP_EOL . '/*' . PHP_EOL . ' * File: ' . $this->trimPath($this->customCssPath) . PHP_EOL . '*/' . PHP_EOL .
  65. file_get_contents($this->customCssPath) . PHP_EOL .
  66. '/*' . PHP_EOL . ' * End of ' . $this->trimPath($this->customCssPath) . PHP_EOL . '*/' . PHP_EOL;
  67. }
  68. }
  69. public function trimPath($path)
  70. {
  71. if (stripos($path, 'app' . DIRECTORY_SEPARATOR . 'UI') !== false)
  72. {
  73. $parts = explode('app' . DIRECTORY_SEPARATOR . 'UI', $path);
  74. $path = '...' . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'UI' . end($parts);
  75. }
  76. return $path;
  77. }
  78. public function initCustomAssetFiles()
  79. {
  80. $this->customJsPath = null;
  81. $this->customCssPath = null;
  82. $controlerName = $this->getAppParam(($this->isIntegration() ? 'IntegrationControlerName' : 'HttpControlerName'));
  83. $method = $this->getAppParam(($this->isIntegration() ? 'IntegrationControlerMethod' : 'HttpControlerMethod'));
  84. if (!is_string($controlerName) || !is_string($method) || $controlerName === '' || $method === '')
  85. {
  86. return false;
  87. }
  88. $this->customJsPath = $this->getCustomAssetPath($controlerName, $method);
  89. $this->customCssPath = $this->getCustomAssetPath($controlerName, $method, 'css', 'css');
  90. }
  91. public function debugGetAssetsPlacement()
  92. {
  93. $controlerName = $this->getAppParam(($this->isIntegration() ? 'IntegrationControlerName' : 'HttpControlerName'));
  94. $method = $this->getAppParam(($this->isIntegration() ? 'IntegrationControlerMethod' : 'HttpControlerMethod'));
  95. if (!is_string($controlerName) || !is_string($method) || $controlerName === '' || $method === '')
  96. {
  97. return false;
  98. }
  99. return [
  100. 'js' => $this->getCustomAssetPath($controlerName, $method, 'js', 'js', true),
  101. 'css' => $this->getCustomAssetPath($controlerName, $method, 'css', 'css', true)
  102. ];
  103. }
  104. }