1
<?php
2

3
namespace SilverStripe\VersionedAdmin\Extensions;
4

5
use SilverStripe\Assets\AssetControlExtension;
6
use SilverStripe\Assets\File;
7
use SilverStripe\Core\Config\Config;
8
use SilverStripe\Forms\GridField\GridFieldDataColumns;
9
use SilverStripe\ORM\DataExtension;
10
use SilverStripe\ORM\FieldType\DBDatetime;
11
use SilverStripe\Versioned\GridFieldRestoreAction;
12
use SilverStripe\VersionedAdmin\ArchiveAdmin;
13
use SilverStripe\VersionedAdmin\Forms\GridField\GridFieldFileRestoreAction;
14
use SilverStripe\VersionedAdmin\Interfaces\ArchiveViewProvider;
15

16
/**
17
 * Adds a archive view for Files
18
 */
19
class FileArchiveExtension extends DataExtension implements ArchiveViewProvider
20
{
21
    /**
22
     * @inheritDoc
23
    */
24 1
    public function getArchiveFieldClass()
25
    {
26 1
        return File::class;
27
    }
28

29
    /**
30
     * @inheritDoc
31
    */
32 0
    public function getArchiveField()
33
    {
34 0
        $listField = ArchiveAdmin::createArchiveGridField('Files', File::class);
35

36 0
        $list = $listField->getList();
37
        // Paginator reports all records even if some can't be viewed, so we filter them out here
38 0
        $list = $list->filterByCallback(function ($item) {
39 0
            return $item->canView();
40 0
        });
41 0
        $listField->setList($list);
42 0
        $listConfig = $listField->getConfig();
43 0
        $listConfig->removeComponentsByType(GridFieldRestoreAction::class);
44 0
        $listConfig->addComponent(new GridFieldFileRestoreAction());
45

46 0
        $listColumns = $listField->getConfig()->getComponentByType(GridFieldDataColumns::class);
47 0
        $listColumns->setDisplayFields([
48 0
            'Name' => File::singleton()->fieldLabel('Name'),
49 0
            'appCategory' => _t('SilverStripe\\VersionedAdmin\\ArchiveAdmin.COLUMN_TYPE', 'Type'),
50 0
            'allVersions.first.LastEdited' => _t(
51 0
                'SilverStripe\\VersionedAdmin\\ArchiveAdmin.COLUMN_DATEARCHIVED',
52 0
                'Date Archived'
53
            ),
54 0
            'Parent.Name' => _t('SilverStripe\\VersionedAdmin\\ArchiveAdmin.COLUMN_ORIGIN', 'Origin'),
55 0
            'allVersions.first.Author.Name' => _t(
56 0
                'SilverStripe\\VersionedAdmin\\ArchiveAdmin.COLUMN_ARCHIVEDBY',
57 0
                'Archived By'
58
            )
59
        ]);
60 0
        $listColumns->setFieldFormatting([
61 0
            'appCategory' => function ($val, $item) {
62 0
                return ucfirst($val ?: $item->i18n_singular_name());
63 0
            },
64 0
            'allVersions.first.LastEdited' => function ($val, $item) {
65 0
                return DBDatetime::create_field('Datetime', $val)->Ago();
66 0
            },
67
        ]);
68

69 0
        return $listField;
70
    }
71

72
    /**
73
     * The files archive is only useful if archived assets are stored
74
     * so this checks if this option is enabled
75
     *
76
     * @return boolean
77
    */
78 1
    public function isArchiveFieldEnabled()
79
    {
80 1
        return Config::inst()->get(AssetControlExtension::class, 'keep_archived_assets');
81
    }
82
}

Read our documentation on viewing source code .

Loading