- Updated SCSS styles for new campaign sync buttons and dropdowns. - Refactored main_view.php to replace the single select for campaigns with a multi-select dropdown. - Added JavaScript functions to handle dropdown interactions and sync status updates. - Introduced sync status bars for clients in main_view.php. - Created new database migrations for client sync flags and cron sync status tracking.
125 lines
2.9 KiB
PHP
125 lines
2.9 KiB
PHP
<?php
|
|
namespace controls;
|
|
|
|
class Clients
|
|
{
|
|
static public function main_view()
|
|
{
|
|
return \view\Clients::main_view(
|
|
\factory\Clients::get_all()
|
|
);
|
|
}
|
|
|
|
static public function save()
|
|
{
|
|
$id = \S::get( 'id' );
|
|
$name = trim( \S::get( 'name' ) );
|
|
$google_ads_customer_id = trim( \S::get( 'google_ads_customer_id' ) );
|
|
$google_merchant_account_id = trim( \S::get( 'google_merchant_account_id' ) );
|
|
|
|
if ( !$name )
|
|
{
|
|
\S::alert( 'Nazwa klienta jest wymagana.' );
|
|
header( 'Location: /clients' );
|
|
exit;
|
|
}
|
|
|
|
$google_ads_start_date = trim( \S::get( 'google_ads_start_date' ) );
|
|
|
|
$data = [
|
|
'name' => $name,
|
|
'google_ads_customer_id' => $google_ads_customer_id ?: null,
|
|
'google_merchant_account_id' => $google_merchant_account_id ?: null,
|
|
'google_ads_start_date' => $google_ads_start_date ?: null,
|
|
];
|
|
|
|
if ( $id )
|
|
{
|
|
\factory\Clients::update( $id, $data );
|
|
\S::alert( 'Klient został zaktualizowany.' );
|
|
}
|
|
else
|
|
{
|
|
\factory\Clients::create( $data );
|
|
\S::alert( 'Klient został dodany.' );
|
|
}
|
|
|
|
header( 'Location: /clients' );
|
|
exit;
|
|
}
|
|
|
|
static public function delete()
|
|
{
|
|
$id = \S::get( 'id' );
|
|
|
|
if ( $id )
|
|
{
|
|
\factory\Clients::delete( $id );
|
|
}
|
|
|
|
echo json_encode( [ 'success' => true ] );
|
|
exit;
|
|
}
|
|
|
|
static public function get()
|
|
{
|
|
$id = \S::get( 'id' );
|
|
$client = \factory\Clients::get( $id );
|
|
|
|
echo json_encode( $client ?: [] );
|
|
exit;
|
|
}
|
|
|
|
static public function sync_status()
|
|
{
|
|
global $mdb;
|
|
|
|
// Kampanie: 1 work unit per row (pending=0, done=1)
|
|
$campaigns_raw = $mdb->query(
|
|
"SELECT client_id, COUNT(*) as total,
|
|
SUM(CASE WHEN phase='done' THEN 1 ELSE 0 END) as done
|
|
FROM cron_sync_status WHERE pipeline='campaigns' GROUP BY client_id"
|
|
)->fetchAll( \PDO::FETCH_ASSOC );
|
|
|
|
// Produkty: 3 work units per row (pending=0, fetch=1, aggregate_30=2, done=3)
|
|
$products_raw = $mdb->query(
|
|
"SELECT client_id, COUNT(*) * 3 as total,
|
|
SUM(CASE phase WHEN 'fetch' THEN 1 WHEN 'aggregate_30' THEN 2 WHEN 'done' THEN 3 ELSE 0 END) as done
|
|
FROM cron_sync_status WHERE pipeline='products' GROUP BY client_id"
|
|
)->fetchAll( \PDO::FETCH_ASSOC );
|
|
|
|
$data = [];
|
|
|
|
foreach ( $campaigns_raw as $row )
|
|
{
|
|
$data[ $row['client_id'] ]['campaigns'] = [ (int) $row['done'], (int) $row['total'] ];
|
|
}
|
|
|
|
foreach ( $products_raw as $row )
|
|
{
|
|
$data[ $row['client_id'] ]['products'] = [ (int) $row['done'], (int) $row['total'] ];
|
|
}
|
|
|
|
echo json_encode( [ 'status' => 'ok', 'data' => $data ] );
|
|
exit;
|
|
}
|
|
|
|
static public function force_sync()
|
|
{
|
|
global $mdb;
|
|
|
|
$id = (int) \S::get( 'id' );
|
|
|
|
if ( !$id )
|
|
{
|
|
echo json_encode( [ 'success' => false, 'message' => 'Brak ID klienta.' ] );
|
|
exit;
|
|
}
|
|
|
|
$mdb -> delete( 'cron_sync_status', [ 'client_id' => $id ] );
|
|
|
|
echo json_encode( [ 'success' => true ] );
|
|
exit;
|
|
}
|
|
}
|