Skip to content

Commit 47673b0

Browse files
committed
Merge pull request #6 from jaedb/2.0
2.0
2 parents 4308180 + ec45f2d commit 47673b0

11 files changed

Lines changed: 100 additions & 231 deletions

File tree

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@ Manage site-wide modules (aka widgets) and select the pages on which they are to
2020
# Usage
2121

2222
### Create a module area
23-
1. Within the *Module Manager* admin, create a new ModulePosition object. The `Alias` field will be automatically generated, or you can enter your custom alias name.
23+
1. Edit your `mysite/_config/config.yml` file to add any additional module areas. Use the following format:
24+
```
25+
ModuleManager:
26+
positions:
27+
- 'module-name-here'
28+
```
29+
2430
2. In your template, use the code `$ModulePosition(alias)` where alias is your position's alias string.
2531
3. Flush your template cache (`?flush=all`)
2632

_config.php

100644100755
File mode changed.

_config/config.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ModuleManager:
2+
positions:
3+
- 'sidebar'

code/Admin/ModuleManager.php

100644100755
Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
<?php
22

33
class ModuleManager extends DataObject {
4-
4+
5+
private static $db = array(
6+
'Positions' => 'Text'
7+
);
8+
59
public function getCMSFields() {
610
$fields = FieldList::create( $tabSet = TabSet::create('Root'));
711
return $fields;
@@ -11,28 +15,83 @@ public function setRequest($req){
1115
return false;
1216
}
1317

18+
public function CMSEditLink() {
19+
return singleton('ModuleManagerController')->Link();
20+
}
21+
22+
public function populateDefaults() {
23+
$this->Positions = 'asdfasdfasfd';//serialize( array('header','footer','sidebar') );
24+
25+
// Allow these defaults to be overridden
26+
parent::populateDefaults();
27+
}
28+
1429
public function requireDefaultRecords() {
1530
parent::requireDefaultRecords();
16-
$moduleManager = ModuleManager::get()->First();
31+
$moduleManager = DataObject::get_one('ModuleManager');
1732
if(!$moduleManager) {
1833
self::make_module_manager();
1934
DB::alteration_message("Added default module manager","created");
2035
}
2136
}
2237

23-
static public function make_module_manager() {
38+
public static function make_module_manager() {
2439
$moduleManager = ModuleManager::create();
2540
$moduleManager->write();
2641
return $moduleManager;
2742
}
2843

29-
public static function CurrentModuleManager(){
30-
$current = ModuleManager::get()->First();
31-
return $current;
44+
/**
45+
* Get the current site's ModuleManager, and creates a new one through
46+
* {@link make_module_manager()} if none is found.
47+
*
48+
* @return ModuleManager
49+
*/
50+
public static function current_module_manager() {
51+
if( $moduleManager = DataObject::get_one('ModuleManager') ){
52+
return $moduleManager;
53+
}
54+
55+
return self::make_module_manager();
3256
}
3357

34-
public function CMSEditLink() {
35-
return singleton('ModuleManagerController')->Link();
58+
59+
/**
60+
* Get the positions
61+
* @return array
62+
**/
63+
public static function get_positions(){
64+
return ModuleManager::config()->positions;
65+
}
66+
67+
68+
/**
69+
* Build positions array into dropdown key => value format for CMS fields
70+
* @return array
71+
**/
72+
public static function get_positions_dropdown(){
73+
$array = [];
74+
foreach( ModuleManager::config()->positions as $position ){
75+
$array[$position] = $position;
76+
}
77+
return $array;
78+
}
79+
80+
81+
/**
82+
* Get the positions in string format (for building Enum values)
83+
* @return array
84+
**/
85+
public static function getPositionsAsString(){
86+
$positions = self::getPositions();
87+
$string = '';
88+
foreach( $positions as $position ){
89+
if( $string != '' ){
90+
$string .= ',';
91+
}
92+
$string .= '"'.$position.'"';
93+
}
94+
return $string;
3695
}
3796

3897
}

code/Admin/ModuleManagerController.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function getResponseNegotiator() {
2626
public function getEditForm($id = null, $fields = null) {
2727

2828
// get the cmsfields from ModuleManager DataObject
29-
$moduleManager = ModuleManager::CurrentModuleManager();
29+
$moduleManager = ModuleManager::current_module_manager();
3030
$fields = $moduleManager->getCMSFields();
3131

3232
// what pages is this module active on
@@ -36,21 +36,22 @@ public function getEditForm($id = null, $fields = null) {
3636
Module::get(),
3737
$modulesGridFieldConfig = GridFieldConfig_RecordEditor::create()
3838
);
39-
$modulePositionsGridField = GridField::create(
40-
"ModulePositions_Gridfield",
41-
"Module Positions",
42-
ModulePosition::get(),
43-
GridFieldConfig_RecordEditor::create()
44-
);
4539

4640
// add multiclass dropdown for modules
4741
$modulesGridFieldConfig->removeComponentsByType('GridFieldAddNewButton');
4842
$modulesGridFieldConfig->addComponent(new GridFieldAddNewMultiClass());
4943

5044
// add the fields
5145
$fields->addFieldToTab('Root.Modules', $modulesGridField);
52-
$fields->addFieldToTab('Root.ModulePositions', $modulePositionsGridField);
53-
$fields->addFieldToTab('Root.ModulePositions', LiteralField::create('html','<em>To load a position into your template, simply write <code>$ModulePosition(Alias)</code> where <code>Alias</code> is your position alias</em>'));
46+
47+
// module positions tab
48+
$positionsHtml = '<h2>Module positions</h2>';
49+
$positionsHtml .= '<p class="message info">To change these you need to edit the positions specified in the <code>_config.php</code> file. These are your currently configured positions available:</p>';
50+
foreach( ModuleManager::config()->positions as $position ){
51+
$positionsHtml .= '<p>&bull;&nbsp; <strong>'.$position.'</strong><br />&nbsp; &nbsp; Use in your template with <code>$ModulePosition("'.$position.'");</code></p>';
52+
}
53+
$positionsHtml .= '</ul>';
54+
$fields->addFieldToTab('Root.Positions', LiteralField::create('html',$positionsHtml));
5455

5556
// actions
5657
$actions = $moduleManager->getCMSActions();

code/DataObjects/Module.php

Lines changed: 8 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,9 @@ class Module extends DataObject {
1111

1212
// set object parameters
1313
private static $db = array(
14-
'Title' => 'Varchar(128)',
14+
'Title' => 'Varchar(255)',
1515
'Content' => 'HTMLText',
16-
'Alias' => 'Text'
17-
);
18-
19-
private static $has_one = array(
20-
'Position' => 'ModulePosition'
16+
'Position' => 'Varchar(255)'
2117
);
2218

2319
private static $belongs_many_many = array(
@@ -27,7 +23,7 @@ class Module extends DataObject {
2723
private static $summary_fields = array(
2824
'Type' => 'Type',
2925
'Title' => 'Title',
30-
'PositionNameNice' => 'Position',
26+
'Position' => 'Position',
3127
'Pages.Count' => 'Number of pages'
3228
);
3329

@@ -36,16 +32,6 @@ class Module extends DataObject {
3632
'PositionID'
3733
);
3834

39-
/**
40-
* The position name that this module is assigned to
41-
* @return string
42-
**/
43-
public function PositionNameNice(){
44-
if( $this->Position()->ID > 0 )
45-
return $this->Position()->Title .' ('.$this->Position()->Alias.')';
46-
return false;
47-
}
48-
4935
/**
5036
* Identify this page component type
5137
* Used in GridField for type identification
@@ -81,52 +67,17 @@ public function getCMSFields() {
8167
$fields->addFieldToTab('Root.Main', LiteralField::create('html','<p><em>'.$this->getDescription().'</em></p><br />') );
8268

8369
$fields->addFieldToTab('Root.Main', TextField::create('Title', 'Title'));
84-
$fields->addFieldToTab('Root.Main', TextField::create('Alias', 'Alias (unique identifier)'));
8570
$fields->addFieldToTab('Root.Main', DropdownField::create(
86-
'PositionID',
8771
'Position',
88-
$this->GetModulePositions()
89-
));
72+
'Position',
73+
ModuleManager::get_positions_dropdown()
74+
)->setEmptyString('Please select'));
75+
$fields->addFieldToTab("Root.Main", TreeMultiselectField::create("Pages", "Shown on pages", "SiteTree"));
9076
$fields->addFieldToTab('Root.Main', HTMLEditorField::create('Content', 'Content') );
9177

92-
$pagesField = TreeMultiselectField::create("Pages", "Shown on pages", "SiteTree");
93-
$fields->addFieldToTab("Root.Main", $pagesField, 'Content');
94-
9578
return $fields;
9679
}
9780

98-
/**
99-
* List all ModulePosition objects
100-
* @return array
101-
**/
102-
function GetModulePositions(){
103-
104-
if($Positions = DataObject::get('ModulePosition')){
105-
106-
// construct container
107-
$map = array();
108-
109-
// loop each position and inject into map
110-
foreach( $Positions as $Position ){
111-
$map[$Position->ID] = $Position->Title .' ('.$Position->Alias.')';
112-
}
113-
114-
return $map;
115-
}else{
116-
return array('You need to create a Module Position first');
117-
}
118-
}
119-
120-
/**
121-
* Get this ModulePosition's name
122-
* @return string
123-
**/
124-
function ModulePositionName(){
125-
126-
$position = ModulePosition::get()->byID($this->ModulePosition);
127-
return $position->Title;
128-
}
129-
13081
/**
13182
* Render the module-wrapper template
13283
* @return HTMLText
@@ -142,57 +93,5 @@ public function ModuleLayout(){
14293
// TODO: Make this work. To make it work we actually need to properly check if we have a custom template for this class.
14394

14495
return $output;
145-
}
146-
147-
148-
/**
149-
* Convert string into url-friendly string
150-
* @param $string = string (ie the title)
151-
* @return string
152-
**/
153-
public function URLFriendly( $string ){
154-
155-
// replace non letter or digits by -
156-
$string = preg_replace('~[^\\pL\d]+~u', '-', $string);
157-
$string = trim($string, '-');
158-
159-
// transliterate
160-
$string = iconv('utf-8', 'us-ascii//TRANSLIT', $string);
161-
162-
// lowercase
163-
$string = strtolower($string);
164-
165-
// remove unwanted characters
166-
$string = preg_replace('~[^-\w]+~', '', $string);
167-
168-
if (empty($string))
169-
return 'n-a';
170-
171-
return $string;
172-
}
173-
174-
// before saving, check alias
175-
public function onBeforeWrite(){
176-
177-
parent::onBeforeWrite();
178-
179-
// convert name to lowercase, dashed
180-
$newAlias = $this->URLFriendly($this->Title);
181-
182-
// get positions that already have this alias
183-
$moduleAliasesThatMatch = ModulePosition::get()->Filter('Alias',$newAlias)->First();
184-
185-
// if we find a match
186-
if( isset($moduleAliasesThatMatch->ID) ){
187-
188-
// create a new unique alias (based on ID)
189-
$this->Alias = $newAlias .'-'. $this->ID;
190-
191-
// no match, meaning we're safe to use this as a unique alias
192-
}else{
193-
$this->Alias = $newAlias;
194-
}
195-
196-
}
197-
96+
}
19897
}

0 commit comments

Comments
 (0)