12/02/2017 5:59pm

PHP | Problem -


<?php


class NewsHolder extends DataObjectAsPageHolder {

private static $has_many = array(
'News' => 'News'
);
private static $description = 'News Holder';

public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->removeByName('Content');



$gridFieldConfig = GridFieldConfig_RecordEditor::create();
$gridFieldConfig->getComponentByType('GridFieldDetailForm');
$gridFieldConfig->removeComponentsByType('GridFieldDeleteAction');
$gridFieldConfig->addComponent(new GridFieldDeleteAction());

$fields->addFieldsToTab(
'Root.News', new GridField(
'News', 'News', $this->News(), $gridFieldConfig
)
);
return $fields;
}

public function onBeforeDelete() {
parent::onBeforeDelete();
if ($this->News()) {
foreach ($this->News() as $item) {
$item->delete();
}
}
}

}

class NewsHolder_Controller extends DataObjectAsPageHolder_Controller {

private static $item_class = 'News';

private static $allowed_actions = array(
'show',
'showAMP'
);

private static $url_handlers = array(
// 'amp.html' => 'amp',
'$ItemSegment!/amp.html!' => 'showAMP',
'$ItemSegment!' => 'show'
);


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

// public function amp() {
// $class = Controller::curr()->ClassName;

// $page = $this->owner->renderWith(array("$class"."_amp", "Amp"));

// return $page;
// }

public function show() {
if ($item = $this->getCurrentItem()) {
$do = new DataObject();
$do->Link = $item->Link();
$do->MenuTitle = $item->Title;
$do->isSelf = true;



if ($item->canView()) {

$data = array(
'Item' => $item,
'BackLink' => base64_decode($this->request->getVar('backlink'))
);


return $this->customise(new ArrayData($data))->renderWith(array($this->stat('item_class'), 'Page'));
} else {
return Security::permissionFailure($this);
}
} else {
return $this->httpError(404);
}
}

public function showAMP() {


if ($item = $this->getCurrentItem()) {

$do = new DataObject();
$do->Link = $item->Link();
$do->MenuTitle = $item->Title;
$do->isSelf = true;



if ($item->canView()) {

$data = array(
'Item' => $item,
'BackLink' => base64_decode($this->request->getVar('backlink'))
);

return $this->customise(new ArrayData($data))->renderWith('News_amp', 'Amp');
} else {
return Security::permissionFailure($this);
}
} else {
return $this->httpError(404);
}
}

public function PaginatedList() {
$list = News::get()->filter(array('isActive' => 1))->sort('Date','DESC');

if($list) {
$news_to_show = new PaginatedList($list, $this->getRequest());
$news_to_show->setPageLength(12);
return $news_to_show;
}

return false;
}
/*
* Get the current DataObject Item from the URL if one exists
*/
public function getCurrentItem($itemID = null)
{
$params = $this->request->allParams();
$class = $this->stat('item_class');
if($itemID)
{
$item = $class::get()->byID($itemID);

}
elseif(isset($params['ItemSegment']))
{
//Sanitize
$URL = Convert::raw2sql($params['ItemSegment']);

$item = $class::get()->filter("URLSegment", $URL)->first();
}
$this->extend('updateCurrentItem', $item);

return $item;
}

public function isAmp() {
$uri = Controller::curr()->getRequest()->getVar('url');
return preg_match('/amp.html/', $uri);
}

}