1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 00:41:34 +02:00
invoiceninja/app/Ninja/Repositories/BaseRepository.php

182 lines
3.9 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
namespace App\Ninja\Repositories;
2015-10-28 20:22:07 +01:00
2016-12-08 23:34:24 +01:00
use Auth;
2016-11-18 14:31:43 +01:00
use Utils;
/**
2017-01-30 20:40:43 +01:00
* Class BaseRepository.
*/
2015-10-28 20:22:07 +01:00
class BaseRepository
{
/**
* @return null
*/
public function getClassName()
2015-10-28 20:22:07 +01:00
{
return null;
}
/**
* @return mixed
*/
2015-10-28 20:22:07 +01:00
private function getInstance()
{
$className = $this->getClassName();
2017-01-30 20:40:43 +01:00
2015-10-28 20:22:07 +01:00
return new $className();
}
/**
* @param $entity
* @param $type
2017-01-30 20:40:43 +01:00
*
* @return string
*/
2015-10-29 23:27:26 +01:00
private function getEventClass($entity, $type)
2015-10-28 20:22:07 +01:00
{
2015-10-29 23:27:26 +01:00
return 'App\Events\\' . ucfirst($entity->getEntityType()) . 'Was' . $type;
2015-10-28 20:22:07 +01:00
}
/**
* @param $entity
*/
2015-10-28 20:22:07 +01:00
public function archive($entity)
{
2016-03-23 14:02:05 +01:00
if ($entity->trashed()) {
return;
}
2015-10-28 20:22:07 +01:00
$entity->delete();
2015-10-29 23:27:26 +01:00
$className = $this->getEventClass($entity, 'Archived');
2015-11-05 23:37:04 +01:00
if (class_exists($className)) {
event(new $className($entity));
}
2015-10-28 20:22:07 +01:00
}
/**
* @param $entity
*/
2015-10-28 20:22:07 +01:00
public function restore($entity)
{
2017-01-30 17:05:31 +01:00
if (! $entity->trashed()) {
2016-03-23 14:02:05 +01:00
return;
}
2015-10-29 23:27:26 +01:00
$fromDeleted = false;
2015-10-28 20:22:07 +01:00
$entity->restore();
2015-10-29 23:27:26 +01:00
if ($entity->is_deleted) {
$fromDeleted = true;
$entity->is_deleted = false;
$entity->save();
}
2015-10-28 20:22:07 +01:00
2015-10-29 23:27:26 +01:00
$className = $this->getEventClass($entity, 'Restored');
2015-11-05 23:37:04 +01:00
if (class_exists($className)) {
event(new $className($entity, $fromDeleted));
}
2015-10-28 20:22:07 +01:00
}
/**
* @param $entity
*/
2015-10-28 20:22:07 +01:00
public function delete($entity)
{
2016-03-23 14:02:05 +01:00
if ($entity->is_deleted) {
return;
}
2015-10-28 20:22:07 +01:00
$entity->is_deleted = true;
$entity->save();
$entity->delete();
2015-10-29 23:27:26 +01:00
$className = $this->getEventClass($entity, 'Deleted');
2015-11-05 23:37:04 +01:00
if (class_exists($className)) {
event(new $className($entity));
}
2015-10-28 20:22:07 +01:00
}
2016-12-08 23:34:24 +01:00
/**
* @param $ids
* @param $action
2017-01-30 20:40:43 +01:00
*
2016-12-08 23:34:24 +01:00
* @return int
*/
public function bulk($ids, $action)
{
2017-01-30 17:05:31 +01:00
if (! $ids) {
2016-12-08 23:34:24 +01:00
return 0;
}
$entities = $this->findByPublicIdsWithTrashed($ids);
foreach ($entities as $entity) {
if (Auth::user()->can('edit', $entity)) {
$this->$action($entity);
}
}
return count($entities);
}
/**
* @param $ids
2017-01-30 20:40:43 +01:00
*
* @return mixed
*/
2015-10-28 20:22:07 +01:00
public function findByPublicIds($ids)
{
return $this->getInstance()->scope($ids)->get();
}
/**
* @param $ids
2017-01-30 20:40:43 +01:00
*
* @return mixed
*/
2015-10-28 20:22:07 +01:00
public function findByPublicIdsWithTrashed($ids)
{
return $this->getInstance()->scope($ids)->withTrashed()->get();
}
2016-11-18 14:31:43 +01:00
protected function applyFilters($query, $entityType, $table = false)
{
$table = Utils::pluralizeEntityType($table ?: $entityType);
2016-11-20 15:08:36 +01:00
if ($filter = session('entity_state_filter:' . $entityType, STATUS_ACTIVE)) {
$filters = explode(',', $filter);
2016-11-18 14:31:43 +01:00
$query->where(function ($query) use ($filters, $table) {
$query->whereNull($table . '.id');
if (in_array(STATUS_ACTIVE, $filters)) {
$query->orWhereNull($table . '.deleted_at');
}
if (in_array(STATUS_ARCHIVED, $filters)) {
2016-11-20 15:08:36 +01:00
$query->orWhere(function ($query) use ($table) {
$query->whereNotNull($table . '.deleted_at');
2017-01-30 17:05:31 +01:00
if (! in_array($table, ['users'])) {
$query->where($table . '.is_deleted', '=', 0);
}
2016-11-20 15:08:36 +01:00
});
2016-11-18 14:31:43 +01:00
}
if (in_array(STATUS_DELETED, $filters)) {
2016-11-20 15:08:36 +01:00
$query->orWhere(function ($query) use ($table) {
$query->whereNotNull($table . '.deleted_at')
->where($table . '.is_deleted', '=', 1);
});
2016-11-18 14:31:43 +01:00
}
});
}
return $query;
}
2015-10-28 20:22:07 +01:00
}