Skip to content

Commit

Permalink
UPDATE CMS 5 compatibility (#14)
Browse files Browse the repository at this point in the history
* UPDATE initial CMS 5 upgrade

* UPDATE rework violator to bootstrap alert

* UPDATE link field version

* UPDATE allow setting a 1 year cookie for violators if they’re closed

UPDATE allow specifying/changing the cookie name for flexibility

* UPDATE php matrix

* UPDATE require-dev

* UPDATE cli.yml

* UPDATE require-dev elemental

* UPDATE move extension fields to models

* UPDATE popup model update to support Show Once and cookie name

UPDATE popup template

* UPDATE logic for single popup based on all available

UPDATE phpcs.xml.dist

* UPDATE PHPCS adjustments

UPDATE remove unused files
  • Loading branch information
muskie9 authored Nov 12, 2024
1 parent 8f1f1a5 commit 18977f5
Show file tree
Hide file tree
Showing 19 changed files with 439 additions and 330 deletions.
72 changes: 5 additions & 67 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,73 +1,11 @@
# https://help.github.com/en/categories/automating-your-workflow-with-github-actions
name: CI

on:
push:
branches: [ '**' ]
pull_request:
branches: [ '**' ]

name: "CI"
workflow_dispatch:

jobs:
tests:
name: "Tests"

runs-on: "ubuntu-latest"

env:
php_extensions: ctype, dom, fileinfo, hash, intl, mbstring, session, simplexml, tokenizer, xml, pdo, mysqli, gd, zip

services:
mysql:
image: "mysql:5.7"
env:
MYSQL_ALLOW_EMPTY_PASSWORD: true
MYSQL_ROOT_PASSWORD:
MYSQL_DATABASE: test_db
ports:
- 3306/tcp
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

strategy:
fail-fast: false
matrix:
php-version:
- "7.4"

steps:
- name: "Checkout"
uses: "actions/checkout@v2"

- name: "Install PHP with extensions"
uses: "shivammathur/setup-php@v2"
with:
php-version: "${{ matrix.php-version }}"
extensions: "${{ env.php_extensions }}"
coverage: "xdebug"

- name: "Start mysql service"
run: "sudo /etc/init.d/mysql start"

- name: "Cache dependencies installed with composer"
uses: "actions/cache@v1"
with:
path: "~/.composer/cache"
key: "php${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-${{ hashFiles('**/composer.json') }}"
restore-keys: "php${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-"

- name: "Install dependencies with composer"
run: "composer install --no-ansi --no-interaction --no-progress"

- name: "Run tests with phpunit/phpunit"
env:
SS_DATABASE_PORT: ${{ job.services.mysql.ports['3306'] }}
run: "vendor/bin/phpunit --coverage-html=build/logs/coverage/html --coverage-xml=build/logs/coverage"

- name: "Upload code coverage results to CodeCov"
uses: codecov/codecov-action@v1
with:
files: ./coverage.xml # optional
flags: unittests # optional

- name: "Run tests with squizlabs/php_codesniffer"
run: "vendor/bin/phpcs -s --report=summary --standard=phpcs.xml.dist --extensions=php,inc --ignore=autoload.php --ignore=vendor/ src/ tests/"
ci:
name: CI
uses: silverstripe/gha-ci/.github/workflows/ci.yml@v1
15 changes: 0 additions & 15 deletions .scrutinizer.yml

This file was deleted.

37 changes: 0 additions & 37 deletions .travis.yml

This file was deleted.

4 changes: 0 additions & 4 deletions _config.php

This file was deleted.

9 changes: 3 additions & 6 deletions _config/config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
---
name: site-notifications-config
After:
- '#rootroutes'
- '#coreroutes'
---
SilverStripe\Control\Director:
rules:
Expand All @@ -8,9 +11,3 @@ SilverStripe\Control\Director:
PageController:
extensions:
- Dynamic\Notifications\Extension\PageControllerExtension
---
Only:
moduleexists: 'ryanpotter/silverstripe-cms-theme'
---
Dynamic\Notifications\Admin\SiteNotificationsAdmin:
menu_icon_class: 'far fa-bell'
62 changes: 46 additions & 16 deletions client/js/notifications.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,47 @@
;(function ($) {
$(window).on("load", function(){
$.ajax({
type: 'GET',
url: '/violatordata',
data: {
'isAjax': 1
}
})
.done(function (response) {
$('.violators').html(response)
})
.fail(function (xhr) {
console.log('Error: ' + xhr.responseText);
window.addEventListener('load', function() {
var xhr = new XMLHttpRequest();
xhr.open('GET', '/violatordata?isAjax=1', true);

xhr.onload = function() {
if (xhr.status >= 200 && xhr.status < 400) {
document.querySelector('.violators').innerHTML = xhr.responseText;
var violators = document.querySelectorAll('.violators__violator[data-cookiename]');
violators.forEach(function(violator) {
var cookieName = violator.getAttribute('data-cookiename');
if (getCookie(cookieName)) {
violator.style.display = 'none';
} else {
violator.querySelector('.btn-close').addEventListener('click', function() {
setCookie(cookieName, 'true', 365);
});
}
});
});
})(jQuery)
} else {
console.error('Error: ' + xhr.responseText);
}
};

xhr.onerror = function() {
console.error('Request failed');
};

xhr.send();

function setCookie(name, value, days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
var expires = "expires=" + date.toUTCString();
document.cookie = name + "=" + value + ";" + expires + ";path=/";
}

function getCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
return null;
}
});
50 changes: 50 additions & 0 deletions client/js/popup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
document.addEventListener('DOMContentLoaded', function() {
// Function to set a cookie
function setCookie(name, value, days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
}

// Function to get a cookie
function getCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
return null;
}

// Function to initialize modals
function initializeModals() {
document.querySelectorAll('.popup__modal').forEach(function(modal) {
var cookieName = modal.getAttribute('data-cookiename');

if (!cookieName || !getCookie(cookieName)) {
var bootstrapModal = new bootstrap.Modal(modal);
bootstrapModal.show();

modal.querySelector('.btn-close').addEventListener('click', function() {
if (cookieName) {
setCookie(cookieName, 'true', 365); // Set cookie for 1 year
}
});
}
});
}

// Check if Bootstrap is loaded and initialize modals
var bootstrapCheckInterval = setInterval(function() {
if (typeof bootstrap !== 'undefined') {
clearInterval(bootstrapCheckInterval);
initializeModals();
}
}, 100);
});
11 changes: 4 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
],
"license": "BSD-3-Clause",
"require": {
"dynamic/silverstripe-linkable": "^1.0",
"silverstripe/recipe-cms": "^4.0"
"silverstripe/linkfield":"^4.0",
"silverstripe/recipe-cms": "^5.0"
},
"require-dev": {
"phpunit/phpunit": "^5.7",
"squizlabs/php_codesniffer": "^3.0"
"silverstripe/recipe-testing": "^3",
"dnadesign/silverstripe-elemental": "^5"
},
"autoload": {
"psr-4": {
Expand All @@ -29,9 +29,6 @@
}
},
"extra": {
"branch-alias": {
"dev-master": "2.x-dev"
},
"expose": [
"client/js"
]
Expand Down
29 changes: 9 additions & 20 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
<?xml version="1.0"?>
<ruleset name="SS4">
<description>Coding standard for SilverStripe 4.x</description>
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="Silverstripe">
<description>CodeSniffer ruleset for Silverstripe coding conventions.</description>

<!-- Don't sniff third party libraries -->
<exclude-pattern>*/vendor/*</exclude-pattern>
<exclude-pattern>*/thirdparty/*</exclude-pattern>
<file>src</file>
<file>tests</file>

<!-- Show progress and output sniff names on violation, and add colours -->
<arg value="sp"/>
<arg name="colors"/>

<!-- Use PSR-2 as a base standard -->
<rule ref="PSR2">
<!-- Allow classes to not declare a namespace -->
<exclude name="PSR1.Classes.ClassDeclaration.MissingNamespace"/>

<!-- Allow underscores in class names -->
<exclude name="Squiz.Classes.ValidClassName.NotCamelCaps"/>

<!-- Allow non camel cased method names -->
<exclude name="PSR1.Methods.CamelCapsMethodName.NotCamelCaps"/>
<!-- base rules are PSR-12 -->
<rule ref="PSR12" >
<!-- You may need to use this exclusion if you override some core methods -->
<!--<exclude name="PSR1.Methods.CamelCapsMethodName.NotCamelCaps" />-->
</rule>
</ruleset>
30 changes: 20 additions & 10 deletions src/Admin/SiteNotificationsAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use Dynamic\Notifications\Model\PopUp;
use Dynamic\Notifications\Model\Violator;
use SilverStripe\Admin\ModelAdmin;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\GridField\GridField;
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;

/**
* Class SiteNotificationsAdmin
Expand All @@ -15,32 +18,39 @@ class SiteNotificationsAdmin extends ModelAdmin
/**
* @var string
*/
private static $url_segment = 'notificatiopns-admin';
private static string $url_segment = 'notificatiopns-admin';

/**
* @var string
*/
private static $menu_title = 'Site Notifications';
private static string $menu_title = 'Site Notifications';

/**
* @var string
*/
private static string $menu_icon_class = 'font-icon-attention';

/**
* @var array
*/
private static $managed_models = [
private static array $managed_models = [
Violator::class,
PopUp::class,
];

/**
* @param null $id
* @param null $fields
* @return mixed
* @param $id
* @param $fields
* @return Form
*/
public function getEditForm($id = null, $fields = null)
public function getEditForm($id = null, $fields = null): Form
{
$form = parent::getEditForm($id, $fields);
//This check is simply to ensure you are on the managed model you want adjust accordingly
if ($this->modelClass == Violator::class &&
$gridField = $form->Fields()->dataFieldByName($this->sanitiseClassName($this->modelClass))) {
//This check is simply to ensure you are on the managed model you want to adjust accordingly
if (
$this->modelClass == Violator::class &&
$gridField = $form->Fields()->dataFieldByName($this->sanitiseClassName($this->modelClass))
) {
//This is just a precaution to ensure we got a GridField from dataFieldByName() which you should have
if ($gridField instanceof GridField) {
$gridField->getConfig()->addComponent(new GridFieldOrderableRows('Sort'));
Expand Down
Loading

0 comments on commit 18977f5

Please sign in to comment.