Apply PSR-12 code style (#3886)
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / data / template / Template.class.php
1 <?php
2
3 namespace wcf\data\template;
4
5 use wcf\data\DatabaseObject;
6 use wcf\data\package\PackageCache;
7 use wcf\system\application\ApplicationHandler;
8 use wcf\system\WCF;
9 use wcf\util\FileUtil;
10
11 /**
12 * Represents a template.
13 *
14 * @author Alexander Ebert
15 * @copyright 2001-2019 WoltLab GmbH
16 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
17 * @package WoltLabSuite\Core\Data\Template
18 *
19 * @property-read int $templateID unique id of the template
20 * @property-read int $packageID id of the package which delivers the template
21 * @property-read string $templateName name of the template
22 * @property-read string $application abbreviation of the application to which the template belongs
23 * @property-read int|null $templateGroupID id of the template group to which the template belongs or `null` if the template belongs to no template group
24 * @property-read int $lastModificationTime timestamp at which the template has been edited the last time
25 */
26 class Template extends DatabaseObject
27 {
28 /**
29 * list of system critical templates
30 * @var string[]
31 */
32 protected static $systemCriticalTemplates = ['headIncludeJavaScript', 'wysiwyg', 'wysiwygToolbar'];
33
34 /** @noinspection PhpMissingParentConstructorInspection */
35
36 /**
37 * @inheritDoc
38 */
39 public function __construct($id, $row = null, ?DatabaseObject $object = null)
40 {
41 if ($id !== null) {
42 $sql = "SELECT template.*, template_group.templateGroupFolderName,
43 package.package
44 FROM wcf" . WCF_N . "_template template
45 LEFT JOIN wcf" . WCF_N . "_template_group template_group
46 ON (template_group.templateGroupID = template.templateGroupID)
47 LEFT JOIN wcf" . WCF_N . "_package package
48 ON (package.packageID = template.packageID)
49 WHERE template.templateID = ?";
50 $statement = WCF::getDB()->prepareStatement($sql);
51 $statement->execute([$id]);
52 $row = $statement->fetchArray();
53
54 if ($row !== false) {
55 // get relative directory of the template the application
56 // belongs to
57 if ($row['application'] != 'wcf') {
58 $application = ApplicationHandler::getInstance()->getApplication($row['application']);
59 } else {
60 $application = ApplicationHandler::getInstance()->getWCF();
61 }
62 $row['packageDir'] = PackageCache::getInstance()->getPackage($application->packageID)->packageDir;
63 } else {
64 $row = [];
65 }
66 } elseif ($object !== null) {
67 $row = $object->data;
68 }
69
70 $this->handleData($row);
71 }
72
73 /**
74 * Returns the path to this template.
75 *
76 * @return string
77 */
78 public function getPath()
79 {
80 /** @noinspection PhpUndefinedFieldInspection */
81 return FileUtil::getRealPath(WCF_DIR . $this->packageDir) . 'templates/' . $this->templateGroupFolderName . $this->templateName . '.tpl';
82 }
83
84 /**
85 * Returns the source of this template.
86 *
87 * @return string
88 */
89 public function getSource()
90 {
91 return @\file_get_contents($this->getPath());
92 }
93
94 /**
95 * Returns true if current template is considered system critical and
96 * may not be customized at any point.
97 *
98 * @return bool
99 */
100 public function canCopy()
101 {
102 if (self::isSystemCritical($this->templateName)) {
103 // system critical templates cannot be modified, because whatever the
104 // gain of a customized version is, the damage potential is much higher
105 return false;
106 }
107
108 return true;
109 }
110
111 /**
112 * Returns true if current template is considered system critical and
113 * may not be customized at any point.
114 *
115 * @param string $templateName
116 * @return bool
117 */
118 public static function isSystemCritical($templateName)
119 {
120 return \in_array($templateName, self::$systemCriticalTemplates);
121 }
122 }