Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mockiemockiz
Created September 16, 2015 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mockiemockiz/447c6aa7a0e21cb4a486 to your computer and use it in GitHub Desktop.
Save mockiemockiz/447c6aa7a0e21cb4a486 to your computer and use it in GitHub Desktop.
<?php
/**
* Created by PhpStorm.
* User: mockie
* Date: 16/04/15
* Time: 7:01
*/
namespace AAA\Forum\Controllers;
use AAA\Forum\Filters\CategorySubscriberFilter;
use AAA\Forum\Models\Repositories\CategoryRepository;
use AAA\Forum\Models\Repositories\ThreadRepository;
use AAA\Forum\Models\Repositories\UserRepository;
use AAA\Forum\Validators\CategoryValidator;
use View, Input, Redirect;
class CategoriesController extends BaseController {
private $categoryRepository;
private $categoryValidator;
private $userRepository;
private $categorySubscriberFilter;
private $threadRepository;
public function __construct(
CategoryRepository $categoryRepository,
CategoryValidator $categoryValidator,
UserRepository $userRepository,
CategorySubscriberFilter $categorySubscriberFilter,
ThreadRepository $threadRepository
)
{
$this->categoryRepository = $categoryRepository;
$this->categoryValidator = $categoryValidator;
$this->userRepository = $userRepository;
$this->categorySubscriberFilter = $categorySubscriberFilter;
$this->threadRepository = $threadRepository;
}
/**
* @URL forum/categories
* @METHOD GET
* @ROUTE forum.categories.index
*
* @return Response
*/
public function index()
{
$data = [
'categories' =>$this->categoryRepository->getPaginatedCategoryList(),
];
return View::make('forum::categories.index', $data);
}
/**
* @URL forum/categories/create
* @METHOD GET
* @ROUTE forum.admin.categories.create
* Show the form for creating a new category
*
* @return Response
*/
public function create()
{
$data = [
'category' => $this->categoryRepository,
'categories' =>$this->categoryRepository->dropDown()
];
return View::make('forum::categories.create', $data);
}
/**
* @URL forum/categories
* @METHOD POST
* @ROUTE forum.admin.categories.store
* Store a newly created category in storage.
*
* @return Response
*/
public function store()
{
$data = Input::all();
$validator = $this->categoryValidator;
if (!$validator->validateInput($data)) {
return Redirect::back()->withErrors($validator->getMessages())->withInput();
}
$this->categoryRepository->create($data);
return Redirect::route('forum.categories.index');
}
/**
* @URL forum/categories/show
* @METHOD GET
* @ROUTE forum.categories.show
* Display the specified category.
*
* @param $slug
* @return Response
*/
public function show($slug)
{
$category = $this->categoryRepository->findBySlugOrFail($slug);
$userId = $this->userRepository->getLoggedInUser()->id;
$data = [
'category' => $category,
'isAlreadySubscribed' => $this->categorySubscriberFilter->isAlreadySubscribed($userId, $category->id),
'threads' => $this->threadRepository->getPaginatedThreadList(['where' => ['category_id' => [$category->id]]])
];
return View::make('forum::categories.show', $data);
}
/**
* @URL forum/categories/{category_id}/edit
* @METHOD GET
* @ROUTE forum.admin.categories.edit
* Show the form for editing the specified category.
*
* @param int $id
* @return Response
*/
public function edit($id)
{
$category =$this->categoryRepository->findOrFail($id, []);
$data = [
'category' => $category,
'categories' =>$this->categoryRepository->dropDown()
];
return View::make('forum::categories.edit', $data);
}
/**
* Update the specified category in storage.
*
* @URL forum/categories/update
* @METHOD PUT
* @ROUTE forum.admin.categories.update
* @return Response
* @internal param int $id
*/
public function update()
{
$data = Input::all();
$category = $this->categoryRepository->findOrFail($data['id']);
if (!$this->categoryValidator->validateInput($data)) {
return Redirect::back()->withErrors($this->categoryValidator->getMessages())->withInput();
}
$category->update($data);
return Redirect::route('forum.categories.show', ['slug' => $category->slug]);
}
/**
* Remove the specified category from storage.
*
* @URL forum/categories/{category_id}/edit
* @METHOD DELETE
* @ROUTE forum.admin.categories.destroy
* @param int $id
* @return Response
*/
public function destroy($id)
{
$this->categoryRepository->destroy($id);
return Redirect::route('forum.categories.index');
}
}
<?php
/**
* Created by PhpStorm.
* User: mockie
* Date: 21/04/15
* Time: 15:06
*/
namespace AAA\Forum\Listeners;
use AAA\Forum\Listeners\Interfaces\AbstractNotification;
use AAA\Forum\Models\Repositories\NotificationMessageRepository;
use AAA\Forum\Models\Repositories\ThreadRepository;
use AAA\Forum\Models\Repositories\ThreadSubscriberRepository;
use AAA\Forum\Models\Repositories\UserRepository;
class WebsiteNotification extends AbstractNotification {
private $notificationMessageRepository;
private $threadRepository;
private $threadSubscriberRepository;
private $userRepository;
private $bodyParams;
public $webNotificationTemplates = [
'thread_subscribe' => '{username} subscribed your thread {thread_title}',
'thread_reply' => '{username} replied in your thread you created or thread you subscribed {thread_title}'
];
public function __construct(
NotificationMessageRepository $notificationMessageRepository,
ThreadSubscriberRepository $threadSubscriberRepository,
UserRepository $userRepository,
ThreadRepository $threadRepository
)
{
$this->notificationMessageRepository = $notificationMessageRepository;
$this->threadRepository = $threadRepository;
$this->threadSubscriberRepository = $threadSubscriberRepository;
$this->userRepository = $userRepository;
}
protected function preparedType()
{
switch ($this->data['type']) {
case "thread_subscribe":
$this->prepareThreadType($this->data['thread_id']);
$this->setTemplate($this->webNotificationTemplates["thread_subscribe"]);
break;
case "thread_reply":
$this->prepareThreadSubscribeType($this->data['thread_id']);
$this->setTemplate($this->webNotificationTemplates["thread_reply"]);
break;
}
}
protected function prepareThreadType($threadId)
{
$thread = $this->threadRepository->find($threadId);
$this->setNotifiedTo([$thread->user_id]);
$this->setLink(route('threads.show', ['slug' => $thread->slug]));
}
protected function prepareThreadSubscribeType($threadId)
{
$thread = $this->threadRepository->find($threadId);
$this->setNotifiedTo([$thread->user_id]);
$this->setLink(route('threads.show', ['slug' => $thread->slug]));
}
protected function setParams()
{
$users = $this->userRepository->whereIn('id', $this->notifiedTo)->get(['id', 'username'])->toArray();
$thread = $this->threadRepository->find($this->data['thread_id']);
foreach ($users as $user) {
$params[$user['id']] = ['{username}' => $user['username'], '{thread_title}' => $thread->title];
$this->setBinding($params);
}
}
public function setData($data)
{
$default = [
'type' => 'thread_subscribe'
];
$data = array_merge($default, $data);
parent::setData($data);
}
public function send()
{
$this->setNotifiedBy($this->data['notified_by']);
$this->preparedType();
$this->setParams();
$this->setClearData();
$this->notificationMessageRepository->insertNotificationMessage($this->getClearData());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment