silverstripe-blog/extensions/BlogPostFilter.php

43 lines
1.3 KiB
PHP
Raw Normal View History

2013-07-21 12:23:35 +02:00
<?php
/**
* This is responsible for filtering only published posts to users who do not have
* permission to view non-published posts.
*
* @package silverstripe
* @subpackage blog
*
* @author Michael Strong <github@michaelstrong.co.uk>
**/
2013-08-04 18:38:26 +02:00
class BlogPostFilter extends DataExtension {
2013-07-21 12:23:35 +02:00
/**
2013-08-04 18:38:26 +02:00
* Augment queries so that we don't fetch unpublished articles.
2013-07-21 12:23:35 +02:00
**/
2013-08-04 18:38:26 +02:00
public function augmentSQL(SQLQuery &$query) {
2013-07-21 12:23:35 +02:00
if(!Permission::check("VIEW_DRAFT_CONTENT")) {
$stage = Versioned::current_stage();
if($stage == "Stage") $stage = "";
else $stage = "_" . Convert::raw2sql($stage);
2013-07-21 12:23:35 +02:00
2013-10-10 00:09:28 +02:00
$query->addWhere("PublishDate < '" . Convert::raw2sql(SS_Datetime::now()) . "'");
}
2013-07-21 12:23:35 +02:00
}
2013-08-23 17:22:13 +02: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
2013-08-23 17:22:13 +02:00
**/
public function augmentLoadLazyFields(SQLQuery &$query, &$dataQuery, $parent) {
// Ensures that we're joining the BlogPost table which holds required db fields.
$dataQuery->innerJoin("BlogPost", "`SiteTree`.`ID` = `BlogPost`.`ID`");
}
2013-07-21 12:23:35 +02:00
}