2016-11-30 22:07:07 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\Forms\GridField;
|
|
|
|
|
2017-01-10 05:57:54 +01:00
|
|
|
use SilverStripe\Core\Injector\Injectable;
|
|
|
|
use SilverStripe\Forms\Form;
|
|
|
|
use SilverStripe\View\ArrayData;
|
|
|
|
use SilverStripe\View\SSViewer;
|
2016-11-30 22:07:07 +01:00
|
|
|
|
|
|
|
class GridFieldImportButton implements GridField_HTMLProvider
|
|
|
|
{
|
2017-01-10 05:57:54 +01:00
|
|
|
use Injectable;
|
|
|
|
|
2016-11-30 22:07:07 +01:00
|
|
|
/**
|
|
|
|
* Fragment to write the button to
|
|
|
|
*/
|
|
|
|
protected $targetFragment;
|
|
|
|
|
|
|
|
/**
|
2017-01-10 05:57:54 +01:00
|
|
|
* Import form
|
|
|
|
*
|
|
|
|
* @var Form
|
|
|
|
*/
|
|
|
|
protected $importForm;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
2016-11-30 22:07:07 +01:00
|
|
|
*/
|
2017-01-10 05:57:54 +01:00
|
|
|
protected $modalTitle = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* URL for iframe
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $importIframe = null;
|
2016-11-30 22:07:07 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $targetFragment The HTML fragment to write the button into
|
|
|
|
*/
|
2017-01-10 05:57:54 +01:00
|
|
|
public function __construct($targetFragment = "after")
|
2016-11-30 22:07:07 +01:00
|
|
|
{
|
|
|
|
$this->targetFragment = $targetFragment;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Place the export button in a <p> tag below the field
|
|
|
|
*
|
|
|
|
* @param GridField $gridField
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getHTMLFragments($gridField)
|
|
|
|
{
|
2017-01-10 05:57:54 +01:00
|
|
|
$modalID = $gridField->ID() . '_ImportModal';
|
|
|
|
|
|
|
|
// Check for form message prior to rendering form (which clears session messages)
|
|
|
|
$form = $this->getImportForm();
|
|
|
|
$hasMessage = $form && $form->getMessage();
|
|
|
|
|
|
|
|
// Render modal
|
|
|
|
$template = SSViewer::get_templates_by_class(static::class, '_Modal');
|
|
|
|
$viewer = new ArrayData([
|
|
|
|
'ImportModalTitle' => $this->getModalTitle(),
|
|
|
|
'ImportModalID' => $modalID,
|
|
|
|
'ImportIframe' => $this->getImportIframe(),
|
|
|
|
'ImportForm' => $this->getImportForm(),
|
|
|
|
]);
|
|
|
|
$modal = $viewer->renderWith($template)->forTemplate();
|
|
|
|
|
|
|
|
// Build action button
|
2016-11-30 22:07:07 +01:00
|
|
|
$button = new GridField_FormAction(
|
|
|
|
$gridField,
|
|
|
|
'import',
|
2017-04-20 03:59:51 +02:00
|
|
|
_t('SilverStripe\\Forms\\GridField\\GridField.CSVIMPORT', 'Import CSV'),
|
2016-11-30 22:07:07 +01:00
|
|
|
'import',
|
|
|
|
null
|
|
|
|
);
|
|
|
|
$button
|
2017-06-23 00:10:01 +02:00
|
|
|
->addExtraClass('btn btn-secondary font-icon-upload btn--icon-large action_import')
|
2017-01-10 05:57:54 +01:00
|
|
|
->setForm($gridField->getForm())
|
|
|
|
->setAttribute('data-toggle', 'modal')
|
|
|
|
->setAttribute('aria-controls', $modalID)
|
|
|
|
->setAttribute('data-target', "#{$modalID}")
|
|
|
|
->setAttribute('data-modal', $modal);
|
2016-11-30 22:07:07 +01:00
|
|
|
|
2017-01-10 05:57:54 +01:00
|
|
|
// If form has a message, trigger it to automatically open
|
|
|
|
if ($hasMessage) {
|
|
|
|
$button->setAttribute('data-state', 'open');
|
|
|
|
}
|
2016-11-30 22:07:07 +01:00
|
|
|
|
|
|
|
return array(
|
2017-09-05 05:08:05 +02:00
|
|
|
$this->targetFragment => $button->Field()
|
2016-11-30 22:07:07 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* export is an action button
|
|
|
|
*
|
|
|
|
* @param GridField $gridField
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getActions($gridField)
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
2017-01-10 05:57:54 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getModalTitle()
|
|
|
|
{
|
|
|
|
return $this->modalTitle;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $modalTitle
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setModalTitle($modalTitle)
|
|
|
|
{
|
|
|
|
$this->modalTitle = $modalTitle;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Form
|
|
|
|
*/
|
|
|
|
public function getImportForm()
|
|
|
|
{
|
|
|
|
return $this->importForm;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Form $importForm
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setImportForm($importForm)
|
|
|
|
{
|
|
|
|
$this->importForm = $importForm;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getImportIframe()
|
|
|
|
{
|
|
|
|
return $this->importIframe;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $importIframe
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setImportIframe($importIframe)
|
|
|
|
{
|
|
|
|
$this->importIframe = $importIframe;
|
|
|
|
return $this;
|
|
|
|
}
|
2016-11-30 22:07:07 +01:00
|
|
|
}
|