Reworking events for L5

This commit is contained in:
Hillel Coren 2015-03-31 20:42:37 +03:00
parent bb66ed7a96
commit 4fc7e3f5d1
26 changed files with 383 additions and 79 deletions

View File

@ -0,0 +1,21 @@
<?php namespace App\Events;
use App\Events\Event;
use Illuminate\Queue\SerializesModels;
class InvoicePaid extends Event {
use SerializesModels;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct()
{
//
}
}

View File

@ -0,0 +1,21 @@
<?php namespace App\Events;
use App\Events\Event;
use Illuminate\Queue\SerializesModels;
class InvoiceSent extends Event {
use SerializesModels;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct()
{
//
}
}

View File

@ -0,0 +1,23 @@
<?php namespace App\Events;
use App\Events\Event;
use Illuminate\Queue\SerializesModels;
class InvoiceViewed extends Event {
use SerializesModels;
public $invoice;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($invoice)
{
$this->invoice = $invoice;
}
}

View File

@ -0,0 +1,21 @@
<?php namespace App\Events;
use App\Events\Event;
use Illuminate\Queue\SerializesModels;
class UserLoggedIn extends Event {
use SerializesModels;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct()
{
//
}
}

View File

@ -0,0 +1,21 @@
<?php namespace App\Events;
use App\Events\Event;
use Illuminate\Queue\SerializesModels;
class UserSettingsChanged extends Event {
use SerializesModels;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct()
{
//
}
}

View File

@ -1,30 +0,0 @@
<?php namespace App\Handlers;
class UserEventHandler
{
public function subscribe($events)
{
$events->listen('user.signup', 'UserEventHandler@onSignup');
$events->listen('user.login', 'UserEventHandler@onLogin');
$events->listen('user.refresh', 'UserEventHandler@onRefresh');
}
public function onSignup()
{
}
public function onLogin()
{
$account = Auth::user()->account;
$account->last_login = Carbon::now()->toDateTimeString();
$account->save();
Event::fire('user.refresh');
}
public function onRefresh()
{
Auth::user()->account->loadLocalizationSettings();
}
}

View File

@ -20,6 +20,8 @@ use App\Models\Industry;
use App\Ninja\Repositories\AccountRepository;
use App\Ninja\Mailers\UserMailer;
use App\Ninja\Mailers\ContactMailer;
use App\Events\UserLoggedIn;
use App\Events\UserSettingsChanged;
class AccountController extends BaseController
{
@ -79,7 +81,7 @@ class AccountController extends BaseController
}
Auth::login($user, true);
Event::fire('user.login');
Event::fire(new UserLoggedIn());
return Redirect::to('invoices/create')->with('sign_up', Input::get('sign_up'));
}
@ -634,7 +636,7 @@ class AccountController extends BaseController
//Image::canvas($image->width, $image->height, '#FFFFFF')->insert($image)->save($account->getLogoPath());
}
Event::fire('user.refresh');
Event::fire(new UserSettingsChanged());
Session::flash('message', trans('texts.updated_settings'));
return Redirect::to('company/details');

View File

@ -1,5 +1,10 @@
<?php namespace App\Http\Controllers\Auth;
use Auth;
use Event;
use Illuminate\Http\Request;
use App\Events\UserLoggedIn;
use App\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\Registrar;
@ -38,4 +43,15 @@ class AuthController extends Controller {
$this->middleware('guest', ['except' => 'getLogout']);
}
public function postLoginWrapper(Request $request)
{
$response = self::postLogin($request);
if (Auth::check()) {
Event::fire(new UserLoggedIn());
}
return $response;
}
}

View File

@ -8,6 +8,7 @@ use Input;
use Cache;
use Redirect;
use DB;
use Event;
use App\Models\Invoice;
use App\Models\Invitation;
@ -21,11 +22,14 @@ use App\Models\Size;
use App\Models\Industry;
use App\Models\PaymentTerm;
use App\Models\InvoiceDesign;
use App\Models\AccountGateway;
use App\Models\Activity;
use App\Ninja\Mailers\ContactMailer as Mailer;
use App\Ninja\Repositories\InvoiceRepository;
use App\Ninja\Repositories\ClientRepository;
use App\Ninja\Repositories\TaxRateRepository;
use App\Events\InvoiceViewed;
class InvoiceController extends BaseController
{
@ -178,7 +182,8 @@ class InvoiceController extends BaseController
if (!Session::has($invitationKey) && (!Auth::check() || Auth::user()->account_id != $invoice->account_id)) {
Activity::viewInvoice($invitation);
Event::fire('invoice.viewed', $invoice);
//Event::fire('invoice.viewed', $invoice);
Event::fire(new InvoiceViewed($invoice));
}
Session::set($invitationKey, true);

View File

@ -7,6 +7,10 @@ use Session;
use Utils;
use View;
use App\Models\Invoice;
use App\Models\Client;
use App\Models\PaymentType;
use App\Ninja\Repositories\PaymentRepository;
use App\Ninja\Repositories\InvoiceRepository;
use App\Ninja\Repositories\AccountRepository;
@ -140,8 +144,8 @@ class PaymentController extends BaseController
'method' => 'POST',
'url' => "payments",
'title' => trans('texts.new_payment'),
//'currencies' => Currency::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
'paymentTypes' => PaymentType::remember(DEFAULT_QUERY_CACHE)->orderBy('id')->get(),
//'paymentTypes' => PaymentType::remember(DEFAULT_QUERY_CACHE)->orderBy('id')->get(),
'paymentTypes' => PaymentType::orderBy('id')->get(),
'clients' => Client::scope()->with('contacts')->orderBy('name')->get(), );
return View::make('payments.edit', $data);
@ -161,8 +165,8 @@ class PaymentController extends BaseController
'method' => 'PUT',
'url' => 'payments/'.$publicId,
'title' => trans('texts.edit_payment'),
//'currencies' => Currency::remember(DEFAULT_QUERY_CACHE)->orderBy('name')->get(),
'paymentTypes' => PaymentType::remember(DEFAULT_QUERY_CACHE)->orderBy('id')->get(),
//'paymentTypes' => PaymentType::remember(DEFAULT_QUERY_CACHE)->orderBy('id')->get(),
'paymentTypes' => PaymentType::orderBy('id')->get(),
'clients' => Client::scope()->with('contacts')->orderBy('name')->get(), );
return View::make('payments.edit', $data);

View File

@ -16,7 +16,6 @@
//dd(DB::getQueryLog());
//dd(Client::getPrivateId(1));
//dd(new DateTime());
//Event::fire('user.signup');
//dd(App::environment());
//dd(gethostname());
//Log::error('test');
@ -64,7 +63,7 @@ Route::controllers([
get('/signup', array('as' => 'signup', 'uses' => 'Auth\AuthController@getRegister'));
post('/signup', array('as' => 'signup', 'uses' => 'Auth\AuthController@postRegister'));
get('/login', array('as' => 'login', 'uses' => 'Auth\AuthController@getLogin'));
post('/login', array('as' => 'login', 'uses' => 'Auth\AuthController@postLogin'));
post('/login', array('as' => 'login', 'uses' => 'Auth\AuthController@postLoginWrapper'));
get('/logout', array('as' => 'logout', 'uses' => 'Auth\AuthController@getLogout'));
get('/forgot', array('as' => 'forgot', 'uses' => 'Auth\PasswordController@getEmail'));
post('/forgot', array('as' => 'forgot', 'uses' => 'Auth\PasswordController@postEmail'));

View File

@ -12,6 +12,7 @@ use Input;
use Log;
use DateTime;
use stdClass;
use Carbon;
use App\Models\Currency;

View File

@ -0,0 +1,45 @@
<?php namespace App\Listeners;
use App\Events\InvoicePaid;
use App\Ninja\Mailers\UserMailer;
use App\Ninja\Mailers\ContactMailer;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldBeQueued;
class HandleInvoicePaid {
protected $userMailer;
protected $contactMailer;
/**
* Create the event handler.
*
* @return void
*/
public function __construct(UserMailer $userMailer, ContactMailer $contactMailer)
{
$this->userMailer = $userMailer;
$this->contactMailer = $contactMailer;
}
/**
* Handle the event.
*
* @param InvoicePaid $event
* @return void
*/
public function handle(InvoicePaid $event)
{
$this->contactMailer->sendPaymentConfirmation($payment);
foreach ($invoice->account->users as $user)
{
if ($user->{'notify_paid'})
{
$this->userMailer->sendNotification($user, $invoice, 'paid', $payment);
}
}
}
}

View File

@ -0,0 +1,40 @@
<?php namespace App\Listeners;
use App\Events\InvoiceSent;
use App\Ninja\Mailers\UserMailer;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldBeQueued;
class HandleInvoiceSent {
protected $userMailer;
/**
* Create the event handler.
*
* @return void
*/
public function __construct(UserMailer $userMailer)
{
$this->userMailer = $userMailer;
}
/**
* Handle the event.
*
* @param InvoiceSent $event
* @return void
*/
public function handle(InvoiceSent $event)
{
foreach ($invoice->account->users as $user)
{
if ($user->{'notify_sent'})
{
$this->userMailer->sendNotification($user, $invoice, 'sent', $payment);
}
}
}
}

View File

@ -0,0 +1,42 @@
<?php namespace App\Listeners;
use App\Events\InvoiceViewed;
use App\Ninja\Mailers\UserMailer;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldBeQueued;
class HandleInvoiceViewed {
protected $userMailer;
/**
* Create the event handler.
*
* @return void
*/
public function __construct(UserMailer $userMailer)
{
$this->userMailer = $userMailer;
}
/**
* Handle the event.
*
* @param InvoiceViewed $event
* @return void
*/
public function handle(InvoiceViewed $event)
{
$invoice = $event->invoice;
foreach ($invoice->account->users as $user)
{
if ($user->{'notify_viewed'})
{
$this->userMailer->sendNotification($user, $invoice, 'viewed', $payment);
}
}
}
}

View File

@ -0,0 +1,37 @@
<?php namespace App\Listeners;
use Auth;
use Carbon;
use App\Events\UserLoggedIn;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldBeQueued;
class HandleUserLoggedIn {
/**
* Create the event handler.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param UserLoggedIn $event
* @return void
*/
public function handle(UserLoggedIn $event)
{
$account = Auth::user()->account;
$account->last_login = Carbon::now()->toDateTimeString();
$account->save();
$account->loadLocalizationSettings();
}
}

View File

@ -0,0 +1,32 @@
<?php namespace App\Listeners;
use App\Events\UserSettingsChanged;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldBeQueued;
class HandleUserSettingsChanged {
/**
* Create the event handler.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param UserSettingsChanged $event
* @return void
*/
public function handle(UserSettingsChanged $event)
{
$account = Auth::user()->account;
$account->loadLocalizationSettings();
}
}

View File

@ -2,6 +2,7 @@
use Eloquent;
use Utils;
use Session;
use Illuminate\Database\Eloquent\SoftDeletes;

View File

@ -5,6 +5,7 @@ use Eloquent;
use Utils;
use Session;
use Request;
use Carbon;
class Activity extends Eloquent
{

View File

@ -172,7 +172,7 @@ class ClientRepository
$client->save();
if (!$publicId || $publicId == "-1") {
\Activity::createClient($client, $notify);
Activity::createClient($client, $notify);
}
return $client;

View File

@ -15,15 +15,7 @@ class ConfigServiceProvider extends ServiceProvider {
*/
public function register()
{
// Surely there has to be a better way than this?
app('config')->set('confide', require $this->app->basePath() . '/config/packages/zizaco/confide/config.php');
/*
This doesn't work:
config([
'config/packages/zizaco/confide/config.php'
]);*/
}
}

View File

@ -11,8 +11,20 @@ class EventServiceProvider extends ServiceProvider {
* @var array
*/
protected $listen = [
'event.name' => [
'EventListener',
'App\Events\UserLoggedIn' => [
'App\Listeners\HandleUserLoggedIn',
],
'App\Events\UserSettingsChanged' => [
'App\Listeners\HandleUserSettingsChanged',
],
'App\Events\InvoiceSent' => [
'App\Listeners\HandleInvoiceSent',
],
'App\Events\InvoiceViewed' => [
'App\Listeners\HandleInvoiceViewed',
],
'App\Events\InvoicePaid' => [
'App\Listeners\HandleInvoicePaid',
],
];

View File

@ -65,7 +65,7 @@
@foreach ($activities as $activity)
<li class="list-group-item">
<span style="color:#888;font-style:italic">{{ Utils::timestampToDateString(strtotime($activity->created_at)) }}:</span>
{{ Utils::decodeActivity($activity->message) }}
{!! Utils::decodeActivity($activity->message) !!}
</li>
@endforeach
</ul>

View File

@ -22,27 +22,27 @@
<p>&nbsp;</p>
<div class="pull-right" style="text-align:right">
@if ($invoice->is_quote)
{!! Button::normal(trans('texts.download_pdf')->withAttributes(array('onclick' => 'onDownloadClick()', 'class' => 'btn-lg')) !!}&nbsp;&nbsp;
{!! Button::normal(trans('texts.download_pdf', array('onclick' => 'onDownloadClick()')))->large() !!}&nbsp;&nbsp;
@if (!$isConverted)
{!! Button::success(trans('texts.approve'))->asLinkTo(URL::to('approve/' . $invitation->invitation_key))->withAttributes(array('class' => 'btn-lg')) !!}
@endif
@elseif ($invoice->client->account->isGatewayConfigured() && !$invoice->isPaid() && !$invoice->is_recurring)
{!! Button::normal(trans('texts.download_pdf'), array('onclick' => 'onDownloadClick()', 'class' => 'btn-lg')) !!}&nbsp;&nbsp;
@if ($hasToken)
{{ DropdownButton::success_lg(trans('texts.pay_now'), [
{!! DropdownButton::success_lg(trans('texts.pay_now'), [
['url' => URL::to("payment/{$invitation->invitation_key}?use_token=true&use_paypal=false"), 'label' => trans('texts.use_card_on_file')],
['url' => URL::to("payment/{$invitation->invitation_key}?use_paypal=false"), 'label' => trans('texts.edit_payment_details')]
])->addClass('btn-lg') }}
])->addClass('btn-lg') !!}
@elseif ($countGateways == 2)
{{ DropdownButton::success_lg(trans('texts.pay_now'), [
{!! DropdownButton::success_lg(trans('texts.pay_now'), [
['url' => URL::to("payment/{$invitation->invitation_key}?use_paypal=true"), 'label' => trans('texts.pay_with_paypal')],
['url' => URL::to("payment/{$invitation->invitation_key}?use_paypal=false"), 'label' => trans('texts.pay_with_card')]
])->addClass('btn-lg') }}
])->addClass('btn-lg') !!}
@else
{{ Button::success_link(URL::to('payment/' . $invitation->invitation_key), trans('texts.pay_now'), array('class' => 'btn-lg')) }}
{!! Button::success_link(URL::to('payment/' . $invitation->invitation_key), trans('texts.pay_now'), array('class' => 'btn-lg')) !!}
@endif
@else
{{ Button::success('Download PDF', array('onclick' => 'onDownloadClick()', 'class' => 'btn-lg')) }}
{!! Button::success('Download PDF', array('onclick' => 'onDownloadClick()'))->large() !!}
@endif
</div>
@ -50,10 +50,10 @@
<script type="text/javascript">
window.invoice = {{ $invoice->toJson() }};
window.invoice = {!! $invoice->toJson() !!};
invoice.is_pro = {{ $invoice->client->account->isPro() ? 'true' : 'false' }};
invoice.is_quote = {{ $invoice->is_quote ? 'true' : 'false' }};
invoice.contact = {{ $contact->toJson() }};
invoice.contact = {!! $contact->toJson() !!};
function getPDFString() {
var doc = generatePDF(invoice, invoice.invoice_design.javascript);

View File

@ -8,11 +8,11 @@
@section('content')
{{ Former::open($url)->addClass('col-md-10 col-md-offset-1 warn-on-exit')->method($method)->rules(array(
{!! Former::open($url)->addClass('col-md-10 col-md-offset-1 warn-on-exit')->method($method)->rules(array(
'client' => 'required',
'invoice' => 'required',
'amount' => 'required',
)); }}
)) !!}
@if ($payment)
{{ Former::populate($payment) }}
@ -22,17 +22,15 @@
<div class="col-md-8">
@if (!$payment)
{{ Former::select('client')->addOption('', '')->addGroupClass('client-select') }}
{{ Former::select('invoice')->addOption('', '')->addGroupClass('invoice-select') }}
{{ Former::text('amount') }}
{!! Former::select('client')->addOption('', '')->addGroupClass('client-select') !!}
{!! Former::select('invoice')->addOption('', '')->addGroupClass('invoice-select') !!}
{!! Former::text('amount') !!}
@endif
{{ Former::select('payment_type_id')->addOption('','')
->fromQuery($paymentTypes, 'name', 'id') }}
{{ Former::text('payment_date')->data_date_format(Session::get(SESSION_DATE_PICKER_FORMAT))->append('<i class="glyphicon glyphicon-calendar"></i>') }}
{{ Former::text('transaction_reference') }}
{{-- Former::select('currency_id')->addOption('','')
->fromQuery($currencies, 'name', 'id')->select(Session::get(SESSION_CURRENCY, DEFAULT_CURRENCY)) --}}
{!! Former::select('payment_type_id')->addOption('','')
->fromQuery($paymentTypes, 'name', 'id') !!}
{!! Former::text('payment_date')->data_date_format(Session::get(SESSION_DATE_PICKER_FORMAT))->append('<i class="glyphicon glyphicon-calendar"></i>') !!}
{!! Former::text('transaction_reference') !!}
</div>
<div class="col-md-6">
@ -41,16 +39,16 @@
</div>
<center class="buttons">
{{ Button::lg_primary_submit_success(trans('texts.save'))->append_with_icon('floppy-disk') }}
{{ Button::lg_default_link('payments/', trans('texts.cancel'))->append_with_icon('remove-circle'); }}
{!! Button::success(trans('texts.save'))->appendIcon(Icon::create('floppy-disk'))->submit()->large() !!}
{!! Button::withValue(trans('texts.cancel'))->appendIcon(Icon::create('remove-circle'))->asLinkTo('/payments')->large() !!}
</center>
{{ Former::close() }}
{!! Former::close() !!}
<script type="text/javascript">
var invoices = {{ $invoices }};
var clients = {{ $clients }};
var invoices = {!! $invoices !!};
var clients = {!! $clients !!};
$(function() {