| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * The file contains: Base controller class. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @package App | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @copyright YetiForce Sp. z o.o. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @license   YetiForce Public License 3.0 (licenses/LicenseEN.txt or yetiforce.com) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @author    Mariusz Krzaczkowski <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace App; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * Base controller class. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class Viewer extends \SmartyBC | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	const DEFAULTLAYOUT = 'Default'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	public static $currentLayout; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	 * Constructor - Sets the templateDir and compileDir for the Smarty files. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	 * @param <String> - $media Layout/Media name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	public function __construct($media = '') | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 		parent::__construct(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 		self::$currentLayout = self::getLayoutName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 		$templatesDir = ROOT_DIRECTORY . \DIRECTORY_SEPARATOR . 'layouts' . \DIRECTORY_SEPARATOR . self::getLayoutName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 		$customTemplate = ROOT_DIRECTORY . \DIRECTORY_SEPARATOR . 'custom' . \DIRECTORY_SEPARATOR . 'layouts' . \DIRECTORY_SEPARATOR . self::getLayoutName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 		$compileDir = ROOT_DIRECTORY . \DIRECTORY_SEPARATOR . 'cache' . \DIRECTORY_SEPARATOR . 'layouts' . \DIRECTORY_SEPARATOR . self::getLayoutName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 		if (!file_exists($compileDir)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 			mkdir($compileDir, 0777, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 		$this->setTemplateDir([$customTemplate, $templatesDir]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 		$this->setCompileDir($compileDir); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	 * Function to return for default layout name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	 * @return string - Default Layout Name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	public static function getLayoutName(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 		return Config::$theme ?? self::DEFAULTLAYOUT; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	 * Static function to get the Instance of the Class Object. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	 * @param <String> $media Layout/Media | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	 * @return Viewer instance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	public static function getInstance(string $media = '') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 		return new self($media); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	 * Function to display/fetch the smarty file contents. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 	 * @param string $templateName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	 * @param string $moduleName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	 * @param bool   $fetch | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	 * @return string|true - html data | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	public function view(string $templateName, string $moduleName = '', bool $fetch = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 		$templatePath = $this->getTemplatePath($templateName, $moduleName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 		$templateFound = $this->templateExists($templatePath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 		if ($templateFound) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 			if ($fetch) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 				return $this->fetch($templatePath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 			$this->display($templatePath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 			return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		throw new Exceptions\AppException("LBL_FILE_TEMPLATE_NOT_FOUND||{$templatePath}"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	 * Function to get the module specific template path for a given template. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 	 * @param string $templateName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 	 * @param string $moduleName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 	 * @return string - Module specific template path if exists, otherwise default template path for the given template name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 	public function getTemplatePath(string $templateName, string $moduleName = ''): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 		foreach ($this->getTemplateDir() as $templateDir) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 			$filePath = 'modules' . \DIRECTORY_SEPARATOR . $moduleName . \DIRECTORY_SEPARATOR . $templateName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 			$completeFilePath = $templateDir . $filePath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 			if (!empty($moduleName) && file_exists($completeFilePath)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 				break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 			$filePath = 'modules' . \DIRECTORY_SEPARATOR . 'Base' . \DIRECTORY_SEPARATOR . $templateName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 			if (file_exists($templateDir . $filePath)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 				break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 		return $filePath; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 	 * Truncating plain text and adding a button showing all the text. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 	 * @param string $text | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 	 * @param int    $length | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 	 * @param bool   $showIcon | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 	 * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 121 |  |  | 	 */ | 
            
                                                        
            
                                    
            
            
                | 122 |  |  | 	public static function truncateText(string $text, int $length, bool $showIcon = false): string | 
            
                                                        
            
                                    
            
            
                | 123 |  |  | 	{ | 
            
                                                        
            
                                    
            
            
                | 124 |  |  | 		if (\mb_strlen($text) < $length) { | 
            
                                                        
            
                                    
            
            
                | 125 |  |  | 			return $text; | 
            
                                                        
            
                                    
            
            
                | 126 |  |  | 		} | 
            
                                                        
            
                                    
            
            
                | 127 |  |  | 		$teaser = TextParser::textTruncate($text, $length); | 
            
                                                        
            
                                    
            
            
                | 128 |  |  | 		$text = nl2br($text); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 129 |  |  | 		if ($showIcon) { | 
            
                                                        
            
                                    
            
            
                | 130 |  |  | 			$btn = '<span class="mdi mdi-overscan"></span>'; | 
            
                                                        
            
                                    
            
            
                | 131 |  |  | 		} else { | 
            
                                                        
            
                                    
            
            
                | 132 |  |  | 			$btn = \App\Language::translate('LBL_MORE_BTN'); | 
            
                                                        
            
                                    
            
            
                | 133 |  |  | 		} | 
            
                                                        
            
                                    
            
            
                | 134 |  |  | 		return "<div class=\"js-more-content\"><span class=\"teaserContent\">$teaser</span><span class=\"fullContent d-none\">$text</span><span class=\"text-right mb-1\"><button type=\"button\" class=\"btn btn-link btn-sm pt-0 js-more\">{$btn}</button></span></div>"; | 
            
                                                        
            
                                    
            
            
                | 135 |  |  | 	} | 
            
                                                        
            
                                    
            
            
                | 136 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 137 |  |  |  | 
            
                        
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.