Files
vidok.com/autoload/admin/factory/class.ContactsMaps.php

138 lines
3.5 KiB
PHP

<?
namespace admin\factory;
class ContactsMaps
{
static public function products()
{
global $mdb;
return $mdb->select(
'pp_contacts_maps_products',
[ 'id', 'code', 'name', 'icon' ],
[
'is_active' => 1,
'ORDER' => [
'sort' => 'ASC'
]
]
);
}
static public function provinces()
{
global $mdb;
return $mdb->select(
'pp_contacts_maps_provinces',
[ 'id', 'code', 'name'],
[
'ORDER' => [
'id' => 'ASC'
]
]
);
}
static public function get($id)
{
global $mdb;
$id = (int)$id;
if (!$id) return null;
$row = $mdb->get(
'pp_contacts_maps',
'*',
[ 'id' => $id ]
);
if (!$row) return null;
$row['phones'] = json_decode($row['phones_json'], true) ?: [];
$row['emails'] = json_decode($row['emails_json'], true) ?: [];
$row['products'] = json_decode($row['products_json'], true) ?: [];
$row['coords'] = '';
if (!empty($row['lat']) && !empty($row['lng'])) {
$row['coords'] = $row['lat'] . ', ' . $row['lng'];
}
return $row;
}
static public function save($v)
{
global $mdb;
$id = (int)($v['id'] ?? 0);
// coords: "lat, lng"
$coords = trim($v['coords'] ?? '');
$lat = 0; $lng = 0;
if ($coords) {
$parts = array_map('trim', explode(',', $coords));
if (count($parts) >= 2) {
$lat = (float)str_replace(' ', '', $parts[0]);
$lng = (float)str_replace(' ', '', $parts[1]);
}
}
$phones = array_filter(array_map('trim', explode("\n", $v['phones'] ?? '')));
$emails = array_filter(array_map('trim', explode("\n", $v['emails'] ?? '')));
$products = [];
if (isset($v['products']) && is_array($v['products'])) {
$products = $v['products'];
}
elseif (isset($v['products[]']) && is_array($v['products[]'])) {
$products = $v['products[]'];
}
$data = [
'province_id' => (int)($v['province_id'] ?? 0),
'city' => trim($v['city'] ?? ''),
'salon_type' => $v['salon_type'] ?? 'sales',
'salon_name' => trim($v['salon_name'] ?? ''),
'address' => trim($v['address'] ?? ''),
'lat' => $lat,
'lng' => $lng,
'opening_hours' => trim($v['opening_hours'] ?? ''),
'phones_json' => json_encode($phones, JSON_UNESCAPED_UNICODE),
'emails_json' => json_encode($emails, JSON_UNESCAPED_UNICODE),
'products_json' => json_encode(array_map('intval', $products)),
'button_label' => trim($v['button_label'] ?? 'SKONTAKTUJ SIĘ Z NAMI'),
'button_url' => trim($v['button_url'] ?? ''),
'banner_image' => trim($v['banner_image'] ?? ''),
'is_active' => !empty($v['is_active']) ? 1 : 0,
'sort' => (int)($v['sort'] ?? 0),
];
if ($id) {
$mdb->update('pp_contacts_maps', $data, [ 'id' => $id ]);
return $id;
}
$mdb->insert('pp_contacts_maps', $data);
return (int)$mdb->id();
}
static public function delete($id)
{
global $mdb;
$id = (int)$id;
if (!$id) return false;
return $mdb->delete(
'pp_contacts_maps',
[ 'id' => $id ]
);
}
}