mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
ENHANCEMENT: Modify GridFieldLevelup to be more reusable
This commit is contained in:
parent
c3877075b9
commit
d935a74e31
@ -1,49 +1,84 @@
|
|||||||
<?php
|
<?php
|
||||||
|
/**
|
||||||
|
* Adds a "level up" link to a GridField table, which is useful
|
||||||
|
* when viewing hierarchical data. Requires the managed record
|
||||||
|
* to have a "getParent()" method or has_one relationship called "Parent".
|
||||||
|
*/
|
||||||
|
class GridFieldLevelup extends Object implements GridField_HTMLProvider{
|
||||||
|
|
||||||
class GridFieldLevelup implements GridField_HTMLProvider{
|
|
||||||
/**
|
/**
|
||||||
* @var integer - the record id of the level up to
|
* @var integer - the record id of the level up to
|
||||||
*/
|
*/
|
||||||
protected $levelID = null;
|
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();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param integer $levelID - the record id of the level up to
|
* @param integer $currentID - The ID of the current item; this button will find that item's parent
|
||||||
*/
|
*/
|
||||||
public function __construct($levelID = null) {
|
public function __construct($currentID) {
|
||||||
if($levelID && is_numeric($levelID)) {
|
if($currentID && is_numeric($currentID)) $this->currentID = $currentID;
|
||||||
$this->levelID = $levelID;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getHTMLFragments($gridField) {
|
public function getHTMLFragments($gridField) {
|
||||||
$modelClass = $gridField->getModelClass();
|
$modelClass = $gridField->getModelClass();
|
||||||
if(isset($_GET['ParentID']) && $_GET['ParentID']){
|
$parentID = 0;
|
||||||
|
|
||||||
$modelObj = DataObject::get_by_id($modelClass, $_GET['ParentID']);
|
if($this->currentID) {
|
||||||
|
$modelObj = DataObject::get_by_id($modelClass, $this->currentID);
|
||||||
|
|
||||||
if(is_callable(array($modelObj, 'getParent'))){
|
if($modelObj->hasMethod('getParent')) {
|
||||||
$levelup = $modelObj->getParent();
|
$parent = $modelObj->getParent();
|
||||||
if(!$levelup){
|
} elseif($modelObj->ParentID) {
|
||||||
$parentID = 0;
|
$parent = $modelObj->Parent();
|
||||||
}else{
|
|
||||||
$parentID = $levelup->ID;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
//$controller = $gridField->getForm()->Controller();
|
|
||||||
|
if($parent) $parentID = $parent->ID;
|
||||||
|
|
||||||
|
// Attributes
|
||||||
|
$attrs = array_merge($this->attributes, array(
|
||||||
|
'href' => sprintf($this->linkSpec, $parentID),
|
||||||
|
'class' => 'cms-panel-link list-parent-link'
|
||||||
|
));
|
||||||
|
$attrsStr = '';
|
||||||
|
foreach($attrs as $k => $v) $attrsStr .= " $k=\"" . Convert::raw2att($v) . "\"";
|
||||||
|
|
||||||
$forTemplate = new ArrayData(array(
|
$forTemplate = new ArrayData(array(
|
||||||
'UpLink' => sprintf(
|
'UpLink' => sprintf('<a%s>%s</a>', $attrsStr, _t('GridField.LEVELUP', 'Level up'))
|
||||||
'<a class="cms-panel-link list-parent-link" href="?ParentID=%d&view=list" data-pjax-target="ListViewForm,Breadcrumbs">%s</a>',
|
|
||||||
$parentID,
|
|
||||||
_t('GridField.LEVELUP', 'Level up' )
|
|
||||||
),
|
|
||||||
));
|
));
|
||||||
|
|
||||||
return array(
|
return array(
|
||||||
'before' => $forTemplate->renderWith('GridFieldLevelup'),
|
'before' => $forTemplate->renderWith('GridFieldLevelup'),
|
||||||
//'header' => $forTemplate->renderWith('GridFieldLevelup_Row'),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
?>
|
|
Loading…
x
Reference in New Issue
Block a user