| @@ -1,6 +1,5 @@ | ||
| 1 | 1 | <?php namespace App\Modules\Acl; | 
| 2 | 2 | |
| 3 | -use Illuminate\Database\Eloquent\Model; | |
| 4 | 3 | use App\User; | 
| 5 | 4 | use Illuminate\Database\Eloquent\SoftDeletes; | 
| 6 | 5 | |
| @@ -9,7 +9,7 @@ discard block | ||
| 9 | 9 | use SoftDeletes; | 
| 10 | 10 | protected $table = 'users'; | 
| 11 | 11 | protected $dates = ['created_at', 'updated_at', 'deleted_at']; | 
| 12 | - protected $hidden = ['password', 'remember_token','deleted_at']; | |
| 12 | + protected $hidden = ['password', 'remember_token', 'deleted_at']; | |
| 13 | 13 | protected $guarded = ['id']; | 
| 14 | 14 | protected $fillable = ['first_name', 'last_name', 'user_name', 'address', 'email', 'password']; | 
| 15 | 15 | protected $appends = ['permissions']; | 
| @@ -47,7 +47,7 @@ discard block | ||
| 47 | 47 | |
| 48 | 48 | public function groups() | 
| 49 | 49 |      { | 
| 50 | -        return $this->belongsToMany('\App\Modules\V1\Acl\AclGroup','users_groups','user_id','group_id')->whereNull('users_groups.deleted_at')->withTimestamps(); | |
| 50 | +        return $this->belongsToMany('\App\Modules\V1\Acl\AclGroup', 'users_groups', 'user_id', 'group_id')->whereNull('users_groups.deleted_at')->withTimestamps(); | |
| 51 | 51 | } | 
| 52 | 52 | |
| 53 | 53 | public function getPermissionsAttribute() | 
| @@ -55,7 +55,7 @@ discard block | ||
| 55 | 55 | $permissions = []; | 
| 56 | 56 | foreach ($this->groups as $group) | 
| 57 | 57 |          { | 
| 58 | -            $group->permissions->each(function ($permission) use (&$permissions){ | |
| 58 | +            $group->permissions->each(function($permission) use (&$permissions){ | |
| 59 | 59 | $permissions[$permission->model][$permission->id] = $permission->name; | 
| 60 | 60 | }); | 
| 61 | 61 | } | 
| @@ -6,66 +6,66 @@ | ||
| 6 | 6 | |
| 7 | 7 |  class AclUser extends User { | 
| 8 | 8 | |
| 9 | - use SoftDeletes; | |
| 10 | - protected $table = 'users'; | |
| 11 | - protected $dates = ['created_at', 'updated_at', 'deleted_at']; | |
| 12 | - protected $hidden = ['password', 'remember_token','deleted_at']; | |
| 13 | - protected $guarded = ['id']; | |
| 14 | - protected $fillable = ['name', 'email', 'password']; | |
| 15 | - protected $appends = ['permissions']; | |
| 9 | + use SoftDeletes; | |
| 10 | + protected $table = 'users'; | |
| 11 | + protected $dates = ['created_at', 'updated_at', 'deleted_at']; | |
| 12 | + protected $hidden = ['password', 'remember_token','deleted_at']; | |
| 13 | + protected $guarded = ['id']; | |
| 14 | + protected $fillable = ['name', 'email', 'password']; | |
| 15 | + protected $appends = ['permissions']; | |
| 16 | 16 | |
| 17 | - public function getCreatedAtAttribute($value) | |
| 18 | -    { | |
| 19 | -        return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); | |
| 20 | - } | |
| 17 | + public function getCreatedAtAttribute($value) | |
| 18 | +	{ | |
| 19 | +		return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); | |
| 20 | + } | |
| 21 | 21 | |
| 22 | - public function getUpdatedAtAttribute($value) | |
| 23 | -    { | |
| 24 | -        return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); | |
| 25 | - } | |
| 22 | + public function getUpdatedAtAttribute($value) | |
| 23 | +	{ | |
| 24 | +		return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); | |
| 25 | + } | |
| 26 | 26 | |
| 27 | - public function getDeletedAtAttribute($value) | |
| 28 | -    { | |
| 29 | -        return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); | |
| 30 | - } | |
| 27 | + public function getDeletedAtAttribute($value) | |
| 28 | +	{ | |
| 29 | +		return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); | |
| 30 | + } | |
| 31 | 31 | |
| 32 | - /** | |
| 33 | - * Encrypt the password attribute before | |
| 34 | - * saving it in the storage. | |
| 35 | - * | |
| 36 | - * @param string $value | |
| 37 | - */ | |
| 38 | - public function setPasswordAttribute($value) | |
| 39 | -    { | |
| 40 | - $this->attributes['password'] = bcrypt($value); | |
| 41 | - } | |
| 32 | + /** | |
| 33 | + * Encrypt the password attribute before | |
| 34 | + * saving it in the storage. | |
| 35 | + * | |
| 36 | + * @param string $value | |
| 37 | + */ | |
| 38 | + public function setPasswordAttribute($value) | |
| 39 | +	{ | |
| 40 | + $this->attributes['password'] = bcrypt($value); | |
| 41 | + } | |
| 42 | 42 | |
| 43 | - public function logs() | |
| 44 | -    { | |
| 45 | -        return $this->hasMany('App\Modules\V1\Logging\Log', 'user_id'); | |
| 46 | - } | |
| 43 | + public function logs() | |
| 44 | +	{ | |
| 45 | +		return $this->hasMany('App\Modules\V1\Logging\Log', 'user_id'); | |
| 46 | + } | |
| 47 | 47 | |
| 48 | - public function groups() | |
| 49 | -    { | |
| 50 | -        return $this->belongsToMany('\App\Modules\V1\Acl\AclGroup','users_groups','user_id','group_id')->whereNull('users_groups.deleted_at')->withTimestamps(); | |
| 51 | - } | |
| 48 | + public function groups() | |
| 49 | +	{ | |
| 50 | +		return $this->belongsToMany('\App\Modules\V1\Acl\AclGroup','users_groups','user_id','group_id')->whereNull('users_groups.deleted_at')->withTimestamps(); | |
| 51 | + } | |
| 52 | 52 | |
| 53 | - public function getPermissionsAttribute() | |
| 54 | -    { | |
| 55 | - $permissions = []; | |
| 56 | - foreach ($this->groups as $group) | |
| 57 | -        { | |
| 58 | -            $group->permissions->each(function ($permission) use (&$permissions){ | |
| 59 | - $permissions[$permission->model][$permission->id] = $permission->name; | |
| 60 | - }); | |
| 61 | - } | |
| 53 | + public function getPermissionsAttribute() | |
| 54 | +	{ | |
| 55 | + $permissions = []; | |
| 56 | + foreach ($this->groups as $group) | |
| 57 | +		{ | |
| 58 | +			$group->permissions->each(function ($permission) use (&$permissions){ | |
| 59 | + $permissions[$permission->model][$permission->id] = $permission->name; | |
| 60 | + }); | |
| 61 | + } | |
| 62 | 62 | |
| 63 | - return \Illuminate\Database\Eloquent\Collection::make($permissions); | |
| 64 | - } | |
| 63 | + return \Illuminate\Database\Eloquent\Collection::make($permissions); | |
| 64 | + } | |
| 65 | 65 | |
| 66 | - public static function boot() | |
| 67 | -    { | |
| 68 | - parent::boot(); | |
| 69 | -        parent::observe(\App::make('App\Modules\V1\Acl\ModelObservers\AclUserObserver')); | |
| 70 | - } | |
| 66 | + public static function boot() | |
| 67 | +	{ | |
| 68 | + parent::boot(); | |
| 69 | +		parent::observe(\App::make('App\Modules\V1\Acl\ModelObservers\AclUserObserver')); | |
| 70 | + } | |
| 71 | 71 | } | 
| @@ -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 | */ | 
| @@ -97,7 +97,7 @@ discard block | ||
| 97 | 97 | * @param integer $perPage | 
| 98 | 98 | * @param array $relations | 
| 99 | 99 | * @param string $sortBy | 
| 100 | - * @param boolean $desc | |
| 100 | + * @param integer $desc | |
| 101 | 101 | * @param array $columns | 
| 102 | 102 | * @return collection | 
| 103 | 103 | */ | 
| @@ -115,7 +115,7 @@ discard block | ||
| 115 | 115 | * @param integer $perPage | 
| 116 | 116 | * @param array $relations | 
| 117 | 117 | * @param string $sortBy | 
| 118 | - * @param boolean $desc | |
| 118 | + * @param integer $desc | |
| 119 | 119 | * @param array $columns | 
| 120 | 120 | * @return collection | 
| 121 | 121 | */ | 
| @@ -372,7 +372,7 @@ discard block | ||
| 372 | 372 | * @param array $conditions array of conditions | 
| 373 | 373 | * @param array $relations | 
| 374 | 374 | * @param string $sortBy | 
| 375 | - * @param boolean $desc | |
| 375 | + * @param integer $desc | |
| 376 | 376 | * @param array $columns | 
| 377 | 377 | * @return collection | 
| 378 | 378 | */ | 
| @@ -433,9 +433,9 @@ discard block | ||
| 433 | 433 | * Abstract method that is called in after | 
| 434 | 434 | * the save method finish. | 
| 435 | 435 | * | 
| 436 | - * @param object $model | |
| 436 | + * @param boolean $model | |
| 437 | 437 | * @param array $relations | 
| 438 | - * @return void | |
| 438 | + * @return boolean | |
| 439 | 439 | */ | 
| 440 | 440 | protected function afterSave($model, $relations) | 
| 441 | 441 |      { | 
| @@ -4,447 +4,447 @@ | ||
| 4 | 4 | |
| 5 | 5 | abstract class AbstractRepository implements RepositoryInterface | 
| 6 | 6 |  { | 
| 7 | - /** | |
| 8 | - * The model implementation. | |
| 9 | - * | |
| 10 | - * @var model | |
| 11 | - */ | |
| 12 | - public $model; | |
| 7 | + /** | |
| 8 | + * The model implementation. | |
| 9 | + * | |
| 10 | + * @var model | |
| 11 | + */ | |
| 12 | + public $model; | |
| 13 | 13 | |
| 14 | - /** | |
| 15 | - * The config implementation. | |
| 16 | - * | |
| 17 | - * @var config | |
| 18 | - */ | |
| 19 | - protected $config; | |
| 14 | + /** | |
| 15 | + * The config implementation. | |
| 16 | + * | |
| 17 | + * @var config | |
| 18 | + */ | |
| 19 | + protected $config; | |
| 20 | 20 | |
| 21 | - /** | |
| 22 | - * Create new AbstractRepository instance. | |
| 23 | - */ | |
| 24 | - public function __construct() | |
| 25 | -    {    | |
| 26 | - $this->config = \CoreConfig::getConfig(); | |
| 27 | - $this->model = \App::make($this->getModel()); | |
| 28 | - } | |
| 21 | + /** | |
| 22 | + * Create new AbstractRepository instance. | |
| 23 | + */ | |
| 24 | + public function __construct() | |
| 25 | +	{    | |
| 26 | + $this->config = \CoreConfig::getConfig(); | |
| 27 | + $this->model = \App::make($this->getModel()); | |
| 28 | + } | |
| 29 | 29 | |
| 30 | - /** | |
| 31 | - * Fetch all records with relations from the storage. | |
| 32 | - * | |
| 33 | - * @param array $relations | |
| 34 | - * @param string $sortBy | |
| 35 | - * @param boolean $desc | |
| 36 | - * @param array $columns | |
| 37 | - * @return collection | |
| 38 | - */ | |
| 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 | 39 |  	public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) | 
| 40 | 40 |  	{ | 
| 41 | - $sort = $desc ? 'desc' : 'asc'; | |
| 42 | -        return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->get($columns); | |
| 41 | + $sort = $desc ? 'desc' : 'asc'; | |
| 42 | +		return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->get($columns); | |
| 43 | 43 | } | 
| 44 | 44 | |
| 45 | - /** | |
| 46 | - * Fetch all records with relations from storage in pages | |
| 47 | - * that matche the given query. | |
| 48 | - * | |
| 49 | - * @param string $query | |
| 50 | - * @param integer $perPage | |
| 51 | - * @param array $relations | |
| 52 | - * @param string $sortBy | |
| 53 | - * @param boolean $desc | |
| 54 | - * @param array $columns | |
| 55 | - * @return collection | |
| 56 | - */ | |
| 57 | -    public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) | |
| 58 | -    { | |
| 59 | -        $model            = call_user_func_array("{$this->getModel()}::with", array($relations)); | |
| 60 | - $conditionColumns = $this->model->getFillable(); | |
| 61 | - $sort = $desc ? 'desc' : 'asc'; | |
| 45 | + /** | |
| 46 | + * Fetch all records with relations from storage in pages | |
| 47 | + * that matche the given query. | |
| 48 | + * | |
| 49 | + * @param string $query | |
| 50 | + * @param integer $perPage | |
| 51 | + * @param array $relations | |
| 52 | + * @param string $sortBy | |
| 53 | + * @param boolean $desc | |
| 54 | + * @param array $columns | |
| 55 | + * @return collection | |
| 56 | + */ | |
| 57 | +	public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) | |
| 58 | +	{ | |
| 59 | +		$model            = call_user_func_array("{$this->getModel()}::with", array($relations)); | |
| 60 | + $conditionColumns = $this->model->getFillable(); | |
| 61 | + $sort = $desc ? 'desc' : 'asc'; | |
| 62 | 62 | |
| 63 | -        $model->where(function ($q) use ($query, $conditionColumns, $relations){ | |
| 64 | -            $q->where(\DB::raw('LOWER(CAST(' . array_shift($conditionColumns) . ' AS TEXT))'), 'LIKE', '%' . strtolower($query) . '%'); | |
| 65 | - foreach ($conditionColumns as $column) | |
| 66 | -            { | |
| 67 | -                $q->orWhere(\DB::raw('LOWER(CAST(' . $column . ' AS TEXT))'), 'LIKE', '%' . strtolower($query) . '%'); | |
| 68 | - } | |
| 69 | - foreach ($relations as $relation) | |
| 70 | -            { | |
| 71 | -                $relation = explode('.', $relation)[0]; | |
| 72 | - if (\Core::$relation()) | |
| 73 | -                { | |
| 74 | -                    $q->orWhereHas($relation, function ($subModel) use ($query, $relation){ | |
| 63 | +		$model->where(function ($q) use ($query, $conditionColumns, $relations){ | |
| 64 | +			$q->where(\DB::raw('LOWER(CAST(' . array_shift($conditionColumns) . ' AS TEXT))'), 'LIKE', '%' . strtolower($query) . '%'); | |
| 65 | + foreach ($conditionColumns as $column) | |
| 66 | +			{ | |
| 67 | +				$q->orWhere(\DB::raw('LOWER(CAST(' . $column . ' AS TEXT))'), 'LIKE', '%' . strtolower($query) . '%'); | |
| 68 | + } | |
| 69 | + foreach ($relations as $relation) | |
| 70 | +			{ | |
| 71 | +				$relation = explode('.', $relation)[0]; | |
| 72 | + if (\Core::$relation()) | |
| 73 | +				{ | |
| 74 | +					$q->orWhereHas($relation, function ($subModel) use ($query, $relation){ | |
| 75 | 75 | |
| 76 | -                        $subModel->where(function ($q) use ($query, $relation){ | |
| 76 | +						$subModel->where(function ($q) use ($query, $relation){ | |
| 77 | 77 | |
| 78 | - $subConditionColumns = \Core::$relation()->model->getFillable(); | |
| 79 | -                            $q->where(\DB::raw('LOWER(CAST(' . array_shift($subConditionColumns) . ' AS TEXT))'), 'LIKE', '%' . strtolower($query) . '%'); | |
| 80 | - foreach ($subConditionColumns as $subConditionColumn) | |
| 81 | -                            { | |
| 82 | -                                $q->orWhere(\DB::raw('LOWER(CAST(' . $subConditionColumn . ' AS TEXT))'), 'LIKE', '%' . strtolower($query) . '%'); | |
| 83 | - } | |
| 84 | - }); | |
| 78 | + $subConditionColumns = \Core::$relation()->model->getFillable(); | |
| 79 | +							$q->where(\DB::raw('LOWER(CAST(' . array_shift($subConditionColumns) . ' AS TEXT))'), 'LIKE', '%' . strtolower($query) . '%'); | |
| 80 | + foreach ($subConditionColumns as $subConditionColumn) | |
| 81 | +							{ | |
| 82 | +								$q->orWhere(\DB::raw('LOWER(CAST(' . $subConditionColumn . ' AS TEXT))'), 'LIKE', '%' . strtolower($query) . '%'); | |
| 83 | + } | |
| 84 | + }); | |
| 85 | 85 | |
| 86 | - }); | |
| 87 | - } | |
| 88 | - } | |
| 89 | - }); | |
| 86 | + }); | |
| 87 | + } | |
| 88 | + } | |
| 89 | + }); | |
| 90 | 90 | |
| 91 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); | |
| 92 | - } | |
| 91 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); | |
| 92 | + } | |
| 93 | 93 | |
| 94 | - /** | |
| 95 | - * Fetch all records with relations from storage in pages. | |
| 96 | - * | |
| 97 | - * @param integer $perPage | |
| 98 | - * @param array $relations | |
| 99 | - * @param string $sortBy | |
| 100 | - * @param boolean $desc | |
| 101 | - * @param array $columns | |
| 102 | - * @return collection | |
| 103 | - */ | |
| 104 | -    public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) | |
| 105 | -    { | |
| 106 | - $sort = $desc ? 'desc' : 'asc'; | |
| 107 | -        return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->paginate($perPage, $columns); | |
| 108 | - } | |
| 94 | + /** | |
| 95 | + * Fetch all records with relations from storage in pages. | |
| 96 | + * | |
| 97 | + * @param integer $perPage | |
| 98 | + * @param array $relations | |
| 99 | + * @param string $sortBy | |
| 100 | + * @param boolean $desc | |
| 101 | + * @param array $columns | |
| 102 | + * @return collection | |
| 103 | + */ | |
| 104 | +	public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) | |
| 105 | +	{ | |
| 106 | + $sort = $desc ? 'desc' : 'asc'; | |
| 107 | +		return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->paginate($perPage, $columns); | |
| 108 | + } | |
| 109 | 109 | |
| 110 | - /** | |
| 111 | - * Fetch all records with relations based on | |
| 112 | - * the given condition from storage in pages. | |
| 113 | - * | |
| 114 | - * @param array $conditions array of conditions | |
| 115 | - * @param integer $perPage | |
| 116 | - * @param array $relations | |
| 117 | - * @param string $sortBy | |
| 118 | - * @param boolean $desc | |
| 119 | - * @param array $columns | |
| 120 | - * @return collection | |
| 121 | - */ | |
| 122 | -    public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) | |
| 123 | -    { | |
| 124 | - unset($conditions['page']); | |
| 125 | - $conditions = $this->constructConditions($conditions); | |
| 126 | - $sort = $desc ? 'desc' : 'asc'; | |
| 127 | -        return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); | |
| 128 | - } | |
| 110 | + /** | |
| 111 | + * Fetch all records with relations based on | |
| 112 | + * the given condition from storage in pages. | |
| 113 | + * | |
| 114 | + * @param array $conditions array of conditions | |
| 115 | + * @param integer $perPage | |
| 116 | + * @param array $relations | |
| 117 | + * @param string $sortBy | |
| 118 | + * @param boolean $desc | |
| 119 | + * @param array $columns | |
| 120 | + * @return collection | |
| 121 | + */ | |
| 122 | +	public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) | |
| 123 | +	{ | |
| 124 | + unset($conditions['page']); | |
| 125 | + $conditions = $this->constructConditions($conditions); | |
| 126 | + $sort = $desc ? 'desc' : 'asc'; | |
| 127 | +		return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); | |
| 128 | + } | |
| 129 | 129 | |
| 130 | - /** | |
| 131 | - * Save the given model to the storage. | |
| 132 | - * | |
| 133 | - * @param array $data | |
| 134 | - * @param boolean $saveLog | |
| 135 | - * @return object | |
| 136 | - */ | |
| 137 | - public function save(array $data, $saveLog = true) | |
| 138 | -    { | |
| 139 | - $model = false; | |
| 140 | - $modelClass = $this->model; | |
| 141 | - $relations = []; | |
| 142 | - $with = []; | |
| 130 | + /** | |
| 131 | + * Save the given model to the storage. | |
| 132 | + * | |
| 133 | + * @param array $data | |
| 134 | + * @param boolean $saveLog | |
| 135 | + * @return object | |
| 136 | + */ | |
| 137 | + public function save(array $data, $saveLog = true) | |
| 138 | +	{ | |
| 139 | + $model = false; | |
| 140 | + $modelClass = $this->model; | |
| 141 | + $relations = []; | |
| 142 | + $with = []; | |
| 143 | 143 | |
| 144 | -        \DB::transaction(function () use (&$model, &$relations, &$with, $data, $saveLog, $modelClass) { | |
| 145 | - /** | |
| 146 | - * If the id is present in the data then select the model for updating, | |
| 147 | - * else create new model. | |
| 148 | - * @var array | |
| 149 | - */ | |
| 150 | -            $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; | |
| 151 | - if ( ! $model) | |
| 152 | -            { | |
| 153 | - \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']); | |
| 154 | - } | |
| 144 | +		\DB::transaction(function () use (&$model, &$relations, &$with, $data, $saveLog, $modelClass) { | |
| 145 | + /** | |
| 146 | + * If the id is present in the data then select the model for updating, | |
| 147 | + * else create new model. | |
| 148 | + * @var array | |
| 149 | + */ | |
| 150 | +			$model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; | |
| 151 | + if ( ! $model) | |
| 152 | +			{ | |
| 153 | + \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']); | |
| 154 | + } | |
| 155 | 155 | |
| 156 | - /** | |
| 157 | - * Construct the model object with the given data, | |
| 158 | - * and if there is a relation add it to relations array, | |
| 159 | - * then save the model. | |
| 160 | - */ | |
| 161 | - foreach ($data as $key => $value) | |
| 162 | -            { | |
| 163 | - $relation = camel_case($key); | |
| 164 | - if (method_exists($model, $relation)) | |
| 165 | -                { | |
| 166 | - $with[] = $relation; | |
| 167 | - if (class_basename($model->$relation) == 'Collection') | |
| 168 | -                    {    | |
| 169 | - if ( ! $value || ! count($value)) | |
| 170 | -                        { | |
| 171 | - $relations[$relation] = 'delete'; | |
| 172 | - } | |
| 173 | - } | |
| 174 | - if (is_array($value)) | |
| 175 | -                    { | |
| 176 | - foreach ($value as $attr => $val) | |
| 177 | -                        { | |
| 178 | - $relationBaseModel = \Core::$relation()->model; | |
| 179 | - if (class_basename($model->$relation) == 'Collection') | |
| 180 | -                            { | |
| 181 | -                                $relationModel = array_key_exists('id', $val) ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; | |
| 182 | - if ( ! $relationModel) | |
| 183 | -                                { | |
| 184 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']); | |
| 185 | - } | |
| 156 | + /** | |
| 157 | + * Construct the model object with the given data, | |
| 158 | + * and if there is a relation add it to relations array, | |
| 159 | + * then save the model. | |
| 160 | + */ | |
| 161 | + foreach ($data as $key => $value) | |
| 162 | +			{ | |
| 163 | + $relation = camel_case($key); | |
| 164 | + if (method_exists($model, $relation)) | |
| 165 | +				{ | |
| 166 | + $with[] = $relation; | |
| 167 | + if (class_basename($model->$relation) == 'Collection') | |
| 168 | +					{    | |
| 169 | + if ( ! $value || ! count($value)) | |
| 170 | +						{ | |
| 171 | + $relations[$relation] = 'delete'; | |
| 172 | + } | |
| 173 | + } | |
| 174 | + if (is_array($value)) | |
| 175 | +					{ | |
| 176 | + foreach ($value as $attr => $val) | |
| 177 | +						{ | |
| 178 | + $relationBaseModel = \Core::$relation()->model; | |
| 179 | + if (class_basename($model->$relation) == 'Collection') | |
| 180 | +							{ | |
| 181 | +								$relationModel = array_key_exists('id', $val) ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; | |
| 182 | + if ( ! $relationModel) | |
| 183 | +								{ | |
| 184 | + \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']); | |
| 185 | + } | |
| 186 | 186 | |
| 187 | - foreach ($val as $attr => $val) | |
| 188 | -                                { | |
| 189 | - if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) | |
| 190 | -                                    { | |
| 191 | - $relationModel->$attr = $val; | |
| 192 | - } | |
| 193 | - } | |
| 194 | - $relations[$relation][] = $relationModel; | |
| 195 | - } | |
| 196 | - else | |
| 197 | -                            { | |
| 198 | - if (gettype($val) !== 'object' && gettype($val) !== 'array') | |
| 199 | -                                { | |
| 200 | -                                    $relationModel = array_key_exists('id', $value) ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; | |
| 201 | - if ( ! $relationModel) | |
| 202 | -                                    { | |
| 203 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']); | |
| 204 | - } | |
| 187 | + foreach ($val as $attr => $val) | |
| 188 | +								{ | |
| 189 | + if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) | |
| 190 | +									{ | |
| 191 | + $relationModel->$attr = $val; | |
| 192 | + } | |
| 193 | + } | |
| 194 | + $relations[$relation][] = $relationModel; | |
| 195 | + } | |
| 196 | + else | |
| 197 | +							{ | |
| 198 | + if (gettype($val) !== 'object' && gettype($val) !== 'array') | |
| 199 | +								{ | |
| 200 | +									$relationModel = array_key_exists('id', $value) ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; | |
| 201 | + if ( ! $relationModel) | |
| 202 | +									{ | |
| 203 | + \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']); | |
| 204 | + } | |
| 205 | 205 | |
| 206 | - if (array_search($attr, $relationModel->getFillable(), true) !== false) | |
| 207 | -                                    { | |
| 208 | - $relationModel->$attr = $val; | |
| 209 | - $relations[$relation] = $relationModel; | |
| 210 | - } | |
| 211 | - } | |
| 212 | - } | |
| 213 | - } | |
| 214 | - } | |
| 215 | - } | |
| 216 | - else if (array_search($key, $model->getFillable(), true) !== false) | |
| 217 | -                { | |
| 218 | - $model->$key = $value; | |
| 219 | - } | |
| 220 | - } | |
| 221 | - $model->save(); | |
| 206 | + if (array_search($attr, $relationModel->getFillable(), true) !== false) | |
| 207 | +									{ | |
| 208 | + $relationModel->$attr = $val; | |
| 209 | + $relations[$relation] = $relationModel; | |
| 210 | + } | |
| 211 | + } | |
| 212 | + } | |
| 213 | + } | |
| 214 | + } | |
| 215 | + } | |
| 216 | + else if (array_search($key, $model->getFillable(), true) !== false) | |
| 217 | +				{ | |
| 218 | + $model->$key = $value; | |
| 219 | + } | |
| 220 | + } | |
| 221 | + $model->save(); | |
| 222 | 222 | |
| 223 | - foreach ($relations as $key => $value) | |
| 224 | -            { | |
| 225 | - if ($value == 'delete' && $model->$key()->count()) | |
| 226 | -                { | |
| 227 | - $model->$key()->delete(); | |
| 228 | - } | |
| 229 | - else if (gettype($value) == 'array') | |
| 230 | -                { | |
| 231 | - $ids = []; | |
| 232 | - foreach ($value as $val) | |
| 233 | -                    { | |
| 234 | - switch (class_basename($model->$key())) | |
| 235 | -                        { | |
| 236 | - case 'HasMany': | |
| 237 | -                                $foreignKeyName       = explode('.', $model->$key()->getForeignKey())[1]; | |
| 238 | - $val->$foreignKeyName = $model->id; | |
| 239 | - $val->save(); | |
| 240 | - $ids[] = $val->id; | |
| 241 | - break; | |
| 223 | + foreach ($relations as $key => $value) | |
| 224 | +			{ | |
| 225 | + if ($value == 'delete' && $model->$key()->count()) | |
| 226 | +				{ | |
| 227 | + $model->$key()->delete(); | |
| 228 | + } | |
| 229 | + else if (gettype($value) == 'array') | |
| 230 | +				{ | |
| 231 | + $ids = []; | |
| 232 | + foreach ($value as $val) | |
| 233 | +					{ | |
| 234 | + switch (class_basename($model->$key())) | |
| 235 | +						{ | |
| 236 | + case 'HasMany': | |
| 237 | +								$foreignKeyName       = explode('.', $model->$key()->getForeignKey())[1]; | |
| 238 | + $val->$foreignKeyName = $model->id; | |
| 239 | + $val->save(); | |
| 240 | + $ids[] = $val->id; | |
| 241 | + break; | |
| 242 | 242 | |
| 243 | - case 'BelongsToMany': | |
| 244 | - $val->save(); | |
| 245 | - $ids[] = $val->id; | |
| 246 | - break; | |
| 247 | - } | |
| 248 | - } | |
| 249 | - switch (class_basename($model->$key())) | |
| 250 | -                    { | |
| 251 | - case 'HasMany': | |
| 252 | -                            $model->$key()->whereNotIn('id', $ids)->delete(); | |
| 253 | - break; | |
| 243 | + case 'BelongsToMany': | |
| 244 | + $val->save(); | |
| 245 | + $ids[] = $val->id; | |
| 246 | + break; | |
| 247 | + } | |
| 248 | + } | |
| 249 | + switch (class_basename($model->$key())) | |
| 250 | +					{ | |
| 251 | + case 'HasMany': | |
| 252 | +							$model->$key()->whereNotIn('id', $ids)->delete(); | |
| 253 | + break; | |
| 254 | 254 | |
| 255 | - case 'BelongsToMany': | |
| 256 | - $model->$key()->detach(); | |
| 257 | - $model->$key()->attach($ids); | |
| 258 | - break; | |
| 259 | - } | |
| 260 | - } | |
| 261 | - else | |
| 262 | -                { | |
| 263 | - switch (class_basename($model->$key())) | |
| 264 | -                    { | |
| 265 | - case 'BelongsTo': | |
| 266 | - $value->save(); | |
| 267 | - $model->$key()->associate($value); | |
| 268 | - $model->save(); | |
| 269 | - break; | |
| 270 | - } | |
| 271 | - } | |
| 272 | - } | |
| 273 | -            $saveLog ? \Logging::saveLog(array_key_exists('id', $data) ? 'update' : 'create', class_basename($modelClass), $this->getModel(), $model->id, $model) : false; | |
| 274 | - }); | |
| 255 | + case 'BelongsToMany': | |
| 256 | + $model->$key()->detach(); | |
| 257 | + $model->$key()->attach($ids); | |
| 258 | + break; | |
| 259 | + } | |
| 260 | + } | |
| 261 | + else | |
| 262 | +				{ | |
| 263 | + switch (class_basename($model->$key())) | |
| 264 | +					{ | |
| 265 | + case 'BelongsTo': | |
| 266 | + $value->save(); | |
| 267 | + $model->$key()->associate($value); | |
| 268 | + $model->save(); | |
| 269 | + break; | |
| 270 | + } | |
| 271 | + } | |
| 272 | + } | |
| 273 | +			$saveLog ? \Logging::saveLog(array_key_exists('id', $data) ? 'update' : 'create', class_basename($modelClass), $this->getModel(), $model->id, $model) : false; | |
| 274 | + }); | |
| 275 | 275 | |
| 276 | - $this->afterSave($model, $relations); | |
| 276 | + $this->afterSave($model, $relations); | |
| 277 | 277 | |
| 278 | - return $this->find($model->id, $with); | |
| 279 | - } | |
| 278 | + return $this->find($model->id, $with); | |
| 279 | + } | |
| 280 | 280 | |
| 281 | - /** | |
| 282 | - * Save the given models to the storage. | |
| 283 | - * | |
| 284 | - * @param array $data | |
| 285 | - * @return array | |
| 286 | - */ | |
| 287 | - public function saveMany(array $data) | |
| 288 | -    { | |
| 289 | - $result = []; | |
| 290 | -        \DB::transaction(function () use (&$result, $data) { | |
| 291 | - foreach ($data as $key => $value) | |
| 292 | -            { | |
| 293 | - $result[] = $this->save($value); | |
| 294 | - } | |
| 295 | - }); | |
| 296 | - return $result; | |
| 297 | - } | |
| 281 | + /** | |
| 282 | + * Save the given models to the storage. | |
| 283 | + * | |
| 284 | + * @param array $data | |
| 285 | + * @return array | |
| 286 | + */ | |
| 287 | + public function saveMany(array $data) | |
| 288 | +	{ | |
| 289 | + $result = []; | |
| 290 | +		\DB::transaction(function () use (&$result, $data) { | |
| 291 | + foreach ($data as $key => $value) | |
| 292 | +			{ | |
| 293 | + $result[] = $this->save($value); | |
| 294 | + } | |
| 295 | + }); | |
| 296 | + return $result; | |
| 297 | + } | |
| 298 | 298 | |
| 299 | - /** | |
| 300 | - * Update record in the storage based on the given | |
| 301 | - * condition. | |
| 302 | - * | |
| 303 | - * @param var $value condition value | |
| 304 | - * @param array $data | |
| 305 | - * @param string $attribute condition column name | |
| 306 | - * @return void | |
| 307 | - */ | |
| 308 | - public function update($value, array $data, $attribute = 'id', $saveLog = true) | |
| 309 | -    { | |
| 310 | - if ($attribute == 'id') | |
| 311 | -        { | |
| 312 | - $model = $this->model->lockForUpdate()->find($value); | |
| 313 | - $model ? $model->update($data) : 0; | |
| 314 | -            $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $value, $model) : false; | |
| 315 | - } | |
| 316 | -    	call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data, $saveLog){ | |
| 317 | - $model->update($data); | |
| 318 | -            $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $model->id, $model) : false; | |
| 319 | - }); | |
| 320 | - } | |
| 299 | + /** | |
| 300 | + * Update record in the storage based on the given | |
| 301 | + * condition. | |
| 302 | + * | |
| 303 | + * @param var $value condition value | |
| 304 | + * @param array $data | |
| 305 | + * @param string $attribute condition column name | |
| 306 | + * @return void | |
| 307 | + */ | |
| 308 | + public function update($value, array $data, $attribute = 'id', $saveLog = true) | |
| 309 | +	{ | |
| 310 | + if ($attribute == 'id') | |
| 311 | +		{ | |
| 312 | + $model = $this->model->lockForUpdate()->find($value); | |
| 313 | + $model ? $model->update($data) : 0; | |
| 314 | +			$saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $value, $model) : false; | |
| 315 | + } | |
| 316 | +		call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data, $saveLog){ | |
| 317 | + $model->update($data); | |
| 318 | +			$saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $model->id, $model) : false; | |
| 319 | + }); | |
| 320 | + } | |
| 321 | 321 | |
| 322 | - /** | |
| 323 | - * Delete record from the storage based on the given | |
| 324 | - * condition. | |
| 325 | - * | |
| 326 | - * @param var $value condition value | |
| 327 | - * @param string $attribute condition column name | |
| 328 | - * @return void | |
| 329 | - */ | |
| 330 | - public function delete($value, $attribute = 'id', $saveLog = true) | |
| 331 | -    { | |
| 332 | - if ($attribute == 'id') | |
| 333 | -    	{ | |
| 334 | -            \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { | |
| 335 | - $model = $this->model->lockForUpdate()->find($value); | |
| 336 | - $model->delete(); | |
| 337 | -                $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $value, $model) : false; | |
| 338 | - }); | |
| 339 | - } | |
| 340 | - else | |
| 341 | -        { | |
| 342 | -            \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { | |
| 343 | -                call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model){ | |
| 344 | - $model->delete(); | |
| 345 | -                    $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $model->id, $model) : false; | |
| 346 | - }); | |
| 347 | - }); | |
| 348 | - } | |
| 349 | - } | |
| 322 | + /** | |
| 323 | + * Delete record from the storage based on the given | |
| 324 | + * condition. | |
| 325 | + * | |
| 326 | + * @param var $value condition value | |
| 327 | + * @param string $attribute condition column name | |
| 328 | + * @return void | |
| 329 | + */ | |
| 330 | + public function delete($value, $attribute = 'id', $saveLog = true) | |
| 331 | +	{ | |
| 332 | + if ($attribute == 'id') | |
| 333 | +		{ | |
| 334 | +			\DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { | |
| 335 | + $model = $this->model->lockForUpdate()->find($value); | |
| 336 | + $model->delete(); | |
| 337 | +				$saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $value, $model) : false; | |
| 338 | + }); | |
| 339 | + } | |
| 340 | + else | |
| 341 | +		{ | |
| 342 | +			\DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { | |
| 343 | +				call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model){ | |
| 344 | + $model->delete(); | |
| 345 | +					$saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $model->id, $model) : false; | |
| 346 | + }); | |
| 347 | + }); | |
| 348 | + } | |
| 349 | + } | |
| 350 | 350 | |
| 351 | - /** | |
| 352 | - * Fetch records from the storage based on the given | |
| 353 | - * id. | |
| 354 | - * | |
| 355 | - * @param integer $id | |
| 356 | - * @param array $relations | |
| 357 | - * @param array $columns | |
| 358 | - * @return object | |
| 359 | - */ | |
| 360 | -    public function find($id, $relations = [], $columns = array('*')) | |
| 361 | -    { | |
| 362 | -        return call_user_func_array("{$this->getModel()}::with", array($relations))->find($id, $columns); | |
| 363 | - } | |
| 351 | + /** | |
| 352 | + * Fetch records from the storage based on the given | |
| 353 | + * id. | |
| 354 | + * | |
| 355 | + * @param integer $id | |
| 356 | + * @param array $relations | |
| 357 | + * @param array $columns | |
| 358 | + * @return object | |
| 359 | + */ | |
| 360 | +	public function find($id, $relations = [], $columns = array('*')) | |
| 361 | +	{ | |
| 362 | +		return call_user_func_array("{$this->getModel()}::with", array($relations))->find($id, $columns); | |
| 363 | + } | |
| 364 | 364 | |
| 365 | - /** | |
| 366 | - * Fetch records from the storage based on the given | |
| 367 | - * condition. | |
| 368 | - * | |
| 369 | - * @param array $conditions array of conditions | |
| 370 | - * @param array $relations | |
| 371 | - * @param string $sortBy | |
| 372 | - * @param boolean $desc | |
| 373 | - * @param array $columns | |
| 374 | - * @return collection | |
| 375 | - */ | |
| 376 | -    public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) | |
| 377 | -    { | |
| 378 | - $conditions = $this->constructConditions($conditions); | |
| 379 | - $sort = $desc ? 'desc' : 'asc'; | |
| 380 | -        return call_user_func_array("{$this->getModel()}::with",  array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); | |
| 381 | - } | |
| 365 | + /** | |
| 366 | + * Fetch records from the storage based on the given | |
| 367 | + * condition. | |
| 368 | + * | |
| 369 | + * @param array $conditions array of conditions | |
| 370 | + * @param array $relations | |
| 371 | + * @param string $sortBy | |
| 372 | + * @param boolean $desc | |
| 373 | + * @param array $columns | |
| 374 | + * @return collection | |
| 375 | + */ | |
| 376 | +	public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) | |
| 377 | +	{ | |
| 378 | + $conditions = $this->constructConditions($conditions); | |
| 379 | + $sort = $desc ? 'desc' : 'asc'; | |
| 380 | +		return call_user_func_array("{$this->getModel()}::with",  array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); | |
| 381 | + } | |
| 382 | 382 | |
| 383 | - /** | |
| 384 | - * Fetch the first record from the storage based on the given | |
| 385 | - * condition. | |
| 386 | - * | |
| 387 | - * @param array $conditions array of conditions | |
| 388 | - * @param array $relations | |
| 389 | - * @param array $columns | |
| 390 | - * @return object | |
| 391 | - */ | |
| 392 | -    public function first($conditions, $relations = [], $columns = array('*')) | |
| 393 | -    { | |
| 394 | - $conditions = $this->constructConditions($conditions); | |
| 395 | -        return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns);   | |
| 396 | - } | |
| 383 | + /** | |
| 384 | + * Fetch the first record from the storage based on the given | |
| 385 | + * condition. | |
| 386 | + * | |
| 387 | + * @param array $conditions array of conditions | |
| 388 | + * @param array $relations | |
| 389 | + * @param array $columns | |
| 390 | + * @return object | |
| 391 | + */ | |
| 392 | +	public function first($conditions, $relations = [], $columns = array('*')) | |
| 393 | +	{ | |
| 394 | + $conditions = $this->constructConditions($conditions); | |
| 395 | +		return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns);   | |
| 396 | + } | |
| 397 | 397 | |
| 398 | - /** | |
| 399 | - * Build the conditions recursively for the retrieving methods. | |
| 400 | - * @param array $conditions | |
| 401 | - * @return array | |
| 402 | - */ | |
| 403 | - protected function constructConditions($conditions) | |
| 404 | -    {    | |
| 405 | - $conditionString = ''; | |
| 406 | - $conditionValues = []; | |
| 407 | - foreach ($conditions as $key => $value) | |
| 408 | -        { | |
| 409 | - if ($key == 'and') | |
| 410 | -            { | |
| 411 | -                $conditionString  .= str_replace('{op}', 'and', $this->constructConditions($value)['conditionString']) . ' {op} '; | |
| 412 | - $conditionValues = array_merge($conditionValues, $this->constructConditions($value)['conditionValues']); | |
| 413 | - } | |
| 414 | - else if ($key == 'or') | |
| 415 | -            { | |
| 416 | -                $conditionString  .= str_replace('{op}', 'or', $this->constructConditions($value)['conditionString']) . ' {op} '; | |
| 417 | - $conditionValues = array_merge($conditionValues, $this->constructConditions($value)['conditionValues']); | |
| 418 | - } | |
| 419 | - else | |
| 420 | -            { | |
| 421 | -                $conditionString  .= $key . '=? {op} '; | |
| 422 | - $conditionValues[] = $value; | |
| 423 | - } | |
| 424 | - } | |
| 425 | -        $conditionString = '(' . rtrim($conditionString, '{op} ') . ')'; | |
| 426 | - return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; | |
| 427 | - } | |
| 398 | + /** | |
| 399 | + * Build the conditions recursively for the retrieving methods. | |
| 400 | + * @param array $conditions | |
| 401 | + * @return array | |
| 402 | + */ | |
| 403 | + protected function constructConditions($conditions) | |
| 404 | +	{    | |
| 405 | + $conditionString = ''; | |
| 406 | + $conditionValues = []; | |
| 407 | + foreach ($conditions as $key => $value) | |
| 408 | +		{ | |
| 409 | + if ($key == 'and') | |
| 410 | +			{ | |
| 411 | +				$conditionString  .= str_replace('{op}', 'and', $this->constructConditions($value)['conditionString']) . ' {op} '; | |
| 412 | + $conditionValues = array_merge($conditionValues, $this->constructConditions($value)['conditionValues']); | |
| 413 | + } | |
| 414 | + else if ($key == 'or') | |
| 415 | +			{ | |
| 416 | +				$conditionString  .= str_replace('{op}', 'or', $this->constructConditions($value)['conditionString']) . ' {op} '; | |
| 417 | + $conditionValues = array_merge($conditionValues, $this->constructConditions($value)['conditionValues']); | |
| 418 | + } | |
| 419 | + else | |
| 420 | +			{ | |
| 421 | +				$conditionString  .= $key . '=? {op} '; | |
| 422 | + $conditionValues[] = $value; | |
| 423 | + } | |
| 424 | + } | |
| 425 | +		$conditionString = '(' . rtrim($conditionString, '{op} ') . ')'; | |
| 426 | + return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; | |
| 427 | + } | |
| 428 | 428 | |
| 429 | - /** | |
| 430 | - * Abstract method that is called in after | |
| 431 | - * the save method finish. | |
| 432 | - * | |
| 433 | - * @param object $model | |
| 434 | - * @param array $relations | |
| 435 | - * @return void | |
| 436 | - */ | |
| 437 | - protected function afterSave($model, $relations) | |
| 438 | -    { | |
| 439 | - return false; | |
| 440 | - } | |
| 429 | + /** | |
| 430 | + * Abstract method that is called in after | |
| 431 | + * the save method finish. | |
| 432 | + * | |
| 433 | + * @param object $model | |
| 434 | + * @param array $relations | |
| 435 | + * @return void | |
| 436 | + */ | |
| 437 | + protected function afterSave($model, $relations) | |
| 438 | +	{ | |
| 439 | + return false; | |
| 440 | + } | |
| 441 | 441 | |
| 442 | - /** | |
| 443 | - * Abstract method that return the necessary | |
| 444 | - * information (full model namespace) | |
| 445 | - * needed to preform the previous actions. | |
| 446 | - * | |
| 447 | - * @return string | |
| 448 | - */ | |
| 449 | - abstract protected function getModel(); | |
| 442 | + /** | |
| 443 | + * Abstract method that return the necessary | |
| 444 | + * information (full model namespace) | |
| 445 | + * needed to preform the previous actions. | |
| 446 | + * | |
| 447 | + * @return string | |
| 448 | + */ | |
| 449 | + abstract protected function getModel(); | |
| 450 | 450 | } | 
| 451 | 451 | \ No newline at end of file | 
| @@ -60,26 +60,26 @@ discard block | ||
| 60 | 60 | $conditionColumns = $this->model->getFillable(); | 
| 61 | 61 | $sort = $desc ? 'desc' : 'asc'; | 
| 62 | 62 | |
| 63 | -        $model->where(function ($q) use ($query, $conditionColumns, $relations){ | |
| 64 | -            $q->where(\DB::raw('LOWER(CAST(' . array_shift($conditionColumns) . ' AS TEXT))'), 'LIKE', '%' . strtolower($query) . '%'); | |
| 63 | +        $model->where(function($q) use ($query, $conditionColumns, $relations){ | |
| 64 | +            $q->where(\DB::raw('LOWER(CAST('.array_shift($conditionColumns).' AS TEXT))'), 'LIKE', '%'.strtolower($query).'%'); | |
| 65 | 65 | foreach ($conditionColumns as $column) | 
| 66 | 66 |              { | 
| 67 | -                $q->orWhere(\DB::raw('LOWER(CAST(' . $column . ' AS TEXT))'), 'LIKE', '%' . strtolower($query) . '%'); | |
| 67 | +                $q->orWhere(\DB::raw('LOWER(CAST('.$column.' AS TEXT))'), 'LIKE', '%'.strtolower($query).'%'); | |
| 68 | 68 | } | 
| 69 | 69 | foreach ($relations as $relation) | 
| 70 | 70 |              { | 
| 71 | 71 |                  $relation = explode('.', $relation)[0]; | 
| 72 | 72 | if (\Core::$relation()) | 
| 73 | 73 |                  { | 
| 74 | -                    $q->orWhereHas($relation, function ($subModel) use ($query, $relation){ | |
| 74 | +                    $q->orWhereHas($relation, function($subModel) use ($query, $relation){ | |
| 75 | 75 | |
| 76 | -                        $subModel->where(function ($q) use ($query, $relation){ | |
| 76 | +                        $subModel->where(function($q) use ($query, $relation){ | |
| 77 | 77 | |
| 78 | 78 | $subConditionColumns = \Core::$relation()->model->getFillable(); | 
| 79 | -                            $q->where(\DB::raw('LOWER(CAST(' . array_shift($subConditionColumns) . ' AS TEXT))'), 'LIKE', '%' . strtolower($query) . '%'); | |
| 79 | +                            $q->where(\DB::raw('LOWER(CAST('.array_shift($subConditionColumns).' AS TEXT))'), 'LIKE', '%'.strtolower($query).'%'); | |
| 80 | 80 | foreach ($subConditionColumns as $subConditionColumn) | 
| 81 | 81 |                              { | 
| 82 | -                                $q->orWhere(\DB::raw('LOWER(CAST(' . $subConditionColumn . ' AS TEXT))'), 'LIKE', '%' . strtolower($query) . '%'); | |
| 82 | +                                $q->orWhere(\DB::raw('LOWER(CAST('.$subConditionColumn.' AS TEXT))'), 'LIKE', '%'.strtolower($query).'%'); | |
| 83 | 83 | } | 
| 84 | 84 | }); | 
| 85 | 85 | |
| @@ -141,7 +141,7 @@ discard block | ||
| 141 | 141 | $relations = []; | 
| 142 | 142 | $with = []; | 
| 143 | 143 | |
| 144 | -        \DB::transaction(function () use (&$model, &$relations, &$with, $data, $saveLog, $modelClass) { | |
| 144 | +        \DB::transaction(function() use (&$model, &$relations, &$with, $data, $saveLog, $modelClass) { | |
| 145 | 145 | /** | 
| 146 | 146 | * If the id is present in the data then select the model for updating, | 
| 147 | 147 | * else create new model. | 
| @@ -150,7 +150,7 @@ discard block | ||
| 150 | 150 |              $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; | 
| 151 | 151 | if ( ! $model) | 
| 152 | 152 |              { | 
| 153 | - \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']); | |
| 153 | + \ErrorHandler::notFound(class_basename($modelClass).' with id : '.$data['id']); | |
| 154 | 154 | } | 
| 155 | 155 | |
| 156 | 156 | /** | 
| @@ -181,12 +181,12 @@ discard block | ||
| 181 | 181 |                                  $relationModel = array_key_exists('id', $val) ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; | 
| 182 | 182 | if ( ! $relationModel) | 
| 183 | 183 |                                  { | 
| 184 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']); | |
| 184 | + \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); | |
| 185 | 185 | } | 
| 186 | 186 | |
| 187 | 187 | foreach ($val as $attr => $val) | 
| 188 | 188 |                                  { | 
| 189 | - if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) | |
| 189 | + if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) | |
| 190 | 190 |                                      { | 
| 191 | 191 | $relationModel->$attr = $val; | 
| 192 | 192 | } | 
| @@ -200,7 +200,7 @@ discard block | ||
| 200 | 200 |                                      $relationModel = array_key_exists('id', $value) ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; | 
| 201 | 201 | if ( ! $relationModel) | 
| 202 | 202 |                                      { | 
| 203 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']); | |
| 203 | + \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); | |
| 204 | 204 | } | 
| 205 | 205 | |
| 206 | 206 | if (array_search($attr, $relationModel->getFillable(), true) !== false) | 
| @@ -287,7 +287,7 @@ discard block | ||
| 287 | 287 | public function saveMany(array $data) | 
| 288 | 288 |      { | 
| 289 | 289 | $result = []; | 
| 290 | -        \DB::transaction(function () use (&$result, $data) { | |
| 290 | +        \DB::transaction(function() use (&$result, $data) { | |
| 291 | 291 | foreach ($data as $key => $value) | 
| 292 | 292 |              { | 
| 293 | 293 | $result[] = $this->save($value); | 
| @@ -313,7 +313,7 @@ discard block | ||
| 313 | 313 | $model ? $model->update($data) : 0; | 
| 314 | 314 |              $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $value, $model) : false; | 
| 315 | 315 | } | 
| 316 | -    	call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data, $saveLog){ | |
| 316 | +    	call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function($model) use ($data, $saveLog){ | |
| 317 | 317 | $model->update($data); | 
| 318 | 318 |              $saveLog ? \Logging::saveLog('update', class_basename($this->model), $this->getModel(), $model->id, $model) : false; | 
| 319 | 319 | }); | 
| @@ -331,7 +331,7 @@ discard block | ||
| 331 | 331 |      { | 
| 332 | 332 | if ($attribute == 'id') | 
| 333 | 333 |      	{ | 
| 334 | -            \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { | |
| 334 | +            \DB::transaction(function() use ($value, $attribute, &$result, $saveLog) { | |
| 335 | 335 | $model = $this->model->lockForUpdate()->find($value); | 
| 336 | 336 | $model->delete(); | 
| 337 | 337 |                  $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $value, $model) : false; | 
| @@ -339,8 +339,8 @@ discard block | ||
| 339 | 339 | } | 
| 340 | 340 | else | 
| 341 | 341 |          { | 
| 342 | -            \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { | |
| 343 | -                call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model){ | |
| 342 | +            \DB::transaction(function() use ($value, $attribute, &$result, $saveLog) { | |
| 343 | +                call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function($model) { | |
| 344 | 344 | $model->delete(); | 
| 345 | 345 |                      $saveLog ? \Logging::saveLog('delete', class_basename($this->model), $this->getModel(), $model->id, $model) : false; | 
| 346 | 346 | }); | 
| @@ -377,7 +377,7 @@ discard block | ||
| 377 | 377 |      { | 
| 378 | 378 | $conditions = $this->constructConditions($conditions); | 
| 379 | 379 | $sort = $desc ? 'desc' : 'asc'; | 
| 380 | -        return call_user_func_array("{$this->getModel()}::with",  array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); | |
| 380 | +        return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); | |
| 381 | 381 | } | 
| 382 | 382 | |
| 383 | 383 | /** | 
| @@ -408,21 +408,21 @@ discard block | ||
| 408 | 408 |          { | 
| 409 | 409 | if ($key == 'and') | 
| 410 | 410 |              { | 
| 411 | -                $conditionString  .= str_replace('{op}', 'and', $this->constructConditions($value)['conditionString']) . ' {op} '; | |
| 411 | +                $conditionString  .= str_replace('{op}', 'and', $this->constructConditions($value)['conditionString']).' {op} '; | |
| 412 | 412 | $conditionValues = array_merge($conditionValues, $this->constructConditions($value)['conditionValues']); | 
| 413 | 413 | } | 
| 414 | 414 | else if ($key == 'or') | 
| 415 | 415 |              { | 
| 416 | -                $conditionString  .= str_replace('{op}', 'or', $this->constructConditions($value)['conditionString']) . ' {op} '; | |
| 416 | +                $conditionString  .= str_replace('{op}', 'or', $this->constructConditions($value)['conditionString']).' {op} '; | |
| 417 | 417 | $conditionValues = array_merge($conditionValues, $this->constructConditions($value)['conditionValues']); | 
| 418 | 418 | } | 
| 419 | 419 | else | 
| 420 | 420 |              { | 
| 421 | -                $conditionString  .= $key . '=? {op} '; | |
| 421 | +                $conditionString  .= $key.'=? {op} '; | |
| 422 | 422 | $conditionValues[] = $value; | 
| 423 | 423 | } | 
| 424 | 424 | } | 
| 425 | -        $conditionString = '(' . rtrim($conditionString, '{op} ') . ')'; | |
| 425 | +        $conditionString = '('.rtrim($conditionString, '{op} ').')'; | |
| 426 | 426 | return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; | 
| 427 | 427 | } | 
| 428 | 428 | |
| @@ -213,8 +213,7 @@ discard block | ||
| 213 | 213 | } | 
| 214 | 214 | } | 
| 215 | 215 | $relations[$relation][] = $relationModel; | 
| 216 | - } | |
| 217 | - else | |
| 216 | + } else | |
| 218 | 217 |                              { | 
| 219 | 218 | if (gettype($val) !== 'object' && gettype($val) !== 'array') | 
| 220 | 219 |                                  { | 
| @@ -234,8 +233,7 @@ discard block | ||
| 234 | 233 | } | 
| 235 | 234 | } | 
| 236 | 235 | } | 
| 237 | - } | |
| 238 | - else if (array_search($key, $model->getFillable(), true) !== false) | |
| 236 | + } else if (array_search($key, $model->getFillable(), true) !== false) | |
| 239 | 237 |                  { | 
| 240 | 238 | $model->$key = $value; | 
| 241 | 239 | } | 
| @@ -247,8 +245,7 @@ discard block | ||
| 247 | 245 | if ($value == 'delete' && $model->$key()->count()) | 
| 248 | 246 |                  { | 
| 249 | 247 | $model->$key()->delete(); | 
| 250 | - } | |
| 251 | - else if (gettype($value) == 'array') | |
| 248 | + } else if (gettype($value) == 'array') | |
| 252 | 249 |                  { | 
| 253 | 250 | $ids = []; | 
| 254 | 251 | foreach ($value as $val) | 
| @@ -279,8 +276,7 @@ discard block | ||
| 279 | 276 | $model->$key()->attach($ids); | 
| 280 | 277 | break; | 
| 281 | 278 | } | 
| 282 | - } | |
| 283 | - else | |
| 279 | + } else | |
| 284 | 280 |                  { | 
| 285 | 281 | switch (class_basename($model->$key())) | 
| 286 | 282 |                      { | 
| @@ -358,8 +354,7 @@ discard block | ||
| 358 | 354 | $model->delete(); | 
| 359 | 355 |                  $saveLog ? $this->logs->saveLog('delete', class_basename($this->model), $this->getModel(), $value, $model) : false; | 
| 360 | 356 | }); | 
| 361 | - } | |
| 362 | - else | |
| 357 | + } else | |
| 363 | 358 |          { | 
| 364 | 359 |              \DB::transaction(function () use ($value, $attribute, &$result, $saveLog) { | 
| 365 | 360 |                  call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model){ | 
| @@ -432,13 +427,11 @@ discard block | ||
| 432 | 427 |              { | 
| 433 | 428 |                  $conditionString  .= str_replace('{op}', 'and', $this->constructConditions($value)['conditionString']) . ' {op} '; | 
| 434 | 429 | $conditionValues = array_merge($conditionValues, $this->constructConditions($value)['conditionValues']); | 
| 435 | - } | |
| 436 | - else if ($key == 'or') | |
| 430 | + } else if ($key == 'or') | |
| 437 | 431 |              { | 
| 438 | 432 |                  $conditionString  .= str_replace('{op}', 'or', $this->constructConditions($value)['conditionString']) . ' {op} '; | 
| 439 | 433 | $conditionValues = array_merge($conditionValues, $this->constructConditions($value)['conditionValues']); | 
| 440 | - } | |
| 441 | - else | |
| 434 | + } else | |
| 442 | 435 |              { | 
| 443 | 436 |                  $conditionString  .= $key . '=? {op} '; | 
| 444 | 437 | $conditionValues[] = $value; | 
| @@ -54,9 +54,7 @@ discard block | ||
| 54 | 54 | /** | 
| 55 | 55 | * Fetch all records with relations from model repository. | 
| 56 | 56 | * | 
| 57 | - * @param string $sortBy | |
| 58 | - * @param boolean $desc | |
| 59 | - * @return \Illuminate\Http\Response | |
| 57 | + * @return \Illuminate\Http\JsonResponse|null | |
| 60 | 58 | */ | 
| 61 | 59 | public function getIndex() | 
| 62 | 60 |      { | 
| @@ -71,7 +69,7 @@ discard block | ||
| 71 | 69 | * Fetch the single object with relations from model repository. | 
| 72 | 70 | * | 
| 73 | 71 | * @param integer $id | 
| 74 | - * @return \Illuminate\Http\Response | |
| 72 | + * @return \Illuminate\Http\JsonResponse|null | |
| 75 | 73 | */ | 
| 76 | 74 | public function getFind($id) | 
| 77 | 75 |      { | 
| @@ -89,8 +87,8 @@ discard block | ||
| 89 | 87 | * @param string $query | 
| 90 | 88 | * @param integer $perPage | 
| 91 | 89 | * @param string $sortBy | 
| 92 | - * @param boolean $desc | |
| 93 | - * @return \Illuminate\Http\Response | |
| 90 | + * @param integer $desc | |
| 91 | + * @return \Illuminate\Http\JsonResponse|null | |
| 94 | 92 | */ | 
| 95 | 93 | public function getSearch($query = '', $perPage = 15, $sortBy = 'created_at', $desc = 1) | 
| 96 | 94 |      { | 
| @@ -107,8 +105,8 @@ discard block | ||
| 107 | 105 | * | 
| 108 | 106 | * @param \Illuminate\Http\Request $request | 
| 109 | 107 | * @param string $sortBy | 
| 110 | - * @param boolean $desc | |
| 111 | - * @return \Illuminate\Http\Response | |
| 108 | + * @param integer $desc | |
| 109 | + * @return \Illuminate\Http\JsonResponse|null | |
| 112 | 110 | */ | 
| 113 | 111 | public function postFindby(Request $request, $sortBy = 'created_at', $desc = 1) | 
| 114 | 112 |      { | 
| @@ -124,7 +122,7 @@ discard block | ||
| 124 | 122 | * condition. | 
| 125 | 123 | * | 
| 126 | 124 | * @param \Illuminate\Http\Request $request | 
| 127 | - * @return \Illuminate\Http\Response | |
| 125 | + * @return \Illuminate\Http\JsonResponse|null | |
| 128 | 126 | */ | 
| 129 | 127 | public function postFirst(Request $request) | 
| 130 | 128 |      { | 
| @@ -140,8 +138,8 @@ discard block | ||
| 140 | 138 | * | 
| 141 | 139 | * @param integer $perPage | 
| 142 | 140 | * @param string $sortBy | 
| 143 | - * @param boolean $desc | |
| 144 | - * @return \Illuminate\Http\Response | |
| 141 | + * @param integer $desc | |
| 142 | + * @return \Illuminate\Http\JsonResponse|null | |
| 145 | 143 | */ | 
| 146 | 144 | public function getPaginate($perPage = 15, $sortBy = 'created_at', $desc = 1) | 
| 147 | 145 |      { | 
| @@ -159,8 +157,8 @@ discard block | ||
| 159 | 157 | * @param \Illuminate\Http\Request $request | 
| 160 | 158 | * @param integer $perPage | 
| 161 | 159 | * @param string $sortBy | 
| 162 | - * @param boolean $desc | |
| 163 | - * @return \Illuminate\Http\Response | |
| 160 | + * @param integer $desc | |
| 161 | + * @return \Illuminate\Http\JsonResponse|null | |
| 164 | 162 | */ | 
| 165 | 163 | public function postPaginateby(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) | 
| 166 | 164 |      { | 
| @@ -175,7 +173,7 @@ discard block | ||
| 175 | 173 | * Save the given model to repository. | 
| 176 | 174 | * | 
| 177 | 175 | * @param \Illuminate\Http\Request $request | 
| 178 | - * @return \Illuminate\Http\Response | |
| 176 | + * @return \Illuminate\Http\JsonResponse|null | |
| 179 | 177 | */ | 
| 180 | 178 | public function postSave(Request $request) | 
| 181 | 179 |      { | 
| @@ -208,7 +206,7 @@ discard block | ||
| 208 | 206 | * Delete by the given id from model repository. | 
| 209 | 207 | * | 
| 210 | 208 | * @param integer $id | 
| 211 | - * @return \Illuminate\Http\Response | |
| 209 | + * @return \Illuminate\Http\JsonResponse|null | |
| 212 | 210 | */ | 
| 213 | 211 | public function getDelete($id) | 
| 214 | 212 |      { | 
| @@ -6,221 +6,221 @@ | ||
| 6 | 6 | |
| 7 | 7 | class BaseApiController extends Controller | 
| 8 | 8 |  { | 
| 9 | - /** | |
| 10 | - * The model implementation. | |
| 11 | - * | |
| 12 | - * @var model | |
| 13 | - */ | |
| 14 | - protected $model; | |
| 15 | - | |
| 16 | - /** | |
| 17 | - * The config implementation. | |
| 18 | - * | |
| 19 | - * @var config | |
| 20 | - */ | |
| 21 | - protected $config; | |
| 22 | - | |
| 23 | - public function __construct() | |
| 24 | -    { | |
| 25 | -        \Session::set('timeZoneDiff', \Request::header('time-zone-diff') ?: 0); | |
| 9 | + /** | |
| 10 | + * The model implementation. | |
| 11 | + * | |
| 12 | + * @var model | |
| 13 | + */ | |
| 14 | + protected $model; | |
| 15 | + | |
| 16 | + /** | |
| 17 | + * The config implementation. | |
| 18 | + * | |
| 19 | + * @var config | |
| 20 | + */ | |
| 21 | + protected $config; | |
| 22 | + | |
| 23 | + public function __construct() | |
| 24 | +	{ | |
| 25 | +		\Session::set('timeZoneDiff', \Request::header('time-zone-diff') ?: 0); | |
| 26 | 26 | |
| 27 | - $this->config = \CoreConfig::getConfig(); | |
| 28 | - $this->model = property_exists($this, 'model') ? $this->model : false; | |
| 29 | - $this->validationRules = property_exists($this, 'validationRules') ? $this->validationRules : false; | |
| 30 | - $this->skipPermissionCheck = property_exists($this, 'skipPermissionCheck') ? $this->skipPermissionCheck : []; | |
| 31 | - $this->skipLoginCheck = property_exists($this, 'skipLoginCheck') ? $this->skipLoginCheck : []; | |
| 32 | - $this->relations = array_key_exists($this->model, $this->config['relations']) ? $this->config['relations'][$this->model] : false; | |
| 33 | -        $route                     = explode('@',\Route::currentRouteAction())[1]; | |
| 34 | -        $this->checkPermission(explode('_', snake_case($route))[1]); | |
| 35 | - } | |
| 36 | - | |
| 37 | - /** | |
| 38 | - * Fetch all records with relations from model repository. | |
| 39 | - * | |
| 40 | - * @param string $sortBy | |
| 41 | - * @param boolean $desc | |
| 42 | - * @return \Illuminate\Http\Response | |
| 43 | - */ | |
| 44 | - public function getIndex() | |
| 45 | -    { | |
| 46 | - if ($this->model) | |
| 47 | -        { | |
| 48 | - $relations = $this->relations && $this->relations['all'] ? $this->relations['all'] : []; | |
| 49 | -            return \Response::json(call_user_func_array("\Core::{$this->model}", [])->all($relations), 200); | |
| 50 | - } | |
| 51 | - } | |
| 52 | - | |
| 53 | - /** | |
| 54 | - * Fetch the single object with relations from model repository. | |
| 55 | - * | |
| 56 | - * @param integer $id | |
| 57 | - * @return \Illuminate\Http\Response | |
| 58 | - */ | |
| 59 | - public function getFind($id) | |
| 60 | -    { | |
| 61 | - if ($this->model) | |
| 62 | -        { | |
| 63 | - $relations = $this->relations && $this->relations['find'] ? $this->relations['find'] : []; | |
| 64 | -            return \Response::json(call_user_func_array("\Core::{$this->model}", [])->find($id, $relations), 200); | |
| 65 | - } | |
| 66 | - } | |
| 67 | - | |
| 68 | - /** | |
| 69 | - * Paginate all records with relations from model repository | |
| 70 | - * that matche the given query. | |
| 71 | - * | |
| 72 | - * @param string $query | |
| 73 | - * @param integer $perPage | |
| 74 | - * @param string $sortBy | |
| 75 | - * @param boolean $desc | |
| 76 | - * @return \Illuminate\Http\Response | |
| 77 | - */ | |
| 78 | - public function getSearch($query = '', $perPage = 15, $sortBy = 'created_at', $desc = 1) | |
| 79 | -    { | |
| 80 | - if ($this->model) | |
| 81 | -        { | |
| 82 | - $relations = $this->relations && $this->relations['paginate'] ? $this->relations['paginate'] : []; | |
| 83 | -            return \Response::json(call_user_func_array("\Core::{$this->model}", [])->search($query, $perPage, $relations, $sortBy, $desc), 200); | |
| 84 | - } | |
| 85 | - } | |
| 86 | - | |
| 87 | - /** | |
| 88 | - * Fetch records from the storage based on the given | |
| 89 | - * condition. | |
| 90 | - * | |
| 91 | - * @param \Illuminate\Http\Request $request | |
| 92 | - * @param string $sortBy | |
| 93 | - * @param boolean $desc | |
| 94 | - * @return \Illuminate\Http\Response | |
| 95 | - */ | |
| 96 | - public function postFindby(Request $request, $sortBy = 'created_at', $desc = 1) | |
| 97 | -    { | |
| 98 | - if ($this->model) | |
| 99 | -        { | |
| 100 | - $relations = $this->relations && $this->relations['findBy'] ? $this->relations['findBy'] : []; | |
| 101 | -            return \Response::json(call_user_func_array("\Core::{$this->model}", [])->findBy($request->all(), $relations, $sortBy, $desc), 200); | |
| 102 | - } | |
| 103 | - } | |
| 104 | - | |
| 105 | - /** | |
| 106 | - * Fetch the first record from the storage based on the given | |
| 107 | - * condition. | |
| 108 | - * | |
| 109 | - * @param \Illuminate\Http\Request $request | |
| 110 | - * @return \Illuminate\Http\Response | |
| 111 | - */ | |
| 112 | - public function postFirst(Request $request) | |
| 113 | -    { | |
| 114 | - if ($this->model) | |
| 115 | -        { | |
| 116 | - $relations = $this->relations && $this->relations['first'] ? $this->relations['first'] : []; | |
| 117 | -            return \Response::json(call_user_func_array("\Core::{$this->model}", [])->first($request->all(), $relations), 200); | |
| 118 | - } | |
| 119 | - } | |
| 120 | - | |
| 121 | - /** | |
| 122 | - * Paginate all records with relations from model repository. | |
| 123 | - * | |
| 124 | - * @param integer $perPage | |
| 125 | - * @param string $sortBy | |
| 126 | - * @param boolean $desc | |
| 127 | - * @return \Illuminate\Http\Response | |
| 128 | - */ | |
| 129 | - public function getPaginate($perPage = 15, $sortBy = 'created_at', $desc = 1) | |
| 130 | -    { | |
| 131 | - if ($this->model) | |
| 132 | -        { | |
| 133 | - $relations = $this->relations && $this->relations['paginate'] ? $this->relations['paginate'] : []; | |
| 134 | -            return \Response::json(call_user_func_array("\Core::{$this->model}", [])->paginate($perPage, $relations, $sortBy, $desc), 200); | |
| 135 | - } | |
| 136 | - } | |
| 137 | - | |
| 138 | - /** | |
| 139 | - * Fetch all records with relations based on | |
| 140 | - * the given condition from storage in pages. | |
| 141 | - * | |
| 142 | - * @param \Illuminate\Http\Request $request | |
| 143 | - * @param integer $perPage | |
| 144 | - * @param string $sortBy | |
| 145 | - * @param boolean $desc | |
| 146 | - * @return \Illuminate\Http\Response | |
| 147 | - */ | |
| 148 | - public function postPaginateby(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) | |
| 149 | -    { | |
| 150 | - if ($this->model) | |
| 151 | -        { | |
| 152 | - $relations = $this->relations && $this->relations['paginateBy'] ? $this->relations['paginateBy'] : []; | |
| 153 | -            return \Response::json(call_user_func_array("\Core::{$this->model}", [])->paginateBy($request->all(), $perPage, $relations, $sortBy, $desc), 200); | |
| 154 | - } | |
| 155 | - } | |
| 156 | - | |
| 157 | - /** | |
| 158 | - * Save the given model to repository. | |
| 159 | - * | |
| 160 | - * @param \Illuminate\Http\Request $request | |
| 161 | - * @return \Illuminate\Http\Response | |
| 162 | - */ | |
| 163 | - public function postSave(Request $request) | |
| 164 | -    { | |
| 165 | - foreach ($this->validationRules as &$rule) | |
| 166 | -        { | |
| 167 | - if (strpos($rule, 'exists') && ! strpos($rule, 'deleted_at,NULL')) | |
| 168 | -            { | |
| 169 | - $rule .= ',deleted_at,NULL'; | |
| 170 | - } | |
| 171 | - | |
| 172 | -            if ($request->has('id'))  | |
| 173 | -            { | |
| 174 | -                $rule = str_replace('{id}', $request->get('id'), $rule); | |
| 175 | - } | |
| 176 | - else | |
| 177 | -            { | |
| 178 | -                $rule = str_replace(',{id}', '', $rule); | |
| 179 | - } | |
| 180 | - } | |
| 27 | + $this->config = \CoreConfig::getConfig(); | |
| 28 | + $this->model = property_exists($this, 'model') ? $this->model : false; | |
| 29 | + $this->validationRules = property_exists($this, 'validationRules') ? $this->validationRules : false; | |
| 30 | + $this->skipPermissionCheck = property_exists($this, 'skipPermissionCheck') ? $this->skipPermissionCheck : []; | |
| 31 | + $this->skipLoginCheck = property_exists($this, 'skipLoginCheck') ? $this->skipLoginCheck : []; | |
| 32 | + $this->relations = array_key_exists($this->model, $this->config['relations']) ? $this->config['relations'][$this->model] : false; | |
| 33 | +		$route                     = explode('@',\Route::currentRouteAction())[1]; | |
| 34 | +		$this->checkPermission(explode('_', snake_case($route))[1]); | |
| 35 | + } | |
| 36 | + | |
| 37 | + /** | |
| 38 | + * Fetch all records with relations from model repository. | |
| 39 | + * | |
| 40 | + * @param string $sortBy | |
| 41 | + * @param boolean $desc | |
| 42 | + * @return \Illuminate\Http\Response | |
| 43 | + */ | |
| 44 | + public function getIndex() | |
| 45 | +	{ | |
| 46 | + if ($this->model) | |
| 47 | +		{ | |
| 48 | + $relations = $this->relations && $this->relations['all'] ? $this->relations['all'] : []; | |
| 49 | +			return \Response::json(call_user_func_array("\Core::{$this->model}", [])->all($relations), 200); | |
| 50 | + } | |
| 51 | + } | |
| 52 | + | |
| 53 | + /** | |
| 54 | + * Fetch the single object with relations from model repository. | |
| 55 | + * | |
| 56 | + * @param integer $id | |
| 57 | + * @return \Illuminate\Http\Response | |
| 58 | + */ | |
| 59 | + public function getFind($id) | |
| 60 | +	{ | |
| 61 | + if ($this->model) | |
| 62 | +		{ | |
| 63 | + $relations = $this->relations && $this->relations['find'] ? $this->relations['find'] : []; | |
| 64 | +			return \Response::json(call_user_func_array("\Core::{$this->model}", [])->find($id, $relations), 200); | |
| 65 | + } | |
| 66 | + } | |
| 67 | + | |
| 68 | + /** | |
| 69 | + * Paginate all records with relations from model repository | |
| 70 | + * that matche the given query. | |
| 71 | + * | |
| 72 | + * @param string $query | |
| 73 | + * @param integer $perPage | |
| 74 | + * @param string $sortBy | |
| 75 | + * @param boolean $desc | |
| 76 | + * @return \Illuminate\Http\Response | |
| 77 | + */ | |
| 78 | + public function getSearch($query = '', $perPage = 15, $sortBy = 'created_at', $desc = 1) | |
| 79 | +	{ | |
| 80 | + if ($this->model) | |
| 81 | +		{ | |
| 82 | + $relations = $this->relations && $this->relations['paginate'] ? $this->relations['paginate'] : []; | |
| 83 | +			return \Response::json(call_user_func_array("\Core::{$this->model}", [])->search($query, $perPage, $relations, $sortBy, $desc), 200); | |
| 84 | + } | |
| 85 | + } | |
| 86 | + | |
| 87 | + /** | |
| 88 | + * Fetch records from the storage based on the given | |
| 89 | + * condition. | |
| 90 | + * | |
| 91 | + * @param \Illuminate\Http\Request $request | |
| 92 | + * @param string $sortBy | |
| 93 | + * @param boolean $desc | |
| 94 | + * @return \Illuminate\Http\Response | |
| 95 | + */ | |
| 96 | + public function postFindby(Request $request, $sortBy = 'created_at', $desc = 1) | |
| 97 | +	{ | |
| 98 | + if ($this->model) | |
| 99 | +		{ | |
| 100 | + $relations = $this->relations && $this->relations['findBy'] ? $this->relations['findBy'] : []; | |
| 101 | +			return \Response::json(call_user_func_array("\Core::{$this->model}", [])->findBy($request->all(), $relations, $sortBy, $desc), 200); | |
| 102 | + } | |
| 103 | + } | |
| 104 | + | |
| 105 | + /** | |
| 106 | + * Fetch the first record from the storage based on the given | |
| 107 | + * condition. | |
| 108 | + * | |
| 109 | + * @param \Illuminate\Http\Request $request | |
| 110 | + * @return \Illuminate\Http\Response | |
| 111 | + */ | |
| 112 | + public function postFirst(Request $request) | |
| 113 | +	{ | |
| 114 | + if ($this->model) | |
| 115 | +		{ | |
| 116 | + $relations = $this->relations && $this->relations['first'] ? $this->relations['first'] : []; | |
| 117 | +			return \Response::json(call_user_func_array("\Core::{$this->model}", [])->first($request->all(), $relations), 200); | |
| 118 | + } | |
| 119 | + } | |
| 120 | + | |
| 121 | + /** | |
| 122 | + * Paginate all records with relations from model repository. | |
| 123 | + * | |
| 124 | + * @param integer $perPage | |
| 125 | + * @param string $sortBy | |
| 126 | + * @param boolean $desc | |
| 127 | + * @return \Illuminate\Http\Response | |
| 128 | + */ | |
| 129 | + public function getPaginate($perPage = 15, $sortBy = 'created_at', $desc = 1) | |
| 130 | +	{ | |
| 131 | + if ($this->model) | |
| 132 | +		{ | |
| 133 | + $relations = $this->relations && $this->relations['paginate'] ? $this->relations['paginate'] : []; | |
| 134 | +			return \Response::json(call_user_func_array("\Core::{$this->model}", [])->paginate($perPage, $relations, $sortBy, $desc), 200); | |
| 135 | + } | |
| 136 | + } | |
| 137 | + | |
| 138 | + /** | |
| 139 | + * Fetch all records with relations based on | |
| 140 | + * the given condition from storage in pages. | |
| 141 | + * | |
| 142 | + * @param \Illuminate\Http\Request $request | |
| 143 | + * @param integer $perPage | |
| 144 | + * @param string $sortBy | |
| 145 | + * @param boolean $desc | |
| 146 | + * @return \Illuminate\Http\Response | |
| 147 | + */ | |
| 148 | + public function postPaginateby(Request $request, $perPage = 15, $sortBy = 'created_at', $desc = 1) | |
| 149 | +	{ | |
| 150 | + if ($this->model) | |
| 151 | +		{ | |
| 152 | + $relations = $this->relations && $this->relations['paginateBy'] ? $this->relations['paginateBy'] : []; | |
| 153 | +			return \Response::json(call_user_func_array("\Core::{$this->model}", [])->paginateBy($request->all(), $perPage, $relations, $sortBy, $desc), 200); | |
| 154 | + } | |
| 155 | + } | |
| 156 | + | |
| 157 | + /** | |
| 158 | + * Save the given model to repository. | |
| 159 | + * | |
| 160 | + * @param \Illuminate\Http\Request $request | |
| 161 | + * @return \Illuminate\Http\Response | |
| 162 | + */ | |
| 163 | + public function postSave(Request $request) | |
| 164 | +	{ | |
| 165 | + foreach ($this->validationRules as &$rule) | |
| 166 | +		{ | |
| 167 | + if (strpos($rule, 'exists') && ! strpos($rule, 'deleted_at,NULL')) | |
| 168 | +			{ | |
| 169 | + $rule .= ',deleted_at,NULL'; | |
| 170 | + } | |
| 171 | + | |
| 172 | +			if ($request->has('id'))  | |
| 173 | +			{ | |
| 174 | +				$rule = str_replace('{id}', $request->get('id'), $rule); | |
| 175 | + } | |
| 176 | + else | |
| 177 | +			{ | |
| 178 | +				$rule = str_replace(',{id}', '', $rule); | |
| 179 | + } | |
| 180 | + } | |
| 181 | 181 | |
| 182 | - $this->validate($request, $this->validationRules); | |
| 183 | - | |
| 184 | - if ($this->model) | |
| 185 | -        { | |
| 186 | -            return \Response::json(call_user_func_array("\Core::{$this->model}", [])->save($request->all()), 200); | |
| 187 | - } | |
| 188 | - } | |
| 189 | - | |
| 190 | - /** | |
| 191 | - * Delete by the given id from model repository. | |
| 192 | - * | |
| 193 | - * @param integer $id | |
| 194 | - * @return \Illuminate\Http\Response | |
| 195 | - */ | |
| 196 | - public function getDelete($id) | |
| 197 | -    { | |
| 198 | - if ($this->model) | |
| 199 | -        { | |
| 200 | -            return \Response::json(call_user_func_array("\Core::{$this->model}", [])->delete($id), 200); | |
| 201 | - } | |
| 202 | - } | |
| 203 | - | |
| 204 | - /** | |
| 205 | - * Check if the logged in user can do the given permission. | |
| 206 | - * | |
| 207 | - * @param string $permission | |
| 208 | - * @return void | |
| 209 | - */ | |
| 210 | - private function checkPermission($permission) | |
| 211 | -    { | |
| 212 | - $permission = $permission !== 'index' ? $permission : 'list'; | |
| 213 | - if ($permission == 'method') | |
| 214 | -        { | |
| 215 | -            \ErrorHandler::notFound('method'); | |
| 216 | - } | |
| 217 | - else if ( ! in_array($permission, $this->skipLoginCheck)) | |
| 218 | -        { | |
| 219 | - \JWTAuth::parseToken()->authenticate(); | |
| 220 | - if ( ! in_array($permission, $this->skipPermissionCheck) && ! \Core::users()->can($permission, $this->model)) | |
| 221 | -            { | |
| 222 | - \ErrorHandler::noPermissions(); | |
| 223 | - } | |
| 224 | - } | |
| 225 | - } | |
| 182 | + $this->validate($request, $this->validationRules); | |
| 183 | + | |
| 184 | + if ($this->model) | |
| 185 | +		{ | |
| 186 | +			return \Response::json(call_user_func_array("\Core::{$this->model}", [])->save($request->all()), 200); | |
| 187 | + } | |
| 188 | + } | |
| 189 | + | |
| 190 | + /** | |
| 191 | + * Delete by the given id from model repository. | |
| 192 | + * | |
| 193 | + * @param integer $id | |
| 194 | + * @return \Illuminate\Http\Response | |
| 195 | + */ | |
| 196 | + public function getDelete($id) | |
| 197 | +	{ | |
| 198 | + if ($this->model) | |
| 199 | +		{ | |
| 200 | +			return \Response::json(call_user_func_array("\Core::{$this->model}", [])->delete($id), 200); | |
| 201 | + } | |
| 202 | + } | |
| 203 | + | |
| 204 | + /** | |
| 205 | + * Check if the logged in user can do the given permission. | |
| 206 | + * | |
| 207 | + * @param string $permission | |
| 208 | + * @return void | |
| 209 | + */ | |
| 210 | + private function checkPermission($permission) | |
| 211 | +	{ | |
| 212 | + $permission = $permission !== 'index' ? $permission : 'list'; | |
| 213 | + if ($permission == 'method') | |
| 214 | +		{ | |
| 215 | +			\ErrorHandler::notFound('method'); | |
| 216 | + } | |
| 217 | + else if ( ! in_array($permission, $this->skipLoginCheck)) | |
| 218 | +		{ | |
| 219 | + \JWTAuth::parseToken()->authenticate(); | |
| 220 | + if ( ! in_array($permission, $this->skipPermissionCheck) && ! \Core::users()->can($permission, $this->model)) | |
| 221 | +			{ | |
| 222 | + \ErrorHandler::noPermissions(); | |
| 223 | + } | |
| 224 | + } | |
| 225 | + } | |
| 226 | 226 | } | 
| @@ -47,7 +47,7 @@ | ||
| 47 | 47 | $this->skipLoginCheck = property_exists($this, 'skipLoginCheck') ? $this->skipLoginCheck : []; | 
| 48 | 48 | |
| 49 | 49 | $this->relations = array_key_exists($this->model, $this->config['relations']) ? $this->config['relations'][$this->model] : false; | 
| 50 | -        $route                     = explode('@',\Route::currentRouteAction())[1]; | |
| 50 | +        $route                     = explode('@', \Route::currentRouteAction())[1]; | |
| 51 | 51 |          $this->checkPermission(explode('_', snake_case($route))[1]); | 
| 52 | 52 | } | 
| 53 | 53 | |
| @@ -190,8 +190,7 @@ discard block | ||
| 190 | 190 |              if ($request->has('id'))  | 
| 191 | 191 |              { | 
| 192 | 192 |                  $rule = str_replace('{id}', $request->get('id'), $rule); | 
| 193 | - } | |
| 194 | - else | |
| 193 | + } else | |
| 195 | 194 |              { | 
| 196 | 195 |                  $rule = str_replace(',{id}', '', $rule); | 
| 197 | 196 | } | 
| @@ -232,8 +231,7 @@ discard block | ||
| 232 | 231 |          { | 
| 233 | 232 |              $error = $this->errorHandler->notFound('method'); | 
| 234 | 233 | abort($error['status'], $error['message']); | 
| 235 | - } | |
| 236 | - else if ( ! in_array($permission, $this->skipLoginCheck)) | |
| 234 | + } else if ( ! in_array($permission, $this->skipLoginCheck)) | |
| 237 | 235 |          { | 
| 238 | 236 | \JWTAuth::parseToken()->authenticate(); | 
| 239 | 237 | if ( ! in_array($permission, $this->skipPermissionCheck) && ! \Core::users()->can($permission, $this->model)) | 
| @@ -7,7 +7,7 @@ discard block | ||
| 7 | 7 | * | 
| 8 | 8 | * @param array $relations | 
| 9 | 9 | * @param array $sortBy | 
| 10 | - * @param array $desc | |
| 10 | + * @param integer $desc | |
| 11 | 11 | * @param array $columns | 
| 12 | 12 | * @return collection | 
| 13 | 13 | */ | 
| @@ -21,7 +21,7 @@ discard block | ||
| 21 | 21 | * @param integer $perPage | 
| 22 | 22 | * @param array $relations | 
| 23 | 23 | * @param array $sortBy | 
| 24 | - * @param array $desc | |
| 24 | + * @param integer $desc | |
| 25 | 25 | * @param array $columns | 
| 26 | 26 | * @return collection | 
| 27 | 27 | */ | 
| @@ -33,7 +33,7 @@ discard block | ||
| 33 | 33 | * @param integer $perPage | 
| 34 | 34 | * @param array $relations | 
| 35 | 35 | * @param array $sortBy | 
| 36 | - * @param array $desc | |
| 36 | + * @param integer $desc | |
| 37 | 37 | * @param array $columns | 
| 38 | 38 | * @return collection | 
| 39 | 39 | */ | 
| @@ -47,7 +47,7 @@ discard block | ||
| 47 | 47 | * @param integer $perPage | 
| 48 | 48 | * @param array $relations | 
| 49 | 49 | * @param array $sortBy | 
| 50 | - * @param array $desc | |
| 50 | + * @param integer $desc | |
| 51 | 51 | * @param array $columns | 
| 52 | 52 | * @return collection | 
| 53 | 53 | */ | 
| @@ -109,7 +109,7 @@ discard block | ||
| 109 | 109 | * @param array $conditions array of conditions | 
| 110 | 110 | * @param array $relations | 
| 111 | 111 | * @param array $sortBy | 
| 112 | - * @param array $desc | |
| 112 | + * @param integer $desc | |
| 113 | 113 | * @param array $columns | 
| 114 | 114 | * @return collection | 
| 115 | 115 | */ | 
| @@ -120,7 +120,6 @@ discard block | ||
| 120 | 120 | * condition. | 
| 121 | 121 | * | 
| 122 | 122 | * @param array $conditions array of conditions | 
| 123 | - * @param [type] $value condition value | |
| 124 | 123 | * @param array $relations | 
| 125 | 124 | * @param array $columns | 
| 126 | 125 | * @return object | 
| @@ -3,127 +3,127 @@ | ||
| 3 | 3 | interface RepositoryInterface | 
| 4 | 4 |  { | 
| 5 | 5 | /** | 
| 6 | - * Fetch all records with relations from the storage. | |
| 7 | - * | |
| 8 | - * @param array $relations | |
| 9 | - * @param array $sortBy | |
| 10 | - * @param array $desc | |
| 11 | - * @param array $columns | |
| 12 | - * @return collection | |
| 13 | - */ | |
| 6 | + * Fetch all records with relations from the storage. | |
| 7 | + * | |
| 8 | + * @param array $relations | |
| 9 | + * @param array $sortBy | |
| 10 | + * @param array $desc | |
| 11 | + * @param array $columns | |
| 12 | + * @return collection | |
| 13 | + */ | |
| 14 | 14 |  	public function all($relations = [], $sortBy = 'created_at', $desc = 0, $columns = array('*')); | 
| 15 | 15 | |
| 16 | - /** | |
| 17 | - * Fetch all records with relations from storage in pages | |
| 18 | - * that matche the given query. | |
| 19 | - * | |
| 20 | - * @param string $query | |
| 21 | - * @param integer $perPage | |
| 22 | - * @param array $relations | |
| 23 | - * @param array $sortBy | |
| 24 | - * @param array $desc | |
| 25 | - * @param array $columns | |
| 26 | - * @return collection | |
| 27 | - */ | |
| 28 | -    public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = array('*')); | |
| 16 | + /** | |
| 17 | + * Fetch all records with relations from storage in pages | |
| 18 | + * that matche the given query. | |
| 19 | + * | |
| 20 | + * @param string $query | |
| 21 | + * @param integer $perPage | |
| 22 | + * @param array $relations | |
| 23 | + * @param array $sortBy | |
| 24 | + * @param array $desc | |
| 25 | + * @param array $columns | |
| 26 | + * @return collection | |
| 27 | + */ | |
| 28 | +	public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = array('*')); | |
| 29 | 29 | |
| 30 | 30 | /** | 
| 31 | - * Fetch all records with relations from storage in pages. | |
| 32 | - * | |
| 33 | - * @param integer $perPage | |
| 34 | - * @param array $relations | |
| 35 | - * @param array $sortBy | |
| 36 | - * @param array $desc | |
| 37 | - * @param array $columns | |
| 38 | - * @return collection | |
| 39 | - */ | |
| 40 | -    public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = array('*')); | |
| 31 | + * Fetch all records with relations from storage in pages. | |
| 32 | + * | |
| 33 | + * @param integer $perPage | |
| 34 | + * @param array $relations | |
| 35 | + * @param array $sortBy | |
| 36 | + * @param array $desc | |
| 37 | + * @param array $columns | |
| 38 | + * @return collection | |
| 39 | + */ | |
| 40 | +	public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = array('*')); | |
| 41 | 41 | |
| 42 | 42 | /** | 
| 43 | - * Fetch all records with relations based on | |
| 44 | - * the given condition from storage in pages. | |
| 45 | - * | |
| 46 | - * @param array $conditions array of conditions | |
| 47 | - * @param integer $perPage | |
| 48 | - * @param array $relations | |
| 49 | - * @param array $sortBy | |
| 50 | - * @param array $desc | |
| 51 | - * @param array $columns | |
| 52 | - * @return collection | |
| 53 | - */ | |
| 54 | -    public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = array('*')); | |
| 43 | + * Fetch all records with relations based on | |
| 44 | + * the given condition from storage in pages. | |
| 45 | + * | |
| 46 | + * @param array $conditions array of conditions | |
| 47 | + * @param integer $perPage | |
| 48 | + * @param array $relations | |
| 49 | + * @param array $sortBy | |
| 50 | + * @param array $desc | |
| 51 | + * @param array $columns | |
| 52 | + * @return collection | |
| 53 | + */ | |
| 54 | +	public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = array('*')); | |
| 55 | 55 | |
| 56 | - /** | |
| 57 | - * Save the given model/models to the storage. | |
| 58 | - * | |
| 59 | - * @param array $data | |
| 60 | - * @param boolean $saveLog | |
| 61 | - * @return object | |
| 62 | - */ | |
| 63 | - public function save(array $data, $saveLog = true); | |
| 56 | + /** | |
| 57 | + * Save the given model/models to the storage. | |
| 58 | + * | |
| 59 | + * @param array $data | |
| 60 | + * @param boolean $saveLog | |
| 61 | + * @return object | |
| 62 | + */ | |
| 63 | + public function save(array $data, $saveLog = true); | |
| 64 | 64 | |
| 65 | - /** | |
| 66 | - * Insert multiple records to the storage. | |
| 67 | - * | |
| 68 | - * @param array $data | |
| 69 | - * @return array | |
| 70 | - */ | |
| 71 | - public function saveMany(array $data); | |
| 65 | + /** | |
| 66 | + * Insert multiple records to the storage. | |
| 67 | + * | |
| 68 | + * @param array $data | |
| 69 | + * @return array | |
| 70 | + */ | |
| 71 | + public function saveMany(array $data); | |
| 72 | 72 | |
| 73 | 73 | /** | 
| 74 | - * Update record in the storage based on the given | |
| 75 | - * condition. | |
| 76 | - * | |
| 77 | - * @param [type] $value condition value | |
| 78 | - * @param array $data | |
| 79 | - * @param string $attribute condition column name | |
| 80 | - * @return integer affected rows | |
| 81 | - */ | |
| 82 | - public function update($value, array $data, $attribute = 'id'); | |
| 74 | + * Update record in the storage based on the given | |
| 75 | + * condition. | |
| 76 | + * | |
| 77 | + * @param [type] $value condition value | |
| 78 | + * @param array $data | |
| 79 | + * @param string $attribute condition column name | |
| 80 | + * @return integer affected rows | |
| 81 | + */ | |
| 82 | + public function update($value, array $data, $attribute = 'id'); | |
| 83 | 83 | |
| 84 | 84 | /** | 
| 85 | - * Delete record from the storage based on the given | |
| 86 | - * condition. | |
| 87 | - * | |
| 88 | - * @param [type] $value condition value | |
| 89 | - * @param string $attribute condition column name | |
| 90 | - * @return integer affected rows | |
| 91 | - */ | |
| 92 | - public function delete($value, $attribute = 'id'); | |
| 85 | + * Delete record from the storage based on the given | |
| 86 | + * condition. | |
| 87 | + * | |
| 88 | + * @param [type] $value condition value | |
| 89 | + * @param string $attribute condition column name | |
| 90 | + * @return integer affected rows | |
| 91 | + */ | |
| 92 | + public function delete($value, $attribute = 'id'); | |
| 93 | 93 | |
| 94 | 94 | /** | 
| 95 | - * Fetch records from the storage based on the given | |
| 96 | - * id. | |
| 97 | - * | |
| 98 | - * @param integer $id | |
| 99 | - * @param array $relations | |
| 100 | - * @param array $columns | |
| 101 | - * @return object | |
| 102 | - */ | |
| 103 | -    public function find($id, $relations = [], $columns = array('*')); | |
| 95 | + * Fetch records from the storage based on the given | |
| 96 | + * id. | |
| 97 | + * | |
| 98 | + * @param integer $id | |
| 99 | + * @param array $relations | |
| 100 | + * @param array $columns | |
| 101 | + * @return object | |
| 102 | + */ | |
| 103 | +	public function find($id, $relations = [], $columns = array('*')); | |
| 104 | 104 | |
| 105 | 105 | /** | 
| 106 | - * Fetch records from the storage based on the given | |
| 107 | - * condition. | |
| 108 | - * | |
| 109 | - * @param array $conditions array of conditions | |
| 110 | - * @param array $relations | |
| 111 | - * @param array $sortBy | |
| 112 | - * @param array $desc | |
| 113 | - * @param array $columns | |
| 114 | - * @return collection | |
| 115 | - */ | |
| 116 | -    public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = array('*')); | |
| 106 | + * Fetch records from the storage based on the given | |
| 107 | + * condition. | |
| 108 | + * | |
| 109 | + * @param array $conditions array of conditions | |
| 110 | + * @param array $relations | |
| 111 | + * @param array $sortBy | |
| 112 | + * @param array $desc | |
| 113 | + * @param array $columns | |
| 114 | + * @return collection | |
| 115 | + */ | |
| 116 | +	public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 0, $columns = array('*')); | |
| 117 | 117 | |
| 118 | - /** | |
| 119 | - * Fetch the first record fro the storage based on the given | |
| 120 | - * condition. | |
| 121 | - * | |
| 122 | - * @param array $conditions array of conditions | |
| 123 | - * @param [type] $value condition value | |
| 124 | - * @param array $relations | |
| 125 | - * @param array $columns | |
| 126 | - * @return object | |
| 127 | - */ | |
| 128 | -    public function first($conditions, $relations = [], $columns = array('*')); | |
| 118 | + /** | |
| 119 | + * Fetch the first record fro the storage based on the given | |
| 120 | + * condition. | |
| 121 | + * | |
| 122 | + * @param array $conditions array of conditions | |
| 123 | + * @param [type] $value condition value | |
| 124 | + * @param array $relations | |
| 125 | + * @param array $columns | |
| 126 | + * @return object | |
| 127 | + */ | |
| 128 | +	public function first($conditions, $relations = [], $columns = array('*')); | |
| 129 | 129 | } | 
| 130 | 130 | \ No newline at end of file | 
| @@ -46,7 +46,7 @@ | ||
| 46 | 46 | * | 
| 47 | 47 | * @param array $conditions array of conditions | 
| 48 | 48 | * @param array $relations | 
| 49 | - * @param array $colunmns | |
| 49 | + * @param array $columns | |
| 50 | 50 | * @return object | 
| 51 | 51 | */ | 
| 52 | 52 |      public function first($conditions, $relations = [], $columns = array('*')) | 
| @@ -11,59 +11,59 @@ | ||
| 11 | 11 | */ | 
| 12 | 12 | protected function getModel() | 
| 13 | 13 |  	{ | 
| 14 | -        $apiVersion = \Request::header('api-version') ?: 1; | |
| 14 | +		$apiVersion = \Request::header('api-version') ?: 1; | |
| 15 | 15 | return 'App\Modules\V1\Reporting\Report'; | 
| 16 | 16 | } | 
| 17 | 17 | |
| 18 | 18 | /** | 
| 19 | - * Render the given report db view. | |
| 20 | - * | |
| 21 | - * @param integer $id | |
| 22 | - * @param array $relations | |
| 23 | - * @param array $columns | |
| 24 | - * @return object | |
| 25 | - */ | |
| 26 | -    public function find($id, $relations = [], $columns = array('*')) | |
| 27 | -    { | |
| 19 | + * Render the given report db view. | |
| 20 | + * | |
| 21 | + * @param integer $id | |
| 22 | + * @param array $relations | |
| 23 | + * @param array $columns | |
| 24 | + * @return object | |
| 25 | + */ | |
| 26 | +	public function find($id, $relations = [], $columns = array('*')) | |
| 27 | +	{ | |
| 28 | 28 |  		$report = call_user_func_array("{$this->getModel()}::with", array($relations))->find($id, $columns); | 
| 29 | 29 | |
| 30 | - if ( ! $report) | |
| 31 | -        { | |
| 32 | -            \ErrorHandler::notFound('report'); | |
| 33 | - } | |
| 30 | + if ( ! $report) | |
| 31 | +		{ | |
| 32 | +			\ErrorHandler::notFound('report'); | |
| 33 | + } | |
| 34 | 34 | |
| 35 | - if ( ! \Core::users()->can($report->view_name, 'reports')) | |
| 36 | -        { | |
| 37 | - \ErrorHandler::noPermissions(); | |
| 38 | - } | |
| 35 | + if ( ! \Core::users()->can($report->view_name, 'reports')) | |
| 36 | +		{ | |
| 37 | + \ErrorHandler::noPermissions(); | |
| 38 | + } | |
| 39 | 39 | |
| 40 | - return \DB::table($report->view_name)->get(); | |
| 41 | - } | |
| 40 | + return \DB::table($report->view_name)->get(); | |
| 41 | + } | |
| 42 | 42 | |
| 43 | - /** | |
| 44 | - * Render the given report db view based on the given | |
| 45 | - * condition. | |
| 46 | - * | |
| 47 | - * @param array $conditions array of conditions | |
| 48 | - * @param array $relations | |
| 49 | - * @param array $colunmns | |
| 50 | - * @return object | |
| 51 | - */ | |
| 52 | -    public function first($conditions, $relations = [], $columns = array('*')) | |
| 53 | -    { | |
| 43 | + /** | |
| 44 | + * Render the given report db view based on the given | |
| 45 | + * condition. | |
| 46 | + * | |
| 47 | + * @param array $conditions array of conditions | |
| 48 | + * @param array $relations | |
| 49 | + * @param array $colunmns | |
| 50 | + * @return object | |
| 51 | + */ | |
| 52 | +	public function first($conditions, $relations = [], $columns = array('*')) | |
| 53 | +	{ | |
| 54 | 54 | $conditions = $this->constructConditions($conditions); | 
| 55 | 55 |  		$report     = call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); | 
| 56 | 56 | |
| 57 | - if ( ! $report) | |
| 58 | -        { | |
| 59 | -            \ErrorHandler::notFound('report'); | |
| 60 | - } | |
| 57 | + if ( ! $report) | |
| 58 | +		{ | |
| 59 | +			\ErrorHandler::notFound('report'); | |
| 60 | + } | |
| 61 | 61 | |
| 62 | - if ( ! \Core::users()->can($report->view_name, 'reports')) | |
| 63 | -        { | |
| 64 | - \ErrorHandler::noPermissions(); | |
| 65 | - } | |
| 62 | + if ( ! \Core::users()->can($report->view_name, 'reports')) | |
| 63 | +		{ | |
| 64 | + \ErrorHandler::noPermissions(); | |
| 65 | + } | |
| 66 | 66 | |
| 67 | - return \DB::table($report->view_name)->get(); | |
| 68 | - } | |
| 67 | + return \DB::table($report->view_name)->get(); | |
| 68 | + } | |
| 69 | 69 | } | 
| @@ -2,18 +2,18 @@ | ||
| 2 | 2 | |
| 3 | 3 | class Log | 
| 4 | 4 |  { | 
| 5 | - public function saveLog($action, $item_name, $item_type, $item_id, $model = false) | |
| 6 | -    { | |
| 7 | - if (\Core::logs() && $item_name !== 'Log') | |
| 8 | -    	{ | |
| 9 | - $item_name = $item_name; | |
| 10 | - \Core::logs()->save([ | |
| 11 | - 'action' => $action, | |
| 12 | - 'item_name' => $item_name, | |
| 13 | - 'item_type' => $item_type, | |
| 14 | - 'item_id' => $item_id, | |
| 15 | - 'user_id' => \JWTAuth::parseToken()->authenticate()->id, | |
| 16 | - ], false, false); | |
| 17 | - } | |
| 18 | - } | |
| 5 | + public function saveLog($action, $item_name, $item_type, $item_id, $model = false) | |
| 6 | +	{ | |
| 7 | + if (\Core::logs() && $item_name !== 'Log') | |
| 8 | +		{ | |
| 9 | + $item_name = $item_name; | |
| 10 | + \Core::logs()->save([ | |
| 11 | + 'action' => $action, | |
| 12 | + 'item_name' => $item_name, | |
| 13 | + 'item_type' => $item_type, | |
| 14 | + 'item_id' => $item_id, | |
| 15 | + 'user_id' => \JWTAuth::parseToken()->authenticate()->id, | |
| 16 | + ], false, false); | |
| 17 | + } | |
| 18 | + } | |
| 19 | 19 | } | 
| 20 | 20 | \ No newline at end of file | 
| @@ -2,14 +2,14 @@ discard block | ||
| 2 | 2 | |
| 3 | 3 | class CoreConfig | 
| 4 | 4 |  { | 
| 5 | - public function getConfig() | |
| 6 | -    { | |
| 7 | - $customSettings = []; | |
| 8 | -    	Settings::get(['key', 'value'])->each(function ($setting) use (&$customSettings){ | |
| 9 | - $customSettings[$setting['key']] = $setting['value']; | |
| 10 | - }); | |
| 5 | + public function getConfig() | |
| 6 | +	{ | |
| 7 | + $customSettings = []; | |
| 8 | +		Settings::get(['key', 'value'])->each(function ($setting) use (&$customSettings){ | |
| 9 | + $customSettings[$setting['key']] = $setting['value']; | |
| 10 | + }); | |
| 11 | 11 | |
| 12 | - return array_merge($customSettings, [ | |
| 12 | + return array_merge($customSettings, [ | |
| 13 | 13 | /** | 
| 14 | 14 | * Specify what relations should be used for every model. | 
| 15 | 15 | */ | 
| @@ -56,5 +56,5 @@ discard block | ||
| 56 | 56 | ], | 
| 57 | 57 | ] | 
| 58 | 58 | ]); | 
| 59 | - } | |
| 59 | + } | |
| 60 | 60 | } | 
| 61 | 61 | \ No newline at end of file | 
| @@ -5,7 +5,7 @@ | ||
| 5 | 5 | public function getConfig() | 
| 6 | 6 |      { | 
| 7 | 7 | $customSettings = []; | 
| 8 | -    	Settings::get(['key', 'value'])->each(function ($setting) use (&$customSettings){ | |
| 8 | +    	Settings::get(['key', 'value'])->each(function($setting) use (&$customSettings){ | |
| 9 | 9 | $customSettings[$setting['key']] = $setting['value']; | 
| 10 | 10 | }); | 
| 11 | 11 | |
| @@ -5,41 +5,41 @@ | ||
| 5 | 5 | |
| 6 | 6 |  class Log extends Model{ | 
| 7 | 7 | |
| 8 | - use SoftDeletes; | |
| 9 | - protected $table = 'logs'; | |
| 10 | - protected $dates = ['created_at', 'updated_at', 'deleted_at']; | |
| 11 | - protected $hidden = ['deleted_at', 'item_type']; | |
| 12 | - protected $guarded = ['id']; | |
| 13 | - protected $fillable = ['action', 'item_name', 'item_type', 'item_id', 'user_id']; | |
| 14 | - | |
| 15 | - public function getCreatedAtAttribute($value) | |
| 16 | -    { | |
| 17 | -        return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); | |
| 18 | - } | |
| 19 | - | |
| 20 | - public function getUpdatedAtAttribute($value) | |
| 21 | -    { | |
| 22 | -        return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); | |
| 23 | - } | |
| 24 | - | |
| 25 | - public function getDeletedAtAttribute($value) | |
| 26 | -    { | |
| 27 | -        return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); | |
| 28 | - } | |
| 8 | + use SoftDeletes; | |
| 9 | + protected $table = 'logs'; | |
| 10 | + protected $dates = ['created_at', 'updated_at', 'deleted_at']; | |
| 11 | + protected $hidden = ['deleted_at', 'item_type']; | |
| 12 | + protected $guarded = ['id']; | |
| 13 | + protected $fillable = ['action', 'item_name', 'item_type', 'item_id', 'user_id']; | |
| 14 | + | |
| 15 | + public function getCreatedAtAttribute($value) | |
| 16 | +	{ | |
| 17 | +		return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); | |
| 18 | + } | |
| 19 | + | |
| 20 | + public function getUpdatedAtAttribute($value) | |
| 21 | +	{ | |
| 22 | +		return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); | |
| 23 | + } | |
| 24 | + | |
| 25 | + public function getDeletedAtAttribute($value) | |
| 26 | +	{ | |
| 27 | +		return \Carbon\Carbon::parse($value)->addHours(\Session::get('timeZoneDiff'))->toDateTimeString(); | |
| 28 | + } | |
| 29 | 29 | |
| 30 | - public function user() | |
| 31 | -    { | |
| 32 | -        return $this->belongsTo('App\Modules\V1\Acl\AclUser'); | |
| 33 | - } | |
| 34 | - | |
| 35 | - public function item() | |
| 36 | -    { | |
| 37 | - return $this->morphTo(); | |
| 38 | - } | |
| 39 | - | |
| 40 | - public static function boot() | |
| 41 | -    { | |
| 42 | - parent::boot(); | |
| 43 | -        parent::observe(\App::make('App\Modules\V1\Core\ModelObservers\LogObserver')); | |
| 44 | - } | |
| 30 | + public function user() | |
| 31 | +	{ | |
| 32 | +		return $this->belongsTo('App\Modules\V1\Acl\AclUser'); | |
| 33 | + } | |
| 34 | + | |
| 35 | + public function item() | |
| 36 | +	{ | |
| 37 | + return $this->morphTo(); | |
| 38 | + } | |
| 39 | + | |
| 40 | + public static function boot() | |
| 41 | +	{ | |
| 42 | + parent::boot(); | |
| 43 | +		parent::observe(\App::make('App\Modules\V1\Core\ModelObservers\LogObserver')); | |
| 44 | + } | |
| 45 | 45 | } | 
| @@ -3,7 +3,7 @@ | ||
| 3 | 3 | use Illuminate\Database\Eloquent\Model; | 
| 4 | 4 | use Illuminate\Database\Eloquent\SoftDeletes; | 
| 5 | 5 | |
| 6 | -class Log extends Model{ | |
| 6 | +class Log extends Model { | |
| 7 | 7 | |
| 8 | 8 | use SoftDeletes; | 
| 9 | 9 | protected $table = 'logs'; | 
| @@ -13,13 +13,13 @@ | ||
| 13 | 13 | public function up() | 
| 14 | 14 |  	{ | 
| 15 | 15 |  		Schema::create('settings', function (Blueprint $table) { | 
| 16 | -            $table->increments('id'); | |
| 17 | -            $table->string('name',100); | |
| 18 | -            $table->string('key',100)->unique(); | |
| 19 | -            $table->string('value',100); | |
| 20 | - $table->softDeletes(); | |
| 21 | - $table->timestamps(); | |
| 22 | - }); | |
| 16 | +			$table->increments('id'); | |
| 17 | +			$table->string('name',100); | |
| 18 | +			$table->string('key',100)->unique(); | |
| 19 | +			$table->string('value',100); | |
| 20 | + $table->softDeletes(); | |
| 21 | + $table->timestamps(); | |
| 22 | + }); | |
| 23 | 23 | } | 
| 24 | 24 | |
| 25 | 25 | /** | 
| @@ -12,11 +12,11 @@ | ||
| 12 | 12 | */ | 
| 13 | 13 | public function up() | 
| 14 | 14 |  	{ | 
| 15 | -		Schema::create('settings', function (Blueprint $table) { | |
| 15 | +		Schema::create('settings', function(Blueprint $table) { | |
| 16 | 16 |              $table->increments('id'); | 
| 17 | -            $table->string('name',100); | |
| 18 | -            $table->string('key',100)->unique(); | |
| 19 | -            $table->string('value',100); | |
| 17 | +            $table->string('name', 100); | |
| 18 | +            $table->string('key', 100)->unique(); | |
| 19 | +            $table->string('value', 100); | |
| 20 | 20 | $table->softDeletes(); | 
| 21 | 21 | $table->timestamps(); | 
| 22 | 22 | }); |