- Fix storage permissions to use 'www' user instead of 'www-data' - Fix UserModel getAllForAdmin query to avoid placeholders in LIMIT/OFFSET - Import missing modules: Advertising, Dashboard, Analytics - Update deploy script to copy files to public directory
307 lines
11 KiB
PHP
307 lines
11 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\Advertising\Services;
|
|
|
|
use App\Core\DatabaseManager;
|
|
use App\Core\ErrorReporter;
|
|
use App\Core\Auth;
|
|
use Visus\Cuid2\Cuid2;
|
|
|
|
/**
|
|
* Service for managing campaign-specific domains
|
|
*
|
|
* This service handles the optional association of specific domains
|
|
* to campaigns for granular targeting within site groups.
|
|
*/
|
|
class CampaignDomainsService
|
|
{
|
|
private DatabaseManager $db;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->db = DatabaseManager::getInstance();
|
|
}
|
|
|
|
/**
|
|
* Get domains associated with a campaign
|
|
*/
|
|
public function getCampaignDomains(int $campaignId): array
|
|
{
|
|
try {
|
|
$sql = "
|
|
SELECT
|
|
cd.*,
|
|
sd.domain,
|
|
sd.subdomain,
|
|
sd.is_primary as domain_is_primary,
|
|
sd.is_verified as domain_is_verified
|
|
FROM advertising_campaign_domains cd
|
|
INNER JOIN advertising_site_domains sd ON cd.site_domain_id = sd.id
|
|
WHERE cd.campaign_id = ?
|
|
AND cd.status >= 0
|
|
AND cd.deleted_at IS NULL
|
|
AND sd.deleted_at IS NULL
|
|
ORDER BY sd.is_primary DESC, sd.domain ASC
|
|
";
|
|
|
|
return $this->db->fetchAll($sql, [$campaignId]) ?: [];
|
|
} catch (\Exception $e) {
|
|
ErrorReporter::report($e, [
|
|
'context' => 'campaign_domains_get',
|
|
'campaign_id' => $campaignId
|
|
]);
|
|
return [];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get available domains for a campaign (from its site group)
|
|
*/
|
|
public function getAvailableDomainsForCampaign(int $campaignId): array
|
|
{
|
|
try {
|
|
$sql = "
|
|
SELECT
|
|
sd.*,
|
|
CASE
|
|
WHEN cd.id IS NOT NULL THEN 1
|
|
ELSE 0
|
|
END as is_associated
|
|
FROM advertising_campaigns c
|
|
INNER JOIN advertising_site_domains sd ON c.site_group_id = sd.site_group_id
|
|
LEFT JOIN advertising_campaign_domains cd ON sd.id = cd.site_domain_id
|
|
AND cd.campaign_id = ?
|
|
AND cd.status >= 0
|
|
AND cd.deleted_at IS NULL
|
|
WHERE c.id = ?
|
|
AND c.deleted_at IS NULL
|
|
AND sd.deleted_at IS NULL
|
|
AND sd.status >= 0
|
|
ORDER BY sd.is_primary DESC, sd.domain ASC
|
|
";
|
|
|
|
return $this->db->fetchAll($sql, [$campaignId, $campaignId]) ?: [];
|
|
} catch (\Exception $e) {
|
|
ErrorReporter::report($e, [
|
|
'context' => 'campaign_domains_available',
|
|
'campaign_id' => $campaignId
|
|
]);
|
|
return [];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Associate a domain with a campaign
|
|
*/
|
|
public function associateDomainToCampaign(int $campaignId, int $siteDomainId, array $targetingSettings = []): bool
|
|
{
|
|
try {
|
|
// Verify that the domain belongs to the campaign's site group
|
|
$sql = "
|
|
SELECT COUNT(*) as count
|
|
FROM advertising_campaigns c
|
|
INNER JOIN advertising_site_domains sd ON c.site_group_id = sd.site_group_id
|
|
WHERE c.id = ? AND sd.id = ?
|
|
AND c.deleted_at IS NULL AND sd.deleted_at IS NULL
|
|
";
|
|
|
|
$result = $this->db->fetch($sql, [$campaignId, $siteDomainId]);
|
|
if (!$result || $result['count'] == 0) {
|
|
throw new \Exception('Domain does not belong to campaign site group');
|
|
}
|
|
|
|
// Check if association already exists
|
|
$existingSql = "
|
|
SELECT id FROM advertising_campaign_domains
|
|
WHERE campaign_id = ? AND site_domain_id = ?
|
|
AND deleted_at IS NULL
|
|
";
|
|
$existing = $this->db->fetch($existingSql, [$campaignId, $siteDomainId]);
|
|
|
|
if ($existing) {
|
|
// Update existing association
|
|
$updateSql = "
|
|
UPDATE advertising_campaign_domains
|
|
SET status = 1, targeting_settings = ?, updated_at = CURRENT_TIMESTAMP
|
|
WHERE id = ?
|
|
";
|
|
$this->db->query($updateSql, [json_encode($targetingSettings), $existing['id']]);
|
|
} else {
|
|
// Create new association
|
|
$insertSql = "
|
|
INSERT INTO advertising_campaign_domains
|
|
(cuid, campaign_id, site_domain_id, status, targeting_settings, created_by, created_at, updated_at)
|
|
VALUES (?, ?, ?, 1, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
|
|
";
|
|
|
|
$this->db->query($insertSql, [
|
|
(string) new Cuid2(),
|
|
$campaignId,
|
|
$siteDomainId,
|
|
json_encode($targetingSettings),
|
|
Auth::id() ?? 1
|
|
]);
|
|
}
|
|
|
|
return true;
|
|
} catch (\Exception $e) {
|
|
ErrorReporter::report($e, [
|
|
'context' => 'campaign_domains_associate',
|
|
'campaign_id' => $campaignId,
|
|
'site_domain_id' => $siteDomainId
|
|
]);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove domain association from campaign
|
|
*/
|
|
public function dissociateDomainFromCampaign(int $campaignId, int $siteDomainId): bool
|
|
{
|
|
try {
|
|
$sql = "
|
|
UPDATE advertising_campaign_domains
|
|
SET status = -1, deleted_at = CURRENT_TIMESTAMP, updated_at = CURRENT_TIMESTAMP
|
|
WHERE campaign_id = ? AND site_domain_id = ? AND deleted_at IS NULL
|
|
";
|
|
|
|
$this->db->query($sql, [$campaignId, $siteDomainId]);
|
|
return true;
|
|
} catch (\Exception $e) {
|
|
ErrorReporter::report($e, [
|
|
'context' => 'campaign_domains_dissociate',
|
|
'campaign_id' => $campaignId,
|
|
'site_domain_id' => $siteDomainId
|
|
]);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Update targeting settings for a campaign domain association
|
|
*/
|
|
public function updateTargetingSettings(int $campaignId, int $siteDomainId, array $targetingSettings): bool
|
|
{
|
|
try {
|
|
$sql = "
|
|
UPDATE advertising_campaign_domains
|
|
SET targeting_settings = ?, updated_at = CURRENT_TIMESTAMP
|
|
WHERE campaign_id = ? AND site_domain_id = ?
|
|
AND status >= 0 AND deleted_at IS NULL
|
|
";
|
|
|
|
$this->db->query($sql, [json_encode($targetingSettings), $campaignId, $siteDomainId]);
|
|
return true;
|
|
} catch (\Exception $e) {
|
|
ErrorReporter::report($e, [
|
|
'context' => 'campaign_domains_update_targeting',
|
|
'campaign_id' => $campaignId,
|
|
'site_domain_id' => $siteDomainId
|
|
]);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get campaigns that have specific domain targeting
|
|
*/
|
|
public function getCampaignsWithDomainTargeting(int $siteGroupId): array
|
|
{
|
|
try {
|
|
$sql = "
|
|
SELECT DISTINCT
|
|
c.id,
|
|
c.name,
|
|
c.cuid,
|
|
COUNT(cd.id) as targeted_domains_count
|
|
FROM advertising_campaigns c
|
|
INNER JOIN advertising_campaign_domains cd ON c.id = cd.campaign_id
|
|
WHERE c.site_group_id = ?
|
|
AND c.deleted_at IS NULL
|
|
AND cd.status >= 0
|
|
AND cd.deleted_at IS NULL
|
|
GROUP BY c.id, c.name, c.cuid
|
|
ORDER BY c.name ASC
|
|
";
|
|
|
|
return $this->db->fetchAll($sql, [$siteGroupId]) ?: [];
|
|
} catch (\Exception $e) {
|
|
ErrorReporter::report($e, [
|
|
'context' => 'campaign_domains_with_targeting',
|
|
'site_group_id' => $siteGroupId
|
|
]);
|
|
return [];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Check if a campaign has domain-specific targeting
|
|
*/
|
|
public function campaignHasDomainTargeting(int $campaignId): bool
|
|
{
|
|
try {
|
|
$sql = "
|
|
SELECT COUNT(*) as count
|
|
FROM advertising_campaign_domains
|
|
WHERE campaign_id = ?
|
|
AND status >= 0
|
|
AND deleted_at IS NULL
|
|
";
|
|
|
|
$result = $this->db->fetch($sql, [$campaignId]);
|
|
return $result && $result['count'] > 0;
|
|
} catch (\Exception $e) {
|
|
ErrorReporter::report($e, [
|
|
'context' => 'campaign_domains_has_targeting',
|
|
'campaign_id' => $campaignId
|
|
]);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get domain targeting statistics for a site group
|
|
*/
|
|
public function getDomainTargetingStats(int $siteGroupId): array
|
|
{
|
|
try {
|
|
$sql = "
|
|
SELECT
|
|
COUNT(DISTINCT c.id) as total_campaigns,
|
|
COUNT(DISTINCT CASE WHEN cd.id IS NOT NULL THEN c.id END) as campaigns_with_targeting,
|
|
COUNT(DISTINCT sd.id) as total_domains,
|
|
COUNT(DISTINCT cd.site_domain_id) as domains_with_targeting
|
|
FROM advertising_campaigns c
|
|
LEFT JOIN advertising_campaign_domains cd ON c.id = cd.campaign_id
|
|
AND cd.status >= 0
|
|
AND cd.deleted_at IS NULL
|
|
LEFT JOIN advertising_site_domains sd ON c.site_group_id = sd.site_group_id
|
|
AND sd.deleted_at IS NULL
|
|
AND sd.status >= 0
|
|
WHERE c.site_group_id = ?
|
|
AND c.deleted_at IS NULL
|
|
";
|
|
|
|
return $this->db->fetch($sql, [$siteGroupId]) ?: [
|
|
'total_campaigns' => 0,
|
|
'campaigns_with_targeting' => 0,
|
|
'total_domains' => 0,
|
|
'domains_with_targeting' => 0
|
|
];
|
|
} catch (\Exception $e) {
|
|
ErrorReporter::report($e, [
|
|
'context' => 'campaign_domains_stats',
|
|
'site_group_id' => $siteGroupId
|
|
]);
|
|
return [
|
|
'total_campaigns' => 0,
|
|
'campaigns_with_targeting' => 0,
|
|
'total_domains' => 0,
|
|
'domains_with_targeting' => 0
|
|
];
|
|
}
|
|
}
|
|
} |