mirror of
https://github.com/colymba/GridFieldBulkEditingTools.git
synced 2024-10-22 11:05:57 +02:00
Converting to PSR-2
This commit is contained in:
parent
7e98d488e5
commit
bbc0215635
11
_config.php
11
_config.php
@ -1,9 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
//define global path to Components' root folder
|
//define global path to Components' root folder
|
||||||
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');
|
|
||||||
}
|
}
|
||||||
|
@ -3,48 +3,47 @@
|
|||||||
* Bulk action handler for deleting records.
|
* Bulk action handler for deleting records.
|
||||||
*
|
*
|
||||||
* @author colymba
|
* @author colymba
|
||||||
* @package GridFieldBulkEditingTools
|
|
||||||
* @subpackage BulkManager
|
|
||||||
*/
|
*/
|
||||||
class GridFieldBulkActionDeleteHandler extends GridFieldBulkActionHandler
|
class GridFieldBulkActionDeleteHandler extends GridFieldBulkActionHandler
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* RequestHandler allowed actions
|
* RequestHandler allowed actions.
|
||||||
* @var array
|
*
|
||||||
*/
|
* @var array
|
||||||
private static $allowed_actions = array('delete');
|
*/
|
||||||
|
private static $allowed_actions = array('delete');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RequestHandler url => action map.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private static $url_handlers = array(
|
||||||
|
'delete' => 'delete',
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* RequestHandler url => action map
|
* Delete the selected records passed from the delete bulk action.
|
||||||
* @var array
|
*
|
||||||
*/
|
* @param SS_HTTPRequest $request
|
||||||
private static $url_handlers = array(
|
*
|
||||||
'delete' => 'delete'
|
* @return SS_HTTPResponse List of deleted records ID
|
||||||
);
|
*/
|
||||||
|
public function delete(SS_HTTPRequest $request)
|
||||||
|
{
|
||||||
|
$ids = array();
|
||||||
|
|
||||||
|
foreach ($this->getRecords() as $record) {
|
||||||
|
array_push($ids, $record->ID);
|
||||||
|
$record->delete();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
$response = new SS_HTTPResponse(Convert::raw2json(array(
|
||||||
* Delete the selected records passed from the delete bulk action
|
'done' => true,
|
||||||
*
|
'records' => $ids,
|
||||||
* @param SS_HTTPRequest $request
|
)));
|
||||||
* @return SS_HTTPResponse List of deleted records ID
|
$response->addHeader('Content-Type', 'text/json');
|
||||||
*/
|
|
||||||
public function delete(SS_HTTPRequest $request)
|
|
||||||
{
|
|
||||||
$ids = array();
|
|
||||||
|
|
||||||
foreach ( $this->getRecords() as $record )
|
return $response;
|
||||||
{
|
}
|
||||||
array_push($ids, $record->ID);
|
|
||||||
$record->delete();
|
|
||||||
}
|
|
||||||
|
|
||||||
$response = new SS_HTTPResponse(Convert::raw2json(array(
|
|
||||||
'done' => true,
|
|
||||||
'records' => $ids
|
|
||||||
)));
|
|
||||||
$response->addHeader('Content-Type', 'text/json');
|
|
||||||
return $response;
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -3,394 +3,372 @@
|
|||||||
* Bulk action handler for editing records.
|
* Bulk action handler for editing records.
|
||||||
*
|
*
|
||||||
* @author colymba
|
* @author colymba
|
||||||
* @package GridFieldBulkEditingTools
|
|
||||||
* @subpackage BulkManager
|
|
||||||
*/
|
*/
|
||||||
class GridFieldBulkActionEditHandler extends GridFieldBulkActionHandler
|
class GridFieldBulkActionEditHandler extends GridFieldBulkActionHandler
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* RequestHandler allowed actions
|
* RequestHandler allowed actions.
|
||||||
* @var array
|
*
|
||||||
*/
|
* @var array
|
||||||
private static $allowed_actions = array(
|
*/
|
||||||
'index',
|
private static $allowed_actions = array(
|
||||||
'bulkEditForm',
|
'index',
|
||||||
'recordEditForm'
|
'bulkEditForm',
|
||||||
);
|
'recordEditForm',
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
/**
|
* RequestHandler url => action map.
|
||||||
* RequestHandler url => action map
|
*
|
||||||
* @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',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return URL to this RequestHandler.
|
||||||
|
*
|
||||||
|
* @param string $action Action to append to URL
|
||||||
|
*
|
||||||
|
* @return string URL
|
||||||
|
*/
|
||||||
|
public function Link($action = null)
|
||||||
|
{
|
||||||
|
return Controller::join_links(parent::Link(), 'bulkEdit', $action);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return URL to this RequestHandler
|
* Return a form for all the selected DataObjects
|
||||||
* @param string $action Action to append to URL
|
* with their respective editable fields.
|
||||||
* @return string URL
|
*
|
||||||
*/
|
* @return Form Selected DataObjects editable fields
|
||||||
public function Link($action = null)
|
*/
|
||||||
{
|
public function bulkEditForm()
|
||||||
return Controller::join_links(parent::Link(), 'bulkEdit', $action);
|
{
|
||||||
}
|
$crumbs = $this->Breadcrumbs();
|
||||||
|
if ($crumbs && $crumbs->count() >= 2) {
|
||||||
|
$one_level_up = $crumbs->offsetGet($crumbs->count() - 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
$actions = new FieldList();
|
||||||
|
|
||||||
/**
|
$actions->push(
|
||||||
* Return a form for all the selected DataObjects
|
FormAction::create('doSave', _t('GRIDFIELD_BULKMANAGER_EDIT_HANDLER.SAVE_BTN_LABEL', 'Save all'))
|
||||||
* with their respective editable fields.
|
->setAttribute('id', 'bulkEditingSaveBtn')
|
||||||
*
|
->addExtraClass('ss-ui-action-constructive')
|
||||||
* @return Form Selected DataObjects editable fields
|
->setAttribute('data-icon', 'accept')
|
||||||
*/
|
->setUseButtonTag(true)
|
||||||
public function bulkEditForm()
|
);
|
||||||
{
|
|
||||||
$crumbs = $this->Breadcrumbs();
|
|
||||||
if($crumbs && $crumbs->count()>=2)
|
|
||||||
{
|
|
||||||
$one_level_up = $crumbs->offsetGet($crumbs->count()-2);
|
|
||||||
}
|
|
||||||
|
|
||||||
$actions = new FieldList();
|
$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')
|
||||||
|
->setAttribute('data-icon', 'decline')
|
||||||
|
->setAttribute('href', $one_level_up->Link)
|
||||||
|
->setUseButtonTag(true)
|
||||||
|
->setAttribute('src', '')//changes type to image so isn't hooked by default actions handlers
|
||||||
|
);
|
||||||
|
|
||||||
$actions->push(
|
$recordList = $this->getRecordIDList();
|
||||||
FormAction::create('doSave', _t('GRIDFIELD_BULKMANAGER_EDIT_HANDLER.SAVE_BTN_LABEL', 'Save all'))
|
$recordsFieldList = new FieldList();
|
||||||
->setAttribute('id', 'bulkEditingSaveBtn')
|
$config = $this->component->getConfig();
|
||||||
->addExtraClass('ss-ui-action-constructive')
|
|
||||||
->setAttribute('data-icon', 'accept')
|
|
||||||
->setUseButtonTag(true)
|
|
||||||
);
|
|
||||||
|
|
||||||
$actions->push(
|
$editingCount = count($recordList);
|
||||||
FormAction::create('Cancel', _t('GRIDFIELD_BULKMANAGER_EDIT_HANDLER.CANCEL_BTN_LABEL', 'Cancel'))
|
$modelClass = $this->gridField->getModelClass();
|
||||||
->setAttribute('id', 'bulkEditingUpdateCancelBtn')
|
$singleton = singleton($modelClass);
|
||||||
->addExtraClass('ss-ui-action-destructive cms-panel-link')
|
$titleModelClass = (($editingCount > 1) ? $singleton->i18n_plural_name() : $singleton->i18n_singular_name());
|
||||||
->setAttribute('data-icon', 'decline')
|
|
||||||
->setAttribute('href', $one_level_up->Link)
|
|
||||||
->setUseButtonTag(true)
|
|
||||||
->setAttribute('src', '')//changes type to image so isn't hooked by default actions handlers
|
|
||||||
);
|
|
||||||
|
|
||||||
$recordList = $this->getRecordIDList();
|
|
||||||
$recordsFieldList = new FieldList();
|
|
||||||
$config = $this->component->getConfig();
|
|
||||||
|
|
||||||
$editingCount = count($recordList);
|
|
||||||
$modelClass = $this->gridField->getModelClass();
|
|
||||||
$singleton = singleton($modelClass);
|
|
||||||
$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('GRIDFIELD_BULKMANAGER_EDIT_HANDLER.HEADER_TEXT',
|
||||||
'Editing {count} {class}',
|
'Editing {count} {class}',
|
||||||
array(
|
array(
|
||||||
'count' => $editingCount,
|
'count' => $editingCount,
|
||||||
'class' => $titleModelClass
|
'class' => $titleModelClass,
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
$header = LiteralField::create(
|
$header = LiteralField::create(
|
||||||
'bulkEditHeader',
|
'bulkEditHeader',
|
||||||
'<h1 id="bulkEditHeader">' . $headerText . '</h1>'
|
'<h1 id="bulkEditHeader">'.$headerText.'</h1>'
|
||||||
);
|
);
|
||||||
$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
|
||||||
|
foreach ($recordList as $id) {
|
||||||
|
$record = DataObject::get_by_id($modelClass, $id);
|
||||||
|
$recordEditingFields = $this->getRecordEditingFields($record);
|
||||||
|
|
||||||
//fetch fields for each record and push to fieldList
|
$toggleField = ToggleCompositeField::create(
|
||||||
foreach ( $recordList as $id )
|
'RecordFields_'.$id,
|
||||||
{
|
$record->getTitle(),
|
||||||
$record = DataObject::get_by_id($modelClass, $id);
|
$recordEditingFields
|
||||||
$recordEditingFields = $this->getRecordEditingFields($record);
|
)
|
||||||
|
->setHeadingLevel(4)
|
||||||
|
->setAttribute('data-id', $id)
|
||||||
|
->addExtraClass('bulkEditingFieldHolder');
|
||||||
|
|
||||||
$toggleField = ToggleCompositeField::create(
|
$recordsFieldList->push($toggleField);
|
||||||
'RecordFields_'.$id,
|
}
|
||||||
$record->getTitle(),
|
|
||||||
$recordEditingFields
|
|
||||||
)
|
|
||||||
->setHeadingLevel(4)
|
|
||||||
->setAttribute('data-id', $id)
|
|
||||||
->addExtraClass('bulkEditingFieldHolder');
|
|
||||||
|
|
||||||
$recordsFieldList->push($toggleField);
|
$bulkEditForm = Form::create(
|
||||||
}
|
$this,
|
||||||
|
'recordEditForm', //recordEditForm name is here to trick SS to pass all subform request to recordEditForm()
|
||||||
|
$recordsFieldList,
|
||||||
|
$actions
|
||||||
|
);
|
||||||
|
|
||||||
$bulkEditForm = Form::create(
|
if ($crumbs && $crumbs->count() >= 2) {
|
||||||
$this,
|
$bulkEditForm->Backlink = $one_level_up->Link;
|
||||||
'recordEditForm', //recordEditForm name is here to trick SS to pass all subform request to recordEditForm()
|
}
|
||||||
$recordsFieldList,
|
|
||||||
$actions
|
|
||||||
);
|
|
||||||
|
|
||||||
if($crumbs && $crumbs->count()>=2){
|
//override form action URL back to bulkEditForm
|
||||||
$bulkEditForm->Backlink = $one_level_up->Link;
|
//and add record ids GET var
|
||||||
}
|
$bulkEditForm->setAttribute(
|
||||||
|
'action',
|
||||||
|
$this->Link('bulkEditForm?records[]='.implode('&', $recordList))
|
||||||
|
);
|
||||||
|
|
||||||
//override form action URL back to bulkEditForm
|
return $bulkEditForm;
|
||||||
//and add record ids GET var
|
}
|
||||||
$bulkEditForm->setAttribute(
|
|
||||||
'action',
|
|
||||||
$this->Link('bulkEditForm?records[]='.implode('&', $recordList))
|
|
||||||
);
|
|
||||||
|
|
||||||
return $bulkEditForm;
|
/**
|
||||||
}
|
* 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()
|
||||||
|
{
|
||||||
|
//clone current request : used to figure out what record we are asking
|
||||||
|
$request = clone $this->request;
|
||||||
|
$recordInfo = $request->shift();
|
||||||
|
|
||||||
|
//shift request till we find the requested field
|
||||||
|
while ($recordInfo) {
|
||||||
|
if ($unescapedRecordInfo = $this->unEscapeFieldName($recordInfo)) {
|
||||||
|
$id = $unescapedRecordInfo['id'];
|
||||||
|
$fieldName = $unescapedRecordInfo['name'];
|
||||||
|
|
||||||
/**
|
$action = $request->shift();
|
||||||
* Return's a form with only one record's fields
|
break;
|
||||||
* Used for bulkEditForm subForm requests via ajax
|
} else {
|
||||||
*
|
$recordInfo = $request->shift();
|
||||||
* @return Form Currently being edited form
|
}
|
||||||
*/
|
}
|
||||||
public function recordEditForm()
|
|
||||||
{
|
|
||||||
//clone current request : used to figure out what record we are asking
|
|
||||||
$request = clone $this->request;
|
|
||||||
$recordInfo = $request->shift();
|
|
||||||
|
|
||||||
//shift request till we find the requested field
|
//generate a form with only that requested record's fields
|
||||||
while ($recordInfo)
|
if ($id) {
|
||||||
{
|
$modelClass = $this->gridField->getModelClass();
|
||||||
if ( $unescapedRecordInfo = $this->unEscapeFieldName($recordInfo) )
|
$record = DataObject::get_by_id($modelClass, $id);
|
||||||
{
|
|
||||||
$id = $unescapedRecordInfo['id'];
|
|
||||||
$fieldName = $unescapedRecordInfo['name'];
|
|
||||||
|
|
||||||
$action = $request->shift();
|
$cmsFields = $record->getCMSFields();
|
||||||
break;
|
$recordEditingFields = $this->getRecordEditingFields($record);
|
||||||
}
|
|
||||||
else{
|
|
||||||
$recordInfo = $request->shift();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//generate a form with only that requested record's fields
|
return Form::create(
|
||||||
if ( $id )
|
$this->gridField,
|
||||||
{
|
'recordEditForm',
|
||||||
$modelClass = $this->gridField->getModelClass();
|
FieldList::create($recordEditingFields),
|
||||||
$record = DataObject::get_by_id($modelClass, $id);
|
FieldList::create()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$cmsFields = $record->getCMSFields();
|
/**
|
||||||
$recordEditingFields = $this->getRecordEditingFields($record);
|
* Returns a record's populated form fields
|
||||||
|
* 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
|
||||||
|
*/
|
||||||
|
private function getRecordEditingFields(DataObject $record)
|
||||||
|
{
|
||||||
|
$tempForm = Form::create(
|
||||||
|
$this, 'TempEditForm',
|
||||||
|
$record->getCMSFields(),
|
||||||
|
FieldList::create()
|
||||||
|
);
|
||||||
|
|
||||||
return Form::create(
|
$tempForm->loadDataFrom($record);
|
||||||
$this->gridField,
|
$fields = $tempForm->Fields();
|
||||||
'recordEditForm',
|
|
||||||
FieldList::create($recordEditingFields),
|
|
||||||
FieldList::create()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
$fields = $this->filterRecordEditingFields($fields, $record->ID);
|
||||||
|
|
||||||
/**
|
return $fields;
|
||||||
* Returns a record's populated form fields
|
}
|
||||||
* 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
|
|
||||||
*/
|
|
||||||
private function getRecordEditingFields(DataObject $record)
|
|
||||||
{
|
|
||||||
$tempForm = Form::create(
|
|
||||||
$this, "TempEditForm",
|
|
||||||
$record->getCMSFields(),
|
|
||||||
FieldList::create()
|
|
||||||
);
|
|
||||||
|
|
||||||
$tempForm->loadDataFrom($record);
|
/**
|
||||||
$fields = $tempForm->Fields();
|
* Filters a records editable fields
|
||||||
|
* based on component's config
|
||||||
$fields = $this->filterRecordEditingFields($fields, $record->ID);
|
* and escape each field with unique name.
|
||||||
|
*
|
||||||
return $fields;
|
* See {@link GridFieldBulkManager} component for filtering config.
|
||||||
}
|
*
|
||||||
|
* @param FieldList $fields Record's CMS Fields
|
||||||
|
* @param int $id Record's ID, used fir unique name
|
||||||
/**
|
*
|
||||||
* Filters a records editable fields
|
* @return array Filtered record's fields
|
||||||
* based on component's config
|
*/
|
||||||
* and escape each field with unique name.
|
private function filterRecordEditingFields(FieldList $fields, $id)
|
||||||
*
|
{
|
||||||
* See {@link GridFieldBulkManager} component for filtering config.
|
$config = $this->component->getConfig();
|
||||||
*
|
$editableFields = $config['editableFields'];
|
||||||
* @param FieldList $fields Record's CMS Fields
|
|
||||||
* @param integer $id Record's ID, used fir unique name
|
|
||||||
* @return array Filtered record's fields
|
|
||||||
*/
|
|
||||||
private function filterRecordEditingFields(FieldList $fields, $id)
|
|
||||||
{
|
|
||||||
$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 )
|
if ($editableFields) {
|
||||||
{
|
$dataFields = array();
|
||||||
$dataFields = array();
|
|
||||||
|
|
||||||
foreach ($editableFields as $fieldName)
|
foreach ($editableFields as $fieldName) {
|
||||||
{
|
$dataFields[$fieldName] = $fields->dataFieldByName($fieldName);
|
||||||
$dataFields[$fieldName] = $fields->dataFieldByName($fieldName);
|
}
|
||||||
}
|
} else {
|
||||||
}
|
$dataFields = $fields->dataFields();
|
||||||
else{
|
}
|
||||||
$dataFields = $fields->dataFields();
|
|
||||||
}
|
|
||||||
|
|
||||||
// escape field names with unique prefix
|
// escape field names with unique prefix
|
||||||
foreach ( $dataFields as $name => $field )
|
foreach ($dataFields as $name => $field) {
|
||||||
{
|
$field->Name = $this->escapeFieldName($id, $name);
|
||||||
$field->Name = $this->escapeFieldName($id, $name);
|
$dataFields[$name] = $field;
|
||||||
$dataFields[$name] = $field;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return $dataFields;
|
return $dataFields;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Escape a fieldName with a unique prefix.
|
||||||
|
*
|
||||||
|
* @param int $recordID Record id from who the field belongs
|
||||||
|
* @param string $name Field name
|
||||||
|
*
|
||||||
|
* @return string Escaped field name
|
||||||
|
*/
|
||||||
|
protected function escapeFieldName($recordID, $name)
|
||||||
|
{
|
||||||
|
return 'record_'.$recordID.'_'.$name;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Escape a fieldName with a unique prefix
|
* Un-escape a previously escaped field name.
|
||||||
*
|
*
|
||||||
* @param integer $recordID Record id from who the field belongs
|
* @param string $fieldName Escaped field name
|
||||||
* @param string $name Field name
|
*
|
||||||
* @return string Escaped field name
|
* @return array|false Fasle if the fieldName was not escaped. Or Array map with record 'id' and field 'name'
|
||||||
*/
|
*/
|
||||||
protected function escapeFieldName($recordID, $name)
|
protected function unEscapeFieldName($fieldName)
|
||||||
{
|
{
|
||||||
return 'record_' . $recordID . '_' . $name;
|
$parts = array();
|
||||||
}
|
$match = preg_match('/record_(\d+)_(\w+)/i', $fieldName, $parts);
|
||||||
|
|
||||||
|
if (!$match) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return array(
|
||||||
|
'id' => $parts[1],
|
||||||
|
'name' => $parts[2],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Un-escape a previously escaped field name
|
* Creates and return the bulk editing interface.
|
||||||
*
|
*
|
||||||
* @param string $fieldName Escaped field name
|
* @return string Form's HTML
|
||||||
* @return array|false Fasle if the fieldName was not escaped. Or Array map with record 'id' and field 'name'
|
*/
|
||||||
*/
|
public function index()
|
||||||
protected function unEscapeFieldName($fieldName)
|
{
|
||||||
{
|
$form = $this->bulkEditForm();
|
||||||
$parts = array();
|
$form->setTemplate('LeftAndMain_EditForm');
|
||||||
$match = preg_match('/record_(\d+)_(\w+)/i', $fieldName, $parts);
|
$form->addExtraClass('center cms-content');
|
||||||
|
$form->setAttribute('data-pjax-fragment', 'CurrentForm Content');
|
||||||
|
|
||||||
if ( !$match )
|
Requirements::javascript(BULKEDITTOOLS_MANAGER_PATH.'/javascript/GridFieldBulkEditingForm.js');
|
||||||
{
|
Requirements::css(BULKEDITTOOLS_MANAGER_PATH.'/css/GridFieldBulkEditingForm.css');
|
||||||
return false;
|
Requirements::add_i18n_javascript(BULKEDITTOOLS_PATH.'/lang/js');
|
||||||
}
|
|
||||||
else{
|
|
||||||
return array(
|
|
||||||
'id' => $parts[1],
|
|
||||||
'name' => $parts[2],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if ($this->request->isAjax()) {
|
||||||
|
$response = new SS_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');
|
||||||
|
|
||||||
/**
|
return $response;
|
||||||
* Creates and return the bulk editing interface
|
} else {
|
||||||
*
|
$controller = $this->getToplevelController();
|
||||||
* @return string Form's HTML
|
|
||||||
*/
|
|
||||||
public function index()
|
|
||||||
{
|
|
||||||
$form = $this->bulkEditForm();
|
|
||||||
$form->setTemplate('LeftAndMain_EditForm');
|
|
||||||
$form->addExtraClass('center cms-content');
|
|
||||||
$form->setAttribute('data-pjax-fragment', 'CurrentForm Content');
|
|
||||||
|
|
||||||
Requirements::javascript(BULKEDITTOOLS_MANAGER_PATH . '/javascript/GridFieldBulkEditingForm.js');
|
return $controller->customise(array('Content' => $form));
|
||||||
Requirements::css(BULKEDITTOOLS_MANAGER_PATH . '/css/GridFieldBulkEditingForm.css');
|
}
|
||||||
Requirements::add_i18n_javascript(BULKEDITTOOLS_PATH . '/lang/js');
|
}
|
||||||
|
|
||||||
if($this->request->isAjax())
|
/**
|
||||||
{
|
* Handles bulkEditForm submission
|
||||||
$response = new SS_HTTPResponse(
|
* and parses and saves each records data.
|
||||||
Convert::raw2json(array( 'Content' => $form->forAjaxTemplate()->getValue() ))
|
*
|
||||||
);
|
* @param array $data Sumitted form data
|
||||||
$response->addHeader('X-Pjax', 'Content');
|
* @param Form $form Form
|
||||||
$response->addHeader('Content-Type', 'text/json');
|
*/
|
||||||
$response->addHeader('X-Title', 'SilverStripe - Bulk '.$this->gridField->list->dataClass.' Editing');
|
public function doSave($data, $form)
|
||||||
return $response;
|
{
|
||||||
}
|
$className = $this->gridField->list->dataClass;
|
||||||
else {
|
$singleton = singleton($className);
|
||||||
$controller = $this->getToplevelController();
|
|
||||||
return $controller->customise(array( 'Content' => $form ));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
$formsData = array();
|
||||||
|
$ids = array();
|
||||||
|
$done = 0;
|
||||||
|
|
||||||
/**
|
//unescape and sort form data per record ID
|
||||||
* Handles bulkEditForm submission
|
foreach ($data as $fieldName => $value) {
|
||||||
* and parses and saves each records data
|
if ($fieldInfo = $this->unEscapeFieldName($fieldName)) {
|
||||||
*
|
if (!isset($formsData[$fieldInfo['id']])) {
|
||||||
* @param array $data Sumitted form data
|
$formsData[$fieldInfo['id']] = array();
|
||||||
* @param Form $form Form
|
}
|
||||||
*/
|
|
||||||
public function doSave($data, $form)
|
|
||||||
{
|
|
||||||
$className = $this->gridField->list->dataClass;
|
|
||||||
$singleton = singleton($className);
|
|
||||||
|
|
||||||
$formsData = array();
|
$formsData[$fieldInfo['id']][$fieldInfo['name']] = $value;
|
||||||
$ids = array();
|
}
|
||||||
$done = 0;
|
}
|
||||||
|
|
||||||
//unescape and sort form data per record ID
|
//process each record's form data and save
|
||||||
foreach ($data as $fieldName => $value)
|
foreach ($formsData as $recordID => $recordData) {
|
||||||
{
|
$record = DataObject::get_by_id($className, $recordID);
|
||||||
if ( $fieldInfo = $this->unEscapeFieldName($fieldName) )
|
$recordForm = Form::create(
|
||||||
{
|
$this, 'RecordForm',
|
||||||
if ( !isset($formsData[$fieldInfo['id']]) )
|
$record->getCMSFields(),
|
||||||
{
|
FieldList::create()
|
||||||
$formsData[$fieldInfo['id']] = array();
|
);
|
||||||
}
|
|
||||||
|
|
||||||
$formsData[$fieldInfo['id']][$fieldInfo['name']] = $value;
|
$recordForm->loadDataFrom($recordData);
|
||||||
}
|
$recordForm->saveInto($record);
|
||||||
}
|
$id = $record->write();
|
||||||
|
|
||||||
//process each record's form data and save
|
array_push($ids, $record->ID);
|
||||||
foreach ($formsData as $recordID => $recordData)
|
|
||||||
{
|
|
||||||
$record = DataObject::get_by_id($className, $recordID);
|
|
||||||
$recordForm = Form::create(
|
|
||||||
$this, "RecordForm",
|
|
||||||
$record->getCMSFields(),
|
|
||||||
FieldList::create()
|
|
||||||
);
|
|
||||||
|
|
||||||
$recordForm->loadDataFrom($recordData);
|
if ($id) {
|
||||||
$recordForm->saveInto($record);
|
++$done;
|
||||||
$id = $record->write();
|
}
|
||||||
|
}
|
||||||
|
|
||||||
array_push($ids, $record->ID);
|
//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.',
|
||||||
|
array(
|
||||||
|
'count' => $done,
|
||||||
|
'class' => $messageModelClass,
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$form->sessionMessage($message, 'good');
|
||||||
|
|
||||||
if ( $id )
|
//return back to form
|
||||||
{
|
return Controller::curr()->redirect($this->Link('?records[]='.implode('&records[]=', $ids)));
|
||||||
$done++;
|
//return Controller::curr()->redirect($form->Backlink); //returns to gridField
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
//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.',
|
|
||||||
array(
|
|
||||||
'count' => $done,
|
|
||||||
'class' => $messageModelClass
|
|
||||||
)
|
|
||||||
);
|
|
||||||
$form->sessionMessage($message, 'good');
|
|
||||||
|
|
||||||
//return back to form
|
|
||||||
return Controller::curr()->redirect($this->Link('?records[]='.implode('&records[]=', $ids)));
|
|
||||||
//return Controller::curr()->redirect($form->Backlink); //returns to gridField
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -2,135 +2,137 @@
|
|||||||
/**
|
/**
|
||||||
* 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
|
||||||
* @package GridFieldBulkEditingTools
|
|
||||||
* @subpackage BulkManager
|
|
||||||
*/
|
*/
|
||||||
class GridFieldBulkActionHandler extends RequestHandler
|
class GridFieldBulkActionHandler extends RequestHandler
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Related GridField instance
|
* Related GridField instance.
|
||||||
* @var GridField
|
*
|
||||||
*/
|
* @var GridField
|
||||||
protected $gridField;
|
*/
|
||||||
|
protected $gridField;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GridFieldBulkManager instance.
|
||||||
|
*
|
||||||
|
* @var GridFieldBulkManager
|
||||||
|
*/
|
||||||
|
protected $component;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* GridFieldBulkManager instance
|
* Current controller instance.
|
||||||
* @var GridFieldBulkManager
|
*
|
||||||
*/
|
* @var Controller
|
||||||
protected $component;
|
*/
|
||||||
|
protected $controller;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param GridFIeld $gridField
|
||||||
|
* @param GridField_URLHandler $component
|
||||||
|
* @param Controller $controller
|
||||||
|
*/
|
||||||
|
public function __construct($gridField, $component, $controller)
|
||||||
|
{
|
||||||
|
$this->gridField = $gridField;
|
||||||
|
$this->component = $component;
|
||||||
|
$this->controller = $controller;
|
||||||
|
parent::__construct();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Current controller instance
|
* Returns the URL for this RequestHandler.
|
||||||
* @var Controller
|
*
|
||||||
*/
|
* @author SilverStripe
|
||||||
protected $controller;
|
*
|
||||||
|
* @see GridFieldDetailForm_ItemRequest
|
||||||
|
*
|
||||||
|
* @param string $action
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function Link($action = null)
|
||||||
|
{
|
||||||
|
return Controller::join_links($this->gridField->Link(), 'bulkAction', $action);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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()
|
||||||
|
{
|
||||||
|
$c = $this->controller;
|
||||||
|
while ($c && ($c instanceof GridFieldDetailForm_ItemRequest || $c instanceof GridFieldDetailForm)) {
|
||||||
|
$c = $c->getController();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
return $c;
|
||||||
*
|
}
|
||||||
* @param GridFIeld $gridField
|
|
||||||
* @param GridField_URLHandler $component
|
|
||||||
* @param Controller $controller
|
|
||||||
*/
|
|
||||||
public function __construct($gridField, $component, $controller)
|
|
||||||
{
|
|
||||||
$this->gridField = $gridField;
|
|
||||||
$this->component = $component;
|
|
||||||
$this->controller = $controller;
|
|
||||||
parent::__construct();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
*
|
||||||
|
* @param bool $unlinked
|
||||||
|
*
|
||||||
|
* @return ArrayData
|
||||||
|
*/
|
||||||
|
public function Breadcrumbs($unlinked = false)
|
||||||
|
{
|
||||||
|
if (!$this->controller->hasMethod('Breadcrumbs')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
$items = $this->controller->Breadcrumbs($unlinked);
|
||||||
* Returns the URL for this RequestHandler
|
$items->push(new ArrayData(array(
|
||||||
*
|
'Title' => 'Bulk Editing',
|
||||||
* @author SilverStripe
|
'Link' => false,
|
||||||
* @see GridFieldDetailForm_ItemRequest
|
)));
|
||||||
* @param string $action
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function Link($action = null)
|
|
||||||
{
|
|
||||||
return Controller::join_links($this->gridField->Link(), 'bulkAction', $action);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
return $items;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Traverse up nested requests until we reach the first that's not a GridFieldDetailForm or GridFieldDetailForm_ItemRequest.
|
* Returns the list of record IDs selected in the front-end.
|
||||||
* The opposite of {@link Controller::curr()}, required because
|
*
|
||||||
* Controller::$controller_stack is not directly accessible.
|
* @return array List of IDs
|
||||||
*
|
*/
|
||||||
* @return Controller
|
public function getRecordIDList()
|
||||||
*/
|
{
|
||||||
protected function getToplevelController()
|
$vars = $this->request->requestVars();
|
||||||
{
|
|
||||||
$c = $this->controller;
|
|
||||||
while($c && ($c instanceof GridFieldDetailForm_ItemRequest || $c instanceof GridFieldDetailForm)) {
|
|
||||||
$c = $c->getController();
|
|
||||||
}
|
|
||||||
return $c;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
return $vars['records'];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Edited version of the GridFieldEditForm function
|
* Returns a DataList of the records selected in the front-end.
|
||||||
* adds the 'Bulk Upload' at the end of the crums
|
*
|
||||||
*
|
* @return DataList List of records
|
||||||
* CMS-specific functionality: Passes through navigation breadcrumbs
|
*/
|
||||||
* to the template, and includes the currently edited record (if any).
|
public function getRecords()
|
||||||
* see {@link LeftAndMain->Breadcrumbs()} for details.
|
{
|
||||||
*
|
$ids = $this->getRecordIDList();
|
||||||
* @author SilverStripe original Breadcrumbs() method
|
|
||||||
* @see GridFieldDetailForm_ItemRequest
|
|
||||||
* @param boolean $unlinked
|
|
||||||
* @return ArrayData
|
|
||||||
*/
|
|
||||||
public function Breadcrumbs($unlinked = false)
|
|
||||||
{
|
|
||||||
if(!$this->controller->hasMethod('Breadcrumbs')) return;
|
|
||||||
|
|
||||||
$items = $this->controller->Breadcrumbs($unlinked);
|
if ($ids) {
|
||||||
$items->push(new ArrayData(array(
|
$class = $this->gridField->list->dataClass;
|
||||||
'Title' => 'Bulk Editing',
|
|
||||||
'Link' => false
|
|
||||||
)));
|
|
||||||
return $items;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
return DataList::create($class)->byIDs($ids);
|
||||||
/**
|
} else {
|
||||||
* Returns the list of record IDs selected in the front-end
|
return false;
|
||||||
*
|
}
|
||||||
* @return array List of IDs
|
}
|
||||||
*/
|
|
||||||
public function getRecordIDList()
|
|
||||||
{
|
|
||||||
$vars = $this->request->requestVars();
|
|
||||||
return $vars['records'];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns a DataList of the records selected in the front-end
|
|
||||||
*
|
|
||||||
* @return DataList List of records
|
|
||||||
*/
|
|
||||||
public function getRecords()
|
|
||||||
{
|
|
||||||
$ids = $this->getRecordIDList();
|
|
||||||
|
|
||||||
if ( $ids )
|
|
||||||
{
|
|
||||||
$class = $this->gridField->list->dataClass;
|
|
||||||
return DataList::create($class)->byIDs( $ids );
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -3,43 +3,43 @@
|
|||||||
* Bulk action handler for unlinking records.
|
* Bulk action handler for unlinking records.
|
||||||
*
|
*
|
||||||
* @author colymba
|
* @author colymba
|
||||||
* @package GridFieldBulkEditingTools
|
|
||||||
* @subpackage BulkManager
|
|
||||||
*/
|
*/
|
||||||
class GridFieldBulkActionUnLinkHandler extends GridFieldBulkActionHandler
|
class GridFieldBulkActionUnlinkHandler extends GridFieldBulkActionHandler
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* RequestHandler allowed actions
|
* RequestHandler allowed actions.
|
||||||
* @var array
|
*
|
||||||
*/
|
* @var array
|
||||||
private static $allowed_actions = array('unLink');
|
*/
|
||||||
|
private static $allowed_actions = array('unLink');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RequestHandler url => action map.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private static $url_handlers = array(
|
||||||
|
'unLink' => 'unLink',
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* RequestHandler url => action map
|
* Unlink the selected records passed from the unlink bulk action.
|
||||||
* @var array
|
*
|
||||||
*/
|
* @param SS_HTTPRequest $request
|
||||||
private static $url_handlers = array(
|
*
|
||||||
'unLink' => 'unLink'
|
* @return SS_HTTPResponse List of affected records ID
|
||||||
);
|
*/
|
||||||
|
public function unLink(SS_HTTPRequest $request)
|
||||||
|
{
|
||||||
|
$ids = $this->getRecordIDList();
|
||||||
|
$this->gridField->list->removeMany($ids);
|
||||||
|
|
||||||
|
$response = new SS_HTTPResponse(Convert::raw2json(array(
|
||||||
|
'done' => true,
|
||||||
|
'records' => $ids,
|
||||||
|
)));
|
||||||
|
$response->addHeader('Content-Type', 'text/json');
|
||||||
|
|
||||||
/**
|
return $response;
|
||||||
* Unlink the selected records passed from the unlink bulk action
|
}
|
||||||
*
|
|
||||||
* @param SS_HTTPRequest $request
|
|
||||||
* @return SS_HTTPResponse List of affected records ID
|
|
||||||
*/
|
|
||||||
public function unLink(SS_HTTPRequest $request)
|
|
||||||
{
|
|
||||||
$ids = $this->getRecordIDList();
|
|
||||||
$this->gridField->list->removeMany($ids);
|
|
||||||
|
|
||||||
$response = new SS_HTTPResponse(Convert::raw2json(array(
|
|
||||||
'done' => true,
|
|
||||||
'records' => $ids
|
|
||||||
)));
|
|
||||||
$response->addHeader('Content-Type', 'text/json');
|
|
||||||
return $response;
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -1,378 +1,364 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* GridField component for editing attached models in bulk
|
* GridField component for editing attached models in bulk.
|
||||||
*
|
*
|
||||||
* @author colymba
|
* @author colymba
|
||||||
* @package GridFieldBulkEditingTools
|
|
||||||
* @subpackage BulkManager
|
|
||||||
*/
|
*/
|
||||||
class GridFieldBulkManager implements GridField_HTMLProvider, GridField_ColumnProvider, GridField_URLHandler
|
class GridFieldBulkManager 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.
|
||||||
|
*
|
||||||
|
* @param array $editableFields List of editable fields
|
||||||
|
* @param bool $defaultActions Use default actions list. False to start fresh.
|
||||||
|
*/
|
||||||
|
public function __construct($editableFields = null, $defaultActions = true)
|
||||||
|
{
|
||||||
|
if ($editableFields != null) {
|
||||||
|
$this->setConfig('editableFields', $editableFields);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
if ($defaultActions) {
|
||||||
* GridFieldBulkManager component constructor
|
$this->config['actions'] = array(
|
||||||
*
|
'bulkEdit' => array(
|
||||||
* @param array $editableFields List of editable fields
|
'label' => _t('GRIDFIELD_BULK_MANAGER.EDIT_SELECT_LABEL', 'Edit'),
|
||||||
* @param boolean $defaultActions Use default actions list. False to start fresh.
|
'handler' => 'GridFieldBulkActionEditHandler',
|
||||||
*/
|
'config' => array(
|
||||||
public function __construct($editableFields = null, $defaultActions = true)
|
'isAjax' => false,
|
||||||
{
|
'icon' => 'pencil',
|
||||||
if ( $editableFields != null ) $this->setConfig ( 'editableFields', $editableFields );
|
'isDestructive' => false,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
'unLink' => array(
|
||||||
|
'label' => _t('GRIDFIELD_BULK_MANAGER.UNLINK_SELECT_LABEL', 'UnLink'),
|
||||||
|
'handler' => 'GridFieldBulkActionUnLinkHandler',
|
||||||
|
'config' => array(
|
||||||
|
'isAjax' => true,
|
||||||
|
'icon' => 'chain--minus',
|
||||||
|
'isDestructive' => false,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
'delete' => array(
|
||||||
|
'label' => _t('GRIDFIELD_BULK_MANAGER.DELETE_SELECT_LABEL', 'Delete'),
|
||||||
|
'handler' => 'GridFieldBulkActionDeleteHandler',
|
||||||
|
'config' => array(
|
||||||
|
'isAjax' => true,
|
||||||
|
'icon' => 'decline',
|
||||||
|
'isDestructive' => true,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ( $defaultActions )
|
/* **********************************************************************
|
||||||
{
|
* Components settings and custom methodes
|
||||||
$this->config['actions'] = array(
|
* */
|
||||||
'bulkEdit' => array(
|
|
||||||
'label' => _t('GRIDFIELD_BULK_MANAGER.EDIT_SELECT_LABEL', 'Edit'),
|
|
||||||
'handler' => 'GridFieldBulkActionEditHandler',
|
|
||||||
'config' => array(
|
|
||||||
'isAjax' => false,
|
|
||||||
'icon' => 'pencil',
|
|
||||||
'isDestructive' => false
|
|
||||||
)
|
|
||||||
),
|
|
||||||
'unLink' => array(
|
|
||||||
'label' => _t('GRIDFIELD_BULK_MANAGER.UNLINK_SELECT_LABEL', 'UnLink'),
|
|
||||||
'handler' => 'GridFieldBulkActionUnLinkHandler',
|
|
||||||
'config' => array(
|
|
||||||
'isAjax' => true,
|
|
||||||
'icon' => 'chain--minus',
|
|
||||||
'isDestructive' => false
|
|
||||||
)
|
|
||||||
),
|
|
||||||
'delete' => array(
|
|
||||||
'label' => _t('GRIDFIELD_BULK_MANAGER.DELETE_SELECT_LABEL', 'Delete'),
|
|
||||||
'handler' => 'GridFieldBulkActionDeleteHandler',
|
|
||||||
'config' => array(
|
|
||||||
'isAjax' => true,
|
|
||||||
'icon' => 'decline',
|
|
||||||
'isDestructive' => true
|
|
||||||
)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the component configuration parameter.
|
||||||
|
*
|
||||||
|
* @param string $reference
|
||||||
|
* @param mixed $value
|
||||||
|
*/
|
||||||
|
public function setConfig($reference, $value)
|
||||||
|
{
|
||||||
|
if (!array_key_exists($reference, $this->config)) {
|
||||||
|
user_error("Unknown option reference: $reference", E_USER_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($reference == 'actions') {
|
||||||
|
user_error('Bulk actions must be edited via addBulkAction() and removeBulkAction()', E_USER_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
/* **********************************************************************
|
if (($reference == 'editableFields') && !is_array($value)) {
|
||||||
* Components settings and custom methodes
|
$value = array($value);
|
||||||
* */
|
}
|
||||||
|
|
||||||
/**
|
$this->config[$reference] = $value;
|
||||||
* Sets the component configuration parameter
|
|
||||||
*
|
|
||||||
* @param string $reference
|
|
||||||
* @param mixed $value
|
|
||||||
*/
|
|
||||||
function setConfig($reference, $value)
|
|
||||||
{
|
|
||||||
if (!array_key_exists($reference, $this->config) )
|
|
||||||
{
|
|
||||||
user_error("Unknown option reference: $reference", E_USER_ERROR);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( $reference == 'actions' )
|
return $this;
|
||||||
{
|
}
|
||||||
user_error("Bulk actions must be edited via addBulkAction() and removeBulkAction()", E_USER_ERROR);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( ($reference == 'editableFields') && !is_array($value) )
|
/**
|
||||||
{
|
* Returns one $config parameter of the full $config.
|
||||||
$value = array($value);
|
*
|
||||||
}
|
* @param string $reference $congif parameter to return
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getConfig($reference = false)
|
||||||
|
{
|
||||||
|
if ($reference) {
|
||||||
|
return $this->config[$reference];
|
||||||
|
} else {
|
||||||
|
return $this->config;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$this->config[$reference] = $value;
|
/**
|
||||||
|
* 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'
|
||||||
|
* @param array $config Front-end configuration array( 'isAjax' => true, 'icon' => 'accept', 'isDestructive' => false )
|
||||||
|
*
|
||||||
|
* @return GridFieldBulkManager Current GridFieldBulkManager instance
|
||||||
|
*/
|
||||||
|
public function addBulkAction($name, $label = null, $handler = null, $config = null)
|
||||||
|
{
|
||||||
|
if (array_key_exists($name, $this->config['actions'])) {
|
||||||
|
user_error("Bulk action '$name' already exists.", E_USER_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
return $this;
|
if (!$label) {
|
||||||
}
|
$label = ucfirst($name);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$handler) {
|
||||||
|
$handler = 'GridFieldBulkAction'.ucfirst($name).'Handler';
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
if (!ClassInfo::exists($handler)) {
|
||||||
* Returns one $config parameter of the full $config
|
user_error("Bulk action handler for $name not found: $handler", E_USER_ERROR);
|
||||||
*
|
}
|
||||||
* @param string $reference $congif parameter to return
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
function getConfig ( $reference = false )
|
|
||||||
{
|
|
||||||
if ( $reference ) return $this->config[$reference];
|
|
||||||
else return $this->config;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if ($config && !is_array($config)) {
|
||||||
|
user_error('Bulk action front-end config should be an array of key => value pairs.', E_USER_ERROR);
|
||||||
|
} else {
|
||||||
|
$config = array(
|
||||||
|
'isAjax' => isset($config['isAjax']) ? $config['isAjax'] : true,
|
||||||
|
'icon' => isset($config['icon']) ? $config['icon'] : 'accept',
|
||||||
|
'isDestructive' => isset($config['isDestructive']) ? $config['isDestructive'] : false,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
$this->config['actions'][$name] = array(
|
||||||
* Lets you add custom bulk actions to the bulk manager interface
|
'label' => $label,
|
||||||
*
|
|
||||||
* @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'
|
|
||||||
* @param array $config Front-end configuration array( 'isAjax' => true, 'icon' => 'accept', 'isDestructive' => false )
|
|
||||||
* @return GridFieldBulkManager Current GridFieldBulkManager instance
|
|
||||||
*/
|
|
||||||
function addBulkAction($name, $label = null, $handler = null, $config = null)
|
|
||||||
{
|
|
||||||
if ( array_key_exists($name, $this->config['actions']) )
|
|
||||||
{
|
|
||||||
user_error("Bulk action '$name' already exists.", E_USER_ERROR);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( !$label )
|
|
||||||
{
|
|
||||||
$label = ucfirst($name);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( !$handler )
|
|
||||||
{
|
|
||||||
$handler = 'GridFieldBulkAction'.ucfirst($name).'Handler';
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( !ClassInfo::exists( $handler ) )
|
|
||||||
{
|
|
||||||
user_error("Bulk action handler for $name not found: $handler", E_USER_ERROR);
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( $config && !is_array($config) )
|
|
||||||
{
|
|
||||||
user_error("Bulk action front-end config should be an array of key => value pairs.", E_USER_ERROR);
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
$config = array(
|
|
||||||
'isAjax' => isset($config['isAjax']) ? $config['isAjax'] : true,
|
|
||||||
'icon' => isset($config['icon']) ? $config['icon'] : 'accept',
|
|
||||||
'isDestructive' => isset($config['isDestructive']) ? $config['isDestructive'] : false
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->config['actions'][$name] = array(
|
|
||||||
'label' => $label,
|
|
||||||
'handler' => $handler,
|
'handler' => $handler,
|
||||||
'config' => $config
|
'config' => $config,
|
||||||
);
|
);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes a bulk actions from the bulk manager interface.
|
||||||
|
*
|
||||||
|
* @param string $name Bulk action's name
|
||||||
|
*
|
||||||
|
* @return GridFieldBulkManager Current GridFieldBulkManager instance
|
||||||
|
*/
|
||||||
|
public function removeBulkAction($name)
|
||||||
|
{
|
||||||
|
if (!array_key_exists($name, $this->config['actions'])) {
|
||||||
|
user_error("Bulk action '$name' doesn't exists.", E_USER_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
unset($this->config['actions'][$name]);
|
||||||
* Removes a bulk actions from the bulk manager interface
|
|
||||||
*
|
|
||||||
* @param string $name Bulk action's name
|
|
||||||
* @return GridFieldBulkManager Current GridFieldBulkManager instance
|
|
||||||
*/
|
|
||||||
function removeBulkAction($name)
|
|
||||||
{
|
|
||||||
if ( !array_key_exists($name, $this->config['actions']) )
|
|
||||||
{
|
|
||||||
user_error("Bulk action '$name' doesn't exists.", E_USER_ERROR);
|
|
||||||
}
|
|
||||||
|
|
||||||
unset( $this->config['actions'][$name] );
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
return $this;
|
/* **********************************************************************
|
||||||
}
|
* GridField_ColumnProvider
|
||||||
|
* */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add bulk select column.
|
||||||
|
*
|
||||||
|
* @param GridField $gridField Current GridField instance
|
||||||
|
* @param array $columns Columns list
|
||||||
|
*/
|
||||||
|
public function augmentColumns($gridField, &$columns)
|
||||||
|
{
|
||||||
|
if (!in_array('BulkSelect', $columns)) {
|
||||||
|
$columns[] = 'BulkSelect';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Which columns are handled by the component.
|
||||||
|
*
|
||||||
|
* @param GridField $gridField Current GridField instance
|
||||||
|
*
|
||||||
|
* @return array List of handled column names
|
||||||
|
*/
|
||||||
|
public function getColumnsHandled($gridField)
|
||||||
|
{
|
||||||
|
return array('BulkSelect');
|
||||||
|
}
|
||||||
|
|
||||||
/* **********************************************************************
|
/**
|
||||||
* GridField_ColumnProvider
|
* 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
|
||||||
|
*
|
||||||
|
* @return mixed Column's field content
|
||||||
|
*/
|
||||||
|
public function getColumnContent($gridField, $record, $columnName)
|
||||||
|
{
|
||||||
|
$cb = CheckboxField::create('bulkSelect_'.$record->ID)
|
||||||
|
->addExtraClass('bulkSelect no-change-track')
|
||||||
|
->setAttribute('data-record', $record->ID);
|
||||||
|
|
||||||
/**
|
return $cb->Field();
|
||||||
* Add bulk select column
|
}
|
||||||
*
|
|
||||||
* @param GridField $gridField Current GridField instance
|
|
||||||
* @param array $columns Columns list
|
|
||||||
*/
|
|
||||||
function augmentColumns($gridField, &$columns)
|
|
||||||
{
|
|
||||||
if(!in_array('BulkSelect', $columns)) $columns[] = 'BulkSelect';
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
*
|
||||||
|
* @return array List of HTML attributes
|
||||||
|
*/
|
||||||
|
public function getColumnAttributes($gridField, $record, $columnName)
|
||||||
|
{
|
||||||
|
return array('class' => 'col-bulkSelect');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Which columns are handled by the component
|
* Set the column's meta data.
|
||||||
*
|
*
|
||||||
* @param GridField $gridField Current GridField instance
|
* @param GridField $gridField Current GridField instance
|
||||||
* @return array List of handled column names
|
* @param string $columnName Column's name for which we need meta data
|
||||||
*/
|
*
|
||||||
function getColumnsHandled($gridField)
|
* @return array List of meta data
|
||||||
{
|
*/
|
||||||
return array('BulkSelect');
|
public function getColumnMetadata($gridField, $columnName)
|
||||||
}
|
{
|
||||||
|
if ($columnName == 'BulkSelect') {
|
||||||
|
return array('title' => 'Select');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* **********************************************************************
|
||||||
|
* GridField_HTMLProvider
|
||||||
|
* */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the column's content
|
* @param GridField $gridField
|
||||||
*
|
*
|
||||||
* @param GridField $gridField Current GridField instance
|
* @return array
|
||||||
* @param DataObject $record Record intance for this row
|
*/
|
||||||
* @param string $columnName Column's name for which we need content
|
public function getHTMLFragments($gridField)
|
||||||
* @return mixed Column's field content
|
{
|
||||||
*/
|
Requirements::css(BULKEDITTOOLS_MANAGER_PATH.'/css/GridFieldBulkManager.css');
|
||||||
function getColumnContent($gridField, $record, $columnName)
|
Requirements::javascript(BULKEDITTOOLS_MANAGER_PATH.'/javascript/GridFieldBulkManager.js');
|
||||||
{
|
Requirements::add_i18n_javascript(BULKEDITTOOLS_PATH.'/lang/js');
|
||||||
$cb = CheckboxField::create('bulkSelect_'.$record->ID)
|
|
||||||
->addExtraClass('bulkSelect no-change-track')
|
|
||||||
->setAttribute('data-record', $record->ID);
|
|
||||||
return $cb->Field();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (!count($this->config['actions'])) {
|
||||||
|
user_error('Trying to use GridFieldBulkManager without any bulk action.', E_USER_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
$actionsListSource = array();
|
||||||
* Set the column's HTML attributes
|
$actionsConfig = array();
|
||||||
*
|
|
||||||
* @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
|
|
||||||
* @return array List of HTML attributes
|
|
||||||
*/
|
|
||||||
function getColumnAttributes($gridField, $record, $columnName)
|
|
||||||
{
|
|
||||||
return array('class' => 'col-bulkSelect');
|
|
||||||
}
|
|
||||||
|
|
||||||
|
foreach ($this->config['actions'] as $action => $actionData) {
|
||||||
|
$actionsListSource[$action] = $actionData['label'];
|
||||||
|
$actionsConfig[$action] = $actionData['config'];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
reset($this->config['actions']);
|
||||||
* Set the column's meta data
|
$firstAction = key($this->config['actions']);
|
||||||
*
|
|
||||||
* @param GridField $gridField Current GridField instance
|
|
||||||
* @param string $columnName Column's name for which we need meta data
|
|
||||||
* @return array List of meta data
|
|
||||||
*/
|
|
||||||
function getColumnMetadata($gridField, $columnName)
|
|
||||||
{
|
|
||||||
if($columnName == 'BulkSelect') {
|
|
||||||
return array('title' => 'Select');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
$dropDownActionsList = DropdownField::create('bulkActionName', '')
|
||||||
|
->setSource($actionsListSource)
|
||||||
|
->setAttribute('class', 'bulkActionName no-change-track')
|
||||||
|
->setAttribute('id', '');
|
||||||
|
|
||||||
|
$templateData = array(
|
||||||
/* **********************************************************************
|
'Menu' => $dropDownActionsList->FieldHolder(),
|
||||||
* GridField_HTMLProvider
|
'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'),
|
||||||
* @param GridField $gridField
|
),
|
||||||
* @return array
|
'Select' => array(
|
||||||
*/
|
'Label' => _t('GRIDFIELD_BULK_MANAGER.SELECT_ALL_LABEL', 'Select all'),
|
||||||
public function getHTMLFragments($gridField)
|
),
|
||||||
{
|
'Colspan' => (count($gridField->getColumns()) - 1),
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
$actionsListSource = array();
|
|
||||||
$actionsConfig = array();
|
|
||||||
|
|
||||||
foreach ($this->config['actions'] as $action => $actionData)
|
|
||||||
{
|
|
||||||
$actionsListSource[$action] = $actionData['label'];
|
|
||||||
$actionsConfig[$action] = $actionData['config'];
|
|
||||||
}
|
|
||||||
|
|
||||||
reset($this->config['actions']);
|
|
||||||
$firstAction = key($this->config['actions']);
|
|
||||||
|
|
||||||
$dropDownActionsList = DropdownField::create('bulkActionName', '')
|
|
||||||
->setSource( $actionsListSource )
|
|
||||||
->setAttribute('class', 'bulkActionName no-change-track')
|
|
||||||
->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)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
$templateData = new ArrayData($templateData);
|
$templateData = new ArrayData($templateData);
|
||||||
|
|
||||||
return array(
|
return array(
|
||||||
'header' => $templateData->renderWith('BulkManagerButtons')
|
'header' => $templateData->renderWith('BulkManagerButtons'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* **********************************************************************
|
||||||
|
* GridField_URLHandler
|
||||||
|
* */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an action => handler list.
|
||||||
|
*
|
||||||
|
* @param GridField $gridField
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getURLHandlers($gridField)
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
'bulkAction' => 'handleBulkAction',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/* **********************************************************************
|
/**
|
||||||
* GridField_URLHandler
|
* Pass control over to the RequestHandler
|
||||||
* */
|
* loop through the handlers provided in config['actions']
|
||||||
|
* and find matching url_handlers.
|
||||||
|
*
|
||||||
|
* $url_handlers rule should not use wildcards like '$Action' => '$Action'
|
||||||
|
* but have more specific path defined
|
||||||
|
*
|
||||||
|
* @param GridField $gridField
|
||||||
|
* @param SS_HTTPRequest $request
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function handleBulkAction($gridField, $request)
|
||||||
|
{
|
||||||
|
$controller = $gridField->getForm()->Controller();
|
||||||
|
|
||||||
/**
|
foreach ($this->config['actions'] as $name => $data) {
|
||||||
* Returns an action => handler list
|
$handlerClass = $data['handler'];
|
||||||
*
|
$urlHandlers = Config::inst()->get($handlerClass, 'url_handlers', Config::UNINHERITED);
|
||||||
* @param GridField $gridField
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getURLHandlers($gridField) {
|
|
||||||
return array(
|
|
||||||
'bulkAction' => 'handleBulkAction'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if ($urlHandlers) {
|
||||||
|
foreach ($urlHandlers as $rule => $action) {
|
||||||
|
if ($request->match($rule, false)) {
|
||||||
|
//print_r('matched ' . $handlerClass . ' to ' . $rule);
|
||||||
|
$handler = Injector::inst()->create($handlerClass, $gridField, $this, $controller);
|
||||||
|
|
||||||
/**
|
return $handler->handleRequest($request, DataModel::inst());
|
||||||
* Pass control over to the RequestHandler
|
}
|
||||||
* loop through the handlers provided in config['actions']
|
}
|
||||||
* and find matching url_handlers.
|
}
|
||||||
*
|
}
|
||||||
* $url_handlers rule should not use wildcards like '$Action' => '$Action'
|
|
||||||
* but have more specific path defined
|
|
||||||
*
|
|
||||||
* @param GridField $gridField
|
|
||||||
* @param SS_HTTPRequest $request
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function handleBulkAction($gridField, $request)
|
|
||||||
{
|
|
||||||
$controller = $gridField->getForm()->Controller();
|
|
||||||
|
|
||||||
foreach ($this->config['actions'] as $name => $data)
|
user_error('Unable to find matching bulk action handler for '.$request->remaining().'.', E_USER_ERROR);
|
||||||
{
|
}
|
||||||
$handlerClass = $data['handler'];
|
|
||||||
$urlHandlers = Config::inst()->get($handlerClass, 'url_handlers', Config::UNINHERITED);
|
|
||||||
|
|
||||||
if($urlHandlers) foreach($urlHandlers as $rule => $action)
|
|
||||||
{
|
|
||||||
if($request->match($rule, false))
|
|
||||||
{
|
|
||||||
//print_r('matched ' . $handlerClass . ' to ' . $rule);
|
|
||||||
$handler = Injector::inst()->create($handlerClass, $gridField, $this, $controller);
|
|
||||||
return $handler->handleRequest($request, DataModel::inst());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
user_error("Unable to find matching bulk action handler for ".$request->remaining().'.', E_USER_ERROR);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,15 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* 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 GridFieldBulkUpload} class instead.
|
||||||
*
|
*
|
||||||
* @author colymba
|
* @author colymba
|
||||||
* @package GridFieldBulkEditingTools
|
|
||||||
* @subpackage BulkUpload
|
|
||||||
*/
|
*/
|
||||||
class GridFieldBulkImageUpload extends GridFieldBulkUpload
|
class GridFieldBulkImageUpload extends GridFieldBulkUpload
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Component constructor
|
* Component constructor.
|
||||||
*
|
*
|
||||||
* @deprecated 2.0 "GridFieldBulkImageUpload" is deprecated, use {@link GridFieldBulkUpload} class instead.
|
* @deprecated 2.0 "GridFieldBulkImageUpload" is deprecated, use {@link GridFieldBulkUpload} class instead.
|
||||||
*
|
*
|
||||||
@ -19,7 +17,8 @@ class GridFieldBulkImageUpload extends GridFieldBulkUpload
|
|||||||
*/
|
*/
|
||||||
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 "GridFieldBulkUpload" class instead.');
|
||||||
return new GridFieldBulkUpload($fileRelationName);
|
|
||||||
|
return new GridFieldBulkUpload($fileRelationName);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,432 +1,422 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* GridField component for uploading images in bulk
|
* GridField component for uploading images in bulk.
|
||||||
*
|
*
|
||||||
* @author colymba
|
* @author colymba
|
||||||
* @package GridFieldBulkEditingTools
|
|
||||||
* @subpackage BulkUpload
|
|
||||||
*/
|
*/
|
||||||
class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandler
|
class GridFieldBulkUpload implements GridField_HTMLProvider, GridField_URLHandler
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* component configuration
|
* component configuration.
|
||||||
*
|
*
|
||||||
* 'fileRelationName' => field name of the $has_one File/Image relation
|
* 'fileRelationName' => field name of the $has_one File/Image relation
|
||||||
* @var array
|
*
|
||||||
*/
|
* @var array
|
||||||
protected $config = array(
|
*/
|
||||||
'fileRelationName' => null
|
protected $config = array(
|
||||||
);
|
'fileRelationName' => null,
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
/**
|
* UploadField configuration.
|
||||||
* UploadField configuration.
|
* 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
|
*
|
||||||
*
|
* @var array
|
||||||
* @var array
|
*/
|
||||||
*/
|
protected $ufConfig = array(
|
||||||
protected $ufConfig = array(
|
'sequentialUploads' => false,
|
||||||
'sequentialUploads' => false,
|
|
||||||
'canAttachExisting' => true,
|
'canAttachExisting' => true,
|
||||||
'canPreviewFolder' => true
|
'canPreviewFolder' => true,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* UploadField setup function calls.
|
||||||
|
* List of setup functions to call on {@link UploadField} with the value to pass.
|
||||||
|
*
|
||||||
|
* e.g. array('setFolderName' => 'bulkUpload') will result in:
|
||||||
|
* $uploadField->setFolderName('bulkUpload')
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $ufSetup = array(
|
||||||
|
'setFolderName' => 'bulkUpload',
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* UploadField setup function calls.
|
* UploadField Validator setup function calls.
|
||||||
* List of setup functions to call on {@link UploadField} with the value to pass
|
* List of setup functions to call on {@link Upload::validator} with the value to pass.
|
||||||
*
|
*
|
||||||
* e.g. array('setFolderName' => 'bulkUpload') will result in:
|
* e.g. array('setAllowedMaxFileSize' => 10) will result in:
|
||||||
* $uploadField->setFolderName('bulkUpload')
|
* $uploadField->getValidator()->setAllowedMaxFileSize(10)
|
||||||
*
|
*
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $ufSetup = array(
|
protected $ufValidatorSetup = array(
|
||||||
'setFolderName' => 'bulkUpload'
|
'setAllowedMaxFileSize' => null,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Component constructor.
|
||||||
|
*
|
||||||
|
* @param string $fileRelationName
|
||||||
|
*/
|
||||||
|
public function __construct($fileRelationName = null)
|
||||||
|
{
|
||||||
|
if ($fileRelationName != null) {
|
||||||
|
$this->setConfig('fileRelationName', $fileRelationName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/* **********************************************************************
|
||||||
* UploadField Validator setup function calls.
|
* Components settings and custom methodes
|
||||||
* List of setup functions to call on {@link Upload::validator} with the value to pass
|
* */
|
||||||
*
|
|
||||||
* e.g. array('setAllowedMaxFileSize' => 10) will result in:
|
|
||||||
* $uploadField->getValidator()->setAllowedMaxFileSize(10)
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
protected $ufValidatorSetup = array(
|
|
||||||
'setAllowedMaxFileSize' => null
|
|
||||||
);
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set a component configuration parameter.
|
||||||
|
*
|
||||||
|
* @param string $reference
|
||||||
|
* @param mixed $value
|
||||||
|
*/
|
||||||
|
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.");
|
||||||
|
|
||||||
/**
|
if ($reference === 'folderName') {
|
||||||
* Component constructor
|
$this->setUfSetup('setFolderName', $value);
|
||||||
*
|
} elseif ($reference === 'maxFileSize') {
|
||||||
* @param string $fileRelationName
|
$this->setUfValidatorSetup('setAllowedMaxFileSize', $value);
|
||||||
*/
|
} else {
|
||||||
public function __construct($fileRelationName = null)
|
$this->setUfConfig($reference, $value);
|
||||||
{
|
}
|
||||||
if ( $fileRelationName != null ) $this->setConfig ( 'fileRelationName', $fileRelationName );
|
} elseif (!array_key_exists($reference, $this->config)) {
|
||||||
}
|
user_error("Unknown option reference: $reference", E_USER_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->config[$reference] = $value;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/* **********************************************************************
|
/**
|
||||||
* Components settings and custom methodes
|
* Set an UploadField configuration parameter.
|
||||||
* */
|
*
|
||||||
|
* @param string $reference
|
||||||
|
* @param mixed $value
|
||||||
|
*/
|
||||||
|
public function setUfConfig($reference, $value)
|
||||||
|
{
|
||||||
|
$this->ufConfig[$reference] = $value;
|
||||||
|
|
||||||
/**
|
return $this;
|
||||||
* Set a component configuration parameter
|
}
|
||||||
*
|
|
||||||
* @param string $reference
|
|
||||||
* @param mixed $value
|
|
||||||
*/
|
|
||||||
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.");
|
|
||||||
|
|
||||||
if ( $reference === 'folderName' )
|
/**
|
||||||
{
|
* Set an UploadField setup function call.
|
||||||
$this->setUfSetup('setFolderName', $value);
|
*
|
||||||
}
|
* @param string $function
|
||||||
else if ( $reference === 'maxFileSize' )
|
* @param mixed $param
|
||||||
{
|
*/
|
||||||
$this->setUfValidatorSetup('setAllowedMaxFileSize', $value);
|
public function setUfSetup($function, $param)
|
||||||
}
|
{
|
||||||
else{
|
$this->ufSetup[$function] = $param;
|
||||||
$this->setUfConfig($reference, $value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (!array_key_exists($reference, $this->config) ) {
|
|
||||||
user_error("Unknown option reference: $reference", E_USER_ERROR);
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->config[$reference] = $value;
|
return $this;
|
||||||
return $this;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set an UploadField Validator setup function call.
|
||||||
|
*
|
||||||
|
* @param string $function
|
||||||
|
* @param mixed $param
|
||||||
|
*/
|
||||||
|
public function setUfValidatorSetup($function, $param)
|
||||||
|
{
|
||||||
|
$this->ufValidatorSetup[$function] = $param;
|
||||||
|
|
||||||
/**
|
return $this;
|
||||||
* Set an UploadField configuration parameter
|
}
|
||||||
*
|
|
||||||
* @param string $reference
|
|
||||||
* @param mixed $value
|
|
||||||
*/
|
|
||||||
function setUfConfig ( $reference, $value )
|
|
||||||
{
|
|
||||||
$this->ufConfig[$reference] = $value;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns one $config reference or the full $config.
|
||||||
|
*
|
||||||
|
* @param string $reference $congif parameter to return
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getConfig($reference = false)
|
||||||
|
{
|
||||||
|
if ($reference) {
|
||||||
|
return $this->config[$reference];
|
||||||
|
} else {
|
||||||
|
return $this->config;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set an UploadField setup function call
|
* Returns one $ufConfig reference or the full config.
|
||||||
*
|
*
|
||||||
* @param string $function
|
* @param string $reference $ufConfig parameter to return
|
||||||
* @param mixed $param
|
*
|
||||||
*/
|
* @return mixed
|
||||||
function setUfSetup ( $function, $param )
|
*/
|
||||||
{
|
public function getUfConfig($reference = false)
|
||||||
$this->ufSetup[$function] = $param;
|
{
|
||||||
return $this;
|
if ($reference) {
|
||||||
}
|
return $this->ufConfig[$reference];
|
||||||
|
} else {
|
||||||
|
return $this->ufConfig;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns one $ufSetup reference or the full config.
|
||||||
|
*
|
||||||
|
* @param string $reference $ufSetup parameter to return
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getUfSetup($reference = false)
|
||||||
|
{
|
||||||
|
if ($reference) {
|
||||||
|
return $this->ufSetup[$reference];
|
||||||
|
} else {
|
||||||
|
return $this->ufSetup;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set an UploadField Validator setup function call
|
* Returns one $ufValidatorSetup reference or the full config.
|
||||||
*
|
*
|
||||||
* @param string $function
|
* @param string $reference $ufValidatorSetup parameter to return
|
||||||
* @param mixed $param
|
*
|
||||||
*/
|
* @return mixed
|
||||||
function setUfValidatorSetup ( $function, $param )
|
*/
|
||||||
{
|
public function getUfValidatorSetup($reference = false)
|
||||||
$this->ufValidatorSetup[$function] = $param;
|
{
|
||||||
return $this;
|
if ($reference) {
|
||||||
}
|
return $this->ufValidatorSetup[$reference];
|
||||||
|
} else {
|
||||||
|
return $this->ufValidatorSetup;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the first has_one Image/File relation from the GridField managed DataObject
|
||||||
|
* i.e. 'MyImage' => 'Image' will return 'MyImage'.
|
||||||
|
*
|
||||||
|
* @return string Name of the $has_one relation
|
||||||
|
*/
|
||||||
|
public function getDefaultFileRelationName($gridField)
|
||||||
|
{
|
||||||
|
$recordClass = $gridField->list->dataClass;
|
||||||
|
$hasOneFields = Config::inst()->get($recordClass, 'has_one', Config::INHERITED);
|
||||||
|
|
||||||
/**
|
$imageField = null;
|
||||||
* Returns one $config reference or the full $config
|
foreach ($hasOneFields as $field => $type) {
|
||||||
*
|
if ($type === 'Image' || $type === 'File' || is_subclass_of($type, 'File')) {
|
||||||
* @param string $reference $congif parameter to return
|
$imageField = $field;
|
||||||
* @return mixed
|
break;
|
||||||
*/
|
}
|
||||||
function getConfig ( $reference = false )
|
}
|
||||||
{
|
|
||||||
if ( $reference ) return $this->config[$reference];
|
|
||||||
else return $this->config;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
return $imageField;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns one $ufConfig reference or the full config.
|
* 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 string $reference $ufConfig parameter to return
|
*
|
||||||
* @return mixed
|
* @return string
|
||||||
*/
|
*/
|
||||||
function getUfConfig ( $reference = false )
|
public function getFileRelationName($gridField)
|
||||||
{
|
{
|
||||||
if ( $reference ) return $this->ufConfig[$reference];
|
$configFileRelationName = $this->getConfig('fileRelationName');
|
||||||
else return $this->ufConfig;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
return $configFileRelationName ? $configFileRelationName : $this->getDefaultFileRelationName($gridField);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns one $ufSetup reference or the full config.
|
* Return the ClassName of the fileRelation
|
||||||
*
|
* i.e. 'MyImage' => 'Image' will return 'Image'
|
||||||
* @param string $reference $ufSetup parameter to return
|
* i.e. 'MyImage' => 'File' will return 'File'.
|
||||||
* @return mixed
|
*
|
||||||
*/
|
* @return string file relation className
|
||||||
function getUfSetup ( $reference = false )
|
*/
|
||||||
{
|
public function getFileRelationClassName($gridField)
|
||||||
if ( $reference ) return $this->ufSetup[$reference];
|
{
|
||||||
else return $this->ufSetup;
|
$recordClass = $gridField->list->dataClass;
|
||||||
}
|
$hasOneFields = Config::inst()->get($recordClass, 'has_one', Config::INHERITED);
|
||||||
|
|
||||||
|
$fieldName = $this->getFileRelationName($gridField);
|
||||||
|
if ($fieldName) {
|
||||||
|
return $hasOneFields[$fieldName];
|
||||||
|
} else {
|
||||||
|
return 'File';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns one $ufValidatorSetup reference or the full config.
|
* Returned a configured UploadField instance
|
||||||
*
|
* embedded in the gridfield heard.
|
||||||
* @param string $reference $ufValidatorSetup parameter to return
|
*
|
||||||
* @return mixed
|
* @param GridField $gridField Current GridField
|
||||||
*/
|
*
|
||||||
function getUfValidatorSetup ( $reference = false )
|
* @return UploadField Configured UploadField instance
|
||||||
{
|
*/
|
||||||
if ( $reference ) return $this->ufValidatorSetup[$reference];
|
public function bulkUploadField($gridField)
|
||||||
else return $this->ufValidatorSetup;
|
{
|
||||||
}
|
$fileRelationName = $this->getFileRelationName($gridField);
|
||||||
|
$uploadField = UploadField::create($fileRelationName, '')
|
||||||
|
->setForm($gridField->getForm())
|
||||||
|
|
||||||
|
->setConfig('previewMaxWidth', 20)
|
||||||
|
->setConfig('previewMaxHeight', 20)
|
||||||
|
->setConfig('changeDetection', false)
|
||||||
|
|
||||||
/**
|
->setRecord(DataObject::create()) // avoid UploadField to get auto-config from the Page (e.g fix allowedMaxFileNumber)
|
||||||
* Get the first has_one Image/File relation from the GridField managed DataObject
|
|
||||||
* i.e. 'MyImage' => 'Image' will return 'MyImage'
|
|
||||||
*
|
|
||||||
* @return string Name of the $has_one relation
|
|
||||||
*/
|
|
||||||
public function getDefaultFileRelationName($gridField)
|
|
||||||
{
|
|
||||||
$recordClass = $gridField->list->dataClass;
|
|
||||||
$hasOneFields = Config::inst()->get($recordClass, 'has_one', Config::INHERITED);
|
|
||||||
|
|
||||||
$imageField = null;
|
->setTemplate('GridFieldBulkUploadField')
|
||||||
foreach( $hasOneFields as $field => $type )
|
->setDownloadTemplateName('colymba-bulkuploaddownloadtemplate')
|
||||||
{
|
|
||||||
if( $type === 'Image' || $type === 'File' || is_subclass_of($type, 'File') )
|
|
||||||
{
|
|
||||||
$imageField = $field;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $imageField;
|
->setConfig('url', $gridField->Link('bulkupload/upload'))
|
||||||
}
|
->setConfig('urlSelectDialog', $gridField->Link('bulkupload/select'))
|
||||||
|
->setConfig('urlAttach', $gridField->Link('bulkupload/attach'))
|
||||||
|
->setConfig('urlFileExists', $gridField->Link('bulkupload/fileexists'))
|
||||||
/**
|
;
|
||||||
* Returns the name of the Image/File field name from the managed record
|
|
||||||
* Either as set in the component config or the default one
|
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function getFileRelationName($gridField)
|
|
||||||
{
|
|
||||||
$configFileRelationName = $this->getConfig('fileRelationName');
|
|
||||||
return $configFileRelationName ? $configFileRelationName : $this->getDefaultFileRelationName($gridField);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the ClassName of the fileRelation
|
|
||||||
* i.e. 'MyImage' => 'Image' will return 'Image'
|
|
||||||
* i.e. 'MyImage' => 'File' will return 'File'
|
|
||||||
*
|
|
||||||
* @return string file relation className
|
|
||||||
*/
|
|
||||||
public function getFileRelationClassName($gridField)
|
|
||||||
{
|
|
||||||
$recordClass = $gridField->list->dataClass;
|
|
||||||
$hasOneFields = Config::inst()->get($recordClass, 'has_one', Config::INHERITED);
|
|
||||||
|
|
||||||
$fieldName = $this->getFileRelationName($gridField);
|
|
||||||
if($fieldName)
|
|
||||||
{
|
|
||||||
return $hasOneFields[$fieldName];
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
return 'File';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returned a configured UploadField instance
|
|
||||||
* embedded in the gridfield heard
|
|
||||||
* @param GridField $gridField Current GridField
|
|
||||||
* @return UploadField Configured UploadField instance
|
|
||||||
*/
|
|
||||||
public function bulkUploadField($gridField)
|
|
||||||
{
|
|
||||||
$fileRelationName = $this->getFileRelationName($gridField);
|
|
||||||
$uploadField = UploadField::create($fileRelationName, '')
|
|
||||||
->setForm($gridField->getForm())
|
|
||||||
|
|
||||||
->setConfig('previewMaxWidth', 20)
|
|
||||||
->setConfig('previewMaxHeight', 20)
|
|
||||||
->setConfig('changeDetection', false)
|
|
||||||
|
|
||||||
->setRecord(DataObject::create()) // avoid UploadField to get auto-config from the Page (e.g fix allowedMaxFileNumber)
|
|
||||||
|
|
||||||
->setTemplate('GridFieldBulkUploadField')
|
|
||||||
->setDownloadTemplateName('colymba-bulkuploaddownloadtemplate')
|
|
||||||
|
|
||||||
->setConfig('url', $gridField->Link('bulkupload/upload'))
|
|
||||||
->setConfig('urlSelectDialog', $gridField->Link('bulkupload/select'))
|
|
||||||
->setConfig('urlAttach', $gridField->Link('bulkupload/attach'))
|
|
||||||
->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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* **********************************************************************
|
||||||
|
* GridField_HTMLProvider
|
||||||
|
* */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* HTML to be embedded into the GridField.
|
||||||
|
*
|
||||||
|
* @param GridField $gridField
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getHTMLFragments($gridField)
|
||||||
|
{
|
||||||
|
// permission check
|
||||||
|
if (!singleton($gridField->getModelClass())->canEdit()) {
|
||||||
|
return array();
|
||||||
|
}
|
||||||
|
|
||||||
/* **********************************************************************
|
// check BulkManager exists
|
||||||
* GridField_HTMLProvider
|
$bulkManager = $gridField->getConfig()->getComponentsByType('GridFieldBulkManager');
|
||||||
* */
|
|
||||||
|
|
||||||
/**
|
// upload management buttons
|
||||||
* HTML to be embedded into the GridField
|
$finishButton = FormAction::create('Finish', _t('GRIDFIELD_BULK_UPLOAD.FINISH_BTN_LABEL', 'Finish'))
|
||||||
*
|
->addExtraClass('bulkUploadFinishButton')
|
||||||
* @param GridField $gridField
|
->setAttribute('data-icon', 'accept')
|
||||||
* @return array
|
->setUseButtonTag(true);
|
||||||
*/
|
|
||||||
public function getHTMLFragments($gridField)
|
|
||||||
{
|
|
||||||
// permission check
|
|
||||||
if( !singleton($gridField->getModelClass())->canEdit() )
|
|
||||||
{
|
|
||||||
return array();
|
|
||||||
}
|
|
||||||
|
|
||||||
// check BulkManager exists
|
$clearErrorButton = FormAction::create('ClearError', _t('GRIDFIELD_BULK_UPLOAD.CLEAR_ERROR_BTN_LABEL', 'Clear errors'))
|
||||||
$bulkManager = $gridField->getConfig()->getComponentsByType('GridFieldBulkManager');
|
->addExtraClass('bulkUploadClearErrorButton')
|
||||||
|
->setAttribute('data-icon', 'arrow-circle-double')
|
||||||
|
->setUseButtonTag(true);
|
||||||
|
|
||||||
// upload management buttons
|
if (count($bulkManager)) {
|
||||||
$finishButton = FormAction::create('Finish', _t('GRIDFIELD_BULK_UPLOAD.FINISH_BTN_LABEL', 'Finish'))
|
$cancelButton = FormAction::create('Cancel', _t('GRIDFIELD_BULK_UPLOAD.CANCEL_BTN_LABEL', 'Cancel'))
|
||||||
->addExtraClass('bulkUploadFinishButton')
|
->addExtraClass('bulkUploadCancelButton ss-ui-action-destructive')
|
||||||
->setAttribute('data-icon', 'accept')
|
->setAttribute('data-icon', 'decline')
|
||||||
->setUseButtonTag(true);
|
->setAttribute('data-url', $gridField->Link('bulkupload/cancel'))
|
||||||
|
->setUseButtonTag(true);
|
||||||
|
|
||||||
$clearErrorButton = FormAction::create('ClearError', _t('GRIDFIELD_BULK_UPLOAD.CLEAR_ERROR_BTN_LABEL', 'Clear errors'))
|
$bulkManager_config = $bulkManager->first()->getConfig();
|
||||||
->addExtraClass('bulkUploadClearErrorButton')
|
$bulkManager_actions = $bulkManager_config['actions'];
|
||||||
->setAttribute('data-icon', 'arrow-circle-double')
|
if (array_key_exists('bulkedit', $bulkManager_actions)) {
|
||||||
->setUseButtonTag(true);
|
$editAllButton = FormAction::create('EditAll', _t('GRIDFIELD_BULK_UPLOAD.EDIT_ALL_BTN_LABEL', 'Edit all'))
|
||||||
|
|
||||||
if ( count($bulkManager) )
|
|
||||||
{
|
|
||||||
$cancelButton = FormAction::create('Cancel', _t('GRIDFIELD_BULK_UPLOAD.CANCEL_BTN_LABEL', 'Cancel'))
|
|
||||||
->addExtraClass('bulkUploadCancelButton ss-ui-action-destructive')
|
|
||||||
->setAttribute('data-icon', 'decline')
|
|
||||||
->setAttribute('data-url', $gridField->Link('bulkupload/cancel'))
|
|
||||||
->setUseButtonTag(true);
|
|
||||||
|
|
||||||
$bulkManager_config = $bulkManager->first()->getConfig();
|
|
||||||
$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')
|
->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 = '';
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else{
|
$cancelButton = '';
|
||||||
$cancelButton = '';
|
$editAllButton = '';
|
||||||
$editAllButton = '';
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// get uploadField + inject extra buttons
|
// get uploadField + inject extra buttons
|
||||||
$uploadField = $this->bulkUploadField($gridField);
|
$uploadField = $this->bulkUploadField($gridField);
|
||||||
$uploadField->FinishButton = $finishButton;
|
$uploadField->FinishButton = $finishButton;
|
||||||
$uploadField->CancelButton = $cancelButton;
|
$uploadField->CancelButton = $cancelButton;
|
||||||
$uploadField->EditAllButton = $editAllButton;
|
$uploadField->EditAllButton = $editAllButton;
|
||||||
$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('GridFieldBulkUpload'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* **********************************************************************
|
||||||
|
* GridField_URLHandler
|
||||||
|
* */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Component URL handlers.
|
||||||
|
*
|
||||||
|
* @param GridField $gridField
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getURLHandlers($gridField)
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
'bulkupload' => 'handleBulkUpload',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/* **********************************************************************
|
/**
|
||||||
* GridField_URLHandler
|
* Pass control over to the RequestHandler.
|
||||||
* */
|
*
|
||||||
|
* @param GridField $gridField
|
||||||
|
* @param SS_HTTPRequest $request
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function handleBulkUpload($gridField, $request)
|
||||||
|
{
|
||||||
|
$controller = $gridField->getForm()->Controller();
|
||||||
|
$handler = new GridFieldBulkUpload_Request($gridField, $this, $controller);
|
||||||
|
|
||||||
/**
|
return $handler->handleRequest($request, DataModel::inst());
|
||||||
* Component URL handlers
|
}
|
||||||
*
|
|
||||||
* @param GridField $gridField
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getURLHandlers($gridField) {
|
|
||||||
return array(
|
|
||||||
'bulkupload' => 'handleBulkUpload'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Pass control over to the RequestHandler
|
|
||||||
*
|
|
||||||
* @param GridField $gridField
|
|
||||||
* @param SS_HTTPRequest $request
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function handleBulkUpload($gridField, $request)
|
|
||||||
{
|
|
||||||
$controller = $gridField->getForm()->Controller();
|
|
||||||
$handler = new GridFieldBulkUpload_Request($gridField, $this, $controller);
|
|
||||||
|
|
||||||
return $handler->handleRequest($request, DataModel::inst());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,305 +1,295 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* Handles request from the GridFieldBulkUpload component
|
* Handles request from the GridFieldBulkUpload component.
|
||||||
*
|
*
|
||||||
* @author colymba
|
* @author colymba
|
||||||
* @package GridFieldBulkEditingTools
|
|
||||||
* @subpackage BulkUpload
|
|
||||||
*/
|
*/
|
||||||
class GridFieldBulkUpload_Request extends RequestHandler
|
class GridFieldBulkUpload_Request extends RequestHandler
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Gridfield instance
|
* Gridfield instance.
|
||||||
* @var GridField
|
*
|
||||||
*/
|
* @var GridField
|
||||||
protected $gridField;
|
*/
|
||||||
|
protected $gridField;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bulk upload component.
|
||||||
|
*
|
||||||
|
* @var GridFieldBulkUpload
|
||||||
|
*/
|
||||||
|
protected $component;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Bulk upload component
|
* Gridfield Form controller.
|
||||||
* @var GridFieldBulkUpload
|
*
|
||||||
*/
|
* @var Controller
|
||||||
protected $component;
|
*/
|
||||||
|
protected $controller;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RequestHandler allowed actions.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private static $allowed_actions = array(
|
||||||
|
'upload', 'select', 'attach', 'fileexists',
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gridfield Form controller
|
* RequestHandler url => action map.
|
||||||
* @var Controller
|
*
|
||||||
*/
|
* @var array
|
||||||
protected $controller;
|
*/
|
||||||
|
private static $url_handlers = array(
|
||||||
|
'$Action!' => '$Action',
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handler's constructor.
|
||||||
|
*
|
||||||
|
* @param GridFIeld $gridField
|
||||||
|
* @param GridField_URLHandler $component
|
||||||
|
* @param Controller $controller
|
||||||
|
*/
|
||||||
|
public function __construct($gridField, $component, $controller)
|
||||||
|
{
|
||||||
|
$this->gridField = $gridField;
|
||||||
|
$this->component = $component;
|
||||||
|
$this->controller = $controller;
|
||||||
|
parent::__construct();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* RequestHandler allowed actions
|
* Return the original component's UploadField.
|
||||||
* @var array
|
*
|
||||||
*/
|
* @return UploadField UploadField instance as defined in the component
|
||||||
private static $allowed_actions = array(
|
*/
|
||||||
'upload', 'select', 'attach', 'fileexists'
|
public function getUploadField()
|
||||||
);
|
{
|
||||||
|
return $this->component->bulkUploadField($this->gridField);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Process upload through UploadField,
|
||||||
|
* creates new record and link newly uploaded file
|
||||||
|
* adds record to GrifField relation list
|
||||||
|
* and return image/file data and record edit form.
|
||||||
|
*
|
||||||
|
* @param SS_HTTPRequest $request
|
||||||
|
*
|
||||||
|
* @return string json
|
||||||
|
*/
|
||||||
|
public function upload(SS_HTTPRequest $request)
|
||||||
|
{
|
||||||
|
//create record
|
||||||
|
$recordClass = $this->gridField->list->dataClass;
|
||||||
|
$record = Object::create($recordClass);
|
||||||
|
$record->write();
|
||||||
|
|
||||||
/**
|
// passes the current gridfield-instance to a call-back method on the new object
|
||||||
* RequestHandler url => action map
|
$record->extend('onBulkUpload', $this->gridField);
|
||||||
* @var array
|
if ($record->hasMethod('onBulkImageUpload')) {
|
||||||
*/
|
Deprecation::notice('2.0', '"onBulkImageUpload" callback is deprecated, use "onBulkUpload" instead.');
|
||||||
private static $url_handlers = array(
|
$record->extend('onBulkImageUpload', $this->gridField);
|
||||||
'$Action!' => '$Action'
|
}
|
||||||
);
|
|
||||||
|
|
||||||
|
//get uploadField and process upload
|
||||||
|
$uploadField = $this->getUploadField();
|
||||||
|
$uploadField->setRecord($record);
|
||||||
|
|
||||||
/**
|
$fileRelationName = $uploadField->getName();
|
||||||
* Handler's constructor
|
$uploadResponse = $uploadField->upload($request);
|
||||||
*
|
|
||||||
* @param GridFIeld $gridField
|
|
||||||
* @param GridField_URLHandler $component
|
|
||||||
* @param Controller $controller
|
|
||||||
*/
|
|
||||||
public function __construct($gridField, $component, $controller)
|
|
||||||
{
|
|
||||||
$this->gridField = $gridField;
|
|
||||||
$this->component = $component;
|
|
||||||
$this->controller = $controller;
|
|
||||||
parent::__construct();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
//get uploaded File response datas
|
||||||
|
$uploadResponse = Convert::json2array($uploadResponse->getBody());
|
||||||
|
$uploadResponse = array_shift($uploadResponse);
|
||||||
|
|
||||||
/**
|
// Attach the file to record.
|
||||||
* Return the original component's UploadField
|
$record->{"{$fileRelationName}ID"} = $uploadResponse['id'];
|
||||||
*
|
$record->write();
|
||||||
* @return UploadField UploadField instance as defined in the component
|
|
||||||
*/
|
|
||||||
public function getUploadField()
|
|
||||||
{
|
|
||||||
return $this->component->bulkUploadField($this->gridField);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// attached record to gridField relation
|
||||||
|
$this->gridField->list->add($record->ID);
|
||||||
|
|
||||||
/**
|
// JS Template Data
|
||||||
* Process upload through UploadField,
|
$responseData = $this->newRecordJSTemplateData($record, $uploadResponse);
|
||||||
* creates new record and link newly uploaded file
|
|
||||||
* adds record to GrifField relation list
|
|
||||||
* and return image/file data and record edit form
|
|
||||||
*
|
|
||||||
* @param SS_HTTPRequest $request
|
|
||||||
* @return string json
|
|
||||||
*/
|
|
||||||
public function upload(SS_HTTPRequest $request)
|
|
||||||
{
|
|
||||||
//create record
|
|
||||||
$recordClass = $this->gridField->list->dataClass;
|
|
||||||
$record = Object::create($recordClass);
|
|
||||||
$record->write();
|
|
||||||
|
|
||||||
// passes the current gridfield-instance to a call-back method on the new object
|
$response = new SS_HTTPResponse(Convert::raw2json(array($responseData)));
|
||||||
$record->extend("onBulkUpload", $this->gridField);
|
$this->contentTypeNegotiation($response);
|
||||||
if ( $record->hasMethod('onBulkImageUpload') )
|
|
||||||
{
|
|
||||||
Deprecation::notice('2.0', '"onBulkImageUpload" callback is deprecated, use "onBulkUpload" instead.');
|
|
||||||
$record->extend("onBulkImageUpload", $this->gridField);
|
|
||||||
}
|
|
||||||
|
|
||||||
//get uploadField and process upload
|
return $response;
|
||||||
$uploadField = $this->getUploadField();
|
}
|
||||||
$uploadField->setRecord($record);
|
|
||||||
|
|
||||||
$fileRelationName = $uploadField->getName();
|
/**
|
||||||
$uploadResponse = $uploadField->upload($request);
|
* Updates the Upload/Attach response from the UploadField
|
||||||
|
* with the new DataObject records for the JS template.
|
||||||
|
*
|
||||||
|
* @param DataObject $record Newly create DataObject record
|
||||||
|
* @param array $uploadResponse Upload or Attach response from UploadField
|
||||||
|
*
|
||||||
|
* @return array Updated $uploadResponse with $record data
|
||||||
|
*/
|
||||||
|
protected function newRecordJSTemplateData(DataObject &$record, &$uploadResponse)
|
||||||
|
{
|
||||||
|
// 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']);
|
||||||
|
|
||||||
//get uploaded File response datas
|
if ($uploadedFile) {
|
||||||
$uploadResponse = Convert::json2array( $uploadResponse->getBody() );
|
$uploadResponse['name'] = $uploadedFile->Name;
|
||||||
$uploadResponse = array_shift( $uploadResponse );
|
$uploadResponse['url'] = $uploadedFile->getURL();
|
||||||
|
|
||||||
// Attach the file to record.
|
if ($uploadedFile instanceof Image) {
|
||||||
$record->{"{$fileRelationName}ID"} = $uploadResponse['id'];
|
$uploadResponse['thumbnail_url'] = $uploadedFile->CroppedImage(30, 30)->getURL();
|
||||||
$record->write();
|
} else {
|
||||||
|
$uploadResponse['thumbnail_url'] = $uploadedFile->Icon();
|
||||||
|
}
|
||||||
|
|
||||||
// attached record to gridField relation
|
// check if our new record has a Title, if not create one automatically
|
||||||
$this->gridField->list->add($record->ID);
|
$title = $record->getTitle();
|
||||||
|
if (!$title || $title === $record->ID) {
|
||||||
|
if ($record->hasDatabaseField('Title')) {
|
||||||
|
$record->Title = $uploadedFile->Title;
|
||||||
|
$record->write();
|
||||||
|
} elseif ($record->hasDatabaseField('Name')) {
|
||||||
|
$record->Name = $uploadedFile->Title;
|
||||||
|
$record->write();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// JS Template Data
|
// Collect all data for JS template
|
||||||
$responseData = $this->newRecordJSTemplateData($record, $uploadResponse);
|
$return = array_merge($uploadResponse, array(
|
||||||
|
'record' => array(
|
||||||
|
'id' => $record->ID,
|
||||||
|
),
|
||||||
|
));
|
||||||
|
|
||||||
$response = new SS_HTTPResponse(Convert::raw2json(array($responseData)));
|
return $return;
|
||||||
$this->contentTypeNegotiation($response);
|
}
|
||||||
|
|
||||||
return $response;
|
/**
|
||||||
}
|
* Pass select request to UploadField.
|
||||||
|
*
|
||||||
|
* @link UploadField->select()
|
||||||
|
*/
|
||||||
|
public function select(SS_HTTPRequest $request)
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
$uploadField = $this->getUploadField();
|
||||||
|
return $uploadField->handleSelect($request);
|
||||||
|
*/
|
||||||
|
$uploadField = $this->getUploadField();
|
||||||
|
|
||||||
|
return UploadField_SelectHandler::create($this, $uploadField->getFolderName());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates the Upload/Attach response from the UploadField
|
* Pass getRelationAutosetClass request to UploadField
|
||||||
* with the new DataObject records for the JS template
|
* Used by select dialog.
|
||||||
*
|
*
|
||||||
* @param DataObject $record Newly create DataObject record
|
* @link UploadField->getRelationAutosetClass()
|
||||||
* @param array $uploadResponse Upload or Attach response from UploadField
|
*/
|
||||||
* @return array Updated $uploadResponse with $record data
|
public function getRelationAutosetClass($default = 'File')
|
||||||
*/
|
{
|
||||||
protected function newRecordJSTemplateData(DataObject &$record, &$uploadResponse)
|
$uploadField = $this->getUploadField();
|
||||||
{
|
|
||||||
// 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'] );
|
|
||||||
|
|
||||||
if ( $uploadedFile )
|
return $uploadField->getRelationAutosetClass($default);
|
||||||
{
|
}
|
||||||
$uploadResponse['name'] = $uploadedFile->Name;
|
|
||||||
$uploadResponse['url'] = $uploadedFile->getURL();
|
|
||||||
|
|
||||||
if ( $uploadedFile instanceof Image )
|
/**
|
||||||
{
|
* Pass getAllowedMaxFileNumber request to UploadField
|
||||||
$uploadResponse['thumbnail_url'] = $uploadedFile->CroppedImage(30,30)->getURL();
|
* Used by select dialog.
|
||||||
}
|
*
|
||||||
else{
|
* @link UploadField->getAllowedMaxFileNumber()
|
||||||
$uploadResponse['thumbnail_url'] = $uploadedFile->Icon();
|
*/
|
||||||
}
|
public function getAllowedMaxFileNumber()
|
||||||
|
{
|
||||||
|
$uploadField = $this->getUploadField();
|
||||||
|
|
||||||
// check if our new record has a Title, if not create one automatically
|
return $uploadField->getAllowedMaxFileNumber();
|
||||||
$title = $record->getTitle();
|
}
|
||||||
if ( !$title || $title === $record->ID )
|
|
||||||
{
|
|
||||||
if ( $record->hasDatabaseField('Title') )
|
|
||||||
{
|
|
||||||
$record->Title = $uploadedFile->Title;
|
|
||||||
$record->write();
|
|
||||||
}
|
|
||||||
else if ($record->hasDatabaseField('Name')){
|
|
||||||
$record->Name = $uploadedFile->Title;
|
|
||||||
$record->write();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Collect all data for JS template
|
/**
|
||||||
$return = array_merge($uploadResponse, array(
|
* Retrieve Files to be attached
|
||||||
'record' => array(
|
* and generated DataObjects for each one.
|
||||||
'id' => $record->ID
|
*
|
||||||
)
|
* @param SS_HTTPRequest $request
|
||||||
));
|
*
|
||||||
|
* @return SS_HTTPResponse
|
||||||
|
*/
|
||||||
|
public function attach(SS_HTTPRequest $request)
|
||||||
|
{
|
||||||
|
$uploadField = $this->getUploadField();
|
||||||
|
$attachResponses = $uploadField->attach($request);
|
||||||
|
$attachResponses = json_decode($attachResponses->getBody(), true);
|
||||||
|
|
||||||
return $return;
|
$fileRelationName = $uploadField->getName();
|
||||||
}
|
$recordClass = $this->gridField->list->dataClass;
|
||||||
|
$return = array();
|
||||||
|
|
||||||
|
foreach ($attachResponses as $attachResponse) {
|
||||||
|
// create record
|
||||||
|
$record = Object::create($recordClass);
|
||||||
|
$record->write();
|
||||||
|
$record->extend('onBulkUpload', $this->gridField);
|
||||||
|
|
||||||
/**
|
// attach file
|
||||||
* Pass select request to UploadField
|
$record->{"{$fileRelationName}ID"} = $attachResponse['id'];
|
||||||
*
|
$record->write();
|
||||||
* @link UploadField->select()
|
|
||||||
*/
|
|
||||||
public function select(SS_HTTPRequest $request)
|
|
||||||
{
|
|
||||||
/*
|
|
||||||
$uploadField = $this->getUploadField();
|
|
||||||
return $uploadField->handleSelect($request);
|
|
||||||
*/
|
|
||||||
$uploadField = $this->getUploadField();
|
|
||||||
return UploadField_SelectHandler::create($this, $uploadField->getFolderName());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// attached record to gridField relation
|
||||||
|
$this->gridField->list->add($record->ID);
|
||||||
|
|
||||||
/**
|
// JS Template Data
|
||||||
* Pass getRelationAutosetClass request to UploadField
|
$responseData = $this->newRecordJSTemplateData($record, $attachResponse);
|
||||||
* Used by select dialog
|
|
||||||
*
|
|
||||||
* @link UploadField->getRelationAutosetClass()
|
|
||||||
*/
|
|
||||||
public function getRelationAutosetClass($default = 'File')
|
|
||||||
{
|
|
||||||
$uploadField = $this->getUploadField();
|
|
||||||
return $uploadField->getRelationAutosetClass($default);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
// add to returned dataset
|
||||||
|
array_push($return, $responseData);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
$response = new SS_HTTPResponse(Convert::raw2json($return));
|
||||||
* Pass getAllowedMaxFileNumber request to UploadField
|
$this->contentTypeNegotiation($response);
|
||||||
* Used by select dialog
|
|
||||||
*
|
|
||||||
* @link UploadField->getAllowedMaxFileNumber()
|
|
||||||
*/
|
|
||||||
public function getAllowedMaxFileNumber()
|
|
||||||
{
|
|
||||||
$uploadField = $this->getUploadField();
|
|
||||||
return $uploadField->getAllowedMaxFileNumber();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve Files to be attached
|
* Pass fileexists request to UploadField.
|
||||||
* and generated DataObjects for each one
|
*
|
||||||
*
|
* @link UploadField->fileexists()
|
||||||
* @param SS_HTTPRequest $request
|
*/
|
||||||
* @return SS_HTTPResponse
|
public function fileexists(SS_HTTPRequest $request)
|
||||||
*/
|
{
|
||||||
public function attach(SS_HTTPRequest $request)
|
$uploadField = $this->getUploadField();
|
||||||
{
|
|
||||||
$uploadField = $this->getUploadField();
|
|
||||||
$attachResponses = $uploadField->attach($request);
|
|
||||||
$attachResponses = json_decode($attachResponses->getBody(), true);
|
|
||||||
|
|
||||||
$fileRelationName = $uploadField->getName();
|
return $uploadField->fileexists($request);
|
||||||
$recordClass = $this->gridField->list->dataClass;
|
}
|
||||||
$return = array();
|
|
||||||
|
|
||||||
foreach ($attachResponses as $attachResponse)
|
/**
|
||||||
{
|
* @param string $action
|
||||||
// create record
|
*
|
||||||
$record = Object::create($recordClass);
|
* @return string
|
||||||
$record->write();
|
*/
|
||||||
$record->extend("onBulkUpload", $this->gridField);
|
public function Link($action = null)
|
||||||
|
{
|
||||||
|
return Controller::join_links($this->gridField->Link(), '/bulkupload/', $action);
|
||||||
|
}
|
||||||
|
|
||||||
// attach file
|
/**
|
||||||
$record->{"{$fileRelationName}ID"} = $attachResponse['id'];
|
* Sets response 'Content-Type' depending on browser capabilities
|
||||||
$record->write();
|
* e.g. IE needs text/plain for iframe transport
|
||||||
|
* https://github.com/blueimp/jQuery-File-Upload/issues/1795.
|
||||||
// attached record to gridField relation
|
*
|
||||||
$this->gridField->list->add($record->ID);
|
* @param SS_HTTPResponse $response HTTP Response to set content-type on
|
||||||
|
*/
|
||||||
// JS Template Data
|
protected function contentTypeNegotiation(&$response)
|
||||||
$responseData = $this->newRecordJSTemplateData($record, $attachResponse);
|
{
|
||||||
|
if (isset($_SERVER['HTTP_ACCEPT']) && ((strpos($_SERVER['HTTP_ACCEPT'], 'application/json') !== false) || $_SERVER['HTTP_ACCEPT'] === '*/*')) {
|
||||||
// add to returned dataset
|
$response->addHeader('Content-Type', 'application/json');
|
||||||
array_push($return, $responseData);
|
} else {
|
||||||
}
|
$response->addHeader('Content-Type', 'text/plain');
|
||||||
|
}
|
||||||
$response = new SS_HTTPResponse(Convert::raw2json($return));
|
}
|
||||||
$this->contentTypeNegotiation($response);
|
|
||||||
|
|
||||||
return $response;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Pass fileexists request to UploadField
|
|
||||||
*
|
|
||||||
* @link UploadField->fileexists()
|
|
||||||
*/
|
|
||||||
public function fileexists(SS_HTTPRequest $request)
|
|
||||||
{
|
|
||||||
$uploadField = $this->getUploadField();
|
|
||||||
return $uploadField->fileexists($request);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $action
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
public function Link($action = null) {
|
|
||||||
return Controller::join_links($this->gridField->Link(), '/bulkupload/', $action);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets response 'Content-Type' depending on browser capabilities
|
|
||||||
* 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
|
|
||||||
*/
|
|
||||||
protected function contentTypeNegotiation(&$response)
|
|
||||||
{
|
|
||||||
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');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -4,66 +4,63 @@
|
|||||||
* from Transifex data. This tasks assumes that:
|
* from Transifex data. This tasks assumes that:
|
||||||
* - Javascript translations are from the Transifex resource called 'js'
|
* - Javascript translations are from the Transifex resource called 'js'
|
||||||
* - YML translations are from the Transifex resource called 'yml'
|
* - YML translations are from the Transifex resource called 'yml'
|
||||||
* - Transifex AUTH credentials to be saved in $txAuthFile with content {"username": "user", "password": "pwd"}
|
* - Transifex AUTH credentials to be saved in $txAuthFile with content {"username": "user", "password": "pwd"}.
|
||||||
*
|
*
|
||||||
* This is inspired by SilverStripe build tools. Thanks
|
* This is inspired by SilverStripe build tools. Thanks
|
||||||
|
*
|
||||||
* @see https://github.com/silverstripe/silverstripe-buildtools/blob/master/src/GenerateJavascriptI18nTask.php
|
* @see https://github.com/silverstripe/silverstripe-buildtools/blob/master/src/GenerateJavascriptI18nTask.php
|
||||||
*/
|
*/
|
||||||
include_once "phing/Task.php";
|
include_once 'phing/Task.php';
|
||||||
|
|
||||||
// Ignore this file if phing is not installed
|
// Ignore this file if phing is not installed
|
||||||
if(!class_exists('Task')) {
|
if (!class_exists('Task')) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
class BuildTransifexTranslations extends Task
|
class BuildTransifexTranslations extends Task
|
||||||
{
|
{
|
||||||
private $txapi = 'https://www.transifex.com/api/2';
|
private $txapi = 'https://www.transifex.com/api/2';
|
||||||
private $txproject = '';
|
private $txproject = '';
|
||||||
private $txAuthFile = 'transifexAuth.json';
|
private $txAuthFile = 'transifexAuth.json';
|
||||||
private $txAuth = null;
|
private $txAuth = null;
|
||||||
|
|
||||||
private $root = '';
|
private $root = '';
|
||||||
private $jsDir = '/lang/js';
|
private $jsDir = '/lang/js';
|
||||||
private $ymlDir = '/lang';
|
private $ymlDir = '/lang';
|
||||||
|
|
||||||
public function settxapi($txapi)
|
public function settxapi($txapi)
|
||||||
{
|
{
|
||||||
$this->txapi = $txapi;
|
$this->txapi = $txapi;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function settxproject($txproject)
|
public function settxproject($txproject)
|
||||||
{
|
{
|
||||||
$this->txproject = $txproject;
|
$this->txproject = $txproject;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Task init
|
* Task init.
|
||||||
*/
|
*/
|
||||||
public function init()
|
public function init()
|
||||||
{
|
{
|
||||||
$root = realpath(__DIR__ . DIRECTORY_SEPARATOR . '..');
|
$root = realpath(__DIR__.DIRECTORY_SEPARATOR.'..');
|
||||||
$authFile = $root . DIRECTORY_SEPARATOR . $this->txAuthFile;
|
$authFile = $root.DIRECTORY_SEPARATOR.$this->txAuthFile;
|
||||||
|
|
||||||
if ( file_exists($authFile) )
|
if (file_exists($authFile)) {
|
||||||
{
|
$txAuthData = file_get_contents($authFile);
|
||||||
$txAuthData = file_get_contents($authFile);
|
$txAuthData = json_decode($txAuthData);
|
||||||
$txAuthData = json_decode($txAuthData);
|
if ($txAuthData->username && $txAuthData->password) {
|
||||||
if ( $txAuthData->username && $txAuthData->password )
|
$this->txAuth = $txAuthData;
|
||||||
{
|
} else {
|
||||||
$this->txAuth = $txAuthData;
|
throw new BuildException("Transifex credentials malformat. Check your $authFile for 'username' and 'password' keys.");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw new BuildException("Transifex credentials not found. $authFile missing.");
|
||||||
}
|
}
|
||||||
else{
|
|
||||||
throw new BuildException("Transifex credentials malformat. Check your $authFile for 'username' and 'password' keys.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
throw new BuildException("Transifex credentials not found. $authFile missing.");
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->root = $root;
|
$this->root = $root;
|
||||||
$this->jsDir = $root . $this->jsDir;
|
$this->jsDir = $root.$this->jsDir;
|
||||||
$this->ymlDir = $root . $this->ymlDir;
|
$this->ymlDir = $root.$this->ymlDir;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -71,138 +68,120 @@ class BuildTransifexTranslations extends Task
|
|||||||
*/
|
*/
|
||||||
public function main()
|
public function main()
|
||||||
{
|
{
|
||||||
$ch = curl_init();
|
$ch = curl_init();
|
||||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||||
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
|
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
|
||||||
curl_setopt($ch, CURLOPT_USERPWD, $this->txAuth->username . ":" . $this->txAuth->password);
|
curl_setopt($ch, CURLOPT_USERPWD, $this->txAuth->username.':'.$this->txAuth->password);
|
||||||
|
|
||||||
// get resources
|
// get resources
|
||||||
$url = $this->txapi.'/project/'.$this->txproject.'/resources/';
|
$url = $this->txapi.'/project/'.$this->txproject.'/resources/';
|
||||||
curl_setopt($ch, CURLOPT_URL, $url);
|
curl_setopt($ch, CURLOPT_URL, $url);
|
||||||
$resources = curl_exec($ch);
|
$resources = curl_exec($ch);
|
||||||
|
|
||||||
if ( !$resources )
|
if (!$resources) {
|
||||||
{
|
throw new BuildException('Cannot fetch resources');
|
||||||
throw new BuildException("Cannot fetch resources");
|
} else {
|
||||||
}
|
$resources = json_decode($resources);
|
||||||
else{
|
}
|
||||||
$resources = json_decode($resources);
|
|
||||||
}
|
|
||||||
|
|
||||||
// get langs
|
// get langs
|
||||||
$url = $this->txapi.'/project/'.$this->txproject.'/languages/';
|
$url = $this->txapi.'/project/'.$this->txproject.'/languages/';
|
||||||
curl_setopt($ch, CURLOPT_URL, $url);
|
curl_setopt($ch, CURLOPT_URL, $url);
|
||||||
$languages = curl_exec($ch);
|
$languages = curl_exec($ch);
|
||||||
|
|
||||||
if ( !$languages )
|
if (!$languages) {
|
||||||
{
|
throw new BuildException('Cannot fetch languages');
|
||||||
throw new BuildException("Cannot fetch languages");
|
} else {
|
||||||
}
|
$languages = json_decode($languages);
|
||||||
else{
|
}
|
||||||
$languages = json_decode($languages);
|
|
||||||
}
|
|
||||||
|
|
||||||
// clear existing translation files and/or setup folders
|
// clear existing translation files and/or setup folders
|
||||||
$this->resetTranslations();
|
$this->resetTranslations();
|
||||||
|
|
||||||
// add source_language_code to languages list
|
// add source_language_code to languages list
|
||||||
$sourceLangs = array();
|
$sourceLangs = array();
|
||||||
foreach ($resources as $resource)
|
foreach ($resources as $resource) {
|
||||||
{
|
$lang = new StdClass();
|
||||||
$lang = new StdClass();
|
$locale = $resource->source_language_code;
|
||||||
$locale = $resource->source_language_code;
|
$lang->language_code = $locale;
|
||||||
$lang->language_code = $locale;
|
if (!array_key_exists($locale, $sourceLangs)) {
|
||||||
if ( !array_key_exists($locale, $sourceLangs) )
|
$sourceLangs[$locale] = $lang;
|
||||||
{
|
}
|
||||||
$sourceLangs[$locale] = $lang;
|
|
||||||
}
|
}
|
||||||
}
|
$sourceLangs = array_values($sourceLangs);
|
||||||
$sourceLangs = array_values($sourceLangs);
|
$languages = array_merge($languages, $sourceLangs);
|
||||||
$languages = array_merge($languages, $sourceLangs);
|
|
||||||
|
|
||||||
// get each resource translations
|
// get each resource translations
|
||||||
foreach ($resources as $resource)
|
foreach ($resources as $resource) {
|
||||||
{
|
foreach ($languages as $language) {
|
||||||
foreach ($languages as $language)
|
$url = $this->txapi.'/project/'.$this->txproject.'/resource/'.$resource->slug.'/translation/'.$language->language_code;
|
||||||
{
|
curl_setopt($ch, CURLOPT_URL, $url);
|
||||||
$url = $this->txapi.'/project/'.$this->txproject.'/resource/'.$resource->slug.'/translation/'.$language->language_code;
|
$data = curl_exec($ch);
|
||||||
curl_setopt($ch, CURLOPT_URL, $url);
|
if ($data) {
|
||||||
$data = curl_exec($ch);
|
$this->saveTranslation($resource->slug, $language->language_code, $data);
|
||||||
if ( $data )
|
}
|
||||||
{
|
|
||||||
$this->saveTranslation($resource->slug, $language->language_code, $data);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
curl_close($ch);
|
curl_close($ch);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clear any existing translation files
|
* Clear any existing translation files
|
||||||
* and create directory structure if needed
|
* and create directory structure if needed.
|
||||||
*/
|
*/
|
||||||
private function resetTranslations()
|
private function resetTranslations()
|
||||||
{
|
{
|
||||||
if ( file_exists($this->jsDir) )
|
if (file_exists($this->jsDir)) {
|
||||||
{
|
echo "Clearing js translations...\n";
|
||||||
echo "Clearing js translations...\n";
|
$iterator = new GlobIterator($this->jsDir.DIRECTORY_SEPARATOR.'*.js');
|
||||||
$iterator = new GlobIterator($this->jsDir . DIRECTORY_SEPARATOR . '*.js');
|
foreach ($iterator as $fileInfo) {
|
||||||
foreach ($iterator as $fileInfo)
|
if ($fileInfo->isFile()) {
|
||||||
{
|
$del = unlink($fileInfo->getRealPath());
|
||||||
if ( $fileInfo->isFile() )
|
}
|
||||||
{
|
}
|
||||||
$del = unlink($fileInfo->getRealPath());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if ( file_exists($this->ymlDir) )
|
if (file_exists($this->ymlDir)) {
|
||||||
{
|
echo "Clearing yml translations...\n";
|
||||||
echo "Clearing yml translations...\n";
|
$iterator = new GlobIterator($this->ymlDir.DIRECTORY_SEPARATOR.'*.yml');
|
||||||
$iterator = new GlobIterator($this->ymlDir . DIRECTORY_SEPARATOR . '*.yml');
|
foreach ($iterator as $fileInfo) {
|
||||||
foreach ($iterator as $fileInfo)
|
if ($fileInfo->isFile()) {
|
||||||
{
|
$del = unlink($fileInfo->getRealPath());
|
||||||
if ( $fileInfo->isFile() )
|
}
|
||||||
{
|
}
|
||||||
$del = unlink($fileInfo->getRealPath());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if ( !file_exists($this->jsDir) )
|
if (!file_exists($this->jsDir)) {
|
||||||
{
|
echo "Creating js folders...\n";
|
||||||
echo "Creating js folders...\n";
|
mkdir($this->jsDir);
|
||||||
mkdir($this->jsDir);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if ( !file_exists($this->ymlDir) )
|
if (!file_exists($this->ymlDir)) {
|
||||||
{
|
echo "Creating yml folders...\n";
|
||||||
echo "Creating yml folders...\n";
|
mkdir($this->ymlDir);
|
||||||
mkdir($this->ymlDir);
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hook that detect the translation type via resource slug
|
* Hook that detect the translation type via resource slug
|
||||||
* and call corect saving function with data
|
* and call corect saving function with data.
|
||||||
|
*
|
||||||
* @param string $resource Transifex resrouce slug
|
* @param string $resource Transifex resrouce slug
|
||||||
* @param string $locale Transifex locale
|
* @param string $locale Transifex locale
|
||||||
* @param string $data Raw Transifex translation data
|
* @param string $data Raw Transifex translation data
|
||||||
*/
|
*/
|
||||||
private function saveTranslation($resource, $locale, $data)
|
private function saveTranslation($resource, $locale, $data)
|
||||||
{
|
{
|
||||||
if ( !$resource || !$locale || !$data )
|
if (!$resource || !$locale || !$data) {
|
||||||
{
|
return;
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
$data = json_decode($data);
|
$data = json_decode($data);
|
||||||
$translation = rtrim($data->content);
|
$translation = rtrim($data->content);
|
||||||
|
|
||||||
switch ($resource)
|
switch ($resource) {
|
||||||
{
|
|
||||||
case 'js':
|
case 'js':
|
||||||
$this->saveJSTranslation($locale, $translation);
|
$this->saveJSTranslation($locale, $translation);
|
||||||
break;
|
break;
|
||||||
@ -215,24 +194,25 @@ class BuildTransifexTranslations extends Task
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Save a JS translation file
|
* Save a JS translation file
|
||||||
* Uses JSTemplate to fit with SilverStripe requirements
|
* Uses JSTemplate to fit with SilverStripe requirements.
|
||||||
|
*
|
||||||
* @param string $locale Locale code
|
* @param string $locale Locale code
|
||||||
* @param string $json JSON translation key:value
|
* @param string $json JSON translation key:value
|
||||||
*/
|
*/
|
||||||
private function saveJSTranslation($locale, $json)
|
private function saveJSTranslation($locale, $json)
|
||||||
{
|
{
|
||||||
echo "Saving $locale.js\n";
|
echo "Saving $locale.js\n";
|
||||||
file_put_contents(
|
file_put_contents(
|
||||||
$this->jsDir . DIRECTORY_SEPARATOR . $locale . '.js',
|
$this->jsDir.DIRECTORY_SEPARATOR.$locale.'.js',
|
||||||
$this->getBanner('js') .
|
$this->getBanner('js').
|
||||||
str_replace(
|
str_replace(
|
||||||
array(
|
array(
|
||||||
'%TRANSLATIONS%',
|
'%TRANSLATIONS%',
|
||||||
'%LOCALE%'
|
'%LOCALE%',
|
||||||
),
|
),
|
||||||
array(
|
array(
|
||||||
$json,
|
$json,
|
||||||
$locale
|
$locale,
|
||||||
),
|
),
|
||||||
$this->getJSTemplate()
|
$this->getJSTemplate()
|
||||||
)
|
)
|
||||||
@ -240,67 +220,70 @@ class BuildTransifexTranslations extends Task
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Save a YML translation file
|
* Save a YML translation file.
|
||||||
|
*
|
||||||
* @param string $locale Locale code
|
* @param string $locale Locale code
|
||||||
* @param string $yml YML translation
|
* @param string $yml YML translation
|
||||||
*/
|
*/
|
||||||
public function saveYMLTranslation($locale, $yml)
|
public function saveYMLTranslation($locale, $yml)
|
||||||
{
|
{
|
||||||
echo "Saving $locale.yml\n";
|
echo "Saving $locale.yml\n";
|
||||||
|
|
||||||
if ($locale !== 'en')
|
if ($locale !== 'en') {
|
||||||
{
|
$content = $this->getBanner('yml').$yml;
|
||||||
$content = $this->getBanner('yml') . $yml;
|
} else {
|
||||||
}
|
$content = $yml;
|
||||||
else{
|
}
|
||||||
$content = $yml;
|
|
||||||
}
|
|
||||||
|
|
||||||
file_put_contents(
|
file_put_contents(
|
||||||
$this->ymlDir . DIRECTORY_SEPARATOR . $locale . '.yml',
|
$this->ymlDir.DIRECTORY_SEPARATOR.$locale.'.yml',
|
||||||
$content
|
$content
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the commented file banner
|
* Return the commented file banner.
|
||||||
|
*
|
||||||
* @param string $type File type e.g js
|
* @param string $type File type e.g js
|
||||||
|
*
|
||||||
* @return string The commented file banner
|
* @return string The commented file banner
|
||||||
*/
|
*/
|
||||||
private function getBanner($type)
|
private function getBanner($type)
|
||||||
{
|
{
|
||||||
switch ( strtolower($type) )
|
switch (strtolower($type)) {
|
||||||
{
|
|
||||||
case 'yml':
|
case 'yml':
|
||||||
$comment = "#";
|
$comment = '#';
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
$comment = "//";
|
$comment = '//';
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
$banner = <<<TMPL
|
$banner = <<<TMPL
|
||||||
$comment DO NOT MODIFY. Generated by build task.
|
$comment DO NOT MODIFY. Generated by build task.
|
||||||
$comment Contribute here: https://www.transifex.com/projects/p/gridfieldbulkeditingtools/
|
$comment Contribute here: https://www.transifex.com/projects/p/gridfieldbulkeditingtools/
|
||||||
|
|
||||||
TMPL;
|
TMPL;
|
||||||
return $banner;
|
|
||||||
|
return $banner;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the SilverStripe JS lang file template
|
* Return the SilverStripe JS lang file template.
|
||||||
|
*
|
||||||
* @return string The JS file template
|
* @return string The JS file template
|
||||||
*/
|
*/
|
||||||
private function getJSTemplate()
|
private function getJSTemplate()
|
||||||
{
|
{
|
||||||
$tmpl = <<<TMPL
|
$tmpl = <<<TMPL
|
||||||
if(typeof(ss) == 'undefined' || typeof(ss.i18n) == 'undefined') {
|
if(typeof(ss) == 'undefined' || typeof(ss.i18n) == 'undefined') {
|
||||||
if(typeof(console) != 'undefined') console.error('Class ss.i18n not defined');
|
if(typeof(console) != 'undefined') console.error('Class ss.i18n not defined');
|
||||||
} else {
|
} else {
|
||||||
ss.i18n.addDictionary('%LOCALE%', %TRANSLATIONS%);
|
ss.i18n.addDictionary('%LOCALE%', %TRANSLATIONS%);
|
||||||
}
|
}
|
||||||
TMPL;
|
TMPL;
|
||||||
return $tmpl;
|
|
||||||
|
return $tmpl;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user