-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.php
373 lines (372 loc) · 20.7 KB
/
logic.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<?php
/**
* @package Joomla.Site
* @subpackage Templates.minimalista
*
* @copyright (C) 2017 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;
// Include common libraries and dependencies
use Joomla\CMS\Document\Document;
use Joomla\CMS\Factory;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Table\Table;
use Joomla\CMS\Uri\Uri;
use Joomla\Filesystem\Folder;
use Joomla\CMS\Router\Route;
/** @var Joomla\CMS\Document\HtmlDocument $this */
// Database connection setup
$db = Factory::getContainer()->get('DatabaseDriver');
$query = $db->getQuery(true);
$query->select($db->quoteName('parent'))
->from($db->quoteName('#__template_styles'))
->where($db->quoteName('template') . ' = ' . $db->quote($this->template));
$db->setQuery($query);
$templateParent = $db->loadResult();
$templateOriginal = $templateParent ? $templateParent : $this->template;
// Get the current user object
$user = Factory::getApplication()->getIdentity();
// Get the user group IDs
$userGroupIds = $user->getAuthorisedGroups();
// Initialize an array to hold the user group names
$userGroupNames = [];
// Loop through the user group IDs to get their names
foreach ($userGroupIds as $groupId) {
$query = $db->getQuery(true)
->select($db->quoteName('title'))
->from($db->quoteName('#__usergroups'))
->where($db->quoteName('id') . ' = ' . (int) $groupId);
$db->setQuery($query);
$groupName = $db->loadResult();
if ($groupName) {
$userGroupNames[] = $groupName;
}
}
// Application and document setup
$app = Factory::getApplication();
$input = $app->getInput();
$doc = $app->getDocument();
$wa = $doc->getWebAssetManager();
$war = $wa->getRegistry();
$doc->setHtml5(true);
$doc->setGenerator('');
$user = $app->getIdentity();
$input = $app->input;
// Get input parameters
$option = $input->getCmd('option', '');
$view = $input->getCmd('view', '');
$layout = $input->getCmd('layout', '');
$task = $input->getCmd('task', '');
$itemid = $input->getCmd('Itemid', '');
$sitename = $app->get('sitename', '');
// Get menu and active menu item information
$menu = $app->getMenu();
$active = $menu->getActive();
$activeParams = $active->getParams();
$alias = $active->alias;
$pageclass = $activeParams->get('pageclass_sfx', '');
$parent = $menu->getItem($active->parent_id);
$parentParams = $parent ? $parent->getParams() : '';
$parentAlias = $parent ? $parent->alias : '';
$parentPageclass = $parentParams ? $parentParams->get('pageclass_sfx', '') : '';
// Template-related settings and parameters
$tpath = Uri::root(true) . '/templates/' . $templateOriginal;
$templateParams = $app->getTemplate(true)->params;
$offcanvasDirection = $templateParams->get('offcanvas_direction', 'start');
$logo = $templateParams->get('logo', '');
$logo_alt = $templateParams->get('logo_alt', '') ? $templateParams->get('logo_alt') : $sitename;
$doc->addFavicon(Uri::root(true) . '/' . $templateParams->get('favicon', ''));
$doc->setMetaData('viewport', 'width=device-width, initial-scale=1.0');
$custom_css_head = $templateParams->get('custom_css_head', '');
$custom_script_head = $templateParams->get('custom_script_head', '');
$startBodyCode = $templateParams->get('custom_script_startbody', '');
$endBodyCode = $templateParams->get('custom_script_endbody', '');
$backtotop = $templateParams->get('backtotop', '1');
// custom_css_head custom_script_head
if ($custom_css_head) {
$wa->addInlineStyle($custom_css_head);
}
if ($custom_script_head) {
$wa->addInlineScript($custom_script_head);
}
// Generate CSS classes for the <body> element
$bodyClasses = ($option ? 'option-' . str_replace('com_', '', $option) : 'no-option')
. ' ' . ($view ? 'view-' . $view : 'no-view')
. ' ' . ($layout ? 'layout-' . $layout : 'no-layout')
. ' ' . ($task ? 'task-' . $task : 'no-task')
. ' ' . ($itemid ? 'itemid-' . $itemid : 'no-itemid')
. ' ' . ($alias ? 'alias-' . $alias : 'no-alias')
. ' ' . ($pageclass ? $pageclass : 'no-pageclass')
. ' ' . ($parentAlias ? 'parent-' . $parentAlias : 'no-parent')
. ' ' . ($parentPageclass ? $parentPageclass : 'no-parent-pageclass')
. ' ' . ($doc->getDirection() === 'rtl' ? 'direction-rtl' : 'direction-ltr')
. ' ' . ($user->guest ? 'user-guest' : 'user-logged-in');
// Output the user group names as CSS classes
foreach ($userGroupNames as $groupName) {
$bodyClasses .= ' user-group-' . strtolower($groupName);
}
$containerFluid = $templateParams->get('container-fluid', '0') == '1' ? '-fluid' : '';
$defaultBoostrapDesktop = '-' . $templateParams->get('default-bootstrap-desktop', 'lg');
$sidebarWidth = $defaultBoostrapDesktop . '-' . $templateParams->get('sidebar-width', '3');
if ($this->countModules('sidebar-left') && $this->countModules('sidebar-right')) {
$mainWidth = $defaultBoostrapDesktop . '-' . (12 - $templateParams->get('sidebar-width', '3') * 2);
} elseif ($this->countModules('sidebar-left') || $this->countModules('sidebar-right')) {
$mainWidth = $defaultBoostrapDesktop . '-' . (12 - $templateParams->get('sidebar-width', '3'));
} else {
$mainWidth = $defaultBoostrapDesktop . '-' . '12';
}
// Load jQuery based on template or Joomla configuration
if ($templateParams->get('load_jquery_from_template', 1) == 1) {
$wa->registerAndUseScript('jquery_from_template', Uri::root(true) . 'media/templates/site/' . $templateOriginal . '/js/jquery-3.7.1.min.js', array('version' => 'auto'));
$wa->registerAndUseScript('jquery-noconflict', Uri::root(true) . 'media/templates/site/' . $templateOriginal . '/js/jquery-noconflict.js', array('version' => 'auto'));
$wa->registerAndUseScript('jquery_migrate_from_template', Uri::root(true) . 'media/templates/site/' . $templateOriginal . '/js/jquery-migrate-3.4.0.min.js', array('version' => 'auto'));
} else {
// Load jQuery from Joomla
HTMLHelper::_('jquery.framework', true, true);
}
// Load Bootstrap CSS and JavaScript based on template or Joomla configuration
if ($templateParams->get('bootstrap_from_template', 1) == 1) {
$wa->registerAndUseStyle('bootstrap_css', Uri::root(true) . 'media/templates/site/' . $templateOriginal . '/css/bootstrap.min.css', array('version' => 'auto'));
$wa->registerAndUseScript('bootstrapbundle_js', Uri::root(true) . 'media/templates/site/' . $templateOriginal . '/js/bootstrap.bundle.min.js', array('version' => 'auto'), array('defer' => true));
} else {
// Load Bootstrap CSS and JavaScript from Joomla
HTMLHelper::_('bootstrap.loadCss', true, $this->direction);
HTMLHelper::_('bootstrap.framework');
}
// Load FontAwesome based on template or Joomla configuration
$loadFontAwesome = $templateParams->get('load_fontawesome', 'css_from_template');
if ($loadFontAwesome == 'css_from_template') {
$wa->registerAndUseStyle('fontawesome_css', Uri::root(true) . 'media/templates/site/' . $templateOriginal . '/css/all.min.css', array('version' => 'auto'));
} elseif ($loadFontAwesome == 'js_from_template') {
$wa->registerAndUseScript('fontawesome_js', Uri::root(true) . 'media/templates/site/' . $templateOriginal . '/js/all.min.js', array('version' => 'auto'), array('defer' => true));
} elseif ($loadFontAwesome == 'css_from_joomla') {
// Load FontAwesome from Joomla
$wa->registerAndUseStyle('fontawesome', 'media/vendor/fontawesome-free/css/all.min.css', array('version' => 'auto'));
} else {
// Do nothing for FontAwesome
}
// Load Joomla 4 system icons
$wa->registerAndUseStyle('icons', 'media/system/css/joomla-fontawesome.min.css', array('version' => 'auto'));
$cssFilePath = JPATH_ROOT . '/media/templates/site/' . $templateOriginal . '/css/template.css';
if (file_exists($cssFilePath)) {
$wa->registerAndUseStyle('template-css', Uri::root(true) . 'media/templates/site/' . $templateOriginal . '/css/template.css', array('version' => filemtime($cssFilePath)));
}
// Load template-specific JavaScript after jQuery and Bootstrap
$wa->registerAndUseScript('template-js', Uri::root(true) . 'media/templates/site/' . $templateOriginal . '/js/template.js', array('version' => 'auto'), array('defer' => true));
$wa->registerAndUseStyle($this->template . 'template-css', Uri::root(true) . 'media/templates/site/' . $this->template . '/css/template.css', array('version' => 'auto'));
$wa->registerAndUseScript($this->template . 'template-js', Uri::root(true) . 'media/templates/site/' . $this->template . '/js/template.js', array('version' => 'auto'), array('defer' => true));
// Assuming $this is an instance of Document
// media/templates/site/minimalista/css/adtitional/
$additionalcssDirectory = JPATH_ROOT . '/media/templates/site/' . $this->template . '/css/additional';
if (is_dir($additionalcssDirectory)) {
$additionalcssFiles = Folder::files($additionalcssDirectory, 'css', true, true);
if (is_array($additionalcssFiles) || is_object($additionalcssFiles)) {
foreach ($additionalcssFiles as $index => $cssFile) {
$wa->registerAndUseStyle($this->template . pathinfo($cssFile, PATHINFO_FILENAME), Uri::root(true) . 'media/templates/site/' . $this->template . '/css/additional' . '/' . basename($cssFile), array('version' => 'auto'));
}
}
} else {
// Handle the case where the directory does not exist
// You can log an error or take appropriate action here
}
$additionaljsDirectory = JPATH_ROOT . '/media/templates/site/' . $this->template . '/js/additional';
if (is_dir($additionaljsDirectory)) {
$additionaljsFiles = Folder::files($additionaljsDirectory, 'js', true, true);
if (is_array($additionaljsFiles) || is_object($additionaljsFiles)) {
foreach ($additionaljsFiles as $index => $jsFile) {
$wa->registerAndUseScript($this->template . pathinfo($jsFile, PATHINFO_FILENAME), Uri::root(true) . 'media/templates/site/' . $this->template . '/js/additional' . '/' . basename($jsFile), array('version' => 'auto'), array('defer' => true));
}
}
} else {
// Handle the case where the directory does not exist
// You can log an error or take appropriate action here
}
// Scan template CSS folder and load all CSS files with "custom" in the name for the original template
$customCssDirectoryOriginalTemplate = JPATH_ROOT . '/media/templates/site/' . $templateOriginal . '/css';
if (is_dir($customCssDirectoryOriginalTemplate)) {
$customCssFilesOriginalTemplate = Folder::files($customCssDirectoryOriginalTemplate, 'custom', true, true);
if (is_array($customCssFilesOriginalTemplate) || is_object($customCssFilesOriginalTemplate)) {
foreach ($customCssFilesOriginalTemplate as $index => $cssFile) {
$wa->registerAndUseStyle(pathinfo($cssFile, PATHINFO_FILENAME), Uri::root(true) . 'media/templates/site/' . $templateOriginal . '/css' . '/' . basename($cssFile), array('version' => 'auto'));
}
}
} else {
// Handle the case where the directory does not exist
// You can log an error or take appropriate action here
}
// Scan template CSS folder and load all CSS files with "customfont" in the name for the current template
$customFontCssDirectoryCurrentTemplate = JPATH_ROOT . '/media/templates/site/' . $this->template . '/css';
if (is_dir($customFontCssDirectoryCurrentTemplate)) {
$customFontCssFilesCurrentTemplate = Folder::files($customFontCssDirectoryCurrentTemplate, 'customfont', true, true);
if (is_array($customFontCssFilesCurrentTemplate) || is_object($customFontCssFilesCurrentTemplate)) {
foreach ($customFontCssFilesCurrentTemplate as $index => $cssFile) {
$wa->registerAndUseStyle($this->template . pathinfo($cssFile, PATHINFO_FILENAME), Uri::root(true) . 'media/templates/site/' . $this->template . '/css' . '/' . basename($cssFile), array('version' => 'auto'));
}
}
} else {
// Handle the case where the directory does not exist
// You can log an error or take appropriate action here
}
// Scan template CSS folder and load all CSS files with "custom" in the name for the current template
$customCssDirectoryCurrentTemplate = JPATH_ROOT . '/media/templates/site/' . $this->template . '/css';
if (is_dir($customCssDirectoryCurrentTemplate)) {
$customCssFilesCurrentTemplate = Folder::files($customCssDirectoryCurrentTemplate, 'custom', true, true);
if (is_array($customCssFilesCurrentTemplate) || is_object($customCssFilesCurrentTemplate)) {
foreach ($customCssFilesCurrentTemplate as $index => $cssFile) {
$wa->registerAndUseStyle($this->template . pathinfo($cssFile, PATHINFO_FILENAME), Uri::root(true) . 'media/templates/site/' . $this->template . '/css' . '/' . basename($cssFile), array('version' => 'auto'));
}
}
} else {
// Handle the case where the directory does not exist
// You can log an error or take appropriate action here
}
// Scan template JS folder and load all JS files with "custom" in the name for the original template
$customJsDirectoryOriginalTemplate = JPATH_ROOT . '/media/templates/site/' . $templateOriginal . '/js';
if (is_dir($customJsDirectoryOriginalTemplate)) {
$customJsFilesOriginalTemplate = Folder::files($customJsDirectoryOriginalTemplate, 'custom', true, true);
if (is_array($customJsFilesOriginalTemplate) || is_object($customJsFilesOriginalTemplate)) {
foreach ($customJsFilesOriginalTemplate as $index => $jsFile) {
$wa->registerAndUseScript(pathinfo($jsFile, PATHINFO_FILENAME), Uri::root(true) . 'media/templates/site/' . $templateOriginal . '/js' . '/' . basename($jsFile), array('version' => 'auto'), array('defer' => true));
}
}
} else {
// Handle the case where the directory does not exist
// You can log an error or take appropriate action here
}
// Scan template JS folder and load all JS files with "custom" in the name for the current template
$customJsDirectoryCurrentTemplate = JPATH_ROOT . '/media/templates/site/' . $this->template . '/js';
if (is_dir($customJsDirectoryCurrentTemplate)) {
$customJsFilesCurrentTemplate = Folder::files($customJsDirectoryCurrentTemplate, 'custom', true, true);
if (is_array($customJsFilesCurrentTemplate) || is_object($customJsFilesCurrentTemplate)) {
foreach ($customJsFilesCurrentTemplate as $index => $jsFile) {
$wa->registerAndUseScript($this->template . pathinfo($jsFile, PATHINFO_FILENAME), Uri::root(true) . 'media/templates/site/' . $this->template . '/js' . '/' . basename($jsFile), array('version' => 'auto'), array('defer' => true));
}
}
} else {
// Handle the case where the directory does not exist
// You can log an error or take appropriate action here
}
$responsiveCssPath = JPATH_ROOT . '/media/templates/site/' . $this->template . '/css/responsive.css';
if (file_exists($responsiveCssPath)) {
$wa->registerAndUseStyle($this->template . 'responsive-css', Uri::root(true) . 'media/templates/site/' . $this->template . '/css/responsive.css', array('version' => filemtime($responsiveCssPath)));
}
// load social meta tags OpenGraph for Faceboook, Twitter Cards and Schema.org
if ($templateParams->get('enable_social_meta_tags', 1)) {
$disable_in = $templateParams->get('disable_in', '');
if (!is_array($disable_in)) {
// If 'disable_in' is a string, convert it to an array with a single element
$disable_in = array($disable_in);
}
if (!in_array($option, $disable_in)) {
$title = $doc->getTitle() ? $doc->getTitle() : $sitename;
$description = $doc->getDescription() ? $doc->getDescription() : Factory::getConfig()->get('MetaDesc');
$image = $templateParams->get('social_image', $logo);
$image_alt = $templateParams->get('social_image_alt', $logo_alt);
$arrobasite = $templateParams->get('arrobasite', '');
$arrobacreator = $templateParams->get('arrobaauthor', '');
// Set common metadata
setMetadata($doc, $title, $description, $image, $image_alt, $arrobasite, $arrobacreator);
// Additional metadata for specific conditions and is not homepage
if ($option == 'com_content' && $view == 'article' && Factory::getApplication()->input->getInt('id') && $active->home != 1) {
$content = Table::getInstance('content');
$content->load(Factory::getApplication()->input->getInt('id'));
$images = null;
if (!empty($content->images) && is_string($content->images)) {
$images = json_decode($content->images);
}
if ($images) {
$image = $images->image_intro ? $images->image_intro : $images->image_fulltext;
$image_alt = $images->image_intro_alt ? $images->image_intro_alt : $images->image_fulltext_alt;
}
$textlimit = $templateParams->get('textlimit', 300);
$text = $content->introtext . $content->fulltext;
$firstTextImageRegx = '/<img[^>]+src=[\'"](?P<src>[^\'"]+)[\'"][^>]*>/i';
preg_match($firstTextImageRegx, $text, $matches);
if (isset($matches['src']) && empty($image)) {
$image = $matches['src'];
}
$text = strip_tags($text);
$text = preg_replace('/\s+/', ' ', $text);
$text = trim($text);
$text = substr($text, 0, $textlimit);
setMetadata($doc, $title, $text, Uri::root() . $image, $image_alt);
}
if ($option == 'com_content' && $view == 'category' && Factory::getApplication()->input->getInt('id') && $active->home != 1) {
$category = Table::getInstance('category');
$category->load(Factory::getApplication()->input->getInt('id'));
$textlimit = $templateParams->get('textlimit', 300);
// Check if the properties exist before accessing them
$image = property_exists($category, 'image') ? $category->image : '';
$image_alt = property_exists($category, 'image_alt') ? $category->image_alt : '';
if (!empty($image)) {
setMetadata($doc, $title, substr(strip_tags($category->description), 0, $textlimit), Uri::root() . $image, $image_alt);
}
}
}
}
// Function to set common metadata
function setMetadata($doc, $title, $description, $image, $image_alt, $arrobasite = '', $arrobacreator = '')
{
$doc->setMetaData('og:title', $title);
$doc->setMetaData('og:description', $description);
$doc->setMetaData('og:image', Uri::root() . $image);
// fb:app_id
$doc->setMetaData('fb:app_id', '');
$doc->setMetaData('og:image:alt', $image_alt);
$doc->setMetaData('og:type', 'website');
$doc->setMetaData('og:url', Uri::root());
$doc->setMetaData('og:site_name', Factory::getApplication()->get('sitename'));
$doc->setMetaData('twitter:card', 'summary_large_image');
$doc->setMetaData('twitter:title', $title);
$doc->setMetaData('twitter:description', $description);
$doc->setMetaData('twitter:image', $image);
$doc->setMetaData('twitter:image:alt', $image_alt);
$doc->setMetaData('twitter:site', $arrobasite);
$doc->setMetaData('twitter:creator', $arrobacreator);
$doc->setMetaData('schema:name', $title);
$doc->setMetaData('schema:description', $description);
$doc->setMetaData('schema:image', $image);
$doc->setMetaData('schema:image:alt', $image_alt);
}
// functions.php
function cleanSectionName($sectionName)
{
$sectionName = str_replace(' ', '-', $sectionName);
$sectionName = strtolower($sectionName);
$sectionName = iconv('UTF-8', 'ASCII//TRANSLIT', $sectionName);
$sectionName = preg_replace('/[^a-zA-Z0-9\-]/', '', $sectionName);
return $sectionName;
}
function hasModules($positions, $template)
{
foreach ($positions as $position) {
if ($template->countModules($position->position) > 0) {
return true;
}
}
return false;
}
function renderSection($section, $defaultBoostrapDesktop, $template, $templateOriginal)
{
$sectionName = cleanSectionName($section->section);
$hasModules = hasModules($section->positions, $template);
if ($hasModules) {
?>
<section id="<?php echo $sectionName; ?>"
class="<?php echo 'section-' . $sectionName . ($section->section_class ? ' ' . $section->section_class : ''); ?>">
<div class="<?php echo $section->containerwidth; ?>">
<div class="row">
<?php foreach ($section->positions as $position): ?>
<div class="<?php echo 'position-' . strtolower($position->position); ?> col<?php echo $defaultBoostrapDesktop . ($position->width ? '-' . $position->width : ''); ?><?php echo $position->customclass ? ' ' . $position->customclass : ''; ?>">
<div class="row">
<jdoc:include type="modules" name="<?php echo $position->position; ?>" style="<?php echo $templateOriginal . '-default'; ?>" />
</div>
</div>
<?php endforeach;?>
</div>
</div>
</section>
<?php
}
}