Attendize/app/Models/MyBaseModel.php

157 lines
3.4 KiB
PHP
Raw Normal View History

2016-02-29 15:59:36 +00:00
<?php
namespace App\Models;
2016-03-05 00:18:10 +00:00
use Auth;
use Validator;
2016-02-29 15:59:36 +00:00
/*
* Adapted from: https://github.com/hillelcoren/invoice-ninja/blob/master/app/models/EntityModel.php
*/
2016-03-05 00:18:10 +00:00
class MyBaseModel extends \Illuminate\Database\Eloquent\Model
{
2016-03-14 16:37:38 +00:00
/**
* Indicates if the model should be timestamped.
*
* @var bool $timestamps
*/
2016-02-29 15:59:36 +00:00
public $timestamps = true;
2016-09-06 20:39:27 +00:00
/**
* Indicates whether the model uses soft deletes.
*
* @var bool $softDelete
*/
protected $softDelete = true;
2016-03-14 16:37:38 +00:00
/**
* The validation rules of the model.
*
* @var array $rules
*/
2016-03-05 00:18:10 +00:00
protected $rules = [];
2016-03-14 16:37:38 +00:00
/**
* The validation error messages of the model.
*
* @var array $messages
*/
2016-03-05 00:18:10 +00:00
protected $messages = [];
2016-03-14 16:37:38 +00:00
/**
* The validation errors of model.
*
* @var $errors
*/
2016-02-29 15:59:36 +00:00
protected $errors;
2016-09-06 20:39:27 +00:00
/**
* Create a new model.
*
* @param int $account_id
* @param int $user_id
* @param bool $ignore_user_id
*
* @return \className
*/
public static function createNew($account_id = false, $user_id = false, $ignore_user_id = false)
{
$className = get_called_class();
$entity = new $className();
if (Auth::check()) {
if (!$ignore_user_id) {
$entity->user_id = Auth::user()->id;
}
$entity->account_id = Auth::user()->account_id;
} elseif ($account_id || $user_id) {
if ($user_id && !$ignore_user_id) {
$entity->user_id = $user_id;
}
$entity->account_id = $account_id;
} else {
App::abort(500);
}
return $entity;
}
2016-03-14 16:37:38 +00:00
/**
* Validate the model instance.
*
* @param $data
*
* @return bool
*/
2016-03-05 00:18:10 +00:00
public function validate($data)
{
2016-02-29 15:59:36 +00:00
$v = Validator::make($data, $this->rules, $this->messages);
if ($v->fails()) {
$this->errors = $v->messages();
2016-03-05 00:18:10 +00:00
2016-02-29 15:59:36 +00:00
return false;
}
// validation pass
return true;
}
2016-03-14 16:37:38 +00:00
/**
* Gets the validation error messages.
*
* @param bool $returnArray
*
* @return mixed
*/
2016-03-05 00:18:10 +00:00
public function errors($returnArray = true)
{
2016-02-29 15:59:36 +00:00
return $returnArray ? $this->errors->toArray() : $this->errors;
}
2016-03-14 16:37:38 +00:00
/**
* Get a formatted date.
*
* @param $field
* @param string $format
*
* @return bool|null|string
*/
2016-06-16 01:36:09 +00:00
public function getFormattedDate($field, $format = 'd-m-Y H:i')
2016-03-05 00:18:10 +00:00
{
return $this->$field === null ? null : date($format, strtotime($this->$field));
2016-02-29 15:59:36 +00:00
}
/**
2016-04-14 14:17:30 +00:00
* Ensures each query looks for account_id
*
* @param $query
* @param bool $accountId
* @param Request $request
* @return mixed
2016-02-29 15:59:36 +00:00
*/
2016-03-05 00:18:10 +00:00
public function scopeScope($query, $accountId = false)
{
2016-02-29 15:59:36 +00:00
/*
* GOD MODE - DON'T UNCOMMENT!
* returning $query before adding the account_id condition will let you
* browse all events etc. in the system.
* //return $query;
*/
if (!$accountId) {
$accountId = Auth::user()->account_id;
}
$table = $this->getTable();
2016-03-05 00:18:10 +00:00
$query->where(function ($query) use ($accountId, $table) {
2016-09-06 20:39:27 +00:00
$query->whereRaw(\DB::raw('(' . $table . '.account_id = ' . $accountId . ')'));
2016-02-29 15:59:36 +00:00
});
return $query;
}
}