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) [![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) [![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) ![preview](screenshots/preview.png)
@ -16,37 +16,47 @@ Set of SilverStripe 3 GridField components to facilitate bulk file upload & reco
[More screenshots here.](screenshots) [More screenshots here.](screenshots)
## Requirements ## 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)) * Silverstripe 3.0 (version [0.5](https://github.com/colymba/GridFieldBulkEditingTools/tree/0.5))
## Installation ## Installation
### Composer ### Composer
* `composer require "colymba/gridfield-bulk-editing-tools:*"` * `composer require colymba/gridfield-bulk-editing-tools`
### Manual ### Manual
* Download and copy module in SilverStripe root directory * 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 ## 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`. * The `GridFieldBulkImageUpload` has been renamed to `GridFieldBulkUpload`.
* `onBulkImageUpload` callback has been renamed to `onBulkUpload` * `onBulkImageUpload` callback has been renamed to `onBulkUpload`
## Bulk Upload ## Bulk Upload
Upload multiple images or files at once into DataObjects. Perfect for galleries and the like. 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. See [BULK_UPLOAD.md](bulkUpload/BULK_UPLOAD.md) for detailed configuration.
## Bulk Manager ## Bulk Manager
Perform actions on multiple records straight from the GridField 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. See [BULK_MANAGER.md](bulkManager/BULK_MANAGER.md) for detailed configuration.
## Interdependencies ## 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 #### @TODO
* Add individual actions for each upload (update + cancel) * 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 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. * 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. * 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. 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')) { if (!defined('BULKEDITTOOLS_PATH')) {
$folder = rtrim(basename(dirname(__FILE__))); $folder = rtrim(basename(dirname(__FILE__)));
define('BULKEDITTOOLS_PATH', $folder); define('BULKEDITTOOLS_PATH', $folder);
define('BULKEDITTOOLS_UPLOAD_PATH', $folder.'/bulkUpload'); define('BULKEDITTOOLS_UPLOAD_PATH', $folder . '/bulkUpload');
define('BULKEDITTOOLS_MANAGER_PATH', $folder.'/bulkManager'); 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 ## Usage
Simply add component to your `GridFieldConfig` Simply add component to your `GridFieldConfig`
$config->addComponent(new GridFieldBulkManager()); ```php
$config->addComponent(new \Colymba\BulkManager\BulkManager());
```
## Configuration ## Configuration
The component's options can be configurated individually or in bulk through the 'config' functions like this: 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 ### $config overview
The available configuration options are: The available configuration options are:
* 'editableFields' : array of string referencing specific CMS fields available for editing * '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 ### Adding a custom action
To add a custom bulk action to the list use: 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: You can omit the handler's class name and the front-end config array, those will default to:
* `'GridFieldBulkAction'.ucfirst($name).'Handler'` * `'GridFieldBulkAction'.ucfirst($name).'Handler'`
* `$config = array( 'isAjax' => true, 'icon' => 'accept', 'isDestructive' => false )` * `$config = array( 'isAjax' => true, 'icon' => 'accept', 'isDestructive' => false )`
#### Custom action handler #### 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 #### Front-end config
The last `addBulkAction()` parameter lets you pass an array with configuration options for the UI/UX: 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 * `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) * `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 <?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. * Bulk action handler for deleting records.
* *
* @author colymba * @author colymba
*/ */
class GridFieldBulkActionDeleteHandler extends GridFieldBulkActionHandler class DeleteHandler extends Handler
{ {
/** /**
* RequestHandler allowed actions. * RequestHandler allowed actions.
@ -24,12 +32,12 @@ class GridFieldBulkActionDeleteHandler extends GridFieldBulkActionHandler
/** /**
* Delete the selected records passed from the delete bulk action. * 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(); $ids = array();
@ -38,7 +46,7 @@ class GridFieldBulkActionDeleteHandler extends GridFieldBulkActionHandler
$record->delete(); $record->delete();
} }
$response = new SS_HTTPResponse(Convert::raw2json(array( $response = new HTTPResponse(Convert::raw2json(array(
'done' => true, 'done' => true,
'records' => $ids, 'records' => $ids,
))); )));

View File

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

View File

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

View File

@ -1,10 +1,18 @@
<?php <?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. * Bulk action handler for unlinking records.
* *
* @author colymba * @author colymba
*/ */
class GridFieldBulkActionUnlinkHandler extends GridFieldBulkActionHandler class UnlinkHandler extends Handler
{ {
/** /**
* RequestHandler allowed actions. * RequestHandler allowed actions.
@ -24,17 +32,17 @@ class GridFieldBulkActionUnlinkHandler extends GridFieldBulkActionHandler
/** /**
* Unlink the selected records passed from the unlink bulk action. * 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(); $ids = $this->getRecordIDList();
$this->gridField->list->removeMany($ids); $this->gridField->list->removeMany($ids);
$response = new SS_HTTPResponse(Convert::raw2json(array( $response = new HTTPResponse(Convert::raw2json(array(
'done' => true, 'done' => true,
'records' => $ids, 'records' => $ids,
))); )));

View File

@ -1,27 +1,42 @@
<?php <?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. * GridField component for editing attached models in bulk.
* *
* @author colymba * @author colymba
*/ */
class GridFieldBulkManager implements GridField_HTMLProvider, GridField_ColumnProvider, GridField_URLHandler class BulkManager implements GridField_HTMLProvider, GridField_ColumnProvider, GridField_URLHandler
{ {
/** /**
* component configuration. * component configuration.
* *
* 'editableFields' => fields editable on the Model * 'editableFields' => fields editable on the Model
* 'actions' => maps of action name and configuration * 'actions' => maps of action name and configuration
* *
* @var array * @var array
*/ */
protected $config = array( protected $config = array(
'editableFields' => null, 'editableFields' => null,
'actions' => array(), 'actions' => array(),
); );
/** /**
* GridFieldBulkManager component constructor. * GridFieldBulkManager component constructor.
* *
* @param array $editableFields List of editable fields * @param array $editableFields List of editable fields
* @param bool $defaultActions Use default actions list. False to start fresh. * @param bool $defaultActions Use default actions list. False to start fresh.
*/ */
@ -33,33 +48,33 @@ class GridFieldBulkManager implements GridField_HTMLProvider, GridField_ColumnPr
if ($defaultActions) { if ($defaultActions) {
$this->config['actions'] = array( $this->config['actions'] = array(
'bulkEdit' => array( 'bulkEdit' => array(
'label' => _t('GRIDFIELD_BULK_MANAGER.EDIT_SELECT_LABEL', 'Edit'), 'label' => _t('GRIDFIELD_BULK_MANAGER.EDIT_SELECT_LABEL', 'Edit'),
'handler' => 'GridFieldBulkActionEditHandler', 'handler' => 'Colymba\\BulkManager\\BulkAction\\EditHandler',
'config' => array( 'config' => array(
'isAjax' => false, 'isAjax' => false,
'icon' => 'pencil', 'icon' => 'pencil',
'isDestructive' => false, 'isDestructive' => false,
), ),
), ),
'unLink' => array( 'unLink' => array(
'label' => _t('GRIDFIELD_BULK_MANAGER.UNLINK_SELECT_LABEL', 'UnLink'), 'label' => _t('GRIDFIELD_BULK_MANAGER.UNLINK_SELECT_LABEL', 'UnLink'),
'handler' => 'GridFieldBulkActionUnlinkHandler', 'handler' => 'Colymba\\BulkManager\\BulkAction\\UnlinkHandler',
'config' => array( 'config' => array(
'isAjax' => true, 'isAjax' => true,
'icon' => 'chain--minus', 'icon' => 'chain--minus',
'isDestructive' => false, 'isDestructive' => false,
), ),
), ),
'delete' => array( 'delete' => array(
'label' => _t('GRIDFIELD_BULK_MANAGER.DELETE_SELECT_LABEL', 'Delete'), 'label' => _t('GRIDFIELD_BULK_MANAGER.DELETE_SELECT_LABEL', 'Delete'),
'handler' => 'GridFieldBulkActionDeleteHandler', 'handler' => 'Colymba\\BulkManager\\BulkAction\\DeleteHandler',
'config' => array( 'config' => array(
'isAjax' => true, 'isAjax' => true,
'icon' => 'decline', 'icon' => 'decline',
'isDestructive' => true, 'isDestructive' => true,
), ),
), )
); );
} }
} }
@ -70,7 +85,7 @@ class GridFieldBulkManager implements GridField_HTMLProvider, GridField_ColumnPr
/** /**
* Sets the component configuration parameter. * Sets the component configuration parameter.
* *
* @param string $reference * @param string $reference
* @param mixed $value * @param mixed $value
*/ */
@ -95,7 +110,7 @@ class GridFieldBulkManager implements GridField_HTMLProvider, GridField_ColumnPr
/** /**
* Returns one $config parameter of the full $config. * Returns one $config parameter of the full $config.
* *
* @param string $reference $congif parameter to return * @param string $reference $congif parameter to return
* *
* @return mixed * @return mixed
@ -113,7 +128,7 @@ class GridFieldBulkManager implements GridField_HTMLProvider, GridField_ColumnPr
* Lets you add custom bulk actions to the bulk manager interface. * Lets you add custom bulk actions to the bulk manager interface.
* *
* @todo add config options for front-end: isAjax, icon * @todo add config options for front-end: isAjax, icon
* *
* @param string $name Bulk action's name. Used by RequestHandler. * @param string $name Bulk action's name. Used by RequestHandler.
* @param string $label Dropdown menu action's label. Default to ucfirst($name). * @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' * @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) { if (!$handler) {
$handler = 'GridFieldBulkAction'.ucfirst($name).'Handler'; $handler = 'Colymba\\BulkManager\\BulkAction\\' . ucfirst($name) . 'Handler';
} }
if (!ClassInfo::exists($handler)) { if (!ClassInfo::exists($handler)) {
@ -150,17 +165,17 @@ class GridFieldBulkManager implements GridField_HTMLProvider, GridField_ColumnPr
} }
$this->config['actions'][$name] = array( $this->config['actions'][$name] = array(
'label' => $label, 'label' => $label,
'handler' => $handler, 'handler' => $handler,
'config' => $config, 'config' => $config,
); );
return $this; return $this;
} }
/** /**
* Removes a bulk actions from the bulk manager interface. * Removes a bulk actions from the bulk manager interface.
* *
* @param string $name Bulk action's name * @param string $name Bulk action's name
* *
* @return GridFieldBulkManager Current GridFieldBulkManager instance * @return GridFieldBulkManager Current GridFieldBulkManager instance
@ -182,7 +197,7 @@ class GridFieldBulkManager implements GridField_HTMLProvider, GridField_ColumnPr
/** /**
* Add bulk select column. * Add bulk select column.
* *
* @param GridField $gridField Current GridField instance * @param GridField $gridField Current GridField instance
* @param array $columns Columns list * @param array $columns Columns list
*/ */
@ -195,7 +210,7 @@ class GridFieldBulkManager implements GridField_HTMLProvider, GridField_ColumnPr
/** /**
* Which columns are handled by the component. * Which columns are handled by the component.
* *
* @param GridField $gridField Current GridField instance * @param GridField $gridField Current GridField instance
* *
* @return array List of handled column names * @return array List of handled column names
@ -207,7 +222,7 @@ class GridFieldBulkManager implements GridField_HTMLProvider, GridField_ColumnPr
/** /**
* Sets the column's content. * Sets the column's content.
* *
* @param GridField $gridField Current GridField instance * @param GridField $gridField Current GridField instance
* @param DataObject $record Record intance for this row * @param DataObject $record Record intance for this row
* @param string $columnName Column's name for which we need content * @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) public function getColumnContent($gridField, $record, $columnName)
{ {
$cb = CheckboxField::create('bulkSelect_'.$record->ID) $cb = CheckboxField::create('bulkSelect_' . $record->ID)
->addExtraClass('bulkSelect no-change-track') ->addExtraClass('bulkSelect no-change-track')
->setAttribute('data-record', $record->ID); ->setAttribute('data-record', $record->ID);
return $cb->Field(); return $cb->Field();
} }
/** /**
* Set the column's HTML attributes. * Set the column's HTML attributes.
* *
* @param GridField $gridField Current GridField instance * @param GridField $gridField Current GridField instance
* @param DataObject $record Record intance for this row * @param DataObject $record Record intance for this row
* @param string $columnName Column's name for which we need attributes * @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. * Set the column's meta data.
* *
* @param GridField $gridField Current GridField instance * @param GridField $gridField Current GridField instance
* @param string $columnName Column's name for which we need meta data * @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) public function getHTMLFragments($gridField)
{ {
Requirements::css(BULKEDITTOOLS_MANAGER_PATH.'/css/GridFieldBulkManager.css'); Requirements::css(BULKEDITTOOLS_MANAGER_PATH . '/css/GridFieldBulkManager.css');
Requirements::javascript(BULKEDITTOOLS_MANAGER_PATH.'/javascript/GridFieldBulkManager.js'); Requirements::javascript(BULKEDITTOOLS_MANAGER_PATH . '/javascript/GridFieldBulkManager.js');
Requirements::add_i18n_javascript(BULKEDITTOOLS_PATH.'/lang/js'); Requirements::add_i18n_javascript(BULKEDITTOOLS_PATH . '/lang/js');
if (!count($this->config['actions'])) { if (!count($this->config['actions'])) {
user_error('Trying to use GridFieldBulkManager without any bulk action.', E_USER_ERROR); 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', '') $dropDownActionsList = DropdownField::create('bulkActionName', '')
->setSource($actionsListSource) ->setSource($actionsListSource)
->setAttribute('class', 'bulkActionName no-change-track') ->addExtraClass('bulkActionName no-change-track no-chosen')
->setAttribute('id', ''); ->setAttribute('id', '');
$templateData = array( $templateData = array(
'Menu' => $dropDownActionsList->FieldHolder(), 'Menu' => $dropDownActionsList,
'Button' => array( 'Button' => array(
'Label' => _t('GRIDFIELD_BULK_MANAGER.ACTION_BTN_LABEL', 'Go'), 'Label' => _t('GRIDFIELD_BULK_MANAGER.ACTION_BTN_LABEL', 'Go'),
'DataURL' => $gridField->Link('bulkAction'), 'DataURL' => $gridField->Link('bulkAction'),
'Icon' => $this->config['actions'][$firstAction]['config']['icon'], 'Icon' => $this->config['actions'][$firstAction]['config']['icon'],
'DataConfig' => htmlspecialchars(json_encode($actionsConfig), ENT_QUOTES, 'UTF-8'), 'DataConfig' => json_encode($actionsConfig),
), ),
'Select' => array( 'Select' => array(
'Label' => _t('GRIDFIELD_BULK_MANAGER.SELECT_ALL_LABEL', 'Select all'), 'Label' => _t('GRIDFIELD_BULK_MANAGER.SELECT_ALL_LABEL', 'Select all'),
), ),
'Colspan' => (count($gridField->getColumns()) - 1), 'Colspan' => (count($gridField->getColumns()) - 1),
); );
$templateData = new ArrayData($templateData); $templateData = new ArrayData($templateData);
@ -314,7 +329,7 @@ class GridFieldBulkManager implements GridField_HTMLProvider, GridField_ColumnPr
/** /**
* Returns an action => handler list. * Returns an action => handler list.
* *
* @param GridField $gridField * @param GridField $gridField
* *
* @return array * @return array
@ -322,8 +337,8 @@ class GridFieldBulkManager implements GridField_HTMLProvider, GridField_ColumnPr
public function getURLHandlers($gridField) public function getURLHandlers($gridField)
{ {
return array( 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' * $url_handlers rule should not use wildcards like '$Action' => '$Action'
* but have more specific path defined * but have more specific path defined
* *
* @param GridField $gridField * @param GridField $gridField
* @param SS_HTTPRequest $request * @param HTTPRequest $request
* *
* @return mixed * @return mixed
*/ */
public function handleBulkAction($gridField, $request) public function handleBulkAction($gridField, $request)
{ {
$controller = $gridField->getForm()->Controller(); $controller = $gridField->getForm()->getController();
foreach ($this->config['actions'] as $name => $data) { foreach ($this->config['actions'] as $name => $data) {
$handlerClass = $data['handler']; $handlerClass = $data['handler'];
@ -351,7 +366,7 @@ class GridFieldBulkManager implements GridField_HTMLProvider, GridField_ColumnPr
foreach ($urlHandlers as $rule => $action) { foreach ($urlHandlers as $rule => $action) {
if ($request->match($rule, false)) { if ($request->match($rule, false)) {
//print_r('matched ' . $handlerClass . ' to ' . $rule); //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()); 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; } text-shadow: none; }
.bulkEditingFieldHolder.updated .ui-accordion-header { .bulkEditingFieldHolder.updated .ui-accordion-header {
background-color: #a4ca3a; background-color: #a4ca3a;
background-image: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #a4ca3a), color-stop(100%, #59781d)); 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: -webkit-linear-gradient(top, #a4ca3a 0%, #59781D 100%);
background-image: -moz-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: -o-linear-gradient(top, #a4ca3a 0%, #59781D 100%);
background-image: -ms-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: linear-gradient(top, #a4ca3a 0%, #59781D 100%); }
.bulkEditingFieldHolder.updated .ui-accordion-header a { .bulkEditingFieldHolder.updated .ui-accordion-header a {
color: #fff; color: #fff;
text-shadow: none; } 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; } 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; } margin-left: 0; }
.cms table.ss-gridfield-table tr.bulkManagerOptions .title { .cms table.grid-field__table tr.bulkManagerOptions .title {
margin: 0 0 2px 2px; } margin: 0 0 2px 2px; }
.cms table.ss-gridfield-table tr.bulkManagerOptions .dropdown { .cms table.grid-field__table tr.bulkManagerOptions .dropdown {
display: inline-block; display: inline-block;
border: none; border: none;
box-shadow: none; box-shadow: none;
@ -12,17 +14,17 @@
margin: 0; margin: 0;
color: #000; color: #000;
vertical-align: top; } 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; } 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; } 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; } 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; width: 25px;
padding: 0 10px; padding: 0 10px;
text-align: center; } 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; background: url("../../framework/admin/images/btn-icon/document--pencil.png") no-repeat 2px 0px;
display: inline-block; display: inline-block;
width: 20px; width: 20px;
@ -32,3 +34,5 @@
vertical-align: middle; vertical-align: middle;
background-position: 2px 0px; background-position: 2px 0px;
background-repeat: no-repeat no-repeat; } 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 tr.bulkManagerOptions
{ {
th.bulkmanagerheading {
padding-left: 40px;
}
th.bulkmanagerselect th.bulkmanagerselect
{ {
text-align: center; text-align: center;
@ -17,7 +21,7 @@
margin: 0 0 2px 2px; margin: 0 0 2px 2px;
} }
.dropdown .dropdown
{ {
display: inline-block; display: inline-block;
border: none; border: none;
@ -71,4 +75,4 @@
background-repeat: no-repeat no-repeat; background-repeat: no-repeat no-repeat;
} }
} }
} }

View File

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

View File

@ -1,15 +1,25 @@
<tr class="bulkManagerOptions"> <tr class="bulkManagerOptions">
<th class="main bulkmanagerheading" colspan="$Colspan"> <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> <p class="title"><% _t('GRIDFIELD_BULK_MANAGER.COMPONENT_TITLE', 'Modify one or more entry at a time.') %></p>
</div>
$Menu <div class="row">
<a data-url="$Button.DataURL" data-config="$Button.DataConfig" class="doBulkActionButton ss-ui-button" data-icon="$Button.Icon"> <div class="form-inline">
$Button.Label $Menu
</a> <a data-url="$Button.DataURL"
data-config="$Button.DataConfig"
</th> class="doBulkActionButton btn btn-primary"
<th class="main bulkmanagerselect"> data-icon="$Button.Icon">
<input class="no-change-track bulkSelectAll" type="checkbox" title="$Select.Label" name="toggleSelectAll" /> $Button.Label
</th> </a>
</tr> <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 ## 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`. 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 ## Usage 2
You can specify which `Image` or `File` field to use and a specific `DataObject` class name to use. 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') $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.) $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 ## Configuration
### Component configuration ### Component configuration
The component's option can be configurated through the `setConfig` functions like this: 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: The available configuration options are:
* 'fileRelationName' : sets the name of the `Image` or `File` has_one field to use (i.e. 'MyImage') * '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: 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') ```php
->setUfSetup('setFolderName', 'myFolder') $config->getComponentByType('Colymba\\BulkUpload\\BulkUploader')
->setUfConfig('sequentialUploads', true); ->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. 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 ## 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 <?php
namespace Colymba\BulkUpload;
use SilverStripe\Control\Controller;
use SilverStripe\Forms\UploadField;
/** /**
* Custom UploadField used to override Link() * Custom UploadField used to override Link()
* and redirect UploadField action properly through the GridField. * and redirect UploadField action properly through the GridField.
@ -7,13 +13,25 @@
*/ */
class BulkUploadField extends UploadField class BulkUploadField extends UploadField
{ {
/**
* @var GridField
*/
protected $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; $this->gridfield = $gridfield;
parent::__construct($parent, $folderName); parent::__construct($parent, $folderName);
} }
/**
* {@inheritDoc}
*/
public function Link($action = null) public function Link($action = null)
{ {
return Controller::join_links($this->gridfield->Link(), 'bulkupload/', $action); return Controller::join_links($this->gridfield->Link(), 'bulkupload/', $action);

View File

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

View File

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

View File

@ -1,24 +1,33 @@
<?php <?php
namespace Colymba\BulkUpload;
use Colymba\BulkUpload\BulkUploader;
use SilverStripe\Dev\Deprecation;
/** /**
* Legacy GridFieldBulkImageUpload component. * 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 * @author colymba
*/ */
class GridFieldBulkImageUpload extends GridFieldBulkUpload class GridFieldBulkImageUpload extends BulkUploader
{ {
/** /**
* Component constructor. * Component constructor.
* *
* @deprecated 2.0 "GridFieldBulkImageUpload" is deprecated, use {@link GridFieldBulkUpload} class instead. * @deprecated 2.0 "GridFieldBulkImageUpload" is deprecated, use {@link BulkUploader} class instead.
* *
* @param string $fileRelationName * @param string $fileRelationName
*/ */
public function __construct($fileRelationName = null) public function __construct($fileRelationName = null)
{ {
Deprecation::notice('2.0', '"GridFieldBulkImageUpload" is deprecated, use "GridFieldBulkUpload" class instead.'); 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", "name": "colymba/gridfield-bulk-editing-tools",
"type": "silverstripe-module", "type": "silverstripe-module",
"description": "SilverStripe 3 GridField component to upload images and edit records in bulk", "description": "SilverStripe 3 GridField component to upload images and edit records in bulk",
"homepage": "https://github.com/colymba/GridFieldBulkEditingTools", "homepage": "https://github.com/colymba/GridFieldBulkEditingTools",
"keywords": ["silverstripe", "bulk upload", "image upload", "gridfield bulk upload"], "keywords": ["silverstripe", "bulk upload", "image upload", "gridfield bulk upload"],
"license": "BSD Simplified", "license": "BSD Simplified",
"authors": [{ "authors": [
"name": "Thierry Francois", {
"homepage": "http://colymba.com" "name": "Thierry Francois",
},{ "homepage": "http://colymba.com"
"name": "GitHub contributors", },
"homepage": "https://github.com/colymba/GridFieldBulkEditingTools/contributors" {
}], "name": "GitHub contributors",
"repositories": [{ "homepage": "https://github.com/colymba/GridFieldBulkEditingTools/contributors"
"type": "vcs", }
"url": "git@github.com:colymba/GridFieldBulkEditingTools.git" ],
}], "require": {
"require": { "silverstripe/framework": "^4.0@dev"
"silverstripe/framework": ">=3.1" },
} "extra": {
"branch-alias": {
"dev-master": "3.x-dev"
}
},
"autoload": {
"psr-4": {
"Colymba\\BulkManager\\": "bulkManager/code/",
"Colymba\\BulkUpload\\": "bulkUpload/code/"
}
}
} }