<?php

class DownloadWidget extends Widget {

	public static $db = array(
		"WidgetTitle" => "Varchar(255)",
        "FileTitle" => "Varchar(255)",
        "WidgetText" => "Text",
		"PageID" => "Int",
        "FileID" => "Int"
	);
	
	public static $defaults = array(
		"WidgetTitle" => 'Title',
        "FileTitle" => 'Download now',
		"PageID" => 0
	);
    
    public static $has_one = array(
	);
    
    
    public static $title = "";
	public static $cmsTitle = "Download Box Widget";
	public static $description = "Download Box Widget for Frontpage";

	public function getCMSFields() {
	
        //Get list of files
		$obj = 'File';
        $filter = "ClassName = 'File'";
		$sort = 'Filename ASC';
		$join = '';
		$limit = '';
		$records = DataObject::get($obj, $filter, $sort, $join, $limit);
		
		//add each file title into a dropdown menu along with it's file ID
		$dropDownArray = array();
        $dropDownArray['0'] = _t('DownloadWidget.ChooseFile', '--Choose File--');
		if($records && $records->exists()) foreach( $records as $record ) {
			$dropDownArray[$record->ID] = $record->Filename;
		}
        
		return new FieldSet(
			new TextField("WidgetTitle", _t('DownloadWidget.Widget_Title', 'Block Title')),
            new TextareaField("WidgetText", _t('DownloadWidget.Widget_Text', 'Text')),
            new TextField("FileTitle", _t('DownloadWidget.File_Title', 'File Title')),
            new DropdownField(
			   $name = "FileID",
			   $title = _t('DownloadWidget.SelectFile', 'Select File'),
			   $source = $dropDownArray
			)   
		);
	}
    
    public function getDownloadFile() {
        $obj = 'File';
		$id = $this->FileID;
		$record = DataObject::get_by_id($obj,$id);
        return $record;
        
    }
}
