/** * Handles the add action for the given DataObject. * * @param $gridField GridField * @param $actionName string * @param $arguments mixed * @param $data array * * @return null|SS_HTTPResponse * * @throws UnexpectedValueException */ public function handleAction(GridField $gridField, $actionName, $arguments, $data) { if ($actionName == 'add') { $dbField = $this->getDataObjectField(); $objClass = $gridField->getModelClass(); /** * @var DataObject $obj */ $obj = new $objClass(); if ($obj->hasField($dbField)) { $obj->setCastedField($dbField, $data['gridfieldaddbydbfield'][$obj->ClassName][$dbField]); if ($obj->canCreate()) { $id = $gridField->getList()->add($obj); if (!$id) { $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('GridFieldAddByDBField.PermissionFail', 'You don\'t have permission to create a {class}.', 'Unable to add the DataObject.', array('class' => get_class($obj)))); } } else { throw new UnexpectedValueException(sprintf('Invalid field (%s) on %s.', $dbField, $obj->ClassName)); } } return null; }
/** * Handles the add action for the given DataObject * * @param $gridFIeld GridFIeld * @param $actionName string * @param $arguments mixed * @param $data array **/ public function handleAction(GridField $gridField, $actionName, $arguments, $data) { if ($actionName == "add") { $dbField = $this->getDataObjectField(); $objClass = $gridField->getModelClass(); $obj = new $objClass(); if ($obj->hasField($dbField)) { $obj->setCastedField($dbField, $data['gridfieldaddbydbfield'][$obj->ClassName][$dbField]); if ($obj->canCreate()) { $id = $gridField->getList()->add($obj); if (!$id) { $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("GridFieldAddByDBField.PermissionFail", "You don't have permission to create a {class}.", "Unable to add the DataObject.", array("class" => get_class($obj)))); } } else { throw new UnexpectedValueException("Invalid field (" . $dbField . ") on " . $obj->ClassName . "."); } } }
/** * Handles the add action for the given DataObject * * @param $gridFIeld GridFIeld * @param $actionName string * @param $arguments mixed * @param $data array **/ public function handleAction(GridField $gridField, $actionName, $arguments, $data) { if ($actionName == "add") { // Get our submitted fields and object class $dbField = $this->getDataObjectField(); $objClass = $gridField->getModelClass(); $source_class = $this->getSourceClass(); $filter = array(); // Has the user used autocomplete if (isset($data['relationID']) && $data['relationID']) { $id = $data['relationID']; } else { $id = null; } $obj = new $objClass(); // Is this a valid field if (!$obj->hasField($dbField)) { throw new UnexpectedValueException("Invalid field (" . $dbField . ") on " . $obj->ClassName . "."); } // If we have an ID try and get an existing object then // check if we have a copy in items if ($id) { $source_item = $source_class::get()->byID($id); foreach ($this->getFilterFields() as $filter_field) { $filter[$filter_field] = $source_item->{$filter_field}; } } else { // Generate the filter we need to use $string = $data['gridfieldaddbydbfield']; foreach ($this->getFilterFields() as $filter_field) { $filter[$filter_field] = $string; } } // First check if we already have an object or if we need to // create one $existing_obj = $gridField->getList()->filterAny($filter)->first(); if ($existing_obj) { $obj = $existing_obj; } if ($obj->ID && $obj->canEdit()) { // An existing record and can edit, update quantity $curr_qty = $obj->Quantity ? $obj->Quantity : 0; $obj->setCastedField("Quantity", $curr_qty + 1); $id = $gridField->getList()->add($obj); } if (!$obj->ID && $obj->canCreate()) { // If source item not set, try and get one or get a // an existing record if (!$source_item) { $source_item = $source_class::get()->filterAny($filter)->first(); } if ($source_item) { foreach ($this->getSourceFields() as $obj_field => $source_field) { $obj->setCastedField($obj_field, $source_item->{$source_field}); } } else { $obj->setCastedField($this->getCreateField(), $string); } $obj->setCastedField("Quantity", 1); $id = $gridField->getList()->add($obj); } if (!$id) { $gridField->setError(_t("GridFieldAddOrderItem.AddFail", "Unable to save {class} to the database.", "Unable to add the DataObject.", array("class" => get_class($obj))), "error"); } // Finally, issue a redirect to update totals $controller = Controller::curr(); $response = $controller->response; $response->addHeader('X-Pjax', 'Content'); $response->addHeader('X-Reload', true); return $controller->redirect($gridField->getForm()->controller->Link(), 302); } }