Skip to content

Commit

Permalink
Merge pull request #278 from evarisk-micka/fix_geolocation_model
Browse files Browse the repository at this point in the history
#271 [Geolocation] fix: data model
  • Loading branch information
evarisk-micka authored Aug 21, 2024
2 parents d65ae9e + ff9a81f commit 55026d4
Show file tree
Hide file tree
Showing 17 changed files with 323 additions and 381 deletions.
106 changes: 0 additions & 106 deletions admin/address.php

This file was deleted.

24 changes: 20 additions & 4 deletions class/actions_easycrm.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,12 +296,12 @@ public function printCommonFooter(array $parameters): int

// Do something only for the current context
if (preg_match('/thirdpartycomm|projectcard/', $parameters['context'])) {
$pictoPath = dol_buildpath('/easycrm/img/easycrm_color.png', 1);
$pictoMod = img_picto('', $pictoPath, '', 1, 0, 0, '', 'pictoModule');

if (isModEnabled('agenda')) {
require_once DOL_DOCUMENT_ROOT . '/comm/action/class/actioncomm.class.php';

$pictopath = dol_buildpath('/easycrm/img/easycrm_color.png', 1);
$picto = img_picto('', $pictopath, '', 1, 0, 0, '', 'pictoModule');

$actionComm = new ActionComm($db);

$filter = ' AND a.id IN (SELECT c.fk_actioncomm FROM ' . MAIN_DB_PREFIX . 'categorie_actioncomm as c WHERE c.fk_categorie = ' . $conf->global->EASYCRM_ACTIONCOMM_COMMERCIAL_RELAUNCH_TAG . ')';
Expand All @@ -322,7 +322,7 @@ public function printCommonFooter(array $parameters): int
}

$url = '?socid=' . $object->socid . '&fromtype=project' . '&project_id=' . $object->id . '&action=create&token=' . newToken();
$out = '<tr><td class="titlefield">' . $picto . $langs->trans('CommercialsRelaunching') . '</td>';
$out = '<tr><td class="titlefield">' . $pictoMod . $langs->trans('CommercialsRelaunching') . '</td>';

$picto = img_picto($langs->trans('CommercialsRelaunching'), 'fontawesome_fa-headset_fas');

Expand All @@ -344,6 +344,22 @@ public function printCommonFooter(array $parameters): int
</script>
<?php
}

if (!empty($object->array_options['options_projectaddress'])) {
$contact = new Contact($db);
$result = $contact->fetch($object->array_options['options_projectaddress']);
if ($result > 0) {
$pictoContact = img_picto('', 'contact', 'class="pictofixedwidth"') . $contact->lastname;
$outAddress = '<td>';
$outAddress .= dolButtonToOpenUrlInDialogPopup('address' . $result, $langs->transnoentities('FavoriteAddress'), $pictoContact, '/contact/card.php?id='. $contact->id);
$outAddress .= '</td></tr>';
?>
<script>
jQuery('.valuefield.project_extras_projectaddress').replaceWith(<?php echo json_encode($outAddress); ?>)
</script>
<?php
}
}
}

// Do something only for the current context
Expand Down
69 changes: 1 addition & 68 deletions class/address.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,72 +426,5 @@ public function LibStatut(int $status, int $mode = 0): string

return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
}

/**
* Fetch addresses in database with parent ID
*
* @param int $element_id ID of object linked
* @param string $element_type Type of object
* @param string $morefilter Filter
* @return array|integer
* @throws Exception
*/
public function fetchAddresses(int $element_id, string $element_type, string $morefilter = '1 = 1')
{
$filter = ['customsql' => 'element_id=' . $element_id . ' AND ' . $morefilter . ' AND element_type="' . $element_type . '"' . ' AND status >= 0'];
return parent::fetchAll('', '', 0, 0, $filter);
}

/**
* Convert longitude and latitude format WGS 84 (EPSG:4326) to Web Mercator (EPSG:3857)
*
* @return object
*/
public function convertCoordinates()
{
$convertFactor = 6378137.0;
$longitude = $this->longitude / 180 * pi();
$latitude = $this->latitude / 180 * pi();
$this->longitude = $convertFactor * $longitude;
$this->latitude = $convertFactor * log(tan(pi() / 4 + $latitude / 2));

return $this;
}

/**
* Inject map features
*
* @param array $features array of features: id, description, color, longitude, latitude
* @param int $chunkSize size of chunk
* @param int $deep
* @return int
*/
public function injectMapFeatures(array $features, int $chunkSize, int $deep = 0): int
{
$error = 0;

if (!empty($features)) {
$bulkFeatures = array_chunk($features, $chunkSize);
foreach ($bulkFeatures as $bulk) {
$encodedBulk = json_encode($bulk);
if (!empty($encodedBulk)) {
print "geojsonMarkers.features = $.merge(geojsonMarkers.features, $encodedBulk);\n";
} else {
if ($chunkSize > 1) {
$result = $this->injectMapFeatures($bulk, floor($chunkSize / 2), $deep + 1);
$result < 0 ? $error++ : '';
} else {
ob_start();
print_r($bulk);
$content = ob_get_contents();
ob_clean();
print "console.error('Map: Error encode json map feature, data:', '" . dol_escape_js($content, 1) . "');\n";
$error++;
}
}
}
}

return $error > 0 ? -1 : 0;
}
}

47 changes: 41 additions & 6 deletions class/geolocation.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ class Geolocation extends SaturneObject
'longitude' => ['type' => 'double(24,8)', 'label' => 'Longitude', 'enabled' => 1, 'position' => 20, 'notnull' => 1, 'visible' => 0, 'default' => 0],
'element_type' => ['type' => 'varchar(255)', 'label' => 'ElementType', 'enabled' => 1, 'position' => 30, 'notnull' => 1, 'visible' => 0],
'fk_element' => ['type' => 'integer', 'label' => 'FkElement', 'enabled' => 1, 'position' => 40, 'notnull' => 1, 'visible' => 0, 'index' => 1],
'fk_address' => ['type' => 'integer', 'label' => 'FkAddress', 'enabled' => 1, 'position' => 50, 'notnull' => 0, 'visible' => 0, 'index' => 1],
];

/**
Expand Down Expand Up @@ -131,11 +130,6 @@ class Geolocation extends SaturneObject
*/
public $fk_element;

/**
* @var int|null Fk_element
*/
public ?int $fk_address;

/**
* Constructor
*
Expand Down Expand Up @@ -198,4 +192,45 @@ public function injectMapFeatures(array $features, int $chunkSize, int $deep = 0

return $error > 0 ? -1 : 0;
}

/**
* Get data from OpenStreetMap API with an address
*
* @param Object $contact Object contact/address to get the geolocation from
* @return array Empty array if KO, filled if OK
*/
public function getDataFromOSM($contact): array
{
global $langs, $user;

$parameters = (dol_strlen($contact->address) > 0 ? $contact->address : '');
$parameters = dol_sanitizeFileName($parameters);
$parameters = str_replace(' ', '+', $parameters);

$context = stream_context_create(["http" => ["header" => "Referer:" . $_SERVER['HTTP_REFERER']]]);
$response = file_get_contents('https://nominatim.openstreetmap.org/search?q='. $parameters .'&format=json&polygon=1&addressdetails=1', false, $context);
$data = json_decode($response, false);

if (is_array($data) && !empty($data)) {
$address = $data[0];
if (!empty($address->address->postcode)) {
$contact->zip = strval($address->address->postcode);
}
if (!empty($address->address->city)) {
$contact->town = $address->address->city;
}
if (!empty($address->address->country_code)) {
$countryID = getCountry(dol_strtoupper($address->address->country_code),3);
if ($countryID > 0) {
$contact->country_id = $countryID;
}
}
$contact->update($contact->id, $user);

return $data;
} else {
$this->errors[] = $langs->trans('CouldntFindDataOnOSM');
return [];
}
}
}
35 changes: 34 additions & 1 deletion core/modules/modEasyCRM.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public function __construct($db)
$i++ => ['EASYCRM_PWA_CLOSE_PROJECT_WHEN_OPPORTUNITY_ZERO', 'integer', 0, '', 0, 'current'],

// CONST ADDRESS
$i++ => ['EASYCRM_DISPLAY_MAIN_ADDRESS', 'integer', 0, '', 0, 'current'],
//$i++ => ['EASYCRM_DISPLAY_MAIN_ADDRESS', 'integer', 0, '', 0, 'current'],
$i++ => ['EASYCRM_ADDRESS_ADDON', 'chaine', 'mod_address_standard', '', 0, 'current'],

// CONST MODULE
Expand Down Expand Up @@ -581,6 +581,39 @@ public function init($options = ''): int

dolibarr_set_const($this->db, 'EASYCRM_ACTIONCOMM_COMMERCIAL_RELAUNCH_TAG', $categoryID, 'integer', 0, '', $conf->entity);
}
if (empty(getDolGlobalInt('EASYCRM_ADDRESS_BACKWARD_COMPATIBILITY'))) {
require_once DOL_DOCUMENT_ROOT . '/contact/class/contact.class.php';
require_once __DIR__ . '/../../class/geolocation.class.php';
require_once __DIR__ . '/../../class/address.class.php';

$contact = new Contact($this->db);
$address = new Address($this->db);
$geolocation = new Geolocation($this->db);

$addresses = $address->fetchAll('', '', 0, 0, ['customsql' => ' status > 0 AND latitude > 0 AND longitude > 0']);

if (is_array($addresses) && !empty($addresses)) {
foreach ($addresses as $address) {
$contact->lastname = $address->name;
$contact->address = $address->address;
$contact->fk_project = $address->element_id;
$contact->fk_pays = $address->fk_country;
$contact->zip = $address->zip;
$contact->town = $address->town;

$contactID = $contact->create($user);

$geolocation->element_type = 'contact';
$geolocation->latitude = $address->latitude;
$geolocation->longitude = $address->longitude;
$geolocation->fk_element = $contactID;

$geolocation->create($user);
}
}

dolibarr_set_const($this->db, 'EASYCRM_ADDRESS_BACKWARD_COMPATIBILITY', 1, 'integer', 0, '', $conf->entity);
}

// Permissions
$this->remove($options);
Expand Down
Loading

0 comments on commit 55026d4

Please sign in to comment.