2015-11-21 07:17:29 +01:00
|
|
|
<?php
|
|
|
|
|
2016-12-15 04:41:49 +01:00
|
|
|
namespace SilverStripe\Blog\Model;
|
|
|
|
|
|
|
|
use SilverStripe\Admin\LeftAndMain;
|
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Core\Convert;
|
|
|
|
use SilverStripe\ORM\DataExtension;
|
2016-12-15 23:57:25 +01:00
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-12-15 04:41:49 +01:00
|
|
|
use SilverStripe\ORM\DataQuery;
|
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime;
|
|
|
|
use SilverStripe\ORM\Queries\SQLSelect;
|
2017-04-18 09:02:04 +02:00
|
|
|
use SilverStripe\Versioned\Versioned;
|
2016-12-15 04:41:49 +01:00
|
|
|
use SilverStripe\Security\Permission;
|
|
|
|
|
2015-11-21 07:17:29 +01:00
|
|
|
/**
|
|
|
|
* This is responsible for filtering only published posts to users who do not have permission to
|
|
|
|
* view non-published posts.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class BlogPostFilter extends DataExtension
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Augment queries so that we don't fetch unpublished articles.
|
|
|
|
*
|
|
|
|
* @param SQLQuery $query
|
|
|
|
*/
|
2016-06-01 07:28:59 +02:00
|
|
|
public function augmentSQL(SQLSelect $query, DataQuery $dataQuery = null)
|
2015-11-21 07:17:29 +01:00
|
|
|
{
|
2016-06-01 07:28:59 +02:00
|
|
|
$stage = Versioned::get_stage();
|
2015-11-21 07:17:29 +01:00
|
|
|
|
2017-07-05 15:49:34 +02:00
|
|
|
if (Controller::has_curr() && Controller::curr() instanceof LeftAndMain) {
|
2015-11-21 07:17:29 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stage == 'Live' || !Permission::check('VIEW_DRAFT_CONTENT')) {
|
2016-06-01 07:28:59 +02:00
|
|
|
$query->addWhere(sprintf(
|
|
|
|
'"PublishDate" < \'%s\'',
|
2016-12-15 04:41:49 +01:00
|
|
|
Convert::raw2sql(DBDatetime::now())
|
2016-06-01 07:28:59 +02:00
|
|
|
));
|
2015-11-21 07:17:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-12-15 04:41:49 +01:00
|
|
|
* {@inheritDoc}
|
|
|
|
*
|
2015-11-21 07:17:29 +01:00
|
|
|
* This is a fix so that when we try to fetch subclasses of BlogPost, lazy loading includes the
|
|
|
|
* BlogPost table in its query. Leaving this table out means the default sort order column
|
|
|
|
* PublishDate causes an error.
|
|
|
|
*
|
|
|
|
* @see https://github.com/silverstripe/silverstripe-framework/issues/1682
|
|
|
|
*
|
2016-12-15 04:41:49 +01:00
|
|
|
* @param SQLSelect $query
|
|
|
|
* @param DataQuery $dataQuery
|
|
|
|
* @param DataObject $dataObject
|
2015-11-21 07:17:29 +01:00
|
|
|
*/
|
2016-12-15 04:41:49 +01:00
|
|
|
public function augmentLoadLazyFields(SQLSelect &$query, DataQuery &$dataQuery = null, $dataObject)
|
|
|
|
{
|
2016-12-15 23:57:25 +01:00
|
|
|
$dataQuery->innerJoin(
|
|
|
|
DataObject::getSchema()->tableName(BlogPost::class),
|
|
|
|
'"SiteTree"."ID" = "BlogPost"."ID"'
|
|
|
|
);
|
2015-11-21 07:17:29 +01:00
|
|
|
}
|
|
|
|
}
|