| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- <?php
- namespace ThurData\Servers\KerioEmail\Core\UI\Traits;
- use \ThurData\Servers\KerioEmail\Core\ModuleConstants;
- /**
- * Custom Js Code(per page) related functions
- * View Trait
- *
- * @autor ThurData <info@thrudata.ch>
- */
- trait CustomJsCode
- {
- protected $customJsPath = null;
- protected $customCssPath = null;
- protected function getCustomAssetPath($class, $function, $assetType = 'js', $fileType = 'js', $forceReturn = false)
- {
- $ctrlPos = stripos($class, '\App\Http\\');
- if ($ctrlPos)
- {
- $ctrl = substr($class, $ctrlPos + 10);
- $parts = explode('\\', $ctrl);
- $tmpType = $parts[0];
- unset($parts[0]);
- $parts[] = $function;
- array_walk($parts, function(&$value, $key){
- $value = lcfirst($value);
- });
- array_unshift($parts, 'app', 'UI', $tmpType, 'Templates', 'assets', $assetType);
- $file = call_user_func_array([ModuleConstants::class, 'getFullPath'], $parts) . '.' . $fileType;
- if (file_exists($file) || $forceReturn === true)
- {
- return $file;
- }
- return null;
- }
- return null;
- }
- /**
- * @return string
- */
- public function getCustomJsCode ()
- {
- if ($this->customJsPath && file_exists($this->customJsPath))
- {
- if (!$this->isDebugOn())
- {
- return file_get_contents($this->customJsPath);
- }
- return PHP_EOL . '/*' . PHP_EOL . ' * File: ' . $this->trimPath($this->customJsPath) . PHP_EOL . '*/' . PHP_EOL .
- file_get_contents($this->customJsPath) . PHP_EOL .
- '/*' . PHP_EOL . ' * End of ' . $this->trimPath($this->customJsPath) . PHP_EOL . '*/' . PHP_EOL;
- }
- }
- /**
- * @return string
- */
- public function getCustomCssCode ()
- {
- if ($this->customCssPath && file_exists($this->customCssPath))
- {
- if (!$this->isDebugOn())
- {
- return file_get_contents($this->customCssPath);
- }
- return PHP_EOL . '/*' . PHP_EOL . ' * File: ' . $this->trimPath($this->customCssPath) . PHP_EOL . '*/' . PHP_EOL .
- file_get_contents($this->customCssPath) . PHP_EOL .
- '/*' . PHP_EOL . ' * End of ' . $this->trimPath($this->customCssPath) . PHP_EOL . '*/' . PHP_EOL;
- }
- }
- public function trimPath($path)
- {
- if (stripos($path, 'app' . DIRECTORY_SEPARATOR . 'UI') !== false)
- {
- $parts = explode('app' . DIRECTORY_SEPARATOR . 'UI', $path);
- $path = '...' . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'UI' . end($parts);
- }
- return $path;
- }
- public function initCustomAssetFiles()
- {
- $this->customJsPath = null;
- $this->customCssPath = null;
- $controlerName = $this->getAppParam(($this->isIntegration() ? 'IntegrationControlerName' : 'HttpControlerName'));
- $method = $this->getAppParam(($this->isIntegration() ? 'IntegrationControlerMethod' : 'HttpControlerMethod'));
- if (!is_string($controlerName) || !is_string($method) || $controlerName === '' || $method === '')
- {
- return false;
- }
- $this->customJsPath = $this->getCustomAssetPath($controlerName, $method);
- $this->customCssPath = $this->getCustomAssetPath($controlerName, $method, 'css', 'css');
- }
- public function debugGetAssetsPlacement()
- {
- $controlerName = $this->getAppParam(($this->isIntegration() ? 'IntegrationControlerName' : 'HttpControlerName'));
- $method = $this->getAppParam(($this->isIntegration() ? 'IntegrationControlerMethod' : 'HttpControlerMethod'));
- if (!is_string($controlerName) || !is_string($method) || $controlerName === '' || $method === '')
- {
- return false;
- }
- return [
- 'js' => $this->getCustomAssetPath($controlerName, $method, 'js', 'js', true),
- 'css' => $this->getCustomAssetPath($controlerName, $method, 'css', 'css', true)
- ];
- }
- }
|