1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
| <?php
class Agences extends Pi_Db_Model
{
protected $_name = 'agences'; // S�lectionne la table
protected $_primary = 'agence_id'; // d�finie la cl� primaire
/*
Fonction qui compte le nombre d'agence
*/
public function getNbAgences() {
$select = $this->getAdapter()->select();
$select->from('agences','count(agence_id) as total');
return $this->getAdapter()->fetchRow($select);
}
public function GetDataAgenceById($id) {
$select = $this->getAdapter()->select();
$select->from($this->_name,'*');
$select->where('agence_id = ?',$id);
return $this->getAdapter()->fetchRow($select);
}
public function GetNextCodeSite($id) {
$select = $this->getAdapter()->select();
$select->from($this->_name,'MAX(`agence_site`) as uppercodesite');
$select->where('cli_id = ?',$id);
// echo $select->__toString();
$result = $this->getAdapter()->fetchRow($select);
return ($result['uppercodesite'] != 0) ? (int)($result['uppercodesite'] + 1) : 1 ;
}
public function getAgenceByCodeSite($cli_id,$code_site) {
$select = $this->getAdapter()->select();
$select->from('agences','*');
$select->where('cli_id = ?',$cli_id);
$select->where('agence_site = ?',$code_site);
return (int) $this->getAdapter()->fetchRow($select);
}
public function getAllAgences($cli_id = null, $cols = '*') {
if ($cols != '*') $cols = explode(' ',$cols);
$select = $this->getAdapter()->select();
$select->from(array('ag' => 'agences'),$cols);
if($cli_id != null)
{
$select->where('cli_id = ?',$cli_id);
}
return $this->getAdapter()->fetchAll($select);
}
public function getAllAgencesForSeo()
{
$select = $this->getAdapter()->select();
$select->from(array('ag' => 'agences'),array('agence_id','agence_rs'));
$select->order('agence_rs ASC');
return $this->getAdapter()->fetchAll($select);
}
public function getAllAgencesByAlpha($letter)
{
$alphabet = array ("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z");
$select = "SELECT * FROM `pagesinterim`.`agences`";
// echo $letter;
echo "<br/>";
if ($letter && in_array($letter, $alphabet)) {
$select .= " WHERE agence_rs LIKE '$letter%' ORDER BY agence_rs ASC";
// echo $select;
$agence_alpha = Zend_Registry::get('db')->query($select)->fetchAll();
// print_r($agence_alpha);
return $agence_alpha;
}
}
/*** Récupère l'agence suivant sa raison social ***/
public function getDataAgenceByRS($search = NULL) {
print_r($search);
// Un terme a été saisi
if ($search) {
$search = array('agence_rs LIKE ?' => $search);
}
// On renvoi les résultats si terme ou tout si rien de saisi
return $this->fetchAll($search);
//return $this->fetchAll($search)->toArray();
}
/*
* Function getOptions : retourne un tableau contenant les informations utiliser lors de la cr�ation des options d'un select.
*/
public function getOptions($cli_id = null,$ag_id=null) {
$select = $this->getAdapter()->select();
$select->from($this->_name,array('agence_id','agence_rs','agence_ville'));
if ($cli_id) $select->where('cli_id = ?',$cli_id);
if ($ag_id) $select->where('agence_id = ?',$ag_id);
// echo $select->__toString();
$arryAll = $this->getAdapter()->fetchAll($select);
$arryData = array();
foreach($arryAll as $data) {
$arryData[$data['agence_id']] = $data['agence_rs'] . ' (' . $data['agence_ville'] . ')';
}
if(count($arryData) > 1){
$arryData = array(''=>'Selectionnez') + $arryData;
}
return $arryData;
}
public function getAgences($criteria = null, $paginate = null, $sort = null, $toString = False)
{
$select = $this->getAdapter()->select();
$select->from(array('Ag' => 'agences'),'*');
// Jointure sur la r�gion
$select->joinLeft(array('cli' => 'clients'),
'(Ag.cli_id = cli.cli_id)',
'*'
);
// Jointure sur le pays
$select->joinLeft(array('pa' => 'pays'),
'(Ag.agence_pays = pa.pays_iso_number)',
'*'
);
/*
* Sort : D�finie l'ordre d'affichage du r�sulat
* Direction et champ
*/
switch($sort) {
case 'nom':
$order['dir'] = 'ASC';
$order['sort'] = 'agence_rs';
break;
case 'cp':
$order['dir'] = 'ASC';
$order['sort'] = 'agence_cp';
break;
case 'ville':
$order['dir'] = 'ASC';
$order['sort'] = 'agence_ville';
break;
default:
$order['dir'] = 'ASC';
$order['sort'] = 'agence_rs';
break;
}
// @param : Mots cl�s
if(!empty($criteria['keys'])) {
$params_where['keys'] = explode(",", strtr($criteria['keys'],' ',','));
$strWhere = "agence_rs LIKE '%".trim($params_where['keys'][0])."%'";
for($i=1;$i<sizeof($params_where['keys']);$i++) {
$strWhere .= " OR agence_rs LIKE '%" . trim($params_where['keys'][$i]) . "%'";
}
$select->where($strWhere);
}
//******** Localisation
//*** Crit�re pays
// @ param - string
if(!empty($criteria['pays']) && $criteria['pays'] != '999') {
$select->where('agence_pays = ?', (int) $criteria['pays']);
}
//*** Crit�re R�gion/D�partement
// @ param - string
if(!empty($criteria['zid'])) {
// Si > 100 : recherche d'une region
if($criteria['zid'] > 100) {
// Recherche dans chaque d�partement de la r�gion
$tDepartements = new Departements();
$dpts_selected = $tDepartements->getAllDptByRegionId(($criteria['zid'] - 100));
// Zend_Debug::Dump($dpts_selected,'Request Select');
// Boucle sur les d�partements trouv�s
$strWhere = "agence_region LIKE '" . $dpts_selected[0]["dpt_id"] . "'";
for($i=1;$i<sizeof($dpts_selected);$i++) {
$strWhere .= " OR agence_region = '" . $dpts_selected[$i]["dpt_id"] . "'";
}
$select->where($strWhere);
}
// Si 1 <= x <= 100 recherche d'un d�partement
if($criteria['zid'] >= 1 && $criteria['zid'] <= 100 ) {
$select->where('agence_region = ?', (int) $criteria['zid']);
}
}
// @param : Location
// string / int
//*** Localisation
// Tableau de ville
if(!empty($criteria['agencesInsideCircle'])){
$where_commune = "Ag.`agence_id` IN (";
foreach($criteria['agencesInsideCircle'] as $agence){
$where_commune .= "'" . $agence['agence_id'] ."',";
}
$where_commune = rtrim($where_commune,',').")";
$select->where($where_commune);
} else if(!empty($criteria['location'])) {
$location = $criteria['location'];
// Recherche par code postal sur 4,5 chiffres
if(preg_match('`^[0-9]{4,5}$`',$location)) {
$where_location = "agence_cp LIKE '%" . (int)$location . "%'";
}else if(preg_match('`^[0-9]{2}$`',$location)) { // recherche dans tout les codes postaux commen�ant par XX et les r�gions
$where_location = "agence_cp LIKE '%" . (int)$location . "%'";
} else {
$where_location = $this->getAdapter()->quoteInto("agence_ville LIKE ?", '%'.$location.'%');
$where_location .= ' OR ' . $this->getAdapter()->quoteInto("agence_ville LIKE ?", '%'.$location.'%');
$where_location .= ' OR ' . $this->getAdapter()->quoteInto("agence_ville LIKE ?", '%'.$location.'%');
}
$select->where($where_location);
}
//*** Pagination
if(!empty($paginate)) {
// Affecte une limite � l'affichage et calcul automatiquement l'offset en fonction de la page et du nombre d'�l�ment par page
$select->limitPage($paginate['currentPage'], $paginate['rowPerPage']);
}
//*** Sort et direction
$select->order('cli_logo DESC');
$select->order($order['sort'] . " " . $order['dir']);
//Zend_Debug::Dump($select->__toString(),'Request Select');
//*** Effectue la requ�te et renvoie les r�sultats
if($toString == True) {
return $select;
}
return $this->getAdapter()->fetchAll($select);
}
/*
R�cup�re toutes les villes dans un carr� en fonction du poitn d'origine et du rayon
*/
public function getAgencesByLatLngSquare($centre_latitude,$centre_longitude,$rayon) {
// Correspondance approximative de kilometre vers degr� longitude
$distance_degre_lat = ($rayon / 1.609344) / (69.1);
$distance_degre_long = ($rayon / 1.609344) / (53);
// On s�lectionne grossi�rement un carr� pour la s�lection des villes
$point_nord_est_lat = $centre_latitude + $distance_degre_lat;
$point_nord_est_long = $centre_longitude + $distance_degre_long;
$point_sud_ouest_lat = $centre_latitude - $distance_degre_lat;
$point_sud_ouest_long = $centre_longitude - $distance_degre_long;
$select = $this->getAdapter()->select();
$select->from(array('Ag' => $this->_name),array('agence_id','agence_lat','agence_long'));
$select->where('agence_lat >=?',$point_sud_ouest_lat);
$select->where('agence_long >=?',$point_sud_ouest_long);
$select->where('agence_lat <= ?',$point_nord_est_lat);
$select->where('agence_long <=?',$point_nord_est_long);
return $this->getAdapter()->fetchAll($select);
}
public function getById($id) {
return $this->getDataAgenceById($id);
}
} |
Partager