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

116 lines
2.8 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?
namespace front\factory;
class ContactsMaps
{
static public function api_list($only_active = true)
{
global $mdb;
// 1) забираємо довідники і індексуємо по id
$provinces = $mdb->select(
'pp_contacts_maps_provinces',
[ 'id', 'code', 'name' ],
[ 'ORDER' => [ 'id' => 'ASC' ] ]
);
$provinces_by_id = [];
foreach ($provinces as $p) {
$provinces_by_id[(int)$p['id']] = $p;
}
$products = $mdb->select(
'pp_contacts_maps_products',
[ 'id', 'code', 'name', 'icon' ],
[
'is_active' => 1,
'ORDER' => [ 'sort' => 'ASC' ]
]
);
$products_by_id = [];
foreach ($products as $pr) {
$products_by_id[(int)$pr['id']] = $pr;
}
// 2) забираємо салони
$where = [
'ORDER' => [ 'sort' => 'ASC' ]
];
if ($only_active) $where['is_active'] = 1;
$rows = $mdb->select('pp_contacts_maps', '*', $where);
// 3) збираємо API формат
$out = [];
foreach ($rows as $r)
{
$phones = json_decode($r['phones_json'] ?? '[]', true);
if (!is_array($phones)) $phones = [];
$emails = json_decode($r['emails_json'] ?? '[]', true);
if (!is_array($emails)) $emails = [];
$prod_ids = json_decode($r['products_json'] ?? '[]', true);
if (!is_array($prod_ids)) $prod_ids = [];
$prod_out = [];
foreach ($prod_ids as $pid) {
$pid = (int)$pid;
if (isset($products_by_id[$pid])) {
$prod_out[] = [
'id' => (int)$products_by_id[$pid]['id'],
'code' => $products_by_id[$pid]['code'],
'name' => $products_by_id[$pid]['name'],
'icon' => $products_by_id[$pid]['icon'],
];
}
}
$prov = $provinces_by_id[(int)$r['province_id']] ?? null;
$out[] = [
'id' => (int)$r['id'],
'province' => $prov ? [
'id' => (int)$prov['id'],
'code' => $prov['code'],
'name' => $prov['name'],
] : null,
'city' => $r['city'],
'salon_type' => $r['salon_type'],
'salon_name' => $r['salon_name'],
'address' => $r['address'],
'position' => [
'lat' => (float)$r['lat'],
'lng' => (float)$r['lng'],
],
'opening_hours' => $r['opening_hours'] ?? '',
'contact' => [
'phones' => array_values($phones),
'emails' => array_values($emails),
],
'products' => $prod_out,
'button' => [
'label' => $r['button_label'] ?? '',
'url' => $r['button_url'] ?? '',
],
'banner_image' => $r['banner_image'] ?? '',
'is_active' => (int)$r['is_active'],
'sort' => (int)$r['sort'],
'updated_at' => $r['updated_at'] ?? null,
];
}
return $out;
}
}