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

Ss4 php74 upgrade #8

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions _config/project.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
MenuItem:
Heyday\MenuManager\MenuItem:
extensions:
- MenuItemSquared
- Marketo\Heyday\MenuItemSquared

MenuAdmin:
Heyday\MenuManager\MenuAdmin:
extensions:
- MenuAdminSquared
- Marketo\Heyday\MenuSetSquared

MenuSet:
Heyday\MenuManager\MenuSet:
extensions:
- MenuSetSquared
- Marketo\Heyday\MenuSetSquared
69 changes: 63 additions & 6 deletions code/MenuAdminSquared.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,74 @@
<?php

namespace Marketo\Heyday;

use SilverStripe\Forms\Form;
use Heyday\MenuManager\MenuSet;
use SilverStripe\ORM\DataExtension;
use SilverStripe\Forms\GridField\GridField;
use Marketo\Heyday\MenuItemSquaredGridFieldConfig;
use SilverStripe\Forms\GridField\GridFieldExportButton;

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

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

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

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

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

// 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',
'MenuSetID' => 'MenuSetID',
'ImageID' => 'ImageID',
'ParentItemID' => 'ParentItemID',
]);
}
}
}
17 changes: 13 additions & 4 deletions code/MenuItem_Separator.php → code/MenuItemSeparator.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
<?php

namespace Marketo\Heyday;

use Heyday\MenuManager\MenuItem;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\HeaderField;
use SilverStripe\Security\PermissionProvider;

/**
* Class MenuItem
* Class MenuItemSeparator
*/
class MenuItem_Separator extends MenuItem implements PermissionProvider
class MenuItemSeparator 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 +68,7 @@ public function onBeforeWrite()
$this->Link = '';
$this->IsNewWindow = 0;

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

}
138 changes: 94 additions & 44 deletions code/MenuItemSquared.php
Original file line number Diff line number Diff line change
@@ -1,85 +1,130 @@
<?php

namespace Marketo\Heyday;

use SilverStripe\Assets\Image;
use Heyday\MenuManager\MenuSet;
use Heyday\MenuManager\MenuItem;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\FormField;
use SilverStripe\Forms\LabelField;
use SilverStripe\ORM\DataExtension;
use SilverStripe\Core\Config\Config;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\AssetAdmin\Forms\UploadField;
use Symbiote\GridFieldExtensions\GridFieldAddNewMultiClass;
use Marketo\Heyday\MenuItemSquaredGridFieldConfig;

/**
* Class MenuItemSquared
*
* @method HasManyList ChildItems
* @method MenuItem ParentItem
* @see MenuItem
*/
class MenuItemSquared extends DataExtension
{

private static $db = [
'Name' => 'Varchar(255)',
];

private static $has_one = [
'Image' => 'Image',
'ParentItem' => 'MenuItem',
'Image' => Image::class,
'ParentItem' => MenuItem::class,
];

private static $has_many = [
'ChildItems' => 'MenuItem',
'ChildItems' => MenuItem::class,
];

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

public function IsNewWindowNice()
{
return $this->IsNewWindow ? 'Yes' : 'No';
}

/**
* @param FieldList $fields
*/
public function updateCMSFields(FieldList $fields)
{
if (!$this->owner->config()->disable_image) {
/** @var MenuItem|MenuItemSquared $owner */
$owner = $this->owner;
if (!$owner->config()->disable_image) {
$fields->push(new UploadField('Image', 'Image'));
}

if (!$this->owner->config()->disable_hierarchy) {
if ($this->owner->ID != null) {
$AllParentItems = $this->owner->getAllParentItems();
$TopMenuSet = $this->owner->TopMenuSet();
$depth = 1;

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 (!$owner->config()->disable_hierarchy) {
if ($owner->ID != null) {
$ascendants = $owner->getAllParentItems();
$topMenuSet = $owner->TopMenuSet();
$topMenuName = $topMenuSet->Name;

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

if (!is_numeric($maxDepth) || $maxDepth < 0) {
$maxDepth = 1;
}

if (!empty($AllParentItems) && count($AllParentItems) >= $depth) {
$fields->push(new LabelField('MenuItems', 'Max Sub Menu Depth Limit'));
} else {
$fields->push(
new GridField(
'MenuItems',
'Sub Menu Items',
$this->owner->ChildItems(),
$config = GridFieldConfig_RecordEditor::create()
)
);
$config->addComponent(new GridFieldOrderableRows('Sort'));
$config->removeComponentsByType('GridFieldAddNewButton');
$multiClass = new GridFieldAddNewMultiClass();
$classes = ClassInfo::subclassesFor('MenuItem');
$multiClass->setClasses($classes);
$config->addComponent($multiClass);
$gridFieldConfig = new MenuItemSquaredGridFieldConfig();
if (count($ascendants) >= $maxDepth) {
$fields->push(new LabelField('MenuItems', 'Max Depth Limit Reached, Update Config to Add Sub Menu Items'));
$gridFieldConfig->removeComponentsByType(GridFieldAddNewMultiClass::class);
}
// Keep GridField in case of import or max depth changed.
$fields->push(new GridField(
'MenuItems',
'Sub Menu Items',
$this->owner->ChildItems(),
$gridFieldConfig
));
} else {
$fields->push(new LabelField('MenuItems', 'Save This Menu Item Before Adding Sub Menu Items'));
}
}
}

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

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

/**
* Create a key value pair of ChildID => Parent relationships.
* Starts with itself, stops at circular relationships.
*
* @return array
*/
public function getAllParentItems()
{
$WorkingItem = $this->owner;
$ParentItems = [];
/** @var MenuItem|MenuItemSquared $current */
$current = $this->owner;
$parents = [];

while ($WorkingItem->ParentItemID && $WorkingItem->ParentItem() && $WorkingItem->ParentItem()->ID && !isset($ParentItems[$WorkingItem->ParentItem()->ID])) {
$ParentItems[$WorkingItem->ID] = $WorkingItem->ParentItem();
$WorkingItem = $ParentItems[$WorkingItem->ID];
while ($current->ParentItemID && $current->ParentItem() && $current->ParentItem()->ID && !isset($parents[$current->ParentItem()->ID])) {
$parents[$current->ID] = $current->ParentItem();
$current = $parents[$current->ID];
}
return $ParentItems;

return $parents;
}

public function onBeforeWrite()
Expand All @@ -95,15 +140,20 @@ public function onBeforeWrite()

public function onBeforeDelete()
{
/** @var MenuItem $childItem */
foreach ($this->owner->ChildItems() as $childItem) {
$childItem->delete();
}
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());
}
}
31 changes: 31 additions & 0 deletions code/MenuItemSquaredGridFieldConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Marketo\Heyday;

use SilverStripe\Core\ClassInfo;
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
use Symbiote\GridFieldExtensions\GridFieldAddNewMultiClass;
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;

/**
* 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);
}
}
Loading