First Commit
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Affiliates\Controllers;
|
||||
|
||||
use App\Controllers\BaseAdminController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Modules\AffiliatesAdmin\Models\Affiliate;
|
||||
use Modules\AffiliatesAdmin\Models\AffiliateCommission;
|
||||
use Modules\AffiliatesAdmin\Models\AffiliateBanner;
|
||||
use Modules\AffiliatesAdmin\Models\AffiliateClick;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
|
||||
class AffiliatesController extends BaseAdminController
|
||||
{
|
||||
/**
|
||||
* Check if user has permission.
|
||||
*
|
||||
* @param string $permission
|
||||
* @return void
|
||||
*/
|
||||
protected function checkPermission(string $permission)
|
||||
{
|
||||
$user = $this->getCurrentUser();
|
||||
if (!$user || !$user->hasPermission($permission)) {
|
||||
$this->addFlash('error', 'Você não tem permissão para acessar esta área.');
|
||||
$this->redirect('admin/dashboard')->send();
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
public function index(): Response
|
||||
{
|
||||
$affiliateId = $_SESSION['affiliate_id'] ?? null;
|
||||
|
||||
if (!$affiliateId) {
|
||||
return new RedirectResponse('/login');
|
||||
}
|
||||
|
||||
$affiliate = Affiliate::find($affiliateId);
|
||||
|
||||
if (!$affiliate) {
|
||||
return new RedirectResponse('/login');
|
||||
}
|
||||
|
||||
$totalEarned = AffiliateCommission::where('affiliate_id', $affiliateId)
|
||||
->where('status', 'approved')
|
||||
->sum('amount');
|
||||
|
||||
$pendingCommissions = AffiliateCommission::where('affiliate_id', $affiliateId)
|
||||
->where('status', 'pending')
|
||||
->count();
|
||||
|
||||
$banners = AffiliateBanner::where('is_active', true)->get();
|
||||
|
||||
return $this->renderAdminView('@affiliates/dashboard/index.twig', [
|
||||
'affiliate' => $affiliate,
|
||||
'totalEarned' => $totalEarned,
|
||||
'pendingCommissions' => $pendingCommissions,
|
||||
'banners' => $banners,
|
||||
]);
|
||||
}
|
||||
|
||||
public function banners(): Response
|
||||
{
|
||||
$affiliateId = $_SESSION['affiliate_id'] ?? null;
|
||||
if (!$affiliateId) {
|
||||
return new RedirectResponse('/login');
|
||||
}
|
||||
|
||||
$banners = AffiliateBanner::where('is_active', true)->get();
|
||||
return $this->renderAdminView('@affiliates/banners/index.twig', ['banners' => $banners]);
|
||||
}
|
||||
|
||||
public function bannerCode(int $id): Response
|
||||
{
|
||||
$affiliateId = $_SESSION['affiliate_id'] ?? null;
|
||||
if (!$affiliateId) {
|
||||
return new RedirectResponse('/login');
|
||||
}
|
||||
|
||||
$banner = AffiliateBanner::findOrFail($id);
|
||||
return $this->renderAdminView('@affiliates/banners/code.twig', ['banner' => $banner, 'affiliateId' => $affiliateId]);
|
||||
}
|
||||
|
||||
public function commissions(): Response
|
||||
{
|
||||
$affiliateId = $_SESSION['affiliate_id'] ?? null;
|
||||
if (!$affiliateId) {
|
||||
return new RedirectResponse('/login');
|
||||
}
|
||||
|
||||
$commissions = AffiliateCommission::where('affiliate_id', $affiliateId)->get();
|
||||
return $this->renderAdminView('@affiliates/commissions/index.twig', ['commissions' => $commissions]);
|
||||
}
|
||||
|
||||
public function profile(): Response
|
||||
{
|
||||
$affiliateId = $_SESSION['affiliate_id'] ?? null;
|
||||
if (!$affiliateId) {
|
||||
return new RedirectResponse('/login');
|
||||
}
|
||||
|
||||
$affiliate = Affiliate::findOrFail($affiliateId);
|
||||
return $this->renderAdminView('@affiliates/profile/index.twig', ['affiliate' => $affiliate]);
|
||||
}
|
||||
|
||||
public function trackClick(Request $request, string $affiliateCode, int $bannerId): Response
|
||||
{
|
||||
$affiliate = Affiliate::where('affiliate_code', $affiliateCode)->first();
|
||||
$banner = AffiliateBanner::find($bannerId);
|
||||
|
||||
if ($affiliate && $banner) {
|
||||
AffiliateClick::create([
|
||||
'affiliate_id' => $affiliate->id,
|
||||
'banner_id' => $banner->id,
|
||||
'ip_address' => $request->getClientIp(),
|
||||
'user_agent' => $request->headers->get('User-Agent'),
|
||||
'referrer' => $request->headers->get('Referer'),
|
||||
]);
|
||||
return new RedirectResponse($banner->target_url);
|
||||
}
|
||||
|
||||
return new RedirectResponse('/');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user