Skip to content

Commit

Permalink
Code style fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
medienbaecker committed Jun 27, 2022
1 parent 8f75b50 commit e7e358e
Show file tree
Hide file tree
Showing 8 changed files with 253 additions and 265 deletions.
90 changes: 45 additions & 45 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,49 +9,49 @@
$moduleRegistry = createModuleRegistry();

Kirby::plugin('medienbaecker/modules', [
'templates' => $moduleRegistry['templates'],
'pageModels' => $moduleRegistry['pageModels'],
'blueprints' => $moduleRegistry['blueprints'],
'sections' => [
'modules' => include __DIR__ . '/lib/sections/modules.php'
],
'fields' => [
'modules_redirect' => include __DIR__ . '/lib/fields/redirect.php'
],
'pageMethods' => [
'renderModules' => function () {
foreach ($this->modules() as $module) {
$moduleTemplate = new Template($module->intendedTemplate());
echo $moduleTemplate->render([
'page' => $this,
'module' => $module,
'site' => $this->site()
]);
}
},
'hasModules' => function () {
$modules = array_filter($this->blueprint()->sections(), function ($section) {
return 'modules' === $section->type();
});
return count($modules) > 0;
},
'modules' => function () {
$modules = new ModulesCollection;
if ($rawModules = $this->find('modules')) {
foreach($rawModules->childrenAndDrafts() as $module) {
if (!$module->isListed() && !$module->isDraft()) continue;
if ($module->isDraft && !$module->isVerified(get('token'))) continue;
$modules->append($module);
}
}
return $modules;
},
'isModule' => function () {
return Str::startsWith($this->intendedTemplate(), 'module.');
}
],
'api' => [
'routes' => include __DIR__ . '/lib/routes.php',
],
'translations' => include __DIR__ . '/lib/translations.php',
'templates' => $moduleRegistry['templates'],
'pageModels' => $moduleRegistry['pageModels'],
'blueprints' => $moduleRegistry['blueprints'],
'sections' => [
'modules' => include __DIR__ . '/lib/sections/modules.php'
],
'fields' => [
'modules_redirect' => include __DIR__ . '/lib/fields/redirect.php'
],
'pageMethods' => [
'renderModules' => function () {
foreach ($this->modules() as $module) {
$moduleTemplate = new Template($module->intendedTemplate());
echo $moduleTemplate->render([
'page' => $this,
'module' => $module,
'site' => $this->site()
]);
}
},
'hasModules' => function () {
$modules = array_filter($this->blueprint()->sections(), function ($section) {
return 'modules' === $section->type();
});
return count($modules) > 0;
},
'modules' => function () {
$modules = new ModulesCollection;
if ($rawModules = $this->find('modules')) {
foreach ($rawModules->childrenAndDrafts() as $module) {
if (!$module->isListed() && !$module->isDraft()) continue;
if ($module->isDraft && !$module->isVerified(get('token'))) continue;
$modules->append($module);
}
}
return $modules;
},
'isModule' => function () {
return Str::startsWith($this->intendedTemplate(), 'module.');
}
],
'api' => [
'routes' => include __DIR__ . '/lib/routes.php',
],
'translations' => include __DIR__ . '/lib/translations.php',
]);
35 changes: 17 additions & 18 deletions lib/collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,23 @@
use Kirby\Cms\Template;

class ModulesCollection extends Pages {
/**
* Converts the object to a string
*
* @return string
*/
public function toString(): string
{
$html = '';
/**
* Converts the object to a string
*
* @return string
*/
public function toString(): string {
$html = '';

foreach ($this->data() as $module) {
$moduleTemplate = new Template($module->intendedTemplate());
$html .= $moduleTemplate->render([
'page' => $module->parent(),
'module' => $module,
'site' => site(),
]);
}
foreach ($this->data() as $module) {
$moduleTemplate = new Template($module->intendedTemplate());
$html .= $moduleTemplate->render([
'page' => $module->parent(),
'module' => $module,
'site' => site(),
]);
}

return $html;
}
return $html;
}
}
24 changes: 9 additions & 15 deletions lib/fields/redirect.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
<?php

return [
'computed' => [
'redirect' => function () {
if ($this->model()->isHomePage()) {
return $this->model()
->site()
->panel()
->url();
} else {
return $this->model()
->parent()
->panel()
->url();
}
}
]
'computed' => [
'redirect' => function () {
if ($this->model()->isHomePage()) {
return $this->model()->site()->panel()->url();
} else {
return $this->model()->parent()->panel()->url();
}
}
]
];
143 changes: 71 additions & 72 deletions lib/functions.php
Original file line number Diff line number Diff line change
@@ -1,77 +1,76 @@
<?php


function createModuleRegistry() {

// Set up registry array
$registry = ['blueprints' => [], 'templates' => [], 'pageModels' => []];
// Add modules in site/modules
$modulesFolder = kirby()->root('site') . "/modules";
foreach (Dir::dirs($modulesFolder) as $folder) {
$blueprint = $modulesFolder . "/". $folder . "/" . $folder . ".yml";
$template = $modulesFolder . "/". $folder . "/" . $folder . ".php";
if(F::exists($blueprint)) {
$blueprintArray = Yaml::read($blueprint);
if(!array_key_exists('status', $blueprintArray)) {
$blueprintArray['status'] = [
'draft' => true,
'listed' => true,
];
}
$registry['blueprints']['pages/module.'. $folder] = $blueprintArray;
$registry['templates']['module.'. $folder] = $template;
$registry['pageModels']['module.'. $folder] = option('medienbaecker.modules.model', 'ModulePage');
}
}
// Add legacy modules to registry
$moduleBlueprints = array_filter(kirby()->blueprints(), function($blueprint) {
return Str::startsWith($blueprint, 'module.');
});
if(!empty($moduleBlueprints)) {
$blueprintsFolder = kirby()->root('blueprints');
$snippetsFolder = kirby()->root('snippets');
foreach($moduleBlueprints as $moduleBlueprint) {
$blueprint = $blueprintsFolder . "/pages/" . $moduleBlueprint . ".yml";
$template = $snippetsFolder . "/modules/" . $moduleBlueprint . ".php";
if(F::exists($blueprint)) {
$blueprintArray = Yaml::read($blueprint);
if(!array_key_exists('status', $blueprintArray)) {
$blueprintArray['status'] = [
'draft' => true,
'listed' => true,
];
}
if(!array_key_exists('pages/'. $moduleBlueprint, $registry['blueprints'])) {
$registry['blueprints']['pages/'. $moduleBlueprint] = $blueprintArray;
}
if(!array_key_exists($moduleBlueprint, $registry['templates'])) {
$registry['templates'][$moduleBlueprint] = $template;
}
if(!array_key_exists($moduleBlueprint, $registry['pageModels'])) {
$registry['pageModels'][$moduleBlueprint] = option('medienbaecker.modules.model', 'ModulePage');
}
}
}
}
// Add modules container blueprint with redirect field
$registry['blueprints']['pages/modules'] = [
'title' => 'Modules',
'options' => [
'changeSlug' => false,
'changeStatus' => false,
'changeTemplate' => false
],
'fields' => [
'modules_redirect' => true
]
];
// Add modules container model
$registry['pageModels']['modules'] = 'ModulesPage';
return $registry;
// Set up registry array
$registry = ['blueprints' => [], 'templates' => [], 'pageModels' => []];
// Add modules in site/modules
$modulesFolder = kirby()->root('site') . "/modules";
foreach (Dir::dirs($modulesFolder) as $folder) {
$blueprint = $modulesFolder . "/". $folder . "/" . $folder . ".yml";
$template = $modulesFolder . "/". $folder . "/" . $folder . ".php";
if(F::exists($blueprint)) {
$blueprintArray = Yaml::read($blueprint);
if(!array_key_exists('status', $blueprintArray)) {
$blueprintArray['status'] = [
'draft' => true,
'listed' => true,
];
}
$registry['blueprints']['pages/module.'. $folder] = $blueprintArray;
$registry['templates']['module.'. $folder] = $template;
$registry['pageModels']['module.'. $folder] = option('medienbaecker.modules.model', 'ModulePage');
}
}
// Add legacy modules to registry
$moduleBlueprints = array_filter(kirby()->blueprints(), function($blueprint) {
return Str::startsWith($blueprint, 'module.');
});
if(!empty($moduleBlueprints)) {
$blueprintsFolder = kirby()->root('blueprints');
$snippetsFolder = kirby()->root('snippets');
foreach($moduleBlueprints as $moduleBlueprint) {
$blueprint = $blueprintsFolder . "/pages/" . $moduleBlueprint . ".yml";
$template = $snippetsFolder . "/modules/" . $moduleBlueprint . ".php";
if(F::exists($blueprint)) {
$blueprintArray = Yaml::read($blueprint);
if(!array_key_exists('status', $blueprintArray)) {
$blueprintArray['status'] = [
'draft' => true,
'listed' => true,
];
}
if(!array_key_exists('pages/'. $moduleBlueprint, $registry['blueprints'])) {
$registry['blueprints']['pages/'. $moduleBlueprint] = $blueprintArray;
}
if(!array_key_exists($moduleBlueprint, $registry['templates'])) {
$registry['templates'][$moduleBlueprint] = $template;
}
if(!array_key_exists($moduleBlueprint, $registry['pageModels'])) {
$registry['pageModels'][$moduleBlueprint] = option('medienbaecker.modules.model', 'ModulePage');
}
}
}
}
// Add modules container blueprint with redirect field
$registry['blueprints']['pages/modules'] = [
'title' => 'Modules',
'options' => [
'changeSlug' => false,
'changeStatus' => false,
'changeTemplate' => false
],
'fields' => [
'modules_redirect' => true
]
];
// Add modules container model
$registry['pageModels']['modules'] = 'ModulesPage';
return $registry;
}
74 changes: 35 additions & 39 deletions lib/models.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,43 @@
use Kirby\Cms\Template;

class ModulePage extends Page {
public static function create(array $props) {
if (option('medienbaecker.modules.autopublish', false)) {
$props['num'] = 9999;
}
return parent::create($props);
}
public function url($options = null): string {
return $this->parents()->filterBy('intendedTemplate', '!=', 'modules')->first()->url() . '#' . $this->slug();
}
public function render(array $data = [], $contentType = 'html'): string {
go($this->parents()->filterBy('intendedTemplate', '!=', 'modules')->first()->url() . '#' . $this->slug());
}
public function renderModule() {
$moduleTemplate = new Template($this->intendedTemplate());
echo $moduleTemplate->render([
'page' => $this->parent()->parent(),
'module' => $this,
'site' => $this->site()
]);
}
public function moduleName() {
return $this->blueprint()->title();
}
public function moduleId() {
return str_replace('.', '__', $this->intendedTemplate());
}
public function parents(){
$parents = parent::parents();
return $parents->filter('slug', '!=', 'modules');
public static function create(array $props) {
if (option('medienbaecker.modules.autopublish', false)) {
$props['num'] = 9999;
}
return parent::create($props);
}
public function url($options = null): string {
return $this->parents()->filterBy('intendedTemplate', '!=', 'modules')->first()->url() . '#' . $this->slug();
}
public function render(array $data = [], $contentType = 'html'): string {
go($this->parents()->filterBy('intendedTemplate', '!=', 'modules')->first()->url() . '#' . $this->slug());
}
public function renderModule() {
$moduleTemplate = new Template($this->intendedTemplate());
echo $moduleTemplate->render([
'page' => $this->parent()->parent(),
'module' => $this,
'site' => $this->site()
]);
}
public function moduleName() {
return $this->blueprint()->title();
}
public function moduleId() {
return str_replace('.', '__', $this->intendedTemplate());
}
public function parents(){
$parents = parent::parents();
return $parents->filter('slug', '!=', 'modules');
}
}

class ModulesPage extends Page {
public function url($options = null): string {
return $this->parent()->url();
}
public function render(array $data = [], $contentType = 'html'): string {
go($this->parent()->url());
}
public function parents(){
$parents = parent::parents();
return $parents->filter('slug', '!=', 'modules');
}
public function url($options = null): string {
return $this->parent()->url();
}
public function render(array $data = [], $contentType = 'html'): string {
go($this->parent()->url());
}
}
Loading

0 comments on commit e7e358e

Please sign in to comment.