Ejemplo n.º 1
0
 /**
  * GetFeatureInfoHtml : return HTML for the getFeatureInfo.
  * @param array $params Array of parameters
  * @param string $xmldata XML data from getFeatureInfo
  * @return Feature Info in HTML format.
  */
 function getFeatureInfoHtml($params, $xmldata)
 {
     // Get data from XML
     $use_errors = libxml_use_internal_errors(true);
     $go = true;
     $errorlist = array();
     // Create a DOM instance
     $xml = simplexml_load_string($xmldata);
     if (!$xml) {
         foreach (libxml_get_errors() as $error) {
             $errorlist[] = $error;
         }
         $go = false;
     }
     // Get json configuration for the project
     $configLayers = $this->project->getLayers();
     // Get optional parameter fid
     $filterFid = null;
     $fid = $this->param('fid');
     if ($fid) {
         $expFid = explode('.', $fid);
         if (count($expFid) == 2) {
             $filterFid = array();
             $filterFid[$expFid[0]] = $expFid[1];
         }
     }
     // Loop through the layers
     $content = array();
     $ptemplate = 'view~popup';
     $lizmapCache = $this->lizmapCache;
     $popupClass = jClasses::getService('view~popup');
     foreach ($xml->Layer as $layer) {
         $layername = $layer['name'];
         $configLayer = $this->project->findLayerByAnyName($layername);
         if ($configLayer == null) {
             continue;
         }
         // Avoid layer if no popup asked by the user for it
         // or if no popup property
         // or if no edition
         $returnPopup = False;
         if (property_exists($configLayer, 'popup') && $configLayer->popup == 'True') {
             $returnPopup = True;
         }
         if (!$returnPopup) {
             $editionLayer = $this->project->findEditionLayerByLayerId($configLayer->id);
             if ($editionLayer != null && ($editionLayer->capabilities->modifyGeometry == 'True' || $editionLayer->capabilities->modifyAttribute == 'True' || $editionLayer->capabilities->deleteFeature == 'True')) {
                 $returnPopup = True;
             }
         }
         if (!$returnPopup) {
             continue;
         }
         // Get layer title
         $layerTitle = $configLayer->title;
         $layerId = $configLayer->id;
         // Get the template for the popup content
         $templateConfigured = False;
         if (property_exists($configLayer, 'popupTemplate')) {
             // Get template content
             $popupTemplate = (string) trim($configLayer->popupTemplate);
             // Use it if not empty
             if (!empty($popupTemplate)) {
                 $templateConfigured = True;
                 // first replace all "media/bla/bla/llkjk.ext" by full url
                 $popupTemplate = preg_replace_callback('#(["\']){1}(media/.+\\.\\w{3,10})(["\']){1}#', array($this, 'replaceMediaPathByMediaUrl'), $popupTemplate);
                 // Replace : html encoded chars to let further regexp_replace find attributes
                 $popupTemplate = str_replace(array('%24', '%7B', '%7D'), array('$', '{', '}'), $popupTemplate);
             }
         }
         // Loop through the features
         foreach ($layer->Feature as $feature) {
             $id = $feature['id'];
             // Optionnally filter by feature id
             if ($filterFid && isset($filterFid[$configLayer->name]) && $filterFid[$configLayer->name] != $id) {
                 continue;
             }
             // Hidden input containing layer id and feature id
             $hiddenFeatureId = '<input type="hidden" value="' . $layerId . '.' . $id . '" class="lizmap-popup-layer-feature-id"/>
     ';
             // First get default template
             $tpl = new jTpl();
             $tpl->assign('attributes', $feature->Attribute);
             $tpl->assign('repository', $this->repository->getKey());
             $tpl->assign('project', $this->project->getKey());
             $popupFeatureContent = $tpl->fetch('view~popupDefaultContent');
             $autoContent = $popupFeatureContent;
             // Get specific template for the layer has been configured
             if ($templateConfigured) {
                 $popupFeatureContent = $popupTemplate;
                 // then replace all column data by appropriate content
                 foreach ($feature->Attribute as $attribute) {
                     // Replace #col and $col by colomn name and value
                     $popupFeatureContent = $popupClass->getHtmlFeatureAttribute($attribute['name'], $attribute['value'], $this->repository->getKey(), $this->project->getKey(), $popupFeatureContent);
                 }
                 $lizmapContent = $popupFeatureContent;
             }
             // Use default template if needed or maptip value if defined
             $hasMaptip = false;
             $maptipValue = '';
             foreach ($feature->Attribute as $attribute) {
                 if ($attribute['name'] == 'maptip') {
                     $hasMaptip = true;
                     $maptipValue = $attribute['value'];
                 }
             }
             // If there is a maptip attribute we display its value
             if ($hasMaptip) {
                 // first replace all "media/bla/bla/llkjk.ext" by full url
                 $maptipValue = preg_replace_callback('#(["\']){1}(media/.+\\.\\w{3,10})(["\']){1}#', array($this, 'replaceMediaPathByMediaUrl'), $maptipValue);
                 // Replace : html encoded chars to let further regexp_replace find attributes
                 $maptipValue = str_replace(array('%24', '%7B', '%7D'), array('$', '{', '}'), $maptipValue);
                 $qgisContent = $maptipValue;
             }
             // New option to choose the popup source : auto (=default), lizmap (=popupTemplate), qgis (=qgis maptip)
             $finalContent = $autoContent;
             if (property_exists($configLayer, 'popupSource')) {
                 if ($configLayer->popupSource == 'qgis' and $hasMaptip) {
                     $finalContent = $qgisContent;
                 }
                 if ($configLayer->popupSource == 'lizmap' and $templateConfigured) {
                     $finalContent = $lizmapContent;
                 }
             }
             $tpl = new jTpl();
             $tpl->assign('layerTitle', $layerTitle);
             $tpl->assign('popupContent', $hiddenFeatureId . $finalContent);
             $content[] = $tpl->fetch('view~popup');
         }
         // loop features
     }
     // loop layers
     $content = array_reverse($content);
     return implode("\n", $content);
 }