<?php
	FNTH_class( "Widget" );

	class AdminCheckboxList extends FNTHWidget {
		protected $aCheckboxes = array();

		public function __construct( $id = "FrontendCheckboxList" , $parent = null ) {
			$this->addMediaDirectory( $GLOBALS["site_root"] , $GLOBALS["site_www"] , "design/rp/","widgets/");
			parent::__construct( $id , $parent );
			$this->setParam("language",$GLOBALS["language"]);
			$this->setAttribute( "class" , "FrontendCheckboxList" );
		}

		public function clear() { $this->aCheckbox = array(); }
		public function addCheckbox( $name , $label , $checked = false ) {
			$this->aCheckboxes[$name] = array(
				"name" => $name,
				"label" => $label,
				"checked" => $checked );
		}

		public function setChecked( $name , $checked = true ) {
			$this->aCheckboxes[$name]["checked"]=$checked;
		}

		public function refreshValueFromArray( $arr = null ) {
			if (!isset( $arr )) $arr = $this->aP;
			// clear all
			foreach ($this->aCheckboxes as $name => $chk ) $this->setChecked( $name , false );
			$prefix = $this->getId() . "_";
			foreach ($arr as $k => $v) {
				if (strpos( $k , $prefix )!==0) continue;
				$val = str_replace( $prefix , "" , $k );
				$this->setChecked( $val , true );
			}
		}

		public function getValue() {
			return array_keys( $this->aCheckboxes );
		}

		public function getCheckeds() {
			$retval = array();
			foreach ($this->aCheckboxes as $val => $chk ) if ($chk["checked"]) $retval[]=$val;
			return $retval;
		}

		public function render() {
			if ($this->sRendered) return $this->sRendered;
			$this->setParam( "checkboxes" , $this->aCheckboxes );
			return parent::render();
		}

	}

?>
