Skip to content

Commit

Permalink
Add initial codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
mlewis-everley committed Aug 8, 2016
0 parents commit dde8257
Show file tree
Hide file tree
Showing 12 changed files with 403 additions and 0 deletions.
28 changes: 28 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Copyright (c) 2015, ilateral
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of silverstripe-contacts nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Silverstripe System Messages Module
===================================

Module for the Silverstripe CMS that adds content manageable messages to a site
(that can be closed by via a user) via a template variable.

## Author
This module was created by [i-lateral](http://www.i-lateral.com).

## Installation

Preferable you should installl this via composer using:
# composer require "i-lateral/silverstripe-systemmessages:0.*"

Alternativley install this module by downloading and adding to:

[silverstripe-root]/systemmessages

Then run:

http://yoursiteurl.com/dev/build/?flush=1

Or:

# sake dev/build flush=1

## Usage

Once installed, you must add the template variable:

$SystemMessages.RenderedMessage

to any templates you require messages to appear on.

You can manage messages in the admin area using the
"Messages" tab.
1 change: 1 addition & 0 deletions _config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
11 changes: 11 additions & 0 deletions _config/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
Name: systemmessageconfig
---
## Extensions
Controller:
extensions:
- SystemMessageControllerExtension
Member:
extensions:
- SystemMessageMemberExtension

59 changes: 59 additions & 0 deletions code/SystemMessages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/**
* Simple object to hold generic settings and functions
* for SystemMessages object.
*
* @package SystemMessages
* @author Mo <[email protected]>
*/
class SystemMessages extends ViewableData
{

/**
* Get the most recent, open system message for the current
* user.
*
* @return SystemMessage
*/
public function Message() {
return $this->OpenMessages()->first();
}

/**
* Get the most recent message and render into a template
*
* @return string HTML of the message
*/
public function RenderedMessage() {
return $this->renderWith(
"SystemMessage",
$this->Message()
);
}

public function OpenMessages() {
$now = SS_Datetime::now()->Value;
$return = ArrayList::create();
$member = Member::currentUser();
$filter = array(
"StartDate:LessThan" => $now,
"ExpiryDate:GreaterThan" => $now
);

// Get all applicable messages
$messages = SystemMessage::get()
->filter($filter)
->sort("StartDate", "ASC");

// Loop through messages and only add relevent to the list
foreach ($messages as $message) {
if ($message->isOpen($member)) {
$return->add($message);
}
}

return $return;
}

}
13 changes: 13 additions & 0 deletions code/admin/SystemMessageAdmin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

/**
* Admin interface
*
*/
class SystemMessageAdmin extends ModelAdmin
{
private static $managed_models = array(
"SystemMessage"
);

}
40 changes: 40 additions & 0 deletions code/extensions/SystemMessageControllerExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

class SystemMessageControllerExtension extends Extension
{
private static $allowed_actions = array(
"closesystemmessage"
);

public function SystemMessages()
{
return SystemMessages::create();
}

/**
* Close the message passed by the URL's ID and return.
* If the user is currently logged in, then mark it against
* their account, else drop a cookie.
*
* @return SS_Response
*/
public function closesystemmessage()
{
$id = $this->owner->request->param("ID");
$message = SystemMessage::get()->byID($id);
$member = Member::currentUser();

// If not a message then generate an error
if (!$message) {
return $this->owner->httpError(500);
}

if ($member) {
$message->close($member);
} else {
$message->close();
}

return $this->owner->redirectBack();
}
}
8 changes: 8 additions & 0 deletions code/extensions/SystemMessageMemberExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

class SystemMessageMemberExtension extends Extension
{
private static $many_many = array(
"ClosedMessages" => "SystemMessage"
);
}
96 changes: 96 additions & 0 deletions code/model/SystemMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

/**
* A system message that can be loaded onto the front end of a site and dismissed
* via a button click.
*
* @package SystemMessage
* @author Mo <[email protected]>
*/
class SystemMessage extends DataObject
{
private static $db = array(
"Content" => "HTMLText",
"ButtonText" => "Varchar",
"StartDate" => "SS_Datetime",
"ExpiryDate" => "SS_Datetime"
);

private static $has_one = array(
'Link' => 'Link'
);

private static $belongs_many_many = array(
"ClosedBy" => "Member"
);

/**
* Link to close this message
*
* @return string
*/
public function CloseLink()
{
return Controller::curr()->Link("closesystemmessage");
}

/**
* Has the current message been closed by the selected member
*
* @param Member $member A member object to check
* @return Boolean
*/
public function isClosedByMember(Member $member)
{
$match = $this->ClosedBy()->byID($member->ID);
return ($match) ? true : false;
}

/**
* Is the current message closed (either by the passed member or a
* cookie)
*
* @param Int $memberID ID of member to search for (if not set current member is used)
* @return Boolean
*/
public function isClosed(Member $member = null)
{
$match = false;

if ($member) {
$match = $this->isClosedByMember($member);
} else {
$match = Cookie::get("systemmessage.closed.{$this->ID}");
}

return ($match) ? true : false;
}

/**
* Is the current message open (either by the passed member or a
* cookie)
*
* @param Int $memberID ID of member to search for (if not set current member is used)
* @return Boolean
*/
public function isOpen(Member $member = null)
{
return !$this->isClosed($member);
}

/**
* Close this message for the current user, either
* via the database or by a cookie
*
* @param Member $member If we want to close for a member
* @return NULL
*/
public function Close(Member $member = null)
{
if ($member) {
$member->ClosedMessages()->add($this);
} else {
Cookie::set("systemmessage.closed.{$this->ID}", true);
}
}
}
28 changes: 28 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "i-lateral/silverstripe-systemmessages",
"description": "Add popup notification messages to a Silverstripe site",
"type": "silverstripe-module",
"homepage": "http://github.com/i-lateral/silverstripe-systemmessages",
"keywords": ["silverstripe", "messages", "notifications"],
"license": "BSD-3-Clause",
"authors": [
{
"name": "Morven Lewis-Everley",
"email": "[email protected]"
},
{
"name": "Mark Anderson",
"email": "[email protected]"
}
],
"support": {
"issues": "http://github.com/i-lateral/silverstripe-systemmessages/issues"
},
"require": {
"silverstripe/framework": ">=3.1",
"sheadawson/silverstripe-linkable": "1.1.*"
},
"extra": {
"installer-name": "carousel"
}
}
Loading

0 comments on commit dde8257

Please sign in to comment.