@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -$factory->define(App\Modules\V1\Notifications\PushNotificationDevice::class, function (Faker\Generator $faker) { |
|
3 | +$factory->define(App\Modules\V1\Notifications\PushNotificationDevice::class, function(Faker\Generator $faker) { |
|
4 | 4 | return [ |
5 | 5 | 'device_token' => $faker->sha1(), |
6 | 6 | 'user_id' => $faker->randomDigitNotNull(), |
@@ -1,10 +1,10 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | $factory->define(App\Modules\V1\Notifications\PushNotificationDevice::class, function (Faker\Generator $faker) { |
4 | - return [ |
|
4 | + return [ |
|
5 | 5 | 'device_token' => $faker->sha1(), |
6 | 6 | 'user_id' => $faker->randomDigitNotNull(), |
7 | 7 | 'created_at' => $faker->dateTimeBetween('-1 years', 'now'), |
8 | 8 | 'updated_at' => $faker->dateTimeBetween('-1 years', 'now') |
9 | - ]; |
|
9 | + ]; |
|
10 | 10 | }); |
11 | 11 | \ No newline at end of file |
@@ -20,7 +20,7 @@ |
||
20 | 20 | $table->unique(array('device_token', 'user_id')); |
21 | 21 | $table->softDeletes(); |
22 | 22 | $table->timestamps(); |
23 | - }); |
|
23 | + }); |
|
24 | 24 | } |
25 | 25 | |
26 | 26 | /** |
@@ -12,7 +12,7 @@ |
||
12 | 12 | */ |
13 | 13 | public function up() |
14 | 14 | { |
15 | - Schema::create('push_notifications_devices', function (Blueprint $table) { |
|
15 | + Schema::create('push_notifications_devices', function(Blueprint $table) { |
|
16 | 16 | $table->increments('id'); |
17 | 17 | $table->string('device_token'); |
18 | 18 | $table->integer('user_id'); |
@@ -6,33 +6,33 @@ |
||
6 | 6 | |
7 | 7 | class CreateJobsTable extends Migration |
8 | 8 | { |
9 | - /** |
|
10 | - * Run the migrations. |
|
11 | - * |
|
12 | - * @return void |
|
13 | - */ |
|
14 | - public function up() |
|
15 | - { |
|
16 | - Schema::create('jobs', function (Blueprint $table) { |
|
17 | - $table->bigIncrements('id'); |
|
18 | - $table->string('queue'); |
|
19 | - $table->longText('payload'); |
|
20 | - $table->tinyInteger('attempts')->unsigned(); |
|
21 | - $table->unsignedInteger('reserved_at')->nullable(); |
|
22 | - $table->unsignedInteger('available_at'); |
|
23 | - $table->unsignedInteger('created_at'); |
|
9 | + /** |
|
10 | + * Run the migrations. |
|
11 | + * |
|
12 | + * @return void |
|
13 | + */ |
|
14 | + public function up() |
|
15 | + { |
|
16 | + Schema::create('jobs', function (Blueprint $table) { |
|
17 | + $table->bigIncrements('id'); |
|
18 | + $table->string('queue'); |
|
19 | + $table->longText('payload'); |
|
20 | + $table->tinyInteger('attempts')->unsigned(); |
|
21 | + $table->unsignedInteger('reserved_at')->nullable(); |
|
22 | + $table->unsignedInteger('available_at'); |
|
23 | + $table->unsignedInteger('created_at'); |
|
24 | 24 | |
25 | - $table->index(['queue', 'reserved_at']); |
|
26 | - }); |
|
27 | - } |
|
25 | + $table->index(['queue', 'reserved_at']); |
|
26 | + }); |
|
27 | + } |
|
28 | 28 | |
29 | - /** |
|
30 | - * Reverse the migrations. |
|
31 | - * |
|
32 | - * @return void |
|
33 | - */ |
|
34 | - public function down() |
|
35 | - { |
|
36 | - Schema::dropIfExists('jobs'); |
|
37 | - } |
|
29 | + /** |
|
30 | + * Reverse the migrations. |
|
31 | + * |
|
32 | + * @return void |
|
33 | + */ |
|
34 | + public function down() |
|
35 | + { |
|
36 | + Schema::dropIfExists('jobs'); |
|
37 | + } |
|
38 | 38 | } |
@@ -13,7 +13,7 @@ |
||
13 | 13 | */ |
14 | 14 | public function up() |
15 | 15 | { |
16 | - Schema::create('jobs', function (Blueprint $table) { |
|
16 | + Schema::create('jobs', function(Blueprint $table) { |
|
17 | 17 | $table->bigIncrements('id'); |
18 | 18 | $table->string('queue'); |
19 | 19 | $table->longText('payload'); |
@@ -2,7 +2,6 @@ |
||
2 | 2 | |
3 | 3 | use App\User; |
4 | 4 | use Illuminate\Database\Eloquent\SoftDeletes; |
5 | -use Illuminate\Notifications\Notifiable; |
|
6 | 5 | use Laravel\Passport\HasApiTokens; |
7 | 6 | |
8 | 7 | class AclUser extends User { |
@@ -7,89 +7,89 @@ |
||
7 | 7 | |
8 | 8 | class AclUser extends User { |
9 | 9 | |
10 | - use SoftDeletes, HasApiTokens; |
|
11 | - protected $table = 'users'; |
|
12 | - protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
13 | - protected $hidden = ['password', 'remember_token','deleted_at']; |
|
14 | - protected $guarded = ['id']; |
|
15 | - protected $fillable = ['name', 'email', 'password']; |
|
16 | - public $searchable = ['name', 'email']; |
|
10 | + use SoftDeletes, HasApiTokens; |
|
11 | + protected $table = 'users'; |
|
12 | + protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
|
13 | + protected $hidden = ['password', 'remember_token','deleted_at']; |
|
14 | + protected $guarded = ['id']; |
|
15 | + protected $fillable = ['name', 'email', 'password']; |
|
16 | + public $searchable = ['name', 'email']; |
|
17 | 17 | |
18 | - public function getCreatedAtAttribute($value) |
|
19 | - { |
|
20 | - return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
21 | - } |
|
18 | + public function getCreatedAtAttribute($value) |
|
19 | + { |
|
20 | + return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
21 | + } |
|
22 | 22 | |
23 | - public function getUpdatedAtAttribute($value) |
|
24 | - { |
|
25 | - return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
26 | - } |
|
23 | + public function getUpdatedAtAttribute($value) |
|
24 | + { |
|
25 | + return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
26 | + } |
|
27 | 27 | |
28 | - public function getDeletedAtAttribute($value) |
|
29 | - { |
|
30 | - return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
31 | - } |
|
28 | + public function getDeletedAtAttribute($value) |
|
29 | + { |
|
30 | + return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * Encrypt the password attribute before |
|
35 | - * saving it in the storage. |
|
36 | - * |
|
37 | - * @param string $value |
|
38 | - */ |
|
39 | - public function setPasswordAttribute($value) |
|
40 | - { |
|
41 | - $this->attributes['password'] = bcrypt($value); |
|
42 | - } |
|
33 | + /** |
|
34 | + * Encrypt the password attribute before |
|
35 | + * saving it in the storage. |
|
36 | + * |
|
37 | + * @param string $value |
|
38 | + */ |
|
39 | + public function setPasswordAttribute($value) |
|
40 | + { |
|
41 | + $this->attributes['password'] = bcrypt($value); |
|
42 | + } |
|
43 | 43 | |
44 | - public function groups() |
|
45 | - { |
|
46 | - return $this->belongsToMany('\App\Modules\V1\Acl\AclGroup','users_groups','user_id','group_id')->whereNull('users_groups.deleted_at')->withTimestamps(); |
|
47 | - } |
|
44 | + public function groups() |
|
45 | + { |
|
46 | + return $this->belongsToMany('\App\Modules\V1\Acl\AclGroup','users_groups','user_id','group_id')->whereNull('users_groups.deleted_at')->withTimestamps(); |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * Return fcm device tokens related to the user. |
|
51 | - * @return array |
|
52 | - */ |
|
53 | - public function routeNotificationForFCM() |
|
54 | - { |
|
55 | - $devices = \Core::pushNotificationsDevices()->findBy(['user_id' => $this->id]); |
|
56 | - $tokens = []; |
|
49 | + /** |
|
50 | + * Return fcm device tokens related to the user. |
|
51 | + * @return array |
|
52 | + */ |
|
53 | + public function routeNotificationForFCM() |
|
54 | + { |
|
55 | + $devices = \Core::pushNotificationsDevices()->findBy(['user_id' => $this->id]); |
|
56 | + $tokens = []; |
|
57 | 57 | |
58 | - foreach ($devices as $device) |
|
59 | - { |
|
60 | - $accessToken = decrypt($device->access_token); |
|
58 | + foreach ($devices as $device) |
|
59 | + { |
|
60 | + $accessToken = decrypt($device->access_token); |
|
61 | 61 | |
62 | - try |
|
63 | - { |
|
64 | - if (\Core::users()->accessTokenExpiredOrRevoked($accessToken)) |
|
65 | - { |
|
66 | - continue; |
|
67 | - } |
|
62 | + try |
|
63 | + { |
|
64 | + if (\Core::users()->accessTokenExpiredOrRevoked($accessToken)) |
|
65 | + { |
|
66 | + continue; |
|
67 | + } |
|
68 | 68 | |
69 | - $tokens[] = $device->device_token; |
|
70 | - } |
|
71 | - catch (\Exception $e) |
|
72 | - { |
|
73 | - $device->forceDelete(); |
|
74 | - } |
|
75 | - } |
|
69 | + $tokens[] = $device->device_token; |
|
70 | + } |
|
71 | + catch (\Exception $e) |
|
72 | + { |
|
73 | + $device->forceDelete(); |
|
74 | + } |
|
75 | + } |
|
76 | 76 | |
77 | - return $tokens; |
|
78 | - } |
|
77 | + return $tokens; |
|
78 | + } |
|
79 | 79 | |
80 | - /** |
|
81 | - * The channels the user receives notification broadcasts on. |
|
82 | - * |
|
83 | - * @return string |
|
84 | - */ |
|
85 | - public function receivesBroadcastNotificationsOn() |
|
86 | - { |
|
87 | - return 'users.' . $this->id; |
|
88 | - } |
|
80 | + /** |
|
81 | + * The channels the user receives notification broadcasts on. |
|
82 | + * |
|
83 | + * @return string |
|
84 | + */ |
|
85 | + public function receivesBroadcastNotificationsOn() |
|
86 | + { |
|
87 | + return 'users.' . $this->id; |
|
88 | + } |
|
89 | 89 | |
90 | - public static function boot() |
|
91 | - { |
|
92 | - parent::boot(); |
|
93 | - parent::observe(\App::make('App\Modules\V1\Acl\ModelObservers\AclUserObserver')); |
|
94 | - } |
|
90 | + public static function boot() |
|
91 | + { |
|
92 | + parent::boot(); |
|
93 | + parent::observe(\App::make('App\Modules\V1\Acl\ModelObservers\AclUserObserver')); |
|
94 | + } |
|
95 | 95 | } |
@@ -10,7 +10,7 @@ discard block |
||
10 | 10 | use SoftDeletes, HasApiTokens; |
11 | 11 | protected $table = 'users'; |
12 | 12 | protected $dates = ['created_at', 'updated_at', 'deleted_at']; |
13 | - protected $hidden = ['password', 'remember_token','deleted_at']; |
|
13 | + protected $hidden = ['password', 'remember_token', 'deleted_at']; |
|
14 | 14 | protected $guarded = ['id']; |
15 | 15 | protected $fillable = ['name', 'email', 'password']; |
16 | 16 | public $searchable = ['name', 'email']; |
@@ -43,7 +43,7 @@ discard block |
||
43 | 43 | |
44 | 44 | public function groups() |
45 | 45 | { |
46 | - return $this->belongsToMany('\App\Modules\V1\Acl\AclGroup','users_groups','user_id','group_id')->whereNull('users_groups.deleted_at')->withTimestamps(); |
|
46 | + return $this->belongsToMany('\App\Modules\V1\Acl\AclGroup', 'users_groups', 'user_id', 'group_id')->whereNull('users_groups.deleted_at')->withTimestamps(); |
|
47 | 47 | } |
48 | 48 | |
49 | 49 | /** |
@@ -84,7 +84,7 @@ discard block |
||
84 | 84 | */ |
85 | 85 | public function receivesBroadcastNotificationsOn() |
86 | 86 | { |
87 | - return 'users.' . $this->id; |
|
87 | + return 'users.'.$this->id; |
|
88 | 88 | } |
89 | 89 | |
90 | 90 | public static function boot() |
@@ -67,8 +67,7 @@ |
||
67 | 67 | } |
68 | 68 | |
69 | 69 | $tokens[] = $device->device_token; |
70 | - } |
|
71 | - catch (\Exception $e) |
|
70 | + } catch (\Exception $e) |
|
72 | 71 | { |
73 | 72 | $device->forceDelete(); |
74 | 73 | } |
@@ -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 | */ |
@@ -164,7 +164,7 @@ discard block |
||
164 | 164 | * @param integer $perPage |
165 | 165 | * @param array $relations |
166 | 166 | * @param string $sortBy |
167 | - * @param boolean $desc |
|
167 | + * @param integer $desc |
|
168 | 168 | * @param array $columns |
169 | 169 | * @return collection |
170 | 170 | */ |
@@ -182,7 +182,7 @@ discard block |
||
182 | 182 | * @param integer $perPage |
183 | 183 | * @param array $relations |
184 | 184 | * @param string $sortBy |
185 | - * @param boolean $desc |
|
185 | + * @param integer $desc |
|
186 | 186 | * @param array $columns |
187 | 187 | * @return collection |
188 | 188 | */ |
@@ -197,7 +197,7 @@ discard block |
||
197 | 197 | /** |
198 | 198 | * Save the given model to the storage. |
199 | 199 | * |
200 | - * @param array $data |
|
200 | + * @param string $data |
|
201 | 201 | * @return void |
202 | 202 | */ |
203 | 203 | public function save(array $data) |
@@ -442,7 +442,7 @@ discard block |
||
442 | 442 | * Update record in the storage based on the given |
443 | 443 | * condition. |
444 | 444 | * |
445 | - * @param var $value condition value |
|
445 | + * @param integer $value condition value |
|
446 | 446 | * @param array $data |
447 | 447 | * @param string $attribute condition column name |
448 | 448 | * @return void |
@@ -515,7 +515,7 @@ discard block |
||
515 | 515 | * @param array $conditions array of conditions |
516 | 516 | * @param array $relations |
517 | 517 | * @param string $sortBy |
518 | - * @param boolean $desc |
|
518 | + * @param integer $desc |
|
519 | 519 | * @param array $columns |
520 | 520 | * @return collection |
521 | 521 | */ |
@@ -547,7 +547,7 @@ discard block |
||
547 | 547 | * @param array $conditions array of conditions |
548 | 548 | * @param integer $perPage |
549 | 549 | * @param string $sortBy |
550 | - * @param boolean $desc |
|
550 | + * @param integer $desc |
|
551 | 551 | * @param array $columns |
552 | 552 | * @return collection |
553 | 553 | */ |
@@ -4,699 +4,699 @@ |
||
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 | - $column = 'LOWER(' . array_shift($conditionColumns) . ')'; |
|
71 | - if (str_contains($column, '->')) |
|
72 | - { |
|
73 | - $column = $this->wrapJsonSelector($column); |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * Use the first element in the model columns to construct the first condition. |
|
78 | - */ |
|
79 | - $q->where(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * Loop through the rest of the columns to construct or where conditions. |
|
84 | - */ |
|
85 | - foreach ($conditionColumns as $column) |
|
86 | - { |
|
87 | - $column = 'LOWER(' . $column . ')'; |
|
88 | - if (str_contains($column, '->')) |
|
89 | - { |
|
90 | - $column = $this->wrapJsonSelector($column); |
|
91 | - } |
|
92 | - |
|
93 | - $q->orWhere(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * Loop through the model relations. |
|
98 | - */ |
|
99 | - foreach ($relations as $relation) |
|
100 | - { |
|
101 | - /** |
|
102 | - * Remove the sub relation if exists. |
|
103 | - */ |
|
104 | - $relation = explode('.', $relation)[0]; |
|
105 | - |
|
106 | - /** |
|
107 | - * Try to fetch the relation repository from the core. |
|
108 | - */ |
|
109 | - if (\Core::$relation()) |
|
110 | - { |
|
111 | - /** |
|
112 | - * Construct the relation condition. |
|
113 | - */ |
|
114 | - $q->orWhereHas($relation, function ($subModel) use ($query, $relation){ |
|
115 | - |
|
116 | - $subModel->where(function ($q) use ($query, $relation){ |
|
117 | - |
|
118 | - /** |
|
119 | - * Get columns of the relation. |
|
120 | - */ |
|
121 | - $subConditionColumns = \Core::$relation()->model->searchable; |
|
122 | - |
|
123 | - if (count($subConditionColumns)) |
|
124 | - { |
|
125 | - |
|
126 | - $column = 'LOWER(' . array_shift($subConditionColumns) . ')'; |
|
127 | - if (str_contains($column, '->')) |
|
128 | - { |
|
129 | - $column = $this->wrapJsonSelector($column); |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Use the first element in the relation model columns to construct the first condition. |
|
134 | - */ |
|
135 | - $q->where(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * Loop through the rest of the columns to construct or where conditions. |
|
140 | - */ |
|
141 | - foreach ($subConditionColumns as $subConditionColumn) |
|
142 | - { |
|
143 | - $column = 'LOWER(' . $subConditionColumn . ')'; |
|
144 | - if (str_contains($column, '->')) |
|
145 | - { |
|
146 | - $column = $this->wrapJsonSelector($column); |
|
147 | - } |
|
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 | + $column = 'LOWER(' . array_shift($conditionColumns) . ')'; |
|
71 | + if (str_contains($column, '->')) |
|
72 | + { |
|
73 | + $column = $this->wrapJsonSelector($column); |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * Use the first element in the model columns to construct the first condition. |
|
78 | + */ |
|
79 | + $q->where(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * Loop through the rest of the columns to construct or where conditions. |
|
84 | + */ |
|
85 | + foreach ($conditionColumns as $column) |
|
86 | + { |
|
87 | + $column = 'LOWER(' . $column . ')'; |
|
88 | + if (str_contains($column, '->')) |
|
89 | + { |
|
90 | + $column = $this->wrapJsonSelector($column); |
|
91 | + } |
|
92 | + |
|
93 | + $q->orWhere(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * Loop through the model relations. |
|
98 | + */ |
|
99 | + foreach ($relations as $relation) |
|
100 | + { |
|
101 | + /** |
|
102 | + * Remove the sub relation if exists. |
|
103 | + */ |
|
104 | + $relation = explode('.', $relation)[0]; |
|
105 | + |
|
106 | + /** |
|
107 | + * Try to fetch the relation repository from the core. |
|
108 | + */ |
|
109 | + if (\Core::$relation()) |
|
110 | + { |
|
111 | + /** |
|
112 | + * Construct the relation condition. |
|
113 | + */ |
|
114 | + $q->orWhereHas($relation, function ($subModel) use ($query, $relation){ |
|
115 | + |
|
116 | + $subModel->where(function ($q) use ($query, $relation){ |
|
117 | + |
|
118 | + /** |
|
119 | + * Get columns of the relation. |
|
120 | + */ |
|
121 | + $subConditionColumns = \Core::$relation()->model->searchable; |
|
122 | + |
|
123 | + if (count($subConditionColumns)) |
|
124 | + { |
|
125 | + |
|
126 | + $column = 'LOWER(' . array_shift($subConditionColumns) . ')'; |
|
127 | + if (str_contains($column, '->')) |
|
128 | + { |
|
129 | + $column = $this->wrapJsonSelector($column); |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Use the first element in the relation model columns to construct the first condition. |
|
134 | + */ |
|
135 | + $q->where(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * Loop through the rest of the columns to construct or where conditions. |
|
140 | + */ |
|
141 | + foreach ($subConditionColumns as $subConditionColumn) |
|
142 | + { |
|
143 | + $column = 'LOWER(' . $subConditionColumn . ')'; |
|
144 | + if (str_contains($column, '->')) |
|
145 | + { |
|
146 | + $column = $this->wrapJsonSelector($column); |
|
147 | + } |
|
148 | 148 | |
149 | - $q->orWhere(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
150 | - } |
|
151 | - }); |
|
152 | - |
|
153 | - }); |
|
154 | - } |
|
155 | - } |
|
156 | - }); |
|
149 | + $q->orWhere(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
150 | + } |
|
151 | + }); |
|
152 | + |
|
153 | + }); |
|
154 | + } |
|
155 | + } |
|
156 | + }); |
|
157 | 157 | |
158 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
159 | - } |
|
158 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
159 | + } |
|
160 | 160 | |
161 | - /** |
|
162 | - * Fetch all records with relations from storage in pages. |
|
163 | - * |
|
164 | - * @param integer $perPage |
|
165 | - * @param array $relations |
|
166 | - * @param string $sortBy |
|
167 | - * @param boolean $desc |
|
168 | - * @param array $columns |
|
169 | - * @return collection |
|
170 | - */ |
|
171 | - public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
172 | - { |
|
173 | - $sort = $desc ? 'desc' : 'asc'; |
|
174 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * Fetch all records with relations based on |
|
179 | - * the given condition from storage in pages. |
|
180 | - * |
|
181 | - * @param array $conditions array of conditions |
|
182 | - * @param integer $perPage |
|
183 | - * @param array $relations |
|
184 | - * @param string $sortBy |
|
185 | - * @param boolean $desc |
|
186 | - * @param array $columns |
|
187 | - * @return collection |
|
188 | - */ |
|
189 | - public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
190 | - { |
|
191 | - unset($conditions['page']); |
|
192 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
193 | - $sort = $desc ? 'desc' : 'asc'; |
|
194 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
195 | - } |
|
161 | + /** |
|
162 | + * Fetch all records with relations from storage in pages. |
|
163 | + * |
|
164 | + * @param integer $perPage |
|
165 | + * @param array $relations |
|
166 | + * @param string $sortBy |
|
167 | + * @param boolean $desc |
|
168 | + * @param array $columns |
|
169 | + * @return collection |
|
170 | + */ |
|
171 | + public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
172 | + { |
|
173 | + $sort = $desc ? 'desc' : 'asc'; |
|
174 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * Fetch all records with relations based on |
|
179 | + * the given condition from storage in pages. |
|
180 | + * |
|
181 | + * @param array $conditions array of conditions |
|
182 | + * @param integer $perPage |
|
183 | + * @param array $relations |
|
184 | + * @param string $sortBy |
|
185 | + * @param boolean $desc |
|
186 | + * @param array $columns |
|
187 | + * @return collection |
|
188 | + */ |
|
189 | + public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
190 | + { |
|
191 | + unset($conditions['page']); |
|
192 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
193 | + $sort = $desc ? 'desc' : 'asc'; |
|
194 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
195 | + } |
|
196 | 196 | |
197 | - /** |
|
198 | - * Save the given model to the storage. |
|
199 | - * |
|
200 | - * @param array $data |
|
201 | - * @return void |
|
202 | - */ |
|
203 | - public function save(array $data) |
|
204 | - { |
|
205 | - $model = false; |
|
206 | - $modelClass = $this->model; |
|
207 | - $relations = []; |
|
208 | - |
|
209 | - \DB::transaction(function () use (&$model, &$relations, $data, $modelClass) { |
|
210 | - /** |
|
211 | - * If the id is present in the data then select the model for updating, |
|
212 | - * else create new model. |
|
213 | - * @var array |
|
214 | - */ |
|
215 | - $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
216 | - if ( ! $model) |
|
217 | - { |
|
218 | - \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']); |
|
219 | - } |
|
220 | - |
|
221 | - /** |
|
222 | - * Construct the model object with the given data, |
|
223 | - * and if there is a relation add it to relations array, |
|
224 | - * then save the model. |
|
225 | - */ |
|
226 | - foreach ($data as $key => $value) |
|
227 | - { |
|
228 | - /** |
|
229 | - * If the attribute is a relation. |
|
230 | - */ |
|
231 | - $relation = camel_case($key); |
|
232 | - if (method_exists($model, $relation) && \Core::$relation()) |
|
233 | - { |
|
234 | - /** |
|
235 | - * Check if the relation is a collection. |
|
236 | - */ |
|
237 | - if (class_basename($model->$relation) == 'Collection') |
|
238 | - { |
|
239 | - /** |
|
240 | - * If the relation has no value then marke the relation data |
|
241 | - * related to the model to be deleted. |
|
242 | - */ |
|
243 | - if ( ! $value || ! count($value)) |
|
244 | - { |
|
245 | - $relations[$relation] = 'delete'; |
|
246 | - } |
|
247 | - } |
|
248 | - if (is_array($value)) |
|
249 | - { |
|
250 | - /** |
|
251 | - * Loop through the relation data. |
|
252 | - */ |
|
253 | - foreach ($value as $attr => $val) |
|
254 | - { |
|
255 | - /** |
|
256 | - * Get the relation model. |
|
257 | - */ |
|
258 | - $relationBaseModel = \Core::$relation()->model; |
|
259 | - |
|
260 | - /** |
|
261 | - * Check if the relation is a collection. |
|
262 | - */ |
|
263 | - if (class_basename($model->$relation) == 'Collection') |
|
264 | - { |
|
265 | - /** |
|
266 | - * If the id is present in the data then select the relation model for updating, |
|
267 | - * else create new model. |
|
268 | - */ |
|
269 | - $relationModel = array_key_exists('id', $val) ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
270 | - |
|
271 | - /** |
|
272 | - * If model doesn't exists. |
|
273 | - */ |
|
274 | - if ( ! $relationModel) |
|
275 | - { |
|
276 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']); |
|
277 | - } |
|
278 | - |
|
279 | - /** |
|
280 | - * Loop through the relation attributes. |
|
281 | - */ |
|
282 | - foreach ($val as $attr => $val) |
|
283 | - { |
|
284 | - /** |
|
285 | - * Prevent the sub relations or attributes not in the fillable. |
|
286 | - */ |
|
287 | - if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
288 | - { |
|
289 | - $relationModel->$attr = $val; |
|
290 | - } |
|
291 | - } |
|
292 | - |
|
293 | - $relations[$relation][] = $relationModel; |
|
294 | - } |
|
295 | - /** |
|
296 | - * If not collection. |
|
297 | - */ |
|
298 | - else |
|
299 | - { |
|
300 | - /** |
|
301 | - * Prevent the sub relations. |
|
302 | - */ |
|
303 | - if (gettype($val) !== 'object' && gettype($val) !== 'array') |
|
304 | - { |
|
305 | - |
|
306 | - /** |
|
307 | - * If the id is present in the data then select the relation model for updating, |
|
308 | - * else create new model. |
|
309 | - */ |
|
310 | - $relationModel = array_key_exists('id', $value) ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
311 | - |
|
312 | - /** |
|
313 | - * If model doesn't exists. |
|
314 | - */ |
|
315 | - if ( ! $relationModel) |
|
316 | - { |
|
317 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']); |
|
318 | - } |
|
319 | - |
|
320 | - foreach ($value as $relationAttribute => $relationValue) |
|
321 | - { |
|
322 | - /** |
|
323 | - * Prevent attributes not in the fillable. |
|
324 | - */ |
|
325 | - if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) |
|
326 | - { |
|
327 | - $relationModel->$relationAttribute = $relationValue; |
|
328 | - } |
|
329 | - } |
|
330 | - |
|
331 | - $relations[$relation] = $relationModel; |
|
332 | - } |
|
333 | - } |
|
334 | - } |
|
335 | - } |
|
336 | - } |
|
337 | - /** |
|
338 | - * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
339 | - */ |
|
340 | - else if (array_search($key, $model->getFillable(), true) !== false) |
|
341 | - { |
|
342 | - $model->$key = $value; |
|
343 | - } |
|
344 | - } |
|
345 | - /** |
|
346 | - * Save the model. |
|
347 | - */ |
|
348 | - $model->save(); |
|
349 | - |
|
350 | - /** |
|
351 | - * Loop through the relations array. |
|
352 | - */ |
|
353 | - foreach ($relations as $key => $value) |
|
354 | - { |
|
355 | - /** |
|
356 | - * If the relation is marked for delete then delete it. |
|
357 | - */ |
|
358 | - if ($value == 'delete' && $model->$key()->count()) |
|
359 | - { |
|
360 | - $model->$key()->delete(); |
|
361 | - } |
|
362 | - /** |
|
363 | - * If the relation is an array. |
|
364 | - */ |
|
365 | - else if (gettype($value) == 'array') |
|
366 | - { |
|
367 | - $ids = []; |
|
368 | - /** |
|
369 | - * Loop through the relations. |
|
370 | - */ |
|
371 | - foreach ($value as $val) |
|
372 | - { |
|
373 | - switch (class_basename($model->$key())) |
|
374 | - { |
|
375 | - /** |
|
376 | - * If the relation is one to many then update it's foreign key with |
|
377 | - * the model id and save it then add its id to ids array to delete all |
|
378 | - * relations who's id isn't in the ids array. |
|
379 | - */ |
|
380 | - case 'HasMany': |
|
381 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
382 | - $val->$foreignKeyName = $model->id; |
|
383 | - $val->save(); |
|
384 | - $ids[] = $val->id; |
|
385 | - break; |
|
386 | - |
|
387 | - /** |
|
388 | - * If the relation is many to many then add it's id to the ids array to |
|
389 | - * attache these ids to the model. |
|
390 | - */ |
|
391 | - case 'BelongsToMany': |
|
392 | - $val->save(); |
|
393 | - $ids[] = $val->id; |
|
394 | - break; |
|
395 | - } |
|
396 | - } |
|
397 | - switch (class_basename($model->$key())) |
|
398 | - { |
|
399 | - /** |
|
400 | - * If the relation is one to many then delete all |
|
401 | - * relations who's id isn't in the ids array. |
|
402 | - */ |
|
403 | - case 'HasMany': |
|
404 | - $model->$key()->whereNotIn('id', $ids)->delete(); |
|
405 | - break; |
|
406 | - |
|
407 | - /** |
|
408 | - * If the relation is many to many then |
|
409 | - * detach the previous data and attach |
|
410 | - * the ids array to the model. |
|
411 | - */ |
|
412 | - case 'BelongsToMany': |
|
413 | - $model->$key()->detach(); |
|
414 | - $model->$key()->attach($ids); |
|
415 | - break; |
|
416 | - } |
|
417 | - } |
|
418 | - /** |
|
419 | - * If the relation isn't array. |
|
420 | - */ |
|
421 | - else |
|
422 | - { |
|
423 | - switch (class_basename($model->$key())) |
|
424 | - { |
|
425 | - /** |
|
426 | - * If the relation is one to many or one to one. |
|
427 | - */ |
|
428 | - case 'HasOne': |
|
429 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
430 | - $value->$foreignKeyName = $model->id; |
|
431 | - $value->save(); |
|
432 | - break; |
|
433 | - } |
|
434 | - } |
|
435 | - } |
|
436 | - }); |
|
197 | + /** |
|
198 | + * Save the given model to the storage. |
|
199 | + * |
|
200 | + * @param array $data |
|
201 | + * @return void |
|
202 | + */ |
|
203 | + public function save(array $data) |
|
204 | + { |
|
205 | + $model = false; |
|
206 | + $modelClass = $this->model; |
|
207 | + $relations = []; |
|
208 | + |
|
209 | + \DB::transaction(function () use (&$model, &$relations, $data, $modelClass) { |
|
210 | + /** |
|
211 | + * If the id is present in the data then select the model for updating, |
|
212 | + * else create new model. |
|
213 | + * @var array |
|
214 | + */ |
|
215 | + $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
216 | + if ( ! $model) |
|
217 | + { |
|
218 | + \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']); |
|
219 | + } |
|
220 | + |
|
221 | + /** |
|
222 | + * Construct the model object with the given data, |
|
223 | + * and if there is a relation add it to relations array, |
|
224 | + * then save the model. |
|
225 | + */ |
|
226 | + foreach ($data as $key => $value) |
|
227 | + { |
|
228 | + /** |
|
229 | + * If the attribute is a relation. |
|
230 | + */ |
|
231 | + $relation = camel_case($key); |
|
232 | + if (method_exists($model, $relation) && \Core::$relation()) |
|
233 | + { |
|
234 | + /** |
|
235 | + * Check if the relation is a collection. |
|
236 | + */ |
|
237 | + if (class_basename($model->$relation) == 'Collection') |
|
238 | + { |
|
239 | + /** |
|
240 | + * If the relation has no value then marke the relation data |
|
241 | + * related to the model to be deleted. |
|
242 | + */ |
|
243 | + if ( ! $value || ! count($value)) |
|
244 | + { |
|
245 | + $relations[$relation] = 'delete'; |
|
246 | + } |
|
247 | + } |
|
248 | + if (is_array($value)) |
|
249 | + { |
|
250 | + /** |
|
251 | + * Loop through the relation data. |
|
252 | + */ |
|
253 | + foreach ($value as $attr => $val) |
|
254 | + { |
|
255 | + /** |
|
256 | + * Get the relation model. |
|
257 | + */ |
|
258 | + $relationBaseModel = \Core::$relation()->model; |
|
259 | + |
|
260 | + /** |
|
261 | + * Check if the relation is a collection. |
|
262 | + */ |
|
263 | + if (class_basename($model->$relation) == 'Collection') |
|
264 | + { |
|
265 | + /** |
|
266 | + * If the id is present in the data then select the relation model for updating, |
|
267 | + * else create new model. |
|
268 | + */ |
|
269 | + $relationModel = array_key_exists('id', $val) ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
270 | + |
|
271 | + /** |
|
272 | + * If model doesn't exists. |
|
273 | + */ |
|
274 | + if ( ! $relationModel) |
|
275 | + { |
|
276 | + \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']); |
|
277 | + } |
|
278 | + |
|
279 | + /** |
|
280 | + * Loop through the relation attributes. |
|
281 | + */ |
|
282 | + foreach ($val as $attr => $val) |
|
283 | + { |
|
284 | + /** |
|
285 | + * Prevent the sub relations or attributes not in the fillable. |
|
286 | + */ |
|
287 | + if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
288 | + { |
|
289 | + $relationModel->$attr = $val; |
|
290 | + } |
|
291 | + } |
|
292 | + |
|
293 | + $relations[$relation][] = $relationModel; |
|
294 | + } |
|
295 | + /** |
|
296 | + * If not collection. |
|
297 | + */ |
|
298 | + else |
|
299 | + { |
|
300 | + /** |
|
301 | + * Prevent the sub relations. |
|
302 | + */ |
|
303 | + if (gettype($val) !== 'object' && gettype($val) !== 'array') |
|
304 | + { |
|
305 | + |
|
306 | + /** |
|
307 | + * If the id is present in the data then select the relation model for updating, |
|
308 | + * else create new model. |
|
309 | + */ |
|
310 | + $relationModel = array_key_exists('id', $value) ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
311 | + |
|
312 | + /** |
|
313 | + * If model doesn't exists. |
|
314 | + */ |
|
315 | + if ( ! $relationModel) |
|
316 | + { |
|
317 | + \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']); |
|
318 | + } |
|
319 | + |
|
320 | + foreach ($value as $relationAttribute => $relationValue) |
|
321 | + { |
|
322 | + /** |
|
323 | + * Prevent attributes not in the fillable. |
|
324 | + */ |
|
325 | + if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) |
|
326 | + { |
|
327 | + $relationModel->$relationAttribute = $relationValue; |
|
328 | + } |
|
329 | + } |
|
330 | + |
|
331 | + $relations[$relation] = $relationModel; |
|
332 | + } |
|
333 | + } |
|
334 | + } |
|
335 | + } |
|
336 | + } |
|
337 | + /** |
|
338 | + * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
339 | + */ |
|
340 | + else if (array_search($key, $model->getFillable(), true) !== false) |
|
341 | + { |
|
342 | + $model->$key = $value; |
|
343 | + } |
|
344 | + } |
|
345 | + /** |
|
346 | + * Save the model. |
|
347 | + */ |
|
348 | + $model->save(); |
|
349 | + |
|
350 | + /** |
|
351 | + * Loop through the relations array. |
|
352 | + */ |
|
353 | + foreach ($relations as $key => $value) |
|
354 | + { |
|
355 | + /** |
|
356 | + * If the relation is marked for delete then delete it. |
|
357 | + */ |
|
358 | + if ($value == 'delete' && $model->$key()->count()) |
|
359 | + { |
|
360 | + $model->$key()->delete(); |
|
361 | + } |
|
362 | + /** |
|
363 | + * If the relation is an array. |
|
364 | + */ |
|
365 | + else if (gettype($value) == 'array') |
|
366 | + { |
|
367 | + $ids = []; |
|
368 | + /** |
|
369 | + * Loop through the relations. |
|
370 | + */ |
|
371 | + foreach ($value as $val) |
|
372 | + { |
|
373 | + switch (class_basename($model->$key())) |
|
374 | + { |
|
375 | + /** |
|
376 | + * If the relation is one to many then update it's foreign key with |
|
377 | + * the model id and save it then add its id to ids array to delete all |
|
378 | + * relations who's id isn't in the ids array. |
|
379 | + */ |
|
380 | + case 'HasMany': |
|
381 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
382 | + $val->$foreignKeyName = $model->id; |
|
383 | + $val->save(); |
|
384 | + $ids[] = $val->id; |
|
385 | + break; |
|
386 | + |
|
387 | + /** |
|
388 | + * If the relation is many to many then add it's id to the ids array to |
|
389 | + * attache these ids to the model. |
|
390 | + */ |
|
391 | + case 'BelongsToMany': |
|
392 | + $val->save(); |
|
393 | + $ids[] = $val->id; |
|
394 | + break; |
|
395 | + } |
|
396 | + } |
|
397 | + switch (class_basename($model->$key())) |
|
398 | + { |
|
399 | + /** |
|
400 | + * If the relation is one to many then delete all |
|
401 | + * relations who's id isn't in the ids array. |
|
402 | + */ |
|
403 | + case 'HasMany': |
|
404 | + $model->$key()->whereNotIn('id', $ids)->delete(); |
|
405 | + break; |
|
406 | + |
|
407 | + /** |
|
408 | + * If the relation is many to many then |
|
409 | + * detach the previous data and attach |
|
410 | + * the ids array to the model. |
|
411 | + */ |
|
412 | + case 'BelongsToMany': |
|
413 | + $model->$key()->detach(); |
|
414 | + $model->$key()->attach($ids); |
|
415 | + break; |
|
416 | + } |
|
417 | + } |
|
418 | + /** |
|
419 | + * If the relation isn't array. |
|
420 | + */ |
|
421 | + else |
|
422 | + { |
|
423 | + switch (class_basename($model->$key())) |
|
424 | + { |
|
425 | + /** |
|
426 | + * If the relation is one to many or one to one. |
|
427 | + */ |
|
428 | + case 'HasOne': |
|
429 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
430 | + $value->$foreignKeyName = $model->id; |
|
431 | + $value->save(); |
|
432 | + break; |
|
433 | + } |
|
434 | + } |
|
435 | + } |
|
436 | + }); |
|
437 | 437 | |
438 | - return $model->id; |
|
439 | - } |
|
438 | + return $model->id; |
|
439 | + } |
|
440 | 440 | |
441 | - /** |
|
442 | - * Update record in the storage based on the given |
|
443 | - * condition. |
|
444 | - * |
|
445 | - * @param var $value condition value |
|
446 | - * @param array $data |
|
447 | - * @param string $attribute condition column name |
|
448 | - * @return void |
|
449 | - */ |
|
450 | - public function update($value, array $data, $attribute = 'id') |
|
451 | - { |
|
452 | - if ($attribute == 'id') |
|
453 | - { |
|
454 | - $model = $this->model->lockForUpdate()->find($value); |
|
455 | - $model ? $model->update($data) : 0; |
|
456 | - } |
|
457 | - else |
|
458 | - { |
|
459 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data){ |
|
460 | - $model->update($data); |
|
461 | - }); |
|
462 | - } |
|
463 | - } |
|
464 | - |
|
465 | - /** |
|
466 | - * Delete record from the storage based on the given |
|
467 | - * condition. |
|
468 | - * |
|
469 | - * @param var $value condition value |
|
470 | - * @param string $attribute condition column name |
|
471 | - * @return void |
|
472 | - */ |
|
473 | - public function delete($value, $attribute = 'id') |
|
474 | - { |
|
475 | - if ($attribute == 'id') |
|
476 | - { |
|
477 | - \DB::transaction(function () use ($value, $attribute, &$result) { |
|
478 | - $model = $this->model->lockForUpdate()->find($value); |
|
479 | - if ( ! $model) |
|
480 | - { |
|
481 | - \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $value); |
|
482 | - } |
|
441 | + /** |
|
442 | + * Update record in the storage based on the given |
|
443 | + * condition. |
|
444 | + * |
|
445 | + * @param var $value condition value |
|
446 | + * @param array $data |
|
447 | + * @param string $attribute condition column name |
|
448 | + * @return void |
|
449 | + */ |
|
450 | + public function update($value, array $data, $attribute = 'id') |
|
451 | + { |
|
452 | + if ($attribute == 'id') |
|
453 | + { |
|
454 | + $model = $this->model->lockForUpdate()->find($value); |
|
455 | + $model ? $model->update($data) : 0; |
|
456 | + } |
|
457 | + else |
|
458 | + { |
|
459 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data){ |
|
460 | + $model->update($data); |
|
461 | + }); |
|
462 | + } |
|
463 | + } |
|
464 | + |
|
465 | + /** |
|
466 | + * Delete record from the storage based on the given |
|
467 | + * condition. |
|
468 | + * |
|
469 | + * @param var $value condition value |
|
470 | + * @param string $attribute condition column name |
|
471 | + * @return void |
|
472 | + */ |
|
473 | + public function delete($value, $attribute = 'id') |
|
474 | + { |
|
475 | + if ($attribute == 'id') |
|
476 | + { |
|
477 | + \DB::transaction(function () use ($value, $attribute, &$result) { |
|
478 | + $model = $this->model->lockForUpdate()->find($value); |
|
479 | + if ( ! $model) |
|
480 | + { |
|
481 | + \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $value); |
|
482 | + } |
|
483 | 483 | |
484 | - $model->delete(); |
|
485 | - }); |
|
486 | - } |
|
487 | - else |
|
488 | - { |
|
489 | - \DB::transaction(function () use ($value, $attribute, &$result) { |
|
490 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model){ |
|
491 | - $model->delete(); |
|
492 | - }); |
|
493 | - }); |
|
494 | - } |
|
495 | - } |
|
484 | + $model->delete(); |
|
485 | + }); |
|
486 | + } |
|
487 | + else |
|
488 | + { |
|
489 | + \DB::transaction(function () use ($value, $attribute, &$result) { |
|
490 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model){ |
|
491 | + $model->delete(); |
|
492 | + }); |
|
493 | + }); |
|
494 | + } |
|
495 | + } |
|
496 | 496 | |
497 | - /** |
|
498 | - * Fetch records from the storage based on the given |
|
499 | - * id. |
|
500 | - * |
|
501 | - * @param integer $id |
|
502 | - * @param array $relations |
|
503 | - * @param array $columns |
|
504 | - * @return object |
|
505 | - */ |
|
506 | - public function find($id, $relations = [], $columns = array('*')) |
|
507 | - { |
|
508 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->find($id, $columns); |
|
509 | - } |
|
497 | + /** |
|
498 | + * Fetch records from the storage based on the given |
|
499 | + * id. |
|
500 | + * |
|
501 | + * @param integer $id |
|
502 | + * @param array $relations |
|
503 | + * @param array $columns |
|
504 | + * @return object |
|
505 | + */ |
|
506 | + public function find($id, $relations = [], $columns = array('*')) |
|
507 | + { |
|
508 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->find($id, $columns); |
|
509 | + } |
|
510 | 510 | |
511 | - /** |
|
512 | - * Fetch records from the storage based on the given |
|
513 | - * condition. |
|
514 | - * |
|
515 | - * @param array $conditions array of conditions |
|
516 | - * @param array $relations |
|
517 | - * @param string $sortBy |
|
518 | - * @param boolean $desc |
|
519 | - * @param array $columns |
|
520 | - * @return collection |
|
521 | - */ |
|
522 | - public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
523 | - { |
|
524 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
525 | - $sort = $desc ? 'desc' : 'asc'; |
|
526 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
527 | - } |
|
528 | - |
|
529 | - /** |
|
530 | - * Fetch the first record from the storage based on the given |
|
531 | - * condition. |
|
532 | - * |
|
533 | - * @param array $conditions array of conditions |
|
534 | - * @param array $relations |
|
535 | - * @param array $columns |
|
536 | - * @return object |
|
537 | - */ |
|
538 | - public function first($conditions, $relations = [], $columns = array('*')) |
|
539 | - { |
|
540 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
541 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
542 | - } |
|
543 | - |
|
544 | - /** |
|
545 | - * Return the deleted models in pages based on the given conditions. |
|
546 | - * |
|
547 | - * @param array $conditions array of conditions |
|
548 | - * @param integer $perPage |
|
549 | - * @param string $sortBy |
|
550 | - * @param boolean $desc |
|
551 | - * @param array $columns |
|
552 | - * @return collection |
|
553 | - */ |
|
554 | - public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
555 | - { |
|
556 | - unset($conditions['page']); |
|
557 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
558 | - $sort = $desc ? 'desc' : 'asc'; |
|
559 | - $model = $this->model->onlyTrashed(); |
|
560 | - |
|
561 | - if (count($conditions['conditionValues'])) |
|
562 | - { |
|
563 | - $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
564 | - } |
|
565 | - |
|
566 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns);; |
|
567 | - } |
|
568 | - |
|
569 | - /** |
|
570 | - * Restore the deleted model. |
|
571 | - * |
|
572 | - * @param integer $id |
|
573 | - * @return void |
|
574 | - */ |
|
575 | - public function restore($id) |
|
576 | - { |
|
577 | - $model = $this->model->onlyTrashed()->find($id); |
|
578 | - |
|
579 | - if ( ! $model) |
|
580 | - { |
|
581 | - \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $id); |
|
582 | - } |
|
583 | - |
|
584 | - $model->restore(); |
|
585 | - } |
|
586 | - |
|
587 | - /** |
|
588 | - * Build the conditions recursively for the retrieving methods. |
|
589 | - * @param array $conditions |
|
590 | - * @return array |
|
591 | - */ |
|
592 | - protected function constructConditions($conditions, $model) |
|
593 | - { |
|
594 | - $conditionString = ''; |
|
595 | - $conditionValues = []; |
|
596 | - foreach ($conditions as $key => $value) |
|
597 | - { |
|
598 | - if (str_contains($key, '->')) |
|
599 | - { |
|
600 | - $key = $this->wrapJsonSelector($key); |
|
601 | - } |
|
602 | - |
|
603 | - if ($key == 'and') |
|
604 | - { |
|
605 | - $conditions = $this->constructConditions($value, $model); |
|
606 | - $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']) . ' {op} '; |
|
607 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
608 | - } |
|
609 | - else if ($key == 'or') |
|
610 | - { |
|
611 | - $conditions = $this->constructConditions($value, $model); |
|
612 | - $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']) . ' {op} '; |
|
613 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
614 | - } |
|
615 | - else |
|
616 | - { |
|
617 | - if (is_array($value)) |
|
618 | - { |
|
619 | - $operator = $value['op']; |
|
620 | - if (strtolower($operator) == 'between') |
|
621 | - { |
|
622 | - $value1 = $value['val1']; |
|
623 | - $value2 = $value['val2']; |
|
624 | - } |
|
625 | - else |
|
626 | - { |
|
627 | - $value = array_key_exists('val', $value) ? $value['val'] : ''; |
|
628 | - } |
|
629 | - } |
|
630 | - else |
|
631 | - { |
|
632 | - $operator = '='; |
|
633 | - } |
|
511 | + /** |
|
512 | + * Fetch records from the storage based on the given |
|
513 | + * condition. |
|
514 | + * |
|
515 | + * @param array $conditions array of conditions |
|
516 | + * @param array $relations |
|
517 | + * @param string $sortBy |
|
518 | + * @param boolean $desc |
|
519 | + * @param array $columns |
|
520 | + * @return collection |
|
521 | + */ |
|
522 | + public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
523 | + { |
|
524 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
525 | + $sort = $desc ? 'desc' : 'asc'; |
|
526 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
527 | + } |
|
528 | + |
|
529 | + /** |
|
530 | + * Fetch the first record from the storage based on the given |
|
531 | + * condition. |
|
532 | + * |
|
533 | + * @param array $conditions array of conditions |
|
534 | + * @param array $relations |
|
535 | + * @param array $columns |
|
536 | + * @return object |
|
537 | + */ |
|
538 | + public function first($conditions, $relations = [], $columns = array('*')) |
|
539 | + { |
|
540 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
541 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
542 | + } |
|
543 | + |
|
544 | + /** |
|
545 | + * Return the deleted models in pages based on the given conditions. |
|
546 | + * |
|
547 | + * @param array $conditions array of conditions |
|
548 | + * @param integer $perPage |
|
549 | + * @param string $sortBy |
|
550 | + * @param boolean $desc |
|
551 | + * @param array $columns |
|
552 | + * @return collection |
|
553 | + */ |
|
554 | + public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
555 | + { |
|
556 | + unset($conditions['page']); |
|
557 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
558 | + $sort = $desc ? 'desc' : 'asc'; |
|
559 | + $model = $this->model->onlyTrashed(); |
|
560 | + |
|
561 | + if (count($conditions['conditionValues'])) |
|
562 | + { |
|
563 | + $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
564 | + } |
|
565 | + |
|
566 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns);; |
|
567 | + } |
|
568 | + |
|
569 | + /** |
|
570 | + * Restore the deleted model. |
|
571 | + * |
|
572 | + * @param integer $id |
|
573 | + * @return void |
|
574 | + */ |
|
575 | + public function restore($id) |
|
576 | + { |
|
577 | + $model = $this->model->onlyTrashed()->find($id); |
|
578 | + |
|
579 | + if ( ! $model) |
|
580 | + { |
|
581 | + \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $id); |
|
582 | + } |
|
583 | + |
|
584 | + $model->restore(); |
|
585 | + } |
|
586 | + |
|
587 | + /** |
|
588 | + * Build the conditions recursively for the retrieving methods. |
|
589 | + * @param array $conditions |
|
590 | + * @return array |
|
591 | + */ |
|
592 | + protected function constructConditions($conditions, $model) |
|
593 | + { |
|
594 | + $conditionString = ''; |
|
595 | + $conditionValues = []; |
|
596 | + foreach ($conditions as $key => $value) |
|
597 | + { |
|
598 | + if (str_contains($key, '->')) |
|
599 | + { |
|
600 | + $key = $this->wrapJsonSelector($key); |
|
601 | + } |
|
602 | + |
|
603 | + if ($key == 'and') |
|
604 | + { |
|
605 | + $conditions = $this->constructConditions($value, $model); |
|
606 | + $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']) . ' {op} '; |
|
607 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
608 | + } |
|
609 | + else if ($key == 'or') |
|
610 | + { |
|
611 | + $conditions = $this->constructConditions($value, $model); |
|
612 | + $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']) . ' {op} '; |
|
613 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
614 | + } |
|
615 | + else |
|
616 | + { |
|
617 | + if (is_array($value)) |
|
618 | + { |
|
619 | + $operator = $value['op']; |
|
620 | + if (strtolower($operator) == 'between') |
|
621 | + { |
|
622 | + $value1 = $value['val1']; |
|
623 | + $value2 = $value['val2']; |
|
624 | + } |
|
625 | + else |
|
626 | + { |
|
627 | + $value = array_key_exists('val', $value) ? $value['val'] : ''; |
|
628 | + } |
|
629 | + } |
|
630 | + else |
|
631 | + { |
|
632 | + $operator = '='; |
|
633 | + } |
|
634 | 634 | |
635 | - if (strtolower($operator) == 'between') |
|
636 | - { |
|
637 | - $conditionString .= $key . ' >= ? and '; |
|
638 | - $conditionValues[] = $value1; |
|
639 | - |
|
640 | - $conditionString .= $key . ' <= ? {op} '; |
|
641 | - $conditionValues[] = $value2; |
|
642 | - } |
|
643 | - elseif (strtolower($operator) == 'in') |
|
644 | - { |
|
645 | - $conditionValues = array_merge($conditionValues, $value); |
|
646 | - $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
647 | - $conditionString .= $key . ' in (' . rtrim($inBindingsString, ',') . ') {op} '; |
|
648 | - } |
|
649 | - elseif (strtolower($operator) == 'null') |
|
650 | - { |
|
651 | - $conditionString .= $key . ' is null {op} '; |
|
652 | - } |
|
653 | - elseif (strtolower($operator) == 'not null') |
|
654 | - { |
|
655 | - $conditionString .= $key . ' is not null {op} '; |
|
656 | - } |
|
657 | - elseif (strtolower($operator) == 'has') |
|
658 | - { |
|
659 | - $sql = $model->withTrashed()->has($key)->toSql(); |
|
660 | - $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
661 | - $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')') . ' and ' . $conditions['conditionString'] . ') {op} '; |
|
662 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
663 | - } |
|
664 | - else |
|
665 | - { |
|
666 | - $conditionString .= $key . ' ' . $operator . ' ? {op} '; |
|
667 | - $conditionValues[] = $value; |
|
668 | - } |
|
669 | - } |
|
670 | - } |
|
671 | - $conditionString = '(' . rtrim($conditionString, '{op} ') . ')'; |
|
672 | - return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
673 | - } |
|
674 | - |
|
675 | - /** |
|
676 | - * Wrap the given JSON selector. |
|
677 | - * |
|
678 | - * @param string $value |
|
679 | - * @return string |
|
680 | - */ |
|
681 | - protected function wrapJsonSelector($value) |
|
682 | - { |
|
683 | - $removeLast = strpos($value, ')'); |
|
684 | - $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
|
685 | - $path = explode('->', $value); |
|
686 | - $field = array_shift($path); |
|
687 | - $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
688 | - return '"'.$part.'"'; |
|
689 | - })->implode('.')); |
|
635 | + if (strtolower($operator) == 'between') |
|
636 | + { |
|
637 | + $conditionString .= $key . ' >= ? and '; |
|
638 | + $conditionValues[] = $value1; |
|
639 | + |
|
640 | + $conditionString .= $key . ' <= ? {op} '; |
|
641 | + $conditionValues[] = $value2; |
|
642 | + } |
|
643 | + elseif (strtolower($operator) == 'in') |
|
644 | + { |
|
645 | + $conditionValues = array_merge($conditionValues, $value); |
|
646 | + $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
647 | + $conditionString .= $key . ' in (' . rtrim($inBindingsString, ',') . ') {op} '; |
|
648 | + } |
|
649 | + elseif (strtolower($operator) == 'null') |
|
650 | + { |
|
651 | + $conditionString .= $key . ' is null {op} '; |
|
652 | + } |
|
653 | + elseif (strtolower($operator) == 'not null') |
|
654 | + { |
|
655 | + $conditionString .= $key . ' is not null {op} '; |
|
656 | + } |
|
657 | + elseif (strtolower($operator) == 'has') |
|
658 | + { |
|
659 | + $sql = $model->withTrashed()->has($key)->toSql(); |
|
660 | + $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
661 | + $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')') . ' and ' . $conditions['conditionString'] . ') {op} '; |
|
662 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
663 | + } |
|
664 | + else |
|
665 | + { |
|
666 | + $conditionString .= $key . ' ' . $operator . ' ? {op} '; |
|
667 | + $conditionValues[] = $value; |
|
668 | + } |
|
669 | + } |
|
670 | + } |
|
671 | + $conditionString = '(' . rtrim($conditionString, '{op} ') . ')'; |
|
672 | + return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
673 | + } |
|
674 | + |
|
675 | + /** |
|
676 | + * Wrap the given JSON selector. |
|
677 | + * |
|
678 | + * @param string $value |
|
679 | + * @return string |
|
680 | + */ |
|
681 | + protected function wrapJsonSelector($value) |
|
682 | + { |
|
683 | + $removeLast = strpos($value, ')'); |
|
684 | + $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
|
685 | + $path = explode('->', $value); |
|
686 | + $field = array_shift($path); |
|
687 | + $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
688 | + return '"'.$part.'"'; |
|
689 | + })->implode('.')); |
|
690 | 690 | |
691 | - return $removeLast === false ? $result : $result . ')'; |
|
692 | - } |
|
693 | - |
|
694 | - /** |
|
695 | - * Abstract method that return the necessary |
|
696 | - * information (full model namespace) |
|
697 | - * needed to preform the previous actions. |
|
698 | - * |
|
699 | - * @return string |
|
700 | - */ |
|
701 | - abstract protected function getModel(); |
|
691 | + return $removeLast === false ? $result : $result . ')'; |
|
692 | + } |
|
693 | + |
|
694 | + /** |
|
695 | + * Abstract method that return the necessary |
|
696 | + * information (full model namespace) |
|
697 | + * needed to preform the previous actions. |
|
698 | + * |
|
699 | + * @return string |
|
700 | + */ |
|
701 | + abstract protected function getModel(); |
|
702 | 702 | } |
703 | 703 | \ No newline at end of file |
@@ -63,11 +63,11 @@ 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 | - $column = 'LOWER(' . array_shift($conditionColumns) . ')'; |
|
70 | + $column = 'LOWER('.array_shift($conditionColumns).')'; |
|
71 | 71 | if (str_contains($column, '->')) |
72 | 72 | { |
73 | 73 | $column = $this->wrapJsonSelector($column); |
@@ -76,7 +76,7 @@ discard block |
||
76 | 76 | /** |
77 | 77 | * Use the first element in the model columns to construct the first condition. |
78 | 78 | */ |
79 | - $q->where(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
79 | + $q->where(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
80 | 80 | } |
81 | 81 | |
82 | 82 | /** |
@@ -84,13 +84,13 @@ discard block |
||
84 | 84 | */ |
85 | 85 | foreach ($conditionColumns as $column) |
86 | 86 | { |
87 | - $column = 'LOWER(' . $column . ')'; |
|
87 | + $column = 'LOWER('.$column.')'; |
|
88 | 88 | if (str_contains($column, '->')) |
89 | 89 | { |
90 | 90 | $column = $this->wrapJsonSelector($column); |
91 | 91 | } |
92 | 92 | |
93 | - $q->orWhere(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
93 | + $q->orWhere(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
94 | 94 | } |
95 | 95 | |
96 | 96 | /** |
@@ -111,9 +111,9 @@ discard block |
||
111 | 111 | /** |
112 | 112 | * Construct the relation condition. |
113 | 113 | */ |
114 | - $q->orWhereHas($relation, function ($subModel) use ($query, $relation){ |
|
114 | + $q->orWhereHas($relation, function($subModel) use ($query, $relation){ |
|
115 | 115 | |
116 | - $subModel->where(function ($q) use ($query, $relation){ |
|
116 | + $subModel->where(function($q) use ($query, $relation){ |
|
117 | 117 | |
118 | 118 | /** |
119 | 119 | * Get columns of the relation. |
@@ -123,7 +123,7 @@ discard block |
||
123 | 123 | if (count($subConditionColumns)) |
124 | 124 | { |
125 | 125 | |
126 | - $column = 'LOWER(' . array_shift($subConditionColumns) . ')'; |
|
126 | + $column = 'LOWER('.array_shift($subConditionColumns).')'; |
|
127 | 127 | if (str_contains($column, '->')) |
128 | 128 | { |
129 | 129 | $column = $this->wrapJsonSelector($column); |
@@ -132,7 +132,7 @@ discard block |
||
132 | 132 | /** |
133 | 133 | * Use the first element in the relation model columns to construct the first condition. |
134 | 134 | */ |
135 | - $q->where(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
135 | + $q->where(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
136 | 136 | } |
137 | 137 | |
138 | 138 | /** |
@@ -140,13 +140,13 @@ discard block |
||
140 | 140 | */ |
141 | 141 | foreach ($subConditionColumns as $subConditionColumn) |
142 | 142 | { |
143 | - $column = 'LOWER(' . $subConditionColumn . ')'; |
|
143 | + $column = 'LOWER('.$subConditionColumn.')'; |
|
144 | 144 | if (str_contains($column, '->')) |
145 | 145 | { |
146 | 146 | $column = $this->wrapJsonSelector($column); |
147 | 147 | } |
148 | 148 | |
149 | - $q->orWhere(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
149 | + $q->orWhere(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
150 | 150 | } |
151 | 151 | }); |
152 | 152 | |
@@ -206,7 +206,7 @@ discard block |
||
206 | 206 | $modelClass = $this->model; |
207 | 207 | $relations = []; |
208 | 208 | |
209 | - \DB::transaction(function () use (&$model, &$relations, $data, $modelClass) { |
|
209 | + \DB::transaction(function() use (&$model, &$relations, $data, $modelClass) { |
|
210 | 210 | /** |
211 | 211 | * If the id is present in the data then select the model for updating, |
212 | 212 | * else create new model. |
@@ -215,7 +215,7 @@ discard block |
||
215 | 215 | $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
216 | 216 | if ( ! $model) |
217 | 217 | { |
218 | - \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']); |
|
218 | + \ErrorHandler::notFound(class_basename($modelClass).' with id : '.$data['id']); |
|
219 | 219 | } |
220 | 220 | |
221 | 221 | /** |
@@ -273,7 +273,7 @@ discard block |
||
273 | 273 | */ |
274 | 274 | if ( ! $relationModel) |
275 | 275 | { |
276 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']); |
|
276 | + \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
|
277 | 277 | } |
278 | 278 | |
279 | 279 | /** |
@@ -284,7 +284,7 @@ discard block |
||
284 | 284 | /** |
285 | 285 | * Prevent the sub relations or attributes not in the fillable. |
286 | 286 | */ |
287 | - if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
287 | + if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
288 | 288 | { |
289 | 289 | $relationModel->$attr = $val; |
290 | 290 | } |
@@ -314,7 +314,7 @@ discard block |
||
314 | 314 | */ |
315 | 315 | if ( ! $relationModel) |
316 | 316 | { |
317 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']); |
|
317 | + \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
|
318 | 318 | } |
319 | 319 | |
320 | 320 | foreach ($value as $relationAttribute => $relationValue) |
@@ -456,7 +456,7 @@ discard block |
||
456 | 456 | } |
457 | 457 | else |
458 | 458 | { |
459 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data){ |
|
459 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function($model) use ($data){ |
|
460 | 460 | $model->update($data); |
461 | 461 | }); |
462 | 462 | } |
@@ -474,11 +474,11 @@ discard block |
||
474 | 474 | { |
475 | 475 | if ($attribute == 'id') |
476 | 476 | { |
477 | - \DB::transaction(function () use ($value, $attribute, &$result) { |
|
477 | + \DB::transaction(function() use ($value, $attribute, &$result) { |
|
478 | 478 | $model = $this->model->lockForUpdate()->find($value); |
479 | 479 | if ( ! $model) |
480 | 480 | { |
481 | - \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $value); |
|
481 | + \ErrorHandler::notFound(class_basename($this->model).' with id : '.$value); |
|
482 | 482 | } |
483 | 483 | |
484 | 484 | $model->delete(); |
@@ -486,8 +486,8 @@ discard block |
||
486 | 486 | } |
487 | 487 | else |
488 | 488 | { |
489 | - \DB::transaction(function () use ($value, $attribute, &$result) { |
|
490 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model){ |
|
489 | + \DB::transaction(function() use ($value, $attribute, &$result) { |
|
490 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function($model) { |
|
491 | 491 | $model->delete(); |
492 | 492 | }); |
493 | 493 | }); |
@@ -523,7 +523,7 @@ discard block |
||
523 | 523 | { |
524 | 524 | $conditions = $this->constructConditions($conditions, $this->model); |
525 | 525 | $sort = $desc ? 'desc' : 'asc'; |
526 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
526 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
527 | 527 | } |
528 | 528 | |
529 | 529 | /** |
@@ -563,7 +563,7 @@ discard block |
||
563 | 563 | $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
564 | 564 | } |
565 | 565 | |
566 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns);; |
|
566 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); ; |
|
567 | 567 | } |
568 | 568 | |
569 | 569 | /** |
@@ -578,7 +578,7 @@ discard block |
||
578 | 578 | |
579 | 579 | if ( ! $model) |
580 | 580 | { |
581 | - \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $id); |
|
581 | + \ErrorHandler::notFound(class_basename($this->model).' with id : '.$id); |
|
582 | 582 | } |
583 | 583 | |
584 | 584 | $model->restore(); |
@@ -603,13 +603,13 @@ discard block |
||
603 | 603 | if ($key == 'and') |
604 | 604 | { |
605 | 605 | $conditions = $this->constructConditions($value, $model); |
606 | - $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']) . ' {op} '; |
|
606 | + $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']).' {op} '; |
|
607 | 607 | $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
608 | 608 | } |
609 | 609 | else if ($key == 'or') |
610 | 610 | { |
611 | 611 | $conditions = $this->constructConditions($value, $model); |
612 | - $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']) . ' {op} '; |
|
612 | + $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']).' {op} '; |
|
613 | 613 | $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
614 | 614 | } |
615 | 615 | else |
@@ -634,41 +634,41 @@ discard block |
||
634 | 634 | |
635 | 635 | if (strtolower($operator) == 'between') |
636 | 636 | { |
637 | - $conditionString .= $key . ' >= ? and '; |
|
637 | + $conditionString .= $key.' >= ? and '; |
|
638 | 638 | $conditionValues[] = $value1; |
639 | 639 | |
640 | - $conditionString .= $key . ' <= ? {op} '; |
|
640 | + $conditionString .= $key.' <= ? {op} '; |
|
641 | 641 | $conditionValues[] = $value2; |
642 | 642 | } |
643 | 643 | elseif (strtolower($operator) == 'in') |
644 | 644 | { |
645 | 645 | $conditionValues = array_merge($conditionValues, $value); |
646 | 646 | $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
647 | - $conditionString .= $key . ' in (' . rtrim($inBindingsString, ',') . ') {op} '; |
|
647 | + $conditionString .= $key.' in ('.rtrim($inBindingsString, ',').') {op} '; |
|
648 | 648 | } |
649 | 649 | elseif (strtolower($operator) == 'null') |
650 | 650 | { |
651 | - $conditionString .= $key . ' is null {op} '; |
|
651 | + $conditionString .= $key.' is null {op} '; |
|
652 | 652 | } |
653 | 653 | elseif (strtolower($operator) == 'not null') |
654 | 654 | { |
655 | - $conditionString .= $key . ' is not null {op} '; |
|
655 | + $conditionString .= $key.' is not null {op} '; |
|
656 | 656 | } |
657 | 657 | elseif (strtolower($operator) == 'has') |
658 | 658 | { |
659 | 659 | $sql = $model->withTrashed()->has($key)->toSql(); |
660 | 660 | $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
661 | - $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')') . ' and ' . $conditions['conditionString'] . ') {op} '; |
|
661 | + $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')').' and '.$conditions['conditionString'].') {op} '; |
|
662 | 662 | $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
663 | 663 | } |
664 | 664 | else |
665 | 665 | { |
666 | - $conditionString .= $key . ' ' . $operator . ' ? {op} '; |
|
666 | + $conditionString .= $key.' '.$operator.' ? {op} '; |
|
667 | 667 | $conditionValues[] = $value; |
668 | 668 | } |
669 | 669 | } |
670 | 670 | } |
671 | - $conditionString = '(' . rtrim($conditionString, '{op} ') . ')'; |
|
671 | + $conditionString = '('.rtrim($conditionString, '{op} ').')'; |
|
672 | 672 | return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
673 | 673 | } |
674 | 674 | |
@@ -684,11 +684,11 @@ discard block |
||
684 | 684 | $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
685 | 685 | $path = explode('->', $value); |
686 | 686 | $field = array_shift($path); |
687 | - $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
687 | + $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function($part) { |
|
688 | 688 | return '"'.$part.'"'; |
689 | 689 | })->implode('.')); |
690 | 690 | |
691 | - return $removeLast === false ? $result : $result . ')'; |
|
691 | + return $removeLast === false ? $result : $result.')'; |
|
692 | 692 | } |
693 | 693 | |
694 | 694 | /** |
@@ -453,8 +453,7 @@ discard block |
||
453 | 453 | { |
454 | 454 | $model = $this->model->lockForUpdate()->find($value); |
455 | 455 | $model ? $model->update($data) : 0; |
456 | - } |
|
457 | - else |
|
456 | + } else |
|
458 | 457 | { |
459 | 458 | call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data){ |
460 | 459 | $model->update($data); |
@@ -483,8 +482,7 @@ discard block |
||
483 | 482 | |
484 | 483 | $model->delete(); |
485 | 484 | }); |
486 | - } |
|
487 | - else |
|
485 | + } else |
|
488 | 486 | { |
489 | 487 | \DB::transaction(function () use ($value, $attribute, &$result) { |
490 | 488 | call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model){ |
@@ -605,14 +603,12 @@ discard block |
||
605 | 603 | $conditions = $this->constructConditions($value, $model); |
606 | 604 | $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']) . ' {op} '; |
607 | 605 | $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
608 | - } |
|
609 | - else if ($key == 'or') |
|
606 | + } else if ($key == 'or') |
|
610 | 607 | { |
611 | 608 | $conditions = $this->constructConditions($value, $model); |
612 | 609 | $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']) . ' {op} '; |
613 | 610 | $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
614 | - } |
|
615 | - else |
|
611 | + } else |
|
616 | 612 | { |
617 | 613 | if (is_array($value)) |
618 | 614 | { |
@@ -621,13 +617,11 @@ discard block |
||
621 | 617 | { |
622 | 618 | $value1 = $value['val1']; |
623 | 619 | $value2 = $value['val2']; |
624 | - } |
|
625 | - else |
|
620 | + } else |
|
626 | 621 | { |
627 | 622 | $value = array_key_exists('val', $value) ? $value['val'] : ''; |
628 | 623 | } |
629 | - } |
|
630 | - else |
|
624 | + } else |
|
631 | 625 | { |
632 | 626 | $operator = '='; |
633 | 627 | } |
@@ -639,29 +633,24 @@ discard block |
||
639 | 633 | |
640 | 634 | $conditionString .= $key . ' <= ? {op} '; |
641 | 635 | $conditionValues[] = $value2; |
642 | - } |
|
643 | - elseif (strtolower($operator) == 'in') |
|
636 | + } elseif (strtolower($operator) == 'in') |
|
644 | 637 | { |
645 | 638 | $conditionValues = array_merge($conditionValues, $value); |
646 | 639 | $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
647 | 640 | $conditionString .= $key . ' in (' . rtrim($inBindingsString, ',') . ') {op} '; |
648 | - } |
|
649 | - elseif (strtolower($operator) == 'null') |
|
641 | + } elseif (strtolower($operator) == 'null') |
|
650 | 642 | { |
651 | 643 | $conditionString .= $key . ' is null {op} '; |
652 | - } |
|
653 | - elseif (strtolower($operator) == 'not null') |
|
644 | + } elseif (strtolower($operator) == 'not null') |
|
654 | 645 | { |
655 | 646 | $conditionString .= $key . ' is not null {op} '; |
656 | - } |
|
657 | - elseif (strtolower($operator) == 'has') |
|
647 | + } elseif (strtolower($operator) == 'has') |
|
658 | 648 | { |
659 | 649 | $sql = $model->withTrashed()->has($key)->toSql(); |
660 | 650 | $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
661 | 651 | $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')') . ' and ' . $conditions['conditionString'] . ') {op} '; |
662 | 652 | $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
663 | - } |
|
664 | - else |
|
653 | + } else |
|
665 | 654 | { |
666 | 655 | $conditionString .= $key . ' ' . $operator . ' ? {op} '; |
667 | 656 | $conditionValues[] = $value; |
@@ -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 | - 'invalidRefreshToken' => 'Invalid refresh token', |
|
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 | + 'invalidRefreshToken' => 'Invalid refresh token', |
|
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 |
@@ -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 | - 'invalidRefreshToken' => 'رمز التحديث غير صالح', |
|
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 | + 'invalidRefreshToken' => 'رمز التحديث غير صالح', |
|
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 |
@@ -8,46 +8,46 @@ |
||
8 | 8 | |
9 | 9 | class OauthClientsController 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 = 'oauthClients'; |
|
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 = 'oauthClients'; |
|
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|max:255', |
|
25 | - 'redirect' => 'required|url', |
|
26 | - 'user_id' => 'required|array|exists:users,id' |
|
27 | - 'personal_access_client' => 'boolean', |
|
28 | - 'password_client' => 'boolean', |
|
29 | - 'revoked' => 'boolean' |
|
30 | - ]; |
|
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|max:255', |
|
25 | + 'redirect' => 'required|url', |
|
26 | + 'user_id' => 'required|array|exists:users,id' |
|
27 | + 'personal_access_client' => 'boolean', |
|
28 | + 'password_client' => 'boolean', |
|
29 | + 'revoked' => 'boolean' |
|
30 | + ]; |
|
31 | 31 | |
32 | - /** |
|
33 | - * Revoke the given client. |
|
34 | - * |
|
35 | - * @param integer $clientId |
|
36 | - * @return \Illuminate\Http\Response |
|
37 | - */ |
|
38 | - public function revoke($clientId) |
|
39 | - { |
|
40 | - return \Response::json($this->repo->revoke($clientId), 200); |
|
41 | - } |
|
32 | + /** |
|
33 | + * Revoke the given client. |
|
34 | + * |
|
35 | + * @param integer $clientId |
|
36 | + * @return \Illuminate\Http\Response |
|
37 | + */ |
|
38 | + public function revoke($clientId) |
|
39 | + { |
|
40 | + return \Response::json($this->repo->revoke($clientId), 200); |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * Regenerate seceret for the given client. |
|
45 | - * |
|
46 | - * @param integer $clientId |
|
47 | - * @return \Illuminate\Http\Response |
|
48 | - */ |
|
49 | - public function regenerateSecret($clientId) |
|
50 | - { |
|
51 | - return \Response::json($this->repo->regenerateSecret($clientId), 200); |
|
52 | - } |
|
43 | + /** |
|
44 | + * Regenerate seceret for the given client. |
|
45 | + * |
|
46 | + * @param integer $clientId |
|
47 | + * @return \Illuminate\Http\Response |
|
48 | + */ |
|
49 | + public function regenerateSecret($clientId) |
|
50 | + { |
|
51 | + return \Response::json($this->repo->regenerateSecret($clientId), 200); |
|
52 | + } |
|
53 | 53 | } |
@@ -13,14 +13,14 @@ |
||
13 | 13 | * to preform actions like (add, edit ... etc). |
14 | 14 | * @var string |
15 | 15 | */ |
16 | - protected $model = 'oauthClients'; |
|
16 | + protected $model = 'oauthClients'; |
|
17 | 17 | |
18 | 18 | /** |
19 | 19 | * The validations rules used by the base api controller |
20 | 20 | * to check before add. |
21 | 21 | * @var array |
22 | 22 | */ |
23 | - protected $validationRules = [ |
|
23 | + protected $validationRules = [ |
|
24 | 24 | 'name' => 'required|max:255', |
25 | 25 | 'redirect' => 'required|url', |
26 | 26 | 'user_id' => 'required|array|exists:users,id' |
@@ -7,368 +7,368 @@ |
||
7 | 7 | |
8 | 8 | class UserRepository extends AbstractRepository |
9 | 9 | { |
10 | - /** |
|
11 | - * Return the model full namespace. |
|
12 | - * |
|
13 | - * @return string |
|
14 | - */ |
|
15 | - protected function getModel() |
|
16 | - { |
|
17 | - return 'App\Modules\V1\Acl\AclUser'; |
|
18 | - } |
|
19 | - |
|
20 | - /** |
|
21 | - * The loginProxy implementation. |
|
22 | - * |
|
23 | - * @var array |
|
24 | - */ |
|
25 | - protected $loginProxy; |
|
26 | - |
|
27 | - /** |
|
28 | - * @var The accessTokenRepository implementation. |
|
29 | - */ |
|
30 | - private $accessTokenRepository; |
|
31 | - |
|
32 | - public function __construct(LoginProxy $loginProxy, AccessTokenRepositoryInterface $accessTokenRepository) |
|
33 | - { |
|
34 | - $this->loginProxy = $loginProxy; |
|
35 | - $this->accessTokenRepository = $accessTokenRepository; |
|
36 | - parent::__construct(); |
|
37 | - } |
|
38 | - |
|
39 | - /** |
|
40 | - * Return the logged in user account. |
|
41 | - * |
|
42 | - * @param array $relations |
|
43 | - * @return boolean |
|
44 | - */ |
|
45 | - public function account($relations = []) |
|
46 | - { |
|
47 | - $permissions = []; |
|
48 | - $user = \Core::users()->find(\Auth::id(), $relations); |
|
49 | - foreach ($user->groups()->get() as $group) |
|
50 | - { |
|
51 | - $group->permissions->each(function ($permission) use (&$permissions){ |
|
52 | - $permissions[$permission->model][$permission->id] = $permission->name; |
|
53 | - }); |
|
54 | - } |
|
55 | - $user->permissions = $permissions; |
|
56 | - |
|
57 | - return $user; |
|
58 | - } |
|
59 | - |
|
60 | - /** |
|
61 | - * Check if the logged in user or the given user |
|
62 | - * has the given permissions on the given model. |
|
63 | - * |
|
64 | - * @param string $nameOfPermission |
|
65 | - * @param string $model |
|
66 | - * @param boolean $user |
|
67 | - * @return boolean |
|
68 | - */ |
|
69 | - public function can($nameOfPermission, $model, $user = false ) |
|
70 | - { |
|
71 | - $user = $user = $this->find(\Auth::id(), ['groups.permissions']); |
|
72 | - $permissions = []; |
|
73 | - |
|
74 | - $user->groups->pluck('permissions')->each(function ($permission) use (&$permissions, $model){ |
|
75 | - $permissions = array_merge($permissions, $permission->where('model', $model)->pluck('name')->toArray()); |
|
76 | - }); |
|
10 | + /** |
|
11 | + * Return the model full namespace. |
|
12 | + * |
|
13 | + * @return string |
|
14 | + */ |
|
15 | + protected function getModel() |
|
16 | + { |
|
17 | + return 'App\Modules\V1\Acl\AclUser'; |
|
18 | + } |
|
19 | + |
|
20 | + /** |
|
21 | + * The loginProxy implementation. |
|
22 | + * |
|
23 | + * @var array |
|
24 | + */ |
|
25 | + protected $loginProxy; |
|
26 | + |
|
27 | + /** |
|
28 | + * @var The accessTokenRepository implementation. |
|
29 | + */ |
|
30 | + private $accessTokenRepository; |
|
31 | + |
|
32 | + public function __construct(LoginProxy $loginProxy, AccessTokenRepositoryInterface $accessTokenRepository) |
|
33 | + { |
|
34 | + $this->loginProxy = $loginProxy; |
|
35 | + $this->accessTokenRepository = $accessTokenRepository; |
|
36 | + parent::__construct(); |
|
37 | + } |
|
38 | + |
|
39 | + /** |
|
40 | + * Return the logged in user account. |
|
41 | + * |
|
42 | + * @param array $relations |
|
43 | + * @return boolean |
|
44 | + */ |
|
45 | + public function account($relations = []) |
|
46 | + { |
|
47 | + $permissions = []; |
|
48 | + $user = \Core::users()->find(\Auth::id(), $relations); |
|
49 | + foreach ($user->groups()->get() as $group) |
|
50 | + { |
|
51 | + $group->permissions->each(function ($permission) use (&$permissions){ |
|
52 | + $permissions[$permission->model][$permission->id] = $permission->name; |
|
53 | + }); |
|
54 | + } |
|
55 | + $user->permissions = $permissions; |
|
56 | + |
|
57 | + return $user; |
|
58 | + } |
|
59 | + |
|
60 | + /** |
|
61 | + * Check if the logged in user or the given user |
|
62 | + * has the given permissions on the given model. |
|
63 | + * |
|
64 | + * @param string $nameOfPermission |
|
65 | + * @param string $model |
|
66 | + * @param boolean $user |
|
67 | + * @return boolean |
|
68 | + */ |
|
69 | + public function can($nameOfPermission, $model, $user = false ) |
|
70 | + { |
|
71 | + $user = $user = $this->find(\Auth::id(), ['groups.permissions']); |
|
72 | + $permissions = []; |
|
73 | + |
|
74 | + $user->groups->pluck('permissions')->each(function ($permission) use (&$permissions, $model){ |
|
75 | + $permissions = array_merge($permissions, $permission->where('model', $model)->pluck('name')->toArray()); |
|
76 | + }); |
|
77 | 77 | |
78 | - return in_array($nameOfPermission, $permissions); |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * Check if the logged in user has the given group. |
|
83 | - * |
|
84 | - * @param string $groupName |
|
85 | - * @param integer $userId |
|
86 | - * @return boolean |
|
87 | - */ |
|
88 | - public function hasGroup($groupName, $userId = false) |
|
89 | - { |
|
90 | - $userId = $userId ?: \Auth::id(); |
|
91 | - $groups = $this->find($userId)->groups; |
|
92 | - return $groups->pluck('name')->search($groupName, true) === false ? false : true; |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * Assign the given group ids to the given user. |
|
97 | - * |
|
98 | - * @param integer $user_id |
|
99 | - * @param array $group_ids |
|
100 | - * @return object |
|
101 | - */ |
|
102 | - public function assignGroups($user_id, $group_ids) |
|
103 | - { |
|
104 | - \DB::transaction(function () use ($user_id, $group_ids) { |
|
105 | - $user = $this->find($user_id); |
|
106 | - $user->groups()->detach(); |
|
107 | - $user->groups()->attach($group_ids); |
|
108 | - }); |
|
109 | - |
|
110 | - return $this->find($user_id); |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * Handle a login request to the application. |
|
115 | - * |
|
116 | - * @param array $credentials |
|
117 | - * @param boolean $adminLogin |
|
118 | - * @return object |
|
119 | - */ |
|
120 | - public function login($credentials, $adminLogin = false) |
|
121 | - { |
|
122 | - if ( ! $user = $this->first(['email' => $credentials['email']])) |
|
123 | - { |
|
124 | - \ErrorHandler::loginFailed(); |
|
125 | - } |
|
126 | - else if ($adminLogin && $user->groups->pluck('name')->search('Admin', true) === false) |
|
127 | - { |
|
128 | - \ErrorHandler::loginFailed(); |
|
129 | - } |
|
130 | - else if ( ! $adminLogin && $user->groups->pluck('name')->search('Admin', true) !== false) |
|
131 | - { |
|
132 | - \ErrorHandler::loginFailed(); |
|
133 | - } |
|
134 | - else if ($user->blocked) |
|
135 | - { |
|
136 | - \ErrorHandler::userIsBlocked(); |
|
137 | - } |
|
138 | - |
|
139 | - return $user; |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * Handle a social login request of the none admin to the application. |
|
144 | - * |
|
145 | - * @param array $credentials |
|
146 | - * @return array |
|
147 | - */ |
|
148 | - public function loginSocial($credentials) |
|
149 | - { |
|
150 | - $access_token = $credentials['auth_code'] ? \Socialite::driver($credentials['type'])->getAccessToken($credentials['auth_code']) : $credentials['access_token']; |
|
151 | - $user = \Socialite::driver($credentials['type'])->userFromToken($access_token); |
|
152 | - |
|
153 | - if ( ! $user->email) |
|
154 | - { |
|
155 | - \ErrorHandler::noSocialEmail(); |
|
156 | - } |
|
157 | - |
|
158 | - if ( ! $registeredUser = $this->model->where('email', $user->email)->first()) |
|
159 | - { |
|
160 | - $data = ['email' => $user->email, 'password' => '']; |
|
161 | - return $this->register($data); |
|
162 | - } |
|
163 | - else |
|
164 | - { |
|
165 | - if ( ! \Auth::attempt(['email' => $registeredUser->email, 'password' => ''])) |
|
166 | - { |
|
167 | - \ErrorHandler::userAlreadyRegistered(); |
|
168 | - } |
|
169 | - |
|
170 | - return $this->loginProxy->login(['email' => $registeredUser->email, 'password' => ''], 0); |
|
171 | - } |
|
172 | - } |
|
78 | + return in_array($nameOfPermission, $permissions); |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * Check if the logged in user has the given group. |
|
83 | + * |
|
84 | + * @param string $groupName |
|
85 | + * @param integer $userId |
|
86 | + * @return boolean |
|
87 | + */ |
|
88 | + public function hasGroup($groupName, $userId = false) |
|
89 | + { |
|
90 | + $userId = $userId ?: \Auth::id(); |
|
91 | + $groups = $this->find($userId)->groups; |
|
92 | + return $groups->pluck('name')->search($groupName, true) === false ? false : true; |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * Assign the given group ids to the given user. |
|
97 | + * |
|
98 | + * @param integer $user_id |
|
99 | + * @param array $group_ids |
|
100 | + * @return object |
|
101 | + */ |
|
102 | + public function assignGroups($user_id, $group_ids) |
|
103 | + { |
|
104 | + \DB::transaction(function () use ($user_id, $group_ids) { |
|
105 | + $user = $this->find($user_id); |
|
106 | + $user->groups()->detach(); |
|
107 | + $user->groups()->attach($group_ids); |
|
108 | + }); |
|
109 | + |
|
110 | + return $this->find($user_id); |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * Handle a login request to the application. |
|
115 | + * |
|
116 | + * @param array $credentials |
|
117 | + * @param boolean $adminLogin |
|
118 | + * @return object |
|
119 | + */ |
|
120 | + public function login($credentials, $adminLogin = false) |
|
121 | + { |
|
122 | + if ( ! $user = $this->first(['email' => $credentials['email']])) |
|
123 | + { |
|
124 | + \ErrorHandler::loginFailed(); |
|
125 | + } |
|
126 | + else if ($adminLogin && $user->groups->pluck('name')->search('Admin', true) === false) |
|
127 | + { |
|
128 | + \ErrorHandler::loginFailed(); |
|
129 | + } |
|
130 | + else if ( ! $adminLogin && $user->groups->pluck('name')->search('Admin', true) !== false) |
|
131 | + { |
|
132 | + \ErrorHandler::loginFailed(); |
|
133 | + } |
|
134 | + else if ($user->blocked) |
|
135 | + { |
|
136 | + \ErrorHandler::userIsBlocked(); |
|
137 | + } |
|
138 | + |
|
139 | + return $user; |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * Handle a social login request of the none admin to the application. |
|
144 | + * |
|
145 | + * @param array $credentials |
|
146 | + * @return array |
|
147 | + */ |
|
148 | + public function loginSocial($credentials) |
|
149 | + { |
|
150 | + $access_token = $credentials['auth_code'] ? \Socialite::driver($credentials['type'])->getAccessToken($credentials['auth_code']) : $credentials['access_token']; |
|
151 | + $user = \Socialite::driver($credentials['type'])->userFromToken($access_token); |
|
152 | + |
|
153 | + if ( ! $user->email) |
|
154 | + { |
|
155 | + \ErrorHandler::noSocialEmail(); |
|
156 | + } |
|
157 | + |
|
158 | + if ( ! $registeredUser = $this->model->where('email', $user->email)->first()) |
|
159 | + { |
|
160 | + $data = ['email' => $user->email, 'password' => '']; |
|
161 | + return $this->register($data); |
|
162 | + } |
|
163 | + else |
|
164 | + { |
|
165 | + if ( ! \Auth::attempt(['email' => $registeredUser->email, 'password' => ''])) |
|
166 | + { |
|
167 | + \ErrorHandler::userAlreadyRegistered(); |
|
168 | + } |
|
169 | + |
|
170 | + return $this->loginProxy->login(['email' => $registeredUser->email, 'password' => ''], 0); |
|
171 | + } |
|
172 | + } |
|
173 | 173 | |
174 | - /** |
|
175 | - * Handle a registration request. |
|
176 | - * |
|
177 | - * @param array $credentials |
|
178 | - * @return array |
|
179 | - */ |
|
180 | - public function register($credentials) |
|
181 | - { |
|
182 | - $this->model->create($credentials) |
|
183 | - return $this->loginProxy->login($credentials, 0); |
|
184 | - } |
|
185 | - |
|
186 | - /** |
|
187 | - * Block the user. |
|
188 | - * |
|
189 | - * @param integer $user_id |
|
190 | - * @return object |
|
191 | - */ |
|
192 | - public function block($user_id) |
|
193 | - { |
|
194 | - if ( ! $user = $this->find($user_id)) |
|
195 | - { |
|
196 | - \ErrorHandler::notFound('user'); |
|
197 | - } |
|
198 | - if ( ! $this->hasGroup('Admin')) |
|
199 | - { |
|
200 | - \ErrorHandler::noPermissions(); |
|
201 | - } |
|
202 | - else if (\Auth::id() == $user_id) |
|
203 | - { |
|
204 | - \ErrorHandler::noPermissions(); |
|
205 | - } |
|
206 | - else if ($user->groups->pluck('name')->search('Admin', true) !== false) |
|
207 | - { |
|
208 | - \ErrorHandler::noPermissions(); |
|
209 | - } |
|
210 | - |
|
211 | - $user->blocked = 1; |
|
212 | - $user->save(); |
|
174 | + /** |
|
175 | + * Handle a registration request. |
|
176 | + * |
|
177 | + * @param array $credentials |
|
178 | + * @return array |
|
179 | + */ |
|
180 | + public function register($credentials) |
|
181 | + { |
|
182 | + $this->model->create($credentials) |
|
183 | + return $this->loginProxy->login($credentials, 0); |
|
184 | + } |
|
185 | + |
|
186 | + /** |
|
187 | + * Block the user. |
|
188 | + * |
|
189 | + * @param integer $user_id |
|
190 | + * @return object |
|
191 | + */ |
|
192 | + public function block($user_id) |
|
193 | + { |
|
194 | + if ( ! $user = $this->find($user_id)) |
|
195 | + { |
|
196 | + \ErrorHandler::notFound('user'); |
|
197 | + } |
|
198 | + if ( ! $this->hasGroup('Admin')) |
|
199 | + { |
|
200 | + \ErrorHandler::noPermissions(); |
|
201 | + } |
|
202 | + else if (\Auth::id() == $user_id) |
|
203 | + { |
|
204 | + \ErrorHandler::noPermissions(); |
|
205 | + } |
|
206 | + else if ($user->groups->pluck('name')->search('Admin', true) !== false) |
|
207 | + { |
|
208 | + \ErrorHandler::noPermissions(); |
|
209 | + } |
|
210 | + |
|
211 | + $user->blocked = 1; |
|
212 | + $user->save(); |
|
213 | 213 | |
214 | - return $user; |
|
215 | - } |
|
216 | - |
|
217 | - /** |
|
218 | - * Unblock the user. |
|
219 | - * |
|
220 | - * @param integer $user_id |
|
221 | - * @return object |
|
222 | - */ |
|
223 | - public function unblock($user_id) |
|
224 | - { |
|
225 | - if ( ! $this->hasGroup('Admin')) |
|
226 | - { |
|
227 | - \ErrorHandler::noPermissions(); |
|
228 | - } |
|
229 | - |
|
230 | - $user = $this->find($user_id); |
|
231 | - $user->blocked = 0; |
|
232 | - $user->save(); |
|
233 | - |
|
234 | - return $user; |
|
235 | - } |
|
236 | - |
|
237 | - /** |
|
238 | - * Send a reset link to the given user. |
|
239 | - * |
|
240 | - * @param string $email |
|
241 | - * @return void |
|
242 | - */ |
|
243 | - public function sendReset($email) |
|
244 | - { |
|
245 | - if ( ! $user = $this->model->where('email', $email)->first()) |
|
246 | - { |
|
247 | - \ErrorHandler::notFound('email'); |
|
248 | - } |
|
249 | - |
|
250 | - $url = $this->config['resetLink']; |
|
251 | - $token = \Password::getRepository()->create($user); |
|
214 | + return $user; |
|
215 | + } |
|
216 | + |
|
217 | + /** |
|
218 | + * Unblock the user. |
|
219 | + * |
|
220 | + * @param integer $user_id |
|
221 | + * @return object |
|
222 | + */ |
|
223 | + public function unblock($user_id) |
|
224 | + { |
|
225 | + if ( ! $this->hasGroup('Admin')) |
|
226 | + { |
|
227 | + \ErrorHandler::noPermissions(); |
|
228 | + } |
|
229 | + |
|
230 | + $user = $this->find($user_id); |
|
231 | + $user->blocked = 0; |
|
232 | + $user->save(); |
|
233 | + |
|
234 | + return $user; |
|
235 | + } |
|
236 | + |
|
237 | + /** |
|
238 | + * Send a reset link to the given user. |
|
239 | + * |
|
240 | + * @param string $email |
|
241 | + * @return void |
|
242 | + */ |
|
243 | + public function sendReset($email) |
|
244 | + { |
|
245 | + if ( ! $user = $this->model->where('email', $email)->first()) |
|
246 | + { |
|
247 | + \ErrorHandler::notFound('email'); |
|
248 | + } |
|
249 | + |
|
250 | + $url = $this->config['resetLink']; |
|
251 | + $token = \Password::getRepository()->create($user); |
|
252 | 252 | |
253 | - \Mail::send('acl::resetpassword', ['user' => $user, 'url' => $url, 'token' => $token], function ($m) use ($user) { |
|
254 | - $m->to($user->email, $user->name)->subject('Your Password Reset Link'); |
|
255 | - }); |
|
256 | - } |
|
257 | - |
|
258 | - /** |
|
259 | - * Reset the given user's password. |
|
260 | - * |
|
261 | - * @param array $credentials |
|
262 | - * @return array |
|
263 | - */ |
|
264 | - public function resetPassword($credentials) |
|
265 | - { |
|
266 | - $response = \Password::reset($credentials, function ($user, $password) { |
|
267 | - $user->password = $password; |
|
268 | - $user->save(); |
|
269 | - }); |
|
270 | - |
|
271 | - switch ($response) { |
|
272 | - case \Password::PASSWORD_RESET: |
|
273 | - return 'success'; |
|
253 | + \Mail::send('acl::resetpassword', ['user' => $user, 'url' => $url, 'token' => $token], function ($m) use ($user) { |
|
254 | + $m->to($user->email, $user->name)->subject('Your Password Reset Link'); |
|
255 | + }); |
|
256 | + } |
|
257 | + |
|
258 | + /** |
|
259 | + * Reset the given user's password. |
|
260 | + * |
|
261 | + * @param array $credentials |
|
262 | + * @return array |
|
263 | + */ |
|
264 | + public function resetPassword($credentials) |
|
265 | + { |
|
266 | + $response = \Password::reset($credentials, function ($user, $password) { |
|
267 | + $user->password = $password; |
|
268 | + $user->save(); |
|
269 | + }); |
|
270 | + |
|
271 | + switch ($response) { |
|
272 | + case \Password::PASSWORD_RESET: |
|
273 | + return 'success'; |
|
274 | 274 | |
275 | - case \Password::INVALID_TOKEN: |
|
276 | - \ErrorHandler::invalidResetToken('token'); |
|
277 | - |
|
278 | - case \Password::INVALID_PASSWORD: |
|
279 | - \ErrorHandler::invalidResetPassword('email'); |
|
280 | - |
|
281 | - case \Password::INVALID_USER: |
|
282 | - \ErrorHandler::notFound('user'); |
|
283 | - |
|
284 | - default: |
|
285 | - \ErrorHandler::generalError(); |
|
286 | - } |
|
287 | - } |
|
288 | - |
|
289 | - /** |
|
290 | - * Change the logged in user password. |
|
291 | - * |
|
292 | - * @param array $credentials |
|
293 | - * @return void |
|
294 | - */ |
|
295 | - public function changePassword($credentials) |
|
296 | - { |
|
297 | - $user = \Auth::user(); |
|
298 | - if ( ! \Hash::check($credentials['old_password'], $user->password)) |
|
299 | - { |
|
300 | - \ErrorHandler::invalidOldPassword(); |
|
301 | - } |
|
302 | - |
|
303 | - $user->password = $credentials['password']; |
|
304 | - $user->save(); |
|
305 | - } |
|
306 | - |
|
307 | - /** |
|
308 | - * Paginate all users in the given group based on the given conditions. |
|
309 | - * |
|
310 | - * @param string $groupName |
|
311 | - * @param array $relations |
|
312 | - * @param integer $perPage |
|
313 | - * @param string $sortBy |
|
314 | - * @param boolean $desc |
|
315 | - * @return \Illuminate\Http\Response |
|
316 | - */ |
|
317 | - public function group($conditions, $groupName, $relations, $perPage, $sortBy, $desc) |
|
318 | - { |
|
319 | - unset($conditions['page']); |
|
320 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
321 | - $sort = $desc ? 'desc' : 'asc'; |
|
322 | - $model = call_user_func_array("{$this->getModel()}::with", array($relations)); |
|
323 | - |
|
324 | - $model->whereHas('groups', function($q) use ($groupName){ |
|
325 | - $q->where('name', $groupName); |
|
326 | - }); |
|
275 | + case \Password::INVALID_TOKEN: |
|
276 | + \ErrorHandler::invalidResetToken('token'); |
|
277 | + |
|
278 | + case \Password::INVALID_PASSWORD: |
|
279 | + \ErrorHandler::invalidResetPassword('email'); |
|
280 | + |
|
281 | + case \Password::INVALID_USER: |
|
282 | + \ErrorHandler::notFound('user'); |
|
283 | + |
|
284 | + default: |
|
285 | + \ErrorHandler::generalError(); |
|
286 | + } |
|
287 | + } |
|
288 | + |
|
289 | + /** |
|
290 | + * Change the logged in user password. |
|
291 | + * |
|
292 | + * @param array $credentials |
|
293 | + * @return void |
|
294 | + */ |
|
295 | + public function changePassword($credentials) |
|
296 | + { |
|
297 | + $user = \Auth::user(); |
|
298 | + if ( ! \Hash::check($credentials['old_password'], $user->password)) |
|
299 | + { |
|
300 | + \ErrorHandler::invalidOldPassword(); |
|
301 | + } |
|
302 | + |
|
303 | + $user->password = $credentials['password']; |
|
304 | + $user->save(); |
|
305 | + } |
|
306 | + |
|
307 | + /** |
|
308 | + * Paginate all users in the given group based on the given conditions. |
|
309 | + * |
|
310 | + * @param string $groupName |
|
311 | + * @param array $relations |
|
312 | + * @param integer $perPage |
|
313 | + * @param string $sortBy |
|
314 | + * @param boolean $desc |
|
315 | + * @return \Illuminate\Http\Response |
|
316 | + */ |
|
317 | + public function group($conditions, $groupName, $relations, $perPage, $sortBy, $desc) |
|
318 | + { |
|
319 | + unset($conditions['page']); |
|
320 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
321 | + $sort = $desc ? 'desc' : 'asc'; |
|
322 | + $model = call_user_func_array("{$this->getModel()}::with", array($relations)); |
|
323 | + |
|
324 | + $model->whereHas('groups', function($q) use ($groupName){ |
|
325 | + $q->where('name', $groupName); |
|
326 | + }); |
|
327 | 327 | |
328 | 328 | |
329 | - if (count($conditions['conditionValues'])) |
|
330 | - { |
|
331 | - $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
332 | - } |
|
333 | - |
|
334 | - if ($perPage) |
|
335 | - { |
|
336 | - return $model->orderBy($sortBy, $sort)->paginate($perPage); |
|
337 | - } |
|
338 | - |
|
339 | - return $model->orderBy($sortBy, $sort)->get(); |
|
340 | - } |
|
341 | - |
|
342 | - /** |
|
343 | - * Save the given data to the logged in user. |
|
344 | - * |
|
345 | - * @param array $credentials |
|
346 | - * @return object |
|
347 | - */ |
|
348 | - public function saveProfile($credentials) |
|
349 | - { |
|
350 | - $user = \Auth::user(); |
|
351 | - $user->save($credentials); |
|
352 | - |
|
353 | - return $user; |
|
354 | - } |
|
355 | - |
|
356 | - /** |
|
357 | - * Ensure access token hasn't expired or revoked. |
|
358 | - * |
|
359 | - * @param string $accessToken |
|
360 | - * @return boolean |
|
361 | - */ |
|
362 | - public function accessTokenExpiredOrRevoked($accessToken) |
|
363 | - { |
|
364 | - $data = new ValidationData(); |
|
365 | - $data->setCurrentTime(time()); |
|
366 | - |
|
367 | - if ($accessToken->validate($data) === false || $this->accessTokenRepository->isAccessTokenRevoked($accessToken->getClaim('jti'))) |
|
368 | - { |
|
369 | - return true; |
|
370 | - } |
|
371 | - |
|
372 | - return false; |
|
373 | - } |
|
329 | + if (count($conditions['conditionValues'])) |
|
330 | + { |
|
331 | + $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
332 | + } |
|
333 | + |
|
334 | + if ($perPage) |
|
335 | + { |
|
336 | + return $model->orderBy($sortBy, $sort)->paginate($perPage); |
|
337 | + } |
|
338 | + |
|
339 | + return $model->orderBy($sortBy, $sort)->get(); |
|
340 | + } |
|
341 | + |
|
342 | + /** |
|
343 | + * Save the given data to the logged in user. |
|
344 | + * |
|
345 | + * @param array $credentials |
|
346 | + * @return object |
|
347 | + */ |
|
348 | + public function saveProfile($credentials) |
|
349 | + { |
|
350 | + $user = \Auth::user(); |
|
351 | + $user->save($credentials); |
|
352 | + |
|
353 | + return $user; |
|
354 | + } |
|
355 | + |
|
356 | + /** |
|
357 | + * Ensure access token hasn't expired or revoked. |
|
358 | + * |
|
359 | + * @param string $accessToken |
|
360 | + * @return boolean |
|
361 | + */ |
|
362 | + public function accessTokenExpiredOrRevoked($accessToken) |
|
363 | + { |
|
364 | + $data = new ValidationData(); |
|
365 | + $data->setCurrentTime(time()); |
|
366 | + |
|
367 | + if ($accessToken->validate($data) === false || $this->accessTokenRepository->isAccessTokenRevoked($accessToken->getClaim('jti'))) |
|
368 | + { |
|
369 | + return true; |
|
370 | + } |
|
371 | + |
|
372 | + return false; |
|
373 | + } |
|
374 | 374 | } |
@@ -48,7 +48,7 @@ discard block |
||
48 | 48 | $user = \Core::users()->find(\Auth::id(), $relations); |
49 | 49 | foreach ($user->groups()->get() as $group) |
50 | 50 | { |
51 | - $group->permissions->each(function ($permission) use (&$permissions){ |
|
51 | + $group->permissions->each(function($permission) use (&$permissions){ |
|
52 | 52 | $permissions[$permission->model][$permission->id] = $permission->name; |
53 | 53 | }); |
54 | 54 | } |
@@ -66,12 +66,12 @@ discard block |
||
66 | 66 | * @param boolean $user |
67 | 67 | * @return boolean |
68 | 68 | */ |
69 | - public function can($nameOfPermission, $model, $user = false ) |
|
69 | + public function can($nameOfPermission, $model, $user = false) |
|
70 | 70 | { |
71 | 71 | $user = $user = $this->find(\Auth::id(), ['groups.permissions']); |
72 | 72 | $permissions = []; |
73 | 73 | |
74 | - $user->groups->pluck('permissions')->each(function ($permission) use (&$permissions, $model){ |
|
74 | + $user->groups->pluck('permissions')->each(function($permission) use (&$permissions, $model){ |
|
75 | 75 | $permissions = array_merge($permissions, $permission->where('model', $model)->pluck('name')->toArray()); |
76 | 76 | }); |
77 | 77 | |
@@ -101,7 +101,7 @@ discard block |
||
101 | 101 | */ |
102 | 102 | public function assignGroups($user_id, $group_ids) |
103 | 103 | { |
104 | - \DB::transaction(function () use ($user_id, $group_ids) { |
|
104 | + \DB::transaction(function() use ($user_id, $group_ids) { |
|
105 | 105 | $user = $this->find($user_id); |
106 | 106 | $user->groups()->detach(); |
107 | 107 | $user->groups()->attach($group_ids); |
@@ -250,7 +250,7 @@ discard block |
||
250 | 250 | $url = $this->config['resetLink']; |
251 | 251 | $token = \Password::getRepository()->create($user); |
252 | 252 | |
253 | - \Mail::send('acl::resetpassword', ['user' => $user, 'url' => $url, 'token' => $token], function ($m) use ($user) { |
|
253 | + \Mail::send('acl::resetpassword', ['user' => $user, 'url' => $url, 'token' => $token], function($m) use ($user) { |
|
254 | 254 | $m->to($user->email, $user->name)->subject('Your Password Reset Link'); |
255 | 255 | }); |
256 | 256 | } |
@@ -263,7 +263,7 @@ discard block |
||
263 | 263 | */ |
264 | 264 | public function resetPassword($credentials) |
265 | 265 | { |
266 | - $response = \Password::reset($credentials, function ($user, $password) { |
|
266 | + $response = \Password::reset($credentials, function($user, $password) { |
|
267 | 267 | $user->password = $password; |
268 | 268 | $user->save(); |
269 | 269 | }); |
@@ -122,16 +122,13 @@ discard block |
||
122 | 122 | if ( ! $user = $this->first(['email' => $credentials['email']])) |
123 | 123 | { |
124 | 124 | \ErrorHandler::loginFailed(); |
125 | - } |
|
126 | - else if ($adminLogin && $user->groups->pluck('name')->search('Admin', true) === false) |
|
125 | + } else if ($adminLogin && $user->groups->pluck('name')->search('Admin', true) === false) |
|
127 | 126 | { |
128 | 127 | \ErrorHandler::loginFailed(); |
129 | - } |
|
130 | - else if ( ! $adminLogin && $user->groups->pluck('name')->search('Admin', true) !== false) |
|
128 | + } else if ( ! $adminLogin && $user->groups->pluck('name')->search('Admin', true) !== false) |
|
131 | 129 | { |
132 | 130 | \ErrorHandler::loginFailed(); |
133 | - } |
|
134 | - else if ($user->blocked) |
|
131 | + } else if ($user->blocked) |
|
135 | 132 | { |
136 | 133 | \ErrorHandler::userIsBlocked(); |
137 | 134 | } |
@@ -159,8 +156,7 @@ discard block |
||
159 | 156 | { |
160 | 157 | $data = ['email' => $user->email, 'password' => '']; |
161 | 158 | return $this->register($data); |
162 | - } |
|
163 | - else |
|
159 | + } else |
|
164 | 160 | { |
165 | 161 | if ( ! \Auth::attempt(['email' => $registeredUser->email, 'password' => ''])) |
166 | 162 | { |
@@ -198,12 +194,10 @@ discard block |
||
198 | 194 | if ( ! $this->hasGroup('Admin')) |
199 | 195 | { |
200 | 196 | \ErrorHandler::noPermissions(); |
201 | - } |
|
202 | - else if (\Auth::id() == $user_id) |
|
197 | + } else if (\Auth::id() == $user_id) |
|
203 | 198 | { |
204 | 199 | \ErrorHandler::noPermissions(); |
205 | - } |
|
206 | - else if ($user->groups->pluck('name')->search('Admin', true) !== false) |
|
200 | + } else if ($user->groups->pluck('name')->search('Admin', true) !== false) |
|
207 | 201 | { |
208 | 202 | \ErrorHandler::noPermissions(); |
209 | 203 | } |