Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SilverStripe 4 Upgrade #7

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 0 additions & 47 deletions code/GridFieldSummaryRow.php

This file was deleted.

10 changes: 7 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "samthejarvis/gridfield-summary-row",
"description": "Add a summary row to gridfields (incl. totals)",
"type": "silverstripe-module",
"type": "silverstripe-vendormodule",
"homepage": "https://github.com/samthejarvis/gridfield-summary-row",
"keywords": [
"silverstripe", "summary", "gridfield", "total", "totals", "samthejarvis"
Expand All @@ -17,7 +17,11 @@
"issues": "https://github.com/samthejarvis/gridfield-summary-row/issues"
},
"require": {
"silverstripe/cms": "~3.1.0",
"silverstripe/framework": "~3.1.0"
"silverstripe/framework": "^4.1"
},
"autoload": {
"psr-4": {
"SamTheJarvis\\GridFieldSummaryRow\\": "src/"
}
}
}
4 changes: 0 additions & 4 deletions css/summary-row.css

This file was deleted.

2 changes: 2 additions & 0 deletions src/.upgrade.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mappings:
GridFieldSummaryRow: SamTheJarvis\GridFieldSummaryRow\GridFieldSummaryRow
99 changes: 99 additions & 0 deletions src/GridFieldSummaryRow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace SamTheJarvis\GridFieldSummaryRow;

use SilverStripe\ORM\ArrayList;
use SilverStripe\View\ArrayData;
use SilverStripe\View\Requirements;
use SilverStripe\ORM\FieldType\DBInt;
use SilverStripe\ORM\FieldType\DBText;
use SilverStripe\ORM\FieldType\DBField;
use SilverStripe\ORM\FieldType\DBFloat;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\ORM\FieldType\DBDecimal;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridField_HTMLProvider;

class GridFieldSummaryRow implements GridField_HTMLProvider
{
use Configurable;

/**
* List of data types that will result in a sum.
*
* *NOTE* This class will also check for instances of these classes
*
* @var array
*/
private static $allowed_classes = [
DBFloat::class,
DBDecimal::class,
DBInt::class,
DBMoney::class
];

public function __construct($fragment = 'footer')
{
$this->fragment = $fragment;
}

/**
* Is the provided field on our allowed list?
*
* @return boolean
*/
protected function isFieldAllowed(DBField $field)
{
foreach ($this->config()->allowed_classes as $class) {
if (get_class($field) == $class || is_subclass_of($field, $class)) {
return true;
}
}

return false;
}


/**
* Render this row
*
* @param GridField $gridField The current gridfield
*
* @return array
*/
public function getHTMLFragments($gridField)
{
$columns = $gridField->getColumns();
$list = $gridField->getList();
$summary_values = ArrayList::create();
$singleton = Injector::inst()->get($list->dataClass, true);
$db = $singleton->config()->db;

foreach ($columns as $column) {
$field = $singleton->dbObject($column);
$summary_value = "";

if (empty($field) || !$this->isFieldAllowed($field)) {
$obj = DBText::create("Summary");
} else {
$obj = clone $field;
if ($db[$column] == "Money") {
$summary_value = $list->sum($column."Amount");
} else {
$summary_value = $list->sum($column);
}
}

$obj->setValue($summary_value);

$summary_values->push(
ArrayData::create(["Value" => $obj])
);
}

$data = ArrayData::create(['SummaryValues' => $summary_values]);

return [$this->fragment => $data->renderWith(__CLASS__)];
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<tr class="summary-row">

<% loop SummaryValues %>
<td>$Value</td>
<td class="text-right"><strong>$Value.Nice</strong></td>
<% end_loop %>
</tr>