diff --git a/cadastre/classes/atlasPrint.listener.php b/cadastre/classes/atlasPrint.listener.php index 5ccd22b..b70965f 100644 --- a/cadastre/classes/atlasPrint.listener.php +++ b/cadastre/classes/atlasPrint.listener.php @@ -18,6 +18,43 @@ class atlasPrintListener extends jEventListener */ public function onBeforePdfCreation($event) { + $cadastreConfig = array(); + $profile = 'cadastre'; + $services = lizmap::getServices(); + if (version_compare($services->qgisServerVersion, '3.0', '<')) { + if (!preg_match('#^cadastre#i', $event->project)) { + return null; + } + $cadastreConfig = array( + 'layer' => 'Parcelles', + 'pk' => 'geo_parcelle', + ); + } else { + $config = cadastreConfig::get($event->repository, $event->project); + if ($config == null) { + return null; + } + $cadastreConfig = array( + 'layer' => $config->parcelle->name, + 'pk' => $config->parcelle->unique_field, + ); + if ($config->parcelle->shortName && strlen($config->parcelle->shortName)) { + $cadastreConfig['layer'] = $config->parcelle->shortName; + } + $profile = cadastreProfile::getWithLayerId( + $event->repository, + $event->project, + $config->parcelle->id + ); + } + + try { + // try to get the specific search profile to do not rebuild it + jProfiles::get('jdb', $profile, true); + } catch (Exception $e) { + return null; + } + $status = 'error'; $file = null; @@ -35,7 +72,7 @@ public function onBeforePdfCreation($event) $layer = $params['layer']; $exp_filter = $params['exp_filter']; - if (!$layer or $layer != 'Parcelles' or !$exp_filter) { + if (!$layer or $layer != $cadastreConfig['layer'] or !$exp_filter) { $event->add( array('status' => $status, 'file' => $file) ); @@ -49,9 +86,18 @@ public function onBeforePdfCreation($event) $get_fid = preg_match('(\d+)', $exp_filter, $match); $fid = intval($match[0]); - if ($fid > 0) { + if ($fid < 1) { + $event->add( + array('status' => $status, 'file' => $file) + ); + + return null; + } + + // Try to get data from geo_commune + try { $sql = 'SELECT geo_parcelle FROM parcelle_info WHERE ogc_fid = ' . $fid; - $cnx = jDb::getConnection('cadastre'); + $cnx = jDb::getConnection($profile); $result = $cnx->query($sql); $geo_parcelle = -1; foreach ($result as $line) { @@ -103,6 +149,8 @@ public function onBeforePdfCreation($event) // \jLog::log($path); $file = $path; $status = 'success'; + } catch (Exception $e) { + jLog::log("Cadastre :: " . $e->getMessage()); } $event->add( diff --git a/cadastre/classes/cadastreConfig.class.php b/cadastre/classes/cadastreConfig.class.php index 5fb0d7e..5da8f40 100644 --- a/cadastre/classes/cadastreConfig.class.php +++ b/cadastre/classes/cadastreConfig.class.php @@ -22,25 +22,67 @@ class cadastreConfig public static function get($repository, $project) { $p = lizmap::getProject($repository . '~' . $project); - if ($p) { - $request = new lizmapCadastreRequest( - $p, - array( - 'service' => 'CADASTRE', - 'version' => '1.0.0', - 'request' => 'GetCapabilities', - ) - ); - $result = $request->process(); - if ($result->code === 200 && $result->mime !== 'text/xml') { - $data = json_decode($result->data); - if ($data->status == 'success') { - return $data->data; + if (!$p) { + return null; + } + $customVariables = $p->getCustomProjectVariables(); + if (!$customVariables) { + return null; + } + if (!array_key_exists('cadastre_parcelle_layer_id', $customVariables) + || !array_key_exists('cadastre_parcelle_unique_field', $customVariables)) { + return null; + } + + $parcelleLayerId = $customVariables['cadastre_parcelle_layer_id']; + $parcelleLayerUniqueField = $customVariables['cadastre_parcelle_unique_field']; + + $parcelleLayer = $p->getLayer($parcelleLayerId); + if (!$parcelleLayer) { + return null; + } + + $capabilities = array( + 'parcelle' => array( + 'id' => $parcelleLayerId, + 'name' => $parcelleLayer->getName(), + 'title' => $parcelleLayer->getTitle(), + 'shortName' => $parcelleLayer->getShortName(), + 'unique_field' => $customVariables['cadastre_parcelle_unique_field'], + ), + ); + + if (array_key_exists('cadastre_section_layer_id', $customVariables)) { + $layer = $p->getLayer($customVariables['cadastre_section_layer_id']); + if ($layer) { + $capabilities['section'] = array( + 'id' => $layer->getId(), + 'name' => $layer->getName(), + 'title' => $layer->getTitle(), + 'shortName' => $layer->getShortName(), + ); + if (array_key_exists('cadastre_section_unique_field', $customVariables)) { + $capabilities['section']['unique_field'] = $customVariables['cadastre_section_unique_field']; } } } - return null; + if (array_key_exists('cadastre_commune_layer_id', $customVariables)) { + $layer = $p->getLayer($customVariables['cadastre_commune_layer_id']); + if ($layer) { + $capabilities['commune'] = array( + 'id' => $layer->getId(), + 'name' => $layer->getName(), + 'title' => $layer->getTitle(), + 'shortName' => $layer->getShortName(), + ); + if (array_key_exists('cadastre_commune_unique_field', $customVariables)) { + $capabilities['commune']['unique_field'] = $customVariables['cadastre_commune_unique_field']; + } + } + } + + return (object) $capabilities; } public static function getLayerSql($repository, $project, $layerId)