<?php
/**
 * Created by PhpStorm.
 * User: Kavand
 * Date: 3/1/2019
 * Time: 11:30 PM
 */


$object = $request['object'];
$query = '';
$error = false;
switch ($object['image-mode']) {
    case 'edit-mode':
        $acceptor = $object['acceptor'];
        $slide = $object['slide'];
        $image = $object['image'];
        $index = $object['index'];
        if (getBase64ImageSize($image) < 50) {
            if ($slide < 0) {
                $query = "INSERT INTO `app_acceptor_image_slider` (`imgSrc`, `imgFor`, `imgIndex`) values ('{$image}','{$acceptor}','{$index}')";
            } else {
                $query = "UPDATE `app_acceptor_image_slider` SET `imgSrc`='{$image}' WHERE `ID`='{$slide}'";
            }
            $error = false;
        } else {
            $error = true;
        }

        break;
    case 'delete-mode':
        $slide = $object['slide'];
        $query = "DELETE FROM `app_acceptor_image_slider` WHERE `ID`='{$slide}'";
        $error = false;
        break;
    default:
        $error = true;
        break;

}
if (!$error) {
    echo($DLA->DoCommand($query) ? '1' : '0');
} else {
    if ($object['image-mode'] == 'delete-mode') echo 0;
    if ($object['image-mode'] == 'edit-mode') echo 'حجم نامعتبر';
}
function getBase64ImageSize($base64Image)
{ //return memory size in B, KB, MB
    try {
        $size_in_bytes = (int)(strlen(rtrim($base64Image, '=')) * 3 / 4);
        $size_in_kb = $size_in_bytes / 1024;
//        $size_in_mb = $size_in_kb / 1024;

        return $size_in_kb;
    } catch (Exception $e) {
        return -1;
    }
}