Skip to content

Commit

Permalink
WIP for Page models
Browse files Browse the repository at this point in the history
  • Loading branch information
allejo committed Feb 8, 2018
1 parent 7c82d87 commit 4b0f1f7
Show file tree
Hide file tree
Showing 8 changed files with 231 additions and 73 deletions.
4 changes: 3 additions & 1 deletion controllers/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ public function showDefaultAction()

public function showAction(Page $page)
{
return array("page" => $page);
return [
'page' => $page
];
}

public function createAction(Player $me, Request $request)
Expand Down
79 changes: 79 additions & 0 deletions migrations/20180208072640_page_status_conversion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

use Phinx\Migration\AbstractMigration;

class PageStatusConversion extends AbstractMigration
{
public function up()
{
$pagesTable = $this->table('pages');
$pagesTable
->addColumn('is_unlisted', 'boolean', [
'after' => 'home',
'null' => false,
'default' => false,
'comment' => 'Whether or not this page should be listed in the secondary navigation',
])
->addColumn('is_draft', 'boolean', [
'after' => 'is_unlisted',
'null' => false,
'default' => false,
'comment' => 'Whether or not the news article is a draft',
])
->addColumn('is_deleted', 'boolean', [
'after' => 'is_draft',
'null' => false,
'default' => false,
'comment' => 'Whether or not this entry has been soft-deleted',
])
->update()
;

$this->query("UPDATE pages SET is_deleted = 1 WHERE status != 'live';");

$pagesTable
->removeColumn('parent_id')
->removeColumn('home')
->removeColumn('status')
->update()
;
}

public function down()
{
$pagesTable = $this->table('pages');
$pagesTable
->addColumn('parent_id', 'integer', [
'after' => 'id',
'null' => true,
'default' => null,
'length' => 10,
'comment' => 'The ID of the original page. If this column is set, then it is a revision',
])
->addColumn('home', 'integer', [
'after' => 'author',
'length' => 4,
'null' => true,
'default' => null,
'comment' => '(Deprecated) Whether or not the page is the home page',
])
->addColumn('status', 'set', [
'values' => ['live', 'revision', 'disabled', 'deleted'],
'after' => 'home',
'null' => false,
'default' => 'live',
'comment' => 'The status of this page',
])
->update()
;

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

$pagesTable
->removeColumn('is_unlisted')
->removeColumn('is_draft')
->removeColumn('is_deleted')
->update()
;
}
}
102 changes: 47 additions & 55 deletions models/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,13 @@ class Page extends AliasModel
*/
protected $author;

/**
* Whether the page is the home page
* @var bool
*/
protected $home;
protected $is_draft;
protected $is_unlisted;

const DEFAULT_STATUS = 'live';

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

const CREATE_PERMISSION = Permission::CREATE_PAGE;
const EDIT_PERMISSION = Permission::EDIT_PAGE;
Expand All @@ -62,8 +57,9 @@ protected function assignResult($page)
$this->name = $page['name'];
$this->alias = $page['alias'];
$this->author = $page['author'];
$this->home = $page['home'];
$this->status = $page['status'];
$this->is_unlisted = $page['is_unlisted'];
$this->is_draft = $page['is_draft'];
$this->is_deleted = $page['is_deleted'];
}

/**
Expand Down Expand Up @@ -118,24 +114,6 @@ public function getAuthor()
return Player::get($this->author);
}

/**
* Get the status of the page
* @return string
*/
public function getStatus()
{
return $this->status;
}

/**
* Find out whether this is the homepage
* @return bool
*/
public function isHomePage()
{
return $this->home;
}

/**
* Set the content of the page
*
Expand All @@ -147,17 +125,6 @@ public function setContent($content)
return $this->updateProperty($this->content, "content", $content);
}

/**
* Set the status of the page
*
* @param string $status One of "live", "revision" or "disabled"
* @return self
*/
public function setStatus($status)
{
return $this->updateProperty($this->status, "status", $status);
}

/**
* Update the last edit timestamp
* @return self
Expand All @@ -173,20 +140,21 @@ public function updateEditTimestamp()
* @param string $title The title of the page
* @param string $content The content of page
* @param int $authorID The ID of the author
* @param string $status Page status: 'live','disabled',or 'deleted'
* @param bool $is_draft Whether or not
*
* @since 0.11.0 The former enum $status parameter has been changed to the boolean $is_draft
*
* @return Page An object representing the page that was just created
*/
public static function addPage($title, $content, $authorID, $status = "live")
public static function addPage($title, $content, $authorID, $is_draft = false)
{
return self::create(array(
return self::create([
'name' => $title,
'alias' => self::generateAlias($title),
'content' => $content,
'author' => $authorID,
'home' => 0,
'status' => $status,
), array('created', 'updated'));
'is_draft' => (bool)$is_draft
], ['created', 'updated']);
}

/**
Expand Down Expand Up @@ -244,18 +212,42 @@ public static function getLazyColumns()
}

/**
* Get a query builder for pages
* @return QueryBuilder
* {@inheritdoc}
*/
public static function getQueryBuilder()
{
return new QueryBuilder('Page', array(
'columns' => array(
'name' => 'name',
'status' => 'status'
),
'name' => 'name'
));
return QueryBuilderFlex::createForModel(Page::class)
->setNameColumn('name')
;
}

/**
* {@inheritdoc}
*/
public static function getActiveModels(QueryBuilderFlex &$qb)
{
$qb
->whereNot(self::DELETED_COLUMN, '=', self::DELETED_VALUE)
->whereNot('is_draft', '=', true)
;

return true;
}

/**
* {@inheritdoc}
*/
public static function getEagerColumnsList()
{
return [
'id',
'name',
'alias',
'author',
'is_draft',
'is_deleted',
'is_unlisted',
];
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/Form/Creator/PageFormCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ public function enter($form)
return \Page::addPage(
$form->get('name')->getData(),
$form->get('content')->getData(),
$this->me->getId(),
$form->get('status')->getData()
$this->me->getId()
);
}
}
90 changes: 83 additions & 7 deletions src/Model/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,17 +241,17 @@ protected static function chooseModelFromDatabase($id)
*/
protected static function fetchColumnValues($id)
{
$table = static::TABLE;
$columns = static::getEagerColumns();

$results = Database::getInstance()
->query("SELECT $columns FROM $table WHERE id = ? LIMIT 1", array($id));
$qb = QueryBuilderFlex::createForTable(static::TABLE);
$results = $qb
->select(static::getEagerColumnsList())
->find($id)
;

if (count($results) < 1) {
return null;
}

return $results[0];
return $results;
}

/**
Expand Down Expand Up @@ -361,7 +361,7 @@ protected static function fetchIdsFrom($column, $possible_values, $negate = fals
/**
* Get the MySQL columns that will be loaded as soon as the model is created
*
* @todo Make this protected
* @deprecated 0.10.2 Replaced by static::getEagerColumnsList() in 0.11.0
*
* @param string $prefix The prefix that'll be prefixed to column names
*
Expand All @@ -379,6 +379,8 @@ public static function getEagerColumns($prefix = null)
* This is done in order to reduce the time needed to load parameters that
* will not be requested (e.g player activation codes or permissions)
*
* @deprecated 0.10.2 Replaced by static::getLazyColumnsList() in 0.11.0
*
* @return string|null The columns in a format readable by MySQL or null to
* fetch no columns at all
*/
Expand All @@ -390,6 +392,8 @@ protected static function getLazyColumns()
/**
* Get a formatted string with a comma separated column list with table/alias prefixes if necessary.
*
* @deprecated 0.10.2 This function has been removed and is no longer required with the new query builder
*
* @param string|null $prefix The table name or SQL alias to be prepend to these columns
* @param array $columns The columns to format
*
Expand All @@ -404,6 +408,78 @@ protected static function formatColumns($prefix = null, $columns = ['*'])
return (($prefix . '.') . implode(sprintf(',%s.', $prefix), $columns));
}

//
// Query building for models
//

/**
* Get a query builder instance for this model.
*
* @throws BadMethodCallException When this function has not been configured for a particular model
* @throws Exception When no database has been configured for BZiON
*
* @since 0.11.0 The expected return type has been changed from QueryBuilder to QueryBuilderFlex
* @since 0.9.0
*
* @return QueryBuilderFlex
*/
public static function getQueryBuilder()
{
throw new BadMethodCallException(sprintf('No Query Builder has been configured for the %s model', get_called_class()));
}

/**
* Modify a QueryBuilderFlex instance to configure what conditions should be applied when fetching "active" entries.
*
* This function is called whenever a QueryBuilder calls `static::active()`. A reference to the QueryBuilderFlex
* instance is passed to allow you to add any necessary conditions to the query. This function should return true if
* the QueryBuilderInstance has been modified to stop propagation to other modifications; return false if nothing
* has been modified.
*
* @internal For use of QueryBuilderFlex when fetching active entries.
*
* @param \QueryBuilderFlex $qb A reference to the QBF to allow modifications.
*
* @since 0.11.0
*
* @return bool Returns true if propagation should stop and the QueryBuilderFlex should not make further modifications
* in this regard.
*/
public static function getActiveModels(QueryBuilderFlex &$qb)
{
return false;
}

/**
* Get the list of columns that should be eagerly loaded when a model is created from database results.
*
* @since 0.11.0
*
* @return array
*/
public static function getEagerColumnsList()
{
return ['*'];
}

/**
* Get the list of columns that need to be lazily loaded in a model.
*
* This function should return an empty array if no columns need to be lazily loaded.
*
* @since 0.11.0
*
* @return array
*/
public static function getLazyColumnsList()
{
return [];
}

//
// Model creation from the database
//

/**
* Load all the parameters of the model that were not loaded during the first
* fetch from the database
Expand Down
Loading

0 comments on commit 4b0f1f7

Please sign in to comment.