Skip to content

Instantly share code, notes, and snippets.

@ubiratanlima
Created September 28, 2017 14:23
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 ubiratanlima/54894aaccab476703cb1c7097cd4e0e3 to your computer and use it in GitHub Desktop.
Save ubiratanlima/54894aaccab476703cb1c7097cd4e0e3 to your computer and use it in GitHub Desktop.
<?php
namespace GES\Models;
use Bootstrapper\Interfaces\TableInterface;
use GES\Notifications\UserCreated;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable implements TableInterface
{
use Notifiable;
const ROLE_ADMIN = 1;
const ROLE_STUDENT = 2;
const ROLE_TEACHER = 3;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password', 'enrolment'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function userable(){
return $this->morphTo();
}
public static function createFully($data){
$password = str_random(6);
$data['password'] = $password;
$user = parent::create($data+['enrolment' => str_random(6)]);
self::assignEnrolment($user, self::ROLE_ADMIN);
$user->save();
if(isset($data['send_mail'])){
$user->notify(new UserCreated());
}
return compact('user','password');
}
public static function assignEnrolment(User $user, $type){
$types = [
self::ROLE_ADMIN => 100000,
self::ROLE_TEACHER => 400000,
self::ROLE_STUDENT => 700000,
];
$user->enrolment = $types[$type] + $user->id;
return $user->enrolment;
}
public function getTableHeaders()
{
return [
'ID',
'Nome',
'E-mail'
];
}
public function getValueForHeader($header)
{
switch ($header){
case 'ID':
return $this->id;
case 'Nome':
return $this->name;
case 'E-mail':
return $this->email;
}
}
}
<?php
namespace GES\Forms;
use Kris\LaravelFormBuilder\Form;
use GES\Models\User;
class UserForm extends Form
{
public function buildForm()
{
$id = $this->getData('id');
$this
->add('name', 'text', [
'label' => 'Nome',
'rules' => 'required|max:100'
])
->add('email', 'email',[
'email' => 'E-mail',
'rules' => "required|max:255|unique:users,email,{$id}"
])
->add('type', 'select',[
'label' => 'Tipo do Usuário',
'choices' => [
User::ROLE_ADMIIN => 'Administrador',
User::ROLE_TEACHER => 'Professor',
User::ROLE_STUDENT => 'Aluno',
]
])
->add('send_mail', 'checkbox', [
'label' => 'Enviar e-mail de boas-vindas',
'value' => true,
'checked' => false
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment