diff --git a/composer.json b/composer.json index 0a089c2..362b0fa 100644 --- a/composer.json +++ b/composer.json @@ -27,7 +27,8 @@ "silverware/colorpicker": "^1.0", "silverware/font-icons": "^1.0", "silverware/select2": "^1.0", - "silverware/theme": "^1.0" + "silverware/theme": "^1.0", + "symbiote/silverstripe-gridfieldextensions": "3.0.x-dev" }, "autoload": { "psr-4": { diff --git a/src/Forms/GridField/GridFieldConfig_MultiClassEditor.php b/src/Forms/GridField/GridFieldConfig_MultiClassEditor.php new file mode 100644 index 0000000..e951540 --- /dev/null +++ b/src/Forms/GridField/GridFieldConfig_MultiClassEditor.php @@ -0,0 +1,103 @@ +=5.6.0 + * + * For full copyright and license information, please view the + * LICENSE.md file that was distributed with this source code. + * + * @package SilverWare\Forms\GridField + * @author Colin Tucker + * @copyright 2017 Praxis Interactive + * @license https://opensource.org/licenses/BSD-3-Clause BSD-3-Clause + * @link https://github.com/praxisnetau/silverware + */ + +namespace SilverWare\Forms\GridField; + +use SilverStripe\Core\ClassInfo; +use SilverStripe\Forms\GridField\GridFieldAddNewButton; +use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor; +use SilverWare\Tools\ClassTools; +use Symbiote\GridFieldExtensions\GridFieldAddNewMultiClass; +use Symbiote\GridFieldExtensions\GridFieldOrderableRows; + +/** + * An extension of the grid field config record editor class for a multi-class editor config. + * + * @package SilverWare\Forms\GridField + * @author Colin Tucker + * @copyright 2017 Praxis Interactive + * @license https://opensource.org/licenses/BSD-3-Clause BSD-3-Clause + * @link https://github.com/praxisnetau/silverware + */ +class GridFieldConfig_MultiClassEditor extends GridFieldConfig_RecordEditor +{ + /** + * Constructs the object upon instantiation. + * + * @param integer $itemsPerPage + */ + public function __construct($itemsPerPage = null) + { + // Construct Parent: + + parent::__construct($itemsPerPage); + + // Construct Object: + + $this->addComponents( + new GridFieldOrderableRows(), + new GridFieldAddNewMultiClass() + )->removeComponentsByType(GridFieldAddNewButton::class); + + // Apply Extensions: + + $this->extend('updateConfig'); + } + + /** + * Defines the classes that can be created using the grid field. + * + * @param array $classes Class names optionally mapped to titles. + * @param string $default Optional default class name. + * + * @return $this + */ + public function setClasses($classes, $default = null) + { + if ($component = $this->getComponentByType(GridFieldAddNewMultiClass::class)) { + $component->setClasses($classes, $default); + } + + return $this; + } + + /** + * Defines the creatable classes as descendants of the specified class (does not include the specified class). + * + * @param string $class Parent class name. + * @param string $default Optional default class name. + * + * @return $this + */ + public function useDescendantsOf($class, $default = null) + { + return $this->setClasses(array_keys(ClassTools::singleton()->getDescendantsOf($class)), $default); + } + + /** + * Defines the creatable classes as subclasses of the specified class (includes the specified class). + * + * @param string $class Parent class name. + * @param string $default Optional default class name. + * + * @return $this + */ + public function useSubclassesOf($class, $default = null) + { + return $this->setClasses(array_keys(ClassInfo::subclassesFor($class)), $default); + } +} diff --git a/src/Forms/GridField/GridFieldConfig_OrderableEditor.php b/src/Forms/GridField/GridFieldConfig_OrderableEditor.php new file mode 100644 index 0000000..6285252 --- /dev/null +++ b/src/Forms/GridField/GridFieldConfig_OrderableEditor.php @@ -0,0 +1,53 @@ +=5.6.0 + * + * For full copyright and license information, please view the + * LICENSE.md file that was distributed with this source code. + * + * @package SilverWare\Forms\GridField + * @author Colin Tucker + * @copyright 2017 Praxis Interactive + * @license https://opensource.org/licenses/BSD-3-Clause BSD-3-Clause + * @link https://github.com/praxisnetau/silverware + */ + +namespace SilverWare\Forms\GridField; + +use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor; +use Symbiote\GridFieldExtensions\GridFieldOrderableRows; + +/** + * An extension of the grid field config record editor class for an orderable editor config. + * + * @package SilverWare\Forms\GridField + * @author Colin Tucker + * @copyright 2017 Praxis Interactive + * @license https://opensource.org/licenses/BSD-3-Clause BSD-3-Clause + * @link https://github.com/praxisnetau/silverware + */ +class GridFieldConfig_OrderableEditor extends GridFieldConfig_RecordEditor +{ + /** + * Constructs the object upon instantiation. + * + * @param integer $itemsPerPage + */ + public function __construct($itemsPerPage = null) + { + // Construct Parent: + + parent::__construct($itemsPerPage); + + // Construct Object: + + $this->addComponent(new GridFieldOrderableRows()); + + // Apply Extensions: + + $this->extend('updateConfig'); + } +} diff --git a/src/Tools/ClassTools.php b/src/Tools/ClassTools.php index 05d3ef3..f4f43fe 100644 --- a/src/Tools/ClassTools.php +++ b/src/Tools/ClassTools.php @@ -22,6 +22,7 @@ use SilverStripe\Core\Convert; use SilverStripe\Core\Injector\Injectable; use SilverStripe\Core\Injector\Injector; +use SilverStripe\Core\Manifest\ClassLoader; use SilverStripe\ORM\HiddenClass; use ReflectionException; @@ -80,6 +81,18 @@ public function getReverseAncestry($nameOrObject) return array_reverse(ClassInfo::ancestry($nameOrObject)); } + /** + * Answers an array containing the descendants of the specified class name. + * + * @param string $name Name of parent class. + * + * @return array + */ + public function getDescendantsOf($name) + { + return ClassLoader::inst()->getManifest()->getDescendantsOf($name); + } + /** * Answers the visible subclasses of the specified class name. *