Skip to content

Commit

Permalink
Merge pull request #13 from zanderwar/pulls/singleobjectadmin/cleanup
Browse files Browse the repository at this point in the history
PSR-2 Clean up & Fixes
  • Loading branch information
stevie-mayhew authored Dec 17, 2017
2 parents c70388e + 5169132 commit 5a40dd7
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 73 deletions.
File renamed without changes.
3 changes: 0 additions & 3 deletions _config/config.yml

This file was deleted.

12 changes: 11 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "littlegiant/silverstripe-singleobjectadmin",
"type": "silverstripe-module",
"type": "silverstripe-vendormodule",
"description": "Single object administration via a LeftAndMain like interface",
"license": "MIT",
"authors": [
Expand All @@ -18,5 +18,15 @@
"require":
{
"silverstripe/framework": "^4.0"
},
"autoload": {
"psr-4": {
"LittleGiant\\SingleObjectAdmin\\": "src/"
}
},
"extra": {
"expose": [
"dist/"
]
}
}
File renamed without changes
131 changes: 62 additions & 69 deletions code/SingleObjectAdmin.php → src/SingleObjectAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

namespace LittleGiant\SingleObjectAdmin;

use SilverStripe\Control\HTTPRequest;
use SilverStripe\Forms\Form;
use SilverStripe\Admin\AdminRootController;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\ORM\ValidationResult;
use SilverStripe\Security\Permission;
use SilverStripe\Versioned\Versioned;
use SilverStripe\Forms\HiddenField;
Expand All @@ -17,22 +21,20 @@
use SilverStripe\Admin\LeftAndMain;
use SilverStripe\Security\PermissionProvider;


/**
* Defines the Single Object Administration interface for the CMS
*
* @package SingleObjectAdmin
* @author Jeremy Bridson with help from Stevie Mayhew
* @author Jeremy Bridson with help from Stevie Mayhew
*/
class SingleObjectAdmin extends LeftAndMain implements PermissionProvider
{

private static $url_rule = '/$Action/$ID/$OtherID';
private static $menu_icon = 'silverstripe-singleobjectadmin/images/singleobjectadmin.png';
private static $menu_icon = 'resources/littlegiant/silverstripe-singleobjectadmin/dist/images/singleobjectadmin.png';

private static $allowed_actions = array(
private static $allowed_actions = [
'EditForm'
);
];

public function canView($member = null)
{
Expand All @@ -41,21 +43,23 @@ public function canView($member = null)

public function providePermissions()
{

return array(
"CMS_ACCESS_SingleObjectAdmin" => array(
'name' => "Access to Single Object Administration",
return [
"CMS_ACCESS_SingleObjectAdmin" => [
'name' => "Access to Single Object Administration",
'category' => 'CMS Access',
'help' => 'Allow use of Single Object Administration'
)
);
'help' => 'Allow use of Single Object Administration'
]
];
}

/**
* @return mixed
* @return DataObject
*/
public function getCurrentObject() {
public function getCurrentObject()
{
$objectClass = $this->config()->get('tree_class');

/** @var DataObject|Versioned $object */
$object = $objectClass::get()->first();

if ($object && $object->exists()) {
Expand All @@ -68,37 +72,34 @@ public function getCurrentObject() {
$object = $objectClass::create();
$object->write();
if ($objectClass::has_extension(Versioned::class)) {
$object->doPublish();
$object->publishRecursive();
}

Versioned::set_stage($currentStage);

return $object;
}


public function getCMSActions() {

$actions = new FieldList(
FormAction::create(
'doSave',
'Save'
)->addExtraClass('btn-primary font-ic1on-save')
);
/**
* @return FieldList
*/
public function getCMSActions()
{
$actions = new FieldList(FormAction::create('doSave', 'Save')->addExtraClass('btn-primary font-ic1on-save'));

$this->extend('updateCMSActions', $actions);

return $actions;
}

/**
* @param null $id Not used.
* @param null $id Not used.
* @param null $fields Not used.
*
* @return Form
*/
public function getEditForm($id = null, $fields = null)
{

$object = $this->getCurrentObject();

$fields = $object->getCMSFields();
Expand All @@ -117,23 +118,17 @@ public function getEditForm($id = null, $fields = null)
$validator = null;
}

$form = Form::create(
$this,
'EditForm',
$fields,
$actions,
$validator
)->setHTMLID('Form_EditForm');
$form = Form::create($this, 'EditForm', $fields, $actions, $validator)->setHTMLID('Form_EditForm');

$form->setValidationResponseCallback(function (ValidationResult $errors) use ($negotiator, $form) {
$request = $this->getRequest();
if ($request->isAjax() && $negotiator) {
$result = $form->forTemplate();
return $negotiator->respond($request, array(
return $negotiator->respond($request, [
'CurrentForm' => function () use ($result) {
return $result;
}
));
]);
}
});

Expand Down Expand Up @@ -169,15 +164,15 @@ public function EditForm($request = null)
/**
* Used for preview controls, mainly links which switch between different states of the page.
*
* @return ArrayData
* @return DBHTMLText|string
*/
public function getSilverStripeNavigator()
{
return $this->renderWith(SingleObjectAdmin::class . '_SilverStripeNavigator');
}

/**
* @return mixed
* @return PjaxResponseNegotiator
*/
public function getResponseNegotiator()
{
Expand All @@ -190,13 +185,16 @@ public function getResponseNegotiator()
}

/**
* @param $data
* @param $form
* @param array $data
* @param Form $form
*
* @return mixed
*/
public function doSave($data, $form)
{
$objectClass = $this->config()->get('tree_class');

/** @var DataObject|Versioned $object */
$object = $objectClass::get()->byID($data['ID']);

$currentStage = Versioned::get_stage();
Expand All @@ -214,14 +212,14 @@ public function doSave($data, $form)
$result = $e->getResult();
$form->loadMessagesFrom($result);

$responseNegotiator = new PjaxResponseNegotiator(array(
$responseNegotiator = new PjaxResponseNegotiator([
'CurrentForm' => function () use (&$form) {
return $form->forTemplate();
},
'default' => function () use (&$controller) {
'default' => function () use (&$controller) {
return $controller->redirectBack();
}
));
]);
if ($controller->getRequest()->isAjax()) {
$controller->getRequest()->addHeader('X-Pjax', 'CurrentForm');
}
Expand All @@ -236,14 +234,10 @@ public function doSave($data, $form)
}

$link = '"' . $object->singular_name() . '"';
$message = _t(
'GridFieldDetailForm.Saved',
'Saved {name} {link}',
array(
'name' => $object->singular_name(),
'link' => $link
)
);
$message = _t('GridFieldDetailForm.Saved', 'Saved {name} {link}', [
'name' => $object->singular_name(),
'link' => $link
]);

$form->sessionMessage($message, 'good');
$action = $this->edit(Controller::curr()->getRequest());
Expand All @@ -252,31 +246,32 @@ public function doSave($data, $form)
}

/**
* @param $request
* @return mixed
* @param HTTPRequest $request
*
* @return DBHTMLText|string
*/
public function edit($request)
{
$controller = Controller::curr();
$form = $this->EditForm($request);

$return = $this->customise(array(
$return = $this->customise([
'Backlink' => $controller->hasMethod('Backlink') ? $controller->Backlink() : $controller->Link(),
'EditForm' => $form,
))->renderWith(SingleObjectAdmin::class . '_Content');
])->renderWith(SingleObjectAdmin::class . '_Content');

if ($request->isAjax()) {
return $return;
} else {
return $controller->customise(array(
'Content' => $return,
));
}

return $controller->customise([
'Content' => $return,
]);
}

/**
* @param $data
* @param $form
* @param array $data
* @param Form $form
*/
private function publish($data, $form)
{
Expand All @@ -285,10 +280,11 @@ private function publish($data, $form)

$objectClass = $this->config()->get('tree_class');

/** @var DataObject|Versioned $object */
$object = $objectClass::get()->byID($data['ID']);

if ($object) {
$object->doPublish();
$object->publishRecursive();
$form->sessionMessage($object->singular_name() . ' has been saved.', 'good');
} else {
$form->sessionMessage('Something failed, please refresh your browser.', 'bad');
Expand All @@ -302,7 +298,9 @@ private function publish($data, $form)
* Overridden to avoid the BadMethodCallException exception when a url_segment is undefined
*
* @param string $action
*
* @return string
* @throws \BadMethodCallException
*/
public function Link($action = null)
{
Expand All @@ -313,16 +311,11 @@ public function Link($action = null)
// Get url_segment
$segment = $this->config()->get('url_segment');
if (!$segment) {
throw new BadMethodCallException("SingleObjectAdmin subclasses must have url_segment");
throw new \BadMethodCallException("SingleObjectAdmin subclasses must have url_segment");
}
}

$link = Controller::join_links(
AdminRootController::admin_url(),
$segment,
'/', // trailing slash needed if $action is null!
"$action"
);
$link = Controller::join_links(AdminRootController::admin_url(), $segment, '/', "$action");
$this->extend('updateLink', $link);
return $link;
}
Expand Down

0 comments on commit 5a40dd7

Please sign in to comment.