Skip to content

Commit

Permalink
Migrate NewsCategory to new QueryBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
allejo committed Feb 6, 2018
1 parent 6e44652 commit 1f1c57c
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 127 deletions.
7 changes: 4 additions & 3 deletions controllers/NewsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ public function deleteAction(Player $me, News $article)

private function getCategories()
{
return $this->getQueryBuilder('NewsCategory')
->sortBy('name')
->getModels();
return NewsCategory::getQueryBuilder()
->orderBy('name')
->getModels(true)
;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

use Phinx\Migration\AbstractMigration;

class NewsCategoryStatusColumnConversion extends AbstractMigration
{
public function up()
{
$newsCategoryTable = $this->table('news_categories');
$newsCategoryTable
->addColumn('is_read_only', 'boolean', [
'after' => 'protected',
'null' => false,
'default' => false,
'comment' => 'When set to true, no new articles should be able to use this category'
])
->addColumn('is_deleted', 'boolean', [
'after' => 'is_read_only',
'null' => false,
'default' => false,
'comment' => 'Whether or not the news category has been soft deleted',
])
->changeColumn('protected', 'boolean', [
'default' => false,
'null' => false,
'comment' => 'When set to true, prevents the category from being deleted from the UI',
])
->update()
;

$this->query("UPDATE news_categories SET is_deleted = 1 WHERE status = 'deleted';");

$newsCategoryTable
->removeColumn('status')
->renameColumn('protected', 'is_protected')
->update()
;
}

public function down()
{
$newsCategoryTable = $this->table('news_categories');
$newsCategoryTable
->addColumn('status', 'set', [
'values' => ['enabled', 'disabled', 'deleted'],
'after' => 'is_deleted',
'null' => false,
'default' => 'enabled',
'comment' => 'The status of the news element',
])
->renameColumn('is_protected', 'protected')
->update()
;

$this->query("UPDATE news_categories SET status = 'deleted' WHERE is_deleted = 1;");

$newsCategoryTable
->removeColumn('is_deleted')
->removeColumn('is_read_only')
->update()
;
}
}
120 changes: 51 additions & 69 deletions models/NewsCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/**
* @TODO Create permissions for creating, editing, and modifying categories
* @TODO Set up methods to modify the News Categories
*/

/**
Expand All @@ -16,17 +17,15 @@
*/
class NewsCategory extends AliasModel
{
/**
* Whether or not the category is protected from being deleted
* @var bool
*/
protected $protected;
/** @var bool Whether or not the category is protected from being deleted from the UI */
protected $is_protected;

/** @var bool When set to true, no new articles can be assigned this category */
protected $is_read_only;

const DEFAULT_STATUS = 'enabled';

/**
* The name of the database table used for queries
*/
const DELETED_COLUMN = 'is_deleted';
const TABLE = "news_categories";

/**
Expand All @@ -36,58 +35,33 @@ protected function assignResult($category)
{
$this->alias = $category['alias'];
$this->name = $category['name'];
$this->protected = $category['protected'];
$this->status = $category['status'];
$this->is_protected = $category['is_protected'];
$this->is_deleted = $category['is_deleted'];
}

/**
* Delete a category. Only delete a category if it is not protected
*
* @throws DeletionDeniedException
* @throws Exception
*/
public function delete()
{
// Get any articles using this category
$articles = News::fetchIdsFrom("category", $this->getId());

// Only delete a category if it is not protected and is not being used
if (!$this->isProtected() && count($articles) == 0) {
parent::delete();
}
}
$hasArticles = (bool) News::getQueryBuilder()
->where('category', '=', $this->getId())
->active()
->count()
;

/**
* Disable the category
*
* @return void
*/
public function disableCategory()
{
if ($this->getStatus() != "disabled") {
$this->status = "disabled";
$this->update("status", "disabled");
if ($hasArticles) {
throw new DeletionDeniedException('This category has news articles and cannot be deleted.');
}
}

/**
* Enable the category
*
* @return void
*/
public function enableCategory()
{
if ($this->getStatus() != "enabled") {
$this->status = "enabled";
$this->update("status", "enabled");
if ($this->isProtected()) {
throw new DeletionDeniedException('This category is protected and cannot be deleted.');
}
}

/**
* Get the status of the category
*
* @return string Either 'enabled', 'disabled', or 'deleted'
*/
public function getStatus()
{
return $this->status;
parent::delete();
}

/**
Expand Down Expand Up @@ -119,13 +93,23 @@ public function getNews($start = 0, $limit = 5, $getDrafts = false)
}

/**
* Check if the category is protected from being deleted
* Check if the category is protected from being deleted.
*
* @return bool Whether or not the category is protected
*/
public function isProtected()
{
return (bool) $this->protected;
return (bool) $this->is_protected;
}

/**
* Check if new News article can be assigned this category.
*
* @return bool
*/
public function isReadOnly()
{
return (bool) $this->is_read_only;
}

/**
Expand All @@ -138,41 +122,39 @@ public function isProtected()
public static function addCategory($name)
{
return self::create(array(
'alias' => self::generateAlias($name),
'name' => $name,
'protected' => 0,
'status' => 'enabled'
'alias' => self::generateAlias($name),
'name' => $name,
));
}

/**
* Get all of the categories for the news
*
* @throws Exception
*
* @return NewsCategory[] An array of categories
*/
public static function getCategories()
{
return self::arrayIdToModel(
self::fetchIdsFrom(
"status", array("deleted"), true,
"ORDER BY name ASC"
)
);
return self::getQueryBuilder()
->orderBy('name', 'ASC')
->active()
->getModels(true)
;
}

/**
* Get a query builder for news categories
* @return QueryBuilder
* Get a query builder for news categories.
*
* @throws Exception
*
* @return QueryBuilderFlex
*/
public static function getQueryBuilder()
{
return new QueryBuilder('NewsCategory', array(
'columns' => array(
'name' => 'name',
'status' => 'status'
),
'name' => 'name',
));
return QueryBuilderFlex::createForModel(NewsCategory::class)
->setNameColumn('name')
;
}

/**
Expand Down
9 changes: 9 additions & 0 deletions src/Exception/DeletionDeniedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

/**
* Exception thrown when something is attempted to be deleted from the database but permission was denied.
*/
class DeletionDeniedException extends RuntimeException
{

}
Loading

0 comments on commit 1f1c57c

Please sign in to comment.