Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/Config/Smartyurl.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,14 @@ class Smartyurl extends BaseConfig
* Use this response code if you intend to keep the short URL active for a temporary period. It tells search engines and browsers that the redirection is temporary, and they should continue to check the original URL for updates.
*/
public $http_response_codes_when_redirect = 301;

/**
* @var string for whom to show All URLs statistics in the dashboard.
* Possible values:
* 'all' (for all logged in users),
* 'permitted' (for permitted users only which admin.manageotherurls or super.admin),
* 'none' (do not display statistics for global urls).
* default permitted
*/
public $show_global_statistics_in_dashboard = 'permitted';
}
71 changes: 70 additions & 1 deletion app/Controllers/Dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,79 @@

namespace App\Controllers;

use App\Models\UrlHitsModel;
use App\Models\UrlModel;

class Dashboard extends BaseController
{
public function index()
{
return view(smarty_view('dashboard'));
$data = [];
// i will get all urls count
$urlmodel = new UrlModel();
$urlhitsmodel = new UrlHitsModel();
$show_global_statistics = false;

switch (setting('Smartyurl.show_global_statistics_in_dashboard')) {
case 'all':
$show_global_statistics = true;
break;

case 'permitted':
if (auth()->user()->can('super.admin', 'admin.manageotherurls')) {
$show_global_statistics = true;
}
break;

case 'none':
$show_global_statistics = false;
break;
}
$data['show_global_statistics'] = $show_global_statistics;

// All URLS statistics
if ($show_global_statistics) {
$all_urls_count = $urlmodel->getUrlCount();
$data['all_urls_count'] = $all_urls_count;

// this month added urls
$all_urls_this_month = $urlmodel->getUrlCount(null, 'this_month');
$data['all_urls_this_month'] = $all_urls_this_month;

// rodat added all urls
$all_urls_today = $urlmodel->getUrlCount(null, 'today');
$data['all_urls_today'] = $all_urls_today;

// working with hits statistics

// all hits
$all_hits_count = $urlhitsmodel->getCountHits();
$data['all_hits_count'] = $all_hits_count;

// all hits this month

$all_hits_this_month = $urlhitsmodel->getCountHits(null, 'this_month');
$data['all_hits_this_month'] = $all_hits_this_month;

// all hits todat
$all_hits_today = $urlhitsmodel->getCountHits(null, 'today');
$data['all_hits_today'] = $all_hits_today;
}

// Logged in user statistics (My URL statistics)

// all hits for my urls all time
$myurl_hits_alltime = $urlhitsmodel->getCountHits(user_id());
$data['myurl_hits_alltime'] = $myurl_hits_alltime;

// all hits for my urls this month
$myurl_hits_thismonth = $urlhitsmodel->getCountHits(user_id(), 'this_month');
$data['myurl_hits_thismonth'] = $myurl_hits_thismonth;

// all hits for my urls today
$myurl_hits_today = $urlhitsmodel->getCountHits(user_id(), 'today');
$data['myurl_hits_today'] = $myurl_hits_today;

return view(smarty_view('dashboard'), $data);
}
}
11 changes: 11 additions & 0 deletions app/Language/ar/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,15 @@
'ajaxErrorTitle' => 'خطا',
'ajaxCallError1' => 'لا يمكن اتمام طلبك الان ، تاكد من اتصالك بالانترنت وحاول مجددا',
'ajaxCallErrorAjaxError' => 'حدث خطأ أثناء تحميل البيانات. حاول مرة اخرى.',

// Statistics in Dashboard
'CountsTotalAllUrl' => 'عدد جميع الروابط',
'CountsThisMonthAllUrl' => 'عدد الروابط المنشئة هذا الشهر',
'CountsTodayAllUrl' => 'عدد الروابط المنشئة هذا اليوم',
'CountsTotalAllHits' => 'عدد زيارات الروابط المسجلة',
'CountsThisMonthAllHits' => 'عدد زيارات الروابط لهذا الشهر',
'CountsTodayAllHits' => 'عدد زيارات الروابط اليوم',
'MyURLsHitCountAllTime' => 'اجمالي زيارات روابطي',
'MyURLsHitCountThisMonth' => 'زيارات روابطي هذا الشهر',
'MyURLsHitCountToday' => 'زيارات روابطي اليوم',
];
11 changes: 11 additions & 0 deletions app/Language/en/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,15 @@
'ajaxErrorTitle' => 'Error',
'ajaxCallError1' => 'Your request cannot be completed at the moment. Please ensure that you are connected to the internet and try again.',
'ajaxCallErrorAjaxError' => 'An error occurred while loading data. Please try again.',

// Statistics in Dashboard
'CountsTotalAllUrl' => 'Total of All URLs',
'CountsThisMonthAllUrl' => 'Created URLs this month',
'CountsTodayAllUrl' => 'Created URLs today',
'CountsTotalAllHits' => 'Total of All URL visits',
'CountsThisMonthAllHits' => 'This Month URL visits',
'CountsTodayAllHits' => 'Today URL visits',
'MyURLsHitCountAllTime' => 'Total visits of my URLs',
'MyURLsHitCountThisMonth' => 'My URLs visits this month',
'MyURLsHitCountToday' => 'My URLs visits today',
];
28 changes: 28 additions & 0 deletions app/Models/UrlHitsModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,32 @@ public function getHitsByUrlId(int|array $urlId, int|null $start = null, int|nul

return $builder->countAllResults();
}

/**
* Get count of hits for all URLs, or for a specific user if $userId is provided,
* within a specified date range.
*
* @param int|null $userId
* @param string $dateRange ('all', 'this_month', 'today')
*
* @return int
*/
public function getCountHits($userId = null, $dateRange = 'all')
{
$query = $this->db->table('urlhits')
->join('urls', 'urls.url_id = urlhits.urlhit_urlid');

if ($userId !== null) {
$query->where('urls.url_user_id', $userId);
}

if ($dateRange === 'this_month') {
$query->where('MONTH(urlhits.urlhit_at)', date('m'))
->where('YEAR(urlhits.urlhit_at)', date('Y'));
} elseif ($dateRange === 'today') {
$query->where('DATE(urlhits.urlhit_at)', date('Y-m-d'));
}

return $query->countAllResults();
}
}
31 changes: 31 additions & 0 deletions app/Models/UrlModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,35 @@ public function deleteUrlById(int|array $urlId)
// Check the affected rows to determine if the deletion was successful
return $this->db->affectedRows() > 0;
}

/**
* Get created Url count for all users, or for a specific user if $userId is provided,
* within a specified create date range (created_at)
*
* @param int|null $userId
* @param string $dateRange ('all', 'this_month', 'today')
*
* @return int
*/
public function getUrlCount($userId = null, $dateRange = 'all')
{
if ($userId !== null) {
// If $urlUserId is an array, use whereIn
if (is_array($userId)) {
$this->whereIn('url_user_id', $userId);
} else {
// If $urlUserId is a single ID, use where
$this->where('url_user_id', $userId);
}
}

if ($dateRange === 'this_month') {
$this->where('MONTH(created_at)', date('m'))
->where('YEAR(created_at)', date('Y'));
} elseif ($dateRange === 'today') {
$this->where('DATE(created_at)', date('Y-m-d'));
}

return $this->countAllResults();
}
}
Loading