Skip to content
This repository
Browse code

ENHANCEMENT: Modify GridFieldLevelup to be more reusable

  • Loading branch information...
commit d935a74e31b1ee3b7172ba934ceb55a57336b980 1 parent c387707
Naomi Guyer authored June 26, 2012 chillu committed June 26, 2012

Showing 1 changed file with 62 additions and 27 deletions. Show diff stats Hide diff stats

  1. 89  forms/gridfield/GridFieldLevelup.php
89  forms/gridfield/GridFieldLevelup.php
... ...
@@ -1,49 +1,84 @@
1 1
 <?php
2  
-
3  
-class GridFieldLevelup implements GridField_HTMLProvider{
  2
+/**
  3
+ * Adds a "level up" link to a GridField table, which is useful
  4
+ * when viewing hierarchical data. Requires the managed record 
  5
+ * to have a "getParent()" method or has_one relationship called "Parent".
  6
+ */
  7
+class GridFieldLevelup extends Object implements GridField_HTMLProvider{
  8
+	
4 9
 	/**
5 10
 	 * @var integer - the record id of the level up to
6 11
 	 */
7  
-	protected $levelID = null;
  12
+	protected $currentID = null;
  13
+
  14
+	/**
  15
+	 * sprintf() spec for link to link to parent.
  16
+	 * Only supports one variable replacement - the parent ID.
  17
+	 * @var string
  18
+	 */
  19
+	protected $linkSpec = '';
  20
+
  21
+	/**
  22
+	 * @var array Extra attributes for the link
  23
+	 */
  24
+	protected $attributes = array();
8 25
 
9 26
 	/**
10 27
 	 *
11  
-	 * @param integer $levelID - the record id of the level up to
  28
+	 * @param integer $currentID - The ID of the current item; this button will find that item's parent
12 29
 	 */
13  
-	public function __construct($levelID = null) {
14  
-		if($levelID && is_numeric($levelID)) {
15  
-			$this->levelID = $levelID;
16  
-		}
  30
+	public function __construct($currentID) {
  31
+		if($currentID && is_numeric($currentID)) $this->currentID = $currentID;
17 32
 	}
18 33
 	
19 34
 	public function getHTMLFragments($gridField) {
20 35
 		$modelClass = $gridField->getModelClass();
21  
-		if(isset($_GET['ParentID']) && $_GET['ParentID']){
22  
-			
23  
-			$modelObj = DataObject::get_by_id($modelClass, $_GET['ParentID']);
24  
-			
25  
-			if(is_callable(array($modelObj, 'getParent'))){
26  
-				$levelup = $modelObj->getParent();
27  
-				if(!$levelup){
28  
-					$parentID = 0;
29  
-				}else{
30  
-					$parentID = $levelup->ID;
31  
-				}
  36
+		$parentID = 0;
  37
+
  38
+		if($this->currentID) {
  39
+			$modelObj = DataObject::get_by_id($modelClass, $this->currentID);
  40
+
  41
+			if($modelObj->hasMethod('getParent')) {
  42
+				$parent = $modelObj->getParent();
  43
+			} elseif($modelObj->ParentID) {
  44
+				$parent = $modelObj->Parent();
32 45
 			}
33  
-			//$controller = $gridField->getForm()->Controller();
  46
+
  47
+			if($parent) $parentID = $parent->ID;
  48
+			
  49
+			// Attributes
  50
+			$attrs = array_merge($this->attributes, array(
  51
+				'href' => sprintf($this->linkSpec, $parentID),
  52
+				'class' => 'cms-panel-link list-parent-link'
  53
+			));
  54
+			$attrsStr = '';
  55
+			foreach($attrs as $k => $v) $attrsStr .= " $k=\"" . Convert::raw2att($v) . "\"";
  56
+
34 57
 			$forTemplate = new ArrayData(array(
35  
-				'UpLink' => sprintf(
36  
-					'<a class="cms-panel-link list-parent-link" href="?ParentID=%d&view=list" data-pjax-target="ListViewForm,Breadcrumbs">%s</a>',
37  
-					$parentID,
38  
-					_t('GridField.LEVELUP', 'Level up' )
39  
-				),
  58
+				'UpLink' => sprintf('<a%s>%s</a>', $attrsStr, _t('GridField.LEVELUP', 'Level up'))
40 59
 			));
41 60
 
42 61
 			return array(
43 62
 				'before' => $forTemplate->renderWith('GridFieldLevelup'),
44  
-				//'header' => $forTemplate->renderWith('GridFieldLevelup_Row'),
45 63
 			);
46 64
 		}
47 65
 	}
  66
+
  67
+	public function setAttributes($attrs) {
  68
+		$this->attributes = $attrs;
  69
+		return $this;
  70
+	}
  71
+
  72
+	public function getAttributes() {
  73
+		return $this->attributes;
  74
+	}
  75
+
  76
+	public function setLinkSpec($link) {
  77
+		$this->linkSpec = $link;
  78
+		return $this;
  79
+	}
  80
+
  81
+	public function getLinkSpec() {
  82
+		return $this->linkSpec;
  83
+	}
48 84
 } 
49  
-?>

0 notes on commit d935a74

Please sign in to comment.
Something went wrong with that request. Please try again.