Skip to content
This repository
Browse code

MINOR Added GridFieldAction_Delete

  • Loading branch information...
commit 24a37daa50129de1ac2ec01817ee92fa9be37791 1 parent d4bde47
Ingo Schommer authored January 09, 2012

Showing 1 changed file with 94 additions and 0 deletions. Show diff stats Hide diff stats

  1. 94  forms/gridfield/GridFieldAction.php
94  forms/gridfield/GridFieldAction.php
... ...
@@ -0,0 +1,94 @@
  1
+<?php
  2
+/**
  3
+ * This class is an GridField Component that add Delete action for Objects in the GridField
  4
+ * 
  5
+ */
  6
+class GridFieldAction_Delete implements GridField_ColumnProvider, GridField_ActionProvider {
  7
+	
  8
+	/**
  9
+	 * Add a column 'Delete'
  10
+	 * 
  11
+	 * @param type $gridField
  12
+	 * @param array $columns 
  13
+	 */
  14
+	public function augmentColumns($gridField, &$columns) {
  15
+		$columns[] = 'DeleteAction';
  16
+	}
  17
+	
  18
+	/**
  19
+	 * Return any special attributes that will be used for FormField::createTag()
  20
+	 *
  21
+	 * @param GridField $gridField
  22
+	 * @param DataObject $record
  23
+	 * @param string $columnName
  24
+	 * @return array
  25
+	 */
  26
+	public function getColumnAttributes($gridField, $record, $columnName) {
  27
+		return array();
  28
+	}
  29
+	
  30
+	/**
  31
+	 * Add the title 
  32
+	 * 
  33
+	 * @param GridField $gridField
  34
+	 * @param string $columnName
  35
+	 * @return array
  36
+	 */
  37
+	public function getColumnMetadata($gridField, $columnName) {
  38
+		if($columnName == 'DeleteAction') {
  39
+			return array('title' => 'Delete');
  40
+		}
  41
+	}
  42
+	
  43
+	/**
  44
+	 * Which columns are handled by this component
  45
+	 * 
  46
+	 * @param type $gridField
  47
+	 * @return type 
  48
+	 */
  49
+	public function getColumnsHandled($gridField) {
  50
+		return array('DeleteAction');
  51
+	}
  52
+	
  53
+	/**
  54
+	 * Which GridField actions are this component handling
  55
+	 *
  56
+	 * @param GridField $gridField
  57
+	 * @return array 
  58
+	 */
  59
+	public function getActions($gridField) {
  60
+		return array('deleterecord');
  61
+	}
  62
+	
  63
+	/**
  64
+	 *
  65
+	 * @param GridField $gridField
  66
+	 * @param DataObject $record
  67
+	 * @param string $columnName
  68
+	 * @return string - the HTML for the column 
  69
+	 */
  70
+	public function getColumnContent($gridField, $record, $columnName) {
  71
+		$field = new GridField_Action($gridField, 'DeleteRecord'.$record->ID, "x", "deleterecord", array('RecordID' => $record->ID));
  72
+		$output = $field->Field();
  73
+		return $output;
  74
+	}
  75
+	
  76
+	/**
  77
+	 * Handle the actions and apply any changes to the GridField
  78
+	 *
  79
+	 * @param GridField $gridField
  80
+	 * @param string $actionName
  81
+	 * @param mixed $arguments
  82
+	 * @param array $data - form data
  83
+	 * @return void
  84
+	 */
  85
+	public function handleAction(GridField $gridField, $actionName, $arguments, $data) {
  86
+		$id = $arguments['RecordID'];
  87
+		$item = $gridField->getList()->byID($id);
  88
+		if(!$item) return;
  89
+
  90
+		if($actionName == 'deleterecord') {
  91
+			$item->delete();
  92
+		}
  93
+	}
  94
+}

0 notes on commit 24a37da

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