Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENHANCEMENT: Modify GridFieldLevelup to be more reusable
- Loading branch information
Showing
1 changed file
with
62 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,84 @@ | ||
<?php | ||
|
||
class GridFieldLevelup implements GridField_HTMLProvider{ | ||
/** | ||
* 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{ | ||
|
||
/** | ||
* @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) { | ||
if($levelID && is_numeric($levelID)) { | ||
$this->levelID = $levelID; | ||
} | ||
public function __construct($currentID) { | ||
if($currentID && is_numeric($currentID)) $this->currentID = $currentID; | ||
} | ||
|
||
public function getHTMLFragments($gridField) { | ||
$modelClass = $gridField->getModelClass(); | ||
if(isset($_GET['ParentID']) && $_GET['ParentID']){ | ||
|
||
$modelObj = DataObject::get_by_id($modelClass, $_GET['ParentID']); | ||
|
||
if(is_callable(array($modelObj, 'getParent'))){ | ||
$levelup = $modelObj->getParent(); | ||
if(!$levelup){ | ||
$parentID = 0; | ||
}else{ | ||
$parentID = $levelup->ID; | ||
} | ||
$parentID = 0; | ||
|
||
if($this->currentID) { | ||
$modelObj = DataObject::get_by_id($modelClass, $this->currentID); | ||
|
||
if($modelObj->hasMethod('getParent')) { | ||
$parent = $modelObj->getParent(); | ||
} elseif($modelObj->ParentID) { | ||
$parent = $modelObj->Parent(); | ||
} | ||
//$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( | ||
'UpLink' => sprintf( | ||
'<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' ) | ||
), | ||
'UpLink' => sprintf('<a%s>%s</a>', $attrsStr, _t('GridField.LEVELUP', 'Level up')) | ||
)); | ||
|
||
return array( | ||
'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; | ||
} | ||
} | ||
?> |