<?php

class PageContentWidget extends Widget {

	public static $db = array(
		"WidgetTitle" => "Varchar(255)",
		"PageID" => "Int"
	);
	
	public static $defaults = array(
		"WidgetTitle" => 'Page Content',
		"PageID" => 0
	);
    
    public static $title = "";
	public static $cmsTitle = "Page Content";
	public static $description = "Display other page content in sidebar (only WidgetPage type)";

	public function getCMSFields() {
	
		//Get list of page titles
		$records = DataObject::get('SiteTree', "Status = 'Published' AND ClassName = 'WidgetPage'", 'Title ASC', '', '');
		
		//add each page title into a dropdown menu along with it's page ID
		$dropDownArray = array();
		if($records->exists()) foreach( $records as $record ) {
			$dropDownArray[$record->ID] = $record->Title;
		}
	
		return new FieldSet(
		//	new TextField("WidgetTitle", _t('PageContentWidget.Widget_Title', 'Blokelio pavadinimas')),
			new DropdownField(
			   $name = "PageID",
			   $title = _t('PageContentWidget.Page_Content', 'Puslapis, kurio turinį rodyti:'),
			   $source = $dropDownArray
			)
		);
	}  
	
	public function output() {
		
		$record = DataObject::get_by_id('SiteTree',$this->PageID);
		if($record) 
            return $record->Content();
        return false;
	}
}
