2015-05-24 16:33:03 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Cachet.
|
|
|
|
*
|
2015-07-06 17:37:01 +01:00
|
|
|
* (c) Alt Three Services Limited
|
2015-05-24 16:33:03 -05:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace CachetHQ\Cachet\Http\Controllers;
|
|
|
|
|
2015-08-03 22:32:36 +01:00
|
|
|
use AltThree\Validator\ValidationException;
|
2016-01-05 02:38:07 +00:00
|
|
|
use CachetHQ\Cachet\Bus\Commands\Subscriber\SubscribeSubscriberCommand;
|
|
|
|
use CachetHQ\Cachet\Bus\Commands\Subscriber\UnsubscribeSubscriberCommand;
|
2016-01-10 15:54:54 +00:00
|
|
|
use CachetHQ\Cachet\Bus\Commands\Subscriber\UnsubscribeSubscriptionCommand;
|
2016-04-30 02:25:05 -05:00
|
|
|
use CachetHQ\Cachet\Bus\Commands\Subscriber\UpdateSubscriberSubscriptionCommand;
|
2016-01-05 02:38:07 +00:00
|
|
|
use CachetHQ\Cachet\Bus\Commands\Subscriber\VerifySubscriberCommand;
|
2016-04-30 02:25:05 -05:00
|
|
|
use CachetHQ\Cachet\Models\Component;
|
2016-07-18 08:49:38 -04:00
|
|
|
use CachetHQ\Cachet\Models\ComponentGroup;
|
2015-05-24 16:33:03 -05:00
|
|
|
use CachetHQ\Cachet\Models\Subscriber;
|
2016-01-10 15:54:54 +00:00
|
|
|
use CachetHQ\Cachet\Models\Subscription;
|
2015-05-24 16:33:03 -05:00
|
|
|
use GrahamCampbell\Binput\Facades\Binput;
|
|
|
|
use GrahamCampbell\Markdown\Facades\Markdown;
|
2017-06-24 19:30:19 +01:00
|
|
|
use Illuminate\Contracts\Auth\Guard;
|
2016-07-20 19:52:23 +01:00
|
|
|
use Illuminate\Contracts\Config\Repository;
|
2015-08-05 17:18:51 -05:00
|
|
|
use Illuminate\Routing\Controller;
|
2016-01-29 22:49:06 +00:00
|
|
|
use Illuminate\Support\Facades\Config;
|
2015-05-24 16:33:03 -05:00
|
|
|
use Illuminate\Support\Facades\View;
|
2015-08-06 21:16:49 -05:00
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
2015-05-24 16:33:03 -05:00
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
|
|
2016-01-10 15:54:54 +00:00
|
|
|
/**
|
|
|
|
* This is the subscribe controller.
|
|
|
|
*
|
|
|
|
* @author James Brooks <james@alt-three.com>
|
|
|
|
*/
|
2015-08-05 17:18:51 -05:00
|
|
|
class SubscribeController extends Controller
|
2015-05-24 16:33:03 -05:00
|
|
|
{
|
2017-06-24 19:30:19 +01:00
|
|
|
/**
|
|
|
|
* The illuminate guard instance.
|
|
|
|
*
|
|
|
|
* @var \Illuminate\Contracts\Auth\Guard
|
|
|
|
*/
|
|
|
|
protected $auth;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new subscribe controller instance.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Contracts\Auth\Guard $auth
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct(Guard $auth)
|
|
|
|
{
|
|
|
|
$this->auth = $auth;
|
|
|
|
}
|
|
|
|
|
2015-05-24 16:33:03 -05:00
|
|
|
/**
|
|
|
|
* Show the subscribe by email page.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\View\View
|
|
|
|
*/
|
|
|
|
public function showSubscribe()
|
|
|
|
{
|
2016-02-09 19:41:22 +00:00
|
|
|
return View::make('subscribe.subscribe')
|
2016-01-29 22:49:06 +00:00
|
|
|
->withAboutApp(Markdown::convertToHtml(Config::get('setting.app_about')));
|
2015-05-24 16:33:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the subscribe user.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\View\View
|
|
|
|
*/
|
|
|
|
public function postSubscribe()
|
|
|
|
{
|
2015-12-19 23:08:28 +08:00
|
|
|
$email = Binput::get('email');
|
2016-01-10 15:54:54 +00:00
|
|
|
$subscriptions = Binput::get('subscriptions');
|
2016-07-20 19:54:48 +01:00
|
|
|
$verified = app(Repository::class)->get('setting.skip_subscriber_verification');
|
2015-12-19 23:08:28 +08:00
|
|
|
|
2015-08-03 22:32:36 +01:00
|
|
|
try {
|
2016-04-30 02:25:05 -05:00
|
|
|
$subscription = dispatch(new SubscribeSubscriberCommand($email, $verified));
|
2015-08-03 22:32:36 +01:00
|
|
|
} catch (ValidationException $e) {
|
2016-10-13 09:51:44 +02:00
|
|
|
return cachet_redirect('status-page')
|
2015-08-03 22:32:36 +01:00
|
|
|
->withInput(Binput::all())
|
2016-05-26 11:22:23 +01:00
|
|
|
->withTitle(sprintf('%s %s', trans('dashboard.notifications.whoops'), trans('cachet.subscriber.email.failure')))
|
2015-08-03 22:32:36 +01:00
|
|
|
->withErrors($e->getMessageBag());
|
2015-05-24 16:33:03 -05:00
|
|
|
}
|
|
|
|
|
2016-08-10 12:56:18 +01:00
|
|
|
if ($subscription->is_verified) {
|
2016-10-13 09:51:44 +02:00
|
|
|
return cachet_redirect('status-page')->withSuccess(trans('cachet.subscriber.email.already-subscribed', ['email' => $email]));
|
2016-08-10 12:56:18 +01:00
|
|
|
}
|
2016-04-30 02:25:05 -05:00
|
|
|
|
2016-10-13 09:51:44 +02:00
|
|
|
return cachet_redirect('subscribe.manage', $subscription->verify_code)
|
2016-08-10 12:56:18 +01:00
|
|
|
->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('cachet.subscriber.email.subscribed')));
|
2015-05-24 16:33:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the verify subscriber email.
|
|
|
|
*
|
2015-08-03 22:32:36 +01:00
|
|
|
* @param string|null $code
|
2015-05-24 16:33:03 -05:00
|
|
|
*
|
|
|
|
* @return \Illuminate\View\View
|
|
|
|
*/
|
|
|
|
public function getVerify($code = null)
|
|
|
|
{
|
2015-11-22 00:18:09 -06:00
|
|
|
if ($code === null) {
|
2015-05-24 16:33:03 -05:00
|
|
|
throw new NotFoundHttpException();
|
|
|
|
}
|
|
|
|
|
2016-10-19 12:29:47 +01:00
|
|
|
$subscriber = Subscriber::where('verify_code', '=', $code)->first();
|
2015-05-24 16:33:03 -05:00
|
|
|
|
2016-04-30 02:25:05 -05:00
|
|
|
if (!$subscriber) {
|
2015-08-06 21:16:49 -05:00
|
|
|
throw new BadRequestHttpException();
|
2015-05-24 16:33:03 -05:00
|
|
|
}
|
|
|
|
|
2016-04-30 02:25:05 -05:00
|
|
|
if (!$subscriber->is_verified) {
|
|
|
|
dispatch(new VerifySubscriberCommand($subscriber));
|
|
|
|
}
|
2015-05-24 16:33:03 -05:00
|
|
|
|
2016-10-13 09:51:44 +02:00
|
|
|
return cachet_redirect('status-page')
|
2016-05-26 11:22:23 +01:00
|
|
|
->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('cachet.subscriber.email.verified')));
|
2015-05-24 16:33:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the unsubscribe.
|
|
|
|
*
|
2015-08-03 22:32:36 +01:00
|
|
|
* @param string|null $code
|
2016-01-10 15:54:54 +00:00
|
|
|
* @param int|null $subscription
|
2015-05-24 16:33:03 -05:00
|
|
|
*
|
|
|
|
* @return \Illuminate\View\View
|
|
|
|
*/
|
2016-01-10 15:54:54 +00:00
|
|
|
public function getUnsubscribe($code = null, $subscription = null)
|
2015-05-24 16:33:03 -05:00
|
|
|
{
|
2015-11-22 00:18:09 -06:00
|
|
|
if ($code === null) {
|
2015-05-24 16:33:03 -05:00
|
|
|
throw new NotFoundHttpException();
|
|
|
|
}
|
|
|
|
|
|
|
|
$subscriber = Subscriber::where('verify_code', '=', $code)->first();
|
|
|
|
|
2015-12-09 17:32:18 -06:00
|
|
|
if (!$subscriber || !$subscriber->is_verified) {
|
2015-08-06 21:16:49 -05:00
|
|
|
throw new BadRequestHttpException();
|
2015-05-24 16:33:03 -05:00
|
|
|
}
|
|
|
|
|
2016-01-10 15:54:54 +00:00
|
|
|
if ($subscription) {
|
|
|
|
dispatch(new UnsubscribeSubscriptionCommand(Subscription::forSubscriber($subscriber->id)->firstOrFail()));
|
|
|
|
} else {
|
2017-10-11 21:19:07 +02:00
|
|
|
dispatch(new UnsubscribeSubscriberCommand($subscriber));
|
2016-01-10 15:54:54 +00:00
|
|
|
}
|
2015-05-24 16:33:03 -05:00
|
|
|
|
2016-10-13 09:51:44 +02:00
|
|
|
return cachet_redirect('status-page')
|
2016-05-26 11:22:23 +01:00
|
|
|
->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('cachet.subscriber.email.unsubscribed')));
|
2015-05-24 16:33:03 -05:00
|
|
|
}
|
2016-02-09 19:41:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows the subscription manager page.
|
|
|
|
*
|
2016-02-20 12:23:06 -06:00
|
|
|
* @param string|null $code
|
|
|
|
*
|
2016-02-09 19:41:22 +00:00
|
|
|
* @return \Illuminate\View\View
|
|
|
|
*/
|
2016-02-20 12:23:06 -06:00
|
|
|
public function showManage($code = null)
|
2016-02-09 19:41:22 +00:00
|
|
|
{
|
2016-02-20 12:23:06 -06:00
|
|
|
if ($code === null) {
|
2016-02-09 19:41:22 +00:00
|
|
|
throw new NotFoundHttpException();
|
|
|
|
}
|
|
|
|
|
2017-06-24 19:30:19 +01:00
|
|
|
$includePrivate = $this->auth->check();
|
|
|
|
|
2016-02-09 19:41:22 +00:00
|
|
|
$subscriber = Subscriber::where('verify_code', '=', $code)->first();
|
2017-06-24 19:30:19 +01:00
|
|
|
$usedComponentGroups = Component::enabled()->authenticated($includePrivate)->where('group_id', '>', 0)->groupBy('group_id')->pluck('group_id');
|
2016-07-18 08:49:38 -04:00
|
|
|
$componentGroups = ComponentGroup::whereIn('id', $usedComponentGroups)->orderBy('order')->get();
|
2017-06-24 19:30:19 +01:00
|
|
|
$ungroupedComponents = Component::enabled()->authenticated($includePrivate)->where('group_id', '=', 0)->orderBy('order')->orderBy('created_at')->get();
|
2016-02-09 19:41:22 +00:00
|
|
|
|
2016-04-30 02:25:05 -05:00
|
|
|
if (!$subscriber) {
|
2016-02-09 19:41:22 +00:00
|
|
|
throw new BadRequestHttpException();
|
|
|
|
}
|
|
|
|
|
2016-04-30 02:25:05 -05:00
|
|
|
return View::make('subscribe.manage')
|
2016-07-18 08:49:38 -04:00
|
|
|
->withUngroupedComponents($ungroupedComponents)
|
2016-04-30 02:25:05 -05:00
|
|
|
->withSubscriber($subscriber)
|
2016-07-18 08:49:38 -04:00
|
|
|
->withSubscriptions($subscriber->subscriptions->pluck('component_id')->all())
|
|
|
|
->withComponentGroups($componentGroups);
|
2016-04-30 02:25:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the subscription manager for a subscriber.
|
|
|
|
*
|
|
|
|
* @param string|null $code
|
|
|
|
*
|
|
|
|
* @return \Illuminate\View\View
|
|
|
|
*/
|
|
|
|
public function postManage($code = null)
|
|
|
|
{
|
|
|
|
if ($code === null) {
|
|
|
|
throw new NotFoundHttpException();
|
|
|
|
}
|
|
|
|
|
|
|
|
$subscriber = Subscriber::where('verify_code', '=', $code)->first();
|
|
|
|
|
|
|
|
if (!$subscriber) {
|
|
|
|
throw new BadRequestHttpException();
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
dispatch(new UpdateSubscriberSubscriptionCommand($subscriber, Binput::get('subscriptions')));
|
|
|
|
} catch (ValidationException $e) {
|
2016-10-13 09:51:44 +02:00
|
|
|
return cachet_redirect('subscribe.manage', $subscriber->verify_code)
|
2016-04-30 02:25:05 -05:00
|
|
|
->withInput(Binput::all())
|
2016-05-26 11:22:23 +01:00
|
|
|
->withTitle(sprintf('%s %s', trans('dashboard.notifications.whoops'), trans('cachet.subscriber.email.failure')))
|
2016-04-30 02:25:05 -05:00
|
|
|
->withErrors($e->getMessageBag());
|
|
|
|
}
|
|
|
|
|
2016-10-13 09:51:44 +02:00
|
|
|
return cachet_redirect('subscribe.manage', $subscriber->verify_code)
|
2016-05-26 11:22:23 +01:00
|
|
|
->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('cachet.subscriber.email.subscribed')));
|
2016-02-09 19:41:22 +00:00
|
|
|
}
|
2015-05-24 16:33:03 -05:00
|
|
|
}
|