Skip to content

Commit

Permalink
#173 [Address] add: edition and improve display
Browse files Browse the repository at this point in the history
  • Loading branch information
evarisk-micka committed Aug 21, 2024
1 parent c6c3426 commit af0a3ae
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 15 deletions.
19 changes: 19 additions & 0 deletions class/geolocation.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,23 @@ public function injectMapFeatures(array $features, int $chunkSize, int $deep = 0

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

/**
* Get data from OpenStreetMap API with an address
*
* @param string $address Address to get the geolocation from
* @return array
*/
public function getDataFromOSM($address)
{
$parameters = (dol_strlen($address) > 0 ? $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);

return $data;
}
}
9 changes: 9 additions & 0 deletions core/tpl/easycrm_address_table_view.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@

// Actions
print '<td class="right">';
if ($permissiontoadd) {
print '<form method="POST" action="' . $_SERVER['PHP_SELF'] . '?from_id=' . $fromId . '&action=edit&from_type=' . $objectType . '">';
print '<input type="hidden" name="token" value="' . newToken() . '">';
print '<input type="hidden" name="addressID" value="' . $element['id'] . '">';
if (!empty($backtopage)) {
print '<input type="hidden" name="backtopage" value="' . $backtopage . '">';
}
print '<button type="submit" class="wpeo-button button-grey"><i class="fas fa-pen"></i></button> ';
}
if ($permissiontodelete) {
print '<form method="POST" action="' . $_SERVER['PHP_SELF'] . '?from_id=' . $fromId . '&module_name=' . $moduleName . '&from_type=' . $objectLinked->element . '">';
print '<input type="hidden" name="token" value="' . newToken() . '">';
Expand Down
13 changes: 3 additions & 10 deletions core/triggers/interface_99_modEasycrm_EasycrmTriggers.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,20 +120,13 @@ public function runTrigger($action, $object, User $user, Translate $langs, Conf
$contact = new Contact($this->db);
$contact->fetch($lastContact['id']);

$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);
$geolocation = new Geolocation($this->db);
$data = $geolocation->getDataFromOSM($contact->address);

if (is_array($data) && !empty($data)) {
$address = $data[0];

$geolocation = new Geolocation($this->db);

$geolocation->element_type('project');
$geolocation->element_type = 'contact';
$geolocation->latitude = $address->lat;
$geolocation->longitude = $address->lon;
$geolocation->fk_element = $lastContact['id'];
Expand Down
2 changes: 2 additions & 0 deletions langs/fr_FR/easycrm.lang
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ AllContactHaveNotification = Tous les tiers ont des notifications cont
Addresses = Adresses
AddAnAddress = Ajouter une adresse
AddressCreated = Adresse créée
AddressUpdated = Adresse modifée
AddressDeleted = Adresse supprimée
ErrorCreateAddress = Une erreur est survenue lors de la création de l'adresse
ErrorDeleteAddress = Une erreur est survenue lors de la suppression de l'adresse
Expand All @@ -121,6 +122,7 @@ NoAddresses = Aucune adresse
Map = Carte
FavoriteAddress = Adresse favorite
NewAddress = Nouvelle adresse
EditAddress = Modifier l'adresse
AddressType = Type d'adresse
Workplace = Lieu de travail
PrincipalResidence = Résidence principale
Expand Down
37 changes: 32 additions & 5 deletions view/address_card.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,28 @@
}
}

if ($action == 'edit_address' && $permissiontoadd && !$cancel) {
$addressID = GETPOST('addressID');

if ($addressID > 0) {
$contact->fetch($addressID);
$contact->lastname = $addressName;
$contact->address = $addressAddress;
$contact->update($addressID, $user);

$geolocations = $geolocation->fetch('', '', ' AND fk_element = ' . $addressID);
$data = $geolocation->getDataFromOSM($contact->address);
if (is_array($data) && !empty($data)) {
$address = $data[0];

$geolocation->latitude = $address->lat;
$geolocation->longitude = $address->lon;
$geolocation->update($user);
}
setEventMessages($langs->trans('AddressUpdated'), []);
}
}

if ($action == 'toggle_favorite') {
$favoriteAddressId = GETPOST('favorite_id');

Expand Down Expand Up @@ -205,16 +227,21 @@
print $form->buttonsSaveCancel('Create', 'Cancel', [], false, 'wpeo-button');
} else if ($action == 'edit' && $fromId > 0) {
$objectLinked->fetch($fromId);
$addressID = GETPOST('addressID');

$contact = new Contact($db);
$contact->fetch($addressID);

saturne_get_fiche_head($objectLinked, 'address', $title);

print load_fiche_titre($langs->trans('NewAddress'), $backtopage, $contact->picto);
print load_fiche_titre($langs->trans('EditAddress'), $backtopage, $contact->picto);

print dol_get_fiche_head();

print '<form method="POST" action="' . $_SERVER['PHP_SELF'] . '?from_id=' . $fromId . '&from_type=' . $objectType . '">';
print '<input type="hidden" name="token" value="'.newToken().'">';
print '<input type="hidden" name="action" value="add_address">';
print '<input type="hidden" name="action" value="edit_address">';
print '<input type="hidden" name="addressID" value="' . $addressID . '">';
if ($backtopage) {
print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
}
Expand All @@ -223,20 +250,20 @@

// Name -- Nom
print '<tr><td class="fieldrequired">' . $langs->trans('Name') . '</td><td>';
print '<input class="flat minwidth300 maxwidth300" type="text" size="36" name="name" id="name" value="' . $addressName . '">';
print '<input class="flat minwidth300 maxwidth300" type="text" size="36" name="name" id="name" value="' . $contact->lastname . '">';
print '</td></tr>';

// Address -- Adresse
print '<tr><td class="fieldrequired">' . $langs->trans('Address') . '</td><td>';
$doleditor = new DolEditor('addressDetail', GETPOST('description'), '', 90, 'dolibarr_details', '', false, true, $conf->global->FCKEDITOR_ENABLE_SOCIETE, ROWS_3, '90%');
$doleditor = new DolEditor('addressDetail', $contact->address, '', 90, 'dolibarr_details', '', false, true, $conf->global->FCKEDITOR_ENABLE_SOCIETE, ROWS_3, '90%');
$doleditor->Create();
print '</td></tr>';

print '</table></br>';

print dol_get_fiche_end();

print $form->buttonsSaveCancel('Create', 'Cancel', [], false, 'wpeo-button');
print $form->buttonsSaveCancel('Edit', 'Cancel', [], false, 'wpeo-button');
} else if ($fromId > 0 || !empty($ref) && empty($action)) {
$objectLinked->fetch($fromId);

Expand Down

0 comments on commit af0a3ae

Please sign in to comment.