diff --git a/.travis.yml b/.travis.yml index 9d45ae8..ab30ad9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,9 +5,9 @@ sudo: false language: php php: - - 5.5 - 5.6 - 7.0 + - 7.1 env: - DB=MYSQL CORE_RELEASE=4 @@ -18,7 +18,9 @@ before_script: - git clone git://github.com/silverstripe/silverstripe-travis-support.git ~/travis-support - php ~/travis-support/travis_setup.php --source `pwd` --target ~/builds/ss - cd ~/builds/ss - - composer install + - composer install --prefer-dist --no-progress --no-suggest + - composer require silverstripe/versioned:1.x-dev --prefer-dist --no-progress --no-suggest + - composer dump-autoload --optimize script: - vendor/bin/phpunit gridfieldextensions/tests diff --git a/src/GridFieldAddNewMultiClass.php b/src/GridFieldAddNewMultiClass.php index b428c46..7aa7f2a 100755 --- a/src/GridFieldAddNewMultiClass.php +++ b/src/GridFieldAddNewMultiClass.php @@ -145,7 +145,12 @@ class GridFieldAddNewMultiClass implements GridField_HTMLProvider, GridField_URL } } - return $result; + $sanitised = array(); + foreach($result as $class=>$title) { + $sanitised[$this->sanitiseClassName($class)] = $title; + } + + return $sanitised; } /** @@ -196,11 +201,12 @@ class GridFieldAddNewMultiClass implements GridField_HTMLProvider, GridField_URL throw new HTTPResponse_Exception(400); } + $unsanitisedClass = $this->unsanitiseClassName($class); $handler = Injector::inst()->create( $this->itemRequestClass, $grid, $component, - new $class(), + new $unsanitisedClass(), $grid->getForm()->getController(), 'add-multi-class' ); @@ -254,4 +260,20 @@ class GridFieldAddNewMultiClass implements GridField_HTMLProvider, GridField_URL $this->itemRequestClass = $class; return $this; } + + /** + * Sanitise a model class' name for inclusion in a link + * @return string + */ + protected function sanitiseClassName($class) { + return str_replace('\\', '-', $class); + } + + /** + * Unsanitise a model class' name from a URL param + * @return string + */ + protected function unsanitiseClassName($class) { + return str_replace('-', '\\', $class); + } } diff --git a/src/GridFieldOrderableRows.php b/src/GridFieldOrderableRows.php index b2ac9e9..2d86084 100755 --- a/src/GridFieldOrderableRows.php +++ b/src/GridFieldOrderableRows.php @@ -476,8 +476,8 @@ class GridFieldOrderableRows extends RequestHandler implements $current = array(); foreach ($items->toArray() as $record) { // NOTE: _SortColumn0 is the first ->sort() field - // used by SS when functions are detected in a SELECT - // or CASE WHEN. + // used by SS when functions are detected in a SELECT + // or CASE WHEN. if (isset($record->_SortColumn0)) { $current[$record->ID] = $record->_SortColumn0; } else { @@ -514,7 +514,16 @@ class GridFieldOrderableRows extends RequestHandler implements // match to order the objects. if (!$isVersioned) { $sortTable = $this->getSortTable($list); - $additionalSQL = (!$list instanceof ManyManyList) ? ', "LastEdited" = NOW()' : ''; + $additionalSQL = ''; + $baseTable = $sortTable; + if(class_exists($sortTable)) { + $baseTable = singleton($sortTable)->baseTable(); + } + $isBaseTable = ($baseTable == $sortTable); + if(!$list instanceof ManyManyList && $isBaseTable){ + $additionalSQL = ', "LastEdited" = NOW()'; + } + foreach ($sortedIDs as $sortValue => $id) { if ($map[$id] != $sortValue) { DB::query(sprintf( @@ -525,6 +534,14 @@ class GridFieldOrderableRows extends RequestHandler implements $additionalSQL, $this->getSortTableClauseForIds($list, $id) )); + + if(!$isBaseTable) { + DB::query(sprintf( + 'UPDATE "%s" SET "LastEdited" = NOW() WHERE %s', + $baseTable, + $this->getSortTableClauseForIds($list, $id) + )); + } } } } else { @@ -550,7 +567,16 @@ class GridFieldOrderableRows extends RequestHandler implements $field = $this->getSortField(); $table = $this->getSortTable($list); $clause = sprintf('"%s"."%s" = 0', $table, $this->getSortField()); - $additionalSQL = (!$list instanceof ManyManyList) ? ', "LastEdited" = NOW()' : ''; + + $additionalSQL = ''; + $baseTable = $table; + if(class_exists($table)) { + $baseTable = singleton($table)->baseTable(); + } + $isBaseTable = ($baseTable == $table); + if(!$list instanceof ManyManyList && $isBaseTable){ + $additionalSQL = ', "LastEdited" = NOW()'; + } foreach ($list->where($clause)->column('ID') as $id) { $max = DB::query(sprintf('SELECT MAX("%s") + 1 FROM "%s"', $field, $table)); @@ -564,6 +590,14 @@ class GridFieldOrderableRows extends RequestHandler implements $additionalSQL, $this->getSortTableClauseForIds($list, $id) )); + + if(!$isBaseTable) { + DB::query(sprintf( + 'UPDATE "%s" SET "LastEdited" = NOW() WHERE %s', + $baseTable, + $this->getSortTableClauseForIds($list, $id) + )); + } } } diff --git a/tests/GridFieldAddNewMultiClassWithNamespacesTest.php b/tests/GridFieldAddNewMultiClassWithNamespacesTest.php new file mode 100644 index 0000000..bb00d5d --- /dev/null +++ b/tests/GridFieldAddNewMultiClassWithNamespacesTest.php @@ -0,0 +1,66 @@ +setModelClass('Symbiote\\Test\\NamespacedClass'); + + $component = new GridFieldAddNewMultiClass(); + + $this->assertEquals( + array( + 'Symbiote-Test-NamespacedClass' => 'NamespacedClass' + ), + $component->getClasses($grid), + 'Namespaced classes are sanitised' + ); + } + + public function testHandleAddWithNamespaces() { + $grid = new GridField('TestGridField'); + $grid->getConfig()->addComponent(new GridFieldDetailForm()); + $grid->setModelClass('Symbiote\\Test\\NamespacedClass'); + $grid->setForm(Form::create(Controller::create(), 'test', FieldList::create(), FieldList::create())); + + $request = new HTTPRequest('POST', 'test'); + $request->setRouteParams(array('ClassName' => 'Symbiote-Test-NamespacedClass')); + + $component = new GridFieldAddNewMultiClass(); + $response = $component->handleAdd($grid, $request); + + $record = new \ReflectionProperty(GridFieldAddNewMultiClassHandler::class, 'record'); + $record->setAccessible(true); + $this->assertInstanceOf('Symbiote\\Test\\NamespacedClass', $record->getValue($response)); + } + +} + +/**#@+ + * @ignore + */ + +class NamespacedClass implements TestOnly { + public function i18n_singular_name() { + return 'NamespacedClass'; + } + + public function canCreate() { + return true; + } +} + +/**#@-*/