setClasses($classes); $this->setFragment($targetFragment); } /** * Change the button name * * @param string $name * @return $this */ public function setButtonName($name) { $this->buttonName = $name; return $this; } /** * Get the button name * * @return string */ public function getButtonName() { return $this->buttonName; } /** * Gets the fragment name this button is rendered into. * * @return string */ public function getFragment() { return $this->targetFragment; } /** * Sets the fragment name this button is rendered into. * * @param string $fragment * @return GridFieldAddNewInlineButton $this */ public function setFragment($fragment) { $this->targetFragment = $fragment; return $this; } /** * Get extra button class * * @return string */ public function getButtonClass() { return $this->buttonClass; } /** * Sets extra CSS classes for this button * * @param string $buttonClass * @return $this */ public function setButtonClass($buttonClass) { $this->buttonClass = $buttonClass; return $this; } /** * Get the classes of the objects to create * * @return array */ public function getClasses() { return $this->modelClasses; } /** * Gets the list of classes which can be created, with checks for permissions. * Will fallback to the default model class for the given DataGrid * * @param DataGrid $grid * @return array */ public function getClassesCreate($grid) { // Get explicit or fallback class list $classes = $this->modelClasses; if(empty($classes) && $grid) { $classes = array($grid->getModelClass()); } // Filter out classes without permission return array_filter($classes, function($class) { return singleton($class)->canCreate(); }); } /** * Specify the classes to create * * @param array $classes */ public function setClasses($classes) { if(!is_array($classes)) { $classes = $classes ? array($classes) : array(); } $this->modelClasses = $classes; } public function getHTMLFragments($grid) { // Check create permission $singleton = singleton($grid->getModelClass()); if(!$singleton->canCreate()) { return array(); } // Get button name $buttonName = $this->getButtonName(); if(!$buttonName) { // provide a default button name, can be changed by calling {@link setButtonName()} on this component $objectName = $singleton->i18n_singular_name(); $buttonName = _t('GridField.Add', 'Add {name}', array('name' => $objectName)); } $data = new ArrayData(array( 'Title' => $this->getButtonName(), 'ButtonClass' => $this->getButtonClass(), 'Link' => Controller::join_links($grid->Link(), $this->getAction()) )); return array( $this->getFragment() => $data->renderWith(__CLASS__) ); } /** * Get the action suburl for this component * * @return type */ protected function getAction() { $classes = implode('-', $this->getClasses()); return Controller::join_links('add-classes', $classes); } /** * {@inheritDoc} */ public function getURLHandlers($grid) { return array( $this->getAction() => 'handleAdd' ); } /** * Handles adding a new instance of a selected class. * * @param GridField $grid * @param SS_HTTPRequest $request */ public function handleAdd($grid, $request) { $classes = $this->getClassesCreate($grid); if(empty($classes)) { throw new SS_HTTPResponse_Exception(400); } // Add item to gridfield $list = $grid->getList(); foreach($classes as $class) { $item = $class::create(); $item->write(); $list->add($item); } // Return directly to the gridfield again return $grid ->getForm() ->getController() ->redirectBack(); } }