Skip to content

Commit

Permalink
working
Browse files Browse the repository at this point in the history
  • Loading branch information
ebakernz committed Sep 19, 2024
1 parent d47751d commit 2badcee
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 3 deletions.
Empty file added .gitignore
Empty file.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Dashboard

CMS dashboard, uses plastyk/silverstripe-dashboard and extends with custom panels.
# Member Activity

SilverStripe add on to track member activity
For now - trackes date last visited and number of visits
Future - could add others
1 change: 1 addition & 0 deletions README.mdgit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# cms-dashboard initgit add README.mdgit commit -m first commitgit branch -M maingit remote add origin [email protected]:PlasticStudio/cms-dashboard.gitgit push -u origin main
1 change: 1 addition & 0 deletions _config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
3 changes: 3 additions & 0 deletions _config/extensions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SilverStripe\Security\Member:
extensions:
- PlasticStudio\Extensions\MemberExtension
29 changes: 29 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "plasticstudio/member-activity",
"type": "silverstripe-vendormodule",
"description": "SilverStripe add on to track member activity",
"homepage": "https://psdigital.co.nz",
"keywords": ["silverstripe","member"],
"license": "BSD-3-Clause",
"authors": [
{
"name": "Emma Baker",
"homepage": "https://psdigital.co.nz",
"email": "[email protected]"
}
],
"support": {
"issues": "http://github.com/plasticstudio/dashboard/issues"
},
"extra": {
"expose": [
"client"
]
},
"require": {
"silverstripe/cms": "^4 || ^5",
"silverstripe/framework": "^4 || ^5",
"silverstripe/vendor-plugin": "^1 || ^2"
},
"minimum-stability": "dev"
}
60 changes: 60 additions & 0 deletions src/Extensions/MemberExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Skeletor\Extensions;

use SilverStripe\ORM\DB;
use SilverStripe\ORM\DataObject;
use SilverStripe\Forms\FieldList;
use SilverStripe\Security\Member;
use SilverStripe\ORM\DataExtension;
use SilverStripe\Security\Security;
use SilverStripe\Forms\ReadonlyField;

class MemberExtension extends DataExtension
{
// https://docs.silverstripe.org/en/5/developer_guides/extending/how_tos/track_member_logins/

private static $db = [
'LastVisited' => 'Datetime',
'NumVisit' => 'Int',
];

/**
* This extension hook is called every time a member is logged in
*/
public function afterMemberLoggedIn()
{
$this->logVisit();
}

/**
* This extension hook is called when a member's session is restored from "remember me" cookies
*/
public function memberAutoLoggedIn()
{
$this->logVisit();
}

public function updateCMSFields(FieldList $fields)
{
$fields->addFieldsToTab('Root.Main', [
ReadonlyField::create('LastVisited', 'Last visited'),
ReadonlyField::create('NumVisit', 'Number of visits'),
]);
}

protected function logVisit()
{
if (!Security::database_is_ready()) {
return;
}

$lastVisitedTable = DataObject::getSchema()->tableForField(Member::class, 'LastVisited');

DB::query(sprintf(
'UPDATE "' . $lastVisitedTable . '" SET "LastVisited" = %s, "NumVisit" = "NumVisit" + 1 WHERE "ID" = %d',
DB::get_conn()->now(),
$this->owner->ID
));
}
}

0 comments on commit 2badcee

Please sign in to comment.