public function _getAnnouncement($render = true) { if (ClassInfo::hasTable('MediaHolder')) { $media = MediaHolder::get()->filter(array('MediaType.Title' => 'News')); $possible = $media->first(); // what about an announcement title'd page $page = $media->filter(array('Title' => 'Announcements'))->first(); if (!$page) { $page = $possible; } if ($page) { $announcement = MediaPage::get()->filter('ParentID', $page->ID)->sort('Date DESC')->first(); if ($announcement) { if (!$render) { return $announcement; } else { return ModelAsController::controller_for($announcement)->index(); } } } } }
/** * Assign the current attribute to each media page of the respective type. */ public function onBeforeWrite() { parent::onBeforeWrite(); // Set the original title of the current attribute for use in templates. if (is_null($this->OriginalTitle)) { $this->OriginalTitle = $this->Title; } // Retrieve the respective media type for updating all attribute references. $parameters = Controller::curr()->getRequest()->requestVars(); $matches = array(); $result = preg_match('#TypesAttributes/item/[0-9]*/#', $parameters['url'], $matches); if ($result) { $ID = preg_replace('#[^0-9]#', '', $matches[0]); $pages = MediaPage::get()->innerJoin('MediaType', 'MediaPage.MediaTypeID = MediaType.ID')->where('MediaType.ID = ' . (int) $ID); // Apply this new attribute to existing media pages of the respective type. if ($pages && (is_null($this->MediaPageID) || $this->MediaPageID === 0)) { foreach ($pages as $key => $page) { if ($key === 0) { // Apply the current attribute to the first media page. self::$write_flag = true; $this->LinkID = -1; $this->MediaPageID = $page->ID; $page->MediaAttributes()->add($this); } else { // Create a new attribute for remaining media pages. $new = MediaAttribute::create(); $new->OriginalTitle = $this->OriginalTitle; $new->Title = $this->Title; $new->LinkID = $this->ID; $new->MediaPageID = $page->ID; $page->MediaAttributes()->add($new); $new->write(); } } } else { if ($pages) { // Confirm that a write occurrence doesn't already exist. if (!self::$write_flag) { foreach ($pages as $page) { foreach ($page->MediaAttributes() as $attribute) { // Confirm that each attribute is linked to the original attribute. if ($attribute->LinkID == $this->ID && $attribute->Title !== $this->Title) { // Apply the changes from this attribute. self::$write_flag = true; $attribute->Title = $this->Title; $attribute->write(); } } } self::$write_flag = false; } } } } }
/** * Retrieve a simple date filter form. * * @return form */ public function getDateFilterForm() { // Display a form that allows filtering from a specified date. $children = MediaPage::get()->where('ParentID = ' . (int) $this->data()->ID); $form = Form::create($this, 'getDateFilterForm', FieldList::create($date = DateField::create('from', '')->setConfig('showcalendar', true)->setConfig('min', $children->min('Date'))->setConfig('max', $children->max('Date'))->setAttribute('placeholder', 'From'), HiddenField::create('category'), HiddenField::create('tag')), FieldList::create(FormAction::create('dateFilter', 'Filter'), FormAction::create('clearFilters', 'Clear'))); $form->setFormMethod('get'); // Display existing request filters. $request = $this->getRequest(); $form->loadDataFrom($request->getVars()); // Validate the date request filter, as this isn't captured on page request. if ($from = $request->getVar('from')) { foreach (explode('-', $from) as $segment) { if (!is_numeric($segment)) { $date->setValue(null); break; } } } // Remove validation if clear has been triggered. if ($request->getVar('action_clearFilters')) { $form->unsetValidator(); } // Allow extension customisation. $this->extend('updateFilterForm', $form); return $form; }
/** * Display the respective CMS media type attributes. */ public function getCMSFields() { $fields = parent::getCMSFields(); if ($this->Title) { // Display the title as read only. $fields->replaceField('Title', ReadonlyField::create('Title')); $fields->addFieldToTab('Root.Main', LiteralField::create('MediaAttributesTitle', "<div class='field'><label class='left'>Custom Attributes</label></div>")); // Allow customisation of media type attributes if a respective media page exists, depending on the current CMS user permissions. if (MediaPage::get()->innerJoin('MediaType', 'MediaPage.MediaTypeID = MediaType.ID')->where(array('MediaType.Title = ?' => $this->Title))->exists()) { $configuration = $this->checkPermissions() === false ? GridFieldConfig_RecordViewer::create() : GridFieldConfig_RecordEditor::create()->removeComponentsByType('GridFieldDeleteAction'); $fields->addFieldToTab('Root.Main', GridField::create('MediaAttributes', 'Custom Attributes', MediaAttribute::get()->innerJoin('MediaPage', 'MediaAttribute.MediaPageID = MediaPage.ID')->innerJoin('MediaType', 'MediaPage.MediaTypeID = MediaType.ID')->where(array('MediaType.Title = ?' => $this->Title, 'MediaAttribute.LinkID = ?' => -1)), $configuration)->setModelClass('MediaAttribute')); } else { // Display a notice that respective media pages should first be created. Requirements::css(MEDIAWESOME_PATH . '/css/mediawesome.css'); $fields->addFieldToTab('Root.Main', LiteralField::create('MediaNotice', "<p class='mediawesome notice'><strong>No {$this->Title} Pages Found</strong></p>")); } } // Allow extension customisation. $this->extend('updateMediaTypeCMSFields', $fields); return $fields; }