Skip to content

Instantly share code, notes, and snippets.

@anhtuank7c
Last active February 12, 2016 01:12
Show Gist options
  • Save anhtuank7c/b276b35c76da12d2c17a to your computer and use it in GitHub Desktop.
Save anhtuank7c/b276b35c76da12d2c17a to your computer and use it in GitHub Desktop.
<?php
namespace App\Event;
use Cake\Event\Event;
use Cake\Event\EventListenerInterface;
class PostActiveListener implements EventListenerInterface
{
public function implementedEvents()
{
return [
'Controller.initialize' => 'checkProfileFields',
];
}
public function checkProfileFields(Event $event)
{
if (null != $event->subject()->request->session()->read('Auth.User') && !in_array($event->subject()->request->action, ['profile', 'signOut'])) {
$user = $event->subject()->request->session()->read('Auth.User');
$mandatoryFields = ['full_name', 'zip_code', 'furigana', 'prefecture', 'city', 'street', 'building', 'phone'];
foreach ($mandatoryFields as $k => $v) {
if (null == $user[$mandatoryFields[$k]] || strlen($user[$mandatoryFields[$k]]) === 0 || empty($user[$mandatoryFields[$k]])) {
if ($event->subject()->Flash) {
$event->subject()->Flash->error(__('Please update your profile before using service'));
}
return $event->subject()->redirect(['controller' => 'Users', 'action' => 'index']);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment