diff options
-rw-r--r-- | lib/Controller/OptionController.php | 2 | ||||
-rw-r--r-- | lib/Db/Option.php | 8 | ||||
-rw-r--r-- | lib/Db/OptionMapper.php | 3 | ||||
-rw-r--r-- | lib/Migration/Version0103Date20200130171244.php | 91 |
4 files changed, 102 insertions, 2 deletions
diff --git a/lib/Controller/OptionController.php b/lib/Controller/OptionController.php index e0b65840..d6708746 100644 --- a/lib/Controller/OptionController.php +++ b/lib/Controller/OptionController.php @@ -144,6 +144,7 @@ class OptionController extends Controller { $NewOption->setPollId($option['pollId']); $NewOption->setPollOptionText(trim(htmlspecialchars($option['pollOptionText']))); $NewOption->setTimestamp($option['timestamp']); + $NewOption->setOrder($option['order']); $this->mapper->insert($NewOption); $this->logService->setLog($option['pollId'], 'addOption'); @@ -172,6 +173,7 @@ class OptionController extends Controller { $updateOption->setPollOptionText(trim(htmlspecialchars($option['pollOptionText']))); $updateOption->setTimestamp($option['timestamp']); + $updateOption->setOrder($option['order']); $this->mapper->update($updateOption); $this->logService->setLog($option['pollId'], 'updateOption'); diff --git a/lib/Db/Option.php b/lib/Db/Option.php index 31d723c4..9f673725 100644 --- a/lib/Db/Option.php +++ b/lib/Db/Option.php @@ -38,6 +38,8 @@ use OCP\AppFramework\Db\Entity; * @method void setPollOptionText(string $value) * @method integer getTimestamp() * @method void setTimestamp(integer $value) + * @method integer getOrder() + * @method void setOrder(integer $value) */ class Option extends Entity implements JsonSerializable { @@ -50,6 +52,9 @@ class Option extends Entity implements JsonSerializable { /** @var int $timestamp */ protected $timestamp; + /** @var int $order */ + protected $order; + public function jsonSerialize() { if (intval($this->timestamp) > 0) { $timestamp = $this->timestamp; @@ -63,7 +68,8 @@ class Option extends Entity implements JsonSerializable { 'id' => intval($this->id), 'pollId' => intval($this->pollId), 'pollOptionText' => htmlspecialchars_decode($this->pollOptionText), - 'timestamp' => intval($timestamp) + 'timestamp' => intval($timestamp), + 'order' => intval($this->order) ]; } diff --git a/lib/Db/OptionMapper.php b/lib/Db/OptionMapper.php index bf787105..f282c522 100644 --- a/lib/Db/OptionMapper.php +++ b/lib/Db/OptionMapper.php @@ -69,7 +69,8 @@ class OptionMapper extends QBMapper { ->from($this->getTableName()) ->where( $qb->expr()->eq('poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT)) - ); + ) + ->orderBy('order', 'ASC');; return $this->findEntities($qb); } diff --git a/lib/Migration/Version0103Date20200130171244.php b/lib/Migration/Version0103Date20200130171244.php new file mode 100644 index 00000000..69f162b7 --- /dev/null +++ b/lib/Migration/Version0103Date20200130171244.php @@ -0,0 +1,91 @@ +<?php +/** + * @copyright Copyright (c) 2017 René Gieling <github@dartcafe.de> + * + * @author René Gieling <github@dartcafe.de> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\Polls\Migration; + +use Doctrine\DBAL\Types\Type; +use OCP\DB\ISchemaWrapper; +use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\IConfig; +use OCP\IDBConnection; +use OCP\Migration\SimpleMigrationStep; +use OCP\Migration\IOutput; + +/** + * Installation class for the polls app. + * Initial db creation + */ +class Version0103Date20200130171244 extends SimpleMigrationStep { + + /** @var IDBConnection */ + protected $connection; + + /** @var IConfig */ + protected $config; + + /** + * @param IDBConnection $connection + * @param IConfig $config + */ + public function __construct(IDBConnection $connection, IConfig $config) { + $this->connection = $connection; + $this->config = $config; + } + + /** + * @param IOutput $output + * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + * @param array $options + * @return null|ISchemaWrapper + * @since 13.0.0 + */ + public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) { + /** @var ISchemaWrapper $schema */ + $schema = $schemaClosure(); + if ($schema->hasTable('polls_options')) { + $table = $schema->getTable('polls_options'); + if (!$table->hasColumn('order')) { + $table->addColumn('order', Type::INTEGER, [ + 'length' => 11, + 'notnull' => true, + 'default' => 0 + ]); + } + } + + return $schema; + } + + /** + * @param IOutput $output + * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + * @param array $options + * @since 13.0.0 + */ + public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) { + $query = $this->connection->getQueryBuilder(); + $query->update('polls_options') + ->set('order', 'timestamp'); + $query->execute(); + } +} |