Skip to content

Commit

Permalink
Geo-Zone code updated from 3.0.x.x
Browse files Browse the repository at this point in the history
  • Loading branch information
condor2 committed Feb 12, 2024
1 parent 91f0d27 commit cf2c431
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 37 deletions.
10 changes: 10 additions & 0 deletions upload/admin/controller/localisation/geo_zone.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,16 @@ protected function getForm(): void {
$data['zone_to_geo_zones'] = [];
}

$geo_zone_ids = [];

foreach ($data['zone_to_geo_zones'] as $zone_to_geo_zone) {
if (!in_array($zone_to_geo_zone['geo_zone_id'],$geo_zone_ids)) {
$geo_zone_ids[] = $zone_to_geo_zone['geo_zone_id'];
}
}

$data['zones'] = $this->model_localisation_geo_zone->getZonesByGeoZones($geo_zone_ids);

$data['header'] = $this->load->controller('common/header');
$data['column_left'] = $this->load->controller('common/column_left');
$data['footer'] = $this->load->controller('common/footer');
Expand Down
31 changes: 30 additions & 1 deletion upload/admin/model/localisation/geo_zone.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function getTotalGeoZones() {
}

public function getZoneToGeoZones($geo_zone_id) {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "zone_to_geo_zone WHERE geo_zone_id = '" . (int)$geo_zone_id . "'");
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "zone_to_geo_zone WHERE geo_zone_id = '" . (int)$geo_zone_id . "' ORDER BY country_id ASC, zone_id ASC");

return $query->rows;
}
Expand All @@ -127,4 +127,33 @@ public function getTotalZoneToGeoZoneByZoneId($zone_id) {

return (int)$query->row['total'];
}

public function getZonesByGeoZones($geo_zone_ids) {
if (empty($geo_zone_ids)) {
return [];
}

$sql = "SELECT DISTINCT zgz.country_id, z.zone_id, c.`name` AS country, z.`name` AS zone ";
$sql .= "FROM `".DB_PREFIX."zone_to_geo_zone` AS zgz ";
$sql .= "LEFT JOIN `".DB_PREFIX."country` c ON c.country_id=zgz.country_id ";
$sql .= "LEFT JOIN `".DB_PREFIX."zone` z ON z.country_id=c.country_id ";
$sql .= "WHERE zgz.geo_zone_id IN (" . implode(',', $geo_zone_ids) . ") ";
$sql .= "ORDER BY country_id ASC, zone ASC;";

$query = $this->db->query($sql);

$results = [];

foreach ($query->rows as $row) {
$country_id = $row['country_id'];

if (!isset($results[$country_id])) {
$results[$country_id] = [];
}

$results[$country_id][$row['zone_id']] = $row['zone'];
}

return $results;
}
}
89 changes: 53 additions & 36 deletions upload/admin/view/template/localisation/geo_zone_form.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<?php $zone_to_geo_zone_row = 0; ?>
<?php foreach ($zone_to_geo_zones as $zone_to_geo_zone) { ?>
<tr id="zone-to-geo-zone-row<?php echo $zone_to_geo_zone_row; ?>">
<td class="text-left"><select name="zone_to_geo_zone[<?php echo $zone_to_geo_zone_row; ?>][country_id]" class="form-control" onchange="country(this, '<?php echo $zone_to_geo_zone_row; ?>', '<?php echo $zone_to_geo_zone['zone_id']; ?>');">
<td class="text-left"><select name="zone_to_geo_zone[<?php echo $zone_to_geo_zone_row; ?>][country_id]" class="form-control" data-index="<?php echo $zone_to_geo_zone_row; ?>" data-zone-id="<?php echo $zone_to_geo_zone['zone_id']; ?>">
<?php foreach ($countries as $country) { ?>
<?php if ($country['country_id'] == $zone_to_geo_zone['country_id']) { ?>
<option value="<?php echo $country['country_id']; ?>" selected="selected"><?php echo $country['name']; ?></option>
Expand All @@ -65,6 +65,14 @@
<?php } ?>
</select></td>
<td class="text-left"><select name="zone_to_geo_zone[<?php echo $zone_to_geo_zone_row; ?>][zone_id]" class="form-control">
<option value="0"><?php echo $text_all_zones; ?></option>
<?php foreach ($zones[$zone_to_geo_zone['country_id']] as $zone_id => $zone) { ?>
<?php if ($zone_id == zone_to_geo_zone['zone_id']) { ?>
<option value="<?php echo $zone_id; ?>" selected="selected"><?php echo $zone; ?></option>
<?php } else { ?>
<option value="<?php echo $zone_id; ?>"><?php echo $zone; ?></option>
<?php } ?>
<?php } ?>
</select></td>
<td class="text-left"><button type="button" onclick="$('#zone-to-geo-zone-row<?php echo $zone_to_geo_zone_row; ?>').remove();" data-toggle="tooltip" title="<?php echo $button_remove; ?>" class="btn btn-danger"><i class="fa fa-minus-circle"></i></button></td>
</tr>
Expand All @@ -74,7 +82,7 @@
<tfoot>
<tr>
<td colspan="2"></td>
<td class="text-left"><button type="button" onclick="addGeoZone();" data-toggle="tooltip" title="<?php echo $button_geo_zone_add; ?>" class="btn btn-primary"><i class="fa fa-plus-circle"></i></button></td>
<td class="text-left"><button type="button" id="button-geo-zone" data-toggle="tooltip" title="<?php echo $button_geo_zone_add; ?>" class="btn btn-primary"><i class="fa fa-plus-circle"></i></button></td>
</tr>
</tfoot>
</table>
Expand All @@ -85,12 +93,12 @@
<script type="text/javascript"><!--
var zone_to_geo_zone_row = <?php echo $zone_to_geo_zone_row; ?>;
function addGeoZone() {
$('#button-geo-zone').on('click', function() {
html = '<tr id="zone-to-geo-zone-row' + zone_to_geo_zone_row + '">';
html += ' <td class="text-left"><select name="zone_to_geo_zone[' + zone_to_geo_zone_row + '][country_id]" class="form-control" onchange="country(this, \'' + zone_to_geo_zone_row + '\', \'0\');">';
html += ' <td class="text-left"><select name="zone_to_geo_zone[' + zone_to_geo_zone_row + '][country_id]" class="form-control" data-index="' + zone_to_geo_zone_row + '">';
<?php foreach ($countries as $country) { ?>
html += ' <option value="<?php echo $country['country_id']; ?>"><?php echo addslashes($country['name']); ?></option>';
<?php } ?>
<?php } ?>
html += '</select></td>';
html += ' <td class="text-left"><select name="zone_to_geo_zone[' + zone_to_geo_zone_row + '][zone_id]" class="form-control"><option value="0"><?php echo $text_all_zones; ?></option></select></td>';
html += ' <td class="text-left"><button type="button" onclick="$(\'#zone-to-geo-zone-row' + zone_to_geo_zone_row + '\').remove();" data-toggle="tooltip" title="<?php echo $button_remove; ?>" class="btn btn-danger"><i class="fa fa-minus-circle"></i></button></td>';
Expand All @@ -101,41 +109,50 @@ function addGeoZone() {
$('zone_to_geo_zone[' + zone_to_geo_zone_row + '][country_id]').trigger();
zone_to_geo_zone_row++;
}
});
function country(element, index, zone_id) {
$.ajax({
url: 'index.php?route=localisation/country/country&token=<?php echo $token; ?>&country_id=' + element.value,
dataType: 'json',
beforeSend: function() {
$('select[name=\'zone_to_geo_zone[' + index + '][country_id]\']').after(' <i class="fa fa-circle-o-notch fa-spin"></i>');
},
complete: function() {
$('.fa-spin').remove();
},
success: function(json) {
html = '<option value="0"><?php echo $text_all_zones; ?></option>';
if (json['zone'] && json['zone'] != '') {
for (i = 0; i < json['zone'].length; i++) {
html += '<option value="' + json['zone'][i]['zone_id'] + '"';
if (json['zone'][i]['zone_id'] == zone_id) {
html += ' selected="selected"';
$('#zone-to-geo-zone').on('change', 'select[name$=\'[country_id]\']', function() {
var element = this;
if (element.value) {
$.ajax({
url: 'index.php?route=localisation/country/country&token=<?php echo $token; ?>&country_id=' + element.value,
dataType: 'json',
beforeSend: function() {
$(element).prop('disabled', true);
$('button[form=\'form-geo-zone\']').prop('disabled', true);
},
complete: function() {
$(element).prop('disabled', false);
$('button[form=\'form-geo-zone\']').prop('disabled', false);
},
success: function(json) {
html = '<option value="0"><?php echo $text_all_zones; ?></option>';
if (json['zone'] && json['zone'] != '') {
for (i = 0; i < json['zone'].length; i++) {
html += '<option value="' + json['zone'][i]['zone_id'] + '"';
if (json['zone'][i]['zone_id'] == $(element).attr('data-zone-id')) {
html += ' selected="selected"';
}
html += '>' + json['zone'][i]['name'] + '</option>';
}
html += '>' + json['zone'][i]['name'] + '</option>';
}
$('select[name=\'zone_to_geo_zone[' + $(element).attr('data-index') + '][zone_id]\']').html(html);
$('select[name=\'zone_to_geo_zone[' + $(element).attr('data-index') + '][zone_id]\']').prop('disabled', false);
$('select[name$=\'[country_id]\']:disabled:first').trigger('change');
},
error: function(xhr, ajaxOptions, thrownError) {
alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
}
});
}
});
$('select[name=\'zone_to_geo_zone[' + index + '][zone_id]\']').html(html);
},
error: function(xhr, ajaxOptions, thrownError) {
alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
}
});
}
$('select[name$=\'[country_id]\']').trigger('change');
//--></script></div>
<?php echo $footer; ?>

0 comments on commit cf2c431

Please sign in to comment.