Enhancement: SilverStripe 4 compatibility (#137)

* Basic SS4 compat: Implemented namespacing, updated some Image manipulations, readmes, composer configuration

* Add bootstrap form styles to the bulk manager

* API Rename classes to be less confusing with the namespaces
This commit is contained in:
Robbie Averill 2017-01-26 01:27:11 +13:00 committed by Thierry François
parent 0e6f2e6efb
commit 7221897f27
25 changed files with 520 additions and 325 deletions

10
.upgrade.yml Normal file
View File

@ -0,0 +1,10 @@
mappings:
GridFieldBulkActionDeleteHandler: Colymba\BulkManager\BulkAction\DeleteHandler
GridFieldBulkActionEditHandler: Colymba\BulkManager\BulkAction\EditHandler
GridFieldBulkActionHandler: Colymba\BulkManager\BulkAction\Handler
GridFieldBulkActionUnlinkHandler: Colymba\BulkManager\BulkAction\UnlinkHandler
GridFieldBulkManager: Colymba\BulkManager\BulkManager
BulkUploadField: Colymba\BulkUpload\BulkUploadField
GridFieldBulkImageUpload: Colymba\BulkUpload\GridFieldBulkImageUpload
GridFieldBulkUpload: Colymba\BulkUpload\BulkUploader
GridFieldBulkUpload_Request: Colymba\BulkUpload\BulkUploadRequest

View File

@ -5,7 +5,7 @@ GridField Bulk Editing Tools
[![Latest Unstable Version](https://poser.pugx.org/colymba/gridfield-bulk-editing-tools/v/unstable.svg)](https://github.com/colymba/GridFieldBulkEditingTools/tree/master)
[![License](https://poser.pugx.org/colymba/gridfield-bulk-editing-tools/license.svg)](#license-bsd-simplified)
Set of SilverStripe 3 GridField components to facilitate bulk file upload & record editing.
Set of SilverStripe 4 GridField components to facilitate bulk file upload & record editing.
![preview](screenshots/preview.png)
@ -16,37 +16,47 @@ Set of SilverStripe 3 GridField components to facilitate bulk file upload & reco
[More screenshots here.](screenshots)
## Requirements
* SilverStripe 3.1 (version master / 2.+ / 1.+)
* SilverStripe 4.0 (version master / 3.+)
* SilverStripe 3.1 (version 2.+ / 1.+)
* Silverstripe 3.0 (version [0.5](https://github.com/colymba/GridFieldBulkEditingTools/tree/0.5))
## Installation
### Composer
* `composer require "colymba/gridfield-bulk-editing-tools:*"`
* `composer require colymba/gridfield-bulk-editing-tools`
### Manual
* Download and copy module in SilverStripe root directory
## 3.0.0 deprecations
The 3.x versions of this module require SilverStripe 4.x+, and PHP 5.5 or above:
* Namespaces are implemented, and some class names have changed (see `.upgrade.yml` for mapping)
## 2.0.0 deprecations
Major depractions in latest 2.0.0 release:
Major deprections in latest 2.0.0 release:
* The `GridFieldBulkImageUpload` has been renamed to `GridFieldBulkUpload`.
* `onBulkImageUpload` callback has been renamed to `onBulkUpload`
## Bulk Upload
Upload multiple images or files at once into DataObjects. Perfect for galleries and the like.
$config->addComponent(new GridFieldBulkUpload());
```php
$config->addComponent(new \Colymba\BulkUpload\BulkUploader());
```
See [BULK_UPLOAD.md](bulkUpload/BULK_UPLOAD.md) for detailed configuration.
## Bulk Manager
Perform actions on multiple records straight from the GridField
$config->addComponent(new GridFieldBulkManager());
```php
$config->addComponent(new \Colymba\BulkManager\BulkManager());
```
See [BULK_MANAGER.md](bulkManager/BULK_MANAGER.md) for detailed configuration.
## Interdependencies
The `GridFieldBulkUpload` component makes use of `GridFieldBulkManager` to allow quick editing of the newly uploaded files. Although not nescessary for the component to work, adding `GridFieldBulkManager` too to your `GridFieldConfig` will give you this advantage.
The `BulkUploader` component makes use of `BulkManager` to allow quick editing of the newly uploaded files. Although not nescessary for the component to work, adding `Colymba\BulkManager\BulkManager` too to your `GridFieldConfig` will give you this advantage.
#### @TODO
* Add individual actions for each upload (update + cancel)
@ -69,5 +79,5 @@ Redistribution and use in source and binary forms, with or without modification,
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of Thierry Francois, colymba nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View File

@ -3,6 +3,6 @@
if (!defined('BULKEDITTOOLS_PATH')) {
$folder = rtrim(basename(dirname(__FILE__)));
define('BULKEDITTOOLS_PATH', $folder);
define('BULKEDITTOOLS_UPLOAD_PATH', $folder.'/bulkUpload');
define('BULKEDITTOOLS_MANAGER_PATH', $folder.'/bulkManager');
define('BULKEDITTOOLS_UPLOAD_PATH', $folder . '/bulkUpload');
define('BULKEDITTOOLS_MANAGER_PATH', $folder . '/bulkManager');
}

View File

@ -1,22 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
# Phing build script
# phing installation
sudo pear channel-discover pear.phing.info
sudo pear install phing/phing
sudo pear install pear/VersionControl_Git-0.4.4
-->
<project name="gridfield-bulk-editing-tools" default="transifex" phingVersion="2.7.0">
<taskdef name="buildTransifexTranslations" classname="tasks.BuildTransifexTranslations" />
<target name="transifex" description="Fetches Transifex translations and build lang files.">
<echo msg="Building translations..." />
<buildTransifexTranslations
txproject="gridfieldbulkeditingtools"
/>
</target>
</project>

View File

@ -3,14 +3,18 @@ Perform actions on multiple records straight from the GridField. Comes with *unl
## Usage
Simply add component to your `GridFieldConfig`
$config->addComponent(new GridFieldBulkManager());
```php
$config->addComponent(new \Colymba\BulkManager\BulkManager());
```
## Configuration
The component's options can be configurated individually or in bulk through the 'config' functions like this:
$config->getComponentByType('GridFieldBulkManager')->setConfig($reference, $value);
```php
$config->getComponentByType('Colymba\\BulkManager\\BulkManager')->setConfig($reference, $value);
```
### $config overview
The available configuration options are:
* 'editableFields' : array of string referencing specific CMS fields available for editing
@ -21,19 +25,23 @@ You can remove or add individual action or replace them all via `addBulkAction()
### Adding a custom action
To add a custom bulk action to the list use:
$config->getComponentByType('GridFieldBulkManager')->addBulkAction('actionName', 'Dropdown label', 'ActionHandlerClassName', $frontEndConfig)
```php
$config
->getComponentByType('Colymba\\BulkManager\\BulkManager')
->addBulkAction('actionName', 'Dropdown label', 'ActionHandlerClassName', $frontEndConfig)
```
You can omit the handler's class name and the front-end config array, those will default to:
* `'GridFieldBulkAction'.ucfirst($name).'Handler'`
* `$config = array( 'isAjax' => true, 'icon' => 'accept', 'isDestructive' => false )`
#### Custom action handler
When creating your own bulk action `RequestHandler`, you should extend `GridFieldBulkActionHandler` which will expose 2 usefull functions `getRecordIDList()` and `getRecords()` returning either an array with the selected records IDs or a `DataList` of the selected records.
When creating your own bulk action `RequestHandler`, you should extend `Colymba\BulkManager\BulkAction\Handler` which will expose 2 useful functions `getRecordIDList()` and `getRecords()` returning either an array with the selected records IDs or a `DataList` of the selected records.
Make sure to define your `$allowed_actions` and `$url_handlers` on your custom bulk action handler. See `GridFieldBulkActionEditHandler`, `GridFieldBulkActionDeleteHandler` and `GridFieldBulkActionUnlinkHandler` for examples.
Make sure to define your `$allowed_actions` and `$url_handlers` on your custom bulk action handler. See `Handler`, `DeleteHandler` and `UnlinkHandler` for examples.
#### Front-end config
The last `addBulkAction()` parameter lets you pass an array with configuration options for the UI/UX:
* `isAjax`: if true the action will be called via XHR request otherwise the browser will be redirected to the action's URL
* `icon`: lets you define which icon to use on the button when the action is selected (SilverStripe button icon name only)
* `isDestructive`: if true, a confirmation dialog will be shown before the action is processed
* `isDestructive`: if true, a confirmation dialog will be shown before the action is processed

View File

@ -1,10 +1,18 @@
<?php
namespace Colymba\BulkManager\BulkAction;
use Colymba\BulkManager\BulkAction\Handler;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Core\Convert;
/**
* Bulk action handler for deleting records.
*
*
* @author colymba
*/
class GridFieldBulkActionDeleteHandler extends GridFieldBulkActionHandler
class DeleteHandler extends Handler
{
/**
* RequestHandler allowed actions.
@ -24,12 +32,12 @@ class GridFieldBulkActionDeleteHandler extends GridFieldBulkActionHandler
/**
* Delete the selected records passed from the delete bulk action.
*
* @param SS_HTTPRequest $request
*
* @return SS_HTTPResponse List of deleted records ID
* @param HTTPRequest $request
*
* @return HTTPResponse List of deleted records ID
*/
public function delete(SS_HTTPRequest $request)
public function delete(HTTPRequest $request)
{
$ids = array();
@ -38,7 +46,7 @@ class GridFieldBulkActionDeleteHandler extends GridFieldBulkActionHandler
$record->delete();
}
$response = new SS_HTTPResponse(Convert::raw2json(array(
$response = new HTTPResponse(Convert::raw2json(array(
'done' => true,
'records' => $ids,
)));

View File

@ -1,10 +1,25 @@
<?php
namespace Colymba\BulkManager\BulkAction;
use Colymba\BulkManager\BulkAction\Handler;
use SilverStripe\Control\Controller;
use SilverStripe\Core\Convert;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\LiteralField;
use SilverStripe\Forms\ToggleCompositeField;
use SilverStripe\ORM\DataObject;
use SilverStripe\View\Requirements;
/**
* Bulk action handler for editing records.
*
*
* @author colymba
*/
class GridFieldBulkActionEditHandler extends GridFieldBulkActionHandler
class EditHandler extends Handler
{
/**
* RequestHandler allowed actions.
@ -23,9 +38,9 @@ class GridFieldBulkActionEditHandler extends GridFieldBulkActionHandler
* @var array
*/
private static $url_handlers = array(
'bulkEdit/bulkEditForm' => 'bulkEditForm',
'bulkEdit/recordEditForm' => 'recordEditForm',
'bulkEdit' => 'index',
'bulkEdit/bulkEditForm' => 'bulkEditForm',
'bulkEdit/recordEditForm' => 'recordEditForm',
'bulkEdit' => 'index',
);
/**
@ -43,7 +58,7 @@ class GridFieldBulkActionEditHandler extends GridFieldBulkActionHandler
/**
* Return a form for all the selected DataObjects
* with their respective editable fields.
*
*
* @return Form Selected DataObjects editable fields
*/
public function bulkEditForm()
@ -58,7 +73,7 @@ class GridFieldBulkActionEditHandler extends GridFieldBulkActionHandler
$actions->push(
FormAction::create('doSave', _t('GRIDFIELD_BULKMANAGER_EDIT_HANDLER.SAVE_BTN_LABEL', 'Save all'))
->setAttribute('id', 'bulkEditingSaveBtn')
->addExtraClass('ss-ui-action-constructive')
->addExtraClass('btn btn-success')
->setAttribute('data-icon', 'accept')
->setUseButtonTag(true)
);
@ -66,7 +81,7 @@ class GridFieldBulkActionEditHandler extends GridFieldBulkActionHandler
$actions->push(
FormAction::create('Cancel', _t('GRIDFIELD_BULKMANAGER_EDIT_HANDLER.CANCEL_BTN_LABEL', 'Cancel'))
->setAttribute('id', 'bulkEditingUpdateCancelBtn')
->addExtraClass('ss-ui-action-destructive cms-panel-link')
->addExtraClass('btn btn-danger cms-panel-link')
->setAttribute('data-icon', 'decline')
->setAttribute('href', $one_level_up->Link)
->setUseButtonTag(true)
@ -82,9 +97,10 @@ class GridFieldBulkActionEditHandler extends GridFieldBulkActionHandler
$singleton = singleton($modelClass);
$titleModelClass = (($editingCount > 1) ? $singleton->i18n_plural_name() : $singleton->i18n_singular_name());
//some cosmetics
$headerText = _t('GRIDFIELD_BULKMANAGER_EDIT_HANDLER.HEADER_TEXT',
'Editing {count} {class}',
//some cosmetics
$headerText = _t(
'GRIDFIELD_BULKMANAGER_EDIT_HANDLER.HEADER_TEXT',
'Editing {count} {class}',
array(
'count' => $editingCount,
'class' => $titleModelClass,
@ -96,16 +112,19 @@ class GridFieldBulkActionEditHandler extends GridFieldBulkActionHandler
);
$recordsFieldList->push($header);
$toggle = LiteralField::create('bulkEditToggle', '<span id="bulkEditToggle">'._t('GRIDFIELD_BULKMANAGER_EDIT_HANDLER.TOGGLE_ALL_LINK', 'Show/Hide all').'</span>');
$toggle = LiteralField::create(
'bulkEditToggle',
'<span id="bulkEditToggle">' . _t('GRIDFIELD_BULKMANAGER_EDIT_HANDLER.TOGGLE_ALL_LINK', 'Show/Hide all') . '</span>'
);
$recordsFieldList->push($toggle);
//fetch fields for each record and push to fieldList
//fetch fields for each record and push to fieldList
foreach ($recordList as $id) {
$record = DataObject::get_by_id($modelClass, $id);
$recordEditingFields = $this->getRecordEditingFields($record);
$toggleField = ToggleCompositeField::create(
'RecordFields_'.$id,
'RecordFields_' . $id,
$record->getTitle(),
$recordEditingFields
)
@ -128,7 +147,7 @@ class GridFieldBulkActionEditHandler extends GridFieldBulkActionHandler
}
//override form action URL back to bulkEditForm
//and add record ids GET var
//and add record ids GET var
$bulkEditForm->setAttribute(
'action',
$this->Link('bulkEditForm?records[]='.implode('&', $recordList))
@ -140,7 +159,7 @@ class GridFieldBulkActionEditHandler extends GridFieldBulkActionHandler
/**
* Return's a form with only one record's fields
* Used for bulkEditForm subForm requests via ajax.
*
*
* @return Form Currently being edited form
*/
public function recordEditForm()
@ -184,7 +203,7 @@ class GridFieldBulkActionEditHandler extends GridFieldBulkActionHandler
* with all filtering done ready to be included in the main form.
*
* @uses DataObject::getCMSFields()
*
*
* @param DataObject $record The record to get the fields from
*
* @return array The record's editable fields
@ -192,7 +211,8 @@ class GridFieldBulkActionEditHandler extends GridFieldBulkActionHandler
private function getRecordEditingFields(DataObject $record)
{
$tempForm = Form::create(
$this, 'TempEditForm',
$this,
'TempEditForm',
$record->getCMSFields(),
FieldList::create()
);
@ -210,8 +230,8 @@ class GridFieldBulkActionEditHandler extends GridFieldBulkActionHandler
* based on component's config
* and escape each field with unique name.
*
* See {@link GridFieldBulkManager} component for filtering config.
*
* See {@link BulkManager} component for filtering config.
*
* @param FieldList $fields Record's CMS Fields
* @param int $id Record's ID, used fir unique name
*
@ -222,7 +242,7 @@ class GridFieldBulkActionEditHandler extends GridFieldBulkActionHandler
$config = $this->component->getConfig();
$editableFields = $config['editableFields'];
// get all dataFields or just the ones allowed in config
// get all dataFields or just the ones allowed in config
if ($editableFields) {
$dataFields = array();
@ -244,7 +264,7 @@ class GridFieldBulkActionEditHandler extends GridFieldBulkActionHandler
/**
* Escape a fieldName with a unique prefix.
*
*
* @param int $recordID Record id from who the field belongs
* @param string $name Field name
*
@ -252,12 +272,12 @@ class GridFieldBulkActionEditHandler extends GridFieldBulkActionHandler
*/
protected function escapeFieldName($recordID, $name)
{
return 'record_'.$recordID.'_'.$name;
return 'record_' . $recordID . '_' . $name;
}
/**
* Un-escape a previously escaped field name.
*
*
* @param string $fieldName Escaped field name
*
* @return array|false Fasle if the fieldName was not escaped. Or Array map with record 'id' and field 'name'
@ -271,35 +291,38 @@ class GridFieldBulkActionEditHandler extends GridFieldBulkActionHandler
return false;
} else {
return array(
'id' => $parts[1],
'name' => $parts[2],
'id' => $parts[1],
'name' => $parts[2],
);
}
}
/**
* Creates and return the bulk editing interface.
*
*
* @return string Form's HTML
*/
public function index()
{
$form = $this->bulkEditForm();
$form->setTemplate('LeftAndMain_EditForm');
$form->setTemplate([
'type' => 'Includes',
'SilverStripe\\Admin\\LeftAndMain_EditForm',
]);
$form->addExtraClass('center cms-content');
$form->setAttribute('data-pjax-fragment', 'CurrentForm Content');
Requirements::javascript(BULKEDITTOOLS_MANAGER_PATH.'/javascript/GridFieldBulkEditingForm.js');
Requirements::css(BULKEDITTOOLS_MANAGER_PATH.'/css/GridFieldBulkEditingForm.css');
Requirements::add_i18n_javascript(BULKEDITTOOLS_PATH.'/lang/js');
Requirements::javascript(BULKEDITTOOLS_MANAGER_PATH . '/javascript/GridFieldBulkEditingForm.js');
Requirements::css(BULKEDITTOOLS_MANAGER_PATH . '/css/GridFieldBulkEditingForm.css');
Requirements::add_i18n_javascript(BULKEDITTOOLS_PATH . '/lang/js');
if ($this->request->isAjax()) {
$response = new SS_HTTPResponse(
$response = new HTTPResponse(
Convert::raw2json(array('Content' => $form->forAjaxTemplate()->getValue()))
);
$response->addHeader('X-Pjax', 'Content');
$response->addHeader('Content-Type', 'text/json');
$response->addHeader('X-Title', 'SilverStripe - Bulk '.$this->gridField->list->dataClass.' Editing');
$response->addHeader('X-Title', 'SilverStripe - Bulk ' . $this->gridField->list->dataClass . ' Editing');
return $response;
} else {
@ -312,7 +335,7 @@ class GridFieldBulkActionEditHandler extends GridFieldBulkActionHandler
/**
* Handles bulkEditForm submission
* and parses and saves each records data.
*
*
* @param array $data Sumitted form data
* @param Form $form Form
*/
@ -340,7 +363,8 @@ class GridFieldBulkActionEditHandler extends GridFieldBulkActionHandler
foreach ($formsData as $recordID => $recordData) {
$record = DataObject::get_by_id($className, $recordID);
$recordForm = Form::create(
$this, 'RecordForm',
$this,
'RecordForm',
$record->getCMSFields(),
FieldList::create()
);
@ -358,8 +382,9 @@ class GridFieldBulkActionEditHandler extends GridFieldBulkActionHandler
//compose form message
$messageModelClass = (($done > 1) ? $singleton->i18n_plural_name() : $singleton->i18n_singular_name());
$message = _t('GRIDFIELD_BULKMANAGER_EDIT_HANDLER.SAVE_RESULT_TEXT',
'{count} {class} saved successfully.',
$message = _t(
'GRIDFIELD_BULKMANAGER_EDIT_HANDLER.SAVE_RESULT_TEXT',
'{count} {class} saved successfully.',
array(
'count' => $done,
'class' => $messageModelClass,
@ -368,7 +393,7 @@ class GridFieldBulkActionEditHandler extends GridFieldBulkActionHandler
$form->sessionMessage($message, 'good');
//return back to form
return Controller::curr()->redirect($this->Link('?records[]='.implode('&records[]=', $ids)));
return Controller::curr()->redirect($this->Link('?records[]=' . implode('&records[]=', $ids)));
//return Controller::curr()->redirect($form->Backlink); //returns to gridField
}
}

View File

@ -1,12 +1,22 @@
<?php
namespace Colymba\BulkManager\BulkAction;
use SilverStripe\Control\Controller;
use SilverStripe\Control\RequestHandler;
use SilverStripe\Forms\GridField\GridFieldDetailForm;
use SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest;
use SilverStripe\ORM\DataList;
use SilverStripe\View\ArrayData;
/**
* Base class to extend for all custom bulk action handlers
* Gives access to the GridField, Component and Controller
* and implements useful functions like {@link getRecordIDList()} and {@link getRecords()}.
*
*
* @author colymba
*/
class GridFieldBulkActionHandler extends RequestHandler
class Handler extends RequestHandler
{
/**
* Related GridField instance.
@ -30,7 +40,7 @@ class GridFieldBulkActionHandler extends RequestHandler
protected $controller;
/**
* @param GridFIeld $gridField
* @param GridField $gridField
* @param GridField_URLHandler $component
* @param Controller $controller
*/
@ -44,7 +54,7 @@ class GridFieldBulkActionHandler extends RequestHandler
/**
* Returns the URL for this RequestHandler.
*
*
* @author SilverStripe
*
* @see GridFieldDetailForm_ItemRequest
@ -62,7 +72,7 @@ class GridFieldBulkActionHandler extends RequestHandler
* Traverse up nested requests until we reach the first that's not a GridFieldDetailForm or GridFieldDetailForm_ItemRequest.
* The opposite of {@link Controller::curr()}, required because
* Controller::$controller_stack is not directly accessible.
*
*
* @return Controller
*/
protected function getToplevelController()
@ -78,11 +88,11 @@ class GridFieldBulkActionHandler extends RequestHandler
/**
* Edited version of the GridFieldEditForm function
* adds the 'Bulk Upload' at the end of the crums.
*
*
* CMS-specific functionality: Passes through navigation breadcrumbs
* to the template, and includes the currently edited record (if any).
* see {@link LeftAndMain->Breadcrumbs()} for details.
*
*
* @author SilverStripe original Breadcrumbs() method
*
* @see GridFieldDetailForm_ItemRequest
@ -99,16 +109,16 @@ class GridFieldBulkActionHandler extends RequestHandler
$items = $this->controller->Breadcrumbs($unlinked);
$items->push(new ArrayData(array(
'Title' => 'Bulk Editing',
'Link' => false,
)));
'Title' => 'Bulk Editing',
'Link' => false,
)));
return $items;
}
/**
* Returns the list of record IDs selected in the front-end.
*
*
* @return array List of IDs
*/
public function getRecordIDList()
@ -120,7 +130,7 @@ class GridFieldBulkActionHandler extends RequestHandler
/**
* Returns a DataList of the records selected in the front-end.
*
*
* @return DataList List of records
*/
public function getRecords()

View File

@ -1,10 +1,18 @@
<?php
namespace Colymba\BulkManager\BulkAction;
use Colymba\BulkManager\BulkAction\Handler;
use SilverStripe\Core\Convert;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
/**
* Bulk action handler for unlinking records.
*
*
* @author colymba
*/
class GridFieldBulkActionUnlinkHandler extends GridFieldBulkActionHandler
class UnlinkHandler extends Handler
{
/**
* RequestHandler allowed actions.
@ -24,17 +32,17 @@ class GridFieldBulkActionUnlinkHandler extends GridFieldBulkActionHandler
/**
* Unlink the selected records passed from the unlink bulk action.
*
* @param SS_HTTPRequest $request
*
* @return SS_HTTPResponse List of affected records ID
* @param HTTPRequest $request
*
* @return HTTPResponse List of affected records ID
*/
public function unLink(SS_HTTPRequest $request)
public function unLink(HTTPRequest $request)
{
$ids = $this->getRecordIDList();
$this->gridField->list->removeMany($ids);
$response = new SS_HTTPResponse(Convert::raw2json(array(
$response = new HTTPResponse(Convert::raw2json(array(
'done' => true,
'records' => $ids,
)));

View File

@ -1,27 +1,42 @@
<?php
namespace Colymba\BulkManager;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\GridField\GridField_HTMLProvider;
use SilverStripe\Forms\GridField\GridField_ColumnProvider;
use SilverStripe\Forms\GridField\GridField_URLHandler;
use SilverStripe\ORM\DataModel;
use SilverStripe\View\ArrayData;
use SilverStripe\View\Requirements;
/**
* GridField component for editing attached models in bulk.
*
* @author colymba
*/
class GridFieldBulkManager implements GridField_HTMLProvider, GridField_ColumnProvider, GridField_URLHandler
class BulkManager implements GridField_HTMLProvider, GridField_ColumnProvider, GridField_URLHandler
{
/**
* component configuration.
*
*
* 'editableFields' => fields editable on the Model
* 'actions' => maps of action name and configuration
*
*
* @var array
*/
protected $config = array(
'editableFields' => null,
'actions' => array(),
'editableFields' => null,
'actions' => array(),
);
/**
* GridFieldBulkManager component constructor.
*
*
* @param array $editableFields List of editable fields
* @param bool $defaultActions Use default actions list. False to start fresh.
*/
@ -33,33 +48,33 @@ class GridFieldBulkManager implements GridField_HTMLProvider, GridField_ColumnPr
if ($defaultActions) {
$this->config['actions'] = array(
'bulkEdit' => array(
'label' => _t('GRIDFIELD_BULK_MANAGER.EDIT_SELECT_LABEL', 'Edit'),
'handler' => 'GridFieldBulkActionEditHandler',
'config' => array(
'bulkEdit' => array(
'label' => _t('GRIDFIELD_BULK_MANAGER.EDIT_SELECT_LABEL', 'Edit'),
'handler' => 'Colymba\\BulkManager\\BulkAction\\EditHandler',
'config' => array(
'isAjax' => false,
'icon' => 'pencil',
'isDestructive' => false,
),
),
'unLink' => array(
'label' => _t('GRIDFIELD_BULK_MANAGER.UNLINK_SELECT_LABEL', 'UnLink'),
'handler' => 'GridFieldBulkActionUnlinkHandler',
'config' => array(
),
'unLink' => array(
'label' => _t('GRIDFIELD_BULK_MANAGER.UNLINK_SELECT_LABEL', 'UnLink'),
'handler' => 'Colymba\\BulkManager\\BulkAction\\UnlinkHandler',
'config' => array(
'isAjax' => true,
'icon' => 'chain--minus',
'isDestructive' => false,
),
),
'delete' => array(
'label' => _t('GRIDFIELD_BULK_MANAGER.DELETE_SELECT_LABEL', 'Delete'),
'handler' => 'GridFieldBulkActionDeleteHandler',
'config' => array(
),
'delete' => array(
'label' => _t('GRIDFIELD_BULK_MANAGER.DELETE_SELECT_LABEL', 'Delete'),
'handler' => 'Colymba\\BulkManager\\BulkAction\\DeleteHandler',
'config' => array(
'isAjax' => true,
'icon' => 'decline',
'isDestructive' => true,
),
),
)
);
}
}
@ -70,7 +85,7 @@ class GridFieldBulkManager implements GridField_HTMLProvider, GridField_ColumnPr
/**
* Sets the component configuration parameter.
*
*
* @param string $reference
* @param mixed $value
*/
@ -95,7 +110,7 @@ class GridFieldBulkManager implements GridField_HTMLProvider, GridField_ColumnPr
/**
* Returns one $config parameter of the full $config.
*
*
* @param string $reference $congif parameter to return
*
* @return mixed
@ -113,7 +128,7 @@ class GridFieldBulkManager implements GridField_HTMLProvider, GridField_ColumnPr
* Lets you add custom bulk actions to the bulk manager interface.
*
* @todo add config options for front-end: isAjax, icon
*
*
* @param string $name Bulk action's name. Used by RequestHandler.
* @param string $label Dropdown menu action's label. Default to ucfirst($name).
* @param string $handler RequestHandler class name for this action. Default to 'GridFieldBulkAction'.ucfirst($name).'Handler'
@ -132,7 +147,7 @@ class GridFieldBulkManager implements GridField_HTMLProvider, GridField_ColumnPr
}
if (!$handler) {
$handler = 'GridFieldBulkAction'.ucfirst($name).'Handler';
$handler = 'Colymba\\BulkManager\\BulkAction\\' . ucfirst($name) . 'Handler';
}
if (!ClassInfo::exists($handler)) {
@ -150,17 +165,17 @@ class GridFieldBulkManager implements GridField_HTMLProvider, GridField_ColumnPr
}
$this->config['actions'][$name] = array(
'label' => $label,
'handler' => $handler,
'config' => $config,
);
'label' => $label,
'handler' => $handler,
'config' => $config,
);
return $this;
}
/**
* Removes a bulk actions from the bulk manager interface.
*
*
* @param string $name Bulk action's name
*
* @return GridFieldBulkManager Current GridFieldBulkManager instance
@ -182,7 +197,7 @@ class GridFieldBulkManager implements GridField_HTMLProvider, GridField_ColumnPr
/**
* Add bulk select column.
*
*
* @param GridField $gridField Current GridField instance
* @param array $columns Columns list
*/
@ -195,7 +210,7 @@ class GridFieldBulkManager implements GridField_HTMLProvider, GridField_ColumnPr
/**
* Which columns are handled by the component.
*
*
* @param GridField $gridField Current GridField instance
*
* @return array List of handled column names
@ -207,7 +222,7 @@ class GridFieldBulkManager implements GridField_HTMLProvider, GridField_ColumnPr
/**
* Sets the column's content.
*
*
* @param GridField $gridField Current GridField instance
* @param DataObject $record Record intance for this row
* @param string $columnName Column's name for which we need content
@ -216,16 +231,16 @@ class GridFieldBulkManager implements GridField_HTMLProvider, GridField_ColumnPr
*/
public function getColumnContent($gridField, $record, $columnName)
{
$cb = CheckboxField::create('bulkSelect_'.$record->ID)
->addExtraClass('bulkSelect no-change-track')
->setAttribute('data-record', $record->ID);
$cb = CheckboxField::create('bulkSelect_' . $record->ID)
->addExtraClass('bulkSelect no-change-track')
->setAttribute('data-record', $record->ID);
return $cb->Field();
}
/**
* Set the column's HTML attributes.
*
*
* @param GridField $gridField Current GridField instance
* @param DataObject $record Record intance for this row
* @param string $columnName Column's name for which we need attributes
@ -239,7 +254,7 @@ class GridFieldBulkManager implements GridField_HTMLProvider, GridField_ColumnPr
/**
* Set the column's meta data.
*
*
* @param GridField $gridField Current GridField instance
* @param string $columnName Column's name for which we need meta data
*
@ -263,9 +278,9 @@ class GridFieldBulkManager implements GridField_HTMLProvider, GridField_ColumnPr
*/
public function getHTMLFragments($gridField)
{
Requirements::css(BULKEDITTOOLS_MANAGER_PATH.'/css/GridFieldBulkManager.css');
Requirements::javascript(BULKEDITTOOLS_MANAGER_PATH.'/javascript/GridFieldBulkManager.js');
Requirements::add_i18n_javascript(BULKEDITTOOLS_PATH.'/lang/js');
Requirements::css(BULKEDITTOOLS_MANAGER_PATH . '/css/GridFieldBulkManager.css');
Requirements::javascript(BULKEDITTOOLS_MANAGER_PATH . '/javascript/GridFieldBulkManager.js');
Requirements::add_i18n_javascript(BULKEDITTOOLS_PATH . '/lang/js');
if (!count($this->config['actions'])) {
user_error('Trying to use GridFieldBulkManager without any bulk action.', E_USER_ERROR);
@ -284,22 +299,22 @@ class GridFieldBulkManager implements GridField_HTMLProvider, GridField_ColumnPr
$dropDownActionsList = DropdownField::create('bulkActionName', '')
->setSource($actionsListSource)
->setAttribute('class', 'bulkActionName no-change-track')
->addExtraClass('bulkActionName no-change-track no-chosen')
->setAttribute('id', '');
$templateData = array(
'Menu' => $dropDownActionsList->FieldHolder(),
'Button' => array(
'Label' => _t('GRIDFIELD_BULK_MANAGER.ACTION_BTN_LABEL', 'Go'),
'DataURL' => $gridField->Link('bulkAction'),
'Icon' => $this->config['actions'][$firstAction]['config']['icon'],
'DataConfig' => htmlspecialchars(json_encode($actionsConfig), ENT_QUOTES, 'UTF-8'),
),
'Select' => array(
'Label' => _t('GRIDFIELD_BULK_MANAGER.SELECT_ALL_LABEL', 'Select all'),
),
'Colspan' => (count($gridField->getColumns()) - 1),
);
'Menu' => $dropDownActionsList,
'Button' => array(
'Label' => _t('GRIDFIELD_BULK_MANAGER.ACTION_BTN_LABEL', 'Go'),
'DataURL' => $gridField->Link('bulkAction'),
'Icon' => $this->config['actions'][$firstAction]['config']['icon'],
'DataConfig' => json_encode($actionsConfig),
),
'Select' => array(
'Label' => _t('GRIDFIELD_BULK_MANAGER.SELECT_ALL_LABEL', 'Select all'),
),
'Colspan' => (count($gridField->getColumns()) - 1),
);
$templateData = new ArrayData($templateData);
@ -314,7 +329,7 @@ class GridFieldBulkManager implements GridField_HTMLProvider, GridField_ColumnPr
/**
* Returns an action => handler list.
*
*
* @param GridField $gridField
*
* @return array
@ -322,8 +337,8 @@ class GridFieldBulkManager implements GridField_HTMLProvider, GridField_ColumnPr
public function getURLHandlers($gridField)
{
return array(
'bulkAction' => 'handleBulkAction',
);
'bulkAction' => 'handleBulkAction',
);
}
/**
@ -333,15 +348,15 @@ class GridFieldBulkManager implements GridField_HTMLProvider, GridField_ColumnPr
*
* $url_handlers rule should not use wildcards like '$Action' => '$Action'
* but have more specific path defined
*
*
* @param GridField $gridField
* @param SS_HTTPRequest $request
* @param HTTPRequest $request
*
* @return mixed
*/
public function handleBulkAction($gridField, $request)
{
$controller = $gridField->getForm()->Controller();
$controller = $gridField->getForm()->getController();
foreach ($this->config['actions'] as $name => $data) {
$handlerClass = $data['handler'];
@ -351,7 +366,7 @@ class GridFieldBulkManager implements GridField_HTMLProvider, GridField_ColumnPr
foreach ($urlHandlers as $rule => $action) {
if ($request->match($rule, false)) {
//print_r('matched ' . $handlerClass . ' to ' . $rule);
$handler = Injector::inst()->create($handlerClass, $gridField, $this, $controller);
$handler = Injector::inst()->create($handlerClass, $gridField, $this, $controller);
return $handler->handleRequest($request, DataModel::inst());
}
@ -359,6 +374,6 @@ class GridFieldBulkManager implements GridField_HTMLProvider, GridField_ColumnPr
}
}
user_error('Unable to find matching bulk action handler for '.$request->remaining().'.', E_USER_ERROR);
user_error('Unable to find matching bulk action handler for ' . $request->remaining() . '.', E_USER_ERROR);
}
}

View File

@ -31,12 +31,14 @@
text-shadow: none; }
.bulkEditingFieldHolder.updated .ui-accordion-header {
background-color: #a4ca3a;
background-image: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #a4ca3a), color-stop(100%, #59781d));
background-image: -webkit-linear-gradient(top, #a4ca3a 0%, #59781d 100%);
background-image: -moz-linear-gradient(top, #a4ca3a 0%, #59781d 100%);
background-image: -o-linear-gradient(top, #a4ca3a 0%, #59781d 100%);
background-image: -ms-linear-gradient(top, #a4ca3a 0%, #59781d 100%);
background-image: linear-gradient(top, #a4ca3a 0%, #59781d 100%); }
background-image: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #a4ca3a), color-stop(100%, #59781D));
background-image: -webkit-linear-gradient(top, #a4ca3a 0%, #59781D 100%);
background-image: -moz-linear-gradient(top, #a4ca3a 0%, #59781D 100%);
background-image: -o-linear-gradient(top, #a4ca3a 0%, #59781D 100%);
background-image: -ms-linear-gradient(top, #a4ca3a 0%, #59781D 100%);
background-image: linear-gradient(top, #a4ca3a 0%, #59781D 100%); }
.bulkEditingFieldHolder.updated .ui-accordion-header a {
color: #fff;
text-shadow: none; }
/*# sourceMappingURL=GridFieldBulkEditingForm.css.map */

View File

@ -0,0 +1,7 @@
{
"version": 3,
"mappings": "AACA,eACA;EACC,KAAK,EAAE,IAAI;EACX,KAAK,EAAE,GAAG;EAEV,MAAM,EAAE,aAAa;EAErB,SAAS,EAAE,IAAI;EACf,WAAW,EAAE,IAAI;;AAGlB,eACA;EACC,KAAK,EAAE,KAAK;EACZ,KAAK,EAAE,KAAK;EACZ,KAAK,EAAE,GAAG;EAEV,MAAM,EAAE,aAAa;EAErB,eAAe,EAAE,SAAS;EAC1B,UAAU,EAAE,KAAK;EACjB,MAAM,EAAE,OAAO;;AAGhB,uBACA;EACC,QAAQ,EAAE,QAAQ;EAClB,KAAK,EAAE,IAAI;EACX,KAAK,EAAE,IAAI;EAEX,sDACA;IACC,gBAAgB,EAAE,OAAO;IAEzB,gBAAgB,EAAE,8FAA8F;IAChH,gBAAgB,EAAE,sDAAsD;IACxE,gBAAgB,EAAE,mDAAmD;IACrE,gBAAgB,EAAE,iDAAiD;IACnE,gBAAgB,EAAE,kDAAkD;IACpE,gBAAgB,EAAE,8CAA8C;IAEhE,wDACA;MACC,KAAK,EAAE,IAAI;MACX,WAAW,EAAE,IAAI;EAInB,oDACA;IACC,gBAAgB,EAAE,OAAO;IAEzB,gBAAgB,EAAE,8FAA8F;IAChH,gBAAgB,EAAE,sDAAsD;IACxE,gBAAgB,EAAE,mDAAmD;IACrE,gBAAgB,EAAE,iDAAiD;IACnE,gBAAgB,EAAE,kDAAkD;IACpE,gBAAgB,EAAE,8CAA8C;IAEhE,sDACA;MACC,KAAK,EAAE,IAAI;MACX,WAAW,EAAE,IAAI",
"sources": ["GridFieldBulkEditingForm.scss"],
"names": [],
"file": "GridFieldBulkEditingForm.css"
}

View File

@ -1,10 +1,12 @@
.cms table.ss-gridfield-table tr.bulkManagerOptions th.bulkmanagerselect {
.cms table.grid-field__table tr.bulkManagerOptions th.bulkmanagerheading {
padding-left: 40px; }
.cms table.grid-field__table tr.bulkManagerOptions th.bulkmanagerselect {
text-align: center; }
.cms table.ss-gridfield-table tr.bulkManagerOptions th.bulkmanagerselect input {
.cms table.grid-field__table tr.bulkManagerOptions th.bulkmanagerselect input {
margin-left: 0; }
.cms table.ss-gridfield-table tr.bulkManagerOptions .title {
.cms table.grid-field__table tr.bulkManagerOptions .title {
margin: 0 0 2px 2px; }
.cms table.ss-gridfield-table tr.bulkManagerOptions .dropdown {
.cms table.grid-field__table tr.bulkManagerOptions .dropdown {
display: inline-block;
border: none;
box-shadow: none;
@ -12,17 +14,17 @@
margin: 0;
color: #000;
vertical-align: top; }
.cms table.ss-gridfield-table tr.bulkManagerOptions .doBulkActionButton .ui-button-text {
.cms table.grid-field__table tr.bulkManagerOptions .doBulkActionButton .ui-button-text {
padding: 6px 0 5px 2.1em; }
.cms table.ss-gridfield-table tr.bulkManagerOptions .doBulkActionButton .ui-button-text .ui-button-text {
.cms table.grid-field__table tr.bulkManagerOptions .doBulkActionButton .ui-button-text .ui-button-text {
padding: 0; }
.cms table.ss-gridfield-table tr.bulkManagerOptions .doBulkActionButton .loading .ui-icon {
.cms table.grid-field__table tr.bulkManagerOptions .doBulkActionButton .loading .ui-icon {
background: transparent url(../../framework/images/network-save.gif) no-repeat 0 0; }
.cms table.ss-gridfield-table tbody .col-bulkSelect {
.cms table.grid-field__table tbody .col-bulkSelect {
width: 25px;
padding: 0 10px;
text-align: center; }
.cms table.ss-gridfield-table tbody td a.tempDisabledEditLink {
.cms table.grid-field__table tbody td a.tempDisabledEditLink {
background: url("../../framework/admin/images/btn-icon/document--pencil.png") no-repeat 2px 0px;
display: inline-block;
width: 20px;
@ -32,3 +34,5 @@
vertical-align: middle;
background-position: 2px 0px;
background-repeat: no-repeat no-repeat; }
/*# sourceMappingURL=GridFieldBulkManager.css.map */

View File

@ -0,0 +1,7 @@
{
"version": 3,
"mappings": "AAII,wEAAsB;EAClB,YAAY,EAAE,IAAI;AAGtB,uEACA;EACE,UAAU,EAAE,MAAM;EAElB,6EACA;IACE,WAAW,EAAE,CAAC;AAIlB,yDACA;EACE,MAAM,EAAE,WAAW;AAGrB,4DACA;EACE,OAAO,EAAE,YAAY;EACrB,MAAM,EAAE,IAAI;EACZ,UAAU,EAAE,IAAI;EAEhB,OAAO,EAAE,CAAC;EACV,MAAM,EAAE,CAAC;EAET,KAAK,EAAE,IAAI;EACX,cAAc,EAAE,GAAG;AAKnB,sFACA;EACE,OAAO,EAAE,eAAe;EAExB,sGACA;IACE,OAAO,EAAE,CAAC;AAId,wFACA;EACE,UAAU,EAAE,sEAAsE;AAOtF,kDACA;EACE,KAAK,EAAE,IAAI;EACX,OAAO,EAAE,MAAM;EACf,UAAU,EAAE,MAAM;AAGpB,4DACA;EACE,UAAU,EAAE,mFAAmF;EAC/F,OAAO,EAAE,YAAY;EACrB,KAAK,EAAE,IAAI;EACX,MAAM,EAAE,IAAI;EACZ,WAAW,EAAE,QAAQ;EACrB,QAAQ,EAAE,MAAM;EAChB,cAAc,EAAE,MAAM;EACtB,mBAAmB,EAAE,OAAO;EAC5B,iBAAiB,EAAE,mBAAmB",
"sources": ["GridFieldBulkManager.scss"],
"names": [],
"file": "GridFieldBulkManager.css"
}

View File

@ -1,7 +1,11 @@
.cms table.ss-gridfield-table
.cms table.grid-field__table
{
tr.bulkManagerOptions
{
th.bulkmanagerheading {
padding-left: 40px;
}
th.bulkmanagerselect
{
text-align: center;
@ -17,7 +21,7 @@
margin: 0 0 2px 2px;
}
.dropdown
.dropdown
{
display: inline-block;
border: none;
@ -71,4 +75,4 @@
background-repeat: no-repeat no-repeat;
}
}
}
}

View File

@ -1,5 +1,5 @@
(function($) {
$.entwine('ss', function($) {
(function($) {
$.entwine('ss', function($) {
$.entwine('colymba', function($) {
@ -34,14 +34,14 @@
onunmatch: function(){}
});
/**
* Bulkselect table cell behaviours
*/
$('td.col-bulkSelect').entwine({
onmatch: function(){
},
onunmatch: function(){
onunmatch: function(){
},
onmouseover: function(){
//disable default row click behaviour -> avoid navigation to edit form when clicking the checkbox
@ -59,20 +59,20 @@
}
});
/**
* Individual select checkbox behaviour
*/
$('td.col-bulkSelect input').entwine({
onmatch: function(){
},
onunmatch: function(){
onunmatch: function(){
},
onclick: function(e) {
$(this).parents('.ss-gridfield-table').find('input.bulkSelectAll').prop('checked', '');
$(this).parents('.grid-field__table').find('input.bulkSelectAll').prop('checked', '');
}
});
/**
* Bulkselect checkbox behaviours
@ -80,35 +80,35 @@
$('input.bulkSelectAll').entwine({
onmatch: function(){
},
onunmatch: function(){
onunmatch: function(){
},
onclick: function()
{
var state = $(this).prop('checked');
$(this).parents('.ss-gridfield-table')
$(this).parents('.grid-field__table')
.find('td.col-bulkSelect input')
.prop('checked', state)
.trigger('change');
},
getSelectRecordsID: function()
{
return $(this).parents('.ss-gridfield-table')
return $(this).parents('.grid-field__table')
.find('td.col-bulkSelect input:checked')
.map(function() {
.map(function() {
return parseInt( $(this).data('record') )
})
.get();
}
});
/**
* Bulk action dropdown behaviours
*/
$('select.bulkActionName').entwine({
onmatch: function(){
},
onunmatch: function(){
onunmatch: function(){
},
onchange: function(e)
{
@ -136,10 +136,10 @@
else{
$btn.removeClass('ss-ui-action-destructive');
}
}
}
});
/**
* bulk action button behaviours
@ -147,7 +147,7 @@
$('.doBulkActionButton').entwine({
onmatch: function(){
},
onunmatch: function(){
onunmatch: function(){
},
getActionURL: function(action, url)
{
@ -176,13 +176,13 @@
var $parent = $(this).parents('.bulkManagerOptions'),
action = $parent.find('select.bulkActionName').val(),
ids = $(this).parents('.bulkManagerOptions').find('input.bulkSelectAll:first').getSelectRecordsID()
;
;
this.doBulkAction(action, ids);
this.doBulkAction(action, ids);
},
doBulkAction: function(action, ids, callbackFunction, callbackContext)
{
{
var $parent = $(this).parents('.bulkManagerOptions'),
$btn = $parent.find('a.doBulkActionButton'),
@ -190,7 +190,7 @@
url = this.getActionURL(action, $(this).data('url')),
data = { records: ids }
;
if ( ids.length <= 0 )
{
alert( ss.i18n._t('GRIDFIELD_BULK_MANAGER.BULKACTION_EMPTY_SELECT') );
@ -207,10 +207,10 @@
callbackFunction.call(callbackContext, false);
}
return false;
}
}
}
}
$btn.addClass('loading');
$btn.addClass('loading');
if ( config[action]['isAjax'] )
{
@ -219,7 +219,7 @@
data: data,
type: "POST",
context: $(this)
}).done(function(data, textStatus, jqXHR) {
}).done(function(data, textStatus, jqXHR) {
$btn.removeClass('loading');
if ( callbackFunction && callbackContext )
{
@ -239,7 +239,7 @@
}
});
});
});
});
}(jQuery));

View File

@ -1,15 +1,25 @@
<tr class="bulkManagerOptions">
<th class="main bulkmanagerheading" colspan="$Colspan">
<p class="title"><% _t('GRIDFIELD_BULK_MANAGER.COMPONENT_TITLE', 'Modify one or more entry at a time.') %></p>
$Menu
<a data-url="$Button.DataURL" data-config="$Button.DataConfig" class="doBulkActionButton ss-ui-button" data-icon="$Button.Icon">
$Button.Label
</a>
</th>
<th class="main bulkmanagerselect">
<input class="no-change-track bulkSelectAll" type="checkbox" title="$Select.Label" name="toggleSelectAll" />
</th>
</tr>
<th class="main bulkmanagerheading" colspan="$Colspan">
<div class="row">
<p class="title"><% _t('GRIDFIELD_BULK_MANAGER.COMPONENT_TITLE', 'Modify one or more entry at a time.') %></p>
</div>
<div class="row">
<div class="form-inline">
$Menu
<a data-url="$Button.DataURL"
data-config="$Button.DataConfig"
class="doBulkActionButton btn btn-primary"
data-icon="$Button.Icon">
$Button.Label
</a>
<label class="form-check-label">
<input class="no-change-track bulkSelectAll form-check-input"
type="checkbox"
title="$Select.Label"
name="toggleSelectAll" />
<% _t('GRIDFIELD_BULK_MANAGER.SELECT_ALL_LABEL', '$Select.Label') %>
</label>
</div>
</div>
</th>
</tr>

View File

@ -4,20 +4,26 @@ A component for uploading images and/or files in bulk into `DataObject` managed
## Usage 1
Simplest usage, add the component to your `GridFieldConfig` as below. The component will find the first `Image` or `File` has_one relation to use on the managed `DataObject`.
$config->addComponent(new GridFieldBulkUpload());
```php
$config->addComponent(new \Colymba\BulkUpload\BulkUploader());
```
## Usage 2
You can specify which `Image` or `File` field to use and a specific `DataObject` class name to use.
$fileRelationName (string, optional): The name of the `Image` or `File` has_one field to use (If your relation is set has 'MyImage' => 'Image', the parameter should be 'MyImage')
$recordClassName (string, optional): The class name of the `DataObject` to create (Usefull if for example your `GridField` holds `DataObject`s of different classes, like when used with the `GridFieldAddNewMultiClass` component.)
$config->addComponent(new GridFieldBulkUpload($fileRelationName, $recordClassName));
```php
$config->addComponent(new \Colymba\BulkUpload\BulkUploader($fileRelationName, $recordClassName));
```
## Configuration
### Component configuration
The component's option can be configurated through the `setConfig` functions like this:
$config->getComponentByType('GridFieldBulkUpload')->setConfig($reference, $value);
```php
$config->getComponentByType('Colymba\\BulkUpload\\BulkUploader')->setConfig($reference, $value);
```
The available configuration options are:
* 'fileRelationName' : sets the name of the `Image` or `File` has_one field to use (i.e. 'MyImage')
@ -30,12 +36,14 @@ The underlying `UploadField` can be configured via a set of APIs:
For example, to set the upload folder, which is set by calling `setFolderName` on the `UploadField`, and setting the upload method as sequential, you would use the following:
$config->getComponentByType('GridFieldBulkUpload')
->setUfSetup('setFolderName', 'myFolder')
->setUfConfig('sequentialUploads', true);
```php
$config->getComponentByType('Colymba\\BulkUpload\\BulkUploader')
->setUfSetup('setFolderName', 'myFolder')
->setUfConfig('sequentialUploads', true);
```
Please see the [`UploadField` api](http://api.silverstripe.org/master/class-UploadField.html) and the [`Upload` api](http://api.silverstripe.org/master/class-Upload.html) for more info.
## Bulk Editing
To get a quick edit shortcut to all the newly upload files, please also add the `GridFieldBulkManager` component to your `GridFieldConfig`.
To get a quick edit shortcut to all the newly upload files, please also add the `Colymba\BulkManager\BulkManager` component to your `GridFieldConfig`.

View File

@ -1,4 +1,10 @@
<?php
namespace Colymba\BulkUpload;
use SilverStripe\Control\Controller;
use SilverStripe\Forms\UploadField;
/**
* Custom UploadField used to override Link()
* and redirect UploadField action properly through the GridField.
@ -7,13 +13,25 @@
*/
class BulkUploadField extends UploadField
{
/**
* @var GridField
*/
protected $gridfield;
public function __construct($gridfield, $parent, $folderName = null) {
/**
* @param GridField $gridfield
* @param string $parent
* @param string $folderName
*/
public function __construct($gridfield, $parent, $folderName = null)
{
$this->gridfield = $gridfield;
parent::__construct($parent, $folderName);
}
/**
* {@inheritDoc}
*/
public function Link($action = null)
{
return Controller::join_links($this->gridfield->Link(), 'bulkupload/', $action);

View File

@ -1,10 +1,24 @@
<?php
namespace Colymba\BulkUpload;
use Colymba\BulkUpload\BulkUploaderRequest;
use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\Deprecation;
use SilverStripe\Forms\FormAction;
use SilverStripe\Forms\GridField\GridField_HTMLProvider;
use SilverStripe\Forms\GridField\GridField_URLHandler;
use SilverStripe\ORM\DataModel;
use SilverStripe\ORM\DataObject;
use SilverStripe\View\ArrayData;
use SilverStripe\View\Requirements;
/**
* GridField component for uploading images in bulk.
*
* @author colymba
*/
class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandler
class BulkUploader implements GridField_HTMLProvider, GridField_URLHandler
{
/**
* Component configuration.
@ -15,8 +29,8 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
* @var array
*/
protected $config = array(
'fileRelationName' => null,
'recordClassName' => null
'fileRelationName' => null,
'recordClassName' => null
);
/**
@ -24,7 +38,7 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
* These options are passed on directly to the UploadField
* via {@link UploadField::setConfig()} api.
*
* Defaults are: *
* Defaults are:
* 'sequentialUploads' => false : process uploads 1 after the other rather than all at once
* 'canAttachExisting' => true : displays "From files" button in the UploadField
* 'canPreviewFolder' => true : displays the upload location in the UploadField
@ -33,8 +47,8 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
*/
protected $ufConfig = array(
'sequentialUploads' => false,
'canAttachExisting' => true,
'canPreviewFolder' => true,
'canAttachExisting' => true,
'canPreviewFolder' => true,
);
/**
@ -47,7 +61,7 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
* @var array
*/
protected $ufSetup = array(
'setFolderName' => 'bulkUpload',
'setFolderName' => 'bulkUpload',
);
/**
@ -60,13 +74,14 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
* @var array
*/
protected $ufValidatorSetup = array(
'setAllowedMaxFileSize' => null,
'setAllowedMaxFileSize' => null,
);
/**
* Component constructor.
*
* @param string $fileRelationName
* @param string $recordClassName
*/
public function __construct($fileRelationName = null, $recordClassName = null)
{
@ -92,7 +107,7 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
public function setConfig($reference, $value)
{
if (in_array($reference, array('folderName', 'maxFileSize', 'sequentialUploads', 'canAttachExisting', 'canPreviewFolder'))) {
Deprecation::notice('2.1.0', "GridFieldBulkUpload 'setConfig()' doesn't support '$reference' anymore. Please use 'setUfConfig()', 'setUfSetup()' or 'setUfValidatorSetup()' instead.");
Deprecation::notice('2.1.0', "BulkUploader 'setConfig()' doesn't support '$reference' anymore. Please use 'setUfConfig()', 'setUfSetup()' or 'setUfValidatorSetup()' instead.");
if ($reference === 'folderName') {
$this->setUfSetup('setFolderName', $value);
@ -228,6 +243,7 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
* Get the first has_one Image/File relation from the GridField managed DataObject
* i.e. 'MyImage' => 'Image' will return 'MyImage'.
*
* @param GridField $gridField
* @return string Name of the $has_one relation
*/
public function getDefaultFileRelationName($gridField)
@ -237,7 +253,10 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
$imageField = null;
foreach ($hasOneFields as $field => $type) {
if ($type === 'Image' || $type === 'File' || is_subclass_of($type, 'File')) {
if ($type === 'SilverStripe\\Assets\\Image'
|| $type === 'SilverStripe\\Assets\\File'
|| is_subclass_of($type, 'SilverStripe\\Assets\\File')
) {
$imageField = $field;
break;
}
@ -250,6 +269,7 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
* Returns the name of the Image/File field name from the managed record
* Either as set in the component config or the default one.
*
* @param GridField $gridField
* @return string
*/
public function getFileRelationName($gridField)
@ -264,6 +284,7 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
* i.e. 'MyImage' => 'Image' will return 'Image'
* i.e. 'MyImage' => 'File' will return 'File'.
*
* @param GridField $gridField
* @return string file relation className
*/
public function getFileRelationClassName($gridField)
@ -275,7 +296,7 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
if ($fieldName) {
return $hasOneFields[$fieldName];
} else {
return 'File';
return 'SilverStripe\\Assets\\File';
}
}
@ -299,7 +320,7 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
->setRecord(DataObject::create()) // avoid UploadField to get auto-config from the Page (e.g fix allowedMaxFileNumber)
->setTemplate('GridFieldBulkUploadField')
->setTemplate('BulkUploadField')
->setDownloadTemplateName('colymba-bulkuploaddownloadtemplate')
->setConfig('url', $gridField->Link('bulkupload/upload'))
@ -308,20 +329,20 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
->setConfig('urlFileExists', $gridField->Link('bulkupload/fileexists'))
;
//set UploadField config
foreach ($this->ufConfig as $key => $val) {
$uploadField->setConfig($key, $val);
}
//set UploadField config
foreach ($this->ufConfig as $key => $val) {
$uploadField->setConfig($key, $val);
}
//UploadField setup
foreach ($this->ufSetup as $fn => $param) {
$uploadField->{$fn}($param);
}
//UploadField setup
foreach ($this->ufSetup as $fn => $param) {
$uploadField->{$fn}($param);
}
//UploadField Validator setup
foreach ($this->ufValidatorSetup as $fn => $param) {
$uploadField->getValidator()->{$fn}($param);
}
//UploadField Validator setup
foreach ($this->ufValidatorSetup as $fn => $param) {
$uploadField->getValidator()->{$fn}($param);
}
return $uploadField;
}
@ -345,7 +366,7 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
}
// check BulkManager exists
$bulkManager = $gridField->getConfig()->getComponentsByType('GridFieldBulkManager');
$bulkManager = $gridField->getConfig()->getComponentsByType('Colymba\\BulkManager\\BulkManager');
// upload management buttons
$finishButton = FormAction::create('Finish', _t('GRIDFIELD_BULK_UPLOAD.FINISH_BTN_LABEL', 'Finish'))
@ -369,10 +390,10 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
$bulkManager_actions = $bulkManager_config['actions'];
if (array_key_exists('bulkedit', $bulkManager_actions)) {
$editAllButton = FormAction::create('EditAll', _t('GRIDFIELD_BULK_UPLOAD.EDIT_ALL_BTN_LABEL', 'Edit all'))
->addExtraClass('bulkUploadEditButton')
->setAttribute('data-icon', 'pencil')
->setAttribute('data-url', $gridField->Link('bulkupload/edit'))
->setUseButtonTag(true);
->addExtraClass('bulkUploadEditButton')
->setAttribute('data-icon', 'pencil')
->setAttribute('data-url', $gridField->Link('bulkupload/edit'))
->setUseButtonTag(true);
} else {
$editAllButton = '';
}
@ -389,17 +410,17 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
$uploadField->ClearErrorButton = $clearErrorButton;
$data = ArrayData::create(array(
'Colspan' => count($gridField->getColumns()),
'UploadField' => $uploadField->Field(), // call ->Field() to get requirements in right order
'Colspan' => count($gridField->getColumns()),
'UploadField' => $uploadField->Field(), // call ->Field() to get requirements in right order
));
Requirements::css(BULKEDITTOOLS_UPLOAD_PATH.'/css/GridFieldBulkUpload.css');
Requirements::javascript(BULKEDITTOOLS_UPLOAD_PATH.'/javascript/GridFieldBulkUpload.js');
Requirements::javascript(BULKEDITTOOLS_UPLOAD_PATH.'/javascript/GridFieldBulkUpload_downloadtemplate.js');
Requirements::add_i18n_javascript(BULKEDITTOOLS_PATH.'/lang/js');
Requirements::css(BULKEDITTOOLS_UPLOAD_PATH . '/css/GridFieldBulkUpload.css');
Requirements::javascript(BULKEDITTOOLS_UPLOAD_PATH . '/javascript/GridFieldBulkUpload.js');
Requirements::javascript(BULKEDITTOOLS_UPLOAD_PATH . '/javascript/GridFieldBulkUpload_downloadtemplate.js');
Requirements::add_i18n_javascript(BULKEDITTOOLS_PATH . '/lang/js');
return array(
'header' => $data->renderWith('GridFieldBulkUpload'),
'header' => $data->renderWith('Colymba\\BulkUpload\\BulkUploader'),
);
}
@ -425,14 +446,14 @@ class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandle
* Pass control over to the RequestHandler.
*
* @param GridField $gridField
* @param SS_HTTPRequest $request
* @param HTTPRequest $request
*
* @return mixed
*/
public function handleBulkUpload($gridField, $request)
{
$controller = $gridField->getForm()->Controller();
$handler = new GridFieldBulkUpload_Request($gridField, $this, $controller);
$controller = $gridField->getForm()->getController();
$handler = new BulkUploaderRequest($gridField, $this, $controller);
return $handler->handleRequest($request, DataModel::inst());
}

View File

@ -1,10 +1,23 @@
<?php
namespace Colymba\BulkUpload;
use SilverStripe\Assets\Image;
use SilverStripe\Core\Convert;
use SilverStripe\Core\Object;
use SilverStripe\Control\Controller;
use SilverStripe\Control\RequestHandler;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Dev\Deprecation;
use SilverStripe\ORM\DataObject;
/**
* Handles request from the GridFieldBulkUpload component.
*
* @author colymba
*/
class GridFieldBulkUpload_Request extends RequestHandler
class BulkUploaderRequest extends RequestHandler
{
/**
* Gridfield instance.
@ -16,7 +29,7 @@ class GridFieldBulkUpload_Request extends RequestHandler
/**
* Bulk upload component.
*
* @var GridFieldBulkUpload
* @var BulkUploader
*/
protected $component;
@ -48,7 +61,7 @@ class GridFieldBulkUpload_Request extends RequestHandler
/**
* Handler's constructor.
*
* @param GridFIeld $gridField
* @param GridField $gridField
* @param GridField_URLHandler $component
* @param Controller $controller
*/
@ -76,11 +89,11 @@ class GridFieldBulkUpload_Request extends RequestHandler
* adds record to GrifField relation list
* and return image/file data and record edit form.
*
* @param SS_HTTPRequest $request
* @param HTTPRequest $request
*
* @return string json
*/
public function upload(SS_HTTPRequest $request)
public function upload(HTTPRequest $request)
{
//create record
$recordClass = $this->component->getRecordClassName($this->gridField);
@ -114,7 +127,7 @@ class GridFieldBulkUpload_Request extends RequestHandler
// JS Template Data
$responseData = $this->newRecordJSTemplateData($record, $uploadResponse);
$response = new SS_HTTPResponse(Convert::raw2json(array($responseData)));
$response = new HTTPResponse(Convert::raw2json(array($responseData)));
$this->contentTypeNegotiation($response);
return $response;
@ -133,16 +146,19 @@ class GridFieldBulkUpload_Request extends RequestHandler
{
// fetch uploadedFile record and sort out previewURL
// update $uploadResponse datas in case changes happened onAfterWrite()
$uploadedFile = DataObject::get_by_id($this->component->getFileRelationClassName($this->gridField), $uploadResponse['id']);
$uploadedFile = DataObject::get_by_id(
$this->component->getFileRelationClassName($this->gridField),
$uploadResponse['id']
);
if ($uploadedFile) {
$uploadResponse['name'] = $uploadedFile->Name;
$uploadResponse['url'] = $uploadedFile->getURL();
if ($uploadedFile instanceof Image) {
$uploadResponse['thumbnail_url'] = $uploadedFile->CroppedImage(30, 30)->getURL();
$uploadResponse['thumbnail_url'] = $uploadedFile->Fill(30, 30)->getURL();
} else {
$uploadResponse['thumbnail_url'] = $uploadedFile->Icon();
$uploadResponse['thumbnail_url'] = $uploadedFile->IconTag();
}
// check if our new record has a Title, if not create one automatically
@ -173,8 +189,10 @@ class GridFieldBulkUpload_Request extends RequestHandler
* Used by select dialog.
*
* @link UploadField->getRelationAutosetClass()
* @param string $default
* @return string
*/
public function getRelationAutosetClass($default = 'File')
public function getRelationAutosetClass($default = 'SilverStripe\\Assets\\File')
{
$uploadField = $this->getUploadField();
@ -186,6 +204,7 @@ class GridFieldBulkUpload_Request extends RequestHandler
* Used by select dialog.
*
* @link UploadField->getAllowedMaxFileNumber()
* @return int|null
*/
public function getAllowedMaxFileNumber()
{
@ -198,11 +217,11 @@ class GridFieldBulkUpload_Request extends RequestHandler
* Retrieve Files to be attached
* and generated DataObjects for each one.
*
* @param SS_HTTPRequest $request
* @param HTTPRequest $request
*
* @return SS_HTTPResponse
* @return HTTPResponse
*/
public function attach(SS_HTTPRequest $request)
public function attach(HTTPRequest $request)
{
$uploadField = $this->getUploadField();
$attachResponses = $uploadField->attach($request);
@ -231,7 +250,7 @@ class GridFieldBulkUpload_Request extends RequestHandler
array_push($return, $responseData);
}
$response = new SS_HTTPResponse(Convert::raw2json($return));
$response = new HTTPResponse(Convert::raw2json($return));
$this->contentTypeNegotiation($response);
return $response;
@ -242,7 +261,7 @@ class GridFieldBulkUpload_Request extends RequestHandler
*
* @link UploadField->select()
*/
public function select(SS_HTTPRequest $request)
public function select(HTTPRequest $request)
{
$uploadField = $this->getUploadField();
@ -256,7 +275,7 @@ class GridFieldBulkUpload_Request extends RequestHandler
*
* @link UploadField->fileexists()
*/
public function fileexists(SS_HTTPRequest $request)
public function fileexists(HTTPRequest $request)
{
$uploadField = $this->getUploadField();
@ -278,11 +297,15 @@ class GridFieldBulkUpload_Request extends RequestHandler
* e.g. IE needs text/plain for iframe transport
* https://github.com/blueimp/jQuery-File-Upload/issues/1795.
*
* @param SS_HTTPResponse $response HTTP Response to set content-type on
* @param HTTPResponse $response HTTP Response to set content-type on
*/
protected function contentTypeNegotiation(&$response)
{
if (isset($_SERVER['HTTP_ACCEPT']) && ((strpos($_SERVER['HTTP_ACCEPT'], 'application/json') !== false) || $_SERVER['HTTP_ACCEPT'] === '*/*')) {
if (isset($_SERVER['HTTP_ACCEPT'])
&& ((strpos($_SERVER['HTTP_ACCEPT'], 'application/json') !== false)
|| $_SERVER['HTTP_ACCEPT'] === '*/*'
)
) {
$response->addHeader('Content-Type', 'application/json');
} else {
$response->addHeader('Content-Type', 'text/plain');

View File

@ -1,24 +1,33 @@
<?php
namespace Colymba\BulkUpload;
use Colymba\BulkUpload\BulkUploader;
use SilverStripe\Dev\Deprecation;
/**
* Legacy GridFieldBulkImageUpload component.
*
* @deprecated 2.0 "GridFieldBulkImageUpload" is deprecated, use {@link GridFieldBulkUpload} class instead.
* @deprecated 2.0 "GridFieldBulkImageUpload" is deprecated, use {@link BulkUploader} class instead.
*
* @author colymba
*/
class GridFieldBulkImageUpload extends GridFieldBulkUpload
class GridFieldBulkImageUpload extends BulkUploader
{
/**
* Component constructor.
*
* @deprecated 2.0 "GridFieldBulkImageUpload" is deprecated, use {@link GridFieldBulkUpload} class instead.
*
* @param string $fileRelationName
*/
public function __construct($fileRelationName = null)
{
Deprecation::notice('2.0', '"GridFieldBulkImageUpload" is deprecated, use "GridFieldBulkUpload" class instead.');
* Component constructor.
*
* @deprecated 2.0 "GridFieldBulkImageUpload" is deprecated, use {@link BulkUploader} class instead.
*
* @param string $fileRelationName
*/
public function __construct($fileRelationName = null)
{
Deprecation::notice(
'2.0',
'"GridFieldBulkImageUpload" is deprecated, use "BulkUploader" class instead.'
);
return new GridFieldBulkUpload($fileRelationName);
}
return new BulkUploader($fileRelationName);
}
}

View File

@ -1,22 +1,32 @@
{
"name": "colymba/gridfield-bulk-editing-tools",
"type": "silverstripe-module",
"description": "SilverStripe 3 GridField component to upload images and edit records in bulk",
"homepage": "https://github.com/colymba/GridFieldBulkEditingTools",
"keywords": ["silverstripe", "bulk upload", "image upload", "gridfield bulk upload"],
"license": "BSD Simplified",
"authors": [{
"name": "Thierry Francois",
"homepage": "http://colymba.com"
},{
"name": "GitHub contributors",
"homepage": "https://github.com/colymba/GridFieldBulkEditingTools/contributors"
}],
"repositories": [{
"type": "vcs",
"url": "git@github.com:colymba/GridFieldBulkEditingTools.git"
}],
"require": {
"silverstripe/framework": ">=3.1"
}
"name": "colymba/gridfield-bulk-editing-tools",
"type": "silverstripe-module",
"description": "SilverStripe 3 GridField component to upload images and edit records in bulk",
"homepage": "https://github.com/colymba/GridFieldBulkEditingTools",
"keywords": ["silverstripe", "bulk upload", "image upload", "gridfield bulk upload"],
"license": "BSD Simplified",
"authors": [
{
"name": "Thierry Francois",
"homepage": "http://colymba.com"
},
{
"name": "GitHub contributors",
"homepage": "https://github.com/colymba/GridFieldBulkEditingTools/contributors"
}
],
"require": {
"silverstripe/framework": "^4.0@dev"
},
"extra": {
"branch-alias": {
"dev-master": "3.x-dev"
}
},
"autoload": {
"psr-4": {
"Colymba\\BulkManager\\": "bulkManager/code/",
"Colymba\\BulkUpload\\": "bulkUpload/code/"
}
}
}