Skip to content
This repository has been archived by the owner on Mar 15, 2024. It is now read-only.

Commit

Permalink
Adding MenuItem import and export and extracting GridFieldConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon32 committed Aug 22, 2018
1 parent 0ccb92d commit 6c79700
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 39 deletions.
57 changes: 52 additions & 5 deletions code/MenuAdminSquared.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,64 @@
<?php

/**
* Class MenuAdminSquared
*
* @see MenuAdmin
*/
class MenuAdminSquared extends DataExtension
{
private static $model_importers = [
'MenuItem' => 'CsvBulkLoader',
];

private static $managed_models = [
'MenuItem',
];

/**
* @param CMSForm $form
*/
public function updateEditForm(CMSForm $form)
{
$fields = $form->Fields();
$MenuSet = $fields->dataFieldByName('MenuSet');
$menuSet = $fields->dataFieldByName('MenuSet');

if ($menuSet instanceof GridField) {
$menuSet->setTitle('Menus');
$config = $menuSet->getConfig();

$config->removeComponentsByType('GridFieldExportButton');
$config->removeComponentsByType('GridFieldPrintButton');

if ($MenuSet instanceof GridField) {
$MenuSetConfig = $MenuSet->getConfig();
$MenuSetConfig->removeComponentsByType('GridFieldAddNewButton');
// Only remove add button if set by config.
if (!empty(MenuSet::config()->get('default_sets'))) {
$config->removeComponentsByType('GridFieldAddNewButton');
}
}
}

$menuItems = $fields->dataFieldByName('MenuItem');
if ($menuItems instanceof GridField) {
$menuItems->setTitle('Items');
$config = new MenuItemSquaredGridFieldConfig();
$menuItems->setConfig($config);

$config->removeComponentsByType('GridFieldAddNewMultiClass');

$export = new GridFieldExportButton('buttons-before-left');
$config->addComponent($export);

$export->setExportColumns([
'ID' => 'ID',
'ClassName' => 'ClassName',
'MenuTitle' => 'MenuTitle',
'Link' => 'Link',
'Sort' => 'Sort',
'IsNewWindow' => 'IsNewWindow',
'Name' => 'Name',
'PageID' => 'PageID',
'ImageID' => 'ImageID',
'ParentItemID' => 'ParentItemID',
]);
}
}
}
53 changes: 37 additions & 16 deletions code/MenuItemSquared.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<?php

/**
* Class MenuItemSquared
*
* @see MenuItem
*/
class MenuItemSquared extends DataExtension
{

private static $db = [
'Name' => 'Varchar(255)',
];
Expand All @@ -16,6 +20,16 @@ class MenuItemSquared extends DataExtension
'ChildItems' => 'MenuItem',
];

private static $summary_fields = [
'MenuTitle' => 'Title',
'Page.Title' => 'Page Title',
'Link' => 'Link',
'IsNewWindow' => 'Open in New Window',
];

/**
* @param FieldList $fields
*/
public function updateCMSFields(FieldList $fields)
{
if (!$this->owner->config()->disable_image) {
Expand All @@ -25,16 +39,17 @@ public function updateCMSFields(FieldList $fields)
if (!$this->owner->config()->disable_hierarchy) {
if ($this->owner->ID != null) {
$AllParentItems = $this->owner->getAllParentItems();
$TopMenuSet = $this->owner->TopMenuSet();
$topMenuSet = $this->owner->TopMenuSet();
$topMenuName = $topMenuSet->Name;

$config = MenuSet::config();
$depth = 1;
if (is_array($config->$topMenuName) && isset($config->{$topMenuName}['depth'])) {
$depth = $config->{$topMenuName}['depth'];
}

if (
is_array(MenuSet::config()->{$TopMenuSet->Name}) &&
isset(MenuSet::config()->{$TopMenuSet->Name}['depth']) &&
is_numeric(MenuSet::config()->{$TopMenuSet->Name}['depth']) &&
MenuSet::config()->{$TopMenuSet->Name}['depth'] >= 0
) {
$depth = MenuSet::config()->{$TopMenuSet->Name}['depth'];
if (!is_numeric($depth) || $depth < 0) {
$depth = 1;
}

if (!empty($AllParentItems) && count($AllParentItems) >= $depth) {
Expand All @@ -45,31 +60,32 @@ public function updateCMSFields(FieldList $fields)
'MenuItems',
'Sub Menu Items',
$this->owner->ChildItems(),
$config = GridFieldConfig_RecordEditor::create()
new MenuItemSquaredGridFieldConfig()
)
);
$config->addComponent(new GridFieldOrderableRows('Sort'));
$config->removeComponentsByType('GridFieldAddNewButton');
$multiClass = new GridFieldAddNewMultiClass();
$classes = ClassInfo::subclassesFor('MenuItem');
$multiClass->setClasses($classes);
$config->addComponent($multiClass);
}
} else {
$fields->push(new LabelField('MenuItems', 'Save This Menu Item Before Adding Sub Menu Items'));
}
}
}

/**
* @return mixed
*/
public function TopMenuSet()
{
$AllParentItems = $this->owner->getAllParentItems();
if (!empty($AllParentItems)) {
return end($AllParentItems)->MenuSet();
}

return $this->owner->MenuSet();
}

/**
* @return array
*/
public function getAllParentItems()
{
$WorkingItem = $this->owner;
Expand All @@ -79,6 +95,7 @@ public function getAllParentItems()
$ParentItems[$WorkingItem->ID] = $WorkingItem->ParentItem();
$WorkingItem = $ParentItems[$WorkingItem->ID];
}

return $ParentItems;
}

Expand All @@ -101,9 +118,13 @@ public function onBeforeDelete()
parent::onBeforeDelete();
}

/**
* @return string
*/
public static function get_user_friendly_name()
{
$title = Config::inst()->get(get_called_class(), 'user_friendly_title');

return $title ?: FormField::name_to_label(get_called_class());
}
}
24 changes: 24 additions & 0 deletions code/MenuItemSquaredGridFieldConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* Class MenuItemSquaredGridFieldConfig
*/
class MenuItemSquaredGridFieldConfig extends GridFieldConfig_RecordEditor
{
/**
* @param int $itemsPerPage
*/
public function __construct($itemsPerPage = 25)
{
parent::__construct($itemsPerPage);

$this->removeComponentsByType('GridFieldAddNewButton');

$this->addComponent(new GridFieldOrderableRows('Sort'));
$multiClass = new GridFieldAddNewMultiClass();
$classes = ClassInfo::subclassesFor('MenuItem');
$multiClass->setClasses($classes);

$this->addComponent($multiClass);
}
}
8 changes: 5 additions & 3 deletions code/MenuItem_Separator.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
<?php

/**
* Class MenuItem
* Class MenuItem_Separator
*/
class MenuItem_Separator extends MenuItem implements PermissionProvider
{
/**
* Title for this type of MenuItem to be displayed in the CMS
*
* @var string
* @config
*/
private static $user_friendly_title = 'Separator';

/**
* Disabling image field
*
* @var boolean
* @config
*/
private static $disable_image = true;

/**
* Disabling child fields
*
* @var boolean
* @config
*/
Expand Down Expand Up @@ -58,8 +61,7 @@ public function onBeforeWrite()
$this->Link = '';
$this->IsNewWindow = 0;

$this->PageID = 0;
$this->PageID = 0;
$this->ImageID = 0;
}

}
24 changes: 12 additions & 12 deletions code/MenuSetSquared.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
<?php

/**
* Class MenuSetSquared
*
* @see MenuSet
*/
class MenuSetSquared extends DataExtension
{
private static $singular_name = 'Menu';

private static $plural_name = 'Menus';

/**
* @param FieldList $fields
*/
public function updateCMSFields(FieldList $fields)
{
$menuItem = $fields->dataFieldByName('MenuItems');

if ($menuItem instanceof GridField) {
$menuItemConfig = $menuItem->getConfig();
$menuItemConfig->removeComponentsByType('GridFieldAddNewButton');

$multiClass = new GridFieldAddNewMultiClass();
$classes = ClassInfo::subclassesFor('MenuItem');

$multiClass->setClasses($classes);
$menuItemConfig->addComponent($multiClass);

$menuItemConfig->removeComponentsByType('GridFieldDeleteAction');
$menuItemConfig->addComponent(new GridFieldDeleteAction());
$menuItem->setConfig(new MenuItemSquaredGridFieldConfig());
}
}

}
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "marketo/silverstripe-menumanager-squared",
"description": "SilverStripe MenuManager Squared",
"type": "silverstripe-module",
"keywords": ["silverstripe", "members", "default"],
"keywords": ["silverstripe", "menu", "manager"],
"require": {
"silverstripe/framework": "3.*",
"composer/installers": "*",
Expand All @@ -21,6 +21,5 @@
"name": "Brandon J. Marshall",
"email": "[email protected]"
}
],
"minimum-stability": "dev"
]
}

0 comments on commit 6c79700

Please sign in to comment.