CMXX - dealers

This commit is contained in:
“VeLiTi”
2025-05-08 13:42:19 +02:00
parent c3e5873912
commit da78217dd9
6 changed files with 226 additions and 50 deletions

View File

@@ -38,6 +38,9 @@ if(isset($get_content) && $get_content!=''){
elseif ($v[0] == 'id') {//create clause
$clause .= ' AND d.rowID = :'.$v[0];
}
elseif ($v[0] == 'rating_overall') {//create clause
$clause .= ' AND d.rating_overall >= :'.$v[0];
}
else {//create clause
$clause .= ' AND d.'.$v[0].' = :'.$v[0];
}
@@ -88,6 +91,7 @@ if (!empty($criterias)){
}
}
//Add paging details
if(isset($criterias['totals']) && $criterias['totals']==''){
$stmt->execute();

View File

@@ -1,6 +1,8 @@
<?php
defined($security_key) or exit;
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
//------------------------------------------
// dealers
//------------------------------------------
@@ -29,6 +31,9 @@ if(isset($post_content['bounds'])){
//------------------------------------------
if(isset($post_content) && $post_content !=''){
//ADD STATUS TO POST_CONTENT, active only
$post_content['status'] = 1;
//------------------------------------------
//RUN THROUGH POST CONTENT
//------------------------------------------
@@ -59,6 +64,10 @@ if(isset($post_content['bounds'])){
$criterias['lat_sw'] = $southWestLat;
$criterias['lat_ne'] = $northEastLat;
}
if ($criteria == 'status'){
$clause .= " AND d.status = :status ";
$criterias['status'] = $value;
}
}
@@ -104,10 +113,36 @@ if(isset($post_content['bounds'])){
// EXECUTE QUERY
//------------------------------------------
$stmt->execute();
//Get results
$messages = $stmt->fetchAll(PDO::FETCH_ASSOC);
//------------------------------------------
// Check if there are 0 rows returned
//------------------------------------------
if (empty($messages)) {
// Calculate the center
$centerLat = ($southWestLat + $northEastLat) / 2;
$centerLng = ($southWestLng + $northEastLng) / 2;
// No rows were returned, execute alternative query
$sql = 'SELECT
d.*,
(6371 * acos(
cos(radians('.$centerLat.')) * cos(radians(d.lat)) * cos(radians(d.lng) - radians('.$centerLng.')) +
sin(radians('.$centerLat.')) * sin(radians(d.lat))
)) AS distance_km,
m.full_path
FROM dealers d LEFT JOIN media m ON d.dealer_media = m.rowID
WHERE d.status = 1
ORDER BY distance_km
LIMIT 10';
$altStmt = $pdo->prepare($sql);
$altStmt->execute();
$messages = $altStmt->fetchAll(PDO::FETCH_ASSOC);
}
//------------------------------------------
//CHANGE ROWID INTO UUID
//------------------------------------------
@@ -151,11 +186,11 @@ elseif(isset($post_content['dealerfinder'])){
$geo_search_2 = 'HAVING distance < '.$post_content['range'];
//Build SQL FOR GEO SEARCH
$sql = 'select d.*, m.full_path, '.$geo_search_1.' FROM dealers d LEFT JOIN media m ON d.dealer_media = m.rowID '.$geo_search_2.' ORDER BY d.rating_overall ';
$sql = 'select d.*, m.full_path, '.$geo_search_1.' FROM dealers d LEFT JOIN media m ON d.dealer_media = m.rowID WHERE d.status = 1 '.$geo_search_2.' ORDER BY ';
}
else {
//Use standard
$sql = 'select d.*, m.full_path FROM dealers d LEFT JOIN media m ON d.dealer_media = m.rowID ORDER BY d.rating_overall ';
$sql = 'select d.*, m.full_path FROM dealers d LEFT JOIN media m ON d.dealer_media = m.rowID WHERE d.status = 1 ORDER BY ';
}
//CHECK ALL THE POSTED ITEMS
@@ -165,24 +200,28 @@ elseif(isset($post_content['dealerfinder'])){
//TRANSLATE RESPONSE TO DATABASE VALUES
switch ($key) {
case $field_question_1: //rating overall
case ($field_question_1 ?? 'budget'): //budget
//check value returned and include SQL
switch ($value) {
case '1':
$sql .= 'case when d.'.$key.' = 8 then 1 else 0 end +';
$sql .= 'case when d.'.$key.' = 1 then 1 else 0 end +';
break;
case '0':
$sql .= 'case when (d.'.$key.' > 6.5 && d.d.'.$key.' < 8 ) then 1 else 0 end +';
$sql .= 'case when d.'.$key.' = 0 then 1 else 0 end +';
break;
}
//------------------------------------
break;
case $field_question_2: //locations
case ($field_question_2 ?? 'showroom_quality'): //showroom_quality
//check value returned and include SQL
switch ($value) {
case '2':
$sql .= 'case when d.'.$key.' = 2 then 1 else 0 end +';
break;
case '1':
$sql .= 'case when d.'.$key.' = 1 then 1 else 0 end +';
break;
@@ -194,10 +233,14 @@ elseif(isset($post_content['dealerfinder'])){
//------------------------------------
break;
case $field_question_3: //brand_type
case ($field_question_3 ?? 'showroom_size'): //showroom_size
//check value returned and include SQL
switch ($value) {
case '2':
$sql .= 'case when d.'.$key.' = 2 then 1 else 0 end +';
break;
case '1':
$sql .= 'case when d.'.$key.' = 1 then 1 else 0 end +';
break;
@@ -209,43 +252,14 @@ elseif(isset($post_content['dealerfinder'])){
//------------------------------------
break;
case $field_question_4: //showroom size
case ($field_question_4 ?? 'brand_category'): //brand_category
//check value returned and include SQL
switch ($value) {
case '1':
$sql .= 'case when d.'.$key.' = 1 then 1 else 0 end +';
case '2':
$sql .= 'case when d.'.$key.' = 2 then 1 else 0 end +';
break;
case '0':
$sql .= 'case when d.'.$key.' = 0 then 1 else 0 end +';
break;
case 'C':
$sql .= 'case when d.'.$key.' = 2 then 1 else 0 end +';
break;
}
//------------------------------------
break;
case $field_question_5: //garden_center
//check value returned and include SQL
switch ($value) {
case '1':
$sql .= 'case when d.'.$key.' = 1 then 1 else 0 end +';
break;
case '0':
$sql .= 'case when d.'.$key.' = 0 then 1 else 0 end +';
break;
}
//------------------------------------
break;
case $field_question_6: // focus offering
//check value returned and include SQL
switch ($value) {
case '1':
$sql .= 'case when d.'.$key.' = 1 then 1 else 0 end +';
break;
@@ -260,11 +274,13 @@ elseif(isset($post_content['dealerfinder'])){
}
}
//Replace LAST J
$sql = removeTrailingElement($sql, ',');
//REPLACE LAST + with DESC
$sql = substr($sql, 0, -1).' desc limit 0,4';
$sql = removeTrailingElement($sql, '+').' desc limit 0,4';
//Prepare statement
$stmt = $pdo->prepare($sql);
//Excute Query
$stmt->execute();
//Get results
@@ -279,13 +295,70 @@ elseif(isset($post_content['dealerfinder'])){
}
$updatedData = array_map('updateRowID', $messages);
$messages = json_encode($messages, JSON_UNESCAPED_UNICODE);
$messages = json_encode($updatedData, JSON_UNESCAPED_UNICODE);
//------------------------------------------
//Send results
//------------------------------------------
echo $messages;
}
elseif(isset($post_content['dealer_closeby'])){
//++++++++++++++++++++++
//Process FIND CLOSEST DEALERS
//++++++++++++++++++++++
//GET GEOLOCATION
if ((isset($post_content['latitude']) && $post_content['latitude']!='0') && (isset($post_content['longitude']) && $post_content['longitude']!='0')){
//INPUT GEOLOCATION USER
$lat = $post_content['latitude'];
$lng = $post_content['longitude'];
//Build SQL FOR GEO SEARCH
$sql = 'SELECT
d.rowID,
d.name as dealer,
d.lat,
d.lng,
(6371 * acos(
cos(radians('.$lat.')) * cos(radians(d.lat)) * cos(radians(d.lng) - radians('.$lng.')) +
sin(radians('.$lat.')) * sin(radians(d.lat))
)) AS distance_km,
m.full_path as imageUrl
FROM dealers d LEFT JOIN media m ON d.dealer_media = m.rowID
WHERE d.status = 1
ORDER BY distance_km
LIMIT 5';
//Prepare statement
$stmt = $pdo->prepare($sql);
//Excute Query
$stmt->execute();
//Get results
$messages = $stmt->fetchAll(PDO::FETCH_ASSOC);
//------------------------------------------
//CHANGE ROWID INTO UUID
//------------------------------------------
function updateRowID($row) {
$row['rowID'] = encodeUuid($row['rowID']);
return $row;
}
$updatedData = array_map('updateRowID', $messages);
header('Content-Type: application/json');
$messages = json_encode($updatedData, JSON_UNESCAPED_UNICODE);
//------------------------------------------
//Send results
//------------------------------------------
echo $messages;
}
else {
header('Content-Type: application/json');
echo json_encode(['error' => "Latitude or longitude not provided."]);
}
}
else
{
//++++++++++++++++++++++
@@ -311,6 +384,8 @@ else
$clause_insert ='';
$input_insert = '';
//UPDATE CALCULATED VALUES BUDGET, SHOWROOM_QUALTIY, BRAND_CATEGORY AND UPDATE DEALER_SLUG
$post_content = processPostContent($post_content);
if (isset($post_content['opening_hours'])){
$post_content['opening_hours'] = json_encode($post_content['opening_hours'], JSON_UNESCAPED_UNICODE);

View File

@@ -4013,7 +4013,7 @@ function getDomainName($hostname) {
// encode ID to UUID
//=======================================
function encodeUuid($number) {
$alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%&?-';
$base = strlen($alphabet);
$encoded = '';
@@ -4035,7 +4035,9 @@ function encodeUuid($number) {
// decode UUID to ID
//=======================================
function decodeUuid($encoded) {
$alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$encoded = strtoupper($encoded);
$alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%&?-';
$base = strlen($alphabet);
$number = 0;
@@ -4398,3 +4400,76 @@ function generateDealerInformation($token){
return false;
}
function removeTrailingElement($string,$element) {
// Trim whitespace from the end
$trimmed = rtrim($string);
// Check if the trimmed string is not empty and ends with a comma
if (!empty($trimmed) && substr($trimmed, -1) === $element) {
// Remove the last character (the comma)
return substr($trimmed, 0, -1);
}
// Return original string if it doesn't end with a comma
return $trimmed;
}
function processPostContent(array $post_content): array
{
// Use provided values if they exist and are not empty
if (isset($post_content['budget']) && !empty($post_content['budget'])) {
$budget = $post_content['budget'];
}
if (isset($post_content['showroom_quality']) && !empty($post_content['showroom_quality'])) {
$showroom_quality = $post_content['showroom_quality'];
}
if (isset($post_content['brand_category']) && !empty($post_content['brand_category'])) {
$brand_category = $post_content['brand_category'];
}
// Check if 'focus_offering' is available and not empty
if (isset($post_content['focus_offering']) && !empty($post_content['focus_offering'])) {
// 1. Add budget based on focus_offering if budget wasn't provided
if (!isset($budget)) {
$post_content['budget'] = $post_content['focus_offering'];
}
// Ensure 'dealer_type' is available for showroom_quality logic
if (isset($post_content['dealer_type'])) {
$dealer_type = $post_content['dealer_type'];
// 2, 3, 4. Determine showroom_quality if it wasn't provided
if (!isset($showroom_quality)) {
if ($post_content['focus_offering'] == 0 && $dealer_type == 0) {
$post_content['showroom_quality'] = 0;
} elseif (($post_content['focus_offering'] == 0 && $dealer_type == 1) || ($post_content['focus_offering'] == 1 && $dealer_type == 0)) {
$post_content['showroom_quality'] = 1;
} elseif ($post_content['focus_offering'] == 1 && $dealer_type == 1) {
$post_content['showroom_quality'] = 2;
}
}
}
}
// 5. Determine brand_category based on showroom_quality if it exists and brand_category wasn't provided
if (isset($post_content['showroom_quality']) && !isset($brand_category)) {
if ($post_content['showroom_quality'] == 2){
$post_content['brand_category'] = 2;
} elseif ($post_content['showroom_quality'] == 0){
$post_content['brand_category'] = 0;
} else {
$post_content['brand_category'] = 1;
}
}
if (isset($post_content['name']) && (isset($post_content['dealer_slug']) && $post_content['dealer_slug'] == '')){
$trimmedString = trim($post_content['name']);
$post_content['dealer_slug'] = str_replace(" ", "_", $trimmedString);
}
return $post_content;
}

View File

@@ -282,6 +282,25 @@ $view .= '<div class="content-block">
</div>
';
$view .= '<div class="content-block">
<div class="block-header">
<i class="fa-solid fa-bars fa-sm"></i>'.$tab3.'
</div>
<div class="table order-table">
<table>
<tr>
<td style="width:25%;">'.$general_created.'</td>
<td>'.getRelativeTime($responses['created']).'</td>
</tr>
<tr>
<td style="width:25%;">'.$general_updated.'</td>
<td>'.getRelativeTime($responses['updated']).'</td>
</tr>
</table>
</div>
</div>
';
//OUTPUT
echo $view;

View File

@@ -87,6 +87,7 @@ $view .= '
<td>'.($dealers_id ?? '#').'</td>
<td>'.($dealers_status ?? 'status').'</td>
<td>'.($dealers_name ?? 'Name').'</td>
<td class="responsive-hidden"></td>
<td class="responsive-hidden">'.($dealers_created ?? 'Created').'</td>
<td>'.$general_actions.'</td>
</tr>
@@ -107,7 +108,8 @@ $view .= '
<tr>
<td>'.$dealer['rowID'].'</td>
<td>'.(${$dealer_status} ?? $dealer_status).'</td>
<td>'.$dealer['name'].'</td>
<td>'.$dealer['name'].'</td>
<td class="responsive-hidden">'.(($dealer['full_path'] !='')?'<img style="border-radius: 4px;height: 50px;" src="'.$dealer['full_path'].'" alt="">' : '').'</td>
<td class="responsive-hidden">'.getRelativeTime($dealer['created']).'</td>
<td><a href="index.php?page=dealer&id='.encodeUuid($dealer['rowID']).'" class="btn_link">'.$general_view.'</a></td>
</tr>';

View File

@@ -18,6 +18,7 @@ if ($create_allowed === 1 && $_POST){
$tableName = json_decode($_POST['table'],true);
$contents = json_decode($_POST['data'],true);
foreach ($contents as $content){
switch ($tableName) {
@@ -284,7 +285,7 @@ $view ='
cells.forEach((cell, index) => {
if (index < headers.length) {
rowData[headers[index]] = cell.textContent;
rowData[headers[index]] = cell.innerHTML;
}
});
@@ -292,7 +293,7 @@ $view ='
});
// GET TABLE NAME
const tableName = document.getElementById(\'table_name\').value;
// Use standard form submission approach instead of JSON
const form = new FormData();
form.append(\'table\', JSON.stringify(tableName));