Skip to content

Commit 69eae41

Browse files
committed
Library/Template: Optimize load sideboxes
1 parent 42e09fb commit 69eae41

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

application/libraries/Template.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,10 @@ public function view(string $content, bool|string|array $css = false, bool|strin
268268
private function handleNormalPage($content, bool|string|array$css, bool|string|array$js): mixed
269269
{
270270
//Load the sideboxes
271-
$sideboxes = $this->loadSideboxes();
272-
$sideboxes_top = $this->loadSideboxes('top');
273-
$sideboxes_bottom = $this->loadSideboxes('bottom');
271+
$sideboxes = $this->loadSideboxes();
272+
$sideboxes_side = $sideboxes['side'] ?? [];
273+
$sideboxes_top = $sideboxes['top'] ?? [];
274+
$sideboxes_bottom = $sideboxes['bottom'] ?? [];
274275
$header = $this->getHeader($css, $js);
275276
$modals = $this->getModals();
276277

@@ -298,7 +299,7 @@ private function handleNormalPage($content, bool|string|array$css, bool|string|a
298299
"image_path" => $this->image_path,
299300
"isOnline" => $this->CI->user->isOnline(),
300301
"isRTL" => $this->CI->language->getLanguage() == 'persian' || $this->CI->language->getClientData() == 'persian',
301-
"sideboxes" => $sideboxes,
302+
"sideboxes" => $sideboxes_side,
302303
"sideboxes_top" => $sideboxes_top,
303304
"sideboxes_bottom" => $sideboxes_bottom
304305
];
@@ -454,16 +455,23 @@ private function voteReminder(): bool|string
454455
/**
455456
* Loads the sideboxes, and returns the result
456457
*
457-
* @param string $location
458458
* @return array
459459
*/
460-
public function loadSideboxes(string $location = 'side'): array
460+
public function loadSideboxes(): array
461461
{
462-
$output = [];
462+
$output = [
463+
'side' => [],
464+
'top' => [],
465+
'bottom' => [],
466+
];
467+
463468
$module = CI::$APP->router->fetch_module();
464-
$sideBoxes = $this->CI->cms_model->getSideboxes($location, $module);
465469

466-
foreach ((array) $sideBoxes as $sideBox) {
470+
$allSideboxes = $this->CI->cms_model->getSideboxes($module);
471+
472+
foreach ((array) $allSideboxes as $sideBox) {
473+
$location = $sideBox['location'] ?? 'side';
474+
467475
if ($sideBox['permission'] && !hasViewPermission($sideBox['permission'], "--SIDEBOX--")) {
468476
continue;
469477
}
@@ -472,7 +480,7 @@ public function loadSideboxes(string $location = 'side'): array
472480
$fileLocation = APPPATH . 'modules/sidebox_' . $sideboxType . '/controllers/' . ucfirst($sideboxType) . '.php';
473481

474482
if (!file_exists($fileLocation)) {
475-
$output[] = [
483+
$output[$location][] = [
476484
'name' => "Oops, something went wrong",
477485
'data' => 'The following sideBox module is missing or contains an invalid module structure: <b>sidebox_' . $sideboxType . '</b>'
478486
];
@@ -483,9 +491,9 @@ public function loadSideboxes(string $location = 'side'): array
483491

484492
$object = ($sideboxType === 'custom') ? new $sideboxType($sideBox['id']) : new $sideboxType();
485493

486-
$output[] = [
494+
$output[$location][] = [
487495
'name' => langColumn($sideBox['displayName']),
488-
'location' => $sideBox['location'],
496+
'location' => $location,
489497
'data' => $object->view(),
490498
'type' => $sideboxType,
491499
];

application/models/Cms_model.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,13 @@ private function logVisit()
3939
}
4040
}
4141

42-
public function getSideboxes(string $type = 'side', string $page = '*')
42+
public function getSideboxes(string $page = '*'): array
4343
{
4444
// Query: Prepare
4545
$query = $this->db->table('sideboxes')
4646
->select('*')
4747
->orderBy('order', 'ASC');
4848

49-
// Query: Filter (Type)
50-
if($type && in_array($type, ['top', 'side', 'bottom']))
51-
$query = $query->where('location', $type);
52-
5349
// Query: Filter (Page)
5450
if($page && $page !== '*')
5551
$query = $query->groupStart()

0 commit comments

Comments
 (0)