Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Data Model Questions

Using DataObject::get() to list last few updated pages that are publicly visible


Reply

7 Posts   2898 Views

Avatar
HansR

24 May 2009 at 8:48pm Community Member, 140 Posts

Hi, I have a homepage class that displays the 10 most recently updated pages on the website. The problem is, that this includes pages that are not publicly visible. The code is:

function RecentlyUpdated() {
$pages = DataObject::get("Page", "", "LastEdited DESC", "", 10);
$doSet = new DataObjectSet();
foreach($pages as $key => $data) {
$title = $data->Title;
$summary = $data->obj('Content')->FirstParagraph('html');;
$record = array(
'Link' => $data->Link(),
'Title' => $title,
'Date' => date('Y-m-d', strtotime($data->LastEdited)),
'Summary' => $summary
);
$doSet->push(new ArrayData($record));
}
return $doSet;
}

What should I set the DataObject::get()'s parameters to in order to obtain just the publicly visible pages. Actually, I'd like to filter out and Forum page too. I assume that I only need to change the filter string, but I have no idea what to set it to.

Hans

Avatar
NickJacobs

25 May 2009 at 4:29pm Community Member, 144 Posts

Hi HansR,

I use this to only include visible pages:

$pages = DataObject::get("Page", "ShowInMenus = 1", "LastEdited DESC", "", 10);

To filter Forum pages out as well, something like this should work:

$pages = DataObject::get("Page", "ShowInMenus = 1 AND ClassName !=ForumPage", "LastEdited DESC", "", 10);

Avatar
HansR

25 May 2009 at 9:42pm Community Member, 140 Posts

Hi NickJacobs,

Thanks for that. I'm still hoping that someone knows how to get the "show to all" pages, because I might want to include a page in the menu. However, your solution should be fine for now.

Hans

Avatar
Ben Gribaudo

27 May 2009 at 12:28am (Last edited: 27 May 2009 12:28am), Community Member, 181 Posts

Hello,

Have you looked at how SilverStripe does this? You might want to look at methods Children(), AllChildren(), stageChildren() and liveChildren() in [url=http://open.silverstripe.org/browser/modules/sapphire/trunk/core/model/Hierarchy.php]class Hierarchy[/url].

Ben

Avatar
HansR

27 May 2009 at 9:36am Community Member, 140 Posts

Hi Ben,

The methods that you pointed to aren't the best option in this case; the get() method is the right one to find the ten most recently updated public pages. Plus, all of the pages are "live." The difference between pages is that most are "viewable by all," whereas some will be "viewable by group X." So, I'm looking to filter out the pages that have restrictive security settings, and forum pages).

Hans

Avatar
Willr

27 May 2009 at 10:39pm Forum Moderator, 5511 Posts

If you want to check permissions then it would be easier to do this in the ORM. Tricky thing is you might have to get more then 10 as you are removing objects as you go. In this one I get all the pages (just for kicks) then adding to a list, making sure I only get 10.

$pages = DataObject::get("Page", "ShowInMenus = 1 AND ClassName !=ForumPage", "LastEdited DESC");
$output = new DataObjectSet();
if($pages) {
foreach($pages as $page) {
if($page->canView()) { // check we cant view
$output->push($page);
if($output->TotalItems() > 9) break;
}
}
}
return $output;

Avatar
HansR

30 May 2009 at 1:59pm Community Member, 140 Posts

@willr

That almost worked. My code ended up being:

$pages = DataObject::get("Page", "ShowInMenus = 1 AND ClassName != 'Forum'", "LastEdited DESC");
if($pages) {
$output = new DataObjectSet();
foreach($pages as $page) {
if($page->canView()) { // check we cant view
$output->push($page);
if($output->TotalItems() > 9)
break;
}
}
}
return $output;

The one change is ForumPage changed to 'Forum'. Thanks for your help.

Hans