From dcd5e5f5d03728d4d69792e1e2d35401ab490b57 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Wed, 24 Apr 2024 17:15:03 +0200 Subject: [PATCH] Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop --- htdocs/core/class/dolgeophp.class.php | 105 ++++++++++++++++++++++++ htdocs/core/class/extrafields.class.php | 28 +++---- 2 files changed, 119 insertions(+), 14 deletions(-) create mode 100644 htdocs/core/class/dolgeophp.class.php diff --git a/htdocs/core/class/dolgeophp.class.php b/htdocs/core/class/dolgeophp.class.php new file mode 100644 index 00000000000..9c54eb4f7cc --- /dev/null +++ b/htdocs/core/class/dolgeophp.class.php @@ -0,0 +1,105 @@ + + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * or see https://www.gnu.org/ + */ + +/** + * \file htdocs/core/class/dolgeophp.class.php + * \brief Absctraction class to manage a WYSIWYG editor + */ + +require_once DOL_DOCUMENT_ROOT.'/includes/geoPHP/geoPHP.inc.php'; + + +/** + * Class to manage Geo processing + * Usage: $dolgeophp=new DolGeoPHP($db); + */ +class DolGeoPHP +{ + /** + * @var DoliDB $db Database handler + */ + public $db; + + /** + * Create an object to build an HTML area to edit a large string content + * + * @param DoliDB $db Database handler + */ + public function __construct($db) + { + $this->db = $db; + } + + /** + * Return data from a value + * + * @param mixed $value Value + * @return array Centroid + */ + public function parseGeoString($value) + { + $geom = geoPHP::load($value, 'wkt'); + $geojson = $geom->out('json'); + $centroid = $geom->getCentroid(); + $centroidjson = $centroid->out('json'); + + return array('geojson' => $geojson, 'centroid' => $centroid, 'centroidjson' => $centroidjson); + } + + /** + * Return a string with x and y + * + * @param mixed $value Value + * @return string X space Y + */ + public function getXYString($value) + { + $geom = geoPHP::load($value, 'wkt'); + $value = $geom->x().' '.$geom->y(); + + return $value; + } + + /** + * Return a string with x and y + * + * @param mixed $value Value + * @return string X space Y + */ + public function getPointString($value) + { + $geom = geoPHP::load($value, 'wkt'); + $value = get_class($geom) . ' : '. $geom->numPoints() . ' Points'; + + return $value; + } + + /** + * Return wkt + * + * @param string $geojson A json string + * @return string X space Y + */ + public function getWkt($geojson) + { + $geom = geoPHP::load($geojson, 'json'); + $value_key = $geom->out('wkt'); + + return $value_key; + } +} diff --git a/htdocs/core/class/extrafields.class.php b/htdocs/core/class/extrafields.class.php index 5fefd4f65ca..ed1a15f969e 100644 --- a/htdocs/core/class/extrafields.class.php +++ b/htdocs/core/class/extrafields.class.php @@ -1712,14 +1712,14 @@ class ExtraFields //$out = $form->selectForForms($param_list[0], $keyprefix.$key.$keysuffix, $value, $showempty, '', '', $morecss, '', 0, 0, ''); $out = $form->selectForForms($tmparray[0], $keyprefix.$key.$keysuffix, $value, $showempty, '', '', $morecss, '', 0, 0, '', $element.':options_'.$key); } elseif (in_array($type, ['point', 'multipts', 'linestrg', 'polygon'])) { - require_once DOL_DOCUMENT_ROOT.'/includes/geoPHP/geoPHP.inc.php'; + require_once DOL_DOCUMENT_ROOT.'/core/class/dolgeophp.class.php'; + $dolgeophp = new DolGeoPHP($this->db); $geojson = '{}'; $centroidjson = getDolGlobalString('MAIN_INFO_SOCIETE_GEO_COORDINATES', '{}'); if (!empty($value)) { - $geom = geoPHP::load($value, 'wkt'); - $geojson = $geom->out('json'); - $centroid = $geom->getCentroid(); - $centroidjson = $centroid->out('json'); + $tmparray = $dolgeophp->parseGeoString($value); + $geojson = $tmparray['geojson']; + $centroidjson = $tmparray['centroidjson']; } if (!preg_match('/search_/', $keyprefix)) { require_once DOL_DOCUMENT_ROOT.'/core/class/geomapeditor.class.php'; @@ -2094,18 +2094,18 @@ class ExtraFields } } } elseif ($type == 'point') { - require_once DOL_DOCUMENT_ROOT.'/includes/geoPHP/geoPHP.inc.php'; if (!empty($value)) { - $geom = geoPHP::load($value, 'wkt'); - $value = $geom->x().' '.$geom->y(); + require_once DOL_DOCUMENT_ROOT.'/core/class/dolgeophp.class.php'; + $dolgeophp = new DolGeoPHP($this->db); + $value = $dolgeophp->getXYString($value); } else { $value = ''; } } elseif (in_array($type, ['multipts','linestrg', 'polygon'])) { - require_once DOL_DOCUMENT_ROOT.'/includes/geoPHP/geoPHP.inc.php'; if (!empty($value)) { - $geom = geoPHP::load($value, 'wkt'); - $value = get_class($geom) . ' : '. $geom->numPoints() . ' Points'; + require_once DOL_DOCUMENT_ROOT.'/core/class/dolgeophp.class.php'; + $dolgeophp = new DolGeoPHP($this->db); + $value = $dolgeophp->getPointString($value); } else { $value = ''; } @@ -2380,11 +2380,11 @@ class ExtraFields $value_key = GETPOST("options_".$key, 'restricthtml'); } elseif (in_array($key_type, ['point', 'multipts', 'linestrg', 'polygon'])) { // construct point - require_once DOL_DOCUMENT_ROOT.'/includes/geoPHP/geoPHP.inc.php'; + require_once DOL_DOCUMENT_ROOT.'/core/class/dolgeophp.class.php'; $geojson = GETPOST("options_".$key, 'restricthtml'); if ($geojson != '{}') { - $geom = geoPHP::load($geojson, 'json'); - $value_key = $geom->out('wkt'); + $dolgeophp = new DolGeoPHP($this->db); + $value_key = $dolgeophp->getWkt($geojson); } else { $value_key = ''; }