odea/application/controllers/GeographiqueController.php
2012-05-20 17:35:13 +00:00

119 lines
2.8 KiB
PHP

<?php
class GeographiqueController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
$auth = Zend_Auth::getInstance();
$user = $auth->getIdentity();
$field = new Scores_Fields();
$this->view->fields = $field;
}
public function resetAction()
{
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$fields = new Scores_Fields();
$fields->resetFamille('geographique');
}
public function completedAction()
{
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$request = $this->getRequest();
$q = $request->getParam('q');
//Région
$output = $this->completedReg($q);
//Département
if ( count($output)==0 ) {
$output = $this->completedDep($q);
}
//Ville
if ( count($output)==0 ) {
$output = $this->completedVil($q);
}
echo json_encode($output);
}
protected function completedDep($q)
{
$separator = ' , ';
$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' => 'D'.$item->numdep
);
}
return $output;
}
protected function completedVil($q)
{
$separator = ' , ';
$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' => 'C'.$item->INSEE
);
}
return $output;
}
protected function getDeptFromReg($dep)
{
$separator = ' , ';
$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)
{
$separator = ' , ';
$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->NCCENR . $separator . $item->REGION,
'value' => 'R'.$item->REGION
);
}
return $output;
}
}