<?php
class BdfController extends Zend_Controller_Action
{

	public function indexAction()
	{
		$request = $this->getRequest();

		//Type de module
		$module  = $request->getParam('module', '');
		$siret   = $request->getParam('siret', '');
		$req     = $request->getParam('req', '');
		$denom   = $request->getParam('denom', '');
		$type    = $request->getParam('type', '');
		$code    = $request->getParam('code', '');
		$rechet  = $request->getParam('rechet', '');
		$ape     = $request->getParam('ape', '');
		$service = $request->getParam('service', '');

		if ($siret != '' && $req != '' && substr($siret, 0, 9) != $req) {
			$siret = '';
		} else if (substr($siret, 0, 9) == $req || empty($req)) {
			$req = substr($siret, 0, 9);
		}

		if (is_array($module)){
			$session = new Zend_Session_Namespace('BDF');
			$session->module = $module;
		}

		//Titre
		$title = 'Banque De France - '.strtoupper($service);
		if ($siret == '') {
			$title .= ' - '.$req;
		} else {
			$title .= substr($siren,0,9);
		}
		$this->view->headTitle()->prepend('Banque de France - '.$titre);


		require_once 'Scores/Bdf.php';
		$bdf = new BDF();

		$this->view->assign('siret', $siret);
		$this->view->assign('req', $req);
		$this->view->assign('module', $session->module);

		//Liste module FIBEN
		$listModulesFiben = $bdf->bdf_modules_fiben();
		$this->view->assign('listModulesFiben', $listModulesFiben);

		//Liste module FCC
		$listModulesFcc = $bdf->bdf_modules_fcc();
		$this->view->assign('listModulesFcc', $listModulesFcc);

	}

	public function moduleAction()
	{
		$request = $this->getRequest();

		//Type de module
		$module  = $request->getParam('bdfmodule', '');
		$siret   = $request->getParam('siret', '');
		$req     = $request->getParam('req', '');
		$denom   = $request->getParam('denom', '');
		$type    = $request->getParam('type', 'u');
		$code    = $request->getParam('code', '');
		$rechet  = $request->getParam('rechet', '');
		$ape     = $request->getParam('ape', '');
		$service = $request->getParam('service', '');

		if ($siret != '' && $req != '' && substr($siret, 0, 9) != $req) {
			$siret = '';
		} else if (substr($siret, 0, 9) == $req || empty($req)) {
			$req = substr($siret, 0, 9);
		}

		Zend_Registry::get('firebug')->info($module);

		$content = array();

		require_once 'Scores/Bdf.php';
		$bdf = new BDF();

		//Mode multi module
		if ($type=='u') {
			if (is_array($module)) {
				foreach($module as $m) {
					if (array_key_exists($m, $bdf->bdf_modules_fiben())){
						$service = 'fiben';
					} elseif ($service=='ficp' && array_key_exists($m, $bdf->bdf_modules_ficp())){
						$service = 'ficp';
					} elseif (array_key_exists($m, $bdf->bdf_modules_fcc())){
						$service = 'fcc';
					}
					$func_module = 'bdf_modules_'.$service;
					$listModules = $bdf->{$func_module}();
					$retour['html'] = $bdf->displayModule($req, $m, $service, $listModules);
					$retour['titre'] = $req.' - Module '.$listModules[$m]['titre'];
					$content[] = $retour;
				}
			} else {
				if (array_key_exists($module, $bdf->bdf_modules_fiben())){
					$service = 'fiben';
				} elseif ($service=='ficp' && array_key_exists($module, $bdf->bdf_modules_ficp())){
					$service = 'ficp';
				} elseif (array_key_exists($module, $bdf->bdf_modules_fcc())){
					$service = 'fcc';
				}
				$func_module = 'bdf_modules_'.$service;
				$listModules = $bdf->{$func_module}();
				$content[]['html'] = $bdf->displayModule($req, $module, $service, $listModules);
				$content[]['titre'] = $req.' - Module '.$listModules[$module]['titre'];
			}
		}
		//Mode multi-identifiant
		elseif ($type=='m') {
			if (is_array($module))
			{
				foreach($module as $m)
				{
					if (array_key_exists($module, $bdf->bdf_modules_fiben())){
						$service = 'fiben';
					} elseif ($service=='ficp' && array_key_exists($module, $bdf->bdf_modules_ficp())){
						$service = 'ficp';
					} elseif (array_key_exists($module, $bdf->bdf_modules_fcc())){
						$service = 'fcc';
					}
					$func_module = 'bdf_modules_'.$service;
					$listModules = $bdf->{$func_module}();
					$content[]['html'] = $bdf->displayModule($req, $module, $service, $listModules);
					$content[]['titre'] = 'Module '.$listModules[$module]['titre'];
				}
			}
		}
		$this->view->assign('content', $content);
	}

	public function module27Action()
	{
		$request = $this->getRequest();
		$params['siret'] = $request->getParam('siret');
		$params['bdfmodule'] = array(27);
		$this->_forward('module', null, null, $params);
	}

	public function module28Action()
	{
		$request = $this->getRequest();
		$params['siret'] = $request->getParam('siret');
		$params['bdfmodule'] = array(28);
		$this->_forward('module', null, null, $params);
	}

	public function module29Action()
	{
		$request = $this->getRequest();
		$params['siret'] = $request->getParam('siret');
		$params['bdfmodule'] = array(29);
		$this->_forward('module', null, null, $params);
	}

	public function module40Action()
	{
		$request = $this->getRequest();
		$params['siret'] = $request->getParam('siret');
		$params['bdfmodule'] = array(40);
		$this->_forward('module', null, null, $params);
	}

	public function module51Action()
	{
		$request = $this->getRequest();
		$params['siret'] = $request->getParam('siret');
		$params['bdfmodule'] = array(51);
		$this->_forward('module', null, null, $params);
	}


}