odea/application/controllers/GeographiqueController.php
2012-05-03 19:06:09 +00:00

112 lines
3.1 KiB
PHP

<?php
class GeographiqueController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
require_once('Scores/Field.php');
$auth = Zend_Auth::getInstance();
$user = $auth->getIdentity();
$field = new Fields();
$this->view->fields = $field;
}
public function resetAction()
{
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender();
require_once('Scores/SessionCiblage.php');
$session = new SessionCiblage();
$session->resetFamille('geographique');
}
public function completedAction()
{
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$request = $this->getRequest();
if($request->getParam('dep')) {
$output = $this->completedDep($request->getParam('q'));
} else if ($request->getParam('reg')) {
$output = $this->completedReg($request->getParam('q'));
} else if($request->getParam('vil')) {
$output = $this->completedVil($request->getParam('q'));
}else if($request->getParam('adr_com')) {
$output = $this->completedVil($request->getParam('q'));
}else if($request->getParam('adr_com_ex')) {
$output = $this->completedVil($request->getParam('q'));
}
echo json_encode($output);
}
protected function completedDep($q)
{
$table = new Application_Model_Departements();
$sql = $table->select()
->where('libdep LIKE "'.$q.'%"');
$result = $table->fetchAll($sql);
foreach ($result as $item) {
$output[] = array(
'label' => $item->libdep . $separator . $item->numdep,
'value' => $item->numdep
);
}
return ($output);
}
protected function completedVil($q)
{
$table = new Application_Model_CodePostaux();
$separator = ' ';
$sql = $table->select()->where('Commune LIKE "'.$q.'%"');
$result = $table->fetchAll($sql);
foreach ($result as $item) {
$output[] = array(
'label' => $item->Commune . $separator . $item->Codepos,
'value' => $item->INSEE
);
}
return ($output);
}
protected function getDeptFromReg($dep)
{
$table = new Application_Model_Departements();
$sql = $table->select()
->where('codeRegionInsee = '.$dep);
$result = $table->fetchAll($sql)->toArray();
$string = '';
foreach ($result as $res) {
$string .= $res['numdep'].',';
}
$string = substr($string, 0, strlen($string)-1);
return ($string);
}
protected function completedReg($q)
{
$table = new Application_Model_Regions();
$separator = ' ';
$sql = $table->select()
->where('NCCENR LIKE "%'.$q.'%" OR REGION LIKE "%'.$q.'%"');
$result = $table->fetchAll($sql);
foreach ($result as $item) {
$output[] = array(
'label' => $item->REGION .' '.$item->NCCENR . $separator . $item->REGION,
'value' => $this->getDeptFromReg($item->REGION)
);
}
return ($output);
}
}