2011-02-07 07:48:44 +01:00
|
|
|
# TableListField
|
|
|
|
|
|
|
|
## Introduction
|
|
|
|
|
|
|
|
Form field that embeds a list of `[api:DataObject]`s into a form, such as a member list or a file list.
|
|
|
|
Provides customizeable columns, record-deletion by ajax, paging, sorting, CSV-export, printing, input by
|
|
|
|
`[api:DataObject]` or raw SQL.
|
|
|
|
|
|
|
|
## Example
|
|
|
|
|
2011-03-08 22:05:51 +01:00
|
|
|
Here's an example of a full featured `[api:TableListField]` implementation. It features editing members in the database
|
|
|
|
directly as a button on each record, as well as filtering, and sorting. It also makes use of the 'export' permission,
|
|
|
|
allowing export of data as a CSV.
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
:::php
|
2012-01-30 23:13:42 +01:00
|
|
|
public function getReportField() {
|
2011-02-07 07:48:44 +01:00
|
|
|
$resultSet = new DataObjectSet();
|
|
|
|
$filter = `;
|
|
|
|
$sort = "Member.ID ASC";
|
|
|
|
$join = `;
|
|
|
|
$instance = singleton('Member');
|
|
|
|
$query = $instance->buildSQL($filter, $sort, null, $join);
|
|
|
|
$query->groupby[] = 'Member.ID';
|
|
|
|
|
|
|
|
$report = new TableListField(
|
|
|
|
'CorporateReport',
|
|
|
|
'Member',
|
|
|
|
array(
|
|
|
|
'ID' => 'ID',
|
|
|
|
'FirstName' => 'First Name',
|
|
|
|
'Surname' => 'Surname',
|
|
|
|
'Email' => 'Email',
|
|
|
|
'MembershipType' => 'Membership Type',
|
|
|
|
'MembershipStatus' => 'Membership Status',
|
|
|
|
'DateJoined' => 'Date Joined',
|
|
|
|
'PaidUntil' => 'Paid Until',
|
|
|
|
'Edit' => ''
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$report->setCustomQuery($query);
|
|
|
|
|
|
|
|
$report->setFieldFormatting(array(
|
|
|
|
'Email' => '<a href=\"mailto: $Email\" title=\"Email $FirstName\">$Email</a>',
|
|
|
|
'Edit' => '<a href=\"admin/security/index/1?executeForm=EditForm&ID=1&ajax=1&action_callfieldmethod&fieldName=Members&ctf[childID]=$ID&ctf[ID]=1&ctf[start]=0&methodName=edit\"><img src=\"cms/images/edit.gif\" alt=\"Edit this member\" /></a>'
|
|
|
|
));
|
|
|
|
|
|
|
|
$report->setFieldCasting(array(
|
|
|
|
'DateJoined' => 'Date->Nice',
|
|
|
|
'PaidUntil' => 'Date->Nice'
|
|
|
|
));
|
|
|
|
|
|
|
|
$report->setShowPagination(true);
|
|
|
|
if(isset($_REQUEST['printable'])) {
|
|
|
|
$report->setPageSize(false);
|
|
|
|
} else {
|
|
|
|
$report->setPageSize(20);
|
|
|
|
}
|
|
|
|
|
|
|
|
$report->setPermissions(array(
|
|
|
|
'export',
|
|
|
|
'delete',
|
|
|
|
'print'
|
|
|
|
));
|
|
|
|
|
|
|
|
return $report;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
For more information on each of the features used in the example, you can read below.
|
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
### Source Input
|
|
|
|
|
|
|
|
:::php
|
|
|
|
// default: DataObject selection (e.g. all 'Product's)
|
|
|
|
$myTableListField = new TableListField(
|
|
|
|
'MyName',
|
|
|
|
'Product',
|
|
|
|
array('Price', 'Code')
|
|
|
|
);
|
|
|
|
|
|
|
|
// custom DataObjectSet
|
|
|
|
$myProducts = DataObject::get('Product','Code = "MyCode"');
|
|
|
|
$myTableListField->setCustomSourceItems($myProducts);
|
|
|
|
|
|
|
|
// custom SQL
|
|
|
|
$customCsvQuery = singleton('Product')->buildSQL();
|
|
|
|
$customCsvQuery->select[] = "CONCAT(col1,col2) AS MyCustomSQLColumn";
|
|
|
|
$myTableListField->setCustomCsvQuery($customQuery);
|
|
|
|
|
2011-03-08 22:05:51 +01:00
|
|
|
`[api:TableListField]` also tries to resolve Component-relations(has_one, has_many) and custom getters automatically:
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
:::php
|
|
|
|
$myTableListField = new TableListField(
|
|
|
|
'MyName',
|
|
|
|
'Product',
|
|
|
|
array(
|
|
|
|
'Buyer.LastName',
|
|
|
|
'PriceWithShipping'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
// Product.php Example
|
|
|
|
class Product extends DataObject {
|
|
|
|
$has_one = array('Buyer'=>'Member');
|
2012-01-30 23:13:42 +01:00
|
|
|
public function getPriceWithShipping() {
|
2011-02-07 07:48:44 +01:00
|
|
|
return $this->Price + $this->Shipping;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
### Pagination
|
|
|
|
|
|
|
|
Paging works by AJAX, but also works without javascript on link-basis.
|
|
|
|
|
|
|
|
:::php
|
|
|
|
$myTableListField->setPageSize(100); // defaults to 20
|
|
|
|
|
|
|
|
|
|
|
|
### Sorting
|
|
|
|
|
|
|
|
The easiest method is to add the sorting criteria as a constructor parameter. Sorting should be applied manually, where
|
|
|
|
appropriate. Only direct columns from the produced SQL-query are supported.
|
|
|
|
|
|
|
|
Example (sorting by "FirstName" column):
|
|
|
|
|
|
|
|
:::php
|
|
|
|
$report = new TableListField(
|
|
|
|
'CorporateReport', // name
|
|
|
|
'Member', // sourceClass
|
|
|
|
array(
|
|
|
|
'ID' => 'ID',
|
|
|
|
'FirstName' => 'First Name',
|
|
|
|
'LastName' => 'Last Name',
|
|
|
|
), // fieldList
|
|
|
|
null, // sourceFilter
|
|
|
|
'FirstName' // sourceSort
|
|
|
|
);
|
|
|
|
|
|
|
|
|
2011-03-08 22:05:51 +01:00
|
|
|
If you want to sort by custom getters in your `[api:DataObject]`, please reformulate them to a custom SQL column. This
|
2011-02-07 07:48:44 +01:00
|
|
|
restriction is needed to avoid performance-hits by caching and sorting potentially large datasets on PHP-level.
|
|
|
|
|
|
|
|
### Casting
|
|
|
|
|
2011-03-08 22:05:51 +01:00
|
|
|
Column-values can be casted, based on the casting-types available through DBObject (sapphire/core/model/fieldtypes).
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
:::php
|
|
|
|
$myTableListField->setFieldCasting(array(
|
|
|
|
"MyCustomDate"=>"Date",
|
|
|
|
"MyShortText"=>"Text->FirstSentence"
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
|
|
### Permissions
|
|
|
|
|
2011-03-08 22:05:51 +01:00
|
|
|
Permissions vary in different `[api:TableListField]`-implementations, and are evaluated in the template.
|
2011-02-07 07:48:44 +01:00
|
|
|
By default, all listed permissions are enabled.
|
|
|
|
|
|
|
|
:::php
|
|
|
|
$myTableListField->setPermissions(array(
|
|
|
|
'delete',
|
|
|
|
'export',
|
|
|
|
'print'
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
|
|
### Formatting
|
|
|
|
|
|
|
|
Specify custom formatting for fields, e.g. to render a link instead of pure text.
|
|
|
|
Caution: Make sure to escape special php-characters like in a normal php-statement.
|
|
|
|
|
|
|
|
:::php
|
|
|
|
$myTableListField->setFieldFormatting(array(
|
|
|
|
"myFieldName" => '<a href=\"custom-admin/$ID\">$ID</a>'
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
|
|
### Highlighting
|
|
|
|
|
|
|
|
"Highlighting" is similiar to "Formatting", but applies to the whole row rather than a column.
|
|
|
|
Definitions for highlighting table-rows with a specific CSS-class. You can use all column-names
|
|
|
|
in the result of a query. Use in combination with {@setCustomQuery} to select custom properties and joined objects.
|
|
|
|
|
|
|
|
:::php
|
|
|
|
$myTableListField->setHighlightConditions(array(
|
|
|
|
array(
|
|
|
|
"rule" => '$Flag == "red"',
|
|
|
|
"class" => "red"
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
"rule" => '$Flag == "orange"',
|
|
|
|
"class" => "orange"
|
|
|
|
)
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
|
|
### Export
|
|
|
|
|
|
|
|
Export works only to CSV currently, with following specs:
|
|
|
|
|
|
|
|
* Line delimiter: "\n"
|
|
|
|
* Separator: ";"
|
|
|
|
* Column-quotes: none
|
|
|
|
|
|
|
|
:::php
|
|
|
|
$myTableListField->setPermissions(array('export'));
|
|
|
|
$myTableListField->setFieldListCsv(array(
|
|
|
|
'Price' => 'Price',
|
|
|
|
'ItemCount' => 'Item Count',
|
|
|
|
'ModelNumber' => 'Model Number'
|
|
|
|
));
|
|
|
|
|
|
|
|
You can influence the exported values by adjusting the generated SQL.
|
|
|
|
|
|
|
|
:::php
|
|
|
|
$customCsvQuery = singleton('Product')->buildSQL();
|
|
|
|
$customCsvQuery->select[] = "CONCAT(col1,col2) AS MyCustomSQLColumn";
|
|
|
|
$myTableListField->setCustomCsvQuery($customQuery);
|
|
|
|
$myTableListField->setFieldListCsv(array(
|
|
|
|
'MyCustomSQLColumn'
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
|
|
### Row-Summaries
|
|
|
|
|
|
|
|
You can summarize specific columns in your result-set. The term "summary" is used in a broad sense, you can also
|
|
|
|
implement averages etc.
|
|
|
|
|
|
|
|
:::php
|
|
|
|
$myTableListField->addSummary(
|
|
|
|
'Total Revenue and Sales Count',
|
|
|
|
array(
|
|
|
|
"Price" => array("sum","Currency->Nice"),
|
|
|
|
"ItemCount" => "sum"
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2011-03-08 22:05:51 +01:00
|
|
|
In `[api:TableListField]`-implementation, these summaries also react to changes in input-fields by javascript.
|
2011-02-07 07:48:44 +01:00
|
|
|
Available methods:
|
|
|
|
|
|
|
|
* sum
|
|
|
|
* avg
|
|
|
|
|
|
|
|
### Grouping
|
|
|
|
|
2011-03-08 22:05:51 +01:00
|
|
|
Used to group by a specific column in the `[api:DataObject]` and create partial summaries.
|
2011-02-07 07:48:44 +01:00
|
|
|
Please use only together with addSummary().
|
|
|
|
(Automatically disables sorting).
|
|
|
|
|
|
|
|
:::php
|
|
|
|
$myTableListField->groupByField = 'MyColumnName';
|
|
|
|
|
|
|
|
|
|
|
|
## Best Practices
|
|
|
|
|
|
|
|
### Custom Sorting
|
|
|
|
|
2011-03-08 22:05:51 +01:00
|
|
|
Please subclass `[api:TableListField]` to implement custom sorting, following the naming-convention
|
2011-02-07 07:48:44 +01:00
|
|
|
"`colFunction_<yourFunctionName>`".
|
|
|
|
|
|
|
|
:::php
|
|
|
|
class CustomTableListField extends TableListField {
|
|
|
|
// referenced through "dateAverage"
|
2012-01-30 23:13:42 +01:00
|
|
|
public function colFunction_dateAverage($values) {
|
2011-02-07 07:48:44 +01:00
|
|
|
// custom date summaries
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
### Adding Utility-functions
|
|
|
|
|
|
|
|
In case you want to perform utility-functions like "export" or "print" through action-buttons,
|
|
|
|
make sure to subclass Utility() which collates all possible actions.
|
|
|
|
|
2011-03-08 22:05:51 +01:00
|
|
|
### Customizing Look & Feel
|
2011-02-07 07:48:44 +01:00
|
|
|
|
|
|
|
You can exchange the used template, e.g. to change applied CSS-classes or the HTML-markup:
|
|
|
|
|
|
|
|
:::php
|
|
|
|
$myTableListField->setTemplate("MyPrettyTableListField");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## API Documentation
|
|
|
|
|
2011-03-08 22:05:51 +01:00
|
|
|
`[api:TableListField]`
|