Avtonomery

Description

Avtonomery is a online shop specializes in the production of duplicate license plates, frames, plates, and signs. The production process is swift, and all products adhere to the state standards of Ukraine.

The website was a collaborative effort with Smychkovska Anastasiia, who designed all pages and plates in Figma, and I implemented the design. All the code was developed from scratch without relying on any backend frameworks. Additionally, I constructed an integrated admin panel aligned with the design, encompassing all the features from the CMS.

The outcome is a straightforward, modern, fast, and optimized online shop complete with an admin panel for efficient content management.

Details

Technologies PHP, SQL, NodeJS, Gulp, Javascript, SASS, PostCSS, SVG, API, Cookies
Features Online shop, Cart, Personal Account, self-coded CMS with managing pages, products & categories, user management, email & telegram notifications, SEO optimized, auto-generating watermarks on images and many more...
Date November 2021
Link https://avtonomery.com.ua

Code snippets

php Telegram notification to admins

<?php

if(!$GLOBALS["notification_telegram_enabled"] || empty($GLOBALS["notification_telegram_token"]) || empty($GLOBALS["notification_telegram_chat_id"])) {
	return false;
}

$message_array = array(
	"Замовлення" => getOrderDescription($cart_data),
	"Сума" => getOrderSummary($cart_data)." ".$GLOBALS["shop_currency"],
	"Спосіб отримання" => $delivery . "%0A",
	"Замовник" => $user_data_arr["lastname"]." ".$user_data_arr["name"],
	"Телефон" => $user_data_arr["phone"],
	"Ел. пошта" => $user_data_arr["email"],
	"Область" => $user_data_arr["region"],
	"Населений пункт" => $user_data_arr["city"]
);

$message = "<b>Нове замовлення №{$order_id}</b>%0A%0A";

foreach($message_array as $key => $value) {
	$message .= "<b>{$key}:</b> {$value}%0A";
};

$message .= "%0A<b>Деталі:</b>%0A{$GLOBALS["site_url"]}/order/{$order_hash}";

$sendToTelegram = fopen("https://api.telegram.org/bot{$GLOBALS["notification_telegram_token"]}/sendMessage?chat_id={$GLOBALS["notification_telegram_chat_id"]}&parse_mode=html&text={$message}", "r");

if($sendToTelegram) fclose($sendToTelegram);

php Code editor with data output helpers

<?php

function extractHelpersFromText($match) {
	global $pdo, $prefix, $user;

	if(!isset($match[1])) {
		return;
	}

	$params = explode(" ", $match[1]);
	$helper_type = $params[0];
	if(isset($params[1])) {
		$helper_param = $params[1];
	} else {
		return '{{{'.$helper_type.'}}}';
	}

	switch ($helper_type) {
		case "template": {
			$template_query = $pdo->prepare("SELECT * FROM {$prefix}_templates WHERE name=:name;");
			$template_query->bindParam(":name", $helper_param);
			$template_query->execute();
			$template = $template_query->fetch(PDO::FETCH_ASSOC);
			if(!empty($template)) {
				if($template["enabled"]) {
					return compileTemplate($template["content"]);
				}
				return "";
			}
			return "Шаблон <b>{$helper_param}</b> відсутній";
		}
		case "products": {
			parse_str($helper_param, $product_args);
			if(isset($product_args["id"])) {
				return generateProductsGrid(getProductsById($product_args["id"]));
			} else if(isset($product_args["category"])) {
				$products_category = null;
				$products_limit = null;
				if($product_args["category"] != "all") {
					$products_category = intval($product_args["category"]);
				}
				if(isset($product_args["limit"])) {
					$products_limit = intval($product_args["limit"]);
				}
				return generateProductsGrid(getProductsByCategory($products_category, $products_limit));
			}
			return "Невірно заповнений допоміжник <b>{{{products}}}</b>";
			break;
		}
		case "svg": {
			return getSvg("img/icons/{$helper_param}.svg");
			break;
		}
		case "setting": {
			if(isset($GLOBALS[$helper_param])) {
				return $GLOBALS[$helper_param];
			}
			return "Налаштування <b>{$helper_param}</b> відсутнє";
			break;
		}
		case "contact_phones": {
			$phones_array = json_decode($GLOBALS["contact_phones"], true);
			$output = "";
			if(empty($phones_array)) {
				return $output;
			}
			if($helper_param == "ul") {
				$output = "<ul>";
			} else if($helper_param == "ol") {
				$output = "<ol>";
			}
			foreach ($phones_array as $item) {
				if($helper_param == "ul" || $helper_param == "ol") {
					$output .= '<li><a href="tel:'.$item["phone"].'">'.$item["phone"].'</a></li>';
				} else {
					$output .= '<'.$helper_param.'><a href="tel:'.$item["phone"].'">'.$item["phone"].'</a></'.$helper_param.'>';
				}
			}
			if($helper_param == "ul") {
				$output .= "</ul>";
			} else if($helper_param == "ol") {
				$output .= "</ol>";
			}
			return $output;
			break;
		}
		default: {
			return "Допоміжник <b>{$helper_type}</b> відсутній";
		}
	}
}

function compileTemplate($content) {
	$output = trim($content);

	$pattern = "/{{{(.+?)}}}/";
	$output = preg_replace_callback($pattern, "extractHelpersFromText", $output);

	return $output;
}

php Watermark image

<?php

function watermarkImage($source_filename, $source_tmpname, $path_to_save, $watermark = "img/watermark.png") {
	if(!file_exists($source_tmpname) || !file_exists($watermark) || empty($path_to_save)) {
		return false;
	}

	$source_ext = strtolower(pathinfo($source_filename, PATHINFO_EXTENSION));

	if($source_ext === "jpg" || $source_ext === "jpeg") {
		$image = imagecreatefromjpeg($source_tmpname);
	} else if($source_ext === "png") {
		$image = imagecreatefrompng($source_tmpname);
	} else if($source_ext === "gif") {
		$image = imagecreatefromgif($source_tmpname);
	}

	if(!isset($image)) {
		return false;
	}

	$image_width = imagesX($image);
	$image_height = imagesY($image);

	$watermark = imagecreatefrompng($watermark);
	$watermark_width = imagesX($watermark);
	$watermark_height = imagesY($watermark);
	$watermark_posX = 0;
	$watermark_posY = CEIL((imagesY($image) - imagesY($watermark)) / 2);
	$watermark_width_fit_image = $image_width;
	$watermark_height_fit_image = $watermark_height >= $image_height ? $image_height: $watermark_height;

	$sample = imagecreatetruecolor($image_width, $image_height);
	imagesavealpha($sample, true);
	$trans_colour = imagecolorallocatealpha($sample, 255, 255, 255, 127);
	imagefill($sample, 0, 0, $trans_colour);
	imagecopy($sample, $image, 0, 0, 0, 0, $image_width, $image_height);
	imagecopyresized($sample, $watermark, $watermark_posX, $watermark_posY, 0, 0, $watermark_width_fit_image, $watermark_height_fit_image, $watermark_width, $watermark_height);

	if($source_ext === "jpg" || $source_ext === "jpeg") {
		if(imagejpeg($sample, $path_to_save, 100)) {
			return true;
		}
	} else if($source_ext === "png") {
		if(imagepng($sample, $path_to_save, 9)) {
			return true;
		}		
	} else if($source_ext === "gif") {
		if(imagegif($sample, $path_to_save)) {
			return true;
		}
	}
	
	imagedestroy($image);
	imagedestroy($watermark);
	imagedestroy($sample);

	return false;
}

php Resize image

<?php

function resizeImage($source_file, $folder = "small", $width = 400) {
	if(!file_exists($source_file)) {
		return false;
	}

	if(!file_exists("uploads")) {
		mkdir("uploads", 0755, true);
	}
	if(!empty($folder) && !file_exists("uploads/".$folder)) {
		mkdir("uploads/".$folder, 0755, true);
	}

	$folder_prepend = "";
	if(!empty($folder)) {
		$folder_prepend = $folder."/";
	}

	$source_filename = strtolower(pathinfo($source_file, PATHINFO_BASENAME));
	$source_ext = strtolower(pathinfo($source_file, PATHINFO_EXTENSION));

	$path_to_save = "uploads/".$folder_prepend.$source_filename;

	if($source_ext === "jpg" || $source_ext === "jpeg") {
		$image = imagecreatefromjpeg($source_file);
	} else if($source_ext === "png") {
		$image = imagecreatefrompng($source_file);
	} else if($source_ext === "gif") {
		$image = imagecreatefromgif($source_file);
	}

	if(!isset($image)) {
		return false;
	}

	$source_width = imagesX($image);
	$source_height = imagesY($image);

	if($source_width <= $width) {
		return false;
	}

	$image_resize_ratio = $width / $source_width;
	$image_width = $source_width * $image_resize_ratio;
	$image_height = $source_height * $image_resize_ratio;

	$sample = imagecreatetruecolor($image_width, $image_height);
	imagesavealpha($sample, true);
	$trans_colour = imagecolorallocatealpha($sample, 255, 255, 255, 127);
	imagefill($sample, 0, 0, $trans_colour);
	imagecopyresized($sample, $image, 0, 0, 0, 0, $image_width, $image_height, $source_width, $source_height);

	if($source_ext === "jpg" || $source_ext === "jpeg") {
		if(imagejpeg($sample, $path_to_save, 100)) {
			return true;
		}
	} else if($source_ext === "png") {
		if(imagepng($sample, $path_to_save, 9)) {
			return true;
		}		
	} else if($source_ext === "gif") {
		if(imagegif($sample, $path_to_save)) {
			return true;
		}
	}
	
	imagedestroy($image);
	imagedestroy($sample);

	return false;
}