1 <?php
2
3 4 5 6 7 8
9 class rex_template
10 {
11 private $id;
12
13 public function __construct($template_id)
14 {
15 $this->id = (int) $template_id;
16 }
17
18 public static function getDefaultId()
19 {
20 return rex_config::get('structure/content', 'default_template_id', 1);
21 }
22
23 public function getId()
24 {
25 return $this->id;
26 }
27
28 public function getFile()
29 {
30 if ($this->getId() < 1) {
31 return false;
32 }
33
34 $file = $this->getFilePath($this->getId());
35 if (!$file) {
36 return false;
37 }
38
39 if (!file_exists($file)) {
40
41 if (!$this->generate()) {
42 throw new rex_exception('Unable to generate rexTemplate with id "' . $this->getId() . '"');
43 }
44 }
45
46 return $file;
47 }
48
49 public static function getFilePath($template_id)
50 {
51 if ($template_id < 1) {
52 return false;
53 }
54
55 return self::getTemplatesDir() . '/' . $template_id . '.template';
56 }
57
58 public static function getTemplatesDir()
59 {
60 return rex_path::addonCache('templates');
61 }
62
63 public function getTemplate()
64 {
65 $file = $this->getFile();
66 if (!$file) {
67 return false;
68 }
69
70 return rex_file::get($file);
71 }
72
73 public function generate()
74 {
75 $template_id = $this->getId();
76
77 if ($template_id < 1) {
78 return false;
79 }
80
81 $sql = rex_sql::factory();
82 $qry = 'SELECT * FROM ' . rex::getTablePrefix() . 'template WHERE id = ' . $template_id;
83 $sql->setQuery($qry);
84
85 if ($sql->getRows() == 1) {
86 $templateFile = self::getFilePath($template_id);
87
88 $content = $sql->getValue('content');
89 $content = rex_var::parse($content, rex_var::ENV_FRONTEND, 'template');
90 if (rex_file::put($templateFile, $content) !== false) {
91 return true;
92 }
93 throw new rex_exception('Unable to generate template ' . $template_id . '!');
94 }
95 throw new rex_exception('Template with id "' . $template_id . '" does not exist!');
96 }
97
98 public function deleteCache()
99 {
100 if ($this->id < 1) {
101 return false;
102 }
103
104 $file = $this->getFilePath($this->getId());
105 rex_file::delete($file);
106 return true;
107 }
108
109 110 111 112 113 114 115 116 117 118
119 public static function getTemplatesForCategory($category_id, $ignore_inactive = true)
120 {
121 $ignore_inactive = $ignore_inactive ? 1 : 0;
122
123 $templates = [];
124 $t_sql = rex_sql::factory();
125 $t_sql->setQuery('select id,name,attributes from ' . rex::getTablePrefix() . 'template where active=' . $ignore_inactive . ' order by name');
126
127 if ($category_id < 1) {
128
129 foreach ($t_sql as $row) {
130 $attributes = $row->getArrayValue('attributes');
131 $categories = isset($attributes['categories']) ? $attributes['categories'] : [];
132 if (!is_array($categories) || (isset($categories['all']) && $categories['all'] == 1)) {
133 $templates[$row->getValue('id')] = $row->getValue('name');
134 }
135 }
136 } else {
137 if ($c = rex_category::get($category_id)) {
138 $path = $c->getPathAsArray();
139 $path[] = $category_id;
140 foreach ($t_sql as $row) {
141 $attributes = $row->getArrayValue('attributes');
142 $categories = isset($attributes['categories']) ? $attributes['categories'] : [];
143
144 if (!is_array($categories) || (isset($categories['all']) && $categories['all'] == 1)) {
145 $templates[$row->getValue('id')] = $row->getValue('name');
146 } else {
147
148
149 foreach ($path as $p) {
150 if (in_array($p, $categories)) {
151 $templates[$row->getValue('id')] = $row->getValue('name');
152 break;
153 }
154 }
155 }
156 }
157 }
158 }
159 return $templates;
160 }
161
162 public static function hasModule(array $template_attributes, $ctype, $module_id)
163 {
164 $template_modules = isset($template_attributes['modules']) ? $template_attributes['modules'] : [];
165 if (!isset($template_modules[$ctype]['all']) || $template_modules[$ctype]['all'] == 1) {
166 return true;
167 }
168
169 if (is_array($template_modules[$ctype]) && in_array($module_id, $template_modules[$ctype])) {
170 return true;
171 }
172
173 return false;
174 }
175 }
176