Completed
Push — master ( 09a99f...0f3793 )
by Sherif
02:40
created
src/Modules/V1/Core/Utl/Logging.php 1 patch
Indentation   +13 added lines, -13 removed lines patch added patch discarded remove patch
@@ -2,17 +2,17 @@
 block discarded – undo
2 2
 
3 3
 class Logging
4 4
 {
5
-    public function saveLog($action, $item_name, $item_type, $item_id)
6
-    {
7
-    	if (\Core::logs() && $item_name !== 'Log')
8
-    	{
9
-    		\Core::logs()->save([
10
-	    		'action'      => $action,
11
-	    		'item_name'   => $item_name,
12
-	    		'item_type'   => $item_type,
13
-	    		'item_id'     => $item_id,
14
-	    		'user_id'     => \JWTAuth::parseToken()->authenticate()->id,
15
-	    		], false, false);
16
-    	}
17
-    }
5
+	public function saveLog($action, $item_name, $item_type, $item_id)
6
+	{
7
+		if (\Core::logs() && $item_name !== 'Log')
8
+		{
9
+			\Core::logs()->save([
10
+				'action'      => $action,
11
+				'item_name'   => $item_name,
12
+				'item_type'   => $item_type,
13
+				'item_id'     => $item_id,
14
+				'user_id'     => \JWTAuth::parseToken()->authenticate()->id,
15
+				], false, false);
16
+		}
17
+	}
18 18
 }
19 19
\ No newline at end of file
Please login to merge, or discard this patch.
src/Modules/V1/Reporting/Repositories/ReportRepository.php 1 patch
Indentation   +40 added lines, -40 removed lines patch added patch discarded remove patch
@@ -15,54 +15,54 @@
 block discarded – undo
15 15
 	}
16 16
 
17 17
 	/**
18
-     * Render the given report db view.
19
-     * 
20
-     * @param  integer $id
21
-     * @param  array   $relations
22
-     * @param  array   $columns
23
-     * @return object
24
-     */
25
-    public function find($id, $relations = [], $columns = array('*'))
26
-    {
18
+	 * Render the given report db view.
19
+	 * 
20
+	 * @param  integer $id
21
+	 * @param  array   $relations
22
+	 * @param  array   $columns
23
+	 * @return object
24
+	 */
25
+	public function find($id, $relations = [], $columns = array('*'))
26
+	{
27 27
 		$report = call_user_func_array("{$this->getModel()}::with", array($relations))->find($id, $columns);
28 28
 
29
-        if ( ! $report) 
30
-        {
31
-            \ErrorHandler::notFound('report');
32
-        }
29
+		if ( ! $report) 
30
+		{
31
+			\ErrorHandler::notFound('report');
32
+		}
33 33
 
34
-        if ( ! \Core::users()->can($report->view_name, 'reports'))
35
-        {
36
-            \ErrorHandler::noPermissions();
37
-        }
34
+		if ( ! \Core::users()->can($report->view_name, 'reports'))
35
+		{
36
+			\ErrorHandler::noPermissions();
37
+		}
38 38
 
39
-        return \DB::table($report->view_name)->get();
40
-    }
39
+		return \DB::table($report->view_name)->get();
40
+	}
41 41
 
42
-    /**
43
-     * Render the given report db view based on the given
44
-     * condition.
45
-     *
46
-     * @param  array   $conditions array of conditions
47
-     * @param  array   $relations
48
-     * @param  array   $colunmns
49
-     * @return object
50
-     */
51
-    public function first($conditions, $relations = [], $columns = array('*'))
52
-    {
42
+	/**
43
+	 * Render the given report db view based on the given
44
+	 * condition.
45
+	 *
46
+	 * @param  array   $conditions array of conditions
47
+	 * @param  array   $relations
48
+	 * @param  array   $colunmns
49
+	 * @return object
50
+	 */
51
+	public function first($conditions, $relations = [], $columns = array('*'))
52
+	{
53 53
 		$conditions = $this->constructConditions($conditions);
54 54
 		$report     = call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns);
55 55
         
56
-        if ( ! $report) 
57
-        {
58
-            \ErrorHandler::notFound('report');
59
-        }
56
+		if ( ! $report) 
57
+		{
58
+			\ErrorHandler::notFound('report');
59
+		}
60 60
         
61
-        if ( ! \Core::users()->can($report->view_name, 'reports'))
62
-        {
63
-            \ErrorHandler::noPermissions();
64
-        }
61
+		if ( ! \Core::users()->can($report->view_name, 'reports'))
62
+		{
63
+			\ErrorHandler::noPermissions();
64
+		}
65 65
 		
66
-        return \DB::table($report->view_name)->get();  
67
-    }
66
+		return \DB::table($report->view_name)->get();  
67
+	}
68 68
 }
Please login to merge, or discard this patch.
src/Modules/V1/Core/AbstractRepositories/AbstractRepository.php 1 patch
Indentation   +491 added lines, -491 removed lines patch added patch discarded remove patch
@@ -4,533 +4,533 @@
 block discarded – undo
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
-    }
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 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
-    }
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 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->getFillable();
61
-        $sort             = $desc ? 'desc' : 'asc';
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->getFillable();
61
+		$sort             = $desc ? 'desc' : 'asc';
62 62
 
63
-        /**
64
-         * Construct the select conditions for the model.
65
-         */
66
-        $model->where(function ($q) use ($query, $conditionColumns, $relations){
63
+		/**
64
+		 * Construct the select conditions for the model.
65
+		 */
66
+		$model->where(function ($q) use ($query, $conditionColumns, $relations){
67 67
 
68
-            /**
69
-             * Use the first element in the model columns to construct the first condition.
70
-             */
71
-            $q->where(\DB::raw('LOWER(' . array_shift($conditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%');
68
+			/**
69
+			 * Use the first element in the model columns to construct the first condition.
70
+			 */
71
+			$q->where(\DB::raw('LOWER(' . array_shift($conditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%');
72 72
 
73
-            /**
74
-             * Loop through the rest of the columns to construct or where conditions.
75
-             */
76
-            foreach ($conditionColumns as $column) 
77
-            {
78
-                $q->orWhere(\DB::raw('LOWER(' . $column . ')'), 'LIKE', '%' . strtolower($query) . '%');
79
-            }
73
+			/**
74
+			 * Loop through the rest of the columns to construct or where conditions.
75
+			 */
76
+			foreach ($conditionColumns as $column) 
77
+			{
78
+				$q->orWhere(\DB::raw('LOWER(' . $column . ')'), 'LIKE', '%' . strtolower($query) . '%');
79
+			}
80 80
 
81
-            /**
82
-             * Loop through the model relations.
83
-             */
84
-            foreach ($relations as $relation) 
85
-            {
86
-                /**
87
-                 * Remove the sub relation if exists.
88
-                 */
89
-                $relation = explode('.', $relation)[0];
81
+			/**
82
+			 * Loop through the model relations.
83
+			 */
84
+			foreach ($relations as $relation) 
85
+			{
86
+				/**
87
+				 * Remove the sub relation if exists.
88
+				 */
89
+				$relation = explode('.', $relation)[0];
90 90
 
91
-                /**
92
-                 * Try to fetch the relation repository from the core.
93
-                 */
94
-                if (\Core::$relation()) 
95
-                {
96
-                    /**
97
-                     * Construct the relation condition.
98
-                     */
99
-                    $q->orWhereHas($relation, function ($subModel) use ($query, $relation){
91
+				/**
92
+				 * Try to fetch the relation repository from the core.
93
+				 */
94
+				if (\Core::$relation()) 
95
+				{
96
+					/**
97
+					 * Construct the relation condition.
98
+					 */
99
+					$q->orWhereHas($relation, function ($subModel) use ($query, $relation){
100 100
 
101
-                        $subModel->where(function ($q) use ($query, $relation){
101
+						$subModel->where(function ($q) use ($query, $relation){
102 102
 
103
-                            /**
104
-                             * Get columns of the relation.
105
-                             */
106
-                            $subConditionColumns = \Core::$relation()->model->getFillable();
103
+							/**
104
+							 * Get columns of the relation.
105
+							 */
106
+							$subConditionColumns = \Core::$relation()->model->getFillable();
107 107
 
108
-                            /**
109
-                             * Use the first element in the relation model columns to construct the first condition.
110
-                             */
111
-                            $q->where(\DB::raw('LOWER(' . array_shift($subConditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%');
108
+							/**
109
+							 * Use the first element in the relation model columns to construct the first condition.
110
+							 */
111
+							$q->where(\DB::raw('LOWER(' . array_shift($subConditionColumns) . ')'), 'LIKE', '%' . strtolower($query) . '%');
112 112
 
113
-                            /**
114
-                             * Loop through the rest of the columns to construct or where conditions.
115
-                             */
116
-                            foreach ($subConditionColumns as $subConditionColumn)
117
-                            {
118
-                                $q->orWhere(\DB::raw('LOWER(' . $subConditionColumn . ')'), 'LIKE', '%' . strtolower($query) . '%');
119
-                            } 
120
-                        });
113
+							/**
114
+							 * Loop through the rest of the columns to construct or where conditions.
115
+							 */
116
+							foreach ($subConditionColumns as $subConditionColumn)
117
+							{
118
+								$q->orWhere(\DB::raw('LOWER(' . $subConditionColumn . ')'), 'LIKE', '%' . strtolower($query) . '%');
119
+							} 
120
+						});
121 121
 
122
-                    });
123
-                }
124
-            }
125
-        });
122
+					});
123
+				}
124
+			}
125
+		});
126 126
         
127
-        return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns);
128
-    }
127
+		return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns);
128
+	}
129 129
     
130
-    /**
131
-     * Fetch all records with relations from storage in pages.
132
-     * 
133
-     * @param  integer $perPage
134
-     * @param  array   $relations
135
-     * @param  string  $sortBy
136
-     * @param  boolean $desc
137
-     * @param  array   $columns
138
-     * @return collection
139
-     */
140
-    public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*'))
141
-    {
142
-        $sort = $desc ? 'desc' : 'asc';
143
-        return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->paginate($perPage, $columns);
144
-    }
130
+	/**
131
+	 * Fetch all records with relations from storage in pages.
132
+	 * 
133
+	 * @param  integer $perPage
134
+	 * @param  array   $relations
135
+	 * @param  string  $sortBy
136
+	 * @param  boolean $desc
137
+	 * @param  array   $columns
138
+	 * @return collection
139
+	 */
140
+	public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*'))
141
+	{
142
+		$sort = $desc ? 'desc' : 'asc';
143
+		return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->paginate($perPage, $columns);
144
+	}
145 145
 
146
-    /**
147
-     * Fetch all records with relations based on
148
-     * the given condition from storage in pages.
149
-     * 
150
-     * @param  array   $conditions array of conditions
151
-     * @param  integer $perPage
152
-     * @param  array   $relations
153
-     * @param  string  $sortBy
154
-     * @param  boolean $desc
155
-     * @param  array   $columns
156
-     * @return collection
157
-     */
158
-    public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*'))
159
-    {
160
-        unset($conditions['page']);
161
-        $conditions = $this->constructConditions($conditions);
162
-        $sort       = $desc ? 'desc' : 'asc';
163
-        return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns);
164
-    }
146
+	/**
147
+	 * Fetch all records with relations based on
148
+	 * the given condition from storage in pages.
149
+	 * 
150
+	 * @param  array   $conditions array of conditions
151
+	 * @param  integer $perPage
152
+	 * @param  array   $relations
153
+	 * @param  string  $sortBy
154
+	 * @param  boolean $desc
155
+	 * @param  array   $columns
156
+	 * @return collection
157
+	 */
158
+	public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*'))
159
+	{
160
+		unset($conditions['page']);
161
+		$conditions = $this->constructConditions($conditions);
162
+		$sort       = $desc ? 'desc' : 'asc';
163
+		return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns);
164
+	}
165 165
     
166
-    /**
167
-     * Save the given model to the storage.
168
-     * 
169
-     * @param  array   $data
170
-     * @param  boolean $saveLog
171
-     * @return object
172
-     */
173
-    public function save(array $data, $saveLog = true)
174
-    {
175
-        $model      = false;
176
-        $modelClass = $this->model;
177
-        $relations  = [];
178
-        $with       = [];
166
+	/**
167
+	 * Save the given model to the storage.
168
+	 * 
169
+	 * @param  array   $data
170
+	 * @param  boolean $saveLog
171
+	 * @return object
172
+	 */
173
+	public function save(array $data, $saveLog = true)
174
+	{
175
+		$model      = false;
176
+		$modelClass = $this->model;
177
+		$relations  = [];
178
+		$with       = [];
179 179
 
180
-        \DB::transaction(function () use (&$model, &$relations, &$with, $data, $saveLog, $modelClass) {
181
-            /**
182
-             * If the id is present in the data then select the model for updating,
183
-             * else create new model.
184
-             * @var array
185
-             */
186
-            $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass;
187
-            if ( ! $model) 
188
-            {
189
-                \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']);
190
-            }
180
+		\DB::transaction(function () use (&$model, &$relations, &$with, $data, $saveLog, $modelClass) {
181
+			/**
182
+			 * If the id is present in the data then select the model for updating,
183
+			 * else create new model.
184
+			 * @var array
185
+			 */
186
+			$model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass;
187
+			if ( ! $model) 
188
+			{
189
+				\ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']);
190
+			}
191 191
 
192
-            /**
193
-             * Construct the model object with the given data,
194
-             * and if there is a relation add it to relations array,
195
-             * then save the model.
196
-             */
197
-            foreach ($data as $key => $value) 
198
-            {
199
-                /**
200
-                 * If the attribute is a relation.
201
-                 */
202
-                $relation = camel_case($key);
203
-                if (method_exists($model, $relation))
204
-                {
205
-                    /**
206
-                     * Add the relation to the with array to eager load the created model with the relations.
207
-                     */
208
-                    $with[] = $relation;
192
+			/**
193
+			 * Construct the model object with the given data,
194
+			 * and if there is a relation add it to relations array,
195
+			 * then save the model.
196
+			 */
197
+			foreach ($data as $key => $value) 
198
+			{
199
+				/**
200
+				 * If the attribute is a relation.
201
+				 */
202
+				$relation = camel_case($key);
203
+				if (method_exists($model, $relation))
204
+				{
205
+					/**
206
+					 * Add the relation to the with array to eager load the created model with the relations.
207
+					 */
208
+					$with[] = $relation;
209 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;
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 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;
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 246
 
247
-                                /**
248
-                                 * If model doesn't exists.
249
-                                 */
250
-                                if ( ! $relationModel) 
251
-                                {
252
-                                    \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']);
253
-                                }
247
+								/**
248
+								 * If model doesn't exists.
249
+								 */
250
+								if ( ! $relationModel) 
251
+								{
252
+									\ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']);
253
+								}
254 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
-                                $relations[$relation][] = $relationModel;
269
-                            }
270
-                            /**
271
-                             * If not collection.
272
-                             */
273
-                            else
274
-                            {
275
-                                /**
276
-                                 * Prevent the sub relations.
277
-                                 */
278
-                                if (gettype($val) !== 'object' && gettype($val) !== 'array') 
279
-                                {
280
-                                    /**
281
-                                     * If the id is present in the data then select the relation model for updating,
282
-                                     * else create new model.
283
-                                     */
284
-                                    $relationModel = array_key_exists('id', $value) ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel;
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
+								$relations[$relation][] = $relationModel;
269
+							}
270
+							/**
271
+							 * If not collection.
272
+							 */
273
+							else
274
+							{
275
+								/**
276
+								 * Prevent the sub relations.
277
+								 */
278
+								if (gettype($val) !== 'object' && gettype($val) !== 'array') 
279
+								{
280
+									/**
281
+									 * If the id is present in the data then select the relation model for updating,
282
+									 * else create new model.
283
+									 */
284
+									$relationModel = array_key_exists('id', $value) ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel;
285 285
 
286
-                                    /**
287
-                                     * If model doesn't exists.
288
-                                     */
289
-                                    if ( ! $relationModel) 
290
-                                    {
291
-                                        \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']);
292
-                                    }
286
+									/**
287
+									 * If model doesn't exists.
288
+									 */
289
+									if ( ! $relationModel) 
290
+									{
291
+										\ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']);
292
+									}
293 293
 
294
-                                    /**
295
-                                     * Prevent attributes not in the fillable.
296
-                                     */
297
-                                    if (array_search($attr, $relationModel->getFillable(), true) !== false) 
298
-                                    {
299
-                                        $relationModel->$attr = $val;
300
-                                        $relations[$relation] = $relationModel;
301
-                                    }
302
-                                }
303
-                            }
304
-                        }
305
-                    }
306
-                }
307
-                /**
308
-                 * If the attribute isn't a relation and prevent attributes not in the fillable.
309
-                 */
310
-                else if (array_search($key, $model->getFillable(), true) !== false)
311
-                {
312
-                    $model->$key = $value;   
313
-                }
314
-            }
315
-            /**
316
-             * Save the model.
317
-             */
318
-            $model->save();
294
+									/**
295
+									 * Prevent attributes not in the fillable.
296
+									 */
297
+									if (array_search($attr, $relationModel->getFillable(), true) !== false) 
298
+									{
299
+										$relationModel->$attr = $val;
300
+										$relations[$relation] = $relationModel;
301
+									}
302
+								}
303
+							}
304
+						}
305
+					}
306
+				}
307
+				/**
308
+				 * If the attribute isn't a relation and prevent attributes not in the fillable.
309
+				 */
310
+				else if (array_search($key, $model->getFillable(), true) !== false)
311
+				{
312
+					$model->$key = $value;   
313
+				}
314
+			}
315
+			/**
316
+			 * Save the model.
317
+			 */
318
+			$model->save();
319 319
             
320
-            /**
321
-             * Loop through the relations array.
322
-             */
323
-            foreach ($relations as $key => $value) 
324
-            {
325
-                /**
326
-                 * If the relation is marked for delete then delete it.
327
-                 */
328
-                if ($value == 'delete' && $model->$key()->count())
329
-                {
330
-                    $model->$key()->delete();
331
-                }
332
-                /**
333
-                 * If the relation is an array.
334
-                 */
335
-                else if (gettype($value) == 'array') 
336
-                {
337
-                    $ids = [];
338
-                    /**
339
-                     * Loop through the relations.
340
-                     */
341
-                    foreach ($value as $val) 
342
-                    {
343
-                        switch (class_basename($model->$key())) 
344
-                        {
345
-                            /**
346
-                             * If the relation is one to many then update it's foreign key with
347
-                             * the model id and save it then add its id to ids array to delete all 
348
-                             * relations who's id isn't in the ids array.
349
-                             */
350
-                            case 'HasMany':
351
-                                $foreignKeyName       = explode('.', $model->$key()->getForeignKey())[1];
352
-                                $val->$foreignKeyName = $model->id;
353
-                                $val->save();
354
-                                $ids[] = $val->id;
355
-                                break;
320
+			/**
321
+			 * Loop through the relations array.
322
+			 */
323
+			foreach ($relations as $key => $value) 
324
+			{
325
+				/**
326
+				 * If the relation is marked for delete then delete it.
327
+				 */
328
+				if ($value == 'delete' && $model->$key()->count())
329
+				{
330
+					$model->$key()->delete();
331
+				}
332
+				/**
333
+				 * If the relation is an array.
334
+				 */
335
+				else if (gettype($value) == 'array') 
336
+				{
337
+					$ids = [];
338
+					/**
339
+					 * Loop through the relations.
340
+					 */
341
+					foreach ($value as $val) 
342
+					{
343
+						switch (class_basename($model->$key())) 
344
+						{
345
+							/**
346
+							 * If the relation is one to many then update it's foreign key with
347
+							 * the model id and save it then add its id to ids array to delete all 
348
+							 * relations who's id isn't in the ids array.
349
+							 */
350
+							case 'HasMany':
351
+								$foreignKeyName       = explode('.', $model->$key()->getForeignKey())[1];
352
+								$val->$foreignKeyName = $model->id;
353
+								$val->save();
354
+								$ids[] = $val->id;
355
+								break;
356 356
 
357
-                            /**
358
-                             * If the relation is many to many then add it's id to the ids array to
359
-                             * attache these ids to the model.
360
-                             */
361
-                            case 'BelongsToMany':
362
-                                $val->save();
363
-                                $ids[] = $val->id;
364
-                                break;
365
-                        }
366
-                    }
367
-                    switch (class_basename($model->$key())) 
368
-                    {
369
-                        /**
370
-                         * If the relation is one to many then delete all 
371
-                         * relations who's id isn't in the ids array.
372
-                         */
373
-                        case 'HasMany':
374
-                            $model->$key()->whereNotIn('id', $ids)->delete();
375
-                            break;
357
+							/**
358
+							 * If the relation is many to many then add it's id to the ids array to
359
+							 * attache these ids to the model.
360
+							 */
361
+							case 'BelongsToMany':
362
+								$val->save();
363
+								$ids[] = $val->id;
364
+								break;
365
+						}
366
+					}
367
+					switch (class_basename($model->$key())) 
368
+					{
369
+						/**
370
+						 * If the relation is one to many then delete all 
371
+						 * relations who's id isn't in the ids array.
372
+						 */
373
+						case 'HasMany':
374
+							$model->$key()->whereNotIn('id', $ids)->delete();
375
+							break;
376 376
 
377
-                        /**
378
-                         * If the relation is many to many then 
379
-                         * detach the previous data and attach 
380
-                         * the ids array to the model.
381
-                         */
382
-                        case 'BelongsToMany':
383
-                            $model->$key()->detach();
384
-                            $model->$key()->attach($ids);
385
-                            break;
386
-                    }
387
-                }
388
-                /**
389
-                 * If the relation isn't array.
390
-                 */
391
-                else
392
-                {
393
-                    switch (class_basename($model->$key())) 
394
-                    {
395
-                        /**
396
-                         * If the relation is one to many or one to one.
397
-                         */
398
-                        case 'BelongsTo':
399
-                            $value->save();
400
-                            $model->$key()->associate($value);
401
-                            $model->save();
402
-                            break;
403
-                    }
404
-                }
405
-            }
377
+						/**
378
+						 * If the relation is many to many then 
379
+						 * detach the previous data and attach 
380
+						 * the ids array to the model.
381
+						 */
382
+						case 'BelongsToMany':
383
+							$model->$key()->detach();
384
+							$model->$key()->attach($ids);
385
+							break;
386
+					}
387
+				}
388
+				/**
389
+				 * If the relation isn't array.
390
+				 */
391
+				else
392
+				{
393
+					switch (class_basename($model->$key())) 
394
+					{
395
+						/**
396
+						 * If the relation is one to many or one to one.
397
+						 */
398
+						case 'BelongsTo':
399
+							$value->save();
400
+							$model->$key()->associate($value);
401
+							$model->save();
402
+							break;
403
+					}
404
+				}
405
+			}
406 406
 
407
-            $saveLog ? \Logging::saveLog(array_key_exists('id', $data) ? 'update' : 'create', class_basename($modelClass), $this->getModel(), $model->id, $model) : false;
408
-        });
407
+			$saveLog ? \Logging::saveLog(array_key_exists('id', $data) ? 'update' : 'create', class_basename($modelClass), $this->getModel(), $model->id, $model) : false;
408
+		});
409 409
     
410
-        /**
411
-         * return the saved mdel with the given relations.
412
-         */
413
-        return $this->find($model->id, $with);
414
-    }
410
+		/**
411
+		 * return the saved mdel with the given relations.
412
+		 */
413
+		return $this->find($model->id, $with);
414
+	}
415 415
     
416
-    /**
417
-     * Delete record from the storage based on the given
418
-     * condition.
419
-     * 
420
-     * @param  var $value condition value
421
-     * @param  string $attribute condition column name
422
-     * @return void
423
-     */
424
-    public function delete($value, $attribute = 'id', $saveLog = true)
425
-    {
426
-        if ($attribute == 'id') 
427
-        {
428
-            \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) {
429
-                $model = $this->model->lockForUpdate()->find($value);
430
-                if ( ! $model) 
431
-                {
432
-                    \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $value);
433
-                }
416
+	/**
417
+	 * Delete record from the storage based on the given
418
+	 * condition.
419
+	 * 
420
+	 * @param  var $value condition value
421
+	 * @param  string $attribute condition column name
422
+	 * @return void
423
+	 */
424
+	public function delete($value, $attribute = 'id', $saveLog = true)
425
+	{
426
+		if ($attribute == 'id') 
427
+		{
428
+			\DB::transaction(function () use ($value, $attribute, &$result, $saveLog) {
429
+				$model = $this->model->lockForUpdate()->find($value);
430
+				if ( ! $model) 
431
+				{
432
+					\ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $value);
433
+				}
434 434
                 
435
-                $model->delete();
436
-                $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $value, $model) : false;
437
-            });
438
-        }
439
-        else
440
-        {
441
-            \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) {
442
-                call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model){
443
-                    $model->delete();
444
-                    $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $model->id, $model) : false;
445
-                });
446
-            });   
447
-        }
448
-    }
435
+				$model->delete();
436
+				$saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $value, $model) : false;
437
+			});
438
+		}
439
+		else
440
+		{
441
+			\DB::transaction(function () use ($value, $attribute, &$result, $saveLog) {
442
+				call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model){
443
+					$model->delete();
444
+					$saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $model->id, $model) : false;
445
+				});
446
+			});   
447
+		}
448
+	}
449 449
     
450
-    /**
451
-     * Fetch records from the storage based on the given
452
-     * id.
453
-     * 
454
-     * @param  integer $id
455
-     * @param  array   $relations
456
-     * @param  array   $columns
457
-     * @return object
458
-     */
459
-    public function find($id, $relations = [], $columns = array('*'))
460
-    {
461
-        return call_user_func_array("{$this->getModel()}::with", array($relations))->find($id, $columns);
462
-    }
450
+	/**
451
+	 * Fetch records from the storage based on the given
452
+	 * id.
453
+	 * 
454
+	 * @param  integer $id
455
+	 * @param  array   $relations
456
+	 * @param  array   $columns
457
+	 * @return object
458
+	 */
459
+	public function find($id, $relations = [], $columns = array('*'))
460
+	{
461
+		return call_user_func_array("{$this->getModel()}::with", array($relations))->find($id, $columns);
462
+	}
463 463
     
464
-    /**
465
-     * Fetch records from the storage based on the given
466
-     * condition.
467
-     * 
468
-     * @param  array   $conditions array of conditions
469
-     * @param  array   $relations
470
-     * @param  string  $sortBy
471
-     * @param  boolean $desc
472
-     * @param  array   $columns
473
-     * @return collection
474
-     */
475
-    public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*'))
476
-    {
477
-        $conditions = $this->constructConditions($conditions);
478
-        $sort       = $desc ? 'desc' : 'asc';
479
-        return call_user_func_array("{$this->getModel()}::with",  array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns);
480
-    }
464
+	/**
465
+	 * Fetch records from the storage based on the given
466
+	 * condition.
467
+	 * 
468
+	 * @param  array   $conditions array of conditions
469
+	 * @param  array   $relations
470
+	 * @param  string  $sortBy
471
+	 * @param  boolean $desc
472
+	 * @param  array   $columns
473
+	 * @return collection
474
+	 */
475
+	public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*'))
476
+	{
477
+		$conditions = $this->constructConditions($conditions);
478
+		$sort       = $desc ? 'desc' : 'asc';
479
+		return call_user_func_array("{$this->getModel()}::with",  array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns);
480
+	}
481 481
 
482
-    /**
483
-     * Fetch the first record from the storage based on the given
484
-     * condition.
485
-     *
486
-     * @param  array   $conditions array of conditions
487
-     * @param  array   $relations
488
-     * @param  array   $columns
489
-     * @return object
490
-     */
491
-    public function first($conditions, $relations = [], $columns = array('*'))
492
-    {
493
-        $conditions = $this->constructConditions($conditions);
494
-        return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns);  
495
-    }
482
+	/**
483
+	 * Fetch the first record from the storage based on the given
484
+	 * condition.
485
+	 *
486
+	 * @param  array   $conditions array of conditions
487
+	 * @param  array   $relations
488
+	 * @param  array   $columns
489
+	 * @return object
490
+	 */
491
+	public function first($conditions, $relations = [], $columns = array('*'))
492
+	{
493
+		$conditions = $this->constructConditions($conditions);
494
+		return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns);  
495
+	}
496 496
 
497
-    /**
498
-     * Build the conditions recursively for the retrieving methods.
499
-     * @param  array $conditions
500
-     * @return array
501
-     */
502
-    protected function constructConditions($conditions)
503
-    {   
504
-        $conditionString = '';
505
-        $conditionValues = [];
506
-        foreach ($conditions as $key => $value) 
507
-        {
508
-            if ($key == 'and') 
509
-            {
510
-                $conditionString  .= str_replace('{op}', 'and', $this->constructConditions($value)['conditionString']) . ' {op} ';
511
-                $conditionValues   = array_merge($conditionValues, $this->constructConditions($value)['conditionValues']);
512
-            }
513
-            else if ($key == 'or')
514
-            {
515
-                $conditionString  .= str_replace('{op}', 'or', $this->constructConditions($value)['conditionString']) . ' {op} ';
516
-                $conditionValues   = array_merge($conditionValues, $this->constructConditions($value)['conditionValues']);
517
-            }
518
-            else
519
-            {
520
-                $conditionString  .= $key . '=? {op} ';
521
-                $conditionValues[] = $value;
522
-            }
523
-        }
524
-        $conditionString = '(' . rtrim($conditionString, '{op} ') . ')';
525
-        return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues];
526
-    }
497
+	/**
498
+	 * Build the conditions recursively for the retrieving methods.
499
+	 * @param  array $conditions
500
+	 * @return array
501
+	 */
502
+	protected function constructConditions($conditions)
503
+	{   
504
+		$conditionString = '';
505
+		$conditionValues = [];
506
+		foreach ($conditions as $key => $value) 
507
+		{
508
+			if ($key == 'and') 
509
+			{
510
+				$conditionString  .= str_replace('{op}', 'and', $this->constructConditions($value)['conditionString']) . ' {op} ';
511
+				$conditionValues   = array_merge($conditionValues, $this->constructConditions($value)['conditionValues']);
512
+			}
513
+			else if ($key == 'or')
514
+			{
515
+				$conditionString  .= str_replace('{op}', 'or', $this->constructConditions($value)['conditionString']) . ' {op} ';
516
+				$conditionValues   = array_merge($conditionValues, $this->constructConditions($value)['conditionValues']);
517
+			}
518
+			else
519
+			{
520
+				$conditionString  .= $key . '=? {op} ';
521
+				$conditionValues[] = $value;
522
+			}
523
+		}
524
+		$conditionString = '(' . rtrim($conditionString, '{op} ') . ')';
525
+		return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues];
526
+	}
527 527
 
528
-    /**
529
-     * Abstract method that return the necessary 
530
-     * information (full model namespace)
531
-     * needed to preform the previous actions.
532
-     * 
533
-     * @return string
534
-     */
535
-    abstract protected function getModel();
528
+	/**
529
+	 * Abstract method that return the necessary 
530
+	 * information (full model namespace)
531
+	 * needed to preform the previous actions.
532
+	 * 
533
+	 * @return string
534
+	 */
535
+	abstract protected function getModel();
536 536
 }
537 537
\ No newline at end of file
Please login to merge, or discard this patch.
src/Modules/V1/Core/Http/Controllers/BaseApiController.php 1 patch
Indentation   +209 added lines, -209 removed lines patch added patch discarded remove patch
@@ -6,215 +6,215 @@
 block discarded – undo
6 6
 
7 7
 class BaseApiController extends Controller
8 8
 {
9
-    /**
10
-     * The model implementation.
11
-     * 
12
-     * @var model
13
-     */
14
-    protected $model;
15
-
16
-    /**
17
-     * The config implementation.
18
-     * 
19
-     * @var config
20
-     */
21
-    protected $config;
22
-
23
-    public function __construct()
24
-    {
25
-        \Session::set('timeZoneDiff', \Request::header('time-zone-diff') ?: 0);
9
+	/**
10
+	 * The model implementation.
11
+	 * 
12
+	 * @var model
13
+	 */
14
+	protected $model;
15
+
16
+	/**
17
+	 * The config implementation.
18
+	 * 
19
+	 * @var config
20
+	 */
21
+	protected $config;
22
+
23
+	public function __construct()
24
+	{
25
+		\Session::set('timeZoneDiff', \Request::header('time-zone-diff') ?: 0);
26 26
         
27
-        $this->config              = \CoreConfig::getConfig();
28
-        $this->model               = property_exists($this, 'model') ? $this->model : false;
29
-        $this->validationRules     = property_exists($this, 'validationRules') ? $this->validationRules : false;
30
-        $this->skipPermissionCheck = property_exists($this, 'skipPermissionCheck') ? $this->skipPermissionCheck : [];
31
-        $this->skipLoginCheck      = property_exists($this, 'skipLoginCheck') ? $this->skipLoginCheck : [];
32
-        $this->relations           = array_key_exists($this->model, $this->config['relations']) ? $this->config['relations'][$this->model] : false;
33
-        $route                     = explode('@',\Route::currentRouteAction())[1];
34
-        $this->checkPermission($route);
35
-    }
36
-
37
-    /**
38
-     * Fetch all records with relations from model repository.
39
-     * 
40
-     * @return \Illuminate\Http\Response
41
-     */
42
-    public function index() 
43
-    {
44
-        if ($this->model)
45
-        {
46
-            $relations = $this->relations && $this->relations['all'] ? $this->relations['all'] : [];
47
-            return \Response::json(call_user_func_array("\Core::{$this->model}", [])->all($relations), 200);
48
-        }
49
-    }
50
-
51
-    /**
52
-     * Fetch the single object with relations from model repository.
53
-     * 
54
-     * @param  integer $id
55
-     * @return \Illuminate\Http\Response
56
-     */
57
-    public function find($id) 
58
-    {
59
-        if ($this->model) 
60
-        {
61
-            $relations = $this->relations && $this->relations['find'] ? $this->relations['find'] : [];
62
-            return \Response::json(call_user_func_array("\Core::{$this->model}", [])->find($id, $relations), 200);
63
-        }
64
-    }
65
-
66
-    /**
67
-     * Paginate all records with relations from model repository
68
-     * that matche the given query.
69
-     * 
70
-     * @param  string  $query
71
-     * @param  integer $perPage
72
-     * @param  string  $sortBy
73
-     * @param  boolean $desc
74
-     * @return \Illuminate\Http\Response
75
-     */
76
-    public function search($query = '', $perPage = 15, $sortBy = 'created_at', $desc = 1) 
77
-    {
78
-        if ($this->model) 
79
-        {
80
-            $relations = $this->relations && $this->relations['paginate'] ? $this->relations['paginate'] : [];
81
-            return \Response::json(call_user_func_array("\Core::{$this->model}", [])->search($query, $perPage, $relations, $sortBy, $desc), 200);
82
-        }
83
-    }
84
-
85
-    /**
86
-     * Fetch records from the storage based on the given
87
-     * condition.
88
-     * 
89
-     * @param  \Illuminate\Http\Request  $request
90
-     * @param  string  $sortBy
91
-     * @param  boolean $desc
92
-     * @return \Illuminate\Http\Response
93
-     */
94
-    public function findby(Request $request, $sortBy = 'created_at', $desc = 1) 
95
-    {
96
-        if ($this->model) 
97
-        {
98
-            $relations = $this->relations && $this->relations['findBy'] ? $this->relations['findBy'] : [];
99
-            return \Response::json(call_user_func_array("\Core::{$this->model}", [])->findBy($request->all(), $relations, $sortBy, $desc), 200);
100
-        }
101
-    }
102
-
103
-    /**
104
-     * Fetch the first record from the storage based on the given
105
-     * condition.
106
-     * 
107
-     * @param  \Illuminate\Http\Request  $request
108
-     * @return \Illuminate\Http\Response
109
-     */
110
-    public function first(Request $request) 
111
-    {
112
-        if ($this->model) 
113
-        {
114
-            $relations = $this->relations && $this->relations['first'] ? $this->relations['first'] : [];
115
-            return \Response::json(call_user_func_array("\Core::{$this->model}", [])->first($request->all(), $relations), 200);
116
-        }
117
-    }
118
-
119
-    /**
120
-     * Paginate all records with relations from model repository.
121
-     * 
122
-     * @param  integer $perPage
123
-     * @param  string  $sortBy
124
-     * @param  boolean $desc
125
-     * @return \Illuminate\Http\Response
126
-     */
127
-    public function paginate($perPage = 15, $sortBy = 'created_at', $desc = 1) 
128
-    {
129
-        if ($this->model) 
130
-        {
131
-            $relations = $this->relations && $this->relations['paginate'] ? $this->relations['paginate'] : [];
132
-            return \Response::json(call_user_func_array("\Core::{$this->model}", [])->paginate($perPage, $relations, $sortBy, $desc), 200);
133
-        }
134
-    }
135
-
136
-    /**
137
-     * Fetch all records with relations based on
138
-     * the given condition from storage in pages.
139
-     * 
140
-     * @param  \Illuminate\Http\Request  $request
141
-     * @param  integer $perPage
142
-     * @param  string  $sortBy
143
-     * @param  boolean $desc
144
-     * @return \Illuminate\Http\Response
145
-     */
146
-    public function paginateby(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) 
147
-    {
148
-        if ($this->model) 
149
-        {
150
-            $relations = $this->relations && $this->relations['paginateBy'] ? $this->relations['paginateBy'] : [];
151
-            return \Response::json(call_user_func_array("\Core::{$this->model}", [])->paginateBy($request->all(), $perPage, $relations, $sortBy, $desc), 200);
152
-        }
153
-    }
154
-
155
-    /**
156
-     * Save the given model to repository.
157
-     * 
158
-     * @param  \Illuminate\Http\Request  $request
159
-     * @return \Illuminate\Http\Response
160
-     */
161
-    public function save(Request $request) 
162
-    {
163
-        foreach ($this->validationRules as &$rule) 
164
-        {
165
-            if (strpos($rule, 'exists') && ! strpos($rule, 'deleted_at,NULL')) 
166
-            {
167
-                $rule .= ',deleted_at,NULL';
168
-            }
169
-
170
-            if ($request->has('id')) 
171
-            {
172
-                $rule = str_replace('{id}', $request->get('id'), $rule);
173
-            }
174
-            else
175
-            {
176
-                $rule = str_replace(',{id}', '', $rule);
177
-            }
178
-        }
27
+		$this->config              = \CoreConfig::getConfig();
28
+		$this->model               = property_exists($this, 'model') ? $this->model : false;
29
+		$this->validationRules     = property_exists($this, 'validationRules') ? $this->validationRules : false;
30
+		$this->skipPermissionCheck = property_exists($this, 'skipPermissionCheck') ? $this->skipPermissionCheck : [];
31
+		$this->skipLoginCheck      = property_exists($this, 'skipLoginCheck') ? $this->skipLoginCheck : [];
32
+		$this->relations           = array_key_exists($this->model, $this->config['relations']) ? $this->config['relations'][$this->model] : false;
33
+		$route                     = explode('@',\Route::currentRouteAction())[1];
34
+		$this->checkPermission($route);
35
+	}
36
+
37
+	/**
38
+	 * Fetch all records with relations from model repository.
39
+	 * 
40
+	 * @return \Illuminate\Http\Response
41
+	 */
42
+	public function index() 
43
+	{
44
+		if ($this->model)
45
+		{
46
+			$relations = $this->relations && $this->relations['all'] ? $this->relations['all'] : [];
47
+			return \Response::json(call_user_func_array("\Core::{$this->model}", [])->all($relations), 200);
48
+		}
49
+	}
50
+
51
+	/**
52
+	 * Fetch the single object with relations from model repository.
53
+	 * 
54
+	 * @param  integer $id
55
+	 * @return \Illuminate\Http\Response
56
+	 */
57
+	public function find($id) 
58
+	{
59
+		if ($this->model) 
60
+		{
61
+			$relations = $this->relations && $this->relations['find'] ? $this->relations['find'] : [];
62
+			return \Response::json(call_user_func_array("\Core::{$this->model}", [])->find($id, $relations), 200);
63
+		}
64
+	}
65
+
66
+	/**
67
+	 * Paginate all records with relations from model repository
68
+	 * that matche the given query.
69
+	 * 
70
+	 * @param  string  $query
71
+	 * @param  integer $perPage
72
+	 * @param  string  $sortBy
73
+	 * @param  boolean $desc
74
+	 * @return \Illuminate\Http\Response
75
+	 */
76
+	public function search($query = '', $perPage = 15, $sortBy = 'created_at', $desc = 1) 
77
+	{
78
+		if ($this->model) 
79
+		{
80
+			$relations = $this->relations && $this->relations['paginate'] ? $this->relations['paginate'] : [];
81
+			return \Response::json(call_user_func_array("\Core::{$this->model}", [])->search($query, $perPage, $relations, $sortBy, $desc), 200);
82
+		}
83
+	}
84
+
85
+	/**
86
+	 * Fetch records from the storage based on the given
87
+	 * condition.
88
+	 * 
89
+	 * @param  \Illuminate\Http\Request  $request
90
+	 * @param  string  $sortBy
91
+	 * @param  boolean $desc
92
+	 * @return \Illuminate\Http\Response
93
+	 */
94
+	public function findby(Request $request, $sortBy = 'created_at', $desc = 1) 
95
+	{
96
+		if ($this->model) 
97
+		{
98
+			$relations = $this->relations && $this->relations['findBy'] ? $this->relations['findBy'] : [];
99
+			return \Response::json(call_user_func_array("\Core::{$this->model}", [])->findBy($request->all(), $relations, $sortBy, $desc), 200);
100
+		}
101
+	}
102
+
103
+	/**
104
+	 * Fetch the first record from the storage based on the given
105
+	 * condition.
106
+	 * 
107
+	 * @param  \Illuminate\Http\Request  $request
108
+	 * @return \Illuminate\Http\Response
109
+	 */
110
+	public function first(Request $request) 
111
+	{
112
+		if ($this->model) 
113
+		{
114
+			$relations = $this->relations && $this->relations['first'] ? $this->relations['first'] : [];
115
+			return \Response::json(call_user_func_array("\Core::{$this->model}", [])->first($request->all(), $relations), 200);
116
+		}
117
+	}
118
+
119
+	/**
120
+	 * Paginate all records with relations from model repository.
121
+	 * 
122
+	 * @param  integer $perPage
123
+	 * @param  string  $sortBy
124
+	 * @param  boolean $desc
125
+	 * @return \Illuminate\Http\Response
126
+	 */
127
+	public function paginate($perPage = 15, $sortBy = 'created_at', $desc = 1) 
128
+	{
129
+		if ($this->model) 
130
+		{
131
+			$relations = $this->relations && $this->relations['paginate'] ? $this->relations['paginate'] : [];
132
+			return \Response::json(call_user_func_array("\Core::{$this->model}", [])->paginate($perPage, $relations, $sortBy, $desc), 200);
133
+		}
134
+	}
135
+
136
+	/**
137
+	 * Fetch all records with relations based on
138
+	 * the given condition from storage in pages.
139
+	 * 
140
+	 * @param  \Illuminate\Http\Request  $request
141
+	 * @param  integer $perPage
142
+	 * @param  string  $sortBy
143
+	 * @param  boolean $desc
144
+	 * @return \Illuminate\Http\Response
145
+	 */
146
+	public function paginateby(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) 
147
+	{
148
+		if ($this->model) 
149
+		{
150
+			$relations = $this->relations && $this->relations['paginateBy'] ? $this->relations['paginateBy'] : [];
151
+			return \Response::json(call_user_func_array("\Core::{$this->model}", [])->paginateBy($request->all(), $perPage, $relations, $sortBy, $desc), 200);
152
+		}
153
+	}
154
+
155
+	/**
156
+	 * Save the given model to repository.
157
+	 * 
158
+	 * @param  \Illuminate\Http\Request  $request
159
+	 * @return \Illuminate\Http\Response
160
+	 */
161
+	public function save(Request $request) 
162
+	{
163
+		foreach ($this->validationRules as &$rule) 
164
+		{
165
+			if (strpos($rule, 'exists') && ! strpos($rule, 'deleted_at,NULL')) 
166
+			{
167
+				$rule .= ',deleted_at,NULL';
168
+			}
169
+
170
+			if ($request->has('id')) 
171
+			{
172
+				$rule = str_replace('{id}', $request->get('id'), $rule);
173
+			}
174
+			else
175
+			{
176
+				$rule = str_replace(',{id}', '', $rule);
177
+			}
178
+		}
179 179
         
180
-        $this->validate($request, $this->validationRules);
181
-
182
-        if ($this->model) 
183
-        {
184
-            return \Response::json(call_user_func_array("\Core::{$this->model}", [])->save($request->all()), 200);
185
-        }
186
-    }
187
-
188
-    /**
189
-     * Delete by the given id from model repository.
190
-     * 
191
-     * @param  integer  $id
192
-     * @return \Illuminate\Http\Response
193
-     */
194
-    public function delete($id) 
195
-    {
196
-        if ($this->model) 
197
-        {
198
-            return \Response::json(call_user_func_array("\Core::{$this->model}", [])->delete($id), 200);
199
-        }
200
-    }
201
-
202
-    /**
203
-     * Check if the logged in user can do the given permission.
204
-     * 
205
-     * @param  string $permission
206
-     * @return void
207
-     */
208
-    private function checkPermission($permission)
209
-    {
210
-        $permission = $permission !== 'index' ? $permission : 'list';
211
-        if ( ! in_array($permission, $this->skipLoginCheck)) 
212
-        {
213
-            \JWTAuth::parseToken()->authenticate();
214
-            if ( ! in_array($permission, $this->skipPermissionCheck) && ! \Core::users()->can($permission, $this->model))
215
-            {
216
-                \ErrorHandler::noPermissions();
217
-            }
218
-        }
219
-    }
180
+		$this->validate($request, $this->validationRules);
181
+
182
+		if ($this->model) 
183
+		{
184
+			return \Response::json(call_user_func_array("\Core::{$this->model}", [])->save($request->all()), 200);
185
+		}
186
+	}
187
+
188
+	/**
189
+	 * Delete by the given id from model repository.
190
+	 * 
191
+	 * @param  integer  $id
192
+	 * @return \Illuminate\Http\Response
193
+	 */
194
+	public function delete($id) 
195
+	{
196
+		if ($this->model) 
197
+		{
198
+			return \Response::json(call_user_func_array("\Core::{$this->model}", [])->delete($id), 200);
199
+		}
200
+	}
201
+
202
+	/**
203
+	 * Check if the logged in user can do the given permission.
204
+	 * 
205
+	 * @param  string $permission
206
+	 * @return void
207
+	 */
208
+	private function checkPermission($permission)
209
+	{
210
+		$permission = $permission !== 'index' ? $permission : 'list';
211
+		if ( ! in_array($permission, $this->skipLoginCheck)) 
212
+		{
213
+			\JWTAuth::parseToken()->authenticate();
214
+			if ( ! in_array($permission, $this->skipPermissionCheck) && ! \Core::users()->can($permission, $this->model))
215
+			{
216
+				\ErrorHandler::noPermissions();
217
+			}
218
+		}
219
+	}
220 220
 }
Please login to merge, or discard this patch.
src/Modules/V1/Notifications/Http/Controllers/NotificationsController.php 1 patch
Indentation   +24 added lines, -24 removed lines patch added patch discarded remove patch
@@ -8,30 +8,30 @@
 block discarded – undo
8 8
 class NotificationsController extends BaseApiController
9 9
 {
10 10
 	/**
11
-     * The name of the model that is used by the base api controller 
12
-     * to preform actions like (add, edit ... etc).
13
-     * @var string
14
-     */
15
-    protected $model            = 'notifications';
11
+	 * The name of the model that is used by the base api controller 
12
+	 * to preform actions like (add, edit ... etc).
13
+	 * @var string
14
+	 */
15
+	protected $model            = 'notifications';
16 16
 
17
-    /**
18
-     * Set the notification notified to true.
19
-     * 
20
-     * @param  integer  $id
21
-     * @return \Illuminate\Http\Response
22
-     */
23
-    public function notified($id)
24
-    {
25
-        return \Response::json(\Core::notifications()->notified($id), 200);
26
-    }
17
+	/**
18
+	 * Set the notification notified to true.
19
+	 * 
20
+	 * @param  integer  $id
21
+	 * @return \Illuminate\Http\Response
22
+	 */
23
+	public function notified($id)
24
+	{
25
+		return \Response::json(\Core::notifications()->notified($id), 200);
26
+	}
27 27
 
28
-    /**
29
-     * Set the notification notified to all.
30
-     * 
31
-     * @return \Illuminate\Http\Response
32
-     */
33
-    public function notifyall()
34
-    {
35
-        return \Response::json(\Core::notifications()->notifyAll(), 200);
36
-    }
28
+	/**
29
+	 * Set the notification notified to all.
30
+	 * 
31
+	 * @return \Illuminate\Http\Response
32
+	 */
33
+	public function notifyall()
34
+	{
35
+		return \Response::json(\Core::notifications()->notifyAll(), 200);
36
+	}
37 37
 }
Please login to merge, or discard this patch.
src/Modules/V1/Acl/Http/Controllers/UsersController.php 1 patch
Indentation   +164 added lines, -164 removed lines patch added patch discarded remove patch
@@ -7,168 +7,168 @@
 block discarded – undo
7 7
 
8 8
 class UsersController extends BaseApiController
9 9
 {
10
-    /**
11
-     * The name of the model that is used by the base api controller 
12
-     * to preform actions like (add, edit ... etc).
13
-     * @var string
14
-     */
15
-    protected $model               = 'users';
16
-
17
-    /**
18
-     * List of all route actions that the base api controller
19
-     * will skip permissions check for them.
20
-     * @var array
21
-     */
22
-    protected $skipPermissionCheck = ['account', 'logout', 'block', 'unblock', 'editprofile', 'sendreset'];
23
-
24
-    /**
25
-     * List of all route actions that the base api controller
26
-     * will skip login check for them.
27
-     * @var array
28
-     */
29
-    protected $skipLoginCheck      = ['login', 'register', 'sendreset', 'resetpassword'];
30
-
31
-    /**
32
-     * The validations rules used by the base api controller
33
-     * to check before add.
34
-     * @var array
35
-     */
36
-    protected $validationRules     = [
37
-    'email'    => 'required|email|unique:users,email,{id}',
38
-    'password' => 'min:6'
39
-    ];
40
-
41
-    /**
42
-     * Return the logged in user account.
43
-     * 
44
-     * @return \Illuminate\Http\Response
45
-     */
46
-    public function account()
47
-    {
48
-       $relations = $this->relations && $this->relations['find'] ? $this->relations['find'] : [];
49
-       return \Response::json(call_user_func_array("\Core::{$this->model}", [])->find(\JWTAuth::parseToken()->authenticate()->id, $relations), 200);
50
-    }
51
-
52
-    /**
53
-     * Block the user.
54
-     *
55
-     * @param  integer  $id
56
-     * @return \Illuminate\Http\Response
57
-     */
58
-    public function block($id)
59
-    {
60
-        return \Response::json(\Core::users()->block($id), 200);
61
-    }
62
-
63
-    /**
64
-     * Unblock the user.
65
-     *
66
-     * @param  integer  $id
67
-     * @return \Illuminate\Http\Response
68
-     */
69
-    public function unblock($id)
70
-    {
71
-        return \Response::json(\Core::users()->unblock($id), 200);
72
-    }
73
-
74
-    /**
75
-     * Logout the user.
76
-     * 
77
-     * @return \Illuminate\Http\Response
78
-     */
79
-    public function logout()
80
-    {
81
-        return \Response::json(\Core::users()->logout(), 200);
82
-    }
83
-
84
-    /**
85
-     * Handle a registration request.
86
-     *
87
-     * @param  \Illuminate\Http\Request  $request
88
-     * @return \Illuminate\Http\Response
89
-     */
90
-    public function register(Request $request)
91
-    {
92
-        $this->validate($request, [
93
-            'email'    => 'email|unique:users,email,{id}', 
94
-            'password' => 'min:6'
95
-            ]);
96
-
97
-        return \Response::json(\Core::users()->register($request->only('email', 'password')), 200);
98
-    }
99
-
100
-    /**
101
-     * Handle a login request of the none admin to the application.
102
-     *
103
-     * @param  \Illuminate\Http\Request  $request
104
-     * @return \Illuminate\Http\Response
105
-     */
106
-    public function login(Request $request)
107
-    {
108
-        $this->validate($request, [
109
-            'email'    => 'required|email', 
110
-            'password' => 'required|min:6',
111
-            'admin'    => 'boolean'
112
-            ]);
113
-
114
-        return \Response::json(\Core::users()->login($request->only('email', 'password'), $request->get('admin')), 200);
115
-    }
116
-
117
-    /**
118
-     * Handle an assign groups to user request.
119
-     *
120
-     * @param  \Illuminate\Http\Request  $request
121
-     * @return \Illuminate\Http\Response
122
-     */
123
-    public function assigngroups(Request $request)
124
-    {
125
-        $this->validate($request, [
126
-            'group_ids' => 'required|exists:groups,id', 
127
-            'user_id'   => 'required|exists:users,id'
128
-            ]);
129
-
130
-        return \Response::json(\Core::users()->assignGroups($request->get('user_id'), $request->get('group_ids')), 200);
131
-    }
132
-
133
-    /**
134
-     * Handle the editing of the user profile.
135
-     *
136
-     * @param  \Illuminate\Http\Request  $request
137
-     * @return \Illuminate\Http\Response
138
-     */
139
-    public function editprofile(Request $request)
140
-    {
141
-        return \Response::json(\Core::users()->editProfile($request->all()), 200);
142
-    }
143
-
144
-    /**
145
-     * Send a reset link to the given user.
146
-     *
147
-     * @param  \Illuminate\Http\Request  $request
148
-     * @return \Illuminate\Http\Response
149
-     */
150
-    public function sendreset(Request $request)
151
-    {
152
-        $this->validate($request, ['email' => 'required|email', 'url' => 'required|url']);
153
-
154
-        return \Response::json(\Core::users()->sendReset($request->only('email'), $request->get('url')), 200);
155
-    }
156
-
157
-    /**
158
-     * Reset the given user's password.
159
-     *
160
-     * @param  \Illuminate\Http\Request  $request
161
-     * @return \Illuminate\Http\Response
162
-     */
163
-    public function resetpassword(Request $request)
164
-    {
165
-        $this->validate($request, [
166
-            'token'                 => 'required',
167
-            'email'                 => 'required|email',
168
-            'password'              => 'required|confirmed|min:6',
169
-            'password_confirmation' => 'required',
170
-        ]);
171
-
172
-        return \Response::json(\Core::users()->resetPassword($request->only('email', 'password', 'password_confirmation', 'token')), 200);
173
-    }
10
+	/**
11
+	 * The name of the model that is used by the base api controller 
12
+	 * to preform actions like (add, edit ... etc).
13
+	 * @var string
14
+	 */
15
+	protected $model               = 'users';
16
+
17
+	/**
18
+	 * List of all route actions that the base api controller
19
+	 * will skip permissions check for them.
20
+	 * @var array
21
+	 */
22
+	protected $skipPermissionCheck = ['account', 'logout', 'block', 'unblock', 'editprofile', 'sendreset'];
23
+
24
+	/**
25
+	 * List of all route actions that the base api controller
26
+	 * will skip login check for them.
27
+	 * @var array
28
+	 */
29
+	protected $skipLoginCheck      = ['login', 'register', 'sendreset', 'resetpassword'];
30
+
31
+	/**
32
+	 * The validations rules used by the base api controller
33
+	 * to check before add.
34
+	 * @var array
35
+	 */
36
+	protected $validationRules     = [
37
+	'email'    => 'required|email|unique:users,email,{id}',
38
+	'password' => 'min:6'
39
+	];
40
+
41
+	/**
42
+	 * Return the logged in user account.
43
+	 * 
44
+	 * @return \Illuminate\Http\Response
45
+	 */
46
+	public function account()
47
+	{
48
+	   $relations = $this->relations && $this->relations['find'] ? $this->relations['find'] : [];
49
+	   return \Response::json(call_user_func_array("\Core::{$this->model}", [])->find(\JWTAuth::parseToken()->authenticate()->id, $relations), 200);
50
+	}
51
+
52
+	/**
53
+	 * Block the user.
54
+	 *
55
+	 * @param  integer  $id
56
+	 * @return \Illuminate\Http\Response
57
+	 */
58
+	public function block($id)
59
+	{
60
+		return \Response::json(\Core::users()->block($id), 200);
61
+	}
62
+
63
+	/**
64
+	 * Unblock the user.
65
+	 *
66
+	 * @param  integer  $id
67
+	 * @return \Illuminate\Http\Response
68
+	 */
69
+	public function unblock($id)
70
+	{
71
+		return \Response::json(\Core::users()->unblock($id), 200);
72
+	}
73
+
74
+	/**
75
+	 * Logout the user.
76
+	 * 
77
+	 * @return \Illuminate\Http\Response
78
+	 */
79
+	public function logout()
80
+	{
81
+		return \Response::json(\Core::users()->logout(), 200);
82
+	}
83
+
84
+	/**
85
+	 * Handle a registration request.
86
+	 *
87
+	 * @param  \Illuminate\Http\Request  $request
88
+	 * @return \Illuminate\Http\Response
89
+	 */
90
+	public function register(Request $request)
91
+	{
92
+		$this->validate($request, [
93
+			'email'    => 'email|unique:users,email,{id}', 
94
+			'password' => 'min:6'
95
+			]);
96
+
97
+		return \Response::json(\Core::users()->register($request->only('email', 'password')), 200);
98
+	}
99
+
100
+	/**
101
+	 * Handle a login request of the none admin to the application.
102
+	 *
103
+	 * @param  \Illuminate\Http\Request  $request
104
+	 * @return \Illuminate\Http\Response
105
+	 */
106
+	public function login(Request $request)
107
+	{
108
+		$this->validate($request, [
109
+			'email'    => 'required|email', 
110
+			'password' => 'required|min:6',
111
+			'admin'    => 'boolean'
112
+			]);
113
+
114
+		return \Response::json(\Core::users()->login($request->only('email', 'password'), $request->get('admin')), 200);
115
+	}
116
+
117
+	/**
118
+	 * Handle an assign groups to user request.
119
+	 *
120
+	 * @param  \Illuminate\Http\Request  $request
121
+	 * @return \Illuminate\Http\Response
122
+	 */
123
+	public function assigngroups(Request $request)
124
+	{
125
+		$this->validate($request, [
126
+			'group_ids' => 'required|exists:groups,id', 
127
+			'user_id'   => 'required|exists:users,id'
128
+			]);
129
+
130
+		return \Response::json(\Core::users()->assignGroups($request->get('user_id'), $request->get('group_ids')), 200);
131
+	}
132
+
133
+	/**
134
+	 * Handle the editing of the user profile.
135
+	 *
136
+	 * @param  \Illuminate\Http\Request  $request
137
+	 * @return \Illuminate\Http\Response
138
+	 */
139
+	public function editprofile(Request $request)
140
+	{
141
+		return \Response::json(\Core::users()->editProfile($request->all()), 200);
142
+	}
143
+
144
+	/**
145
+	 * Send a reset link to the given user.
146
+	 *
147
+	 * @param  \Illuminate\Http\Request  $request
148
+	 * @return \Illuminate\Http\Response
149
+	 */
150
+	public function sendreset(Request $request)
151
+	{
152
+		$this->validate($request, ['email' => 'required|email', 'url' => 'required|url']);
153
+
154
+		return \Response::json(\Core::users()->sendReset($request->only('email'), $request->get('url')), 200);
155
+	}
156
+
157
+	/**
158
+	 * Reset the given user's password.
159
+	 *
160
+	 * @param  \Illuminate\Http\Request  $request
161
+	 * @return \Illuminate\Http\Response
162
+	 */
163
+	public function resetpassword(Request $request)
164
+	{
165
+		$this->validate($request, [
166
+			'token'                 => 'required',
167
+			'email'                 => 'required|email',
168
+			'password'              => 'required|confirmed|min:6',
169
+			'password_confirmation' => 'required',
170
+		]);
171
+
172
+		return \Response::json(\Core::users()->resetPassword($request->only('email', 'password', 'password_confirmation', 'token')), 200);
173
+	}
174 174
 }
Please login to merge, or discard this patch.
src/Modules/V1/Acl/Http/Controllers/GroupsController.php 1 patch
Indentation   +28 added lines, -28 removed lines patch added patch discarded remove patch
@@ -8,35 +8,35 @@
 block discarded – undo
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|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|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
 }
Please login to merge, or discard this patch.