<?php
require_once(KT_LIB_DIR . '/dispatcher.inc.php');

/**
 * Dispatches the the default dashboard configure page
 */
class SetDefaultDashboardDispatcher extends KTAdminDispatcher {

	function do_main(){
		require_once(KT_DIR . "/thirdparty/pear/JSON.php");
		if($_POST['saveDashboard']){ //form submitted
			$this->form_submit();
		}

		$oJSON = new Services_JSON();
			
		//set breadcrumbs
		$this->oPage->setBreadcrumbDetails(_kt('Default dashboard'));
			
		//load avaible dashlets
		$aDashletList = $this->getAllDashlets();

		//load current default state
		$defaultState = $oJSON->decode(KTUtil::getSystemSetting("wemag-dashboard-management-default-dashboard"));
		$defaultStateDashletNameList = array();
		$defaultStateDashletNameList['left'] = array();
		$defaultStateDashletNameList['right'] = array();


		/**
		 * Loop trough all available dashlets, and figure out what their
		 * position is in the default state. If no default state has been set
		 * put all dashlets in the left column.
		 * Put all results in an array with objects containing title, class and position
		 */
		$disabledDashlets = array();
		$dashlets = array();
		$order = 1;
		if($defaultState!=""){
			foreach($defaultState->left as $dashlet){
				$defaultStateDashletNameList['left'][] = $dashlet->id;
				$d = new stdClass();
				$d->sTitle = $aDashletList[$dashlet->id]->sTitle;
				$d->id = $dashlet->id;
				if($dashlet->state == 2){
					$disabledDashlets[] = $d;
					$d->order = 0;
					$d->disabled = "checked='checked'";
				}else{
					$d->left = "checked='checked'";
					$d->order = $order;
					$dashlets[] = $d;
					$order++;
				}	
			}
			
			$order=1;
			foreach($defaultState->right as $dashlet){
				$defaultStateDashletNameList['right'][] = $dashlet->id;
				$d = new stdClass();
				$d->sTitle = $aDashletList[$dashlet->id]->sTitle;
				$d->id = $dashlet->id;
				if($dashlet->state == 2){
					$disabledDashlets[] = $d;
					$d->order = 0;
					$d->disabled = "checked='checked'";
				}else{
					$d->order = $order;
					$d->right = "checked='checked'";
					$dashlets[] = $d;
					$order++;
				}		
			}
			$dashlets = array_merge($dashlets,$disabledDashlets);
		}
		
		//find out if there are any dashlets that have not been added to the default state 
		//yet, and mark them as disabled. 
		foreach($aDashletList as $dashlet){
			if(array_search(get_class($dashlet),$defaultStateDashletNameList['left'])=== false && 
			array_search(get_class($dashlet),$defaultStateDashletNameList['right'])=== false ){
				$d = new stdClass();
				$d->sTitle = $dashlet->sTitle;
				$d->id = get_class($dashlet);
				$d->order = 0;
				$d->disabled = "checked='checked'";
				$dashlets[] = $d;
			}
		}
			
		//render template
		$oTemplating =& KTTemplating::getSingleton();
		$oTemplate = $oTemplating->loadTemplate("defaultDashboard");
		return $oTemplate->render(array('aDashlets' => $dashlets));
	}

	/**
	 * Retrieves all dashlets from the dashletregistry.
	 * We can't use the registry's built-in function for this
	 * because it checks which dashlets are actually available to a user,
	 * and we simply want to list all.
	 *
	 * @return array with dashlet objects
	 */
	function getAllDashlets(){
		$dashletRegistry = & KTDashletRegistry::getSingleton();
		$aDashlets = array();
		$oRegistry =& KTPluginRegistry::getSingleton();
		// probably not the _best_ way to do things.
		foreach ($dashletRegistry->nsnames as $aPortlet) {
			$name = $aPortlet[0];
			$filename = $aPortlet[1];
			$sPluginName = $aPortlet[3];

			require_once($aPortlet[1]);
			$oPlugin =& $oRegistry->getPlugin($sPluginName);

			$oDashlet =& new $name;
			$oDashlet->setPlugin($oPlugin);
			if(get_class($oDashlet)!='ResetDashboardDashlet'){
				$aDashlets[get_class($oDashlet)] = $oDashlet;
			}
		}

		return $aDashlets;
	}

	/**
	 * Handle the form submit
	 */
	function form_submit(){
		$oJSON = new Services_JSON();
		$dashlets = $_POST['dashlets'];
		$dashlet_order = $_POST['dashlet_order'];
		//order the dashlets
		asort($dashlet_order);
		//move disabled dashlets (order 0), to the end
		$dashlets_end = array();
		foreach($dashlet_order as $dashlet => $order){
			if($order == 0){
				$dashlets_end[$dashlet] = $order;
				array_shift($dashlet_order);
			}else{
				break;
			}
		}
		$dashlet_order = array_merge($dashlet_order,$dashlets_end);		
			
		/**
		 * Loop trough all submitted dashlets, and build a usuable
		 * array which we can encode to JSON.
		 */
		$column = 0;
		$aDashlets = new stdClass();
		$aDashlets->left = array();
		$aDashlets->right = array();
		foreach($dashlet_order as $sDashlet => $order){
			$position = $dashlets[$sDashlet];
			$dashlet = new stdClass();
			$dashlet->id=$sDashlet;
			if($position == "disabled"){
				$dashlet->state = 2;
			}else{
				$dashlet->state = 0;
			}
			if($position=="left" || ($position=="disabled") && $column==0){
				$aDashlets->left[]=$dashlet;
				$column=1;
			}else{
				$aDashlets->right[]=$dashlet;
				$column=0;
			}
		}
		//Make sure that the keys are nice in order so they wont appear in the JSON
		$aDashlets->right = array_merge(array(),$aDashlets->right);
		$aDashlets->left = array_merge(array(), $aDashlets->left);
		
		//encode result into JSON and save it to the database
		$state = $oJSON->encode($aDashlets);
		KTUtil::setSystemSetting("wemag-dashboard-management-default-dashboard",$state);
		$this->addInfoMessage(_kt("Default dashboard saved"));
	}

}

?>