Skip to content

Commit

Permalink
Merge pull request #16 from muskie9/enhancement/pageSectionBlock
Browse files Browse the repository at this point in the history
ENHANCEMENT PageSectionBlock
  • Loading branch information
muskie9 authored Dec 7, 2016
2 parents b1ac67e + 2bad8c0 commit 38ef728
Show file tree
Hide file tree
Showing 4 changed files with 242 additions and 1 deletion.
60 changes: 60 additions & 0 deletions code/blocks/PageSectionBlock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/**
* Class PageSectionBlock
*
* @method HasManyList $Sections
*/
class PageSectionBlock extends Block
{

/**
* @var string
*/
private static $singular_name = 'Page Section Block';

/**
* @var string
*/
private static $plural_name = 'Page Section Blocks';

/**
* @var array
*/
private static $has_many = [
'Sections' => 'PageSectionObject',
];

/**
* @return FieldList
*/
public function getCMSFields()
{
$fields = parent::getCMSFields();

if ($this->ID) {
// Sections
$config = GridFieldConfig_RecordEditor::create();
if (class_exists('GridFieldSortableRows')) {
$config->addComponent(new GridFieldSortableRows('SortOrder'));
}
$config->removeComponentsByType('GridFieldAddExistingAutocompleter');
$config->removeComponentsByType('GridFieldDeleteAction');
$config->addComponent(new GridFieldDeleteAction(false));
$sectionsField = GridField::create('Sections', 'Sections', $this->Sections()->sort('SortOrder'), $config);
$fields->addFieldsToTab('Root.Sections', array(
$sectionsField,
));
}

return $fields;
}

/**
* @return mixed
*/
public function getPageSections()
{
return $this->Sections()->sort('SortOrder');
}
}
144 changes: 144 additions & 0 deletions code/objects/PageSectionObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php

/**
* Class PageSectionObject
*
* @property string $Name
* @property string $Title
* @property HTMLText $Content
* @property int $SortOrder
* @property int $ImageID
* @property int $PageSectionBlockID
*/
class PageSectionObject extends DataObject
{
/**
* @return string
*/
private static $singular_name = 'Page Section';

/**
* @return string
*/
private static $plural_name = 'Page Sections';

/**
* @var array
*/
private static $db = array(
'Name' => 'Varchar(255)',
'Title' => 'Varchar(255)',
'Content' => 'HTMLText',
'SortOrder' => 'Int',
);

/**
* @var array
*/
private static $has_one = array(
'Image' => 'Image',
'PageSectionBlock' => 'PageSectionBlock',
);

/**
* @var string
*/
private static $default_sort = 'Name ASC';

/**
* @var array
*/
private static $summary_fields = array(
'Image.CMSThumbnail' => 'Image',
'Name' => 'Name',
'Title' => 'Title',
);

/**
* @var array
*/
private static $searchable_fields = array(
'Name' => 'Name',
'Title' => 'Title',
);

/**
* @return FieldList
*/
public function getCMSFields()
{
$fields = parent::getCMSFields();

$fields->removeByName(array(
'PageSectionBlockID',
'SortOrder',
));

$fields->dataFieldByName('Name')->setDescription('For internal reference only');

$image = $fields->dataFieldByName('Image');
$image->setFolderName('Uploads/PageSections');
$fields->insertBefore($image, 'Content');

return $fields;
}

/**
* @return ValidationResult
*/
public function validate()
{
$result = parent::validate();

if (!$this->Name) {
$result->error('Name is requied before you can save');
}

return $result;
}

/**
* Set permissions, allow all users to access by default.
* Override in descendant classes, or use PermissionProvider.
*/

/**
* @param null $member
*
* @return bool
*/
public function canCreate($member = null)
{
return true;
}

/**
* @param null $member
*
* @return bool
*/
public function canView($member = null)
{
return true;
}

/**
* @param null $member
*
* @return bool
*/
public function canEdit($member = null)
{
return true;
}

/**
* @param null $member
*
* @return bool
*/
public function canDelete($member = null)
{
return true;
}
}
14 changes: 13 additions & 1 deletion tests/Fixtures.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,16 @@ FormBlock:
Title: "Form"
VideoBlock:
one:
Title: "Video One"
Title: "Video One"
PageSectionObject:
one:
Name: "Page Section One Name"
Title: "Page Section One Title"
Content: "<p>Page Section Vivamus magna justo, lacinia eget consectetur sed, convallis at tellus. Donec sollicitudin molestie malesuada. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur arcu erat, accumsan id imperdiet et, porttitor at sem. Nulla porttitor accumsan tincidunt. Donec rutrum congue leo eget malesuada. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec velit neque, auctor sit amet aliquam vel, ullamcorper sit amet ligula. Vivamus suscipit tortor eget felis porttitor volutpat. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur aliquet quam id dui posuere blandit.</p>"
two:
Name: "Page Section One Name"
Title: "Page Section One Title"
Content: "<p>Page Section Vivamus magna justo, lacinia eget consectetur sed, convallis at tellus. Donec sollicitudin molestie malesuada. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur arcu erat, accumsan id imperdiet et, porttitor at sem. Nulla porttitor accumsan tincidunt. Donec rutrum congue leo eget malesuada. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec velit neque, auctor sit amet aliquam vel, ullamcorper sit amet ligula. Vivamus suscipit tortor eget felis porttitor volutpat. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur aliquet quam id dui posuere blandit.</p>"
PageSectionBlock:
one:
Sections: =>PageSectionObject.one,=>PageSectionObject.two
25 changes: 25 additions & 0 deletions tests/PageSectionBlockTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/**
* Class PageSectionBlockTest
*/
class PageSectionBlockTest extends SapphireTest
{

/**
* @var string
*/
protected static $fixture_file = 'dynamic-blocks/tests/Fixtures.yml';

/**
*
*/
public function testGetCMSFields()
{
$object = $this->objFromFixture('PageSectionBlock', 'one');
$fields = $object->getCMSFields();
$this->assertInstanceOf('FieldList', $fields);
$this->assertNotNull($fields->dataFieldByName('Sections'));
}

}

0 comments on commit 38ef728

Please sign in to comment.