2016-11-30 22:07:07 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\Forms\GridField;
|
|
|
|
|
2017-01-10 05:57:54 +01:00
|
|
|
use SilverStripe\Control\Session;
|
|
|
|
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',
|
|
|
|
_t('TableListField.CSVIMPORT', 'Import CSV'),
|
|
|
|
'import',
|
|
|
|
null
|
|
|
|
);
|
|
|
|
$button
|
2017-01-10 05:57:54 +01:00
|
|
|
->addExtraClass('btn btn-secondary no-ajax font-icon-upload btn--icon-large action_import')
|
|
|
|
->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(
|
2016-12-21 04:23:00 +01:00
|
|
|
$this->targetFragment => '<p class="grid-csv-button">'. $button->Field() . '</p>'
|
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
|
|
|
}
|