public function index() { $f3 = \Base::instance(); $this->_requireLogin(); $user = $f3->get('user'); $user_obj = $f3->get('user_obj'); $user_org_links = $f3->get('user_org_links'); $db = $f3->get('db.instance'); // Redirect to welcome page if user has no organisations if (count($user_org_links) == 0) { $f3->reroute('/dashboard/welcome'); } /* * * HOME PAGE STATS * */ // Use the organisation the user selected, if none take first in database if ($f3->exists('SESSION.selected_organisation')) { $orgId = $f3->get('SESSION.selected_organisation'); foreach ($user_org_links as $orgKey => $orgValue) { if ($orgValue['orgId'] == $orgId) { $validated = true; } } } if (!isset($validated)) { // Select first $orgId = $user_org_links[0]['orgId']; } $f3->set('user_org_selected', (new \Models\Organisation())->load($orgId)->cast()); // Newsletter opens $result = $f3->get('db.instance')->exec('SELECT COUNT(*) AS `count` FROM newsletter_opens WHERE orgId = ? AND DATE(open_time) > DATE_SUB(CURDATE(), INTERVAL 7 DAY)', $orgId); $f3->set('stats_views', $result[0]['count']); // Chart stats $f3->set('stats_chart_views', json_encode(Organisations::views($orgId))); $f3->set('stats_chart_sending', json_encode(Organisations::sends($orgId))); // Subscriptions and Unsubscriptions $result = $db->exec('SELECT COUNT(*) AS `count` FROM organisation_subs WHERE orgId = ? AND DATE(sub_time) > DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND unsub_time = NULL', $orgId); $f3->set('stats_subs', $result[0]['count']); $result = $db->exec('SELECT COUNT(*) AS `count` FROM organisation_subs WHERE orgId = ? AND DATE(sub_time) > DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND unsub_time IS NOT NULL', $orgId); $f3->set('stats_unsubs', $result[0]['count']); // Stats Subscriptions // Stats mails sent $result = $db->exec('SELECT COUNT(*) AS `count` FROM mails_sent WHERE orgId = ? AND DATE(sent_time) > DATE_SUB(CURDATE(), INTERVAL 7 DAY)', $orgId); $f3->set('stats_mails_sent', $result[0]['count']); // Organisation subscription details $orgSub = new \Models\Subscriptions(); $orgSub->load(array('orgId = ?', $orgId)); $f3->set('org_sub', $orgSub->calculate()); // Display a notification to masquerading administrators if ($f3->exists('SESSION.mask')) { new Notification('You are currently masquerading as a client, <a href="/dashboard/admin/masquerade/reveal">back to your admin account</a>', 'danger', true); } $f3->set('target', 'dashboard/index/index.html'); $this->_render('base.html'); }
/** * Shows the recent SquareMS update log and the user's newsletters statistics (or a button "Create a newsletter") * * @param $f3 */ public function home() { $f3 = \Base::instance(); $this->_requireLogin(); $user = $f3->get('user'); $user_obj = $f3->get('user_obj'); $user_org_links = $f3->get('user_org_links'); // Create new organisation if the user has none if ($f3->exists('POST.organisationName')) { $org = $f3->get('POST.organisationName'); $org = $f3->scrub($org); if (strlen($org) < 4) { $notif = new Notification(); $notif->title = 'Error'; $notif->body = 'Organisation name must be at least 4 long.'; $notif->save(); } else { $orgEntry = new \Models\Organisation(); $orgEntry->name = $org; $orgEntry->desc = NULL; $orgEntry->ownerId = $user['id']; $orgEntry->save(); // Create a new org member entry and give the user all rights to it (fields starting with 'r' for rights) $orgMemberEntry = new \Models\OrganisationMembers(); $orgMemberEntry->orgId = $orgEntry->id; $orgMemberEntry->memberId = $user['id']; $orgMemberEntry->rEdit = true; $orgMemberEntry->rSend = true; $orgMemberEntry->save(); // Create a new subsription entry $orgSubEntry = new \Models\Subscriptions(); $orgSubEntry->orgId = $orgEntry->id; $orgSubEntry->planId = 1; $orgSubEntry->payDay = date("d"); $orgSubEntry->save(); $f3->reroute($f3->get('PATH')); } } if (count($user_org_links) == 0) { $f3->set('target', 'dashboard/index/first-org.html'); } else { // Home-page stats if ($f3->exists('SESSION.selected_organisation')) { $orgId = $f3->get('SESSION.selected_organisation'); foreach ($user_org_links as $orgKey => $orgValue) { if ($orgValue['orgId'] == $orgId) { $validated = true; } } } if (!isset($validated)) { // Select first $orgId = $user_org_links[0]['orgId']; } // Gather some stats $result = $f3->get('db.instance')->exec('SELECT COUNT(*) AS `count` FROM newsletter_opens WHERE orgId = ? AND DATE(open_time) > DATE_SUB(CURDATE(), INTERVAL 7 DAY)', $orgId); $f3->set('stats_views', $result[0]['count']); // $result = $f3->get('db.instance')->exec('SELECT COUNT(*) AS `count` FROM newsletter_unsub WHERE orgId = ? AND DATE(open_time) > DATE_SUB(CURDATE(), INTERVAL 7 DAY)', $orgId); // $f3->set('stats_unsub', $result[0]['count']); // Gather some stats for the chart $f3->set('stats_chart_views', json_encode(Organisations::views($orgId))); $f3->set('stats_chart_sending', json_encode(Organisations::sends($orgId))); $orgMap = new \Models\Organisation(); $orgMap->load($orgId); $f3->set('user_org_selected', $orgMap->cast()); // Stats Subscriptions $db = $f3->get('db.instance'); $result = $db->exec('SELECT COUNT(*) AS `count` FROM organisation_subs WHERE orgId = ? AND DATE(sub_time) > DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND unsub_time = NULL', $orgId); $f3->set('stats_subs', $result[0]['count']); $result = $db->exec('SELECT COUNT(*) AS `count` FROM organisation_subs WHERE orgId = ? AND DATE(sub_time) > DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND unsub_time IS NOT NULL', $orgId); $f3->set('stats_unsubs', $result[0]['count']); // Stats mails sent $result = $db->exec('SELECT COUNT(*) AS `count` FROM mails_sent WHERE orgId = ? AND DATE(sent_time) > DATE_SUB(CURDATE(), INTERVAL 7 DAY)', $orgId); $f3->set('stats_mails_sent', $result[0]['count']); // Organisation subscription details $orgSub = new \Models\Subscriptions(); $orgSub->load(array('orgId = ?', $orgId)); $f3->set('org_sub', $orgSub->calculate()); $f3->set('target', 'dashboard/index/index.html'); if ($f3->exists('SESSION.mask')) { $notif = new Notification(); $notif->text = 'You are currently masquerading as a client, <a href="/dashboard/admin/masquerade/reveal">back to your admin account</a>'; $notif->type = 'danger'; $notif->save(); } } $this->_render('base.html'); }