Skip to content

Commit e7e358e

Browse files
committed
Code style fixes
1 parent 8f75b50 commit e7e358e

File tree

8 files changed

+253
-265
lines changed

8 files changed

+253
-265
lines changed

index.php

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,49 +9,49 @@
99
$moduleRegistry = createModuleRegistry();
1010

1111
Kirby::plugin('medienbaecker/modules', [
12-
'templates' => $moduleRegistry['templates'],
13-
'pageModels' => $moduleRegistry['pageModels'],
14-
'blueprints' => $moduleRegistry['blueprints'],
15-
'sections' => [
16-
'modules' => include __DIR__ . '/lib/sections/modules.php'
17-
],
18-
'fields' => [
19-
'modules_redirect' => include __DIR__ . '/lib/fields/redirect.php'
20-
],
21-
'pageMethods' => [
22-
'renderModules' => function () {
23-
foreach ($this->modules() as $module) {
24-
$moduleTemplate = new Template($module->intendedTemplate());
25-
echo $moduleTemplate->render([
26-
'page' => $this,
27-
'module' => $module,
28-
'site' => $this->site()
29-
]);
30-
}
31-
},
32-
'hasModules' => function () {
33-
$modules = array_filter($this->blueprint()->sections(), function ($section) {
34-
return 'modules' === $section->type();
35-
});
36-
return count($modules) > 0;
37-
},
38-
'modules' => function () {
39-
$modules = new ModulesCollection;
40-
if ($rawModules = $this->find('modules')) {
41-
foreach($rawModules->childrenAndDrafts() as $module) {
42-
if (!$module->isListed() && !$module->isDraft()) continue;
43-
if ($module->isDraft && !$module->isVerified(get('token'))) continue;
44-
$modules->append($module);
45-
}
46-
}
47-
return $modules;
48-
},
49-
'isModule' => function () {
50-
return Str::startsWith($this->intendedTemplate(), 'module.');
51-
}
52-
],
53-
'api' => [
54-
'routes' => include __DIR__ . '/lib/routes.php',
55-
],
56-
'translations' => include __DIR__ . '/lib/translations.php',
12+
'templates' => $moduleRegistry['templates'],
13+
'pageModels' => $moduleRegistry['pageModels'],
14+
'blueprints' => $moduleRegistry['blueprints'],
15+
'sections' => [
16+
'modules' => include __DIR__ . '/lib/sections/modules.php'
17+
],
18+
'fields' => [
19+
'modules_redirect' => include __DIR__ . '/lib/fields/redirect.php'
20+
],
21+
'pageMethods' => [
22+
'renderModules' => function () {
23+
foreach ($this->modules() as $module) {
24+
$moduleTemplate = new Template($module->intendedTemplate());
25+
echo $moduleTemplate->render([
26+
'page' => $this,
27+
'module' => $module,
28+
'site' => $this->site()
29+
]);
30+
}
31+
},
32+
'hasModules' => function () {
33+
$modules = array_filter($this->blueprint()->sections(), function ($section) {
34+
return 'modules' === $section->type();
35+
});
36+
return count($modules) > 0;
37+
},
38+
'modules' => function () {
39+
$modules = new ModulesCollection;
40+
if ($rawModules = $this->find('modules')) {
41+
foreach ($rawModules->childrenAndDrafts() as $module) {
42+
if (!$module->isListed() && !$module->isDraft()) continue;
43+
if ($module->isDraft && !$module->isVerified(get('token'))) continue;
44+
$modules->append($module);
45+
}
46+
}
47+
return $modules;
48+
},
49+
'isModule' => function () {
50+
return Str::startsWith($this->intendedTemplate(), 'module.');
51+
}
52+
],
53+
'api' => [
54+
'routes' => include __DIR__ . '/lib/routes.php',
55+
],
56+
'translations' => include __DIR__ . '/lib/translations.php',
5757
]);

lib/collection.php

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,23 @@
44
use Kirby\Cms\Template;
55

66
class ModulesCollection extends Pages {
7-
/**
8-
* Converts the object to a string
9-
*
10-
* @return string
11-
*/
12-
public function toString(): string
13-
{
14-
$html = '';
7+
/**
8+
* Converts the object to a string
9+
*
10+
* @return string
11+
*/
12+
public function toString(): string {
13+
$html = '';
1514

16-
foreach ($this->data() as $module) {
17-
$moduleTemplate = new Template($module->intendedTemplate());
18-
$html .= $moduleTemplate->render([
19-
'page' => $module->parent(),
20-
'module' => $module,
21-
'site' => site(),
22-
]);
23-
}
15+
foreach ($this->data() as $module) {
16+
$moduleTemplate = new Template($module->intendedTemplate());
17+
$html .= $moduleTemplate->render([
18+
'page' => $module->parent(),
19+
'module' => $module,
20+
'site' => site(),
21+
]);
22+
}
2423

25-
return $html;
26-
}
24+
return $html;
25+
}
2726
}

lib/fields/redirect.php

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
<?php
22

33
return [
4-
'computed' => [
5-
'redirect' => function () {
6-
if ($this->model()->isHomePage()) {
7-
return $this->model()
8-
->site()
9-
->panel()
10-
->url();
11-
} else {
12-
return $this->model()
13-
->parent()
14-
->panel()
15-
->url();
16-
}
17-
}
18-
]
4+
'computed' => [
5+
'redirect' => function () {
6+
if ($this->model()->isHomePage()) {
7+
return $this->model()->site()->panel()->url();
8+
} else {
9+
return $this->model()->parent()->panel()->url();
10+
}
11+
}
12+
]
1913
];

lib/functions.php

Lines changed: 71 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,76 @@
11
<?php
22

3-
43
function createModuleRegistry() {
54

6-
// Set up registry array
7-
$registry = ['blueprints' => [], 'templates' => [], 'pageModels' => []];
8-
9-
// Add modules in site/modules
10-
$modulesFolder = kirby()->root('site') . "/modules";
11-
foreach (Dir::dirs($modulesFolder) as $folder) {
12-
$blueprint = $modulesFolder . "/". $folder . "/" . $folder . ".yml";
13-
$template = $modulesFolder . "/". $folder . "/" . $folder . ".php";
14-
if(F::exists($blueprint)) {
15-
$blueprintArray = Yaml::read($blueprint);
16-
if(!array_key_exists('status', $blueprintArray)) {
17-
$blueprintArray['status'] = [
18-
'draft' => true,
19-
'listed' => true,
20-
];
21-
}
22-
$registry['blueprints']['pages/module.'. $folder] = $blueprintArray;
23-
$registry['templates']['module.'. $folder] = $template;
24-
$registry['pageModels']['module.'. $folder] = option('medienbaecker.modules.model', 'ModulePage');
25-
}
26-
}
27-
28-
// Add legacy modules to registry
29-
$moduleBlueprints = array_filter(kirby()->blueprints(), function($blueprint) {
30-
return Str::startsWith($blueprint, 'module.');
31-
});
32-
if(!empty($moduleBlueprints)) {
33-
$blueprintsFolder = kirby()->root('blueprints');
34-
$snippetsFolder = kirby()->root('snippets');
35-
foreach($moduleBlueprints as $moduleBlueprint) {
36-
$blueprint = $blueprintsFolder . "/pages/" . $moduleBlueprint . ".yml";
37-
$template = $snippetsFolder . "/modules/" . $moduleBlueprint . ".php";
38-
if(F::exists($blueprint)) {
39-
$blueprintArray = Yaml::read($blueprint);
40-
if(!array_key_exists('status', $blueprintArray)) {
41-
$blueprintArray['status'] = [
42-
'draft' => true,
43-
'listed' => true,
44-
];
45-
}
46-
if(!array_key_exists('pages/'. $moduleBlueprint, $registry['blueprints'])) {
47-
$registry['blueprints']['pages/'. $moduleBlueprint] = $blueprintArray;
48-
}
49-
if(!array_key_exists($moduleBlueprint, $registry['templates'])) {
50-
$registry['templates'][$moduleBlueprint] = $template;
51-
}
52-
if(!array_key_exists($moduleBlueprint, $registry['pageModels'])) {
53-
$registry['pageModels'][$moduleBlueprint] = option('medienbaecker.modules.model', 'ModulePage');
54-
}
55-
}
56-
}
57-
}
58-
59-
// Add modules container blueprint with redirect field
60-
$registry['blueprints']['pages/modules'] = [
61-
'title' => 'Modules',
62-
'options' => [
63-
'changeSlug' => false,
64-
'changeStatus' => false,
65-
'changeTemplate' => false
66-
],
67-
'fields' => [
68-
'modules_redirect' => true
69-
]
70-
];
71-
72-
// Add modules container model
73-
$registry['pageModels']['modules'] = 'ModulesPage';
74-
75-
76-
return $registry;
5+
// Set up registry array
6+
$registry = ['blueprints' => [], 'templates' => [], 'pageModels' => []];
7+
8+
// Add modules in site/modules
9+
$modulesFolder = kirby()->root('site') . "/modules";
10+
foreach (Dir::dirs($modulesFolder) as $folder) {
11+
$blueprint = $modulesFolder . "/". $folder . "/" . $folder . ".yml";
12+
$template = $modulesFolder . "/". $folder . "/" . $folder . ".php";
13+
if(F::exists($blueprint)) {
14+
$blueprintArray = Yaml::read($blueprint);
15+
if(!array_key_exists('status', $blueprintArray)) {
16+
$blueprintArray['status'] = [
17+
'draft' => true,
18+
'listed' => true,
19+
];
20+
}
21+
$registry['blueprints']['pages/module.'. $folder] = $blueprintArray;
22+
$registry['templates']['module.'. $folder] = $template;
23+
$registry['pageModels']['module.'. $folder] = option('medienbaecker.modules.model', 'ModulePage');
24+
}
25+
}
26+
27+
// Add legacy modules to registry
28+
$moduleBlueprints = array_filter(kirby()->blueprints(), function($blueprint) {
29+
return Str::startsWith($blueprint, 'module.');
30+
});
31+
if(!empty($moduleBlueprints)) {
32+
$blueprintsFolder = kirby()->root('blueprints');
33+
$snippetsFolder = kirby()->root('snippets');
34+
foreach($moduleBlueprints as $moduleBlueprint) {
35+
$blueprint = $blueprintsFolder . "/pages/" . $moduleBlueprint . ".yml";
36+
$template = $snippetsFolder . "/modules/" . $moduleBlueprint . ".php";
37+
if(F::exists($blueprint)) {
38+
$blueprintArray = Yaml::read($blueprint);
39+
if(!array_key_exists('status', $blueprintArray)) {
40+
$blueprintArray['status'] = [
41+
'draft' => true,
42+
'listed' => true,
43+
];
44+
}
45+
if(!array_key_exists('pages/'. $moduleBlueprint, $registry['blueprints'])) {
46+
$registry['blueprints']['pages/'. $moduleBlueprint] = $blueprintArray;
47+
}
48+
if(!array_key_exists($moduleBlueprint, $registry['templates'])) {
49+
$registry['templates'][$moduleBlueprint] = $template;
50+
}
51+
if(!array_key_exists($moduleBlueprint, $registry['pageModels'])) {
52+
$registry['pageModels'][$moduleBlueprint] = option('medienbaecker.modules.model', 'ModulePage');
53+
}
54+
}
55+
}
56+
}
57+
58+
// Add modules container blueprint with redirect field
59+
$registry['blueprints']['pages/modules'] = [
60+
'title' => 'Modules',
61+
'options' => [
62+
'changeSlug' => false,
63+
'changeStatus' => false,
64+
'changeTemplate' => false
65+
],
66+
'fields' => [
67+
'modules_redirect' => true
68+
]
69+
];
70+
71+
// Add modules container model
72+
$registry['pageModels']['modules'] = 'ModulesPage';
73+
74+
75+
return $registry;
7776
}

lib/models.php

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,43 @@
33
use Kirby\Cms\Template;
44

55
class ModulePage extends Page {
6-
public static function create(array $props) {
7-
if (option('medienbaecker.modules.autopublish', false)) {
8-
$props['num'] = 9999;
9-
}
10-
return parent::create($props);
11-
}
12-
public function url($options = null): string {
13-
return $this->parents()->filterBy('intendedTemplate', '!=', 'modules')->first()->url() . '#' . $this->slug();
14-
}
15-
public function render(array $data = [], $contentType = 'html'): string {
16-
go($this->parents()->filterBy('intendedTemplate', '!=', 'modules')->first()->url() . '#' . $this->slug());
17-
}
18-
public function renderModule() {
19-
$moduleTemplate = new Template($this->intendedTemplate());
20-
echo $moduleTemplate->render([
21-
'page' => $this->parent()->parent(),
22-
'module' => $this,
23-
'site' => $this->site()
24-
]);
25-
}
26-
public function moduleName() {
27-
return $this->blueprint()->title();
28-
}
29-
public function moduleId() {
30-
return str_replace('.', '__', $this->intendedTemplate());
31-
}
32-
public function parents(){
33-
$parents = parent::parents();
34-
return $parents->filter('slug', '!=', 'modules');
6+
public static function create(array $props) {
7+
if (option('medienbaecker.modules.autopublish', false)) {
8+
$props['num'] = 9999;
359
}
10+
return parent::create($props);
11+
}
12+
public function url($options = null): string {
13+
return $this->parents()->filterBy('intendedTemplate', '!=', 'modules')->first()->url() . '#' . $this->slug();
14+
}
15+
public function render(array $data = [], $contentType = 'html'): string {
16+
go($this->parents()->filterBy('intendedTemplate', '!=', 'modules')->first()->url() . '#' . $this->slug());
17+
}
18+
public function renderModule() {
19+
$moduleTemplate = new Template($this->intendedTemplate());
20+
echo $moduleTemplate->render([
21+
'page' => $this->parent()->parent(),
22+
'module' => $this,
23+
'site' => $this->site()
24+
]);
25+
}
26+
public function moduleName() {
27+
return $this->blueprint()->title();
28+
}
29+
public function moduleId() {
30+
return str_replace('.', '__', $this->intendedTemplate());
31+
}
32+
public function parents(){
33+
$parents = parent::parents();
34+
return $parents->filter('slug', '!=', 'modules');
35+
}
3636
}
3737

3838
class ModulesPage extends Page {
39-
public function url($options = null): string {
40-
return $this->parent()->url();
41-
}
42-
public function render(array $data = [], $contentType = 'html'): string {
43-
go($this->parent()->url());
44-
}
45-
public function parents(){
46-
$parents = parent::parents();
47-
return $parents->filter('slug', '!=', 'modules');
48-
}
39+
public function url($options = null): string {
40+
return $this->parent()->url();
41+
}
42+
public function render(array $data = [], $contentType = 'html'): string {
43+
go($this->parent()->url());
44+
}
4945
}

0 commit comments

Comments
 (0)