@@ -5,102 +5,102 @@ |
||
| 5 | 5 | |
| 6 | 6 | class CachingDecorator |
| 7 | 7 | { |
| 8 | - /** |
|
| 9 | - * @var string |
|
| 10 | - */ |
|
| 11 | - public $repo; |
|
| 8 | + /** |
|
| 9 | + * @var string |
|
| 10 | + */ |
|
| 11 | + public $repo; |
|
| 12 | 12 | |
| 13 | - /** |
|
| 14 | - * @var Cache |
|
| 15 | - */ |
|
| 16 | - protected $cache; |
|
| 13 | + /** |
|
| 14 | + * @var Cache |
|
| 15 | + */ |
|
| 16 | + protected $cache; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * @var string |
|
| 20 | - */ |
|
| 21 | - public $modelKey; |
|
| 18 | + /** |
|
| 19 | + * @var string |
|
| 20 | + */ |
|
| 21 | + public $modelKey; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * @var string |
|
| 25 | - */ |
|
| 26 | - public $model; |
|
| 23 | + /** |
|
| 24 | + * @var string |
|
| 25 | + */ |
|
| 26 | + public $model; |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * @var string |
|
| 30 | - */ |
|
| 31 | - public $modelClass; |
|
| 28 | + /** |
|
| 29 | + * @var string |
|
| 30 | + */ |
|
| 31 | + public $modelClass; |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * @var mixed |
|
| 35 | - */ |
|
| 36 | - public $cacheConfig; |
|
| 33 | + /** |
|
| 34 | + * @var mixed |
|
| 35 | + */ |
|
| 36 | + public $cacheConfig; |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * @var string |
|
| 40 | - */ |
|
| 41 | - public $cacheTag; |
|
| 38 | + /** |
|
| 39 | + * @var string |
|
| 40 | + */ |
|
| 41 | + public $cacheTag; |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Init new object. |
|
| 45 | - * |
|
| 46 | - * @param string $repo |
|
| 47 | - * @param Cache $cache |
|
| 48 | - * |
|
| 49 | - * @return void |
|
| 50 | - */ |
|
| 51 | - public function __construct($repo, Cache $cache) |
|
| 52 | - { |
|
| 53 | - $this->repo = $repo; |
|
| 54 | - $this->cache = $cache; |
|
| 55 | - $this->model = $this->repo->model; |
|
| 56 | - $this->modelClass = get_class($this->model); |
|
| 57 | - $repoClass = explode('\\', get_class($this->repo)); |
|
| 58 | - $repoName = end($repoClass); |
|
| 59 | - $this->cacheTag = lcfirst(substr($repoName, 0, strpos($repoName, 'Repository'))); |
|
| 60 | - } |
|
| 43 | + /** |
|
| 44 | + * Init new object. |
|
| 45 | + * |
|
| 46 | + * @param string $repo |
|
| 47 | + * @param Cache $cache |
|
| 48 | + * |
|
| 49 | + * @return void |
|
| 50 | + */ |
|
| 51 | + public function __construct($repo, Cache $cache) |
|
| 52 | + { |
|
| 53 | + $this->repo = $repo; |
|
| 54 | + $this->cache = $cache; |
|
| 55 | + $this->model = $this->repo->model; |
|
| 56 | + $this->modelClass = get_class($this->model); |
|
| 57 | + $repoClass = explode('\\', get_class($this->repo)); |
|
| 58 | + $repoName = end($repoClass); |
|
| 59 | + $this->cacheTag = lcfirst(substr($repoName, 0, strpos($repoName, 'Repository'))); |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Handle the cache mechanism for the called method |
|
| 64 | - * based the configurations. |
|
| 65 | - * |
|
| 66 | - * @param string $name the called method name |
|
| 67 | - * @param array $arguments the method arguments |
|
| 68 | - * @return object |
|
| 69 | - */ |
|
| 70 | - public function __call($name, $arguments) |
|
| 71 | - { |
|
| 72 | - $this->setCacheConfig($name); |
|
| 62 | + /** |
|
| 63 | + * Handle the cache mechanism for the called method |
|
| 64 | + * based the configurations. |
|
| 65 | + * |
|
| 66 | + * @param string $name the called method name |
|
| 67 | + * @param array $arguments the method arguments |
|
| 68 | + * @return object |
|
| 69 | + */ |
|
| 70 | + public function __call($name, $arguments) |
|
| 71 | + { |
|
| 72 | + $this->setCacheConfig($name); |
|
| 73 | 73 | |
| 74 | - if ($this->cacheConfig && $this->cacheConfig == 'cache') { |
|
| 75 | - $page = \Request::get('page') !== null ? \Request::get('page') : '1'; |
|
| 76 | - $cacheKey = $name.$page.\Session::get('locale').serialize($arguments); |
|
| 77 | - return $this->cache->tags([$this->cacheTag])->rememberForever($cacheKey, function () use ($arguments, $name) { |
|
| 78 | - return call_user_func_array([$this->repo, $name], $arguments); |
|
| 79 | - }); |
|
| 80 | - } elseif ($this->cacheConfig) { |
|
| 81 | - $this->cache->tags($this->cacheConfig)->flush(); |
|
| 82 | - return call_user_func_array([$this->repo, $name], $arguments); |
|
| 83 | - } |
|
| 74 | + if ($this->cacheConfig && $this->cacheConfig == 'cache') { |
|
| 75 | + $page = \Request::get('page') !== null ? \Request::get('page') : '1'; |
|
| 76 | + $cacheKey = $name.$page.\Session::get('locale').serialize($arguments); |
|
| 77 | + return $this->cache->tags([$this->cacheTag])->rememberForever($cacheKey, function () use ($arguments, $name) { |
|
| 78 | + return call_user_func_array([$this->repo, $name], $arguments); |
|
| 79 | + }); |
|
| 80 | + } elseif ($this->cacheConfig) { |
|
| 81 | + $this->cache->tags($this->cacheConfig)->flush(); |
|
| 82 | + return call_user_func_array([$this->repo, $name], $arguments); |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - return call_user_func_array([$this->repo, $name], $arguments); |
|
| 86 | - } |
|
| 85 | + return call_user_func_array([$this->repo, $name], $arguments); |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - /** |
|
| 89 | - * Set cache config based on the called method. |
|
| 90 | - * |
|
| 91 | - * @param string $name |
|
| 92 | - * @return void |
|
| 93 | - */ |
|
| 94 | - private function setCacheConfig($name) |
|
| 95 | - { |
|
| 96 | - $config = \CoreConfig::getConfig(); |
|
| 97 | - $cacheConfig = Arr::get($config['cacheConfig'], $this->cacheTag, false); |
|
| 98 | - $this->cacheConfig = false; |
|
| 88 | + /** |
|
| 89 | + * Set cache config based on the called method. |
|
| 90 | + * |
|
| 91 | + * @param string $name |
|
| 92 | + * @return void |
|
| 93 | + */ |
|
| 94 | + private function setCacheConfig($name) |
|
| 95 | + { |
|
| 96 | + $config = \CoreConfig::getConfig(); |
|
| 97 | + $cacheConfig = Arr::get($config['cacheConfig'], $this->cacheTag, false); |
|
| 98 | + $this->cacheConfig = false; |
|
| 99 | 99 | |
| 100 | - if ($cacheConfig && in_array($name, $cacheConfig['cache'])) { |
|
| 101 | - $this->cacheConfig = 'cache'; |
|
| 102 | - } elseif ($cacheConfig && isset($cacheConfig['clear'][$name])) { |
|
| 103 | - $this->cacheConfig = $cacheConfig['clear'][$name]; |
|
| 104 | - } |
|
| 105 | - } |
|
| 100 | + if ($cacheConfig && in_array($name, $cacheConfig['cache'])) { |
|
| 101 | + $this->cacheConfig = 'cache'; |
|
| 102 | + } elseif ($cacheConfig && isset($cacheConfig['clear'][$name])) { |
|
| 103 | + $this->cacheConfig = $cacheConfig['clear'][$name]; |
|
| 104 | + } |
|
| 105 | + } |
|
| 106 | 106 | } |
@@ -8,89 +8,89 @@ |
||
| 8 | 8 | |
| 9 | 9 | class BaseApiController extends Controller |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * Array of eager loaded relations. |
|
| 13 | - * |
|
| 14 | - * @var array |
|
| 15 | - */ |
|
| 16 | - protected $relations; |
|
| 11 | + /** |
|
| 12 | + * Array of eager loaded relations. |
|
| 13 | + * |
|
| 14 | + * @var array |
|
| 15 | + */ |
|
| 16 | + protected $relations; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * @var object |
|
| 20 | - */ |
|
| 21 | - protected $service; |
|
| 18 | + /** |
|
| 19 | + * @var object |
|
| 20 | + */ |
|
| 21 | + protected $service; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Path of the model resource. |
|
| 25 | - * |
|
| 26 | - * @var string |
|
| 27 | - */ |
|
| 28 | - protected $modelResource; |
|
| 23 | + /** |
|
| 24 | + * Path of the model resource. |
|
| 25 | + * |
|
| 26 | + * @var string |
|
| 27 | + */ |
|
| 28 | + protected $modelResource; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Init new object. |
|
| 32 | - * |
|
| 33 | - * @param mixed $service |
|
| 34 | - * @return void |
|
| 35 | - */ |
|
| 36 | - public function __construct($service) |
|
| 37 | - { |
|
| 38 | - $this->service = $service; |
|
| 39 | - } |
|
| 30 | + /** |
|
| 31 | + * Init new object. |
|
| 32 | + * |
|
| 33 | + * @param mixed $service |
|
| 34 | + * @return void |
|
| 35 | + */ |
|
| 36 | + public function __construct($service) |
|
| 37 | + { |
|
| 38 | + $this->service = $service; |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Fetch all records with relations from storage. |
|
| 43 | - * |
|
| 44 | - * @param Request $request |
|
| 45 | - * @return \Illuminate\Http\Response |
|
| 46 | - */ |
|
| 47 | - public function index(Request $request) |
|
| 48 | - { |
|
| 49 | - return $this->modelResource::collection($this->service->list($request->relations, $request->query(), $request->query('perPage'), $request->query('sortBy'), $request->query('desc'))); |
|
| 50 | - } |
|
| 41 | + /** |
|
| 42 | + * Fetch all records with relations from storage. |
|
| 43 | + * |
|
| 44 | + * @param Request $request |
|
| 45 | + * @return \Illuminate\Http\Response |
|
| 46 | + */ |
|
| 47 | + public function index(Request $request) |
|
| 48 | + { |
|
| 49 | + return $this->modelResource::collection($this->service->list($request->relations, $request->query(), $request->query('perPage'), $request->query('sortBy'), $request->query('desc'))); |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Fetch the single object with relations from storage. |
|
| 54 | - * |
|
| 55 | - * @param Request $request |
|
| 56 | - * @param integer $id Id of the requested model. |
|
| 57 | - * @return \Illuminate\Http\Response |
|
| 58 | - */ |
|
| 59 | - public function find(Request $request, $id) |
|
| 60 | - { |
|
| 61 | - return new $this->modelResource($this->service->find($id, $request->relations)); |
|
| 62 | - } |
|
| 52 | + /** |
|
| 53 | + * Fetch the single object with relations from storage. |
|
| 54 | + * |
|
| 55 | + * @param Request $request |
|
| 56 | + * @param integer $id Id of the requested model. |
|
| 57 | + * @return \Illuminate\Http\Response |
|
| 58 | + */ |
|
| 59 | + public function find(Request $request, $id) |
|
| 60 | + { |
|
| 61 | + return new $this->modelResource($this->service->find($id, $request->relations)); |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * Delete by the given id from storage. |
|
| 66 | - * |
|
| 67 | - * @param integer $id Id of the deleted model. |
|
| 68 | - * @return \Illuminate\Http\Response |
|
| 69 | - */ |
|
| 70 | - public function delete($id) |
|
| 71 | - { |
|
| 72 | - return new GeneralResource($this->service->delete($id)); |
|
| 73 | - } |
|
| 64 | + /** |
|
| 65 | + * Delete by the given id from storage. |
|
| 66 | + * |
|
| 67 | + * @param integer $id Id of the deleted model. |
|
| 68 | + * @return \Illuminate\Http\Response |
|
| 69 | + */ |
|
| 70 | + public function delete($id) |
|
| 71 | + { |
|
| 72 | + return new GeneralResource($this->service->delete($id)); |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * Return the deleted models in pages based on the given conditions. |
|
| 77 | - * |
|
| 78 | - * @param Request $request |
|
| 79 | - * @return \Illuminate\Http\Response |
|
| 80 | - */ |
|
| 81 | - public function deleted(Request $request) |
|
| 82 | - { |
|
| 83 | - return $this->modelResource::collection($this->service->deleted($request->all(), $request->query('perPage'), $request->query('sortBy'), $request->query('desc'))); |
|
| 84 | - } |
|
| 75 | + /** |
|
| 76 | + * Return the deleted models in pages based on the given conditions. |
|
| 77 | + * |
|
| 78 | + * @param Request $request |
|
| 79 | + * @return \Illuminate\Http\Response |
|
| 80 | + */ |
|
| 81 | + public function deleted(Request $request) |
|
| 82 | + { |
|
| 83 | + return $this->modelResource::collection($this->service->deleted($request->all(), $request->query('perPage'), $request->query('sortBy'), $request->query('desc'))); |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - /** |
|
| 87 | - * Restore the deleted model. |
|
| 88 | - * |
|
| 89 | - * @param integer $id Id of the restored model. |
|
| 90 | - * @return \Illuminate\Http\Response |
|
| 91 | - */ |
|
| 92 | - public function restore($id) |
|
| 93 | - { |
|
| 94 | - return new GeneralResource($this->service->restore($id)); |
|
| 95 | - } |
|
| 86 | + /** |
|
| 87 | + * Restore the deleted model. |
|
| 88 | + * |
|
| 89 | + * @param integer $id Id of the restored model. |
|
| 90 | + * @return \Illuminate\Http\Response |
|
| 91 | + */ |
|
| 92 | + public function restore($id) |
|
| 93 | + { |
|
| 94 | + return new GeneralResource($this->service->restore($id)); |
|
| 95 | + } |
|
| 96 | 96 | } |
@@ -6,676 +6,676 @@ |
||
| 6 | 6 | |
| 7 | 7 | abstract class BaseRepository implements BaseRepositoryInterface |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * @var object |
|
| 11 | - */ |
|
| 12 | - public $model; |
|
| 9 | + /** |
|
| 10 | + * @var object |
|
| 11 | + */ |
|
| 12 | + public $model; |
|
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * Init new object. |
|
| 16 | - * |
|
| 17 | - * @var mixed model |
|
| 18 | - * @return void |
|
| 19 | - */ |
|
| 20 | - public function __construct($model) |
|
| 21 | - { |
|
| 22 | - $this->model = $model; |
|
| 23 | - } |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * Fetch records with relations based on the given params. |
|
| 27 | - * |
|
| 28 | - * @param string $relations |
|
| 29 | - * @param array $conditions |
|
| 30 | - * @param integer $perPage |
|
| 31 | - * @param string $sortBy |
|
| 32 | - * @param boolean $desc |
|
| 33 | - * @return collection |
|
| 34 | - */ |
|
| 35 | - public function list($relations = [], $conditions = false, $perPage = 15, $sortBy = 'created_at', $desc = true) |
|
| 36 | - { |
|
| 37 | - unset($conditions['perPage']); |
|
| 38 | - unset($conditions['sortBy']); |
|
| 39 | - unset($conditions['sort']); |
|
| 40 | - unset($conditions['page']); |
|
| 41 | - |
|
| 42 | - if (count($conditions)) { |
|
| 43 | - return $this->paginateBy(['and' => $conditions], $perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true); |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - return $this->paginate($perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true); |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Fetch all records with relations from the storage. |
|
| 51 | - * |
|
| 52 | - * @param array $relations |
|
| 53 | - * @param string $sortBy |
|
| 54 | - * @param boolean $desc |
|
| 55 | - * @param array $columns |
|
| 56 | - * @return collection |
|
| 57 | - */ |
|
| 58 | - public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 59 | - { |
|
| 60 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 61 | - return $this->model->with($relations)->orderBy($sortBy, $sort)->get($columns); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Fetch all records with relations from storage in pages |
|
| 66 | - * that matche the given query. |
|
| 67 | - * |
|
| 68 | - * @param string $query |
|
| 69 | - * @param integer $perPage |
|
| 70 | - * @param array $relations |
|
| 71 | - * @param string $sortBy |
|
| 72 | - * @param boolean $desc |
|
| 73 | - * @param array $columns |
|
| 74 | - * @return collection |
|
| 75 | - */ |
|
| 76 | - public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 77 | - { |
|
| 78 | - $model = $this->model->with($relations); |
|
| 79 | - $conditionColumns = $this->model->searchable; |
|
| 80 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Construct the select conditions for the model. |
|
| 84 | - */ |
|
| 85 | - $model->where(function ($q) use ($query, $conditionColumns, $relations) { |
|
| 86 | - |
|
| 87 | - if (count($conditionColumns)) { |
|
| 88 | - $column = 'LOWER('.array_shift($conditionColumns).')'; |
|
| 89 | - if (Str::contains($column, '->')) { |
|
| 90 | - $column = $this->wrapJsonSelector($column); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Use the first element in the model columns to construct the first condition. |
|
| 95 | - */ |
|
| 96 | - $q->where(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * Loop through the rest of the columns to construct or where conditions. |
|
| 101 | - */ |
|
| 102 | - foreach ($conditionColumns as $column) { |
|
| 103 | - $column = 'LOWER('.$column.')'; |
|
| 104 | - if (Str::contains($column, '->')) { |
|
| 105 | - $column = $this->wrapJsonSelector($column); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - $q->orWhere(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Loop through the model relations. |
|
| 113 | - */ |
|
| 114 | - foreach ($relations as $relation) { |
|
| 115 | - /** |
|
| 116 | - * Remove the sub relation if exists. |
|
| 117 | - */ |
|
| 118 | - $relation = explode('.', $relation)[0]; |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * Try to fetch the relation repository from the core. |
|
| 122 | - */ |
|
| 123 | - if (\Core::$relation()) { |
|
| 124 | - /** |
|
| 125 | - * Construct the relation condition. |
|
| 126 | - */ |
|
| 127 | - $q->orWhereHas($relation, function ($subModel) use ($query, $relation) { |
|
| 128 | - |
|
| 129 | - $subModel->where(function ($q) use ($query, $relation) { |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * Get columns of the relation. |
|
| 133 | - */ |
|
| 134 | - $subConditionColumns = \Core::$relation()->model->searchable; |
|
| 135 | - |
|
| 136 | - if (count($subConditionColumns)) { |
|
| 137 | - $column = 'LOWER('.array_shift($subConditionColumns).')'; |
|
| 138 | - if (Str::contains($column, '->')) { |
|
| 139 | - $column = $this->wrapJsonSelector($column); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * Use the first element in the relation model columns to construct the first condition. |
|
| 144 | - */ |
|
| 145 | - $q->where(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * Loop through the rest of the columns to construct or where conditions. |
|
| 150 | - */ |
|
| 151 | - foreach ($subConditionColumns as $subConditionColumn) { |
|
| 152 | - $column = 'LOWER('.$subConditionColumn.')'; |
|
| 153 | - if (Str::contains($column, '->')) { |
|
| 154 | - $column = $this->wrapJsonSelector($column); |
|
| 155 | - } |
|
| 14 | + /** |
|
| 15 | + * Init new object. |
|
| 16 | + * |
|
| 17 | + * @var mixed model |
|
| 18 | + * @return void |
|
| 19 | + */ |
|
| 20 | + public function __construct($model) |
|
| 21 | + { |
|
| 22 | + $this->model = $model; |
|
| 23 | + } |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * Fetch records with relations based on the given params. |
|
| 27 | + * |
|
| 28 | + * @param string $relations |
|
| 29 | + * @param array $conditions |
|
| 30 | + * @param integer $perPage |
|
| 31 | + * @param string $sortBy |
|
| 32 | + * @param boolean $desc |
|
| 33 | + * @return collection |
|
| 34 | + */ |
|
| 35 | + public function list($relations = [], $conditions = false, $perPage = 15, $sortBy = 'created_at', $desc = true) |
|
| 36 | + { |
|
| 37 | + unset($conditions['perPage']); |
|
| 38 | + unset($conditions['sortBy']); |
|
| 39 | + unset($conditions['sort']); |
|
| 40 | + unset($conditions['page']); |
|
| 41 | + |
|
| 42 | + if (count($conditions)) { |
|
| 43 | + return $this->paginateBy(['and' => $conditions], $perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true); |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + return $this->paginate($perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true); |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Fetch all records with relations from the storage. |
|
| 51 | + * |
|
| 52 | + * @param array $relations |
|
| 53 | + * @param string $sortBy |
|
| 54 | + * @param boolean $desc |
|
| 55 | + * @param array $columns |
|
| 56 | + * @return collection |
|
| 57 | + */ |
|
| 58 | + public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 59 | + { |
|
| 60 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 61 | + return $this->model->with($relations)->orderBy($sortBy, $sort)->get($columns); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Fetch all records with relations from storage in pages |
|
| 66 | + * that matche the given query. |
|
| 67 | + * |
|
| 68 | + * @param string $query |
|
| 69 | + * @param integer $perPage |
|
| 70 | + * @param array $relations |
|
| 71 | + * @param string $sortBy |
|
| 72 | + * @param boolean $desc |
|
| 73 | + * @param array $columns |
|
| 74 | + * @return collection |
|
| 75 | + */ |
|
| 76 | + public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 77 | + { |
|
| 78 | + $model = $this->model->with($relations); |
|
| 79 | + $conditionColumns = $this->model->searchable; |
|
| 80 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Construct the select conditions for the model. |
|
| 84 | + */ |
|
| 85 | + $model->where(function ($q) use ($query, $conditionColumns, $relations) { |
|
| 86 | + |
|
| 87 | + if (count($conditionColumns)) { |
|
| 88 | + $column = 'LOWER('.array_shift($conditionColumns).')'; |
|
| 89 | + if (Str::contains($column, '->')) { |
|
| 90 | + $column = $this->wrapJsonSelector($column); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Use the first element in the model columns to construct the first condition. |
|
| 95 | + */ |
|
| 96 | + $q->where(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * Loop through the rest of the columns to construct or where conditions. |
|
| 101 | + */ |
|
| 102 | + foreach ($conditionColumns as $column) { |
|
| 103 | + $column = 'LOWER('.$column.')'; |
|
| 104 | + if (Str::contains($column, '->')) { |
|
| 105 | + $column = $this->wrapJsonSelector($column); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + $q->orWhere(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Loop through the model relations. |
|
| 113 | + */ |
|
| 114 | + foreach ($relations as $relation) { |
|
| 115 | + /** |
|
| 116 | + * Remove the sub relation if exists. |
|
| 117 | + */ |
|
| 118 | + $relation = explode('.', $relation)[0]; |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * Try to fetch the relation repository from the core. |
|
| 122 | + */ |
|
| 123 | + if (\Core::$relation()) { |
|
| 124 | + /** |
|
| 125 | + * Construct the relation condition. |
|
| 126 | + */ |
|
| 127 | + $q->orWhereHas($relation, function ($subModel) use ($query, $relation) { |
|
| 128 | + |
|
| 129 | + $subModel->where(function ($q) use ($query, $relation) { |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * Get columns of the relation. |
|
| 133 | + */ |
|
| 134 | + $subConditionColumns = \Core::$relation()->model->searchable; |
|
| 135 | + |
|
| 136 | + if (count($subConditionColumns)) { |
|
| 137 | + $column = 'LOWER('.array_shift($subConditionColumns).')'; |
|
| 138 | + if (Str::contains($column, '->')) { |
|
| 139 | + $column = $this->wrapJsonSelector($column); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * Use the first element in the relation model columns to construct the first condition. |
|
| 144 | + */ |
|
| 145 | + $q->where(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * Loop through the rest of the columns to construct or where conditions. |
|
| 150 | + */ |
|
| 151 | + foreach ($subConditionColumns as $subConditionColumn) { |
|
| 152 | + $column = 'LOWER('.$subConditionColumn.')'; |
|
| 153 | + if (Str::contains($column, '->')) { |
|
| 154 | + $column = $this->wrapJsonSelector($column); |
|
| 155 | + } |
|
| 156 | 156 | |
| 157 | - $q->orWhere(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
| 158 | - } |
|
| 159 | - }); |
|
| 160 | - }); |
|
| 161 | - } |
|
| 162 | - } |
|
| 163 | - }); |
|
| 157 | + $q->orWhere(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
| 158 | + } |
|
| 159 | + }); |
|
| 160 | + }); |
|
| 161 | + } |
|
| 162 | + } |
|
| 163 | + }); |
|
| 164 | 164 | |
| 165 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 166 | - } |
|
| 165 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 166 | + } |
|
| 167 | 167 | |
| 168 | - /** |
|
| 169 | - * Fetch all records with relations from storage in pages. |
|
| 170 | - * |
|
| 171 | - * @param integer $perPage |
|
| 172 | - * @param array $relations |
|
| 173 | - * @param string $sortBy |
|
| 174 | - * @param boolean $desc |
|
| 175 | - * @param array $columns |
|
| 176 | - * @return collection |
|
| 177 | - */ |
|
| 178 | - public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 179 | - { |
|
| 180 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 181 | - return $this->model->with($relations)->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - /** |
|
| 185 | - * Fetch all records with relations based on |
|
| 186 | - * the given condition from storage in pages. |
|
| 187 | - * |
|
| 188 | - * @param array $conditions array of conditions |
|
| 189 | - * @param integer $perPage |
|
| 190 | - * @param array $relations |
|
| 191 | - * @param string $sortBy |
|
| 192 | - * @param boolean $desc |
|
| 193 | - * @param array $columns |
|
| 194 | - * @return collection |
|
| 195 | - */ |
|
| 196 | - public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 197 | - { |
|
| 198 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 199 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 200 | - return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 201 | - } |
|
| 168 | + /** |
|
| 169 | + * Fetch all records with relations from storage in pages. |
|
| 170 | + * |
|
| 171 | + * @param integer $perPage |
|
| 172 | + * @param array $relations |
|
| 173 | + * @param string $sortBy |
|
| 174 | + * @param boolean $desc |
|
| 175 | + * @param array $columns |
|
| 176 | + * @return collection |
|
| 177 | + */ |
|
| 178 | + public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 179 | + { |
|
| 180 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 181 | + return $this->model->with($relations)->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + /** |
|
| 185 | + * Fetch all records with relations based on |
|
| 186 | + * the given condition from storage in pages. |
|
| 187 | + * |
|
| 188 | + * @param array $conditions array of conditions |
|
| 189 | + * @param integer $perPage |
|
| 190 | + * @param array $relations |
|
| 191 | + * @param string $sortBy |
|
| 192 | + * @param boolean $desc |
|
| 193 | + * @param array $columns |
|
| 194 | + * @return collection |
|
| 195 | + */ |
|
| 196 | + public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 197 | + { |
|
| 198 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 199 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 200 | + return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 201 | + } |
|
| 202 | 202 | |
| 203 | - /** |
|
| 204 | - * Save the given model to the storage. |
|
| 205 | - * |
|
| 206 | - * @param array $data |
|
| 207 | - * @return mixed |
|
| 208 | - */ |
|
| 209 | - public function save(array $data) |
|
| 210 | - { |
|
| 211 | - \Session::put('locale', 'all'); |
|
| 212 | - $model = false; |
|
| 213 | - $relations = []; |
|
| 214 | - |
|
| 215 | - \DB::transaction(function () use (&$model, $relations, $data) { |
|
| 203 | + /** |
|
| 204 | + * Save the given model to the storage. |
|
| 205 | + * |
|
| 206 | + * @param array $data |
|
| 207 | + * @return mixed |
|
| 208 | + */ |
|
| 209 | + public function save(array $data) |
|
| 210 | + { |
|
| 211 | + \Session::put('locale', 'all'); |
|
| 212 | + $model = false; |
|
| 213 | + $relations = []; |
|
| 214 | + |
|
| 215 | + \DB::transaction(function () use (&$model, $relations, $data) { |
|
| 216 | 216 | |
| 217 | - $model = $this->prepareModel($data); |
|
| 218 | - $relations = $this->prepareRelations($data, $model); |
|
| 219 | - $model = $this->saveModel($model, $relations); |
|
| 220 | - }); |
|
| 217 | + $model = $this->prepareModel($data); |
|
| 218 | + $relations = $this->prepareRelations($data, $model); |
|
| 219 | + $model = $this->saveModel($model, $relations); |
|
| 220 | + }); |
|
| 221 | 221 | |
| 222 | - return $model; |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * Delete record from the storage based on the given |
|
| 227 | - * condition. |
|
| 228 | - * |
|
| 229 | - * @param var $value condition value |
|
| 230 | - * @param string $attribute condition column name |
|
| 231 | - * @return void |
|
| 232 | - */ |
|
| 233 | - public function delete($value, $attribute = 'id') |
|
| 234 | - { |
|
| 235 | - if ($attribute == 'id') { |
|
| 236 | - \DB::transaction(function () use ($value) { |
|
| 237 | - $model = $this->model->lockForUpdate()->find($value); |
|
| 238 | - if (! $model) { |
|
| 239 | - \ErrorHandler::notFound(class_basename($this->model).' with id : '.$value); |
|
| 240 | - } |
|
| 222 | + return $model; |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * Delete record from the storage based on the given |
|
| 227 | + * condition. |
|
| 228 | + * |
|
| 229 | + * @param var $value condition value |
|
| 230 | + * @param string $attribute condition column name |
|
| 231 | + * @return void |
|
| 232 | + */ |
|
| 233 | + public function delete($value, $attribute = 'id') |
|
| 234 | + { |
|
| 235 | + if ($attribute == 'id') { |
|
| 236 | + \DB::transaction(function () use ($value) { |
|
| 237 | + $model = $this->model->lockForUpdate()->find($value); |
|
| 238 | + if (! $model) { |
|
| 239 | + \ErrorHandler::notFound(class_basename($this->model).' with id : '.$value); |
|
| 240 | + } |
|
| 241 | 241 | |
| 242 | - $model->delete(); |
|
| 243 | - }); |
|
| 244 | - } else { |
|
| 245 | - \DB::transaction(function () use ($value, $attribute) { |
|
| 246 | - $this->model->where($attribute, '=', $value)->lockForUpdate()->get()->each(function ($model) { |
|
| 247 | - $model->delete(); |
|
| 248 | - }); |
|
| 249 | - }); |
|
| 250 | - } |
|
| 251 | - } |
|
| 242 | + $model->delete(); |
|
| 243 | + }); |
|
| 244 | + } else { |
|
| 245 | + \DB::transaction(function () use ($value, $attribute) { |
|
| 246 | + $this->model->where($attribute, '=', $value)->lockForUpdate()->get()->each(function ($model) { |
|
| 247 | + $model->delete(); |
|
| 248 | + }); |
|
| 249 | + }); |
|
| 250 | + } |
|
| 251 | + } |
|
| 252 | 252 | |
| 253 | - /** |
|
| 254 | - * Fetch records from the storage based on the given |
|
| 255 | - * id. |
|
| 256 | - * |
|
| 257 | - * @param integer $id |
|
| 258 | - * @param string[] $relations |
|
| 259 | - * @param array $columns |
|
| 260 | - * @return object |
|
| 261 | - */ |
|
| 262 | - public function find($id, $relations = [], $columns = ['*']) |
|
| 263 | - { |
|
| 264 | - return $this->model->with($relations)->find($id, $columns); |
|
| 265 | - } |
|
| 253 | + /** |
|
| 254 | + * Fetch records from the storage based on the given |
|
| 255 | + * id. |
|
| 256 | + * |
|
| 257 | + * @param integer $id |
|
| 258 | + * @param string[] $relations |
|
| 259 | + * @param array $columns |
|
| 260 | + * @return object |
|
| 261 | + */ |
|
| 262 | + public function find($id, $relations = [], $columns = ['*']) |
|
| 263 | + { |
|
| 264 | + return $this->model->with($relations)->find($id, $columns); |
|
| 265 | + } |
|
| 266 | 266 | |
| 267 | - /** |
|
| 268 | - * Fetch records from the storage based on the given |
|
| 269 | - * condition. |
|
| 270 | - * |
|
| 271 | - * @param array $conditions array of conditions |
|
| 272 | - * @param array $relations |
|
| 273 | - * @param string $sortBy |
|
| 274 | - * @param boolean $desc |
|
| 275 | - * @param array $columns |
|
| 276 | - * @return collection |
|
| 277 | - */ |
|
| 278 | - public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 279 | - { |
|
| 280 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 281 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 282 | - return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - /** |
|
| 286 | - * Fetch the first record from the storage based on the given |
|
| 287 | - * condition. |
|
| 288 | - * |
|
| 289 | - * @param array $conditions array of conditions |
|
| 290 | - * @param array $relations |
|
| 291 | - * @param array $columns |
|
| 292 | - * @return object |
|
| 293 | - */ |
|
| 294 | - public function first($conditions, $relations = [], $columns = ['*']) |
|
| 295 | - { |
|
| 296 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 297 | - return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - /** |
|
| 301 | - * Return the deleted models in pages based on the given conditions. |
|
| 302 | - * |
|
| 303 | - * @param array $conditions array of conditions |
|
| 304 | - * @param integer $perPage |
|
| 305 | - * @param string $sortBy |
|
| 306 | - * @param boolean $desc |
|
| 307 | - * @param array $columns |
|
| 308 | - * @return collection |
|
| 309 | - */ |
|
| 310 | - public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 311 | - { |
|
| 312 | - unset($conditions['page']); |
|
| 313 | - unset($conditions['perPage']); |
|
| 314 | - unset($conditions['sortBy']); |
|
| 315 | - unset($conditions['sort']); |
|
| 316 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 317 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 318 | - $model = $this->model->onlyTrashed(); |
|
| 319 | - |
|
| 320 | - if (count($conditions['conditionValues'])) { |
|
| 321 | - $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - /** |
|
| 328 | - * Restore the deleted model. |
|
| 329 | - * |
|
| 330 | - * @param integer $id |
|
| 331 | - * @return void |
|
| 332 | - */ |
|
| 333 | - public function restore($id) |
|
| 334 | - { |
|
| 335 | - $model = $this->model->onlyTrashed()->find($id); |
|
| 336 | - |
|
| 337 | - if (! $model) { |
|
| 338 | - \ErrorHandler::notFound(class_basename($this->model).' with id : '.$id); |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - $model->restore(); |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - /** |
|
| 345 | - * Fill the model with the given data. |
|
| 346 | - * |
|
| 347 | - * @param array $data |
|
| 348 | - * |
|
| 349 | - * @return object |
|
| 350 | - */ |
|
| 351 | - public function prepareModel($data) |
|
| 352 | - { |
|
| 353 | - $modelClass = $this->model; |
|
| 354 | - |
|
| 355 | - /** |
|
| 356 | - * If the id is present in the data then select the model for updating, |
|
| 357 | - * else create new model. |
|
| 358 | - * @var array |
|
| 359 | - */ |
|
| 360 | - $model = Arr::has($data, 'id') ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
| 361 | - if (! $model) { |
|
| 362 | - \ErrorHandler::notFound(class_basename($modelClass).' with id : '.$data['id']); |
|
| 363 | - } |
|
| 364 | - |
|
| 365 | - /** |
|
| 366 | - * Construct the model object with the given data, |
|
| 367 | - * and if there is a relation add it to relations array, |
|
| 368 | - * then save the model. |
|
| 369 | - */ |
|
| 370 | - foreach ($data as $key => $value) { |
|
| 371 | - if (array_search($key, $model->getFillable(), true) !== false) { |
|
| 372 | - /** |
|
| 373 | - * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
| 374 | - */ |
|
| 375 | - $model->$key = $value; |
|
| 376 | - } |
|
| 377 | - } |
|
| 378 | - |
|
| 379 | - return $model; |
|
| 380 | - } |
|
| 267 | + /** |
|
| 268 | + * Fetch records from the storage based on the given |
|
| 269 | + * condition. |
|
| 270 | + * |
|
| 271 | + * @param array $conditions array of conditions |
|
| 272 | + * @param array $relations |
|
| 273 | + * @param string $sortBy |
|
| 274 | + * @param boolean $desc |
|
| 275 | + * @param array $columns |
|
| 276 | + * @return collection |
|
| 277 | + */ |
|
| 278 | + public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 279 | + { |
|
| 280 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 281 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 282 | + return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + /** |
|
| 286 | + * Fetch the first record from the storage based on the given |
|
| 287 | + * condition. |
|
| 288 | + * |
|
| 289 | + * @param array $conditions array of conditions |
|
| 290 | + * @param array $relations |
|
| 291 | + * @param array $columns |
|
| 292 | + * @return object |
|
| 293 | + */ |
|
| 294 | + public function first($conditions, $relations = [], $columns = ['*']) |
|
| 295 | + { |
|
| 296 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 297 | + return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + /** |
|
| 301 | + * Return the deleted models in pages based on the given conditions. |
|
| 302 | + * |
|
| 303 | + * @param array $conditions array of conditions |
|
| 304 | + * @param integer $perPage |
|
| 305 | + * @param string $sortBy |
|
| 306 | + * @param boolean $desc |
|
| 307 | + * @param array $columns |
|
| 308 | + * @return collection |
|
| 309 | + */ |
|
| 310 | + public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 311 | + { |
|
| 312 | + unset($conditions['page']); |
|
| 313 | + unset($conditions['perPage']); |
|
| 314 | + unset($conditions['sortBy']); |
|
| 315 | + unset($conditions['sort']); |
|
| 316 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 317 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 318 | + $model = $this->model->onlyTrashed(); |
|
| 319 | + |
|
| 320 | + if (count($conditions['conditionValues'])) { |
|
| 321 | + $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + /** |
|
| 328 | + * Restore the deleted model. |
|
| 329 | + * |
|
| 330 | + * @param integer $id |
|
| 331 | + * @return void |
|
| 332 | + */ |
|
| 333 | + public function restore($id) |
|
| 334 | + { |
|
| 335 | + $model = $this->model->onlyTrashed()->find($id); |
|
| 336 | + |
|
| 337 | + if (! $model) { |
|
| 338 | + \ErrorHandler::notFound(class_basename($this->model).' with id : '.$id); |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + $model->restore(); |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + /** |
|
| 345 | + * Fill the model with the given data. |
|
| 346 | + * |
|
| 347 | + * @param array $data |
|
| 348 | + * |
|
| 349 | + * @return object |
|
| 350 | + */ |
|
| 351 | + public function prepareModel($data) |
|
| 352 | + { |
|
| 353 | + $modelClass = $this->model; |
|
| 354 | + |
|
| 355 | + /** |
|
| 356 | + * If the id is present in the data then select the model for updating, |
|
| 357 | + * else create new model. |
|
| 358 | + * @var array |
|
| 359 | + */ |
|
| 360 | + $model = Arr::has($data, 'id') ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
| 361 | + if (! $model) { |
|
| 362 | + \ErrorHandler::notFound(class_basename($modelClass).' with id : '.$data['id']); |
|
| 363 | + } |
|
| 364 | + |
|
| 365 | + /** |
|
| 366 | + * Construct the model object with the given data, |
|
| 367 | + * and if there is a relation add it to relations array, |
|
| 368 | + * then save the model. |
|
| 369 | + */ |
|
| 370 | + foreach ($data as $key => $value) { |
|
| 371 | + if (array_search($key, $model->getFillable(), true) !== false) { |
|
| 372 | + /** |
|
| 373 | + * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
| 374 | + */ |
|
| 375 | + $model->$key = $value; |
|
| 376 | + } |
|
| 377 | + } |
|
| 378 | + |
|
| 379 | + return $model; |
|
| 380 | + } |
|
| 381 | 381 | |
| 382 | - /** |
|
| 383 | - * Prepare related models based on the given data for the given model. |
|
| 384 | - * |
|
| 385 | - * @param array $data |
|
| 386 | - * @param object $model |
|
| 387 | - * |
|
| 388 | - * @return array |
|
| 389 | - */ |
|
| 390 | - public function prepareRelations($data, $model) |
|
| 391 | - { |
|
| 392 | - /** |
|
| 393 | - * Construct the model object with the given data, |
|
| 394 | - * and if there is a relation add it to relations array, |
|
| 395 | - * then save the model. |
|
| 396 | - */ |
|
| 397 | - foreach ($data as $key => $value) { |
|
| 398 | - /** |
|
| 399 | - * If the attribute is a relation. |
|
| 400 | - */ |
|
| 401 | - $relation = \Str::camel($key); |
|
| 402 | - if (method_exists($model, $relation) && \Core::$relation()) { |
|
| 403 | - /** |
|
| 404 | - * Check if the relation is a collection. |
|
| 405 | - */ |
|
| 406 | - if (class_basename($model->$relation) == 'Collection') { |
|
| 407 | - /** |
|
| 408 | - * If the relation has no value then marke the relation data |
|
| 409 | - * related to the model to be deleted. |
|
| 410 | - */ |
|
| 411 | - if (! $value || ! count($value)) { |
|
| 412 | - $relations[$relation] = 'delete'; |
|
| 413 | - } |
|
| 414 | - } |
|
| 415 | - if (is_array($value)) { |
|
| 416 | - /** |
|
| 417 | - * Loop through the relation data. |
|
| 418 | - */ |
|
| 419 | - foreach ($value as $attr => $val) { |
|
| 420 | - /** |
|
| 421 | - * Get the relation model. |
|
| 422 | - */ |
|
| 423 | - $relationBaseModel = \Core::$relation()->model; |
|
| 424 | - |
|
| 425 | - /** |
|
| 426 | - * Check if the relation is a collection. |
|
| 427 | - */ |
|
| 428 | - if (class_basename($model->$relation) == 'Collection') { |
|
| 429 | - /** |
|
| 430 | - * If the id is present in the data then select the relation model for updating, |
|
| 431 | - * else create new model. |
|
| 432 | - */ |
|
| 433 | - $relationModel = Arr::has($val, 'id') ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
| 434 | - |
|
| 435 | - /** |
|
| 436 | - * If model doesn't exists. |
|
| 437 | - */ |
|
| 438 | - if (! $relationModel) { |
|
| 439 | - \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
|
| 440 | - } |
|
| 441 | - |
|
| 442 | - /** |
|
| 443 | - * Loop through the relation attributes. |
|
| 444 | - */ |
|
| 445 | - foreach ($val as $attr => $val) { |
|
| 446 | - /** |
|
| 447 | - * Prevent the sub relations or attributes not in the fillable. |
|
| 448 | - */ |
|
| 449 | - if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) { |
|
| 450 | - $relationModel->$attr = $val; |
|
| 451 | - } |
|
| 452 | - } |
|
| 453 | - |
|
| 454 | - $relations[$relation][] = $relationModel; |
|
| 455 | - } else { |
|
| 456 | - /** |
|
| 457 | - * Prevent the sub relations. |
|
| 458 | - */ |
|
| 459 | - if (gettype($val) !== 'object' && gettype($val) !== 'array') { |
|
| 460 | - /** |
|
| 461 | - * If the id is present in the data then select the relation model for updating, |
|
| 462 | - * else create new model. |
|
| 463 | - */ |
|
| 464 | - $relationModel = Arr::has($value, 'id') ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
| 465 | - |
|
| 466 | - /** |
|
| 467 | - * If model doesn't exists. |
|
| 468 | - */ |
|
| 469 | - if (! $relationModel) { |
|
| 470 | - \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
|
| 471 | - } |
|
| 472 | - |
|
| 473 | - foreach ($value as $relationAttribute => $relationValue) { |
|
| 474 | - /** |
|
| 475 | - * Prevent attributes not in the fillable. |
|
| 476 | - */ |
|
| 477 | - if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) { |
|
| 478 | - $relationModel->$relationAttribute = $relationValue; |
|
| 479 | - } |
|
| 480 | - } |
|
| 481 | - |
|
| 482 | - $relations[$relation] = $relationModel; |
|
| 483 | - } |
|
| 484 | - } |
|
| 485 | - } |
|
| 486 | - } |
|
| 487 | - } |
|
| 488 | - } |
|
| 489 | - |
|
| 490 | - return $relations; |
|
| 491 | - } |
|
| 492 | - |
|
| 493 | - /** |
|
| 494 | - * Save the model with related models. |
|
| 495 | - * |
|
| 496 | - * @param object $model |
|
| 497 | - * @param array $relations |
|
| 498 | - * |
|
| 499 | - * @return object |
|
| 500 | - */ |
|
| 501 | - public function saveModel($model, $relations) |
|
| 502 | - { |
|
| 503 | - |
|
| 504 | - /** |
|
| 505 | - * Loop through the relations array. |
|
| 506 | - */ |
|
| 507 | - foreach ($relations as $key => $value) { |
|
| 508 | - /** |
|
| 509 | - * If the relation is marked for delete then delete it. |
|
| 510 | - */ |
|
| 511 | - if ($value == 'delete' && $model->$key()->count()) { |
|
| 512 | - $model->$key()->delete(); |
|
| 513 | - } elseif (gettype($value) == 'array') { |
|
| 514 | - /** |
|
| 515 | - * Save the model. |
|
| 516 | - */ |
|
| 517 | - $model->save(); |
|
| 518 | - $ids = []; |
|
| 519 | - |
|
| 520 | - /** |
|
| 521 | - * Loop through the relations. |
|
| 522 | - */ |
|
| 523 | - foreach ($value as $val) { |
|
| 524 | - switch (class_basename($model->$key())) { |
|
| 525 | - /** |
|
| 526 | - * If the relation is one to many then update it's foreign key with |
|
| 527 | - * the model id and save it then add its id to ids array to delete all |
|
| 528 | - * relations who's id isn't in the ids array. |
|
| 529 | - */ |
|
| 530 | - case 'HasMany': |
|
| 531 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
| 532 | - $val->$foreignKeyName = $model->id; |
|
| 533 | - $val->save(); |
|
| 534 | - $ids[] = $val->id; |
|
| 535 | - break; |
|
| 536 | - |
|
| 537 | - /** |
|
| 538 | - * If the relation is many to many then add it's id to the ids array to |
|
| 539 | - * attache these ids to the model. |
|
| 540 | - */ |
|
| 541 | - case 'BelongsToMany': |
|
| 542 | - $val->save(); |
|
| 543 | - $ids[] = $val->id; |
|
| 544 | - break; |
|
| 545 | - } |
|
| 546 | - } |
|
| 547 | - switch (class_basename($model->$key())) { |
|
| 548 | - /** |
|
| 549 | - * If the relation is one to many then delete all |
|
| 550 | - * relations who's id isn't in the ids array. |
|
| 551 | - */ |
|
| 552 | - case 'HasMany': |
|
| 553 | - $model->$key()->whereNotIn('id', $ids)->delete(); |
|
| 554 | - break; |
|
| 555 | - |
|
| 556 | - /** |
|
| 557 | - * If the relation is many to many then |
|
| 558 | - * detach the previous data and attach |
|
| 559 | - * the ids array to the model. |
|
| 560 | - */ |
|
| 561 | - case 'BelongsToMany': |
|
| 562 | - $model->$key()->detach(); |
|
| 563 | - $model->$key()->attach($ids); |
|
| 564 | - break; |
|
| 565 | - } |
|
| 566 | - } else { |
|
| 567 | - switch (class_basename($model->$key())) { |
|
| 568 | - /** |
|
| 569 | - * If the relation is one to one. |
|
| 570 | - */ |
|
| 571 | - case 'HasOne': |
|
| 572 | - /** |
|
| 573 | - * Save the model. |
|
| 574 | - */ |
|
| 575 | - $model->save(); |
|
| 576 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
| 577 | - $value->$foreignKeyName = $model->id; |
|
| 578 | - $value->save(); |
|
| 579 | - break; |
|
| 580 | - case 'BelongsTo': |
|
| 581 | - /** |
|
| 582 | - * Save the model. |
|
| 583 | - */ |
|
| 584 | - $value->save(); |
|
| 585 | - $model->$key()->associate($value); |
|
| 586 | - break; |
|
| 587 | - } |
|
| 588 | - } |
|
| 589 | - } |
|
| 590 | - |
|
| 591 | - /** |
|
| 592 | - * Save the model. |
|
| 593 | - */ |
|
| 594 | - $model->save(); |
|
| 595 | - |
|
| 596 | - return $model; |
|
| 597 | - } |
|
| 598 | - |
|
| 599 | - /** |
|
| 600 | - * Build the conditions recursively for the retrieving methods. |
|
| 601 | - * @param array $conditions |
|
| 602 | - * @return array |
|
| 603 | - */ |
|
| 604 | - protected function constructConditions($conditions, $model) |
|
| 605 | - { |
|
| 606 | - $conditionString = ''; |
|
| 607 | - $conditionValues = []; |
|
| 608 | - foreach ($conditions as $key => $value) { |
|
| 609 | - if (Str::contains($key, '->')) { |
|
| 610 | - $key = $this->wrapJsonSelector($key); |
|
| 611 | - } |
|
| 612 | - |
|
| 613 | - if ($key == 'and') { |
|
| 614 | - $conditions = $this->constructConditions($value, $model); |
|
| 615 | - $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']).' {op} '; |
|
| 616 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 617 | - } elseif ($key == 'or') { |
|
| 618 | - $conditions = $this->constructConditions($value, $model); |
|
| 619 | - $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']).' {op} '; |
|
| 620 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 621 | - } else { |
|
| 622 | - if (is_array($value)) { |
|
| 623 | - $operator = $value['op']; |
|
| 624 | - if (strtolower($operator) == 'between') { |
|
| 625 | - $value1 = $value['val1']; |
|
| 626 | - $value2 = $value['val2']; |
|
| 627 | - } else { |
|
| 628 | - $value = Arr::get($value, 'val', ''); |
|
| 629 | - } |
|
| 630 | - } else { |
|
| 631 | - $operator = '='; |
|
| 632 | - } |
|
| 382 | + /** |
|
| 383 | + * Prepare related models based on the given data for the given model. |
|
| 384 | + * |
|
| 385 | + * @param array $data |
|
| 386 | + * @param object $model |
|
| 387 | + * |
|
| 388 | + * @return array |
|
| 389 | + */ |
|
| 390 | + public function prepareRelations($data, $model) |
|
| 391 | + { |
|
| 392 | + /** |
|
| 393 | + * Construct the model object with the given data, |
|
| 394 | + * and if there is a relation add it to relations array, |
|
| 395 | + * then save the model. |
|
| 396 | + */ |
|
| 397 | + foreach ($data as $key => $value) { |
|
| 398 | + /** |
|
| 399 | + * If the attribute is a relation. |
|
| 400 | + */ |
|
| 401 | + $relation = \Str::camel($key); |
|
| 402 | + if (method_exists($model, $relation) && \Core::$relation()) { |
|
| 403 | + /** |
|
| 404 | + * Check if the relation is a collection. |
|
| 405 | + */ |
|
| 406 | + if (class_basename($model->$relation) == 'Collection') { |
|
| 407 | + /** |
|
| 408 | + * If the relation has no value then marke the relation data |
|
| 409 | + * related to the model to be deleted. |
|
| 410 | + */ |
|
| 411 | + if (! $value || ! count($value)) { |
|
| 412 | + $relations[$relation] = 'delete'; |
|
| 413 | + } |
|
| 414 | + } |
|
| 415 | + if (is_array($value)) { |
|
| 416 | + /** |
|
| 417 | + * Loop through the relation data. |
|
| 418 | + */ |
|
| 419 | + foreach ($value as $attr => $val) { |
|
| 420 | + /** |
|
| 421 | + * Get the relation model. |
|
| 422 | + */ |
|
| 423 | + $relationBaseModel = \Core::$relation()->model; |
|
| 424 | + |
|
| 425 | + /** |
|
| 426 | + * Check if the relation is a collection. |
|
| 427 | + */ |
|
| 428 | + if (class_basename($model->$relation) == 'Collection') { |
|
| 429 | + /** |
|
| 430 | + * If the id is present in the data then select the relation model for updating, |
|
| 431 | + * else create new model. |
|
| 432 | + */ |
|
| 433 | + $relationModel = Arr::has($val, 'id') ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
| 434 | + |
|
| 435 | + /** |
|
| 436 | + * If model doesn't exists. |
|
| 437 | + */ |
|
| 438 | + if (! $relationModel) { |
|
| 439 | + \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
|
| 440 | + } |
|
| 441 | + |
|
| 442 | + /** |
|
| 443 | + * Loop through the relation attributes. |
|
| 444 | + */ |
|
| 445 | + foreach ($val as $attr => $val) { |
|
| 446 | + /** |
|
| 447 | + * Prevent the sub relations or attributes not in the fillable. |
|
| 448 | + */ |
|
| 449 | + if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) { |
|
| 450 | + $relationModel->$attr = $val; |
|
| 451 | + } |
|
| 452 | + } |
|
| 453 | + |
|
| 454 | + $relations[$relation][] = $relationModel; |
|
| 455 | + } else { |
|
| 456 | + /** |
|
| 457 | + * Prevent the sub relations. |
|
| 458 | + */ |
|
| 459 | + if (gettype($val) !== 'object' && gettype($val) !== 'array') { |
|
| 460 | + /** |
|
| 461 | + * If the id is present in the data then select the relation model for updating, |
|
| 462 | + * else create new model. |
|
| 463 | + */ |
|
| 464 | + $relationModel = Arr::has($value, 'id') ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
| 465 | + |
|
| 466 | + /** |
|
| 467 | + * If model doesn't exists. |
|
| 468 | + */ |
|
| 469 | + if (! $relationModel) { |
|
| 470 | + \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
|
| 471 | + } |
|
| 472 | + |
|
| 473 | + foreach ($value as $relationAttribute => $relationValue) { |
|
| 474 | + /** |
|
| 475 | + * Prevent attributes not in the fillable. |
|
| 476 | + */ |
|
| 477 | + if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) { |
|
| 478 | + $relationModel->$relationAttribute = $relationValue; |
|
| 479 | + } |
|
| 480 | + } |
|
| 481 | + |
|
| 482 | + $relations[$relation] = $relationModel; |
|
| 483 | + } |
|
| 484 | + } |
|
| 485 | + } |
|
| 486 | + } |
|
| 487 | + } |
|
| 488 | + } |
|
| 489 | + |
|
| 490 | + return $relations; |
|
| 491 | + } |
|
| 492 | + |
|
| 493 | + /** |
|
| 494 | + * Save the model with related models. |
|
| 495 | + * |
|
| 496 | + * @param object $model |
|
| 497 | + * @param array $relations |
|
| 498 | + * |
|
| 499 | + * @return object |
|
| 500 | + */ |
|
| 501 | + public function saveModel($model, $relations) |
|
| 502 | + { |
|
| 503 | + |
|
| 504 | + /** |
|
| 505 | + * Loop through the relations array. |
|
| 506 | + */ |
|
| 507 | + foreach ($relations as $key => $value) { |
|
| 508 | + /** |
|
| 509 | + * If the relation is marked for delete then delete it. |
|
| 510 | + */ |
|
| 511 | + if ($value == 'delete' && $model->$key()->count()) { |
|
| 512 | + $model->$key()->delete(); |
|
| 513 | + } elseif (gettype($value) == 'array') { |
|
| 514 | + /** |
|
| 515 | + * Save the model. |
|
| 516 | + */ |
|
| 517 | + $model->save(); |
|
| 518 | + $ids = []; |
|
| 519 | + |
|
| 520 | + /** |
|
| 521 | + * Loop through the relations. |
|
| 522 | + */ |
|
| 523 | + foreach ($value as $val) { |
|
| 524 | + switch (class_basename($model->$key())) { |
|
| 525 | + /** |
|
| 526 | + * If the relation is one to many then update it's foreign key with |
|
| 527 | + * the model id and save it then add its id to ids array to delete all |
|
| 528 | + * relations who's id isn't in the ids array. |
|
| 529 | + */ |
|
| 530 | + case 'HasMany': |
|
| 531 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
| 532 | + $val->$foreignKeyName = $model->id; |
|
| 533 | + $val->save(); |
|
| 534 | + $ids[] = $val->id; |
|
| 535 | + break; |
|
| 536 | + |
|
| 537 | + /** |
|
| 538 | + * If the relation is many to many then add it's id to the ids array to |
|
| 539 | + * attache these ids to the model. |
|
| 540 | + */ |
|
| 541 | + case 'BelongsToMany': |
|
| 542 | + $val->save(); |
|
| 543 | + $ids[] = $val->id; |
|
| 544 | + break; |
|
| 545 | + } |
|
| 546 | + } |
|
| 547 | + switch (class_basename($model->$key())) { |
|
| 548 | + /** |
|
| 549 | + * If the relation is one to many then delete all |
|
| 550 | + * relations who's id isn't in the ids array. |
|
| 551 | + */ |
|
| 552 | + case 'HasMany': |
|
| 553 | + $model->$key()->whereNotIn('id', $ids)->delete(); |
|
| 554 | + break; |
|
| 555 | + |
|
| 556 | + /** |
|
| 557 | + * If the relation is many to many then |
|
| 558 | + * detach the previous data and attach |
|
| 559 | + * the ids array to the model. |
|
| 560 | + */ |
|
| 561 | + case 'BelongsToMany': |
|
| 562 | + $model->$key()->detach(); |
|
| 563 | + $model->$key()->attach($ids); |
|
| 564 | + break; |
|
| 565 | + } |
|
| 566 | + } else { |
|
| 567 | + switch (class_basename($model->$key())) { |
|
| 568 | + /** |
|
| 569 | + * If the relation is one to one. |
|
| 570 | + */ |
|
| 571 | + case 'HasOne': |
|
| 572 | + /** |
|
| 573 | + * Save the model. |
|
| 574 | + */ |
|
| 575 | + $model->save(); |
|
| 576 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
| 577 | + $value->$foreignKeyName = $model->id; |
|
| 578 | + $value->save(); |
|
| 579 | + break; |
|
| 580 | + case 'BelongsTo': |
|
| 581 | + /** |
|
| 582 | + * Save the model. |
|
| 583 | + */ |
|
| 584 | + $value->save(); |
|
| 585 | + $model->$key()->associate($value); |
|
| 586 | + break; |
|
| 587 | + } |
|
| 588 | + } |
|
| 589 | + } |
|
| 590 | + |
|
| 591 | + /** |
|
| 592 | + * Save the model. |
|
| 593 | + */ |
|
| 594 | + $model->save(); |
|
| 595 | + |
|
| 596 | + return $model; |
|
| 597 | + } |
|
| 598 | + |
|
| 599 | + /** |
|
| 600 | + * Build the conditions recursively for the retrieving methods. |
|
| 601 | + * @param array $conditions |
|
| 602 | + * @return array |
|
| 603 | + */ |
|
| 604 | + protected function constructConditions($conditions, $model) |
|
| 605 | + { |
|
| 606 | + $conditionString = ''; |
|
| 607 | + $conditionValues = []; |
|
| 608 | + foreach ($conditions as $key => $value) { |
|
| 609 | + if (Str::contains($key, '->')) { |
|
| 610 | + $key = $this->wrapJsonSelector($key); |
|
| 611 | + } |
|
| 612 | + |
|
| 613 | + if ($key == 'and') { |
|
| 614 | + $conditions = $this->constructConditions($value, $model); |
|
| 615 | + $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']).' {op} '; |
|
| 616 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 617 | + } elseif ($key == 'or') { |
|
| 618 | + $conditions = $this->constructConditions($value, $model); |
|
| 619 | + $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']).' {op} '; |
|
| 620 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 621 | + } else { |
|
| 622 | + if (is_array($value)) { |
|
| 623 | + $operator = $value['op']; |
|
| 624 | + if (strtolower($operator) == 'between') { |
|
| 625 | + $value1 = $value['val1']; |
|
| 626 | + $value2 = $value['val2']; |
|
| 627 | + } else { |
|
| 628 | + $value = Arr::get($value, 'val', ''); |
|
| 629 | + } |
|
| 630 | + } else { |
|
| 631 | + $operator = '='; |
|
| 632 | + } |
|
| 633 | 633 | |
| 634 | - if (strtolower($operator) == 'between') { |
|
| 635 | - $conditionString .= $key.' >= ? and '; |
|
| 636 | - $conditionValues[] = $value1; |
|
| 637 | - |
|
| 638 | - $conditionString .= $key.' <= ? {op} '; |
|
| 639 | - $conditionValues[] = $value2; |
|
| 640 | - } elseif (strtolower($operator) == 'in') { |
|
| 641 | - $conditionValues = array_merge($conditionValues, $value); |
|
| 642 | - $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
| 643 | - $conditionString .= $key.' in ('.rtrim($inBindingsString, ',').') {op} '; |
|
| 644 | - } elseif (strtolower($operator) == 'null') { |
|
| 645 | - $conditionString .= $key.' is null {op} '; |
|
| 646 | - } elseif (strtolower($operator) == 'not null') { |
|
| 647 | - $conditionString .= $key.' is not null {op} '; |
|
| 648 | - } elseif (strtolower($operator) == 'has') { |
|
| 649 | - $sql = $model->withTrashed()->has($key)->toSql(); |
|
| 650 | - $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
| 651 | - $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')').' and '.$conditions['conditionString'].') {op} '; |
|
| 652 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 653 | - } else { |
|
| 654 | - $conditionString .= $key.' '.$operator.' ? {op} '; |
|
| 655 | - $conditionValues[] = $value; |
|
| 656 | - } |
|
| 657 | - } |
|
| 658 | - } |
|
| 659 | - $conditionString = '('.rtrim($conditionString, '{op} ').')'; |
|
| 660 | - return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
| 661 | - } |
|
| 662 | - |
|
| 663 | - /** |
|
| 664 | - * Wrap the given JSON selector. |
|
| 665 | - * |
|
| 666 | - * @param string $value |
|
| 667 | - * @return string |
|
| 668 | - */ |
|
| 669 | - protected function wrapJsonSelector($value) |
|
| 670 | - { |
|
| 671 | - $removeLast = strpos($value, ')'); |
|
| 672 | - $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
|
| 673 | - $path = explode('->', $value); |
|
| 674 | - $field = array_shift($path); |
|
| 675 | - $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
| 676 | - return '"'.$part.'"'; |
|
| 677 | - })->implode('.')); |
|
| 634 | + if (strtolower($operator) == 'between') { |
|
| 635 | + $conditionString .= $key.' >= ? and '; |
|
| 636 | + $conditionValues[] = $value1; |
|
| 637 | + |
|
| 638 | + $conditionString .= $key.' <= ? {op} '; |
|
| 639 | + $conditionValues[] = $value2; |
|
| 640 | + } elseif (strtolower($operator) == 'in') { |
|
| 641 | + $conditionValues = array_merge($conditionValues, $value); |
|
| 642 | + $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
| 643 | + $conditionString .= $key.' in ('.rtrim($inBindingsString, ',').') {op} '; |
|
| 644 | + } elseif (strtolower($operator) == 'null') { |
|
| 645 | + $conditionString .= $key.' is null {op} '; |
|
| 646 | + } elseif (strtolower($operator) == 'not null') { |
|
| 647 | + $conditionString .= $key.' is not null {op} '; |
|
| 648 | + } elseif (strtolower($operator) == 'has') { |
|
| 649 | + $sql = $model->withTrashed()->has($key)->toSql(); |
|
| 650 | + $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
| 651 | + $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')').' and '.$conditions['conditionString'].') {op} '; |
|
| 652 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 653 | + } else { |
|
| 654 | + $conditionString .= $key.' '.$operator.' ? {op} '; |
|
| 655 | + $conditionValues[] = $value; |
|
| 656 | + } |
|
| 657 | + } |
|
| 658 | + } |
|
| 659 | + $conditionString = '('.rtrim($conditionString, '{op} ').')'; |
|
| 660 | + return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
| 661 | + } |
|
| 662 | + |
|
| 663 | + /** |
|
| 664 | + * Wrap the given JSON selector. |
|
| 665 | + * |
|
| 666 | + * @param string $value |
|
| 667 | + * @return string |
|
| 668 | + */ |
|
| 669 | + protected function wrapJsonSelector($value) |
|
| 670 | + { |
|
| 671 | + $removeLast = strpos($value, ')'); |
|
| 672 | + $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
|
| 673 | + $path = explode('->', $value); |
|
| 674 | + $field = array_shift($path); |
|
| 675 | + $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
| 676 | + return '"'.$part.'"'; |
|
| 677 | + })->implode('.')); |
|
| 678 | 678 | |
| 679 | - return $removeLast === false ? $result : $result.')'; |
|
| 680 | - } |
|
| 679 | + return $removeLast === false ? $result : $result.')'; |
|
| 680 | + } |
|
| 681 | 681 | } |
@@ -12,73 +12,73 @@ |
||
| 12 | 12 | |
| 13 | 13 | class CheckPermissions |
| 14 | 14 | { |
| 15 | - protected $route; |
|
| 16 | - protected $auth; |
|
| 17 | - protected $errorHandler; |
|
| 18 | - protected $authMiddleware; |
|
| 19 | - protected $userService; |
|
| 20 | - protected $arr; |
|
| 15 | + protected $route; |
|
| 16 | + protected $auth; |
|
| 17 | + protected $errorHandler; |
|
| 18 | + protected $authMiddleware; |
|
| 19 | + protected $userService; |
|
| 20 | + protected $arr; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Init new object. |
|
| 24 | - * |
|
| 25 | - * @param Route $route |
|
| 26 | - * @param Auth $auth |
|
| 27 | - * @param ErrorHandler $errorHandler |
|
| 28 | - * @param AuthMiddleware $authMiddleware |
|
| 29 | - * @param UserService $userService |
|
| 30 | - * @param Arr $arr |
|
| 31 | - * |
|
| 32 | - * @return void |
|
| 33 | - */ |
|
| 34 | - public function __construct(Route $route, Auth $auth, ErrorHandler $errorHandler, AuthMiddleware $authMiddleware, UserService $userService, Arr $arr) |
|
| 35 | - { |
|
| 36 | - $this->route = $route; |
|
| 37 | - $this->auth = $auth; |
|
| 38 | - $this->errorHandler = $errorHandler; |
|
| 39 | - $this->authMiddleware = $authMiddleware; |
|
| 40 | - $this->userService = $userService; |
|
| 41 | - $this->arr = $arr; |
|
| 42 | - } |
|
| 22 | + /** |
|
| 23 | + * Init new object. |
|
| 24 | + * |
|
| 25 | + * @param Route $route |
|
| 26 | + * @param Auth $auth |
|
| 27 | + * @param ErrorHandler $errorHandler |
|
| 28 | + * @param AuthMiddleware $authMiddleware |
|
| 29 | + * @param UserService $userService |
|
| 30 | + * @param Arr $arr |
|
| 31 | + * |
|
| 32 | + * @return void |
|
| 33 | + */ |
|
| 34 | + public function __construct(Route $route, Auth $auth, ErrorHandler $errorHandler, AuthMiddleware $authMiddleware, UserService $userService, Arr $arr) |
|
| 35 | + { |
|
| 36 | + $this->route = $route; |
|
| 37 | + $this->auth = $auth; |
|
| 38 | + $this->errorHandler = $errorHandler; |
|
| 39 | + $this->authMiddleware = $authMiddleware; |
|
| 40 | + $this->userService = $userService; |
|
| 41 | + $this->arr = $arr; |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * Handle an incoming request. |
|
| 46 | - * |
|
| 47 | - * @param \Illuminate\Http\Request $request |
|
| 48 | - * @param \Closure $next |
|
| 49 | - * @return mixed |
|
| 50 | - */ |
|
| 51 | - public function handle($request, Closure $next) |
|
| 52 | - { |
|
| 53 | - $routeActions = explode('@', $this->route->currentRouteAction()); |
|
| 54 | - $reflectionClass = new \ReflectionClass($routeActions[0]); |
|
| 55 | - $classProperties = $reflectionClass->getDefaultProperties(); |
|
| 56 | - $skipPermissionCheck = $this->arr->get($classProperties, 'skipPermissionCheck', []); |
|
| 57 | - $skipLoginCheck = $this->arr->get($classProperties, 'skipLoginCheck', []); |
|
| 58 | - $modelName = explode('\\', $routeActions[0]); |
|
| 59 | - $modelName = lcfirst(str_replace('Controller', '', end($modelName))); |
|
| 60 | - $permission = $routeActions[1]; |
|
| 44 | + /** |
|
| 45 | + * Handle an incoming request. |
|
| 46 | + * |
|
| 47 | + * @param \Illuminate\Http\Request $request |
|
| 48 | + * @param \Closure $next |
|
| 49 | + * @return mixed |
|
| 50 | + */ |
|
| 51 | + public function handle($request, Closure $next) |
|
| 52 | + { |
|
| 53 | + $routeActions = explode('@', $this->route->currentRouteAction()); |
|
| 54 | + $reflectionClass = new \ReflectionClass($routeActions[0]); |
|
| 55 | + $classProperties = $reflectionClass->getDefaultProperties(); |
|
| 56 | + $skipPermissionCheck = $this->arr->get($classProperties, 'skipPermissionCheck', []); |
|
| 57 | + $skipLoginCheck = $this->arr->get($classProperties, 'skipLoginCheck', []); |
|
| 58 | + $modelName = explode('\\', $routeActions[0]); |
|
| 59 | + $modelName = lcfirst(str_replace('Controller', '', end($modelName))); |
|
| 60 | + $permission = $routeActions[1]; |
|
| 61 | 61 | |
| 62 | - $this->auth->shouldUse('api'); |
|
| 63 | - if (! in_array($permission, $skipLoginCheck)) { |
|
| 64 | - $this->authMiddleware->handle($request, function ($request) use ($modelName, $skipPermissionCheck, $skipLoginCheck, $permission) { |
|
| 65 | - $user = $this->auth->user(); |
|
| 66 | - $isPasswordClient = $user->token()->client->password_client; |
|
| 62 | + $this->auth->shouldUse('api'); |
|
| 63 | + if (! in_array($permission, $skipLoginCheck)) { |
|
| 64 | + $this->authMiddleware->handle($request, function ($request) use ($modelName, $skipPermissionCheck, $skipLoginCheck, $permission) { |
|
| 65 | + $user = $this->auth->user(); |
|
| 66 | + $isPasswordClient = $user->token()->client->password_client; |
|
| 67 | 67 | |
| 68 | - if ($user->blocked) { |
|
| 69 | - $this->errorHandler->userIsBlocked(); |
|
| 70 | - } |
|
| 68 | + if ($user->blocked) { |
|
| 69 | + $this->errorHandler->userIsBlocked(); |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - if ($isPasswordClient && (in_array($permission, $skipPermissionCheck) || $this->userService->can($permission, $modelName))) { |
|
| 73 | - } elseif (! $isPasswordClient && $user->tokenCan($modelName.'-'.$permission)) { |
|
| 74 | - } else { |
|
| 75 | - $this->errorHandler->noPermissions(); |
|
| 76 | - } |
|
| 77 | - }); |
|
| 78 | - } |
|
| 72 | + if ($isPasswordClient && (in_array($permission, $skipPermissionCheck) || $this->userService->can($permission, $modelName))) { |
|
| 73 | + } elseif (! $isPasswordClient && $user->tokenCan($modelName.'-'.$permission)) { |
|
| 74 | + } else { |
|
| 75 | + $this->errorHandler->noPermissions(); |
|
| 76 | + } |
|
| 77 | + }); |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - return $next($request); |
|
| 80 | + return $next($request); |
|
| 81 | 81 | |
| 82 | - //$this->middleware('auth:api', ['except' => $skipLoginCheck]); |
|
| 83 | - } |
|
| 82 | + //$this->middleware('auth:api', ['except' => $skipLoginCheck]); |
|
| 83 | + } |
|
| 84 | 84 | } |
@@ -60,8 +60,8 @@ discard block |
||
| 60 | 60 | $permission = $routeActions[1]; |
| 61 | 61 | |
| 62 | 62 | $this->auth->shouldUse('api'); |
| 63 | - if (! in_array($permission, $skipLoginCheck)) { |
|
| 64 | - $this->authMiddleware->handle($request, function ($request) use ($modelName, $skipPermissionCheck, $skipLoginCheck, $permission) { |
|
| 63 | + if ( ! in_array($permission, $skipLoginCheck)) { |
|
| 64 | + $this->authMiddleware->handle($request, function($request) use ($modelName, $skipPermissionCheck, $skipLoginCheck, $permission) { |
|
| 65 | 65 | $user = $this->auth->user(); |
| 66 | 66 | $isPasswordClient = $user->token()->client->password_client; |
| 67 | 67 | |
@@ -70,7 +70,7 @@ discard block |
||
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | if ($isPasswordClient && (in_array($permission, $skipPermissionCheck) || $this->userService->can($permission, $modelName))) { |
| 73 | - } elseif (! $isPasswordClient && $user->tokenCan($modelName.'-'.$permission)) { |
|
| 73 | + } elseif ( ! $isPasswordClient && $user->tokenCan($modelName.'-'.$permission)) { |
|
| 74 | 74 | } else { |
| 75 | 75 | $this->errorHandler->noPermissions(); |
| 76 | 76 | } |
@@ -10,43 +10,43 @@ |
||
| 10 | 10 | |
| 11 | 11 | class SettingController extends BaseApiController |
| 12 | 12 | { |
| 13 | - /** |
|
| 14 | - * Path of the model resource |
|
| 15 | - * |
|
| 16 | - * @var string |
|
| 17 | - */ |
|
| 18 | - protected $modelResource = 'App\Modules\Core\Http\Resources\Setting'; |
|
| 13 | + /** |
|
| 14 | + * Path of the model resource |
|
| 15 | + * |
|
| 16 | + * @var string |
|
| 17 | + */ |
|
| 18 | + protected $modelResource = 'App\Modules\Core\Http\Resources\Setting'; |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * Init new object. |
|
| 22 | - * |
|
| 23 | - * @param SettingService $service |
|
| 24 | - * @return void |
|
| 25 | - */ |
|
| 26 | - public function __construct(SettingService $service) |
|
| 27 | - { |
|
| 28 | - parent::__construct($service); |
|
| 29 | - } |
|
| 20 | + /** |
|
| 21 | + * Init new object. |
|
| 22 | + * |
|
| 23 | + * @param SettingService $service |
|
| 24 | + * @return void |
|
| 25 | + */ |
|
| 26 | + public function __construct(SettingService $service) |
|
| 27 | + { |
|
| 28 | + parent::__construct($service); |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Update the given model to storage. |
|
| 33 | - * |
|
| 34 | - * @param UpdateSetting $request |
|
| 35 | - * @return \Illuminate\Http\Response |
|
| 36 | - */ |
|
| 37 | - public function update(UpdateSetting $request) |
|
| 38 | - { |
|
| 39 | - return new $this->modelResource($this->service->save($request->all())); |
|
| 40 | - } |
|
| 31 | + /** |
|
| 32 | + * Update the given model to storage. |
|
| 33 | + * |
|
| 34 | + * @param UpdateSetting $request |
|
| 35 | + * @return \Illuminate\Http\Response |
|
| 36 | + */ |
|
| 37 | + public function update(UpdateSetting $request) |
|
| 38 | + { |
|
| 39 | + return new $this->modelResource($this->service->save($request->all())); |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * Save list of settings. |
|
| 44 | - * |
|
| 45 | - * @param Request $request |
|
| 46 | - * @return \Illuminate\Http\Response |
|
| 47 | - */ |
|
| 48 | - public function saveMany(Request $request) |
|
| 49 | - { |
|
| 50 | - return new GeneralResource($this->service->saveMany($request->all())); |
|
| 51 | - } |
|
| 42 | + /** |
|
| 43 | + * Save list of settings. |
|
| 44 | + * |
|
| 45 | + * @param Request $request |
|
| 46 | + * @return \Illuminate\Http\Response |
|
| 47 | + */ |
|
| 48 | + public function saveMany(Request $request) |
|
| 49 | + { |
|
| 50 | + return new GeneralResource($this->service->saveMany($request->all())); |
|
| 51 | + } |
|
| 52 | 52 | } |
@@ -6,96 +6,96 @@ |
||
| 6 | 6 | |
| 7 | 7 | class ApiDocumentController extends Controller |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * @var ReprotService |
|
| 11 | - */ |
|
| 12 | - protected $reportService; |
|
| 9 | + /** |
|
| 10 | + * @var ReprotService |
|
| 11 | + */ |
|
| 12 | + protected $reportService; |
|
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * Init new object. |
|
| 16 | - * |
|
| 17 | - * @return void |
|
| 18 | - */ |
|
| 19 | - public function __construct(ReportService $reportService) |
|
| 20 | - { |
|
| 21 | - $this->reportService = $reportService; |
|
| 22 | - } |
|
| 14 | + /** |
|
| 15 | + * Init new object. |
|
| 16 | + * |
|
| 17 | + * @return void |
|
| 18 | + */ |
|
| 19 | + public function __construct(ReportService $reportService) |
|
| 20 | + { |
|
| 21 | + $this->reportService = $reportService; |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - public function index() |
|
| 25 | - { |
|
| 26 | - $jsonDoc = json_decode(file_get_contents(app_path('Modules/Core/Resources/api.json')), true); |
|
| 27 | - $modules = $jsonDoc['modules']; |
|
| 28 | - $reports = $jsonDoc['reports']; |
|
| 29 | - $errors = $jsonDoc['errors']; |
|
| 30 | - $models = $jsonDoc['models']; |
|
| 31 | - $conditions = [ |
|
| 32 | - [ |
|
| 33 | - 'title' => 'email equal [email protected]:', |
|
| 34 | - 'content' => ['email' => '[email protected]'] |
|
| 35 | - ], |
|
| 36 | - [ |
|
| 37 | - 'title' => 'email equal [email protected] and user is blocked:', |
|
| 38 | - 'content' => ['and' => ['email' => '[email protected]', 'blocked' => 1]] |
|
| 39 | - ], |
|
| 40 | - [ |
|
| 41 | - 'title' => 'email equal [email protected] or user is blocked:', |
|
| 42 | - 'content' => ['or' => ['email' => '[email protected]', 'blocked' => 1]] |
|
| 43 | - ], |
|
| 44 | - [ |
|
| 45 | - 'title' => 'email contain John:', |
|
| 46 | - 'content' => ['email' => ['op' => 'like', 'val' => '%John%']] |
|
| 47 | - ], |
|
| 48 | - [ |
|
| 49 | - 'title' => 'user created after 2016-10-25:', |
|
| 50 | - 'content' => ['created_at' => ['op' => '>', 'val' => '2016-10-25']] |
|
| 51 | - ], |
|
| 52 | - [ |
|
| 53 | - 'title' => 'user created between 2016-10-20 and 2016-10-25:', |
|
| 54 | - 'content' => ['created_at' => ['op' => 'between', 'val1' => '2016-10-20', 'val2' => '2016-10-25']] |
|
| 55 | - ], |
|
| 56 | - [ |
|
| 57 | - 'title' => 'user id in 1,2,3:', |
|
| 58 | - 'content' => ['id' => ['op' => 'in', 'val' => [1, 2, 3]]] |
|
| 59 | - ], |
|
| 60 | - [ |
|
| 61 | - 'title' => 'user name is null:', |
|
| 62 | - 'content' => ['name' => ['op' => 'null']] |
|
| 63 | - ], |
|
| 64 | - [ |
|
| 65 | - 'title' => 'user name is not null:', |
|
| 66 | - 'content' => ['name' => ['op' => 'not null']] |
|
| 67 | - ], |
|
| 68 | - [ |
|
| 69 | - 'title' => 'user has role admin:', |
|
| 70 | - 'content' => ['roles' => ['op' => 'has', 'val' => ['name' => 'Admin']]] |
|
| 71 | - ] |
|
| 72 | - ]; |
|
| 24 | + public function index() |
|
| 25 | + { |
|
| 26 | + $jsonDoc = json_decode(file_get_contents(app_path('Modules/Core/Resources/api.json')), true); |
|
| 27 | + $modules = $jsonDoc['modules']; |
|
| 28 | + $reports = $jsonDoc['reports']; |
|
| 29 | + $errors = $jsonDoc['errors']; |
|
| 30 | + $models = $jsonDoc['models']; |
|
| 31 | + $conditions = [ |
|
| 32 | + [ |
|
| 33 | + 'title' => 'email equal [email protected]:', |
|
| 34 | + 'content' => ['email' => '[email protected]'] |
|
| 35 | + ], |
|
| 36 | + [ |
|
| 37 | + 'title' => 'email equal [email protected] and user is blocked:', |
|
| 38 | + 'content' => ['and' => ['email' => '[email protected]', 'blocked' => 1]] |
|
| 39 | + ], |
|
| 40 | + [ |
|
| 41 | + 'title' => 'email equal [email protected] or user is blocked:', |
|
| 42 | + 'content' => ['or' => ['email' => '[email protected]', 'blocked' => 1]] |
|
| 43 | + ], |
|
| 44 | + [ |
|
| 45 | + 'title' => 'email contain John:', |
|
| 46 | + 'content' => ['email' => ['op' => 'like', 'val' => '%John%']] |
|
| 47 | + ], |
|
| 48 | + [ |
|
| 49 | + 'title' => 'user created after 2016-10-25:', |
|
| 50 | + 'content' => ['created_at' => ['op' => '>', 'val' => '2016-10-25']] |
|
| 51 | + ], |
|
| 52 | + [ |
|
| 53 | + 'title' => 'user created between 2016-10-20 and 2016-10-25:', |
|
| 54 | + 'content' => ['created_at' => ['op' => 'between', 'val1' => '2016-10-20', 'val2' => '2016-10-25']] |
|
| 55 | + ], |
|
| 56 | + [ |
|
| 57 | + 'title' => 'user id in 1,2,3:', |
|
| 58 | + 'content' => ['id' => ['op' => 'in', 'val' => [1, 2, 3]]] |
|
| 59 | + ], |
|
| 60 | + [ |
|
| 61 | + 'title' => 'user name is null:', |
|
| 62 | + 'content' => ['name' => ['op' => 'null']] |
|
| 63 | + ], |
|
| 64 | + [ |
|
| 65 | + 'title' => 'user name is not null:', |
|
| 66 | + 'content' => ['name' => ['op' => 'not null']] |
|
| 67 | + ], |
|
| 68 | + [ |
|
| 69 | + 'title' => 'user has role admin:', |
|
| 70 | + 'content' => ['roles' => ['op' => 'has', 'val' => ['name' => 'Admin']]] |
|
| 71 | + ] |
|
| 72 | + ]; |
|
| 73 | 73 | |
| 74 | - $paginateObject = [ |
|
| 75 | - 'data' => ['Array of model objects'], |
|
| 76 | - "links" => [ |
|
| 77 | - "first" => "apiUrl?page=1", |
|
| 78 | - "last" => "apiUrl?page=3", |
|
| 79 | - "prev" => "apiUrl?page=2", |
|
| 80 | - "next" => "apiUrl?page=3" |
|
| 81 | - ], |
|
| 82 | - "meta" => [ |
|
| 83 | - "current_page" => 2, |
|
| 84 | - "from" => 6, |
|
| 85 | - "last_page" => 3, |
|
| 86 | - "path" => "apiUrl", |
|
| 87 | - "per_page" => 5, |
|
| 88 | - "to" => 10, |
|
| 89 | - "total" => 15 |
|
| 90 | - ] |
|
| 91 | - ]; |
|
| 74 | + $paginateObject = [ |
|
| 75 | + 'data' => ['Array of model objects'], |
|
| 76 | + "links" => [ |
|
| 77 | + "first" => "apiUrl?page=1", |
|
| 78 | + "last" => "apiUrl?page=3", |
|
| 79 | + "prev" => "apiUrl?page=2", |
|
| 80 | + "next" => "apiUrl?page=3" |
|
| 81 | + ], |
|
| 82 | + "meta" => [ |
|
| 83 | + "current_page" => 2, |
|
| 84 | + "from" => 6, |
|
| 85 | + "last_page" => 3, |
|
| 86 | + "path" => "apiUrl", |
|
| 87 | + "per_page" => 5, |
|
| 88 | + "to" => 10, |
|
| 89 | + "total" => 15 |
|
| 90 | + ] |
|
| 91 | + ]; |
|
| 92 | 92 | |
| 93 | - $responseObject = [ |
|
| 94 | - 'data' => ['The model object'] |
|
| 95 | - ]; |
|
| 93 | + $responseObject = [ |
|
| 94 | + 'data' => ['The model object'] |
|
| 95 | + ]; |
|
| 96 | 96 | |
| 97 | - $avaialableReports = $this->reportService->all(); |
|
| 97 | + $avaialableReports = $this->reportService->all(); |
|
| 98 | 98 | |
| 99 | - return view('core::doc', ['modules' => $modules, 'reports' => $reports, 'errors' => $errors, 'conditions' => $conditions, 'models' => $models, 'paginateObject' => json_encode($paginateObject, JSON_PRETTY_PRINT), 'responseObject' => json_encode($responseObject, JSON_PRETTY_PRINT), 'avaialableReports' => $avaialableReports]); |
|
| 100 | - } |
|
| 99 | + return view('core::doc', ['modules' => $modules, 'reports' => $reports, 'errors' => $errors, 'conditions' => $conditions, 'models' => $models, 'paginateObject' => json_encode($paginateObject, JSON_PRETTY_PRINT), 'responseObject' => json_encode($responseObject, JSON_PRETTY_PRINT), 'avaialableReports' => $avaialableReports]); |
|
| 100 | + } |
|
| 101 | 101 | } |
@@ -5,29 +5,29 @@ |
||
| 5 | 5 | |
| 6 | 6 | class SettingService extends BaseService |
| 7 | 7 | { |
| 8 | - /** |
|
| 9 | - * Init new object. |
|
| 10 | - * |
|
| 11 | - * @param SettingRepository $repo |
|
| 12 | - * @return void |
|
| 13 | - */ |
|
| 14 | - public function __construct(SettingRepository $repo) |
|
| 15 | - { |
|
| 16 | - parent::__construct($repo); |
|
| 17 | - } |
|
| 8 | + /** |
|
| 9 | + * Init new object. |
|
| 10 | + * |
|
| 11 | + * @param SettingRepository $repo |
|
| 12 | + * @return void |
|
| 13 | + */ |
|
| 14 | + public function __construct(SettingRepository $repo) |
|
| 15 | + { |
|
| 16 | + parent::__construct($repo); |
|
| 17 | + } |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Save list of settings. |
|
| 21 | - * |
|
| 22 | - * @param array $data |
|
| 23 | - * @return void |
|
| 24 | - */ |
|
| 25 | - public function saveMany(array $data) |
|
| 26 | - { |
|
| 27 | - \DB::transaction(function () use ($data) { |
|
| 28 | - foreach ($data as $value) { |
|
| 29 | - $this->repo->save($value); |
|
| 30 | - } |
|
| 31 | - }); |
|
| 32 | - } |
|
| 19 | + /** |
|
| 20 | + * Save list of settings. |
|
| 21 | + * |
|
| 22 | + * @param array $data |
|
| 23 | + * @return void |
|
| 24 | + */ |
|
| 25 | + public function saveMany(array $data) |
|
| 26 | + { |
|
| 27 | + \DB::transaction(function () use ($data) { |
|
| 28 | + foreach ($data as $value) { |
|
| 29 | + $this->repo->save($value); |
|
| 30 | + } |
|
| 31 | + }); |
|
| 32 | + } |
|
| 33 | 33 | } |
@@ -24,7 +24,7 @@ |
||
| 24 | 24 | */ |
| 25 | 25 | public function saveMany(array $data) |
| 26 | 26 | { |
| 27 | - \DB::transaction(function () use ($data) { |
|
| 27 | + \DB::transaction(function() use ($data) { |
|
| 28 | 28 | foreach ($data as $value) { |
| 29 | 29 | $this->repo->save($value); |
| 30 | 30 | } |
@@ -5,45 +5,45 @@ |
||
| 5 | 5 | |
| 6 | 6 | class ReportRepository extends BaseRepository |
| 7 | 7 | { |
| 8 | - /** |
|
| 9 | - * Init new object. |
|
| 10 | - * |
|
| 11 | - * @param Report $model |
|
| 12 | - * @return void |
|
| 13 | - */ |
|
| 14 | - public function __construct(Report $model) |
|
| 15 | - { |
|
| 16 | - parent::__construct($model); |
|
| 17 | - } |
|
| 8 | + /** |
|
| 9 | + * Init new object. |
|
| 10 | + * |
|
| 11 | + * @param Report $model |
|
| 12 | + * @return void |
|
| 13 | + */ |
|
| 14 | + public function __construct(Report $model) |
|
| 15 | + { |
|
| 16 | + parent::__construct($model); |
|
| 17 | + } |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Render the given report db view based on the given |
|
| 21 | - * condition. |
|
| 22 | - * |
|
| 23 | - * @param mixed $report |
|
| 24 | - * @param array $conditions |
|
| 25 | - * @param integer $perPage |
|
| 26 | - * @return object |
|
| 27 | - */ |
|
| 28 | - public function renderReport($report, $conditions = [], $perPage = 0) |
|
| 29 | - { |
|
| 30 | - $report = ! is_int($report) ? $report : $this->find($report); |
|
| 31 | - /** |
|
| 32 | - * Fetch data from the report based on the given conditions. |
|
| 33 | - */ |
|
| 34 | - $report = \DB::table($report->view_name); |
|
| 35 | - unset($conditions['page']); |
|
| 36 | - if (count($conditions)) { |
|
| 37 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 38 | - $report->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
| 39 | - } |
|
| 40 | - /** |
|
| 41 | - * Paginate or all data. |
|
| 42 | - */ |
|
| 43 | - if ($perPage) { |
|
| 44 | - return $report->paginate($perPage); |
|
| 45 | - } else { |
|
| 46 | - return $report->get(); |
|
| 47 | - } |
|
| 48 | - } |
|
| 19 | + /** |
|
| 20 | + * Render the given report db view based on the given |
|
| 21 | + * condition. |
|
| 22 | + * |
|
| 23 | + * @param mixed $report |
|
| 24 | + * @param array $conditions |
|
| 25 | + * @param integer $perPage |
|
| 26 | + * @return object |
|
| 27 | + */ |
|
| 28 | + public function renderReport($report, $conditions = [], $perPage = 0) |
|
| 29 | + { |
|
| 30 | + $report = ! is_int($report) ? $report : $this->find($report); |
|
| 31 | + /** |
|
| 32 | + * Fetch data from the report based on the given conditions. |
|
| 33 | + */ |
|
| 34 | + $report = \DB::table($report->view_name); |
|
| 35 | + unset($conditions['page']); |
|
| 36 | + if (count($conditions)) { |
|
| 37 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 38 | + $report->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
| 39 | + } |
|
| 40 | + /** |
|
| 41 | + * Paginate or all data. |
|
| 42 | + */ |
|
| 43 | + if ($perPage) { |
|
| 44 | + return $report->paginate($perPage); |
|
| 45 | + } else { |
|
| 46 | + return $report->get(); |
|
| 47 | + } |
|
| 48 | + } |
|
| 49 | 49 | } |
@@ -6,27 +6,27 @@ |
||
| 6 | 6 | |
| 7 | 7 | class AssignRelationsSeeder extends Seeder |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * Run the database seeds. |
|
| 11 | - * |
|
| 12 | - * @return void |
|
| 13 | - */ |
|
| 14 | - public function run() |
|
| 15 | - { |
|
| 16 | - $adminRoleId = \DB::table('roles')->where('name', 'admin')->select('id')->first()->id; |
|
| 9 | + /** |
|
| 10 | + * Run the database seeds. |
|
| 11 | + * |
|
| 12 | + * @return void |
|
| 13 | + */ |
|
| 14 | + public function run() |
|
| 15 | + { |
|
| 16 | + $adminRoleId = \DB::table('roles')->where('name', 'admin')->select('id')->first()->id; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Assign the permissions to the admin role. |
|
| 20 | - */ |
|
| 21 | - \DB::table('permissions')->orderBy('created_at', 'asc')->whereIn('model', ['report'])->each(function ($permission) use ($adminRoleId) { |
|
| 22 | - \DB::table('permission_role')->insert( |
|
| 23 | - [ |
|
| 24 | - 'permission_id' => $permission->id, |
|
| 25 | - 'role_id' => $adminRoleId, |
|
| 26 | - 'created_at' => \DB::raw('NOW()'), |
|
| 27 | - 'updated_at' => \DB::raw('NOW()') |
|
| 28 | - ] |
|
| 29 | - ); |
|
| 30 | - }); |
|
| 31 | - } |
|
| 18 | + /** |
|
| 19 | + * Assign the permissions to the admin role. |
|
| 20 | + */ |
|
| 21 | + \DB::table('permissions')->orderBy('created_at', 'asc')->whereIn('model', ['report'])->each(function ($permission) use ($adminRoleId) { |
|
| 22 | + \DB::table('permission_role')->insert( |
|
| 23 | + [ |
|
| 24 | + 'permission_id' => $permission->id, |
|
| 25 | + 'role_id' => $adminRoleId, |
|
| 26 | + 'created_at' => \DB::raw('NOW()'), |
|
| 27 | + 'updated_at' => \DB::raw('NOW()') |
|
| 28 | + ] |
|
| 29 | + ); |
|
| 30 | + }); |
|
| 31 | + } |
|
| 32 | 32 | } |
@@ -18,7 +18,7 @@ |
||
| 18 | 18 | /** |
| 19 | 19 | * Assign the permissions to the admin role. |
| 20 | 20 | */ |
| 21 | - \DB::table('permissions')->orderBy('created_at', 'asc')->whereIn('model', ['report'])->each(function ($permission) use ($adminRoleId) { |
|
| 21 | + \DB::table('permissions')->orderBy('created_at', 'asc')->whereIn('model', ['report'])->each(function($permission) use ($adminRoleId) { |
|
| 22 | 22 | \DB::table('permission_role')->insert( |
| 23 | 23 | [ |
| 24 | 24 | 'permission_id' => $permission->id, |