Files
adsPRO/autoload/factory/class.CampaignAlerts.php
Jacek Pyziak 167ced3573 feat: Enhance user settings with cron URL plan display
- Added a new field to display the cron URL plan in user settings.
- Updated JavaScript to handle the new plan data.

refactor: Unify product model and migrate data

- Migrated product data from `products_data` to `products` table.
- Added new columns to `products` for better data organization.
- Created `products_aggregate` table for storing aggregated product metrics.

chore: Drop deprecated products_data table

- Removed `products_data` table as data is now stored in `products`.

feat: Add merchant URL flags to products

- Introduced flags for tracking merchant URL status in `products` table.
- Normalized product URLs to handle empty or invalid values.

feat: Link campaign alerts to specific products

- Added `product_id` column to `campaign_alerts` table for better tracking.
- Created an index for efficient querying of alerts by product.

chore: Add debug scripts for client data inspection

- Created debug scripts to inspect client data from local and remote databases.
- Included error handling and output formatting for better readability.
2026-02-20 17:50:14 +01:00

139 lines
3.0 KiB
PHP

<?php
namespace factory;
class CampaignAlerts
{
static public function get_alerts_count( $client_id = 0 )
{
global $mdb;
$where = [];
$client_id = (int) $client_id;
if ( $client_id > 0 )
{
$where['client_id'] = $client_id;
}
return (int) $mdb -> count( 'campaign_alerts', $where );
}
static public function get_unseen_count()
{
global $mdb;
return (int) $mdb -> count( 'campaign_alerts', [ 'unseen' => 1 ] );
}
static public function mark_all_seen()
{
global $mdb;
$mdb -> update( 'campaign_alerts', [ 'unseen' => 0 ], [ 'unseen' => 1 ] );
}
static public function get_clients()
{
global $mdb;
return $mdb -> select( 'clients', [ 'id', 'name' ], [
'deleted' => 0,
'ORDER' => [ 'name' => 'ASC' ]
] );
}
static public function get_alerts( $client_id = 0, $limit = 15, $offset = 0 )
{
global $mdb;
$client_id = (int) $client_id;
$limit = max( 1, (int) $limit );
$offset = max( 0, (int) $offset );
$sql = 'SELECT
ca.id,
ca.client_id,
ca.campaign_id,
ca.campaign_external_id,
ca.ad_group_id,
ca.ad_group_external_id,
ca.product_id,
ca.alert_type,
ca.message,
ca.meta_json,
ca.date_detected,
ca.date_add AS date_created,
cl.name AS client_name,
c.campaign_name,
ag.ad_group_name
FROM campaign_alerts AS ca
LEFT JOIN clients AS cl ON cl.id = ca.client_id
LEFT JOIN campaigns AS c ON c.id = ca.campaign_id
LEFT JOIN campaign_ad_groups AS ag ON ag.id = ca.ad_group_id';
if ( $client_id > 0 )
{
$sql .= ' WHERE ca.client_id = :client_id';
}
$sql .= ' ORDER BY ca.date_detected DESC, ca.id DESC LIMIT ' . $offset . ', ' . $limit;
$stmt = $mdb -> pdo -> prepare( $sql );
if ( !$stmt )
{
return [];
}
if ( $client_id > 0 )
{
$stmt -> bindValue( ':client_id', $client_id, \PDO::PARAM_INT );
}
$ok = $stmt -> execute();
if ( !$ok )
{
return [];
}
$rows = $stmt -> fetchAll( \PDO::FETCH_ASSOC );
return is_array( $rows ) ? $rows : [];
}
static public function delete_old_alerts( $days = 30 )
{
global $mdb;
$mdb -> delete( 'campaign_alerts', [
'date_detected[<]' => date( 'Y-m-d', strtotime( '-' . (int) $days . ' days' ) )
] );
}
static public function delete_alerts( array $ids )
{
global $mdb;
$ids = array_map( 'intval', $ids );
$ids = array_filter( $ids, function( $id ) { return $id > 0; } );
if ( empty( $ids ) )
{
return 0;
}
$before = (int) $mdb -> count( 'campaign_alerts', [ 'id' => $ids ] );
if ( $before <= 0 )
{
return 0;
}
$mdb -> delete( 'campaign_alerts', [ 'id' => $ids ] );
$after = (int) $mdb -> count( 'campaign_alerts', [ 'id' => $ids ] );
return max( 0, $before - $after );
}
}