Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring getcmsfields #10

Open
wants to merge 2 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
48 changes: 29 additions & 19 deletions code/CalendarEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,43 +34,49 @@ public function validate()
return $result;
}

/**
* @return FieldList
*/
public function getCMSFields()
{
$this->beforeUpdateCMSFields(function ($fields) {
$datefield = new DateField('Date', 'Date (DD/MM/YYYY)*');
$datefield->setConfig('showcalendar', true);
$datefield->setConfig('dateformat', 'dd/MM/YYYY');

$imagefield = new UploadField('Image', 'Image');
$imagefield->allowedExtensions = array('jpg', 'gif', 'png');
$imagefield->setFolderName("Managed/CalendarImages");
$imagefield->setCanPreviewFolder(false);

$fields->addFieldToTab('Root.Main', new TextField('Title', "Event Title*"));
$fields->addFieldToTab('Root.Main', $datefield);
$fields->addFieldToTab('Root.Main', new TextField('Time', "Time (HH:MM)"));
$fields->addFieldToTab('Root.Main', new TextareaField('Description'));
$fields->addFieldToTab('Root.Main', $imagefield);
});

$fields = parent::getCMSFields();

$this->extend('updateCMSFields', $fields);
// add the new fields in
$fields->addFieldsToTab(
'Root.Main',
array(
TextField::create('Title', "Event Title*"),
DateField::create('Date', 'Date (DD/MM/YYYY)*')
->setConfig('showcalendar', true)
->setConfig('dateformat', 'dd/MM/YYYY'),
TextField::create('Time', "Time (HH:MM)"),
TextareaField::create('Description'),

UploadField::create('Image', 'Image')
->setAllowedExtensions(array('jpg', 'gif', 'png'))
->setFolderName("Managed/CalendarImages")
->setCanPreviewFolder(false)
)
);

$fields->removeFieldFromTab('Root.Main', 'CalendarPageID');

$fields->removeFieldFromTab("Root.Main", "CalendarPageID");
$this->extend('updateCalendarCMSFields', $fields);

return $fields;
}

public function getMonthDigit()
{
$date = strtotime($this->Date);

return date('m', $date);
}

public function getYear()
{
$date = strtotime($this->Date);

return date('Y', $date);
}

Expand All @@ -80,6 +86,7 @@ public function canCreate($members = null)
if ($extended !== null) {
return $extended;
}

return true;
}

Expand All @@ -89,6 +96,7 @@ public function canEdit($members = null)
if ($extended !== null) {
return $extended;
}

return true;
}

Expand All @@ -98,6 +106,7 @@ public function canDelete($members = null)
if ($extended !== null) {
return $extended;
}

return true;
}

Expand All @@ -107,6 +116,7 @@ public function canView($members = null)
if ($extended !== null) {
return $extended;
}

return true;
}
}
15 changes: 10 additions & 5 deletions code/CalendarPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ public function getCMSFields()
{
$fields = parent::getCMSFields();

if ($this->EventTabFirst) {
$fields->insertBefore(new Tab('Events'), 'Main');
}
if ($this->EventTabFirst) $fields->insertBefore(new Tab('Events'), 'Main');

$config = GridFieldConfig_RecordEditor::create();
$gridField = new GridField("Events", "Upcoming Events", $this->Events()->where("Date >= CURRENT_DATE OR Date IS NULL"), $config);
Expand All @@ -47,12 +45,13 @@ class CalendarPage_Controller extends Page_Controller

public function init()
{
parent::init();

if (Director::fileExists(project() . "/css/calendar.css")) {
Requirements::css(project() . "/css/calendar.css");
} else {
Requirements::css("basic-calendar/css/calendar.css");
}
parent::init();
}

public function getEvents($dates = "all")
Expand All @@ -74,44 +73,50 @@ public function getEvents($dates = "all")

public function ShowPast()
{
return isset($_GET['past']) ? true : false;
return isset($_GET['past']);
}

// THIS PAGE'S ENTRIES
public function getFutureCalendarEntries()
{
$entries = GroupedList::create(CalendarEntry::get()->filter(array("CalendarPageID"=>$this-ID))->Sort('Date, Time')->where("Date >= CURRENT_DATE OR Date IS NULL"));

return $entries;
}

public function getGroupedPastCalendarEntries()
{
$entries = GroupedList::create(CalendarEntry::get()->filter(array("CalendarPageID"=>$this-ID))->Sort('Date, Time')->where("Date < CURRENT_DATE"));

return $entries;
}

public function getGroupedCalendarEntries()
{
$entries = GroupedList::create(CalendarEntry::get()->filter(array("CalendarPageID"=>$this-ID))->Sort('Date, Time'));

return $entries;
}

// ALL ENTRIES - FROM ALL PAGES
public function getAllGroupedFutureCalendarEntries()
{
$entries = GroupedList::create(CalendarEntry::get()->Sort('Date, Time')->where("Date >= CURRENT_DATE OR Date IS NULL"));

return $entries;
}

public function getAllGroupedPastCalendarEntries()
{
$entries = GroupedList::create(CalendarEntry::get()->Sort('Date, Time')->where("Date < CURRENT_DATE"));

return $entries;
}

public function getAllGroupedCalendarEntries()
{
$entries = GroupedList::create(CalendarEntry::get()->Sort('Date, Time'));

return $entries;
}
}