From da78217dd91f6e67339eaa07b2bf50377a575b58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CVeLiTi=E2=80=9D?= <“info@veliti.nl”> Date: Thu, 8 May 2025 13:42:19 +0200 Subject: [PATCH] CMXX - dealers --- api/v2/get/dealers.php | 4 + api/v2/post/dealers.php | 165 +++++++++++++++++++++++++++++----------- assets/functions.php | 79 ++++++++++++++++++- dealer.php | 19 +++++ dealers.php | 4 +- uploader.php | 5 +- 6 files changed, 226 insertions(+), 50 deletions(-) diff --git a/api/v2/get/dealers.php b/api/v2/get/dealers.php index 05d462f..fbba26d 100644 --- a/api/v2/get/dealers.php +++ b/api/v2/get/dealers.php @@ -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(); diff --git a/api/v2/post/dealers.php b/api/v2/post/dealers.php index 2fb9406..3d85926 100644 --- a/api/v2/post/dealers.php +++ b/api/v2/post/dealers.php @@ -1,6 +1,8 @@ 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); diff --git a/assets/functions.php b/assets/functions.php index be0d044..560ce1a 100644 --- a/assets/functions.php +++ b/assets/functions.php @@ -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; +} \ No newline at end of file diff --git a/dealer.php b/dealer.php index bf35941..c0caf14 100644 --- a/dealer.php +++ b/dealer.php @@ -282,6 +282,25 @@ $view .= '
'; +$view .= '
+
+ '.$tab3.' +
+
+ + + + + + + + + +
'.$general_created.''.getRelativeTime($responses['created']).'
'.$general_updated.''.getRelativeTime($responses['updated']).'
+
+
+'; + //OUTPUT echo $view; diff --git a/dealers.php b/dealers.php index b8bd1ef..47ffa39 100644 --- a/dealers.php +++ b/dealers.php @@ -87,6 +87,7 @@ $view .= ' '.($dealers_id ?? '#').' '.($dealers_status ?? 'status').' '.($dealers_name ?? 'Name').' + '.($dealers_created ?? 'Created').' '.$general_actions.' @@ -107,7 +108,8 @@ $view .= ' '.$dealer['rowID'].' '.(${$dealer_status} ?? $dealer_status).' - '.$dealer['name'].' + '.$dealer['name'].' + '.(($dealer['full_path'] !='')?'' : '').' '.getRelativeTime($dealer['created']).' '.$general_view.' '; diff --git a/uploader.php b/uploader.php index 4e88c41..ec9dcee 100644 --- a/uploader.php +++ b/uploader.php @@ -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));