From cf78da99d4570b077d2cd83e37514ea9152bda7f Mon Sep 17 00:00:00 2001 From: Mohammed AlShannaq Date: Wed, 20 Dec 2023 06:29:30 +0300 Subject: [PATCH 1/8] chore: add deps to composer.json --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index c10af02..1e39a15 100644 --- a/composer.json +++ b/composer.json @@ -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", From b2f2f43d282797570356c1b126b7e01662eae885 Mon Sep 17 00:00:00 2001 From: Mohammed AlShannaq Date: Wed, 20 Dec 2023 07:04:17 +0300 Subject: [PATCH 2/8] docs: update deps --- docs/developers.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/developers.md b/docs/developers.md index 9363428..f7238db 100644 --- a/docs/developers.md +++ b/docs/developers.md @@ -4,7 +4,7 @@ - You need a web hosting account (for a domain or sub-domain) with PHP 8.1 or higher support and the following PHP extensions (typically supported by most PHP hosting providers): - - [mysqlnd](http://php.net/manual/en/mysqlnd.install.php) , [intl](http://php.net/manual/en/intl.requirements.php) , [mbstring](http://php.net/manual/en/mbstring.installation.php) , [libcurl](https://www.php.net/manual/en/curl.setup.php) , [gmp](https://www.php.net/manual/en/gmp.installation.php) , [json](https://www.php.net/manual/en/json.installation.php), [bcmath](https://www.php.net/manual/en/bc.setup.php) + - [mysqlnd](http://php.net/manual/en/mysqlnd.install.php) , [intl](http://php.net/manual/en/intl.requirements.php) , [mbstring](http://php.net/manual/en/mbstring.installation.php) , [libcurl](https://www.php.net/manual/en/curl.setup.php) , [gmp](https://www.php.net/manual/en/gmp.installation.php) , [json](https://www.php.net/manual/en/json.installation.php), [bcmath](https://www.php.net/manual/en/bc.setup.php), [gmp](https://www.php.net/manual/en/gmp.setup.php) - Your web hosting account should have MySQL 8.0+ support - You need yo use `composer` and `php spark` command , therefor you must have SSH Access to your host account. From 29fc0d2fb9d35090563f24b5c696577b36d80c5f Mon Sep 17 00:00:00 2001 From: Mohammed AlShannaq Date: Wed, 20 Dec 2023 07:10:37 +0300 Subject: [PATCH 3/8] chore: update deps --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 1e39a15..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", From 7d59d48535cb13a029d2e2e64ca2869aab3f4084 Mon Sep 17 00:00:00 2001 From: Mohammed AlShannaq Date: Tue, 26 Dec 2023 18:46:22 +0300 Subject: [PATCH 4/8] lang: add language string --- app/Language/ar/Common.php | 7 +++++++ app/Language/en/Common.php | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/app/Language/ar/Common.php b/app/Language/ar/Common.php index 3fe2382..06ee36d 100644 --- a/app/Language/ar/Common.php +++ b/app/Language/ar/Common.php @@ -22,4 +22,11 @@ 'ajaxErrorTitle' => 'خطا', 'ajaxCallError1' => 'لا يمكن اتمام طلبك الان ، تاكد من اتصالك بالانترنت وحاول مجددا', 'ajaxCallErrorAjaxError' => 'حدث خطأ أثناء تحميل البيانات. حاول مرة اخرى.', + + 'CountsTotalAllUrl' => 'عدد جميع الروابط', + 'CountsThisMonthAllUrl' => 'عدد الروابط المنشئة هذا الشهر', + 'CountsTodayAllUrl' => 'عدد الروابط المنشئة هذا اليوم', + 'CountsTotalAllHits' => 'عدد زيارات الروابط المسجلة', + 'CountsThisMonthAllHits' => 'عدد زيارات الروابط لهذا الشهر', + 'CountsTodayAllHits' => 'عدد زيارات الروابط اليوم', ]; diff --git a/app/Language/en/Common.php b/app/Language/en/Common.php index d5d833e..12c119e 100644 --- a/app/Language/en/Common.php +++ b/app/Language/en/Common.php @@ -22,4 +22,11 @@ '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.', + + 'CountsTotalAllUrl' => 'Total of All URLs', + 'CountsThisMonthAllUrl' => 'This Month Created URLs Count', + 'CountsTodayAllUrl' => 'Today Created URL Count', + 'CountsTotalAllHits' => 'Total of All URL visits', + 'CountsThisMonthAllHits' => 'This Month URL visits', + 'CountsTodayAllHits' => 'Today URL visits', ]; From 19792574cd0c1388aae07f79ccec4d7eba8850eb Mon Sep 17 00:00:00 2001 From: Mohammed AlShannaq Date: Tue, 26 Dec 2023 18:49:30 +0300 Subject: [PATCH 5/8] docs: remove duplicates --- docs/developers.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/developers.md b/docs/developers.md index f7238db..9363428 100644 --- a/docs/developers.md +++ b/docs/developers.md @@ -4,7 +4,7 @@ - You need a web hosting account (for a domain or sub-domain) with PHP 8.1 or higher support and the following PHP extensions (typically supported by most PHP hosting providers): - - [mysqlnd](http://php.net/manual/en/mysqlnd.install.php) , [intl](http://php.net/manual/en/intl.requirements.php) , [mbstring](http://php.net/manual/en/mbstring.installation.php) , [libcurl](https://www.php.net/manual/en/curl.setup.php) , [gmp](https://www.php.net/manual/en/gmp.installation.php) , [json](https://www.php.net/manual/en/json.installation.php), [bcmath](https://www.php.net/manual/en/bc.setup.php), [gmp](https://www.php.net/manual/en/gmp.setup.php) + - [mysqlnd](http://php.net/manual/en/mysqlnd.install.php) , [intl](http://php.net/manual/en/intl.requirements.php) , [mbstring](http://php.net/manual/en/mbstring.installation.php) , [libcurl](https://www.php.net/manual/en/curl.setup.php) , [gmp](https://www.php.net/manual/en/gmp.installation.php) , [json](https://www.php.net/manual/en/json.installation.php), [bcmath](https://www.php.net/manual/en/bc.setup.php) - Your web hosting account should have MySQL 8.0+ support - You need yo use `composer` and `php spark` command , therefor you must have SSH Access to your host account. From d68bfa0f1e6247c4e696687ad283cc678ac6a704 Mon Sep 17 00:00:00 2001 From: Mohammed AlShannaq Date: Wed, 27 Dec 2023 13:42:04 +0300 Subject: [PATCH 6/8] lang: add language strings --- app/Language/ar/Common.php | 4 ++++ app/Language/en/Common.php | 15 +++++++++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/app/Language/ar/Common.php b/app/Language/ar/Common.php index 06ee36d..1ca2fb6 100644 --- a/app/Language/ar/Common.php +++ b/app/Language/ar/Common.php @@ -29,4 +29,8 @@ 'CountsTotalAllHits' => 'عدد زيارات الروابط المسجلة', 'CountsThisMonthAllHits' => 'عدد زيارات الروابط لهذا الشهر', 'CountsTodayAllHits' => 'عدد زيارات الروابط اليوم', + + 'MyURLsHitCountAllTime' => 'اجمالي زيارات روابطي', + 'MyURLsHitCountThisMonth' => 'زيارات روابطي هذا الشهر', + 'MyURLsHitCountToday' => 'زيارات روابطي اليوم', ]; diff --git a/app/Language/en/Common.php b/app/Language/en/Common.php index 12c119e..5395dba 100644 --- a/app/Language/en/Common.php +++ b/app/Language/en/Common.php @@ -23,10 +23,13 @@ '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.', - 'CountsTotalAllUrl' => 'Total of All URLs', - 'CountsThisMonthAllUrl' => 'This Month Created URLs Count', - 'CountsTodayAllUrl' => 'Today Created URL Count', - 'CountsTotalAllHits' => 'Total of All URL visits', - 'CountsThisMonthAllHits' => 'This Month URL visits', - 'CountsTodayAllHits' => 'Today URL visits', + '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', ]; From ae9722536b5ed9fe9a3a797b73232fafd67832de Mon Sep 17 00:00:00 2001 From: Mohammed AlShannaq Date: Wed, 27 Dec 2023 13:54:09 +0300 Subject: [PATCH 7/8] lang: style language file --- app/Language/ar/Common.php | 14 +++++++------- app/Language/en/Common.php | 1 + 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/app/Language/ar/Common.php b/app/Language/ar/Common.php index 1ca2fb6..4c5f7e8 100644 --- a/app/Language/ar/Common.php +++ b/app/Language/ar/Common.php @@ -23,13 +23,13 @@ 'ajaxCallError1' => 'لا يمكن اتمام طلبك الان ، تاكد من اتصالك بالانترنت وحاول مجددا', 'ajaxCallErrorAjaxError' => 'حدث خطأ أثناء تحميل البيانات. حاول مرة اخرى.', - 'CountsTotalAllUrl' => 'عدد جميع الروابط', - 'CountsThisMonthAllUrl' => 'عدد الروابط المنشئة هذا الشهر', - 'CountsTodayAllUrl' => 'عدد الروابط المنشئة هذا اليوم', - 'CountsTotalAllHits' => 'عدد زيارات الروابط المسجلة', - 'CountsThisMonthAllHits' => 'عدد زيارات الروابط لهذا الشهر', - 'CountsTodayAllHits' => 'عدد زيارات الروابط اليوم', - + // 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 5395dba..3d254fb 100644 --- a/app/Language/en/Common.php +++ b/app/Language/en/Common.php @@ -23,6 +23,7 @@ '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', From 2edb190ab1d457bf2636c7cff8bac5bd1710ae71 Mon Sep 17 00:00:00 2001 From: Mohammed AlShannaq Date: Wed, 27 Dec 2023 13:55:08 +0300 Subject: [PATCH 8/8] feat: URLs Statics in Dashboard #113 --- app/Config/Smartyurl.php | 10 ++ app/Controllers/Dashboard.php | 71 ++++++++- app/Models/UrlHitsModel.php | 28 ++++ app/Models/UrlModel.php | 31 ++++ app/Views/basic/dashboard.php | 262 +++++++++++++++++++++++++--------- 5 files changed, 336 insertions(+), 66 deletions(-) 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/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

-
-
- -
-
- -
-
-
- -
- + + +
+ + + + + + + + + + + + + + + + +
-
+
+
+ + + - - + +
+ +
+
+ + + + +
+ +
- + +
+ +
+ + + + - - + + + + + +
+
+
+ + + - - + +