How do you Filter CSV exports on silverstripe CMS?

Viewed 341

I am trying to export a CSV of my data which is currently displayed in a section of my Silverstripe CMS as filtered by a particular date range . It works fine at the moment when exporting the entire contents but I would like to be able to filter the results that are exported so that it returns all results within a particular date range.

My Database has a column thats records the date created - in the format 'D-M-Y; H-M-S' which I think could be used to do the filtering but I cant figure out how to set up the search filter. I understand that if you use the searchable fields and then export, you only export the filtered search results so would assume thats the best way of doing it but can't figure out how to implement it.

Any suggestions would be greatly appreciated.

-- disclaimer - I would have liked to put this on the silverstripe forum but I am completely unable to sign up for some reason - I never receive the email confirmations. ---

<?php
namespace AffiliateProgram;

use SilverStripe\Forms\GridField\GridField;
use UndefinedOffset\SortableGridField\Forms\GridFieldSortableRows;
use SilverStripe\Security\Permission;
use SilverStripe\ORM\DataObject;


class MemberBonus extends DataObject
 {
private static $db = [
    'Amount' => 'Currency',
    'Confirmed' => 'Boolean',
    'Level' => 'Int',
    'Percentage' => 'Int'
];

private static $has_one = [
    'Member' => 'AffiliateProgram\Member',
    'MemberPayment' => 'AffiliateProgram\MemberPayment',
    'PaymentType' => 'AffiliateProgram\PaymentType',
    'ProgramType' => 'AffiliateProgram\ProgramType'
];

private static $summary_fields = [
    'Amount' => 'Amount (USD)',
    'Member.Email' => 'Email', 
    'Level',
    'MemberPayment.PaymentType.Symbol' => 'Recieved As',
    'Percentage' => 'Percentage Bonus Applied',
    'ProgramType.Name' => 'Program Type',
    'MemberPayment.Created' => 'Payment Date',
    'Confirmed' => 'Confirmed?',
    'MemberPayment.ID' => 'Payment ID'
];
}

There is also a DateCreated column on the table.

1 Answers

You can add custom search fields to a ModelAdmin via getSearchContext(), and customise the query based on them with getList(). See this section of the SilverStripe documentation.

Here's an example of excluding results that have a CreatedAt value below the date provided in a search field (assuming your ModelAdmin only manages MemberBonus):

<?php

use SilverStripe\Admin\ModelAdmin;
use SilverStripe\Forms\DatetimeField;

class MemberBonusAdmin extends ModelAdmin
{
    ...

    public function getSearchContext()
    {
        $context = parent::getSearchContext();

        $context->getFields()->push(new DatetimeField('q[CreatedAfter]', 'Created After'));

        return $context;
    }

    public function getList() 
    {
        $list = parent::getList();

        $params = $this->getRequest()->requestVar('q');

        if (!empty($params['CreatedAfter'])) {
            $list = $list->exclude('CreatedAt:LessThan', $params['CreatedAfter']);
        }

        return $list;
    }
}

To get a range working, you'd just need to add a CreatedBefore field and filter.

Related