Viewing file: DashboardController.php (12.83 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller; use App\Models\BecameReseller; use App\Models\BillingRequest; use App\Models\Customer; use App\Models\Domain; use App\Models\EmailTemplate; use App\Models\Message; use App\Models\MessageLog; use App\Models\NumberRequest; use App\Models\SenderId; use App\Models\SmsQueue; use App\Models\Ticket; use App\Models\TopUpRequest; use App\Models\UserAnalytics; use App\Models\WhatsAppNumberRequest; use Carbon\Carbon; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Cookie;
class DashboardController extends Controller { public function index(){ $cache_in_seconds = config('cache_time');
$user =auth()->user(); $cacheCustomers = cache('customers_'.$user->id); if (is_null($cacheCustomers)) { $customers=$user->customers; $customers = cache()->remember('customers_'.$user->id, $cache_in_seconds, function () use ($customers) { return $customers; }); } else { $customers = $cacheCustomers; }
$customer_ids=[]; foreach ($customers as $key=>$customer){ $customer_ids[]=$customer->id; }
$cacheNewMessageCount = cache('newMessageCount_'.$user->id); if (is_null($cacheNewMessageCount)) { $newMessageCount = MessageLog::whereIn('customer_id',$customer_ids)->where('type','inbox')->whereDate('created_at', '>=', Carbon::now())->count(); $newMessageCount = cache()->remember('newMessageCount_'.$user->id, $cache_in_seconds, function () use ($newMessageCount) { return $newMessageCount; }); } else { $newMessageCount = $cacheNewMessageCount; } $data['newMessageCount']=$newMessageCount;
$cacheNewSentCount = cache('newSentCount_'.$user->id); if (is_null($cacheNewSentCount)) { $newSentCount = MessageLog::whereIn('customer_id',$customer_ids)->where('type','sent')->whereDate('created_at', '>=', Carbon::now())->count(); $newSentCount = cache()->remember('newSentCount_'.$user->id, $cache_in_seconds, function () use ($newSentCount) { return $newSentCount; }); } else { $newSentCount = $cacheNewSentCount; } $data['newSentCount']=$newSentCount;
$cacheTotalInbox = cache('totalInbox_'.$user->id); if (is_null($cacheTotalInbox)) { $totalInbox = MessageLog::whereIn('customer_id',$customer_ids)->where('type','inbox')->count(); $totalInbox = cache()->remember('totalInbox_'.$user->id, $cache_in_seconds, function () use ($totalInbox) { return $totalInbox; }); } else { $totalInbox = $cacheTotalInbox; } $data['totalInbox']=$totalInbox;
$cacheTotalSent = cache('totalSent_'.$user->id); if (is_null($cacheTotalSent)) { $totalSent = MessageLog::whereIn('customer_id',$customer_ids)->where('type','sent')->count(); $totalSent = cache()->remember('totalSent_'.$user->id, $cache_in_seconds, function () use ($totalSent) { return $totalSent; }); } else { $totalSent = $cacheTotalSent; } $data['totalSent']=$totalSent;
$cacheInboxes = cache('inboxes_'.$user->id); if (is_null($cacheInboxes)) { $inboxes = MessageLog::whereIn('customer_id',$customer_ids)->where('type','inbox') ->select(DB::Raw('count(*) as count'),DB::Raw('DATE(created_at) day')) ->whereDate('created_at', '>', Carbon::now()->subDays(30)) ->groupBy('day')->get() ->pluck('count','day'); $inboxes = cache()->remember('inboxes_'.$user->id, $cache_in_seconds, function () use ($inboxes) { return $inboxes; }); } else { $inboxes = $cacheInboxes; } $data['inboxes']=$inboxes;
$data['weekDates']=getLastNDays(30); $chatInboxes=[]; foreach (getLastNDays(30) as $day){ $chatInboxes[]=isset($inboxes[trim($day, '"')])?$inboxes[trim($day, '"')]:0; } $data['chart_inbox']=$chatInboxes;
$cacheSents = cache('sents_'.$user->id); if (is_null($cacheSents)) { $sents = MessageLog::whereIn('customer_id',$customer_ids)->where('type','sent') ->select(DB::Raw('count(*) as count'),DB::Raw('DATE(created_at) day')) ->whereDate('created_at', '>', Carbon::now()->subDays(30)) ->groupBy('day')->get() ->pluck('count','day'); $sents = cache()->remember('sents_'.$user->id, $cache_in_seconds, function () use ($sents) { return $sents; }); } else { $sents = $cacheSents; } $data['sents']=$sents;
$chat_sents=[]; foreach (getLastNDays(30) as $day){ $chat_sents[]=isset($sents[trim($day, '"')])?$sents[trim($day, '"')]:0; } $data['chart_sent']=$chat_sents;
// Check Settings
$availableSettings=[]; $pgs=json_decode(get_settings('payment_gateway')); $app_name=get_settings('app_name'); $recaptcha_site_key=get_settings('recaptcha_site_key'); $app_favicon=get_settings('app_favicon'); $app_logo=get_settings('app_logo'); $contact_info=json_decode(get_settings('contact_info'));
// Mail $mail_name=get_settings('mail_name'); $mail_from=get_settings('mail_from'); $mail_host=get_settings('mail_host'); $mail_port=get_settings('mail_port'); $mail_username=get_settings('mail_username'); $mail_password=get_settings('mail_password'); $mail_encryption=get_settings('mail_encryption');
// Local $local_setting=json_decode(get_settings('local_setting'));
if(!$app_name || !$recaptcha_site_key || !$app_favicon || !$app_logo || !$contact_info || !isset($contact_info->phone_number) || !isset($contact_info->email_address) || !isset($contact_info->address)){ $availableSettings[]=trans("Configure Application Settings"); };
if(!$mail_name || !$mail_from || !$mail_host || !$mail_port || !$mail_username || !$mail_password || !$mail_encryption){ $availableSettings[]=trans("Configure SMTP Settings"); }; $cacheEt = cache('e_template_'.$user->id); if (is_null($cacheEt)) { $et=EmailTemplate::whereIn('type',['registration','forget_password'])->get(); $et = cache()->remember('e_template_'.$user->id, $cache_in_seconds, function () use ($et) { return $et; }); } else { $et = $cacheEt; }
if($et->count() <=0){ $availableSettings[]=trans("Configure Email Template"); } if(!$local_setting){ $availableSettings[]=trans("Configure Local Settings"); }
$ss=json_decode(get_settings('site_setting')); if(isset($ss) && !$ss->favicon){ $availableSettings[]=trans("Need to upload favicon");}; if(isset($ss) && !$ss->logo){ $availableSettings[]=trans("Need to upload logo");};
$es=json_decode(get_settings('email_setting')); if(isset($es) && !$es->host){ $availableSettings[]=trans("Need to configure email settings");};
$data['available_setting']=$availableSettings;
$cache_browser_data = cache('cache_browser_data'); if (is_null($cache_browser_data)) { $browser_data = UserAnalytics::whereDate('created_at', '>', Carbon::now()->subDays(30)) ->select(DB::raw('browser as browser_name'), DB::raw('SUM(counter) as total_browsers')) ->groupBy('browser_name')->pluck('total_browsers', 'browser_name'); $browser_data = cache()->remember('cache_browser_data', $cache_in_seconds, function () use ($browser_data) { return $browser_data; }); } else { $browser_data = $cache_browser_data; } $labels = []; $series = [];
if ($browser_data) { foreach ($browser_data as $key => $val) { $labels[] = $key; $series[] = (int)$val; } } $data['browser_name'] = $labels; $data['browser_number'] = $series;
$cache_os_data = cache('cache_os_data'); if (is_null($cache_os_data)) { $os_data = UserAnalytics::whereDate('created_at', '>', Carbon::now()->subDays(30)) ->select(DB::raw('os as os_name'), DB::raw('SUM(counter) as total_os')) ->groupBy('os_name')->pluck('total_os', 'os_name');
$cache_os_data = cache()->remember('cache_os_data', $cache_in_seconds, function () use ($os_data) { return $os_data; }); } else { $os_data = $cache_os_data; } $labels_os = []; $series_os = [];
if ($os_data) { foreach ($os_data as $key => $val) { $labels_os[] = $key; $series_os[] = (int)$val; } } $data['os_name'] = $labels_os; $data['os_number'] = $series_os;
$cache_country_data = cache('cache_country_data'); if (is_null($cache_country_data)) { $country_data = UserAnalytics::whereDate('created_at', '>', Carbon::now()->subDays(30)) ->select(DB::raw('country'), DB::raw('SUM(counter) as total_countries')) ->groupBy('country')->pluck('total_countries', 'country');
$cache_country_data = cache()->remember('cache_country_data', $cache_in_seconds, function () use ($country_data) { return $country_data; }); } else { $country_data = $cache_country_data; }
$labels_country = []; $series_country = [];
if ($country_data) { foreach ($country_data as $key => $val) { $labels_country[] = $key; $series_country[] = (int)$val; } } $data['country_name'] = $labels_country; $data['country_number'] = $series_country;
$data['active_customers'] = Customer::where('status','active')->where('type','normal')->count(); $data['inactive_customers'] = Customer::where('status','inactive')->where('type','normal')->count(); $data['active_reseller'] = Customer::where('status','active')->where('type','reseller')->count(); $data['inactive_reseller'] = Customer::where('status','inactive')->where('type','reseller')->count();
return view('admin.dashboard',$data); } public function setLocale($type) { $availableLang = get_available_languages();
if (!in_array($type, $availableLang)) abort(400);
session()->put('locale', $type);
Cookie::queue(Cookie::make('user_locale', $type, 1440));
return redirect()->back(); }
public function countNotification(){ $adminUsers=Customer::where('added_by', 'admin')->pluck('id'); $planReq=BillingRequest::where('admin_id', auth()->user()->id)->whereIn('customer_id', $adminUsers)->where('status', 'pending')->count(); $tickets=Ticket::where('status', 'pending')->count(); $verifications=BecameReseller::where('status', 'pending')->count(); $topUpReq=TopUpRequest::where('admin_id', auth()->user()->id)->whereIn('customer_id', $adminUsers)->where('status', 'pending')->count(); $domain=Domain::where('status', 'pending')->count(); $numberReq=NumberRequest::where('status', 'pending')->count(); $whatsAppReq=WhatsAppNumberRequest::where('status', 'pending')->count(); $senderId=SenderId::where('is_dlt','no')->whereIn('status', ['pending','review','review_pending'])->count(); $headerId=SenderId::where('is_dlt','yes')->whereIn('status', ['pending','review','review_pending'])->count();
$data=[ 'plan_request'=>$planReq, 'tickets'=>$tickets, 'verifications'=>$verifications, 'topUpReq'=>$topUpReq, 'domain'=>$domain, 'numberReq'=>$numberReq, 'senderId'=>$senderId, 'headerId'=>$headerId, 'whatsappReq'=>$whatsAppReq, ]; return response()->json([ $data, 'status'=>'success'], 200); }
public function clearCache(){ $user=auth()->id(); cache()->forget('customers_'.$user); cache()->forget('newMessageCount_'.$user); cache()->forget('newSentCount_'.$user); cache()->forget('totalInbox_'.$user); cache()->forget('totalSent_'.$user); cache()->forget('inboxes_'.$user); cache()->forget('sents_'.$user);
return redirect()->back()->with('success', trans('customer.messages.cache_cleared')); } }
|