Initial commit
This commit is contained in:
65
app/plugins/subscribe/controllers/admin/subscriptions.php
Normal file
65
app/plugins/subscribe/controllers/admin/subscriptions.php
Normal file
@ -0,0 +1,65 @@
|
||||
<?php
|
||||
/**
|
||||
* app/plugins/subscribe/controllers/admin/subscriptions.php
|
||||
*
|
||||
* This is the subscriptions admin controller.
|
||||
*
|
||||
* @package TP Subscribe
|
||||
* @version 3.0
|
||||
* @author Joey Kimsey <Joey@thetempusproject.com>
|
||||
* @link https://TheTempusProject.com
|
||||
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
|
||||
*/
|
||||
namespace TheTempusProject\Controllers\Admin;
|
||||
|
||||
use TheTempusProject\Bedrock\Functions\Input;
|
||||
use TheTempusProject\Bedrock\Functions\Check;
|
||||
use TheTempusProject\Houdini\Classes\Issues;
|
||||
use TheTempusProject\Houdini\Classes\Views;
|
||||
use TheTempusProject\Classes\AdminController;
|
||||
use TheTempusProject\Models\Subscribe;
|
||||
use TheTempusProject\Classes\Forms;
|
||||
|
||||
class Subscriptions extends AdminController {
|
||||
public static $subscribe;
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
self::$title = 'Admin - Email Subscribers';
|
||||
self::$subscribe = new Subscribe;
|
||||
}
|
||||
|
||||
public function delete( $data = null ) {
|
||||
if ( $data == null ) {
|
||||
if ( Input::exists( 'submit' ) ) {
|
||||
$data = Input::post( 'S_' );
|
||||
}
|
||||
}
|
||||
if ( !self::$subscribe->delete( (array) $data ) ) {
|
||||
Issues::add( 'error', 'There was an error with your request, please try again.' );
|
||||
} else {
|
||||
Issues::add( 'success', 'Subscriber removed.' );
|
||||
}
|
||||
$this->index();
|
||||
}
|
||||
|
||||
public function add( $data = null ) {
|
||||
if ( !Input::exists( 'submit' ) ) {
|
||||
return Views::view( 'subscribe.admin.add' );
|
||||
}
|
||||
if ( !Forms::check( 'subscribe' ) ) {
|
||||
Issues::add( 'error', [ 'There was an error with your request.' => Check::userErrors() ] );
|
||||
return $this->index();
|
||||
}
|
||||
if ( !self::$subscribe->add( Input::post( 'email' ) ) ) {
|
||||
Issues::add( 'error', 'There was an error with your request, please try again.' );
|
||||
return $this->index();
|
||||
}
|
||||
Issues::add( 'success', 'Subscriber added.' );
|
||||
$this->index();
|
||||
}
|
||||
|
||||
public function index( $data = null ) {
|
||||
Views::view( 'subscribe.admin.list', self::$subscribe->listPaginated() );
|
||||
}
|
||||
}
|
79
app/plugins/subscribe/controllers/subscribe.php
Normal file
79
app/plugins/subscribe/controllers/subscribe.php
Normal file
@ -0,0 +1,79 @@
|
||||
<?php
|
||||
/**
|
||||
* app/plugins/subscribe/controllers/subscribe.php
|
||||
*
|
||||
* This is the home controller for the subscribe plugin.
|
||||
*
|
||||
* @package TP Subscribe
|
||||
* @version 3.0
|
||||
* @author Joey Kimsey <Joey@thetempusproject.com>
|
||||
* @link https://TheTempusProject.com
|
||||
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
|
||||
*/
|
||||
namespace TheTempusProject\Controllers;
|
||||
|
||||
use TheTempusProject\Classes\Controller;
|
||||
use TheTempusProject\Classes\Forms;
|
||||
use TheTempusProject\Hermes\Functions\Redirect;
|
||||
use TheTempusProject\Bedrock\Functions\Session;
|
||||
use TheTempusProject\Bedrock\Functions\Input;
|
||||
use TheTempusProject\Bedrock\Functions\Check;
|
||||
use TheTempusProject\Classes\Email;
|
||||
use TheTempusProject\Houdini\Classes\Issues;
|
||||
use TheTempusProject\Houdini\Classes\Views;
|
||||
use TheTempusProject\Models\Subscribe as SubscribeModel;
|
||||
|
||||
class Subscribe extends Controller {
|
||||
private static $subscribe;
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
self::$subscribe = new SubscribeModel;
|
||||
}
|
||||
|
||||
public function index() {
|
||||
self::$title = 'Subscribe - {SITENAME}';
|
||||
self::$pageDescription = 'We are always publishing great content and keeping our members up to date. If you would like to join our list, you can subscribe here.';
|
||||
if ( !Input::exists( 'email' ) ) {
|
||||
return Views::view( 'subscribe.subscribe' );
|
||||
}
|
||||
if ( !Forms::check( 'subscribe' ) ) {
|
||||
Issues::add( 'error', [ 'There was an error with your form.' => Check::userErrors() ] );
|
||||
return Views::view( 'subscribe.subscribe' );
|
||||
}
|
||||
if ( !self::$subscribe->add( Input::post( 'email' ) ) ) {
|
||||
Issues::add( 'error', 'There was an error with your request, please try again.' );
|
||||
return Views::view( 'subscribe.subscribe' );
|
||||
}
|
||||
$data = self::$subscribe->get( Input::post( 'email' ) );
|
||||
Email::send( Input::post( 'email' ), 'subscribe', $data->confirmationCode, ['template' => true] );
|
||||
Issues::add( 'success', 'You have successfully been subscribed to our mailing list.' );
|
||||
}
|
||||
|
||||
public function unsubscribe( $email = null, $code = null ) {
|
||||
self::$title = '{SITENAME}';
|
||||
self::$pageDescription = '';
|
||||
if ( !empty( $email ) && !empty( $code ) ) {
|
||||
if ( self::$subscribe->unsubscribe( $email, $code ) ) {
|
||||
return Issues::add( 'success', 'You have been successfully unsubscribed from receiving further mailings.' );
|
||||
}
|
||||
Issues::add( 'error', 'There was an error with your request.' );
|
||||
return Views::view( 'subscribe.unsubscribe' );
|
||||
}
|
||||
if ( !Input::exists( 'submit' ) ) {
|
||||
return Views::view( 'subscribe.unsubscribe' );
|
||||
}
|
||||
if ( !Forms::check( 'unsubscribe' ) ) {
|
||||
Issues::add( 'error', [ 'There was an error with your request.' => Check::userErrors() ] );
|
||||
return Views::view( 'subscribe.unsubscribe' );
|
||||
}
|
||||
$data = self::$subscribe->get( Input::post( 'email' ) );
|
||||
if ( empty( $data ) ) {
|
||||
Issues::add( 'notice', 'There was an error with your request.' );
|
||||
return Views::view( 'subscribe.unsubscribe' );
|
||||
}
|
||||
Email::send( Input::post( 'email' ), 'unsubInstructions', $data->confirmationCode, ['template' => true] );
|
||||
Session::flash( 'success', 'An email with instructions on how to unsubscribe has been sent to your email.' );
|
||||
Redirect::to( 'home/index' );
|
||||
}
|
||||
}
|
73
app/plugins/subscribe/forms.php
Normal file
73
app/plugins/subscribe/forms.php
Normal file
@ -0,0 +1,73 @@
|
||||
<?php
|
||||
/**
|
||||
* app/plugins/subscribe/forms.php
|
||||
*
|
||||
* This houses all of the form checking functions for this plugin.
|
||||
*
|
||||
* @package TP Subscribe
|
||||
* @version 3.0
|
||||
* @author Joey Kimsey <Joey@thetempusproject.com>
|
||||
* @link https://TheTempusProject.com
|
||||
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
|
||||
*/
|
||||
namespace TheTempusProject\Plugins\Subscribe;
|
||||
|
||||
use TheTempusProject\Bedrock\Functions\Input;
|
||||
use TheTempusProject\Classes\Forms;
|
||||
|
||||
class SubscribeForms extends Forms {
|
||||
/**
|
||||
* Adds these functions to the form list.
|
||||
*/
|
||||
public function __construct() {
|
||||
self::addHandler( 'subscribe', __CLASS__, 'subscribe' );
|
||||
self::addHandler( 'unsubscribe', __CLASS__, 'unsubscribe' );
|
||||
self::addHandler( 'newSubscription', __CLASS__, 'newSubscription' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the subscribe form.
|
||||
*
|
||||
* @return {bool}
|
||||
*/
|
||||
public static function subscribe() {
|
||||
if ( !self::email( Input::post( 'email' ) ) ) {
|
||||
self::addUserError( 'Invalid email.' );
|
||||
return false;
|
||||
}
|
||||
if ( !self::token() ) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the unsubscribe form.
|
||||
*
|
||||
* @return {bool}
|
||||
*/
|
||||
public static function unsubscribe() {
|
||||
if ( !self::email( Input::post( 'email' ) ) ) {
|
||||
self::addUserError( 'Invalid email.' );
|
||||
return false;
|
||||
}
|
||||
if ( !self::token() ) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the new subscription form.
|
||||
*
|
||||
* @return {bool}
|
||||
*/
|
||||
public static function newSubscription() {
|
||||
if ( !self::token() ) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
new SubscribeForms;
|
97
app/plugins/subscribe/models/subscribe.php
Normal file
97
app/plugins/subscribe/models/subscribe.php
Normal file
@ -0,0 +1,97 @@
|
||||
<?php
|
||||
/**
|
||||
* app/plugins/subscribe/models/subscribe.php
|
||||
*
|
||||
* This class is used for the manipulation of the subscribers database table.
|
||||
*
|
||||
* @package TP Subscribe
|
||||
* @version 3.0
|
||||
* @author Joey Kimsey <Joey@thetempusproject.com>
|
||||
* @link https://TheTempusProject.com
|
||||
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
|
||||
*/
|
||||
namespace TheTempusProject\Models;
|
||||
|
||||
use TheTempusProject\Bedrock\Functions\Check;
|
||||
use TheTempusProject\Bedrock\Functions\Code;
|
||||
use TheTempusProject\Canary\Canary as Debug;
|
||||
use TheTempusProject\Classes\DatabaseModel;
|
||||
|
||||
class Subscribe extends DatabaseModel {
|
||||
public $tableName = 'subscribers';
|
||||
public $databaseMatrix = [
|
||||
[ 'confirmed', 'int', '1' ],
|
||||
[ 'subscribed', 'int', '10' ],
|
||||
[ 'confirmationCode', 'varchar', '80' ],
|
||||
[ 'email', 'varchar', '75' ],
|
||||
];
|
||||
|
||||
/**
|
||||
* Adds an email to the subscribers database.
|
||||
*
|
||||
* @param string $email - the email you are trying to add.
|
||||
* @return bool
|
||||
*/
|
||||
public function add( $email ) {
|
||||
if ( !Check::email( $email ) ) {
|
||||
return false;
|
||||
}
|
||||
$alreadyExists = self::$db->get( $this->tableName, ['email', '=', $email] );
|
||||
|
||||
if ( $alreadyExists->error() ) {
|
||||
Debug::info( 'Error querying database: ' . $alreadyExists->errorMessage() );
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( $alreadyExists->count() ) {
|
||||
Debug::info( 'email already subscribed.' );
|
||||
return false;
|
||||
}
|
||||
$fields = [
|
||||
'email' => $email,
|
||||
'confirmationCode' => Code::genConfirmation(),
|
||||
'confirmed' => 0,
|
||||
'subscribed' => time(),
|
||||
];
|
||||
self::$db->insert( $this->tableName, $fields );
|
||||
return self::$db->lastId();
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes an email from the subscribers database.
|
||||
*
|
||||
* @param string $email - The email you are trying to remove.
|
||||
* @param string $code - The confirmation code to unsubscribe.
|
||||
* @return boolean
|
||||
*/
|
||||
public function unsubscribe( $email, $code ) {
|
||||
if ( !Check::email( $email ) ) {
|
||||
return false;
|
||||
}
|
||||
$user = self::$db->get( $this->tableName, ['email', '=', $email, 'AND', 'confirmationCode', '=', $code] );
|
||||
if ( !$user->count() ) {
|
||||
Debug::info( __METHOD__ . ' - Cannot find subscriber with that email and code' );
|
||||
return false;
|
||||
}
|
||||
self::$db->delete( $this->tableName, ['ID', '=', $user->first()->ID] );
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a subscriber object for the provided email address.
|
||||
*
|
||||
* @param string $email - An email address to look for.
|
||||
* @return bool|object - Depending on success.
|
||||
*/
|
||||
public function get( $email ) {
|
||||
if ( !Check::email( $email ) ) {
|
||||
return false;
|
||||
}
|
||||
$data = self::$db->get( $this->tableName, ['email', '=', $email] );
|
||||
if ( !$data->count() ) {
|
||||
Debug::info( __METHOD__ . ' - Email not found' );
|
||||
return false;
|
||||
}
|
||||
return (object) $data->first();
|
||||
}
|
||||
}
|
34
app/plugins/subscribe/plugin.php
Normal file
34
app/plugins/subscribe/plugin.php
Normal file
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
/**
|
||||
* app/plugins/subscribe/plugin.php
|
||||
*
|
||||
* This houses all of the main plugin info and functionality.
|
||||
*
|
||||
* @package TP Subscribe
|
||||
* @version 3.0
|
||||
* @author Joey Kimsey <Joey@thetempusproject.com>
|
||||
* @link https://TheTempusProject.com
|
||||
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
|
||||
*/
|
||||
namespace TheTempusProject\Plugins;
|
||||
|
||||
use ReflectionClass;
|
||||
use TheTempusProject\Classes\Installer;
|
||||
use TheTempusProject\Houdini\Classes\Navigation;
|
||||
use TheTempusProject\Classes\Plugin;
|
||||
use TheTempusProject\TheTempusProject as App;
|
||||
|
||||
class Subscribe extends Plugin {
|
||||
public $pluginName = 'TP Subscribe';
|
||||
public $pluginAuthor = 'JoeyK';
|
||||
public $pluginWebsite = 'https://TheTempusProject.com';
|
||||
public $modelVersion = '1.0';
|
||||
public $pluginVersion = '3.0';
|
||||
public $pluginDescription = 'A simple plugin to add a method for users to share their email.';
|
||||
public $admin_links = [
|
||||
[
|
||||
'text' => '<i class="fa fa-fw fa-address-book"></i> Subscriptions',
|
||||
'url' => '{ROOT_URL}admin/subscriptions',
|
||||
],
|
||||
];
|
||||
}
|
5
app/plugins/subscribe/views/admin/add.html
Normal file
5
app/plugins/subscribe/views/admin/add.html
Normal file
@ -0,0 +1,5 @@
|
||||
<form action="" method="post" class="form-horizontal">
|
||||
<input type="email" placeholder="Email" id="email" name="email">
|
||||
<input type="hidden" name="token" value="{TOKEN}">
|
||||
<button name="submit" value="submit" type="submit" class="btn btn-lg btn-primary center-block">Submit</button><br>
|
||||
</form>
|
37
app/plugins/subscribe/views/admin/list.html
Normal file
37
app/plugins/subscribe/views/admin/list.html
Normal file
@ -0,0 +1,37 @@
|
||||
<legend>Subscribers</legend>
|
||||
{PAGINATION}
|
||||
<form action="{ROOT_URL}admin/subscriptions/delete" method="post">
|
||||
<table class="table table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<th style="width: 5%">ID</th>
|
||||
<th style="width: 85%">email</th>
|
||||
<th style="width: 5%"></th>
|
||||
<th style="width: 5%">
|
||||
<INPUT type="checkbox" onchange="checkAll(this)" name="check.s" value="S_[]"/>
|
||||
</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{LOOP}
|
||||
<tr>
|
||||
<td align="center">{ID}</td>
|
||||
<td>{EMAIL}</td>
|
||||
<td><a href="{ROOT_URL}admin/subscriptions/delete/{ID}" class="btn btn-sm btn-danger" role="button"><i class="glyphicon glyphicon-trash"></i></a></td>
|
||||
<td>
|
||||
<input type="checkbox" value="{ID}" name="S_[]">
|
||||
</td>
|
||||
</tr>
|
||||
{/LOOP}
|
||||
{ALT}
|
||||
<tr>
|
||||
<td align="center" colspan="6">
|
||||
No results to show.
|
||||
</td>
|
||||
</tr>
|
||||
{/ALT}
|
||||
</tbody>
|
||||
</table>
|
||||
<a href="{ROOT_URL}admin/subscriptions/add" class="btn btn-sm btn-primary" role="button">Add</a>
|
||||
<button name="submit" value="submit" type="submit" class="btn btn-sm btn-danger">Delete</button>
|
||||
</form>
|
5
app/plugins/subscribe/views/subscribe.html
Normal file
5
app/plugins/subscribe/views/subscribe.html
Normal file
@ -0,0 +1,5 @@
|
||||
<form action="{ROOT_URL}subscribe/home" method="post" class="form-horizontal">
|
||||
<input type="email" placeholder="Email" id="email" name="email" autocomplete="email">
|
||||
<input type="hidden" name="token" value="{TOKEN}">
|
||||
<button name="submit" value="submit" type="submit" class="btn btn-lg btn-primary center-block">Submit</button><br>
|
||||
</form>
|
5
app/plugins/subscribe/views/unsubscribe.html
Normal file
5
app/plugins/subscribe/views/unsubscribe.html
Normal file
@ -0,0 +1,5 @@
|
||||
<form action="{ROOT_URL}home/unsubscribe" method="post" class="form-horizontal">
|
||||
<input type="email" placeholder="Email" id="email" name="email">
|
||||
<input type="hidden" name="token" value="{TOKEN}">
|
||||
<button name="submit" value="submit" type="submit" class="btn btn-lg btn-primary center-block">Submit</button><br>
|
||||
</form>
|
Reference in New Issue
Block a user