Source of file SimpleCalendarPage.php
Size: 4,725 Bytes - Last Modified: 2021-12-23T10:11:51+00:00
/var/www/docs.ssmods.com/process/src/src/Pages/SimpleCalendarPage.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 | <?php namespace Restruct\SilverStripe\SimpleCalendar { use Page; use SilverStripe\Forms\DropdownField; use SilverStripe\Forms\GridField\GridField; use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor; use SilverStripe\ORM\ArrayList; use UncleCheese\DisplayLogic\Forms\Wrapper; use Unisolutions\GridField\CopyButton; class SimpleCalendarPage extends Page { private static $table_name = 'SimpleCalendarPage'; private static $singular_name = 'Calendar'; private static $plural_name = 'Calendars'; private static $description = 'Create a calendar page'; private static $icon = 'restruct/silverstripe-simple-calendar:client/images/calendar.gif'; private static $db = [ 'ShowEventsIn' => 'Enum("Future, Past, Both","Future")', ]; private static $has_one = [ 'ShowEventsFromCalendar' => SimpleCalendarPage::class, ]; private static $has_many = [ 'Events' => Event::class, 'ExternalCalendars' => ICSFeed::class, ]; public function getCMSFields() { $fields = parent::getCMSFields(); $fields->dataFieldByName('Content')->setRows(20)->addExtraClass('margin-left'); $fields->addFieldsToTab('Root.Calendar', [ DropdownField::create('ShowEventsFromCalendarID', 'Show calendar items from another calendar', SimpleCalendarPage::get()->exclude('ID', $this->ID)->map('ID', 'BreadCrumbPath')->toArray() )->setHasEmptyDefault(true), DropdownField::create('ShowEventsIn', _t('SIMPLECALENDAR.ShowEventsInFutureOrPast', 'Show events in'), [ 'Future' => _t('SIMPLECALENDAR.TheFuture', 'The Future'), 'Past' => _t('SIMPLECALENDAR.ThePast', 'The Past'), 'Both' => _t('SIMPLECALENDAR.Both', 'Both'), ]), ]); $fields->addFieldToTab('Root.Calendar', Wrapper::create( new GridField( 'Events', 'Manage events', $this->Events(), GridFieldConfig_RecordEditor::create() ->addComponent(new CopyButton(), 'GridFieldEditButton') // ->addComponent(new SC_GFToggleShowPastButton('buttons-before-right')) ->addComponent(new GridFieldToggleShowPastButton('buttons-before-left')) ) )->displayUnless('ShowEventsFromCalendarID')->isGreaterThan(0)->end() ); $fields->addFieldToTab('Root.CalendarFeeds', Wrapper::create( new GridField( 'ExternalCalendars', 'External Calendar feeds (.ics)', $this->ExternalCalendars(), $gconf = GridFieldConfig_RecordEditor::create() ) ) ); return $fields; } // limit to future events. public function updateGetItems($items) { if ( $this->ShowEventsFromCalendarID ) { $items = $this->ShowEventsFromCalendar()->Events(); } // from here on, $items becomes an ArrayList instead of a DataList $items = new ArrayList($items->toArray()); foreach ( $this->ExternalCalendars() as $icsfeed ) { // merge-in the (virtual) events from .ics feeds $items->merge($icsfeed->GetFeedEvents()); } // Because of switching to ArrayList, we need to filterByCallback in order to // be able to use the SearchFilter modifiers (they only work on DataList() switch ( $this->ShowEventsIn ) { case 'Past': // return $items->filter('Date:LessThanOrEqual',date("Y-m-d"))->sort('Date DESC'); return $items->filterByCallback(function ($item, $list) { return strtotime($item->Date) <= strtotime(date("Y-m-d")); })->sort('Date DESC'); case 'Both': return $items->sort('Date ASC'); default: // return $items->filter('Date:GreaterThanOrEqual',date("Y-m-d"))->sort('Date ASC'); return $items->filterByCallback(function ($item, $list) { return strtotime($item->Date) >= strtotime(date("Y-m-d")); })->sort('Date ASC'); } } } } |