diff --git a/application/models/JoLiens.php b/application/models/JoLiens.php new file mode 100644 index 00000000..766ffab7 --- /dev/null +++ b/application/models/JoLiens.php @@ -0,0 +1,5 @@ +authenticate(); - $data = json_decode($infos, true); + $dataLien = json_decode($infos, true); + //Connect to the database + try { + $db = Zend_Db::factory($this->dbConfig->db->jo); + $db->getConnection(); + } catch (Zend_Db_Adapter_Exception $e) { + if ($this->tabInfoUser['idClient']!=1) { + throw new SoapFault('ERR', "Application error"); + } else { + throw new SoapFault('ERR', $e->getMessage()); + } + } catch (Zend_Exception $e) { + throw new SoapFault('ERR', "Application error"); + } + + $lienM = new Application_Model_JoLiens($db); + //Enregistrer les infos + if ($id===null) { + + try { + $id = $lienM->insert($dataLien); + } catch (Zend_Db_Exception $e) { + if ($this->tabInfoUser['idClient']!=1) { + throw new SoapFault('ERR', "Application error"); + } else { + throw new SoapFault('ERR', $e->getMessage()); + } + } + + } + //Mise à jour des infos + else { + + try { + $id = $lienM->update($dataLien, 'id = '.$id); + } catch (Zend_Db_Exception $e) { + if ($this->tabInfoUser['idClient']!=1) { + throw new SoapFault('ERR', "Application error"); + } else { + throw new SoapFault('ERR', $e->getMessage()); + } + } + + } - // + return $id; + } + + /** + * Définition d'une fiche lien + * @param string $infos + * @param string $id + * @throws SoapFault + * @return int + */ + public function setLienRef($infos, $id=null) + { + $this->authenticate(); + $dataRef = json_decode($infos, true); + //Connect to the database + try { + $db = Zend_Db::factory($this->dbConfig->db->jo); + $db->getConnection(); + } catch (Zend_Db_Adapter_Exception $e) { + if ($this->tabInfoUser['idClient']!=1) { + throw new SoapFault('ERR', "Application error"); + } else { + throw new SoapFault('ERR', $e->getMessage()); + } + } catch (Zend_Exception $e) { + throw new SoapFault('ERR', "Application error"); + } + $refM = new Application_Model_JoLiensRef($db); + + //Check if exist + if (intval($dataRef['siren'])!=0) { + $sql = $refM->select()->from($refM, array('id'))->where('siren=?', $dataRef['siren']); + $rowset = $refM->fetchRow($sql); + if ( $rowset!==null ) { + return $rowset->id; + } + } + + //Insert + if ($id===null) { + + $dataRef = array_merge($dataRef, array('dateInsert'=>date('Ymd'))); + + try { + $id = $refM->insert($dataRef); + } catch (Zend_Db_Exception $e) { + if ($this->tabInfoUser['idClient']!=1) { + throw new SoapFault('ERR', "Application error"); + } else { + throw new SoapFault('ERR', $e->getMessage()); + } + } + + } + //Update + else { + + try { + $id = $refM->update($dataRef, 'id = '.$id); + } catch (Zend_Db_Exception $e) { + if ($this->tabInfoUser['idClient']!=1) { + throw new SoapFault('ERR', "Application error"); + } else { + throw new SoapFault('ERR', $e->getMessage()); + } + } + + } + + return $id; + } + + /** + * Recherche de fiche lien dans le référentiel + * @param string $query + * @throws SoapFault + * @return SearchLienRef[] + */ + public function searchLienRef($query) + { + $this->authenticate(); + + //Connect to the database + try { + $db = Zend_Db::factory($this->dbConfig->db->jo); + $db->getConnection(); + } catch (Zend_Db_Adapter_Exception $e) { + if ($this->tabInfoUser['idClient']!=1) { + throw new SoapFault('ERR', "Application error"); + } else { + throw new SoapFault('ERR', $e->getMessage()); + } + } catch (Zend_Exception $e) { + throw new SoapFault('ERR', "Application error"); + } + + $refM = new Application_Model_JoLiensRef($db); + + $queries = explode(' ', $query); + $sql = $refM->select()->from($refM,array('id','siren', 'RS', 'nom', 'idLoc1Num')); + if (count($queries)>0) { + $where = ''; + $i = 0; + foreach ($queries as $item) { + if (strlen($item)>2) { + $where = 'LIKE "%'.strtolower($item).'%"'; + } + $i++; + if (count($queries) < $i){ + $where.= ' OR '; + } + } + $sql->where("(siren ".$where.") OR (RS ".$where.") OR (nom ".$where.") OR (idLoc1Num ".$where.")"); + } + + try { + $result = $refM->fetchAll($sql, 20); + } catch (Zend_Db_Exception $e) { + if ($this->tabInfoUser['idClient']!=1) { + throw new SoapFault('ERR', "Application error"); + } else { + throw new SoapFault('ERR', $e->getMessage()); + } + } + + $output = array(); + if ($result->count()>0) { + foreach($result as $item) { + $tmp = new SearchLienRef(); + $tmp->id = $item->id; + $itemsLib = array('siren', 'RS', 'nom', 'idLoc1Num'); + $lib = ''; + foreach($itemsLib as $value) { + if (!empty($item->$value)) { + $lib.= ' , '.$item->$value; + } + } + $tmp->lib = $lib; + $output[] = $tmp; + } + } + + return $output; } } \ No newline at end of file diff --git a/library/WsScore/Saisie/v0.2/Types.php b/library/WsScore/Saisie/v0.2/Types.php index 49d06a23..9521c2a5 100644 --- a/library/WsScore/Saisie/v0.2/Types.php +++ b/library/WsScore/Saisie/v0.2/Types.php @@ -302,3 +302,14 @@ class BilanInfos public $postes; } +class SearchLienRef +{ + /** @var int */ + public $id; + + /** @var string */ + public $lib; + +} + +