@@ -8,35 +8,35 @@ |
||
| 8 | 8 | |
| 9 | 9 | class GroupsController extends BaseApiController |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * The name of the model that is used by the base api controller |
|
| 13 | - * to preform actions like (add, edit ... etc). |
|
| 14 | - * @var string |
|
| 15 | - */ |
|
| 16 | - protected $model = 'groups'; |
|
| 11 | + /** |
|
| 12 | + * The name of the model that is used by the base api controller |
|
| 13 | + * to preform actions like (add, edit ... etc). |
|
| 14 | + * @var string |
|
| 15 | + */ |
|
| 16 | + protected $model = 'groups'; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * The validations rules used by the base api controller |
|
| 20 | - * to check before add. |
|
| 21 | - * @var array |
|
| 22 | - */ |
|
| 23 | - protected $validationRules = [ |
|
| 24 | - 'name' => 'required|string|max:100|unique:groups,name,{id}' |
|
| 25 | - ]; |
|
| 18 | + /** |
|
| 19 | + * The validations rules used by the base api controller |
|
| 20 | + * to check before add. |
|
| 21 | + * @var array |
|
| 22 | + */ |
|
| 23 | + protected $validationRules = [ |
|
| 24 | + 'name' => 'required|string|max:100|unique:groups,name,{id}' |
|
| 25 | + ]; |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Handle an assign permissions to group request. |
|
| 29 | - * |
|
| 30 | - * @param \Illuminate\Http\Request $request |
|
| 31 | - * @return \Illuminate\Http\Response |
|
| 32 | - */ |
|
| 33 | - public function assignpermissions(Request $request) |
|
| 34 | - { |
|
| 35 | - $this->validate($request, [ |
|
| 36 | - 'permission_ids' => 'required|exists:permissions,id', |
|
| 37 | - 'group_id' => 'required|array|exists:groups,id' |
|
| 38 | - ]); |
|
| 27 | + /** |
|
| 28 | + * Handle an assign permissions to group request. |
|
| 29 | + * |
|
| 30 | + * @param \Illuminate\Http\Request $request |
|
| 31 | + * @return \Illuminate\Http\Response |
|
| 32 | + */ |
|
| 33 | + public function assignpermissions(Request $request) |
|
| 34 | + { |
|
| 35 | + $this->validate($request, [ |
|
| 36 | + 'permission_ids' => 'required|exists:permissions,id', |
|
| 37 | + 'group_id' => 'required|array|exists:groups,id' |
|
| 38 | + ]); |
|
| 39 | 39 | |
| 40 | - return \Response::json(\Core::groups()->assignPermissions($request->get('group_id'), $request->get('permission_ids')), 200); |
|
| 41 | - } |
|
| 40 | + return \Response::json(\Core::groups()->assignPermissions($request->get('group_id'), $request->get('permission_ids')), 200); |
|
| 41 | + } |
|
| 42 | 42 | } |
@@ -29,6 +29,6 @@ |
||
| 29 | 29 | $group->permissions()->attach($permission_ids); |
| 30 | 30 | }); |
| 31 | 31 | |
| 32 | - return $this->find($group_id); |
|
| 32 | + return $this->find($group_id); |
|
| 33 | 33 | } |
| 34 | 34 | } |
@@ -32,7 +32,7 @@ discard block |
||
| 32 | 32 | * |
| 33 | 33 | * @param array $relations |
| 34 | 34 | * @param string $sortBy |
| 35 | - * @param boolean $desc |
|
| 35 | + * @param integer $desc |
|
| 36 | 36 | * @param array $columns |
| 37 | 37 | * @return collection |
| 38 | 38 | */ |
@@ -50,7 +50,7 @@ discard block |
||
| 50 | 50 | * @param integer $perPage |
| 51 | 51 | * @param array $relations |
| 52 | 52 | * @param string $sortBy |
| 53 | - * @param boolean $desc |
|
| 53 | + * @param integer $desc |
|
| 54 | 54 | * @param array $columns |
| 55 | 55 | * @return collection |
| 56 | 56 | */ |
@@ -139,7 +139,7 @@ discard block |
||
| 139 | 139 | * @param integer $perPage |
| 140 | 140 | * @param array $relations |
| 141 | 141 | * @param string $sortBy |
| 142 | - * @param boolean $desc |
|
| 142 | + * @param integer $desc |
|
| 143 | 143 | * @param array $columns |
| 144 | 144 | * @return collection |
| 145 | 145 | */ |
@@ -157,7 +157,7 @@ discard block |
||
| 157 | 157 | * @param integer $perPage |
| 158 | 158 | * @param array $relations |
| 159 | 159 | * @param string $sortBy |
| 160 | - * @param boolean $desc |
|
| 160 | + * @param integer $desc |
|
| 161 | 161 | * @param array $columns |
| 162 | 162 | * @return collection |
| 163 | 163 | */ |
@@ -496,7 +496,7 @@ discard block |
||
| 496 | 496 | * @param array $conditions array of conditions |
| 497 | 497 | * @param array $relations |
| 498 | 498 | * @param string $sortBy |
| 499 | - * @param boolean $desc |
|
| 499 | + * @param integer $desc |
|
| 500 | 500 | * @param array $columns |
| 501 | 501 | * @return collection |
| 502 | 502 | */ |
@@ -528,7 +528,7 @@ discard block |
||
| 528 | 528 | * @param array $conditions array of conditions |
| 529 | 529 | * @param integer $perPage |
| 530 | 530 | * @param string $sortBy |
| 531 | - * @param boolean $desc |
|
| 531 | + * @param integer $desc |
|
| 532 | 532 | * @param array $columns |
| 533 | 533 | * @return collection |
| 534 | 534 | */ |
@@ -551,7 +551,6 @@ discard block |
||
| 551 | 551 | * Restore the deleted model. |
| 552 | 552 | * |
| 553 | 553 | * @param integer $id |
| 554 | - * @param string $attribute condition column name |
|
| 555 | 554 | * @return void |
| 556 | 555 | */ |
| 557 | 556 | public function restore($id) |
@@ -4,634 +4,634 @@ |
||
| 4 | 4 | |
| 5 | 5 | abstract class AbstractRepository implements RepositoryInterface |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * The model implementation. |
|
| 9 | - * |
|
| 10 | - * @var model |
|
| 11 | - */ |
|
| 12 | - public $model; |
|
| 7 | + /** |
|
| 8 | + * The model implementation. |
|
| 9 | + * |
|
| 10 | + * @var model |
|
| 11 | + */ |
|
| 12 | + public $model; |
|
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * The config implementation. |
|
| 16 | - * |
|
| 17 | - * @var config |
|
| 18 | - */ |
|
| 19 | - protected $config; |
|
| 14 | + /** |
|
| 15 | + * The config implementation. |
|
| 16 | + * |
|
| 17 | + * @var config |
|
| 18 | + */ |
|
| 19 | + protected $config; |
|
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Create new AbstractRepository instance. |
|
| 23 | - */ |
|
| 24 | - public function __construct() |
|
| 25 | - { |
|
| 26 | - $this->config = \CoreConfig::getConfig(); |
|
| 27 | - $this->model = \App::make($this->getModel()); |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * Fetch all records with relations from the storage. |
|
| 32 | - * |
|
| 33 | - * @param array $relations |
|
| 34 | - * @param string $sortBy |
|
| 35 | - * @param boolean $desc |
|
| 36 | - * @param array $columns |
|
| 37 | - * @return collection |
|
| 38 | - */ |
|
| 39 | - public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
| 40 | - { |
|
| 41 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 42 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->get($columns); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Fetch all records with relations from storage in pages |
|
| 47 | - * that matche the given query. |
|
| 48 | - * |
|
| 49 | - * @param string $query |
|
| 50 | - * @param integer $perPage |
|
| 51 | - * @param array $relations |
|
| 52 | - * @param string $sortBy |
|
| 53 | - * @param boolean $desc |
|
| 54 | - * @param array $columns |
|
| 55 | - * @return collection |
|
| 56 | - */ |
|
| 57 | - public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
| 58 | - { |
|
| 59 | - $model = call_user_func_array("{$this->getModel()}::with", array($relations)); |
|
| 60 | - $conditionColumns = $this->model->searchable; |
|
| 61 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Construct the select conditions for the model. |
|
| 65 | - */ |
|
| 66 | - $model->where(function ($q) use ($query, $conditionColumns, $relations){ |
|
| 67 | - |
|
| 68 | - if (count($conditionColumns)) |
|
| 69 | - { |
|
| 70 | - /** |
|
| 71 | - * Use the first element in the model columns to construct the first condition. |
|
| 72 | - */ |
|
| 73 | - $q->where(\DB::raw('LOWER(' . array_shift($conditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Loop through the rest of the columns to construct or where conditions. |
|
| 78 | - */ |
|
| 79 | - foreach ($conditionColumns as $column) |
|
| 80 | - { |
|
| 81 | - $q->orWhere(\DB::raw('LOWER(' . $column . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Loop through the model relations. |
|
| 86 | - */ |
|
| 87 | - foreach ($relations as $relation) |
|
| 88 | - { |
|
| 89 | - /** |
|
| 90 | - * Remove the sub relation if exists. |
|
| 91 | - */ |
|
| 92 | - $relation = explode('.', $relation)[0]; |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * Try to fetch the relation repository from the core. |
|
| 96 | - */ |
|
| 97 | - if (\Core::$relation()) |
|
| 98 | - { |
|
| 99 | - /** |
|
| 100 | - * Construct the relation condition. |
|
| 101 | - */ |
|
| 102 | - $q->orWhereHas($relation, function ($subModel) use ($query, $relation){ |
|
| 103 | - |
|
| 104 | - $subModel->where(function ($q) use ($query, $relation){ |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * Get columns of the relation. |
|
| 108 | - */ |
|
| 109 | - $subConditionColumns = \Core::$relation()->model->searchable; |
|
| 110 | - |
|
| 111 | - if (count($subConditionColumns)) |
|
| 112 | - { |
|
| 113 | - /** |
|
| 114 | - * Use the first element in the relation model columns to construct the first condition. |
|
| 115 | - */ |
|
| 116 | - $q->where(\DB::raw('LOWER(' . array_shift($subConditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Loop through the rest of the columns to construct or where conditions. |
|
| 121 | - */ |
|
| 122 | - foreach ($subConditionColumns as $subConditionColumn) |
|
| 123 | - { |
|
| 124 | - $q->orWhere(\DB::raw('LOWER(' . $subConditionColumn . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
| 125 | - } |
|
| 126 | - }); |
|
| 127 | - |
|
| 128 | - }); |
|
| 129 | - } |
|
| 130 | - } |
|
| 131 | - }); |
|
| 21 | + /** |
|
| 22 | + * Create new AbstractRepository instance. |
|
| 23 | + */ |
|
| 24 | + public function __construct() |
|
| 25 | + { |
|
| 26 | + $this->config = \CoreConfig::getConfig(); |
|
| 27 | + $this->model = \App::make($this->getModel()); |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * Fetch all records with relations from the storage. |
|
| 32 | + * |
|
| 33 | + * @param array $relations |
|
| 34 | + * @param string $sortBy |
|
| 35 | + * @param boolean $desc |
|
| 36 | + * @param array $columns |
|
| 37 | + * @return collection |
|
| 38 | + */ |
|
| 39 | + public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
| 40 | + { |
|
| 41 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 42 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->get($columns); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Fetch all records with relations from storage in pages |
|
| 47 | + * that matche the given query. |
|
| 48 | + * |
|
| 49 | + * @param string $query |
|
| 50 | + * @param integer $perPage |
|
| 51 | + * @param array $relations |
|
| 52 | + * @param string $sortBy |
|
| 53 | + * @param boolean $desc |
|
| 54 | + * @param array $columns |
|
| 55 | + * @return collection |
|
| 56 | + */ |
|
| 57 | + public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
| 58 | + { |
|
| 59 | + $model = call_user_func_array("{$this->getModel()}::with", array($relations)); |
|
| 60 | + $conditionColumns = $this->model->searchable; |
|
| 61 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Construct the select conditions for the model. |
|
| 65 | + */ |
|
| 66 | + $model->where(function ($q) use ($query, $conditionColumns, $relations){ |
|
| 67 | + |
|
| 68 | + if (count($conditionColumns)) |
|
| 69 | + { |
|
| 70 | + /** |
|
| 71 | + * Use the first element in the model columns to construct the first condition. |
|
| 72 | + */ |
|
| 73 | + $q->where(\DB::raw('LOWER(' . array_shift($conditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Loop through the rest of the columns to construct or where conditions. |
|
| 78 | + */ |
|
| 79 | + foreach ($conditionColumns as $column) |
|
| 80 | + { |
|
| 81 | + $q->orWhere(\DB::raw('LOWER(' . $column . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * Loop through the model relations. |
|
| 86 | + */ |
|
| 87 | + foreach ($relations as $relation) |
|
| 88 | + { |
|
| 89 | + /** |
|
| 90 | + * Remove the sub relation if exists. |
|
| 91 | + */ |
|
| 92 | + $relation = explode('.', $relation)[0]; |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * Try to fetch the relation repository from the core. |
|
| 96 | + */ |
|
| 97 | + if (\Core::$relation()) |
|
| 98 | + { |
|
| 99 | + /** |
|
| 100 | + * Construct the relation condition. |
|
| 101 | + */ |
|
| 102 | + $q->orWhereHas($relation, function ($subModel) use ($query, $relation){ |
|
| 103 | + |
|
| 104 | + $subModel->where(function ($q) use ($query, $relation){ |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * Get columns of the relation. |
|
| 108 | + */ |
|
| 109 | + $subConditionColumns = \Core::$relation()->model->searchable; |
|
| 110 | + |
|
| 111 | + if (count($subConditionColumns)) |
|
| 112 | + { |
|
| 113 | + /** |
|
| 114 | + * Use the first element in the relation model columns to construct the first condition. |
|
| 115 | + */ |
|
| 116 | + $q->where(\DB::raw('LOWER(' . array_shift($subConditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Loop through the rest of the columns to construct or where conditions. |
|
| 121 | + */ |
|
| 122 | + foreach ($subConditionColumns as $subConditionColumn) |
|
| 123 | + { |
|
| 124 | + $q->orWhere(\DB::raw('LOWER(' . $subConditionColumn . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
| 125 | + } |
|
| 126 | + }); |
|
| 127 | + |
|
| 128 | + }); |
|
| 129 | + } |
|
| 130 | + } |
|
| 131 | + }); |
|
| 132 | 132 | |
| 133 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 134 | - } |
|
| 133 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 134 | + } |
|
| 135 | 135 | |
| 136 | - /** |
|
| 137 | - * Fetch all records with relations from storage in pages. |
|
| 138 | - * |
|
| 139 | - * @param integer $perPage |
|
| 140 | - * @param array $relations |
|
| 141 | - * @param string $sortBy |
|
| 142 | - * @param boolean $desc |
|
| 143 | - * @param array $columns |
|
| 144 | - * @return collection |
|
| 145 | - */ |
|
| 146 | - public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
| 147 | - { |
|
| 148 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 149 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * Fetch all records with relations based on |
|
| 154 | - * the given condition from storage in pages. |
|
| 155 | - * |
|
| 156 | - * @param array $conditions array of conditions |
|
| 157 | - * @param integer $perPage |
|
| 158 | - * @param array $relations |
|
| 159 | - * @param string $sortBy |
|
| 160 | - * @param boolean $desc |
|
| 161 | - * @param array $columns |
|
| 162 | - * @return collection |
|
| 163 | - */ |
|
| 164 | - public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
| 165 | - { |
|
| 166 | - unset($conditions['page']); |
|
| 167 | - $conditions = $this->constructConditions($conditions); |
|
| 168 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 169 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 170 | - } |
|
| 136 | + /** |
|
| 137 | + * Fetch all records with relations from storage in pages. |
|
| 138 | + * |
|
| 139 | + * @param integer $perPage |
|
| 140 | + * @param array $relations |
|
| 141 | + * @param string $sortBy |
|
| 142 | + * @param boolean $desc |
|
| 143 | + * @param array $columns |
|
| 144 | + * @return collection |
|
| 145 | + */ |
|
| 146 | + public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
| 147 | + { |
|
| 148 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 149 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * Fetch all records with relations based on |
|
| 154 | + * the given condition from storage in pages. |
|
| 155 | + * |
|
| 156 | + * @param array $conditions array of conditions |
|
| 157 | + * @param integer $perPage |
|
| 158 | + * @param array $relations |
|
| 159 | + * @param string $sortBy |
|
| 160 | + * @param boolean $desc |
|
| 161 | + * @param array $columns |
|
| 162 | + * @return collection |
|
| 163 | + */ |
|
| 164 | + public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
| 165 | + { |
|
| 166 | + unset($conditions['page']); |
|
| 167 | + $conditions = $this->constructConditions($conditions); |
|
| 168 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 169 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 170 | + } |
|
| 171 | 171 | |
| 172 | - /** |
|
| 173 | - * Save the given model to the storage. |
|
| 174 | - * |
|
| 175 | - * @param array $data |
|
| 176 | - * @param boolean $saveLog |
|
| 177 | - * @return void |
|
| 178 | - */ |
|
| 179 | - public function save(array $data, $saveLog = true) |
|
| 180 | - { |
|
| 181 | - $model = false; |
|
| 182 | - $modelClass = $this->model; |
|
| 183 | - $relations = []; |
|
| 184 | - |
|
| 185 | - \DB::transaction(function () use (&$model, &$relations, $data, $saveLog, $modelClass) { |
|
| 186 | - /** |
|
| 187 | - * If the id is present in the data then select the model for updating, |
|
| 188 | - * else create new model. |
|
| 189 | - * @var array |
|
| 190 | - */ |
|
| 191 | - $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
| 192 | - if ( ! $model) |
|
| 193 | - { |
|
| 194 | - \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']); |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * Construct the model object with the given data, |
|
| 199 | - * and if there is a relation add it to relations array, |
|
| 200 | - * then save the model. |
|
| 201 | - */ |
|
| 202 | - foreach ($data as $key => $value) |
|
| 203 | - { |
|
| 204 | - /** |
|
| 205 | - * If the attribute is a relation. |
|
| 206 | - */ |
|
| 207 | - $relation = camel_case($key); |
|
| 208 | - if (method_exists($model, $relation) && \Core::$relation()) |
|
| 209 | - { |
|
| 210 | - /** |
|
| 211 | - * Check if the relation is a collection. |
|
| 212 | - */ |
|
| 213 | - if (class_basename($model->$relation) == 'Collection') |
|
| 214 | - { |
|
| 215 | - /** |
|
| 216 | - * If the relation has no value then marke the relation data |
|
| 217 | - * related to the model to be deleted. |
|
| 218 | - */ |
|
| 219 | - if ( ! $value || ! count($value)) |
|
| 220 | - { |
|
| 221 | - $relations[$relation] = 'delete'; |
|
| 222 | - } |
|
| 223 | - } |
|
| 224 | - if (is_array($value)) |
|
| 225 | - { |
|
| 226 | - /** |
|
| 227 | - * Loop through the relation data. |
|
| 228 | - */ |
|
| 229 | - foreach ($value as $attr => $val) |
|
| 230 | - { |
|
| 231 | - /** |
|
| 232 | - * Get the relation model. |
|
| 233 | - */ |
|
| 234 | - $relationBaseModel = \Core::$relation()->model; |
|
| 235 | - |
|
| 236 | - /** |
|
| 237 | - * Check if the relation is a collection. |
|
| 238 | - */ |
|
| 239 | - if (class_basename($model->$relation) == 'Collection') |
|
| 240 | - { |
|
| 241 | - /** |
|
| 242 | - * If the id is present in the data then select the relation model for updating, |
|
| 243 | - * else create new model. |
|
| 244 | - */ |
|
| 245 | - $relationModel = array_key_exists('id', $val) ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
| 246 | - |
|
| 247 | - /** |
|
| 248 | - * If model doesn't exists. |
|
| 249 | - */ |
|
| 250 | - if ( ! $relationModel) |
|
| 251 | - { |
|
| 252 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']); |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - /** |
|
| 256 | - * Loop through the relation attributes. |
|
| 257 | - */ |
|
| 258 | - foreach ($val as $attr => $val) |
|
| 259 | - { |
|
| 260 | - /** |
|
| 261 | - * Prevent the sub relations or attributes not in the fillable. |
|
| 262 | - */ |
|
| 263 | - if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
| 264 | - { |
|
| 265 | - $relationModel->$attr = $val; |
|
| 266 | - } |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - $relations[$relation][] = $relationModel; |
|
| 270 | - } |
|
| 271 | - /** |
|
| 272 | - * If not collection. |
|
| 273 | - */ |
|
| 274 | - else |
|
| 275 | - { |
|
| 276 | - /** |
|
| 277 | - * Prevent the sub relations. |
|
| 278 | - */ |
|
| 279 | - if (gettype($val) !== 'object' && gettype($val) !== 'array') |
|
| 280 | - { |
|
| 281 | - |
|
| 282 | - /** |
|
| 283 | - * If the id is present in the data then select the relation model for updating, |
|
| 284 | - * else create new model. |
|
| 285 | - */ |
|
| 286 | - $relationModel = array_key_exists('id', $value) ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * If model doesn't exists. |
|
| 290 | - */ |
|
| 291 | - if ( ! $relationModel) |
|
| 292 | - { |
|
| 293 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']); |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - foreach ($value as $relationAttribute => $relationValue) |
|
| 297 | - { |
|
| 298 | - /** |
|
| 299 | - * Prevent attributes not in the fillable. |
|
| 300 | - */ |
|
| 301 | - if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) |
|
| 302 | - { |
|
| 303 | - $relationModel->$relationAttribute = $relationValue; |
|
| 304 | - } |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - $relations[$relation] = $relationModel; |
|
| 308 | - } |
|
| 309 | - } |
|
| 310 | - } |
|
| 311 | - } |
|
| 312 | - } |
|
| 313 | - /** |
|
| 314 | - * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
| 315 | - */ |
|
| 316 | - else if (array_search($key, $model->getFillable(), true) !== false) |
|
| 317 | - { |
|
| 318 | - $model->$key = $value; |
|
| 319 | - } |
|
| 320 | - } |
|
| 321 | - /** |
|
| 322 | - * Save the model. |
|
| 323 | - */ |
|
| 324 | - $model->save(); |
|
| 325 | - |
|
| 326 | - /** |
|
| 327 | - * Loop through the relations array. |
|
| 328 | - */ |
|
| 329 | - foreach ($relations as $key => $value) |
|
| 330 | - { |
|
| 331 | - /** |
|
| 332 | - * If the relation is marked for delete then delete it. |
|
| 333 | - */ |
|
| 334 | - if ($value == 'delete' && $model->$key()->count()) |
|
| 335 | - { |
|
| 336 | - $model->$key()->delete(); |
|
| 337 | - } |
|
| 338 | - /** |
|
| 339 | - * If the relation is an array. |
|
| 340 | - */ |
|
| 341 | - else if (gettype($value) == 'array') |
|
| 342 | - { |
|
| 343 | - $ids = []; |
|
| 344 | - /** |
|
| 345 | - * Loop through the relations. |
|
| 346 | - */ |
|
| 347 | - foreach ($value as $val) |
|
| 348 | - { |
|
| 349 | - switch (class_basename($model->$key())) |
|
| 350 | - { |
|
| 351 | - /** |
|
| 352 | - * If the relation is one to many then update it's foreign key with |
|
| 353 | - * the model id and save it then add its id to ids array to delete all |
|
| 354 | - * relations who's id isn't in the ids array. |
|
| 355 | - */ |
|
| 356 | - case 'HasMany': |
|
| 357 | - $foreignKeyName = explode('.', $model->$key()->getForeignKey())[1]; |
|
| 358 | - $val->$foreignKeyName = $model->id; |
|
| 359 | - $val->save(); |
|
| 360 | - $ids[] = $val->id; |
|
| 361 | - break; |
|
| 362 | - |
|
| 363 | - /** |
|
| 364 | - * If the relation is many to many then add it's id to the ids array to |
|
| 365 | - * attache these ids to the model. |
|
| 366 | - */ |
|
| 367 | - case 'BelongsToMany': |
|
| 368 | - $val->save(); |
|
| 369 | - $ids[] = $val->id; |
|
| 370 | - break; |
|
| 371 | - } |
|
| 372 | - } |
|
| 373 | - switch (class_basename($model->$key())) |
|
| 374 | - { |
|
| 375 | - /** |
|
| 376 | - * If the relation is one to many then delete all |
|
| 377 | - * relations who's id isn't in the ids array. |
|
| 378 | - */ |
|
| 379 | - case 'HasMany': |
|
| 380 | - $model->$key()->whereNotIn('id', $ids)->delete(); |
|
| 381 | - break; |
|
| 382 | - |
|
| 383 | - /** |
|
| 384 | - * If the relation is many to many then |
|
| 385 | - * detach the previous data and attach |
|
| 386 | - * the ids array to the model. |
|
| 387 | - */ |
|
| 388 | - case 'BelongsToMany': |
|
| 389 | - $model->$key()->detach(); |
|
| 390 | - $model->$key()->attach($ids); |
|
| 391 | - break; |
|
| 392 | - } |
|
| 393 | - } |
|
| 394 | - /** |
|
| 395 | - * If the relation isn't array. |
|
| 396 | - */ |
|
| 397 | - else |
|
| 398 | - { |
|
| 399 | - switch (class_basename($model->$key())) |
|
| 400 | - { |
|
| 401 | - /** |
|
| 402 | - * If the relation is one to many or one to one. |
|
| 403 | - */ |
|
| 404 | - case 'HasOne': |
|
| 405 | - |
|
| 406 | - $foreignKeyName = explode('.', $model->$key()->getForeignKey())[1]; |
|
| 407 | - $value->$foreignKeyName = $model->id; |
|
| 408 | - $value->save(); |
|
| 409 | - break; |
|
| 410 | - } |
|
| 411 | - } |
|
| 412 | - } |
|
| 413 | - |
|
| 414 | - $saveLog ? \Logging::saveLog(array_key_exists('id', $data) ? 'update' : 'create', class_basename($modelClass), $this->getModel(), $model->id, $model) : false; |
|
| 415 | - }); |
|
| 416 | - } |
|
| 172 | + /** |
|
| 173 | + * Save the given model to the storage. |
|
| 174 | + * |
|
| 175 | + * @param array $data |
|
| 176 | + * @param boolean $saveLog |
|
| 177 | + * @return void |
|
| 178 | + */ |
|
| 179 | + public function save(array $data, $saveLog = true) |
|
| 180 | + { |
|
| 181 | + $model = false; |
|
| 182 | + $modelClass = $this->model; |
|
| 183 | + $relations = []; |
|
| 184 | + |
|
| 185 | + \DB::transaction(function () use (&$model, &$relations, $data, $saveLog, $modelClass) { |
|
| 186 | + /** |
|
| 187 | + * If the id is present in the data then select the model for updating, |
|
| 188 | + * else create new model. |
|
| 189 | + * @var array |
|
| 190 | + */ |
|
| 191 | + $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
| 192 | + if ( ! $model) |
|
| 193 | + { |
|
| 194 | + \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']); |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * Construct the model object with the given data, |
|
| 199 | + * and if there is a relation add it to relations array, |
|
| 200 | + * then save the model. |
|
| 201 | + */ |
|
| 202 | + foreach ($data as $key => $value) |
|
| 203 | + { |
|
| 204 | + /** |
|
| 205 | + * If the attribute is a relation. |
|
| 206 | + */ |
|
| 207 | + $relation = camel_case($key); |
|
| 208 | + if (method_exists($model, $relation) && \Core::$relation()) |
|
| 209 | + { |
|
| 210 | + /** |
|
| 211 | + * Check if the relation is a collection. |
|
| 212 | + */ |
|
| 213 | + if (class_basename($model->$relation) == 'Collection') |
|
| 214 | + { |
|
| 215 | + /** |
|
| 216 | + * If the relation has no value then marke the relation data |
|
| 217 | + * related to the model to be deleted. |
|
| 218 | + */ |
|
| 219 | + if ( ! $value || ! count($value)) |
|
| 220 | + { |
|
| 221 | + $relations[$relation] = 'delete'; |
|
| 222 | + } |
|
| 223 | + } |
|
| 224 | + if (is_array($value)) |
|
| 225 | + { |
|
| 226 | + /** |
|
| 227 | + * Loop through the relation data. |
|
| 228 | + */ |
|
| 229 | + foreach ($value as $attr => $val) |
|
| 230 | + { |
|
| 231 | + /** |
|
| 232 | + * Get the relation model. |
|
| 233 | + */ |
|
| 234 | + $relationBaseModel = \Core::$relation()->model; |
|
| 235 | + |
|
| 236 | + /** |
|
| 237 | + * Check if the relation is a collection. |
|
| 238 | + */ |
|
| 239 | + if (class_basename($model->$relation) == 'Collection') |
|
| 240 | + { |
|
| 241 | + /** |
|
| 242 | + * If the id is present in the data then select the relation model for updating, |
|
| 243 | + * else create new model. |
|
| 244 | + */ |
|
| 245 | + $relationModel = array_key_exists('id', $val) ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
| 246 | + |
|
| 247 | + /** |
|
| 248 | + * If model doesn't exists. |
|
| 249 | + */ |
|
| 250 | + if ( ! $relationModel) |
|
| 251 | + { |
|
| 252 | + \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']); |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * Loop through the relation attributes. |
|
| 257 | + */ |
|
| 258 | + foreach ($val as $attr => $val) |
|
| 259 | + { |
|
| 260 | + /** |
|
| 261 | + * Prevent the sub relations or attributes not in the fillable. |
|
| 262 | + */ |
|
| 263 | + if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
| 264 | + { |
|
| 265 | + $relationModel->$attr = $val; |
|
| 266 | + } |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + $relations[$relation][] = $relationModel; |
|
| 270 | + } |
|
| 271 | + /** |
|
| 272 | + * If not collection. |
|
| 273 | + */ |
|
| 274 | + else |
|
| 275 | + { |
|
| 276 | + /** |
|
| 277 | + * Prevent the sub relations. |
|
| 278 | + */ |
|
| 279 | + if (gettype($val) !== 'object' && gettype($val) !== 'array') |
|
| 280 | + { |
|
| 281 | + |
|
| 282 | + /** |
|
| 283 | + * If the id is present in the data then select the relation model for updating, |
|
| 284 | + * else create new model. |
|
| 285 | + */ |
|
| 286 | + $relationModel = array_key_exists('id', $value) ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
| 287 | + |
|
| 288 | + /** |
|
| 289 | + * If model doesn't exists. |
|
| 290 | + */ |
|
| 291 | + if ( ! $relationModel) |
|
| 292 | + { |
|
| 293 | + \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']); |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + foreach ($value as $relationAttribute => $relationValue) |
|
| 297 | + { |
|
| 298 | + /** |
|
| 299 | + * Prevent attributes not in the fillable. |
|
| 300 | + */ |
|
| 301 | + if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) |
|
| 302 | + { |
|
| 303 | + $relationModel->$relationAttribute = $relationValue; |
|
| 304 | + } |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + $relations[$relation] = $relationModel; |
|
| 308 | + } |
|
| 309 | + } |
|
| 310 | + } |
|
| 311 | + } |
|
| 312 | + } |
|
| 313 | + /** |
|
| 314 | + * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
| 315 | + */ |
|
| 316 | + else if (array_search($key, $model->getFillable(), true) !== false) |
|
| 317 | + { |
|
| 318 | + $model->$key = $value; |
|
| 319 | + } |
|
| 320 | + } |
|
| 321 | + /** |
|
| 322 | + * Save the model. |
|
| 323 | + */ |
|
| 324 | + $model->save(); |
|
| 325 | + |
|
| 326 | + /** |
|
| 327 | + * Loop through the relations array. |
|
| 328 | + */ |
|
| 329 | + foreach ($relations as $key => $value) |
|
| 330 | + { |
|
| 331 | + /** |
|
| 332 | + * If the relation is marked for delete then delete it. |
|
| 333 | + */ |
|
| 334 | + if ($value == 'delete' && $model->$key()->count()) |
|
| 335 | + { |
|
| 336 | + $model->$key()->delete(); |
|
| 337 | + } |
|
| 338 | + /** |
|
| 339 | + * If the relation is an array. |
|
| 340 | + */ |
|
| 341 | + else if (gettype($value) == 'array') |
|
| 342 | + { |
|
| 343 | + $ids = []; |
|
| 344 | + /** |
|
| 345 | + * Loop through the relations. |
|
| 346 | + */ |
|
| 347 | + foreach ($value as $val) |
|
| 348 | + { |
|
| 349 | + switch (class_basename($model->$key())) |
|
| 350 | + { |
|
| 351 | + /** |
|
| 352 | + * If the relation is one to many then update it's foreign key with |
|
| 353 | + * the model id and save it then add its id to ids array to delete all |
|
| 354 | + * relations who's id isn't in the ids array. |
|
| 355 | + */ |
|
| 356 | + case 'HasMany': |
|
| 357 | + $foreignKeyName = explode('.', $model->$key()->getForeignKey())[1]; |
|
| 358 | + $val->$foreignKeyName = $model->id; |
|
| 359 | + $val->save(); |
|
| 360 | + $ids[] = $val->id; |
|
| 361 | + break; |
|
| 362 | + |
|
| 363 | + /** |
|
| 364 | + * If the relation is many to many then add it's id to the ids array to |
|
| 365 | + * attache these ids to the model. |
|
| 366 | + */ |
|
| 367 | + case 'BelongsToMany': |
|
| 368 | + $val->save(); |
|
| 369 | + $ids[] = $val->id; |
|
| 370 | + break; |
|
| 371 | + } |
|
| 372 | + } |
|
| 373 | + switch (class_basename($model->$key())) |
|
| 374 | + { |
|
| 375 | + /** |
|
| 376 | + * If the relation is one to many then delete all |
|
| 377 | + * relations who's id isn't in the ids array. |
|
| 378 | + */ |
|
| 379 | + case 'HasMany': |
|
| 380 | + $model->$key()->whereNotIn('id', $ids)->delete(); |
|
| 381 | + break; |
|
| 382 | + |
|
| 383 | + /** |
|
| 384 | + * If the relation is many to many then |
|
| 385 | + * detach the previous data and attach |
|
| 386 | + * the ids array to the model. |
|
| 387 | + */ |
|
| 388 | + case 'BelongsToMany': |
|
| 389 | + $model->$key()->detach(); |
|
| 390 | + $model->$key()->attach($ids); |
|
| 391 | + break; |
|
| 392 | + } |
|
| 393 | + } |
|
| 394 | + /** |
|
| 395 | + * If the relation isn't array. |
|
| 396 | + */ |
|
| 397 | + else |
|
| 398 | + { |
|
| 399 | + switch (class_basename($model->$key())) |
|
| 400 | + { |
|
| 401 | + /** |
|
| 402 | + * If the relation is one to many or one to one. |
|
| 403 | + */ |
|
| 404 | + case 'HasOne': |
|
| 405 | + |
|
| 406 | + $foreignKeyName = explode('.', $model->$key()->getForeignKey())[1]; |
|
| 407 | + $value->$foreignKeyName = $model->id; |
|
| 408 | + $value->save(); |
|
| 409 | + break; |
|
| 410 | + } |
|
| 411 | + } |
|
| 412 | + } |
|
| 413 | + |
|
| 414 | + $saveLog ? \Logging::saveLog(array_key_exists('id', $data) ? 'update' : 'create', class_basename($modelClass), $this->getModel(), $model->id, $model) : false; |
|
| 415 | + }); |
|
| 416 | + } |
|
| 417 | 417 | |
| 418 | - /** |
|
| 419 | - * Update record in the storage based on the given |
|
| 420 | - * condition. |
|
| 421 | - * |
|
| 422 | - * @param [type] $value condition value |
|
| 423 | - * @param array $data |
|
| 424 | - * @param string $attribute condition column name |
|
| 425 | - * @return void |
|
| 426 | - */ |
|
| 427 | - public function update($value, array $data, $attribute = 'id', $saveLog = true) |
|
| 428 | - { |
|
| 429 | - if ($attribute == 'id') |
|
| 430 | - { |
|
| 431 | - $model = $this->model->lockForUpdate()->find($value); |
|
| 432 | - $model ? $model->update($data) : 0; |
|
| 433 | - $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $value, $model) : false; |
|
| 434 | - } |
|
| 435 | - else |
|
| 436 | - { |
|
| 437 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data, $saveLog){ |
|
| 438 | - $model->update($data); |
|
| 439 | - $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
|
| 440 | - }); |
|
| 441 | - } |
|
| 442 | - } |
|
| 443 | - |
|
| 444 | - /** |
|
| 445 | - * Delete record from the storage based on the given |
|
| 446 | - * condition. |
|
| 447 | - * |
|
| 448 | - * @param var $value condition value |
|
| 449 | - * @param string $attribute condition column name |
|
| 450 | - * @return void |
|
| 451 | - */ |
|
| 452 | - public function delete($value, $attribute = 'id', $saveLog = true) |
|
| 453 | - { |
|
| 454 | - if ($attribute == 'id') |
|
| 455 | - { |
|
| 456 | - \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
| 457 | - $model = $this->model->lockForUpdate()->find($value); |
|
| 458 | - if ( ! $model) |
|
| 459 | - { |
|
| 460 | - \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $value); |
|
| 461 | - } |
|
| 418 | + /** |
|
| 419 | + * Update record in the storage based on the given |
|
| 420 | + * condition. |
|
| 421 | + * |
|
| 422 | + * @param [type] $value condition value |
|
| 423 | + * @param array $data |
|
| 424 | + * @param string $attribute condition column name |
|
| 425 | + * @return void |
|
| 426 | + */ |
|
| 427 | + public function update($value, array $data, $attribute = 'id', $saveLog = true) |
|
| 428 | + { |
|
| 429 | + if ($attribute == 'id') |
|
| 430 | + { |
|
| 431 | + $model = $this->model->lockForUpdate()->find($value); |
|
| 432 | + $model ? $model->update($data) : 0; |
|
| 433 | + $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $value, $model) : false; |
|
| 434 | + } |
|
| 435 | + else |
|
| 436 | + { |
|
| 437 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data, $saveLog){ |
|
| 438 | + $model->update($data); |
|
| 439 | + $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
|
| 440 | + }); |
|
| 441 | + } |
|
| 442 | + } |
|
| 443 | + |
|
| 444 | + /** |
|
| 445 | + * Delete record from the storage based on the given |
|
| 446 | + * condition. |
|
| 447 | + * |
|
| 448 | + * @param var $value condition value |
|
| 449 | + * @param string $attribute condition column name |
|
| 450 | + * @return void |
|
| 451 | + */ |
|
| 452 | + public function delete($value, $attribute = 'id', $saveLog = true) |
|
| 453 | + { |
|
| 454 | + if ($attribute == 'id') |
|
| 455 | + { |
|
| 456 | + \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
| 457 | + $model = $this->model->lockForUpdate()->find($value); |
|
| 458 | + if ( ! $model) |
|
| 459 | + { |
|
| 460 | + \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $value); |
|
| 461 | + } |
|
| 462 | 462 | |
| 463 | - $model->delete(); |
|
| 464 | - $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $value, $model) : false; |
|
| 465 | - }); |
|
| 466 | - } |
|
| 467 | - else |
|
| 468 | - { |
|
| 469 | - \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
| 470 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model){ |
|
| 471 | - $model->delete(); |
|
| 472 | - $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
|
| 473 | - }); |
|
| 474 | - }); |
|
| 475 | - } |
|
| 476 | - } |
|
| 463 | + $model->delete(); |
|
| 464 | + $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $value, $model) : false; |
|
| 465 | + }); |
|
| 466 | + } |
|
| 467 | + else |
|
| 468 | + { |
|
| 469 | + \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
| 470 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model){ |
|
| 471 | + $model->delete(); |
|
| 472 | + $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
|
| 473 | + }); |
|
| 474 | + }); |
|
| 475 | + } |
|
| 476 | + } |
|
| 477 | 477 | |
| 478 | - /** |
|
| 479 | - * Fetch records from the storage based on the given |
|
| 480 | - * id. |
|
| 481 | - * |
|
| 482 | - * @param integer $id |
|
| 483 | - * @param array $relations |
|
| 484 | - * @param array $columns |
|
| 485 | - * @return object |
|
| 486 | - */ |
|
| 487 | - public function find($id, $relations = [], $columns = array('*')) |
|
| 488 | - { |
|
| 489 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->find($id, $columns); |
|
| 490 | - } |
|
| 478 | + /** |
|
| 479 | + * Fetch records from the storage based on the given |
|
| 480 | + * id. |
|
| 481 | + * |
|
| 482 | + * @param integer $id |
|
| 483 | + * @param array $relations |
|
| 484 | + * @param array $columns |
|
| 485 | + * @return object |
|
| 486 | + */ |
|
| 487 | + public function find($id, $relations = [], $columns = array('*')) |
|
| 488 | + { |
|
| 489 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->find($id, $columns); |
|
| 490 | + } |
|
| 491 | 491 | |
| 492 | - /** |
|
| 493 | - * Fetch records from the storage based on the given |
|
| 494 | - * condition. |
|
| 495 | - * |
|
| 496 | - * @param array $conditions array of conditions |
|
| 497 | - * @param array $relations |
|
| 498 | - * @param string $sortBy |
|
| 499 | - * @param boolean $desc |
|
| 500 | - * @param array $columns |
|
| 501 | - * @return collection |
|
| 502 | - */ |
|
| 503 | - public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
| 504 | - { |
|
| 505 | - $conditions = $this->constructConditions($conditions); |
|
| 506 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 507 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
| 508 | - } |
|
| 509 | - |
|
| 510 | - /** |
|
| 511 | - * Fetch the first record from the storage based on the given |
|
| 512 | - * condition. |
|
| 513 | - * |
|
| 514 | - * @param array $conditions array of conditions |
|
| 515 | - * @param array $relations |
|
| 516 | - * @param array $columns |
|
| 517 | - * @return object |
|
| 518 | - */ |
|
| 519 | - public function first($conditions, $relations = [], $columns = array('*')) |
|
| 520 | - { |
|
| 521 | - $conditions = $this->constructConditions($conditions); |
|
| 522 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
| 523 | - } |
|
| 524 | - |
|
| 525 | - /** |
|
| 526 | - * Return the deleted models in pages based on the given conditions. |
|
| 527 | - * |
|
| 528 | - * @param array $conditions array of conditions |
|
| 529 | - * @param integer $perPage |
|
| 530 | - * @param string $sortBy |
|
| 531 | - * @param boolean $desc |
|
| 532 | - * @param array $columns |
|
| 533 | - * @return collection |
|
| 534 | - */ |
|
| 535 | - public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
| 536 | - { |
|
| 537 | - unset($conditions['page']); |
|
| 538 | - $conditions = $this->constructConditions($conditions); |
|
| 539 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 540 | - $model = $this->model->onlyTrashed(); |
|
| 541 | - |
|
| 542 | - if (count($conditions['conditionValues'])) |
|
| 543 | - { |
|
| 544 | - $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
| 545 | - } |
|
| 546 | - |
|
| 547 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns);; |
|
| 548 | - } |
|
| 549 | - |
|
| 550 | - /** |
|
| 551 | - * Restore the deleted model. |
|
| 552 | - * |
|
| 553 | - * @param integer $id |
|
| 554 | - * @param string $attribute condition column name |
|
| 555 | - * @return void |
|
| 556 | - */ |
|
| 557 | - public function restore($id) |
|
| 558 | - { |
|
| 559 | - $model = $this->model->onlyTrashed()->find($id); |
|
| 560 | - |
|
| 561 | - if ( ! $model) |
|
| 562 | - { |
|
| 563 | - \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $id); |
|
| 564 | - } |
|
| 565 | - |
|
| 566 | - $model->restore(); |
|
| 567 | - } |
|
| 568 | - |
|
| 569 | - /** |
|
| 570 | - * Build the conditions recursively for the retrieving methods. |
|
| 571 | - * @param array $conditions |
|
| 572 | - * @return array |
|
| 573 | - */ |
|
| 574 | - protected function constructConditions($conditions) |
|
| 575 | - { |
|
| 576 | - $conditionString = ''; |
|
| 577 | - $conditionValues = []; |
|
| 578 | - foreach ($conditions as $key => $value) |
|
| 579 | - { |
|
| 580 | - if ($key == 'and') |
|
| 581 | - { |
|
| 582 | - $conditionString .= str_replace('{op}', 'and', $this->constructConditions($value)['conditionString']) . ' {op} '; |
|
| 583 | - $conditionValues = array_merge($conditionValues, $this->constructConditions($value)['conditionValues']); |
|
| 584 | - } |
|
| 585 | - else if ($key == 'or') |
|
| 586 | - { |
|
| 587 | - $conditionString .= str_replace('{op}', 'or', $this->constructConditions($value)['conditionString']) . ' {op} '; |
|
| 588 | - $conditionValues = array_merge($conditionValues, $this->constructConditions($value)['conditionValues']); |
|
| 589 | - } |
|
| 590 | - else |
|
| 591 | - { |
|
| 592 | - if (is_array($value)) |
|
| 593 | - { |
|
| 594 | - $operator = $value['op']; |
|
| 595 | - if (strtolower($operator) == 'between') |
|
| 596 | - { |
|
| 597 | - $value1 = $value['val1']; |
|
| 598 | - $value2 = $value['val2']; |
|
| 599 | - } |
|
| 600 | - else |
|
| 601 | - { |
|
| 602 | - $value = $value['val']; |
|
| 603 | - } |
|
| 604 | - } |
|
| 605 | - else |
|
| 606 | - { |
|
| 607 | - $operator = '='; |
|
| 608 | - } |
|
| 492 | + /** |
|
| 493 | + * Fetch records from the storage based on the given |
|
| 494 | + * condition. |
|
| 495 | + * |
|
| 496 | + * @param array $conditions array of conditions |
|
| 497 | + * @param array $relations |
|
| 498 | + * @param string $sortBy |
|
| 499 | + * @param boolean $desc |
|
| 500 | + * @param array $columns |
|
| 501 | + * @return collection |
|
| 502 | + */ |
|
| 503 | + public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
| 504 | + { |
|
| 505 | + $conditions = $this->constructConditions($conditions); |
|
| 506 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 507 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
| 508 | + } |
|
| 509 | + |
|
| 510 | + /** |
|
| 511 | + * Fetch the first record from the storage based on the given |
|
| 512 | + * condition. |
|
| 513 | + * |
|
| 514 | + * @param array $conditions array of conditions |
|
| 515 | + * @param array $relations |
|
| 516 | + * @param array $columns |
|
| 517 | + * @return object |
|
| 518 | + */ |
|
| 519 | + public function first($conditions, $relations = [], $columns = array('*')) |
|
| 520 | + { |
|
| 521 | + $conditions = $this->constructConditions($conditions); |
|
| 522 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
| 523 | + } |
|
| 524 | + |
|
| 525 | + /** |
|
| 526 | + * Return the deleted models in pages based on the given conditions. |
|
| 527 | + * |
|
| 528 | + * @param array $conditions array of conditions |
|
| 529 | + * @param integer $perPage |
|
| 530 | + * @param string $sortBy |
|
| 531 | + * @param boolean $desc |
|
| 532 | + * @param array $columns |
|
| 533 | + * @return collection |
|
| 534 | + */ |
|
| 535 | + public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
| 536 | + { |
|
| 537 | + unset($conditions['page']); |
|
| 538 | + $conditions = $this->constructConditions($conditions); |
|
| 539 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 540 | + $model = $this->model->onlyTrashed(); |
|
| 541 | + |
|
| 542 | + if (count($conditions['conditionValues'])) |
|
| 543 | + { |
|
| 544 | + $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
| 545 | + } |
|
| 546 | + |
|
| 547 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns);; |
|
| 548 | + } |
|
| 549 | + |
|
| 550 | + /** |
|
| 551 | + * Restore the deleted model. |
|
| 552 | + * |
|
| 553 | + * @param integer $id |
|
| 554 | + * @param string $attribute condition column name |
|
| 555 | + * @return void |
|
| 556 | + */ |
|
| 557 | + public function restore($id) |
|
| 558 | + { |
|
| 559 | + $model = $this->model->onlyTrashed()->find($id); |
|
| 560 | + |
|
| 561 | + if ( ! $model) |
|
| 562 | + { |
|
| 563 | + \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $id); |
|
| 564 | + } |
|
| 565 | + |
|
| 566 | + $model->restore(); |
|
| 567 | + } |
|
| 568 | + |
|
| 569 | + /** |
|
| 570 | + * Build the conditions recursively for the retrieving methods. |
|
| 571 | + * @param array $conditions |
|
| 572 | + * @return array |
|
| 573 | + */ |
|
| 574 | + protected function constructConditions($conditions) |
|
| 575 | + { |
|
| 576 | + $conditionString = ''; |
|
| 577 | + $conditionValues = []; |
|
| 578 | + foreach ($conditions as $key => $value) |
|
| 579 | + { |
|
| 580 | + if ($key == 'and') |
|
| 581 | + { |
|
| 582 | + $conditionString .= str_replace('{op}', 'and', $this->constructConditions($value)['conditionString']) . ' {op} '; |
|
| 583 | + $conditionValues = array_merge($conditionValues, $this->constructConditions($value)['conditionValues']); |
|
| 584 | + } |
|
| 585 | + else if ($key == 'or') |
|
| 586 | + { |
|
| 587 | + $conditionString .= str_replace('{op}', 'or', $this->constructConditions($value)['conditionString']) . ' {op} '; |
|
| 588 | + $conditionValues = array_merge($conditionValues, $this->constructConditions($value)['conditionValues']); |
|
| 589 | + } |
|
| 590 | + else |
|
| 591 | + { |
|
| 592 | + if (is_array($value)) |
|
| 593 | + { |
|
| 594 | + $operator = $value['op']; |
|
| 595 | + if (strtolower($operator) == 'between') |
|
| 596 | + { |
|
| 597 | + $value1 = $value['val1']; |
|
| 598 | + $value2 = $value['val2']; |
|
| 599 | + } |
|
| 600 | + else |
|
| 601 | + { |
|
| 602 | + $value = $value['val']; |
|
| 603 | + } |
|
| 604 | + } |
|
| 605 | + else |
|
| 606 | + { |
|
| 607 | + $operator = '='; |
|
| 608 | + } |
|
| 609 | 609 | |
| 610 | - if (strtolower($operator) == 'between') |
|
| 611 | - { |
|
| 612 | - $conditionString .= $key . ' >= ? and '; |
|
| 613 | - $conditionValues[] = $value1; |
|
| 614 | - |
|
| 615 | - $conditionString .= $key . ' <= ? {op} '; |
|
| 616 | - $conditionValues[] = $value2; |
|
| 617 | - } |
|
| 618 | - else |
|
| 619 | - { |
|
| 620 | - $conditionString .= $key . ' ' . $operator . ' ? {op} '; |
|
| 621 | - $conditionValues[] = $value; |
|
| 622 | - } |
|
| 623 | - } |
|
| 624 | - } |
|
| 625 | - $conditionString = '(' . rtrim($conditionString, '{op} ') . ')'; |
|
| 626 | - return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
| 627 | - } |
|
| 628 | - |
|
| 629 | - /** |
|
| 630 | - * Abstract method that return the necessary |
|
| 631 | - * information (full model namespace) |
|
| 632 | - * needed to preform the previous actions. |
|
| 633 | - * |
|
| 634 | - * @return string |
|
| 635 | - */ |
|
| 636 | - abstract protected function getModel(); |
|
| 610 | + if (strtolower($operator) == 'between') |
|
| 611 | + { |
|
| 612 | + $conditionString .= $key . ' >= ? and '; |
|
| 613 | + $conditionValues[] = $value1; |
|
| 614 | + |
|
| 615 | + $conditionString .= $key . ' <= ? {op} '; |
|
| 616 | + $conditionValues[] = $value2; |
|
| 617 | + } |
|
| 618 | + else |
|
| 619 | + { |
|
| 620 | + $conditionString .= $key . ' ' . $operator . ' ? {op} '; |
|
| 621 | + $conditionValues[] = $value; |
|
| 622 | + } |
|
| 623 | + } |
|
| 624 | + } |
|
| 625 | + $conditionString = '(' . rtrim($conditionString, '{op} ') . ')'; |
|
| 626 | + return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
| 627 | + } |
|
| 628 | + |
|
| 629 | + /** |
|
| 630 | + * Abstract method that return the necessary |
|
| 631 | + * information (full model namespace) |
|
| 632 | + * needed to preform the previous actions. |
|
| 633 | + * |
|
| 634 | + * @return string |
|
| 635 | + */ |
|
| 636 | + abstract protected function getModel(); |
|
| 637 | 637 | } |
| 638 | 638 | \ No newline at end of file |
@@ -63,14 +63,14 @@ discard block |
||
| 63 | 63 | /** |
| 64 | 64 | * Construct the select conditions for the model. |
| 65 | 65 | */ |
| 66 | - $model->where(function ($q) use ($query, $conditionColumns, $relations){ |
|
| 66 | + $model->where(function($q) use ($query, $conditionColumns, $relations){ |
|
| 67 | 67 | |
| 68 | 68 | if (count($conditionColumns)) |
| 69 | 69 | { |
| 70 | 70 | /** |
| 71 | 71 | * Use the first element in the model columns to construct the first condition. |
| 72 | 72 | */ |
| 73 | - $q->where(\DB::raw('LOWER(' . array_shift($conditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
| 73 | + $q->where(\DB::raw('LOWER('.array_shift($conditionColumns).')'), 'LIKE', '%'.strtolower($query).'%'); |
|
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | /** |
@@ -78,7 +78,7 @@ discard block |
||
| 78 | 78 | */ |
| 79 | 79 | foreach ($conditionColumns as $column) |
| 80 | 80 | { |
| 81 | - $q->orWhere(\DB::raw('LOWER(' . $column . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
| 81 | + $q->orWhere(\DB::raw('LOWER('.$column.')'), 'LIKE', '%'.strtolower($query).'%'); |
|
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | /** |
@@ -99,9 +99,9 @@ discard block |
||
| 99 | 99 | /** |
| 100 | 100 | * Construct the relation condition. |
| 101 | 101 | */ |
| 102 | - $q->orWhereHas($relation, function ($subModel) use ($query, $relation){ |
|
| 102 | + $q->orWhereHas($relation, function($subModel) use ($query, $relation){ |
|
| 103 | 103 | |
| 104 | - $subModel->where(function ($q) use ($query, $relation){ |
|
| 104 | + $subModel->where(function($q) use ($query, $relation){ |
|
| 105 | 105 | |
| 106 | 106 | /** |
| 107 | 107 | * Get columns of the relation. |
@@ -113,7 +113,7 @@ discard block |
||
| 113 | 113 | /** |
| 114 | 114 | * Use the first element in the relation model columns to construct the first condition. |
| 115 | 115 | */ |
| 116 | - $q->where(\DB::raw('LOWER(' . array_shift($subConditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
| 116 | + $q->where(\DB::raw('LOWER('.array_shift($subConditionColumns).')'), 'LIKE', '%'.strtolower($query).'%'); |
|
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | /** |
@@ -121,7 +121,7 @@ discard block |
||
| 121 | 121 | */ |
| 122 | 122 | foreach ($subConditionColumns as $subConditionColumn) |
| 123 | 123 | { |
| 124 | - $q->orWhere(\DB::raw('LOWER(' . $subConditionColumn . ')'), 'LIKE', '%' . strtolower($query) . '%'); |
|
| 124 | + $q->orWhere(\DB::raw('LOWER('.$subConditionColumn.')'), 'LIKE', '%'.strtolower($query).'%'); |
|
| 125 | 125 | } |
| 126 | 126 | }); |
| 127 | 127 | |
@@ -182,7 +182,7 @@ discard block |
||
| 182 | 182 | $modelClass = $this->model; |
| 183 | 183 | $relations = []; |
| 184 | 184 | |
| 185 | - \DB::transaction(function () use (&$model, &$relations, $data, $saveLog, $modelClass) { |
|
| 185 | + \DB::transaction(function() use (&$model, &$relations, $data, $saveLog, $modelClass) { |
|
| 186 | 186 | /** |
| 187 | 187 | * If the id is present in the data then select the model for updating, |
| 188 | 188 | * else create new model. |
@@ -191,7 +191,7 @@ discard block |
||
| 191 | 191 | $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
| 192 | 192 | if ( ! $model) |
| 193 | 193 | { |
| 194 | - \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']); |
|
| 194 | + \ErrorHandler::notFound(class_basename($modelClass).' with id : '.$data['id']); |
|
| 195 | 195 | } |
| 196 | 196 | |
| 197 | 197 | /** |
@@ -249,7 +249,7 @@ discard block |
||
| 249 | 249 | */ |
| 250 | 250 | if ( ! $relationModel) |
| 251 | 251 | { |
| 252 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']); |
|
| 252 | + \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
|
| 253 | 253 | } |
| 254 | 254 | |
| 255 | 255 | /** |
@@ -260,7 +260,7 @@ discard block |
||
| 260 | 260 | /** |
| 261 | 261 | * Prevent the sub relations or attributes not in the fillable. |
| 262 | 262 | */ |
| 263 | - if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
| 263 | + if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
| 264 | 264 | { |
| 265 | 265 | $relationModel->$attr = $val; |
| 266 | 266 | } |
@@ -290,7 +290,7 @@ discard block |
||
| 290 | 290 | */ |
| 291 | 291 | if ( ! $relationModel) |
| 292 | 292 | { |
| 293 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']); |
|
| 293 | + \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
|
| 294 | 294 | } |
| 295 | 295 | |
| 296 | 296 | foreach ($value as $relationAttribute => $relationValue) |
@@ -434,7 +434,7 @@ discard block |
||
| 434 | 434 | } |
| 435 | 435 | else |
| 436 | 436 | { |
| 437 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data, $saveLog){ |
|
| 437 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function($model) use ($data, $saveLog){ |
|
| 438 | 438 | $model->update($data); |
| 439 | 439 | $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
| 440 | 440 | }); |
@@ -453,11 +453,11 @@ discard block |
||
| 453 | 453 | { |
| 454 | 454 | if ($attribute == 'id') |
| 455 | 455 | { |
| 456 | - \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
| 456 | + \DB::transaction(function() use ($value, $attribute, &$result, $saveLog) { |
|
| 457 | 457 | $model = $this->model->lockForUpdate()->find($value); |
| 458 | 458 | if ( ! $model) |
| 459 | 459 | { |
| 460 | - \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $value); |
|
| 460 | + \ErrorHandler::notFound(class_basename($this->model).' with id : '.$value); |
|
| 461 | 461 | } |
| 462 | 462 | |
| 463 | 463 | $model->delete(); |
@@ -466,8 +466,8 @@ discard block |
||
| 466 | 466 | } |
| 467 | 467 | else |
| 468 | 468 | { |
| 469 | - \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { |
|
| 470 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model){ |
|
| 469 | + \DB::transaction(function() use ($value, $attribute, &$result, $saveLog) { |
|
| 470 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function($model) { |
|
| 471 | 471 | $model->delete(); |
| 472 | 472 | $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $model->id, $model) : false; |
| 473 | 473 | }); |
@@ -504,7 +504,7 @@ discard block |
||
| 504 | 504 | { |
| 505 | 505 | $conditions = $this->constructConditions($conditions); |
| 506 | 506 | $sort = $desc ? 'desc' : 'asc'; |
| 507 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
| 507 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
| 508 | 508 | } |
| 509 | 509 | |
| 510 | 510 | /** |
@@ -544,7 +544,7 @@ discard block |
||
| 544 | 544 | $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
| 545 | 545 | } |
| 546 | 546 | |
| 547 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns);; |
|
| 547 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); ; |
|
| 548 | 548 | } |
| 549 | 549 | |
| 550 | 550 | /** |
@@ -560,7 +560,7 @@ discard block |
||
| 560 | 560 | |
| 561 | 561 | if ( ! $model) |
| 562 | 562 | { |
| 563 | - \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $id); |
|
| 563 | + \ErrorHandler::notFound(class_basename($this->model).' with id : '.$id); |
|
| 564 | 564 | } |
| 565 | 565 | |
| 566 | 566 | $model->restore(); |
@@ -579,12 +579,12 @@ discard block |
||
| 579 | 579 | { |
| 580 | 580 | if ($key == 'and') |
| 581 | 581 | { |
| 582 | - $conditionString .= str_replace('{op}', 'and', $this->constructConditions($value)['conditionString']) . ' {op} '; |
|
| 582 | + $conditionString .= str_replace('{op}', 'and', $this->constructConditions($value)['conditionString']).' {op} '; |
|
| 583 | 583 | $conditionValues = array_merge($conditionValues, $this->constructConditions($value)['conditionValues']); |
| 584 | 584 | } |
| 585 | 585 | else if ($key == 'or') |
| 586 | 586 | { |
| 587 | - $conditionString .= str_replace('{op}', 'or', $this->constructConditions($value)['conditionString']) . ' {op} '; |
|
| 587 | + $conditionString .= str_replace('{op}', 'or', $this->constructConditions($value)['conditionString']).' {op} '; |
|
| 588 | 588 | $conditionValues = array_merge($conditionValues, $this->constructConditions($value)['conditionValues']); |
| 589 | 589 | } |
| 590 | 590 | else |
@@ -609,20 +609,20 @@ discard block |
||
| 609 | 609 | |
| 610 | 610 | if (strtolower($operator) == 'between') |
| 611 | 611 | { |
| 612 | - $conditionString .= $key . ' >= ? and '; |
|
| 612 | + $conditionString .= $key.' >= ? and '; |
|
| 613 | 613 | $conditionValues[] = $value1; |
| 614 | 614 | |
| 615 | - $conditionString .= $key . ' <= ? {op} '; |
|
| 615 | + $conditionString .= $key.' <= ? {op} '; |
|
| 616 | 616 | $conditionValues[] = $value2; |
| 617 | 617 | } |
| 618 | 618 | else |
| 619 | 619 | { |
| 620 | - $conditionString .= $key . ' ' . $operator . ' ? {op} '; |
|
| 620 | + $conditionString .= $key.' '.$operator.' ? {op} '; |
|
| 621 | 621 | $conditionValues[] = $value; |
| 622 | 622 | } |
| 623 | 623 | } |
| 624 | 624 | } |
| 625 | - $conditionString = '(' . rtrim($conditionString, '{op} ') . ')'; |
|
| 625 | + $conditionString = '('.rtrim($conditionString, '{op} ').')'; |
|
| 626 | 626 | return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
| 627 | 627 | } |
| 628 | 628 | |
@@ -61,7 +61,7 @@ discard block |
||
| 61 | 61 | /** |
| 62 | 62 | * Fetch all records with relations from model repository. |
| 63 | 63 | * |
| 64 | - * @return \Illuminate\Http\Response |
|
| 64 | + * @return \Illuminate\Http\JsonResponse|null |
|
| 65 | 65 | */ |
| 66 | 66 | public function index() |
| 67 | 67 | { |
@@ -76,7 +76,7 @@ discard block |
||
| 76 | 76 | * Fetch the single object with relations from model repository. |
| 77 | 77 | * |
| 78 | 78 | * @param integer $id |
| 79 | - * @return \Illuminate\Http\Response |
|
| 79 | + * @return \Illuminate\Http\JsonResponse|null |
|
| 80 | 80 | */ |
| 81 | 81 | public function find($id) |
| 82 | 82 | { |
@@ -94,8 +94,8 @@ discard block |
||
| 94 | 94 | * @param string $query |
| 95 | 95 | * @param integer $perPage |
| 96 | 96 | * @param string $sortBy |
| 97 | - * @param boolean $desc |
|
| 98 | - * @return \Illuminate\Http\Response |
|
| 97 | + * @param integer $desc |
|
| 98 | + * @return \Illuminate\Http\JsonResponse|null |
|
| 99 | 99 | */ |
| 100 | 100 | public function search($query = '', $perPage = 15, $sortBy = 'created_at', $desc = 1) |
| 101 | 101 | { |
@@ -112,8 +112,8 @@ discard block |
||
| 112 | 112 | * |
| 113 | 113 | * @param \Illuminate\Http\Request $request |
| 114 | 114 | * @param string $sortBy |
| 115 | - * @param boolean $desc |
|
| 116 | - * @return \Illuminate\Http\Response |
|
| 115 | + * @param integer $desc |
|
| 116 | + * @return \Illuminate\Http\JsonResponse|null |
|
| 117 | 117 | */ |
| 118 | 118 | public function findby(Request $request, $sortBy = 'created_at', $desc = 1) |
| 119 | 119 | { |
@@ -129,7 +129,7 @@ discard block |
||
| 129 | 129 | * condition. |
| 130 | 130 | * |
| 131 | 131 | * @param \Illuminate\Http\Request $request |
| 132 | - * @return \Illuminate\Http\Response |
|
| 132 | + * @return \Illuminate\Http\JsonResponse|null |
|
| 133 | 133 | */ |
| 134 | 134 | public function first(Request $request) |
| 135 | 135 | { |
@@ -145,8 +145,8 @@ discard block |
||
| 145 | 145 | * |
| 146 | 146 | * @param integer $perPage |
| 147 | 147 | * @param string $sortBy |
| 148 | - * @param boolean $desc |
|
| 149 | - * @return \Illuminate\Http\Response |
|
| 148 | + * @param integer $desc |
|
| 149 | + * @return \Illuminate\Http\JsonResponse|null |
|
| 150 | 150 | */ |
| 151 | 151 | public function paginate($perPage = 15, $sortBy = 'created_at', $desc = 1) |
| 152 | 152 | { |
@@ -164,8 +164,8 @@ discard block |
||
| 164 | 164 | * @param \Illuminate\Http\Request $request |
| 165 | 165 | * @param integer $perPage |
| 166 | 166 | * @param string $sortBy |
| 167 | - * @param boolean $desc |
|
| 168 | - * @return \Illuminate\Http\Response |
|
| 167 | + * @param integer $desc |
|
| 168 | + * @return \Illuminate\Http\JsonResponse|null |
|
| 169 | 169 | */ |
| 170 | 170 | public function paginateby(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) |
| 171 | 171 | { |
@@ -180,7 +180,7 @@ discard block |
||
| 180 | 180 | * Save the given model to repository. |
| 181 | 181 | * |
| 182 | 182 | * @param \Illuminate\Http\Request $request |
| 183 | - * @return \Illuminate\Http\Response |
|
| 183 | + * @return \Illuminate\Http\JsonResponse|null |
|
| 184 | 184 | */ |
| 185 | 185 | public function save(Request $request) |
| 186 | 186 | { |
@@ -213,7 +213,7 @@ discard block |
||
| 213 | 213 | * Delete by the given id from model repository. |
| 214 | 214 | * |
| 215 | 215 | * @param integer $id |
| 216 | - * @return \Illuminate\Http\Response |
|
| 216 | + * @return \Illuminate\Http\JsonResponse|null |
|
| 217 | 217 | */ |
| 218 | 218 | public function delete($id) |
| 219 | 219 | { |
@@ -229,8 +229,8 @@ discard block |
||
| 229 | 229 | * @param \Illuminate\Http\Request $request |
| 230 | 230 | * @param integer $perPage |
| 231 | 231 | * @param string $sortBy |
| 232 | - * @param boolean $desc |
|
| 233 | - * @return \Illuminate\Http\Response |
|
| 232 | + * @param integer $desc |
|
| 233 | + * @return \Illuminate\Http\JsonResponse |
|
| 234 | 234 | */ |
| 235 | 235 | public function deleted(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) |
| 236 | 236 | { |
@@ -241,7 +241,7 @@ discard block |
||
| 241 | 241 | * Restore the deleted model. |
| 242 | 242 | * |
| 243 | 243 | * @param integer $id |
| 244 | - * @return \Illuminate\Http\Response |
|
| 244 | + * @return \Illuminate\Http\JsonResponse|null |
|
| 245 | 245 | */ |
| 246 | 246 | public function restore($id) |
| 247 | 247 | { |
@@ -6,272 +6,272 @@ |
||
| 6 | 6 | |
| 7 | 7 | class BaseApiController extends Controller |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * The model implementation. |
|
| 11 | - * |
|
| 12 | - * @var model |
|
| 13 | - */ |
|
| 14 | - protected $model; |
|
| 9 | + /** |
|
| 10 | + * The model implementation. |
|
| 11 | + * |
|
| 12 | + * @var model |
|
| 13 | + */ |
|
| 14 | + protected $model; |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * The config implementation. |
|
| 18 | - * |
|
| 19 | - * @var config |
|
| 20 | - */ |
|
| 21 | - protected $config; |
|
| 16 | + /** |
|
| 17 | + * The config implementation. |
|
| 18 | + * |
|
| 19 | + * @var config |
|
| 20 | + */ |
|
| 21 | + protected $config; |
|
| 22 | 22 | |
| 23 | - public function __construct() |
|
| 24 | - { |
|
| 25 | - \Session::set('timeZoneDiff', \Request::header('time-zone-diff') ?: 0); |
|
| 23 | + public function __construct() |
|
| 24 | + { |
|
| 25 | + \Session::set('timeZoneDiff', \Request::header('time-zone-diff') ?: 0); |
|
| 26 | 26 | |
| 27 | - $locale = \Request::header('locale'); |
|
| 28 | - switch ($locale) |
|
| 29 | - { |
|
| 30 | - case 'en': |
|
| 31 | - \App::setLocale('en'); |
|
| 32 | - \Session::set('locale', 'en'); |
|
| 33 | - break; |
|
| 27 | + $locale = \Request::header('locale'); |
|
| 28 | + switch ($locale) |
|
| 29 | + { |
|
| 30 | + case 'en': |
|
| 31 | + \App::setLocale('en'); |
|
| 32 | + \Session::set('locale', 'en'); |
|
| 33 | + break; |
|
| 34 | 34 | |
| 35 | - case 'ar': |
|
| 36 | - \App::setLocale('ar'); |
|
| 37 | - \Session::set('locale', 'ar'); |
|
| 38 | - break; |
|
| 35 | + case 'ar': |
|
| 36 | + \App::setLocale('ar'); |
|
| 37 | + \Session::set('locale', 'ar'); |
|
| 38 | + break; |
|
| 39 | 39 | |
| 40 | - case 'all': |
|
| 41 | - \App::setLocale('en'); |
|
| 42 | - \Session::set('locale', 'all'); |
|
| 43 | - break; |
|
| 40 | + case 'all': |
|
| 41 | + \App::setLocale('en'); |
|
| 42 | + \Session::set('locale', 'all'); |
|
| 43 | + break; |
|
| 44 | 44 | |
| 45 | - default: |
|
| 46 | - \App::setLocale('en'); |
|
| 47 | - \Session::set('locale', 'en'); |
|
| 48 | - break; |
|
| 49 | - } |
|
| 45 | + default: |
|
| 46 | + \App::setLocale('en'); |
|
| 47 | + \Session::set('locale', 'en'); |
|
| 48 | + break; |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - $this->config = \CoreConfig::getConfig(); |
|
| 52 | - $this->model = property_exists($this, 'model') ? $this->model : false; |
|
| 53 | - $this->validationRules = property_exists($this, 'validationRules') ? $this->validationRules : false; |
|
| 54 | - $this->skipPermissionCheck = property_exists($this, 'skipPermissionCheck') ? $this->skipPermissionCheck : []; |
|
| 55 | - $this->skipLoginCheck = property_exists($this, 'skipLoginCheck') ? $this->skipLoginCheck : []; |
|
| 56 | - $this->relations = array_key_exists($this->model, $this->config['relations']) ? $this->config['relations'][$this->model] : false; |
|
| 57 | - $route = explode('@',\Route::currentRouteAction())[1]; |
|
| 58 | - $this->checkPermission($route); |
|
| 59 | - } |
|
| 51 | + $this->config = \CoreConfig::getConfig(); |
|
| 52 | + $this->model = property_exists($this, 'model') ? $this->model : false; |
|
| 53 | + $this->validationRules = property_exists($this, 'validationRules') ? $this->validationRules : false; |
|
| 54 | + $this->skipPermissionCheck = property_exists($this, 'skipPermissionCheck') ? $this->skipPermissionCheck : []; |
|
| 55 | + $this->skipLoginCheck = property_exists($this, 'skipLoginCheck') ? $this->skipLoginCheck : []; |
|
| 56 | + $this->relations = array_key_exists($this->model, $this->config['relations']) ? $this->config['relations'][$this->model] : false; |
|
| 57 | + $route = explode('@',\Route::currentRouteAction())[1]; |
|
| 58 | + $this->checkPermission($route); |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * Fetch all records with relations from model repository. |
|
| 63 | - * |
|
| 64 | - * @return \Illuminate\Http\Response |
|
| 65 | - */ |
|
| 66 | - public function index() |
|
| 67 | - { |
|
| 68 | - if ($this->model) |
|
| 69 | - { |
|
| 70 | - $relations = $this->relations && $this->relations['all'] ? $this->relations['all'] : []; |
|
| 71 | - return \Response::json(call_user_func_array("\Core::{$this->model}", [])->all($relations), 200); |
|
| 72 | - } |
|
| 73 | - } |
|
| 61 | + /** |
|
| 62 | + * Fetch all records with relations from model repository. |
|
| 63 | + * |
|
| 64 | + * @return \Illuminate\Http\Response |
|
| 65 | + */ |
|
| 66 | + public function index() |
|
| 67 | + { |
|
| 68 | + if ($this->model) |
|
| 69 | + { |
|
| 70 | + $relations = $this->relations && $this->relations['all'] ? $this->relations['all'] : []; |
|
| 71 | + return \Response::json(call_user_func_array("\Core::{$this->model}", [])->all($relations), 200); |
|
| 72 | + } |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * Fetch the single object with relations from model repository. |
|
| 77 | - * |
|
| 78 | - * @param integer $id |
|
| 79 | - * @return \Illuminate\Http\Response |
|
| 80 | - */ |
|
| 81 | - public function find($id) |
|
| 82 | - { |
|
| 83 | - if ($this->model) |
|
| 84 | - { |
|
| 85 | - $relations = $this->relations && $this->relations['find'] ? $this->relations['find'] : []; |
|
| 86 | - return \Response::json(call_user_func_array("\Core::{$this->model}", [])->find($id, $relations), 200); |
|
| 87 | - } |
|
| 88 | - } |
|
| 75 | + /** |
|
| 76 | + * Fetch the single object with relations from model repository. |
|
| 77 | + * |
|
| 78 | + * @param integer $id |
|
| 79 | + * @return \Illuminate\Http\Response |
|
| 80 | + */ |
|
| 81 | + public function find($id) |
|
| 82 | + { |
|
| 83 | + if ($this->model) |
|
| 84 | + { |
|
| 85 | + $relations = $this->relations && $this->relations['find'] ? $this->relations['find'] : []; |
|
| 86 | + return \Response::json(call_user_func_array("\Core::{$this->model}", [])->find($id, $relations), 200); |
|
| 87 | + } |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - /** |
|
| 91 | - * Paginate all records with relations from model repository |
|
| 92 | - * that matche the given query. |
|
| 93 | - * |
|
| 94 | - * @param string $query |
|
| 95 | - * @param integer $perPage |
|
| 96 | - * @param string $sortBy |
|
| 97 | - * @param boolean $desc |
|
| 98 | - * @return \Illuminate\Http\Response |
|
| 99 | - */ |
|
| 100 | - public function search($query = '', $perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
| 101 | - { |
|
| 102 | - if ($this->model) |
|
| 103 | - { |
|
| 104 | - $relations = $this->relations && $this->relations['search'] ? $this->relations['search'] : []; |
|
| 105 | - return \Response::json(call_user_func_array("\Core::{$this->model}", [])->search($query, $perPage, $relations, $sortBy, $desc), 200); |
|
| 106 | - } |
|
| 107 | - } |
|
| 90 | + /** |
|
| 91 | + * Paginate all records with relations from model repository |
|
| 92 | + * that matche the given query. |
|
| 93 | + * |
|
| 94 | + * @param string $query |
|
| 95 | + * @param integer $perPage |
|
| 96 | + * @param string $sortBy |
|
| 97 | + * @param boolean $desc |
|
| 98 | + * @return \Illuminate\Http\Response |
|
| 99 | + */ |
|
| 100 | + public function search($query = '', $perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
| 101 | + { |
|
| 102 | + if ($this->model) |
|
| 103 | + { |
|
| 104 | + $relations = $this->relations && $this->relations['search'] ? $this->relations['search'] : []; |
|
| 105 | + return \Response::json(call_user_func_array("\Core::{$this->model}", [])->search($query, $perPage, $relations, $sortBy, $desc), 200); |
|
| 106 | + } |
|
| 107 | + } |
|
| 108 | 108 | |
| 109 | - /** |
|
| 110 | - * Fetch records from the storage based on the given |
|
| 111 | - * condition. |
|
| 112 | - * |
|
| 113 | - * @param \Illuminate\Http\Request $request |
|
| 114 | - * @param string $sortBy |
|
| 115 | - * @param boolean $desc |
|
| 116 | - * @return \Illuminate\Http\Response |
|
| 117 | - */ |
|
| 118 | - public function findby(Request $request, $sortBy = 'created_at', $desc = 1) |
|
| 119 | - { |
|
| 120 | - if ($this->model) |
|
| 121 | - { |
|
| 122 | - $relations = $this->relations && $this->relations['findBy'] ? $this->relations['findBy'] : []; |
|
| 123 | - return \Response::json(call_user_func_array("\Core::{$this->model}", [])->findBy($request->all(), $relations, $sortBy, $desc), 200); |
|
| 124 | - } |
|
| 125 | - } |
|
| 109 | + /** |
|
| 110 | + * Fetch records from the storage based on the given |
|
| 111 | + * condition. |
|
| 112 | + * |
|
| 113 | + * @param \Illuminate\Http\Request $request |
|
| 114 | + * @param string $sortBy |
|
| 115 | + * @param boolean $desc |
|
| 116 | + * @return \Illuminate\Http\Response |
|
| 117 | + */ |
|
| 118 | + public function findby(Request $request, $sortBy = 'created_at', $desc = 1) |
|
| 119 | + { |
|
| 120 | + if ($this->model) |
|
| 121 | + { |
|
| 122 | + $relations = $this->relations && $this->relations['findBy'] ? $this->relations['findBy'] : []; |
|
| 123 | + return \Response::json(call_user_func_array("\Core::{$this->model}", [])->findBy($request->all(), $relations, $sortBy, $desc), 200); |
|
| 124 | + } |
|
| 125 | + } |
|
| 126 | 126 | |
| 127 | - /** |
|
| 128 | - * Fetch the first record from the storage based on the given |
|
| 129 | - * condition. |
|
| 130 | - * |
|
| 131 | - * @param \Illuminate\Http\Request $request |
|
| 132 | - * @return \Illuminate\Http\Response |
|
| 133 | - */ |
|
| 134 | - public function first(Request $request) |
|
| 135 | - { |
|
| 136 | - if ($this->model) |
|
| 137 | - { |
|
| 138 | - $relations = $this->relations && $this->relations['first'] ? $this->relations['first'] : []; |
|
| 139 | - return \Response::json(call_user_func_array("\Core::{$this->model}", [])->first($request->all(), $relations), 200); |
|
| 140 | - } |
|
| 141 | - } |
|
| 127 | + /** |
|
| 128 | + * Fetch the first record from the storage based on the given |
|
| 129 | + * condition. |
|
| 130 | + * |
|
| 131 | + * @param \Illuminate\Http\Request $request |
|
| 132 | + * @return \Illuminate\Http\Response |
|
| 133 | + */ |
|
| 134 | + public function first(Request $request) |
|
| 135 | + { |
|
| 136 | + if ($this->model) |
|
| 137 | + { |
|
| 138 | + $relations = $this->relations && $this->relations['first'] ? $this->relations['first'] : []; |
|
| 139 | + return \Response::json(call_user_func_array("\Core::{$this->model}", [])->first($request->all(), $relations), 200); |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | 142 | |
| 143 | - /** |
|
| 144 | - * Paginate all records with relations from model repository. |
|
| 145 | - * |
|
| 146 | - * @param integer $perPage |
|
| 147 | - * @param string $sortBy |
|
| 148 | - * @param boolean $desc |
|
| 149 | - * @return \Illuminate\Http\Response |
|
| 150 | - */ |
|
| 151 | - public function paginate($perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
| 152 | - { |
|
| 153 | - if ($this->model) |
|
| 154 | - { |
|
| 155 | - $relations = $this->relations && $this->relations['paginate'] ? $this->relations['paginate'] : []; |
|
| 156 | - return \Response::json(call_user_func_array("\Core::{$this->model}", [])->paginate($perPage, $relations, $sortBy, $desc), 200); |
|
| 157 | - } |
|
| 158 | - } |
|
| 143 | + /** |
|
| 144 | + * Paginate all records with relations from model repository. |
|
| 145 | + * |
|
| 146 | + * @param integer $perPage |
|
| 147 | + * @param string $sortBy |
|
| 148 | + * @param boolean $desc |
|
| 149 | + * @return \Illuminate\Http\Response |
|
| 150 | + */ |
|
| 151 | + public function paginate($perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
| 152 | + { |
|
| 153 | + if ($this->model) |
|
| 154 | + { |
|
| 155 | + $relations = $this->relations && $this->relations['paginate'] ? $this->relations['paginate'] : []; |
|
| 156 | + return \Response::json(call_user_func_array("\Core::{$this->model}", [])->paginate($perPage, $relations, $sortBy, $desc), 200); |
|
| 157 | + } |
|
| 158 | + } |
|
| 159 | 159 | |
| 160 | - /** |
|
| 161 | - * Fetch all records with relations based on |
|
| 162 | - * the given condition from storage in pages. |
|
| 163 | - * |
|
| 164 | - * @param \Illuminate\Http\Request $request |
|
| 165 | - * @param integer $perPage |
|
| 166 | - * @param string $sortBy |
|
| 167 | - * @param boolean $desc |
|
| 168 | - * @return \Illuminate\Http\Response |
|
| 169 | - */ |
|
| 170 | - public function paginateby(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
| 171 | - { |
|
| 172 | - if ($this->model) |
|
| 173 | - { |
|
| 174 | - $relations = $this->relations && $this->relations['paginateBy'] ? $this->relations['paginateBy'] : []; |
|
| 175 | - return \Response::json(call_user_func_array("\Core::{$this->model}", [])->paginateBy($request->all(), $perPage, $relations, $sortBy, $desc), 200); |
|
| 176 | - } |
|
| 177 | - } |
|
| 160 | + /** |
|
| 161 | + * Fetch all records with relations based on |
|
| 162 | + * the given condition from storage in pages. |
|
| 163 | + * |
|
| 164 | + * @param \Illuminate\Http\Request $request |
|
| 165 | + * @param integer $perPage |
|
| 166 | + * @param string $sortBy |
|
| 167 | + * @param boolean $desc |
|
| 168 | + * @return \Illuminate\Http\Response |
|
| 169 | + */ |
|
| 170 | + public function paginateby(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
| 171 | + { |
|
| 172 | + if ($this->model) |
|
| 173 | + { |
|
| 174 | + $relations = $this->relations && $this->relations['paginateBy'] ? $this->relations['paginateBy'] : []; |
|
| 175 | + return \Response::json(call_user_func_array("\Core::{$this->model}", [])->paginateBy($request->all(), $perPage, $relations, $sortBy, $desc), 200); |
|
| 176 | + } |
|
| 177 | + } |
|
| 178 | 178 | |
| 179 | - /** |
|
| 180 | - * Save the given model to repository. |
|
| 181 | - * |
|
| 182 | - * @param \Illuminate\Http\Request $request |
|
| 183 | - * @return \Illuminate\Http\Response |
|
| 184 | - */ |
|
| 185 | - public function save(Request $request) |
|
| 186 | - { |
|
| 187 | - foreach ($this->validationRules as &$rule) |
|
| 188 | - { |
|
| 189 | - if (strpos($rule, 'exists') && ! strpos($rule, 'deleted_at,NULL')) |
|
| 190 | - { |
|
| 191 | - $rule .= ',deleted_at,NULL'; |
|
| 192 | - } |
|
| 179 | + /** |
|
| 180 | + * Save the given model to repository. |
|
| 181 | + * |
|
| 182 | + * @param \Illuminate\Http\Request $request |
|
| 183 | + * @return \Illuminate\Http\Response |
|
| 184 | + */ |
|
| 185 | + public function save(Request $request) |
|
| 186 | + { |
|
| 187 | + foreach ($this->validationRules as &$rule) |
|
| 188 | + { |
|
| 189 | + if (strpos($rule, 'exists') && ! strpos($rule, 'deleted_at,NULL')) |
|
| 190 | + { |
|
| 191 | + $rule .= ',deleted_at,NULL'; |
|
| 192 | + } |
|
| 193 | 193 | |
| 194 | - if ($request->has('id')) |
|
| 195 | - { |
|
| 196 | - $rule = str_replace('{id}', $request->get('id'), $rule); |
|
| 197 | - } |
|
| 198 | - else |
|
| 199 | - { |
|
| 200 | - $rule = str_replace(',{id}', '', $rule); |
|
| 201 | - } |
|
| 202 | - } |
|
| 194 | + if ($request->has('id')) |
|
| 195 | + { |
|
| 196 | + $rule = str_replace('{id}', $request->get('id'), $rule); |
|
| 197 | + } |
|
| 198 | + else |
|
| 199 | + { |
|
| 200 | + $rule = str_replace(',{id}', '', $rule); |
|
| 201 | + } |
|
| 202 | + } |
|
| 203 | 203 | |
| 204 | - $this->validate($request, $this->validationRules); |
|
| 204 | + $this->validate($request, $this->validationRules); |
|
| 205 | 205 | |
| 206 | - if ($this->model) |
|
| 207 | - { |
|
| 208 | - return \Response::json(call_user_func_array("\Core::{$this->model}", [])->save($request->all()), 200); |
|
| 209 | - } |
|
| 210 | - } |
|
| 206 | + if ($this->model) |
|
| 207 | + { |
|
| 208 | + return \Response::json(call_user_func_array("\Core::{$this->model}", [])->save($request->all()), 200); |
|
| 209 | + } |
|
| 210 | + } |
|
| 211 | 211 | |
| 212 | - /** |
|
| 213 | - * Delete by the given id from model repository. |
|
| 214 | - * |
|
| 215 | - * @param integer $id |
|
| 216 | - * @return \Illuminate\Http\Response |
|
| 217 | - */ |
|
| 218 | - public function delete($id) |
|
| 219 | - { |
|
| 220 | - if ($this->model) |
|
| 221 | - { |
|
| 222 | - return \Response::json(call_user_func_array("\Core::{$this->model}", [])->delete($id), 200); |
|
| 223 | - } |
|
| 224 | - } |
|
| 212 | + /** |
|
| 213 | + * Delete by the given id from model repository. |
|
| 214 | + * |
|
| 215 | + * @param integer $id |
|
| 216 | + * @return \Illuminate\Http\Response |
|
| 217 | + */ |
|
| 218 | + public function delete($id) |
|
| 219 | + { |
|
| 220 | + if ($this->model) |
|
| 221 | + { |
|
| 222 | + return \Response::json(call_user_func_array("\Core::{$this->model}", [])->delete($id), 200); |
|
| 223 | + } |
|
| 224 | + } |
|
| 225 | 225 | |
| 226 | - /** |
|
| 227 | - * Return the deleted models in pages based on the given conditions. |
|
| 228 | - * |
|
| 229 | - * @param \Illuminate\Http\Request $request |
|
| 230 | - * @param integer $perPage |
|
| 231 | - * @param string $sortBy |
|
| 232 | - * @param boolean $desc |
|
| 233 | - * @return \Illuminate\Http\Response |
|
| 234 | - */ |
|
| 235 | - public function deleted(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
| 236 | - { |
|
| 237 | - return \Response::json(call_user_func_array("\Core::{$this->model}", [])->deleted($request->all(), $perPage, $sortBy, $desc), 200); |
|
| 238 | - } |
|
| 226 | + /** |
|
| 227 | + * Return the deleted models in pages based on the given conditions. |
|
| 228 | + * |
|
| 229 | + * @param \Illuminate\Http\Request $request |
|
| 230 | + * @param integer $perPage |
|
| 231 | + * @param string $sortBy |
|
| 232 | + * @param boolean $desc |
|
| 233 | + * @return \Illuminate\Http\Response |
|
| 234 | + */ |
|
| 235 | + public function deleted(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) |
|
| 236 | + { |
|
| 237 | + return \Response::json(call_user_func_array("\Core::{$this->model}", [])->deleted($request->all(), $perPage, $sortBy, $desc), 200); |
|
| 238 | + } |
|
| 239 | 239 | |
| 240 | - /** |
|
| 241 | - * Restore the deleted model. |
|
| 242 | - * |
|
| 243 | - * @param integer $id |
|
| 244 | - * @return \Illuminate\Http\Response |
|
| 245 | - */ |
|
| 246 | - public function restore($id) |
|
| 247 | - { |
|
| 248 | - if ($this->model) |
|
| 249 | - { |
|
| 250 | - return \Response::json(call_user_func_array("\Core::{$this->model}", [])->restore($id), 200); |
|
| 251 | - } |
|
| 252 | - } |
|
| 240 | + /** |
|
| 241 | + * Restore the deleted model. |
|
| 242 | + * |
|
| 243 | + * @param integer $id |
|
| 244 | + * @return \Illuminate\Http\Response |
|
| 245 | + */ |
|
| 246 | + public function restore($id) |
|
| 247 | + { |
|
| 248 | + if ($this->model) |
|
| 249 | + { |
|
| 250 | + return \Response::json(call_user_func_array("\Core::{$this->model}", [])->restore($id), 200); |
|
| 251 | + } |
|
| 252 | + } |
|
| 253 | 253 | |
| 254 | - /** |
|
| 255 | - * Check if the logged in user can do the given permission. |
|
| 256 | - * |
|
| 257 | - * @param string $permission |
|
| 258 | - * @return void |
|
| 259 | - */ |
|
| 260 | - private function checkPermission($permission) |
|
| 261 | - { |
|
| 262 | - $permission = $permission !== 'index' ? $permission : 'list'; |
|
| 263 | - if ( ! in_array($permission, $this->skipLoginCheck)) |
|
| 264 | - { |
|
| 265 | - $user = \Core::users()->find(\JWTAuth::parseToken()->authenticate()->id); |
|
| 266 | - if ($user->blocked) |
|
| 267 | - { |
|
| 268 | - \ErrorHandler::userIsBlocked(); |
|
| 269 | - } |
|
| 254 | + /** |
|
| 255 | + * Check if the logged in user can do the given permission. |
|
| 256 | + * |
|
| 257 | + * @param string $permission |
|
| 258 | + * @return void |
|
| 259 | + */ |
|
| 260 | + private function checkPermission($permission) |
|
| 261 | + { |
|
| 262 | + $permission = $permission !== 'index' ? $permission : 'list'; |
|
| 263 | + if ( ! in_array($permission, $this->skipLoginCheck)) |
|
| 264 | + { |
|
| 265 | + $user = \Core::users()->find(\JWTAuth::parseToken()->authenticate()->id); |
|
| 266 | + if ($user->blocked) |
|
| 267 | + { |
|
| 268 | + \ErrorHandler::userIsBlocked(); |
|
| 269 | + } |
|
| 270 | 270 | |
| 271 | - if ( ! in_array($permission, $this->skipPermissionCheck) && ! \Core::users()->can($permission, $this->model)) |
|
| 272 | - { |
|
| 273 | - \ErrorHandler::noPermissions(); |
|
| 274 | - } |
|
| 275 | - } |
|
| 276 | - } |
|
| 271 | + if ( ! in_array($permission, $this->skipPermissionCheck) && ! \Core::users()->can($permission, $this->model)) |
|
| 272 | + { |
|
| 273 | + \ErrorHandler::noPermissions(); |
|
| 274 | + } |
|
| 275 | + } |
|
| 276 | + } |
|
| 277 | 277 | } |
@@ -2,25 +2,25 @@ |
||
| 2 | 2 | |
| 3 | 3 | return [ |
| 4 | 4 | |
| 5 | - /** |
|
| 6 | - * List of error messages used in core module. |
|
| 7 | - */ |
|
| 8 | - 'unAuthorized' => 'من فضلك قم بتسجيل الدخول', |
|
| 9 | - 'tokenExpired' => 'انتهت صلاحية الدخول', |
|
| 10 | - 'noPermissions' => 'لا توجد صلاحية', |
|
| 11 | - 'loginFailed' => 'خطأ في البريد لاكتروني او كلمة المرور', |
|
| 12 | - 'noSocialEmail' => 'لا يمكن الحصول علي تابريد الاكتروني', |
|
| 13 | - 'userAlreadyRegistered' => 'المستخد مسجل بالفعل.سجل الدخول بالبريد الاكتروني و كلمة السر', |
|
| 14 | - 'connectionError' => 'خطأ في الاتصال', |
|
| 15 | - 'redisNotRunning' => 'سيرفير الاشعارات لايعمل', |
|
| 16 | - 'dbQueryError' => 'خطا في البيانات', |
|
| 17 | - 'cannotCreateSetting' => 'لا يمكن اضافة اعدادات', |
|
| 18 | - 'cannotUpdateSettingKey' => 'لا يمكن تعديل اعدادات', |
|
| 19 | - 'userIsBlocked' => 'لقد تم حظرك', |
|
| 20 | - 'invalidResetToken' => 'رمز تعديل كلمة المرور خطا', |
|
| 21 | - 'invalidResetPassword' => 'خطا في نعديل كلمة المرور', |
|
| 22 | - 'invalidOldPassword' => 'كلمة السر القديمه خطا', |
|
| 23 | - 'notFound' => 'ال :replace المطلوب غير موجود', |
|
| 24 | - 'generalError' => 'حدث خطا ما', |
|
| 5 | + /** |
|
| 6 | + * List of error messages used in core module. |
|
| 7 | + */ |
|
| 8 | + 'unAuthorized' => 'من فضلك قم بتسجيل الدخول', |
|
| 9 | + 'tokenExpired' => 'انتهت صلاحية الدخول', |
|
| 10 | + 'noPermissions' => 'لا توجد صلاحية', |
|
| 11 | + 'loginFailed' => 'خطأ في البريد لاكتروني او كلمة المرور', |
|
| 12 | + 'noSocialEmail' => 'لا يمكن الحصول علي تابريد الاكتروني', |
|
| 13 | + 'userAlreadyRegistered' => 'المستخد مسجل بالفعل.سجل الدخول بالبريد الاكتروني و كلمة السر', |
|
| 14 | + 'connectionError' => 'خطأ في الاتصال', |
|
| 15 | + 'redisNotRunning' => 'سيرفير الاشعارات لايعمل', |
|
| 16 | + 'dbQueryError' => 'خطا في البيانات', |
|
| 17 | + 'cannotCreateSetting' => 'لا يمكن اضافة اعدادات', |
|
| 18 | + 'cannotUpdateSettingKey' => 'لا يمكن تعديل اعدادات', |
|
| 19 | + 'userIsBlocked' => 'لقد تم حظرك', |
|
| 20 | + 'invalidResetToken' => 'رمز تعديل كلمة المرور خطا', |
|
| 21 | + 'invalidResetPassword' => 'خطا في نعديل كلمة المرور', |
|
| 22 | + 'invalidOldPassword' => 'كلمة السر القديمه خطا', |
|
| 23 | + 'notFound' => 'ال :replace المطلوب غير موجود', |
|
| 24 | + 'generalError' => 'حدث خطا ما', |
|
| 25 | 25 | |
| 26 | 26 | ]; |
| 27 | 27 | \ No newline at end of file |
@@ -2,25 +2,25 @@ |
||
| 2 | 2 | |
| 3 | 3 | return [ |
| 4 | 4 | |
| 5 | - /** |
|
| 6 | - * List of error messages used in core module. |
|
| 7 | - */ |
|
| 8 | - 'unAuthorized' => 'Please login before any action', |
|
| 9 | - 'tokenExpired' => 'Login token expired', |
|
| 10 | - 'noPermissions' => 'No permissions', |
|
| 11 | - 'loginFailed' => 'Wrong mail or password', |
|
| 12 | - 'noSocialEmail' => 'Couldn\'t retrieve email', |
|
| 13 | - 'userAlreadyRegistered' => 'User already registered. Please login using email and password', |
|
| 14 | - 'connectionError' => 'Connection error', |
|
| 15 | - 'redisNotRunning' => 'Your redis notification server is\'t running', |
|
| 16 | - 'dbQueryError' => 'Please check the given inputes', |
|
| 17 | - 'cannotCreateSetting' => 'Can\'t create setting', |
|
| 18 | - 'cannotUpdateSettingKey' => 'Can\'t update setting key', |
|
| 19 | - 'userIsBlocked' => 'You have been blocked', |
|
| 20 | - 'invalidResetToken' => 'Reset password token is invalid', |
|
| 21 | - 'invalidResetPassword' => 'Reset password is invalid', |
|
| 22 | - 'invalidOldPassword' => 'Old password is invalid', |
|
| 23 | - 'notFound' => 'The requested :replace not found', |
|
| 24 | - 'generalError' => 'Something went wrong', |
|
| 5 | + /** |
|
| 6 | + * List of error messages used in core module. |
|
| 7 | + */ |
|
| 8 | + 'unAuthorized' => 'Please login before any action', |
|
| 9 | + 'tokenExpired' => 'Login token expired', |
|
| 10 | + 'noPermissions' => 'No permissions', |
|
| 11 | + 'loginFailed' => 'Wrong mail or password', |
|
| 12 | + 'noSocialEmail' => 'Couldn\'t retrieve email', |
|
| 13 | + 'userAlreadyRegistered' => 'User already registered. Please login using email and password', |
|
| 14 | + 'connectionError' => 'Connection error', |
|
| 15 | + 'redisNotRunning' => 'Your redis notification server is\'t running', |
|
| 16 | + 'dbQueryError' => 'Please check the given inputes', |
|
| 17 | + 'cannotCreateSetting' => 'Can\'t create setting', |
|
| 18 | + 'cannotUpdateSettingKey' => 'Can\'t update setting key', |
|
| 19 | + 'userIsBlocked' => 'You have been blocked', |
|
| 20 | + 'invalidResetToken' => 'Reset password token is invalid', |
|
| 21 | + 'invalidResetPassword' => 'Reset password is invalid', |
|
| 22 | + 'invalidOldPassword' => 'Old password is invalid', |
|
| 23 | + 'notFound' => 'The requested :replace not found', |
|
| 24 | + 'generalError' => 'Something went wrong', |
|
| 25 | 25 | |
| 26 | 26 | ]; |
| 27 | 27 | \ No newline at end of file |
@@ -12,7 +12,7 @@ discard block |
||
| 12 | 12 | */ |
| 13 | 13 | public function up() |
| 14 | 14 | { |
| 15 | - DB::statement( "CREATE VIEW admin_count AS |
|
| 15 | + DB::statement("CREATE VIEW admin_count AS |
|
| 16 | 16 | select count(u.id) |
| 17 | 17 | from users u, groups g ,users_groups ug |
| 18 | 18 | where |
@@ -39,6 +39,6 @@ discard block |
||
| 39 | 39 | */ |
| 40 | 40 | public function down() |
| 41 | 41 | { |
| 42 | - DB::statement( "DROP VIEW IF EXISTS admin_count"); |
|
| 42 | + DB::statement("DROP VIEW IF EXISTS admin_count"); |
|
| 43 | 43 | } |
| 44 | 44 | } |
| 45 | 45 | \ No newline at end of file |
@@ -5,27 +5,27 @@ |
||
| 5 | 5 | |
| 6 | 6 | class PasswordResets extends Migration |
| 7 | 7 | { |
| 8 | - /** |
|
| 9 | - * Run the migrations. |
|
| 10 | - * |
|
| 11 | - * @return void |
|
| 12 | - */ |
|
| 13 | - public function up() |
|
| 14 | - { |
|
| 15 | - Schema::create('password_resets', function (Blueprint $table) { |
|
| 16 | - $table->string('email')->index(); |
|
| 17 | - $table->string('token')->index(); |
|
| 18 | - $table->timestamp('created_at'); |
|
| 19 | - }); |
|
| 20 | - } |
|
| 8 | + /** |
|
| 9 | + * Run the migrations. |
|
| 10 | + * |
|
| 11 | + * @return void |
|
| 12 | + */ |
|
| 13 | + public function up() |
|
| 14 | + { |
|
| 15 | + Schema::create('password_resets', function (Blueprint $table) { |
|
| 16 | + $table->string('email')->index(); |
|
| 17 | + $table->string('token')->index(); |
|
| 18 | + $table->timestamp('created_at'); |
|
| 19 | + }); |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Reverse the migrations. |
|
| 24 | - * |
|
| 25 | - * @return void |
|
| 26 | - */ |
|
| 27 | - public function down() |
|
| 28 | - { |
|
| 29 | - Schema::dropIfExists('password_resets'); |
|
| 30 | - } |
|
| 22 | + /** |
|
| 23 | + * Reverse the migrations. |
|
| 24 | + * |
|
| 25 | + * @return void |
|
| 26 | + */ |
|
| 27 | + public function down() |
|
| 28 | + { |
|
| 29 | + Schema::dropIfExists('password_resets'); |
|
| 30 | + } |
|
| 31 | 31 | } |
@@ -13,16 +13,16 @@ |
||
| 13 | 13 | public function up() |
| 14 | 14 | { |
| 15 | 15 | Schema::create('users', function (Blueprint $table) { |
| 16 | - $table->increments('id'); |
|
| 17 | - $table->string('name', 100)->nullable(); |
|
| 18 | - $table->string('email')->unique(); |
|
| 19 | - $table->string('password', 60)->nullable(); |
|
| 20 | - $table->boolean('blocked')->default(0); |
|
| 16 | + $table->increments('id'); |
|
| 17 | + $table->string('name', 100)->nullable(); |
|
| 18 | + $table->string('email')->unique(); |
|
| 19 | + $table->string('password', 60)->nullable(); |
|
| 20 | + $table->boolean('blocked')->default(0); |
|
| 21 | 21 | $table->date('last_change_password')->nullable(); |
| 22 | - $table->softDeletes(); |
|
| 23 | - $table->rememberToken(); |
|
| 24 | - $table->timestamps(); |
|
| 25 | - }); |
|
| 22 | + $table->softDeletes(); |
|
| 23 | + $table->rememberToken(); |
|
| 24 | + $table->timestamps(); |
|
| 25 | + }); |
|
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | /** |