Skip to content

Commit

Permalink
Initial module logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Jackson committed Jul 29, 2018
1 parent d97622a commit c3120a1
Show file tree
Hide file tree
Showing 18 changed files with 1,486 additions and 1 deletion.
29 changes: 29 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# For more information about the properties used in
# this file, please see the EditorConfig documentation:
# http://editorconfig.org/

root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[*.yml]
indent_size = 2
indent_style = space

[*.{yml,json}]
# The indent size used in the `package.json` file cannot be changed
# https://github.com/npm/npm/pull/3180#issuecomment-16336516
indent_size = 2
indent_style = space

[composer.json]
indent_size = 4
5 changes: 5 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/tests export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore
/.scrutinizer.yml export-ignore
15 changes: 15 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
inherit: true

build:
nodes:
analysis:
tests:
override: [php-scrutinizer-run]

checks:
php:
code_rating: true
duplication: true

filter:
paths: [src/*, tests/*]
36 changes: 36 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
language: php

dist: trusty

env:
global:
- COMPOSER_ROOT_VERSION=4.1.x-dev

matrix:
include:
- php: 5.6
env: DB=MYSQL RECIPE_VERSION=1.0.x-dev PHPCS_TEST=1 PHPUNIT_TEST=1
- php: 7.0
env: DB=MYSQL RECIPE_VERSION=1.1.x-dev PHPUNIT_TEST=1
- php: 7.1
env: DB=MYSQL RECIPE_VERSION=4.2.x-dev PHPUNIT_COVERAGE_TEST=1
- php: 7.2
env: DB=MYSQL RECIPE_VERSION=4.x-dev PHPUNIT_TEST=1

before_script:
# Init PHP
- phpenv rehash
- phpenv config-rm xdebug.ini

# Install composer dependencies
- composer validate
- composer require --no-update silverstripe/recipe-cms:"$RECIPE_VERSION"
- composer install --prefer-dist --no-interaction --no-progress --no-suggest --optimize-autoloader --verbose --profile

script:
- if [[ $PHPUNIT_TEST ]]; then vendor/bin/phpunit tests/php; fi
- if [[ $PHPUNIT_COVERAGE_TEST ]]; then phpdbg -qrr vendor/bin/phpunit --coverage-clover=coverage.xml tests/php; fi
- if [[ $PHPCS_TEST ]]; then vendor/bin/phpcs src/ tests/; fi

after_success:
- if [[ $PHPUNIT_COVERAGE_TEST ]]; then bash <(curl -s https://codecov.io/bash) -f coverage.xml; fi
241 changes: 240 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,241 @@
# silverstripe-announce
Display announcement messages for SilverStripe

Announce things to the page controller & view in SilverStripe. Can be used to power plain messages, modals, alerts, and callouts.

Multiple announcements can be stacked.

Announcements can be templated to match component libraries such as Bootstrap.

## Installation

**Composer**

`composer require codecraft/silverstripe-announce`

## Introduction

Create announcements with Titles, Headings, Contents, Footers and Actions. Control whether they should be stored for later responses. Control whether they should be dismissable.

Announcements are retained at StilverStripe's `Controller`, and stored in the Announcement store (default is `$_SESSION`).

Each announcement is made available to the view via a SilverStripe `Controller` extension.

Only one set of announcements can be stacked at a time.

The announcement store can be replaced with a custom class that implements `AnnouncementStoreInterface`;

## Usage

Queue a new announcement
```
Announcements::queue('AnnouncementName', 'Announcement Title', 'This is an announcement message');
```
`Accounments::queue()` will accept all arguements for an `Announcement`

Queue an existing announcement

```
$msg = Announcement::create('AnnouncementName', 'Announcement Title', 'This is an announcement message');
Announcements::queue($msg);
```

Get all current announcements
```
$announcements = Announcements::get();
```

Get an announcement by name
```
Announcements::get()->getByName('AnnouncementName');
```

Get an announcement by type
```
Announcements::get()->getByType(Announcement::DEFAULT);
```

Clear the announcements queue
```
Announcements::clear();
```

Display announcements in the top scope of a template
```
<% if $Announcements %>
$Announcements
<% end_if %>
```

### Customise an announcement

Example of all options

```
use CodeCraft\Announce\Model\Announcement;
use CodeCraft\Announce\Model\Action;
...
$announcement = Announcement::create(
$name = 'AnnouncementName',
$title = 'Announcement Title',
$content = 'A plain or html string',
$heading = 'A plain or html string',
$footer = 'A plain or html string',
$actions = [
Action::create(
$name = 'ActionName',
$content = 'A plain or html string',
$type = Action::BUTTON,
$link = 'https://www.google.com/',
$extraClass = 'btn btn-primary'
)
],
$type = Announcement::DEFAULT
)
// Store announcement
->setStoreable(true)
// Dismissable
->setDismissable(true)
// Name
->setName('AnnouncementName')
// Title
->setTitle('Announcement Title')
->setTitle('<p>Announcement Title</p>')
// Content
->setContent('Announcement body')
->setContent('<p>Announcement body</p>')
// Heading
->setHeading('Announcement heading')
->setHeading('<p>Announcement heading</p>')
// Footer
->setFooter('Announcement footer')
->setFooter('<p>Announcement footer</p>')
// Action
->addAction(
Action::create()
->setName('ActionName')
->setContent('OK')
->setContent('<p>OK</p>')
->setType(Action::BUTTON)
->setLink('http://www.google.com/')
->addExtraClass('btn btn-primary')
->setAttribute('title', 'A helpful title')
)
// Type
->setType(Announcement::DEFAULT)
// Template
->setTemplate('templates\CodeCraft\Announce\Announcement');
```

### Announcement store

Access the announcement store
```
Announcements::get()->getStore();
```

Expect announcements to be stored as an `array`

#### Set storable

Announcements are stored by default, so that they can be included in the next relevant response.

Define if the announcement should be stored
```
$msg = Announcement::create('AnnouncementName', 'Un-stored Announcement', 'This announcement is not stored')
->setStoreable(false);
```

### Custom announcement template

Set a custom template for all announcements by [overloading templates](#overloading-templates)

Set a custom template for any announcement
```
$msg = Announcement::create('AnnouncementName', 'Announcement Title', 'This is an announcement message')
->setTemplate('MyTemplate');
```

### Overloading templates

**Announcement template**

Overload the default template for all announcement actions by overloading `templates/CodeCraft/Announce/Model/Announcement`

**Announcement Action template**

Overload the default template for all announcement actions by overloading `templates/CodeCraft/Announce/Model/Action`

Read more about SilverStripe [Template Inheritance](https://docs.silverstripe.org/en/4/developer_guides/templates/template_inheritance/)

### Announcements by name

Each announcement name is distinct. Call announcements by name

**Back-end**
```
Announcements::get()->getByName('AnnouncementName');
```

**Template**
```
$Announcements.ByName('AnnouncementName');
```

### Announcements by type

Set any announcement's type with one of the available types; `Announcement::DEFAULT`, `Announcement::MODAL`, `Announcement::TRAY`, `Announcement::MESSAGE`, or `Announcement::CALLOUT`.

```
$msg = Announcement::create('Name')->setType(Announcement::MODAL);
```

Call announcements by type

**Back-end**
```
Announcements::get()->getByType(Announcement::MODAL);
```

**Template**
```
$Announcements.ByType('modal');
```
_Can be 'default', 'modal', 'tray', 'message', 'callout'_

## Requirements

* SilverStripe >= 4.0
* PHP >= 5.6

## License

Modified BSD License

Copyright (c) 2018, Jackson Darlow

Read the [license](https://github.com/codecraft/silverstripe-announce/blob/master/LICENSE)

## To do

* Tests
* Add redundancies for cases where announcement store is ahead of announcement stack
* Add a way to disable the store
* Create a database announcement store
* Create a redis announcement store
* Replace Announcement type constants with an object-oriented model
* Create an email Announcement type
* An announcement history for user-focused announcements
* Flag announcement when viewed by user

6 changes: 6 additions & 0 deletions _config/announce.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
Name: announce
---
SilverStripe\Control\Controller:
extensions:
- CodeCraft\Announce\Extension\ControllerExtension
37 changes: 37 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "codecraft/silverstripe-announce",
"description": "Add a queue of announcements to the SilverStripe controller",
"keywords": ["silverstripe", "announcements", "modal", "callout", "popup", "alert", "message"],
"type": "silverstripe-vendormodule",
"license": "BSD-3-Clause",
"authors": [
{
"name": "Jackson Darlow",
"email": "[email protected]"
}
],
"support": {
"issues": "http://github.com/codecraft/silverstripe-announce/issues"
},
"require": {
"php": ">=5.6",
"silverstripe/framework": "^4"
},
"require-dev": {
"phpunit/phpunit": "^5.7"
},
"autoload": {
"psr-4": {
"CodeCraft\\Announce\\": "src/",
"CodeCraft\\Announce\\Tests\\": "tests/"
}
},
"extra": {
"branch-alias": {
"dev-master": "0.1.x-dev"
}
},
"target-dir": "announce",
"minimum-stability": "dev",
"prefer-stable": true
}
Loading

0 comments on commit c3120a1

Please sign in to comment.