diff --git a/app/Config/Smartyurl.php b/app/Config/Smartyurl.php index c92f7de..376ff0f 100644 --- a/app/Config/Smartyurl.php +++ b/app/Config/Smartyurl.php @@ -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'; } diff --git a/app/Controllers/Dashboard.php b/app/Controllers/Dashboard.php index 8b3e01d..48e0a3a 100644 --- a/app/Controllers/Dashboard.php +++ b/app/Controllers/Dashboard.php @@ -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); } } diff --git a/app/Language/ar/Common.php b/app/Language/ar/Common.php index 3fe2382..4c5f7e8 100644 --- a/app/Language/ar/Common.php +++ b/app/Language/ar/Common.php @@ -22,4 +22,15 @@ 'ajaxErrorTitle' => 'خطا', 'ajaxCallError1' => 'لا يمكن اتمام طلبك الان ، تاكد من اتصالك بالانترنت وحاول مجددا', 'ajaxCallErrorAjaxError' => 'حدث خطأ أثناء تحميل البيانات. حاول مرة اخرى.', + + // Statistics in Dashboard + 'CountsTotalAllUrl' => 'عدد جميع الروابط', + 'CountsThisMonthAllUrl' => 'عدد الروابط المنشئة هذا الشهر', + 'CountsTodayAllUrl' => 'عدد الروابط المنشئة هذا اليوم', + 'CountsTotalAllHits' => 'عدد زيارات الروابط المسجلة', + 'CountsThisMonthAllHits' => 'عدد زيارات الروابط لهذا الشهر', + 'CountsTodayAllHits' => 'عدد زيارات الروابط اليوم', + 'MyURLsHitCountAllTime' => 'اجمالي زيارات روابطي', + 'MyURLsHitCountThisMonth' => 'زيارات روابطي هذا الشهر', + 'MyURLsHitCountToday' => 'زيارات روابطي اليوم', ]; diff --git a/app/Language/en/Common.php b/app/Language/en/Common.php index d5d833e..3d254fb 100644 --- a/app/Language/en/Common.php +++ b/app/Language/en/Common.php @@ -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', ]; diff --git a/app/Models/UrlHitsModel.php b/app/Models/UrlHitsModel.php index 762da9d..ecc461a 100644 --- a/app/Models/UrlHitsModel.php +++ b/app/Models/UrlHitsModel.php @@ -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(); + } } diff --git a/app/Models/UrlModel.php b/app/Models/UrlModel.php index eacc2c8..f8ed615 100644 --- a/app/Models/UrlModel.php +++ b/app/Models/UrlModel.php @@ -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(); + } } diff --git a/app/Views/basic/dashboard.php b/app/Views/basic/dashboard.php index d62881d..a1497ac 100644 --- a/app/Views/basic/dashboard.php +++ b/app/Views/basic/dashboard.php @@ -5,92 +5,224 @@ section('main') ?> -
- -
- -
-
-

Fixed Layout

-
-
- -
-
- -
-
-
- -
- + + +
+ + + + + + + + + + + + + + + + +
-
+
+
+ + + - - + +
+ +
+
+ + + + +
+ +
- + +
+ +
+ + + + - - + + + + + +
+
+
+ + + - - + + diff --git a/composer.json b/composer.json index c10af02..ae9afe7 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "type": "project", "version": "1.0.0-dev", "require": { - "php": "^7.4 || ^8.0", + "php": "^8.1", "codeigniter4/framework": "^4.3", "codeigniter4/settings": "^2.1", "codeigniter4/shield": "dev-develop", @@ -12,6 +12,7 @@ "ext-json": "*", "ext-mbstring": "*", "ext-gettext": "*", + "ext-gmp": "*", "laminas/laminas-escaper": "^2.9", "psr/log": "^1.1", "ip2location/ip2location-php": "^9.7",