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 /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Get all unpublished sitetree objects in modeladmin? [Solved]


Go to End
Reply


5 Posts   1310 Views

Avatar
bartvanirsel

Community Member, 96 Posts

5 July 2013 at 2:15am

Hi,

I need to get an overview of all SiteTree objects which are not Published yet in modeladmin.
Wondering how to do this in 3.1


class UnPublishedPagesAdmin extends ModelAdmin {
    
    public static $managed_models = array('SiteTree'); 
    
    static $url_segment = 'unpublishedpages'; 
    
    static $menu_title = 'Publish content';    
    
    public $showImportForm = false;
    
    public $showExportForm = false;
    
    public function getList() {
        
        $unpublished_site_trees = ArrayList::create('SiteTree');

        $all_site_trees = SiteTree::get();

        foreach ($all_site_trees as $sitetree_obj) {
            if (! $sitetree_obj->isPublished()) {
                $unpublished_site_trees->push($sitetree_obj);
            }
        }

        return $unpublished_site_trees;

        }

    }    
    
}

Above my unsuccesful attempt ... The is not something like ->filter('NotPublished')?

Cheers,

Bart

Avatar
copernican

Community Member, 189 Posts

5 July 2013 at 7:28am

Hey bartvanirsel,

Not the best answer, and I hope someone does come along with a better one but, I usually just fall back and use the Versioned class to get a list of pages by stage. I haven't seen or figured out an elegant way of doing this using DataList::get() yet :(

Avatar
bartvanirsel

Community Member, 96 Posts

8 July 2013 at 9:10pm

Hi IOTI,

Thanks for your tip! It helped me on the way.

I Solved it by doing the following:


<?php 

class UnPublishedPagesAdmin extends ModelAdmin {
	
	public static $managed_models = array('SiteTree'); 
	
	static $url_segment = 'unpublishedpages'; 
	
	static $menu_title = 'Publish content';    
	
	public $showImportForm = false;
	
	public $showExportForm = false;
	
	public function getList() {
		
		$stageRecords = Versioned::get_by_stage('SiteTree', 'Stage');

		$concept_records_ids_array = array();

		foreach ($stageRecords as $rec) {
			if (!$rec->isPublished()) {
				$concept_records_ids_array[] = $rec->ID;
			}
		}

		$sitetree_objects = SiteTree::get()->filter(array(
			'ID' => $concept_records_ids_array
		));

		return $sitetree_objects;
	}    
	
}

Avatar
JonoM

Community Member, 130 Posts

18 October 2013 at 11:16pm

Hi,

I just did something similar to this for 2.4 - below might cause a lot less database queries as I think each isPublished() is a new query. (for 3.1 I guess just change DataObjectSet to DataList)

$draftPages = Versioned::get_by_stage('SiteTree', 'Stage');
$livePages = Versioned::get_by_stage('SiteTree', 'Live');
$newPages = new DataObjectSet();

foreach ($draftPages as $p) {
	if (!$livePages->find('ID',$p->ID)) {
		$newPages->push($p);
	}
}

return $newPages;

Avatar
BenWu

Community Member, 93 Posts

25 January 2014 at 12:13pm

Thanks JonoM,

I use the following to get random products to show on a page :

$randomProducts = Versioned::get_by_stage('ProductPage', 'Live', $filter=' ', ' rand()' , $join='', $limit=12 );