Skip to content

Instantly share code, notes, and snippets.

@muskie9
Created January 13, 2020 14:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muskie9/6341d37de63dc199d1e45a40c4e6711c to your computer and use it in GitHub Desktop.
Save muskie9/6341d37de63dc199d1e45a40c4e6711c to your computer and use it in GitHub Desktop.
<?php
namespace MyApp\Extension;
use MyApp\Model\Reservation;
use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Core\Extension;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest;
use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\ORM\ValidationResult;
/**
* Class GridFieldDetailForm_ItemRequestExtension
* @package MyApp\Extension
*/
class GridFieldDetailForm_ItemRequestExtension extends Extension
{
/**
* @var array
*/
private static $allowed_actions = [
'approveReservation',
'denyReservation',
'pendingReservation',
];
/**
* @param FieldList $actions
*/
public function updateFormActions(FieldList $actions)
{
$record = $this->owner->getRecord();
// This extension would run on every GridFieldDetailForm, so ensure you ignore contexts where
// you are managing a DataObject you don't care about
if (!$record instanceof Reservation || !$record->exists()) {
return;
}
$approve = FormAction::create('approveReservation', 'Approve Reservation')
->setUseButtonTag(true)
->addExtraClass('btn btn-primary');
$deny = FormAction::create('denyReservation', 'Deny Reservation')
->setUseButtonTag(true)
->addExtraClass('btn btn-primary');
$pending = FormAction::create('pendingReservation', 'Mark As Pending')
->setUseButtonTag(true)
->addExtraClass('btn btn-primary');
if ($record->Status == 'Pending') {
$actions->fieldByName('MajorActions')->push($approve);
$actions->fieldByName('MajorActions')->push($deny);
} elseif ($record->Status == 'Approved') {
$actions->fieldByName('MajorActions')->push($pending);
$actions->fieldByName('MajorActions')->push($deny);
} else {
$actions->fieldByName('MajorActions')->push($approve);
$actions->fieldByName('MajorActions')->push($pending);
}
}
/**
* @param $data
* @param $form
* @return HTTPResponse
*/
public function approveReservation($data, $form)
{
// Check permission
if (!$this->owner->getRecord()->canEdit()) {
return $this->owner->httpError(403);
}
$record = $this->owner->getRecord();
$record->Status = 'Approved';
$record->write();
$link = '<a href="' . $this->owner->Link('edit') . '">"'
. htmlspecialchars($this->owner->getRecord()->Title, ENT_QUOTES)
. '"</a>';
$message = _t(
'SilverStripe\\Forms\\GridField\\GridFieldDetailForm.Saved',
'Approved {name} {link}',
[
'name' => $this->owner->getRecord()->i18n_singular_name(),
'link' => $link,
]
);
$form->sessionMessage($message, 'good', ValidationResult::CAST_HTML);
// Redirect after save
return $this->fetchToplevelController()->redirect($this->owner->Link());
}
/**
* @param $data
* @param $form
* @return HTTPResponse
*/
public function denyReservation($data, $form)
{
// Check permission
if (!$this->owner->getRecord()->canEdit()) {
return $this->owner->httpError(403);
}
$record = $this->owner->getRecord();
$record->Status = 'Denied';
$record->write();
$link = '<a href="' . $this->owner->Link('edit') . '">"'
. htmlspecialchars($this->owner->getRecord()->Title, ENT_QUOTES)
. '"</a>';
$message = _t(
'SilverStripe\\Forms\\GridField\\GridFieldDetailForm.Saved',
'Denied {name} {link}',
[
'name' => $this->owner->getRecord()->i18n_singular_name(),
'link' => $link,
]
);
$form->sessionMessage($message, 'good', ValidationResult::CAST_HTML);
// Redirect after save
return $this->fetchToplevelController()->redirect($this->owner->Link());
}
/**
* @param $data
* @param $form
* @return HTTPResponse
*/
public function pendingReservation($data, $form)
{
// Check permission
if (!$this->owner->getRecord()->canEdit()) {
return $this->owner->httpError(403);
}
$record = $this->owner->getRecord();
$record->Status = 'Pending';
$record->write();
$link = '<a href="' . $this->owner->Link('edit') . '">"'
. htmlspecialchars($this->owner->getRecord()->Title, ENT_QUOTES)
. '"</a>';
$message = _t(
'SilverStripe\\Forms\\GridField\\GridFieldDetailForm.Saved',
'Marked as Pending {name} {link}',
[
'name' => $this->owner->getRecord()->i18n_singular_name(),
'link' => $link,
]
);
$form->sessionMessage($message, 'good', ValidationResult::CAST_HTML);
// Redirect after save
return $this->fetchToplevelController()->redirect($this->owner->Link());
}
/**
* Traverse the nested RequestHandlers until we reach something that's not GridFieldDetailForm_ItemRequest.
* This allows us to access the Controller responsible for invoking the top-level GridField.
* This should be equivalent to getting the controller off the top of the controller stack via Controller::curr(),
* but allows us to avoid accessing the global state.
*
* GridFieldDetailForm_ItemRequests are RequestHandlers, and as such they are not part of the controller stack.
*
* @return Controller
*/
protected function fetchToplevelController()
{
$c = $this->owner->getController();
while ($c && $c instanceof GridFieldDetailForm_ItemRequest) {
$c = $c->getController();
}
return $c;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment