Skip to content
This repository was archived by the owner on Jan 25, 2021. It is now read-only.

Commit 7afbbfb

Browse files
committed
Initial commit
0 parents  commit 7afbbfb

File tree

6 files changed

+445
-0
lines changed

6 files changed

+445
-0
lines changed

etc/template.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Kirby\Modules;
4+
5+
// Redirect to the page where the module appears
6+
if($page->parent()->uid() === Modules::uid()) {
7+
go($page->parent()->parent());
8+
} else {
9+
go($page->parent());
10+
}

lib/module.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Kirby\Modules;
4+
5+
// Kirby dependencies
6+
use Error;
7+
use Obj;
8+
use Page;
9+
use Str;
10+
11+
/**
12+
* Module
13+
*
14+
* @package Kirby Modules Plugin
15+
* @author Lukas Bestle <[email protected]>
16+
* @license MIT
17+
*/
18+
class Module extends Obj {
19+
public $name;
20+
public $template;
21+
public $blueprintFile;
22+
public $snippetFile;
23+
24+
/**
25+
* Class constructor
26+
*
27+
* @param string $name Name of the module directory or module page
28+
*/
29+
public function __construct($name) {
30+
$templatePrefix = Modules::templatePrefix();
31+
32+
// Get the module name from the template if a page is given
33+
if(is_a($name, 'Page')) {
34+
// Validate that the page is a module
35+
if(!str::startsWith($name->intendedTemplate(), $templatePrefix)) {
36+
throw new Error('The given page is no module.');
37+
}
38+
39+
$prefixLength = str::length($templatePrefix);
40+
$name = str::substr($name->intendedTemplate(), $prefixLength);
41+
}
42+
43+
$this->name = $name;
44+
$this->template = $templatePrefix . $name;
45+
46+
// Store the file paths of the module
47+
$basePath = Modules::directory() . DS . $name;
48+
$this->blueprintFile = $basePath . DS . $name . '.yml';
49+
$this->snippetFile = $basePath . DS . $name . '.html.php';
50+
}
51+
52+
/**
53+
* Validates if the module is valid and active
54+
*
55+
* @return boolean
56+
*/
57+
public function validate() {
58+
return is_file($this->blueprintFile) && is_file($this->snippetFile);
59+
}
60+
}

lib/modules.php

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
<?php
2+
3+
namespace Kirby\Modules;
4+
5+
// Kirby dependencies
6+
use C;
7+
use Dir;
8+
use Error;
9+
use Str;
10+
use Tpl;
11+
12+
/**
13+
* Modules
14+
*
15+
* @package Kirby Modules Plugin
16+
* @author Lukas Bestle <[email protected]>
17+
* @license MIT
18+
*/
19+
class Modules {
20+
protected $page;
21+
22+
// Caches
23+
protected $modules;
24+
protected static $allModules;
25+
26+
/**
27+
* Class constructor
28+
*
29+
* @param Page $page Kirby page that contains modules
30+
*/
31+
public function __construct($page) {
32+
$this->page = $page;
33+
}
34+
35+
/**
36+
* Outputs all modules of the given page
37+
*
38+
* @param array $data Optional additional data to pass to each module
39+
* @param boolean $return Whether to output or return the module string
40+
* @return string
41+
*/
42+
public function output($data = array(), $return = false) {
43+
$result = '';
44+
45+
// Loop through all valid modules in the file system order
46+
foreach($this->modules() as $module) {
47+
$moduleObj = new Module($module);
48+
$moduleName = $moduleObj->name();
49+
50+
// Use the additional data but make sure that $module and $moduleName always win
51+
$moduleData = array_merge($data, compact('module', 'moduleName'));
52+
$result .= tpl::load($moduleObj->snippetFile(), $moduleData, true);
53+
}
54+
55+
if($return) return $result;
56+
echo $result;
57+
}
58+
59+
/**
60+
* Returns a collection of module subpages of the given page
61+
*
62+
* @return Pages
63+
*/
64+
public function modules() {
65+
// Return from cache if possible
66+
if($this->modules) return $this->modules;
67+
68+
// Determine where the modules live
69+
if($childPage = $this->page->find(static::parentUid())) {
70+
// Modules child page exists, use its children
71+
$modules = $childPage->children();
72+
} else {
73+
// Try to use the direct subpages (filtered below)
74+
$modules = $this->page->children();
75+
}
76+
77+
// Filter the modules by visibility and valid module
78+
$modules = $modules->visible()->filter(function($page) {
79+
try {
80+
$module = new Module($page);
81+
return $module->validate();
82+
} catch(Error $e) {
83+
return false;
84+
}
85+
});
86+
87+
return $this->modules = $modules;
88+
}
89+
90+
/**
91+
* Registers the page method and all blueprints within Kirby
92+
* Called only once when the plugin is loaded
93+
*/
94+
public static function register() {
95+
$kirby = kirby();
96+
97+
// Register $page->modules($data, $return) method
98+
// Calling it will call the modules() helper
99+
$kirby->set('page::method', 'modules', 'modules');
100+
101+
// Register blueprints and dummy templates for all modules
102+
foreach(static::allModules() as $module) {
103+
$kirby->set('blueprint', $module->template(), $module->blueprintFile());
104+
$kirby->set('template', $module->template(), dirname(__DIR__) . DS . 'etc' . DS . 'template.php');
105+
}
106+
}
107+
108+
/**
109+
* Returns the base directory for the modules
110+
* Can be changed with the modules.directory option
111+
*
112+
* @return string
113+
*/
114+
public static function directory() {
115+
return c::get('modules.directory', kirby()->roots()->site() . DS . 'modules');
116+
}
117+
118+
/**
119+
* Returns the UID of the modules pages
120+
* Can be changed with the modules.parent.uid option
121+
*
122+
* @return string
123+
*/
124+
public static function parentUid() {
125+
return c::get('modules.parent.uid', 'modules');
126+
}
127+
128+
/**
129+
* Returns the template prefix for modules
130+
* Can be changed with the modules.template.prefix option
131+
*
132+
* @return string
133+
*/
134+
public static function templatePrefix() {
135+
return c::get('modules.template.prefix', 'module.');
136+
}
137+
138+
/**
139+
* Returns an array of all Module objects
140+
*
141+
* @return array
142+
*/
143+
public static function allModules() {
144+
// Return from cache if possible
145+
if(static::$allModules) return static::$allModules;
146+
$allModules = array();
147+
148+
// Read the modules directory
149+
$basePath = static::directory();
150+
foreach(dir::read($basePath) as $name) {
151+
$path = $basePath . DS . $name;
152+
if(!is_dir($path)) continue;
153+
154+
$module = new Module($name);
155+
if($module->validate()) $allModules[$name] = $module;
156+
}
157+
158+
return static::$allModules = $allModules;
159+
}
160+
}

modules.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/**
4+
* Kirby Modules Plugin
5+
*
6+
* @author Lukas Bestle <[email protected]>
7+
*/
8+
9+
// Define autoloader
10+
load(array(
11+
'kirby\\modules\\modules' => __DIR__ . DS . 'lib' . DS . 'modules.php',
12+
'kirby\\modules\\module' => __DIR__ . DS . 'lib' . DS . 'module.php',
13+
));
14+
15+
// Register page method and blueprints
16+
Kirby\Modules\Modules::register();
17+
18+
/**
19+
* Helper function to output modules for a given page
20+
* You can also use $page->modules($data, $return)
21+
*
22+
* @param Page $page Kirby page that contains modules
23+
* @param array $data Optional additional data to pass to each module
24+
* @param boolean $return Whether to output or return the module string
25+
* @return string
26+
*/
27+
function modules($page, $data = array(), $return = false) {
28+
$modules = new Kirby\Modules\Modules($page);
29+
return $modules->output($data, $return);
30+
}

package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "modules",
3+
"description": "Kirby Modules Plugin",
4+
"author": "Lukas Bestle <[email protected]>",
5+
"license": "MIT",
6+
"version": "1.0.0",
7+
"type": "kirby-plugin"
8+
}

0 commit comments

Comments
 (0)