2012-05-07 03:40:41 +02:00
|
|
|
<?php
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\Forms\GridField;
|
|
|
|
|
|
|
|
use SilverStripe\Core\Object;
|
|
|
|
use SilverStripe\Forms\FormField;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-06-17 08:49:23 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBField;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\View\ArrayData;
|
|
|
|
use SilverStripe\View\SSViewer;
|
2016-06-17 08:49:23 +02:00
|
|
|
|
2012-06-26 05:59:06 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Adds a "level up" link to a GridField table, which is useful when viewing
|
|
|
|
* hierarchical data. Requires the managed record to have a "getParent()"
|
2013-05-20 12:18:07 +02:00
|
|
|
* method or has_one relationship called "Parent".
|
2012-06-26 05:59:06 +02:00
|
|
|
*/
|
2013-05-20 12:18:07 +02:00
|
|
|
class GridFieldLevelup extends Object implements GridField_HTMLProvider {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-07 03:40:41 +02:00
|
|
|
/**
|
|
|
|
* @var integer - the record id of the level up to
|
|
|
|
*/
|
2012-06-26 05:59:06 +02:00
|
|
|
protected $currentID = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sprintf() spec for link to link to parent.
|
|
|
|
* Only supports one variable replacement - the parent ID.
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $linkSpec = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array Extra attributes for the link
|
|
|
|
*/
|
|
|
|
protected $attributes = array();
|
2012-05-07 03:40:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2012-06-26 05:59:06 +02:00
|
|
|
* @param integer $currentID - The ID of the current item; this button will find that item's parent
|
2012-05-07 03:40:41 +02:00
|
|
|
*/
|
2012-06-26 05:59:06 +02:00
|
|
|
public function __construct($currentID) {
|
2016-08-23 04:32:26 +02:00
|
|
|
parent::__construct();
|
|
|
|
if($currentID && is_numeric($currentID)) {
|
|
|
|
$this->currentID = $currentID;
|
|
|
|
}
|
2012-05-07 03:40:41 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-07 03:40:41 +02:00
|
|
|
public function getHTMLFragments($gridField) {
|
|
|
|
$modelClass = $gridField->getModelClass();
|
2012-06-26 05:59:06 +02:00
|
|
|
$parentID = 0;
|
|
|
|
|
2016-08-23 04:32:26 +02:00
|
|
|
if(!$this->currentID) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$modelObj = DataObject::get_by_id($modelClass, $this->currentID);
|
2012-06-26 05:59:06 +02:00
|
|
|
|
2016-08-23 04:32:26 +02:00
|
|
|
$parent = null;
|
|
|
|
if($modelObj->hasMethod('getParent')) {
|
|
|
|
$parent = $modelObj->getParent();
|
|
|
|
} elseif($modelObj->ParentID) {
|
|
|
|
$parent = $modelObj->Parent();
|
|
|
|
}
|
2012-06-26 05:59:06 +02:00
|
|
|
|
2016-08-23 04:32:26 +02:00
|
|
|
if ($parent) {
|
|
|
|
$parentID = $parent->ID;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-08-23 04:32:26 +02:00
|
|
|
// Attributes
|
|
|
|
$attrs = array_merge($this->attributes, array(
|
|
|
|
'href' => sprintf($this->linkSpec, $parentID),
|
|
|
|
'class' => 'cms-panel-link ss-ui-button font-icon-level-up no-text grid-levelup'
|
|
|
|
));
|
|
|
|
$linkTag = FormField::create_tag('a', $attrs);
|
2012-06-26 05:59:06 +02:00
|
|
|
|
2016-08-23 04:32:26 +02:00
|
|
|
$forTemplate = new ArrayData(array(
|
|
|
|
'UpLink' => DBField::create_field('HTMLFragment', $linkTag)
|
|
|
|
));
|
2012-05-07 03:40:41 +02:00
|
|
|
|
2016-08-23 04:32:26 +02:00
|
|
|
$template = SSViewer::get_templates_by_class($this, '', __CLASS__);
|
2012-05-07 03:40:41 +02:00
|
|
|
return array(
|
2016-08-23 04:32:26 +02:00
|
|
|
'before' => $forTemplate->renderWith($template),
|
|
|
|
);
|
2012-05-07 03:40:41 +02:00
|
|
|
}
|
2012-06-26 05:59:06 +02:00
|
|
|
|
|
|
|
public function setAttributes($attrs) {
|
|
|
|
$this->attributes = $attrs;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAttributes() {
|
|
|
|
return $this->attributes;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setLinkSpec($link) {
|
|
|
|
$this->linkSpec = $link;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getLinkSpec() {
|
|
|
|
return $this->linkSpec;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|