Files
adsPRO/autoload/controls/class.Clients.php
Jacek Pyziak 0024a25bfb feat: add campaign alerts feature with alerts management and UI integration
- Introduced a new `CampaignAlerts` class for handling alerts logic.
- Added database migration for `campaign_alerts` table creation.
- Implemented methods for fetching, marking, and deleting alerts in the `CampaignAlerts` factory class.
- Created a new view for displaying campaign alerts with filtering options.
- Updated the main client view to include a badge for the number of alerts.
- Enhanced sync functionality to support campaigns and products separately.
- Adjusted styles for alert badges in the UI.
2026-02-20 01:33:53 +01:00

133 lines
3.2 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' );
$pipeline = \S::get( 'pipeline' );
if ( !$id )
{
echo json_encode( [ 'success' => false, 'message' => 'Brak ID klienta.' ] );
exit;
}
$where = [ 'client_id' => $id ];
if ( in_array( $pipeline, [ 'campaigns', 'products' ] ) )
{
$where['pipeline'] = $pipeline;
}
$mdb -> delete( 'cron_sync_status', $where );
echo json_encode( [ 'success' => true, 'pipeline' => $pipeline ?: 'all' ] );
exit;
}
}