silverstripe-blog/code/forms/gridfield/GridFieldAddByDBField.php

186 lines
4.1 KiB
PHP
Raw Normal View History

2013-07-21 12:23:35 +02:00
<?php
/**
* Adds a component which allows a user to add a new DataObject by database field.
*
* @package silverstripe
* @subpackage blog
2015-05-09 16:33:12 +02:00
*/
2013-07-21 12:23:35 +02:00
class GridFieldAddByDBField implements GridField_ActionProvider, GridField_HTMLProvider {
/**
* HTML Fragment to render the field.
*
* @var string
2015-05-09 16:33:12 +02:00
*/
2013-07-21 12:23:35 +02:00
protected $targetFragment;
/**
2015-05-09 16:33:12 +02:00
* Default field to create the DataObject by should be Title.
2013-07-21 12:23:35 +02:00
*
* @var string
2015-05-09 16:33:12 +02:00
*/
protected $dataObjectField = 'Title';
2013-07-21 12:23:35 +02:00
/**
* Creates a text field and add button which allows the user to directly create a new
* DataObject by just entering the title.
*
2015-05-09 16:33:12 +02:00
* @param string $targetFragment
* @param string $dataObjectField
*/
public function __construct($targetFragment = 'before', $dataObjectField = 'Title') {
2013-07-21 12:23:35 +02:00
$this->targetFragment = $targetFragment;
$this->dataObjectField = (string) $dataObjectField;
}
/**
* Provide actions to this component.
*
2015-05-09 16:33:12 +02:00
* @param GridField $gridField
2013-07-21 12:23:35 +02:00
*
* @return array
2015-05-09 16:33:12 +02:00
*/
2013-07-21 12:23:35 +02:00
public function getActions($gridField) {
2015-05-09 16:33:12 +02:00
return array(
'add',
);
2013-07-21 12:23:35 +02:00
}
/**
2015-05-09 16:33:12 +02:00
* Handles the add action for the given DataObject.
2013-07-21 12:23:35 +02:00
*
2015-05-09 16:33:12 +02:00
* @param $gridField GridField
2013-07-21 12:23:35 +02:00
* @param $actionName string
* @param $arguments mixed
* @param $data array
2015-05-09 16:33:12 +02:00
*
* @return null|SS_HTTPResponse
*
* @throws UnexpectedValueException
*/
2013-07-21 12:23:35 +02:00
public function handleAction(GridField $gridField, $actionName, $arguments, $data) {
2015-05-09 16:33:12 +02:00
if($actionName == 'add') {
2013-07-21 12:23:35 +02:00
$dbField = $this->getDataObjectField();
$objClass = $gridField->getModelClass();
2015-05-09 16:33:12 +02:00
2013-07-21 12:23:35 +02:00
$obj = new $objClass();
2015-05-09 16:33:12 +02:00
2013-07-21 12:23:35 +02:00
if($obj->hasField($dbField)) {
$obj->setCastedField($dbField, $data['gridfieldaddbydbfield'][$obj->ClassName][$dbField]);
if($obj->canCreate()) {
$id = $gridField->getList()->add($obj);
if(!$id) {
2015-05-09 16:33:12 +02:00
$gridField->setError(
_t(
'GridFieldAddByDBField.AddFail',
'Unable to save {class} to the database.',
'Unable to add the DataObject.',
array(
'class' => get_class($obj),
)
),
'error'
);
}
} else {
return Security::permissionFailure(
Controller::curr(),
_t(
2015-05-09 16:33:12 +02:00
'GridFieldAddByDBField.PermissionFail',
'You don\'t have permission to create a {class}.',
'Unable to add the DataObject.',
array(
2015-05-09 16:33:12 +02:00
'class' => get_class($obj)
)
)
);
2013-07-21 12:23:35 +02:00
}
} else {
2015-05-09 16:33:12 +02:00
throw new UnexpectedValueException(
sprintf(
'Invalid field (%s) on %s.',
$dbField,
$obj->ClassName
)
);
2013-07-21 12:23:35 +02:00
}
}
2015-05-09 16:33:12 +02:00
return null;
2013-07-21 12:23:35 +02:00
}
2015-05-09 16:33:12 +02:00
/**
* Returns the database field for which we'll add the new data object.
*
* @return string
*/
public function getDataObjectField() {
return $this->dataObjectField;
}
2013-07-21 12:23:35 +02:00
2015-05-09 16:33:12 +02:00
/**
* Set the database field.
*
* @param $field string
*/
public function setDataObjectField($field) {
$this->dataObjectField = (string) $field;
}
2013-07-21 12:23:35 +02:00
/**
* Renders the TextField and add button to the GridField.
*
2015-05-09 16:33:12 +02:00
* @param $gridField GridField
2013-07-21 12:23:35 +02:00
*
2015-05-09 16:33:12 +02:00
* @return string
*/
2013-07-21 12:23:35 +02:00
public function getHTMLFragments($gridField) {
$dataClass = $gridField->getList()->dataClass();
2015-05-09 16:33:12 +02:00
2013-07-21 12:23:35 +02:00
$obj = singleton($dataClass);
2015-05-09 16:33:12 +02:00
if(!$obj->canCreate()) {
return "";
}
2013-07-21 12:23:35 +02:00
$dbField = $this->getDataObjectField();
$textField = TextField::create(
2015-05-09 16:33:12 +02:00
sprintf(
"gridfieldaddbydbfield[%s][%s]",
$obj->ClassName,
Convert::raw2htmlatt($dbField)
)
)
->setAttribute('placeholder', $obj->fieldLabel($dbField))
->addExtraClass('no-change-track');
$addAction = new GridField_FormAction(
$gridField,
'add',
_t('GridFieldAddByDBField.Add',
'Add {name}', "Add button text",
array(
'name' => $obj->i18n_singular_name(),
)
),
'add',
'add'
);
2015-05-09 16:33:12 +02:00
2013-07-21 12:23:35 +02:00
$addAction->setAttribute('data-icon', 'add');
$forTemplate = new ArrayData(array());
2015-05-09 16:33:12 +02:00
$forTemplate->Fields = new ArrayList();
2013-07-21 12:23:35 +02:00
$forTemplate->Fields->push($textField);
$forTemplate->Fields->push($addAction);
return array(
2015-05-09 16:33:12 +02:00
$this->targetFragment => $forTemplate->renderWith('GridFieldAddByDBField')
2013-07-21 12:23:35 +02:00
);
}
2015-05-09 16:33:12 +02:00
}