diff --git a/readme.md b/readme.md index e7431b6..4049a6a 100644 --- a/readme.md +++ b/readme.md @@ -16,17 +16,11 @@ $ composer require webplusm/laraextjs ``` ## Usage -@Todo +If you want some help, please send me an email. ## Change log Please see the [changelog](changelog.md) for more information on what has changed recently. -## Testing - -``` bash -$ composer test -``` - ## Contributing Please see [contributing.md](contributing.md) for details and a todolist. @@ -54,4 +48,4 @@ license. Please see the [license file](license.md) for more information. [link-travis]: https://travis-ci.org/webplusm/laraextjs [link-styleci]: https://styleci.io/repos/12345678 [link-author]: https://github.com/webplusm -[link-contributors]: ../../contributors] \ No newline at end of file +[link-contributors]: ../../contributors] diff --git a/src/LaraextjsController.php b/src/LaraextjsController.php index 7f7cf92..7fae314 100644 --- a/src/LaraextjsController.php +++ b/src/LaraextjsController.php @@ -16,6 +16,7 @@ use Gate; use Illuminate\Http\Request; use Validator; + use function foo\func; class LaraextjsController extends Controller { @@ -298,76 +299,72 @@ protected function applyFilterToQuery($query) $query->havingRaw($field . " " . $value['operator'] . " " . DB::raw($value['value'])); break; - + // filtre par rapport à une relation : Has (contient) case 'has' : - if(is_array($value)){ - $query->whereHas($field,function($q) use ($value){ - foreach ($value as $i => $v){ - $q->where(head($v),last($v)); + if (is_array($value)) { + $query->whereHas($field, function ($q) use ($value) { + foreach ($value as $i => $v) { + $q->where(head($v), last($v)); } }); - } - else + } else $query->has($field); break; - + // filtre par rapport à une relation : ou Has (ou contient) case 'or_has' : - if(is_array($value)){ - $query->orWhereHas($field,function($q) use ($value){ - foreach ($value as $i => $v){ - $q->where(head($v),last($v)); + if (is_array($value)) { + $query->orWhereHas($field, function ($q) use ($value) { + foreach ($value as $i => $v) { + $q->where(head($v), last($v)); } }); - } - else + } else $query->orHas($field); break; - + // filtre par rapport à une relation : doesnt Have (ne contient pas) case 'not_has' : - if(is_array($value)){ - $query->whereDoesntHave($field,function($q) use ($value){ - foreach ($value as $i => $v){ - $q->where(head($v),last($v)); + if (is_array($value)) { + $query->whereDoesntHave($field, function ($q) use ($value) { + foreach ($value as $i => $v) { + $q->where(head($v), last($v)); } }); - } - else + } else $query->doesntHave($field); break; - + // filtre par rapport à une relation : ou doesnt Have (ou ne contient pas) case 'or_not_has' : - if(is_array($value)){ - $query->orWhereDoesntHave($field,function($q) use ($value){ - foreach ($value as $i => $v){ - $q->where(head($v),last($v)); + if (is_array($value)) { + $query->orWhereDoesntHave($field, function ($q) use ($value) { + foreach ($value as $i => $v) { + $q->where(head($v), last($v)); } }); - } - else + } else $query->orDoesntHave($field); break; case 'or_sql' : if (is_array($field)) { - $query->where(function ($q) use ($value,$field) { + $query->where(function ($q) use ($value, $field) { foreach ($field as $i => $v) { $q->orWhere($v, '!=', $value); } }); - + } break; - + case 'and_or_sql' : if (is_array($field)) { if (!is_array($value)) { $value = explode(',', $value); } foreach ($field as $i => $f) { - + if (is_array($value) && count($value)) { $query->where(function ($q) use ($f, $value) { foreach ($value as $v) { @@ -381,34 +378,53 @@ protected function applyFilterToQuery($query) default: $q->orWhere($f, $v); break; - + } - - + + } - + }); } } } break; - + case 'like' : if (!is_array($value) && !empty($value)) { $valeur = explode(';', $value); - + } + $fields = explode('.', $field); if (is_array($valeur) && count($valeur) > 1) { - $query->where(function ($q) use ($valeur, $field) { - foreach ($valeur as $mot) { - if (!empty($mot)) - $q->orWhere($field, 'like', "%" . $mot . "%"); - } - }); - - - } else - $query->where($field, 'like', "%" . $value . "%"); + + if (is_array($fields) && count($fields) == 2) { + $query->whereHas($fields[0], function ($q) use ($valeur, $fields) { + $q->where(function ($qq) use ($valeur, $fields) { + foreach ($valeur as $mot) { + if (!empty($mot)) + $qq->orWhere($fields[1], 'like', "%" . $mot . "%"); + } + }); + }); + + } else { + $query->where(function ($q) use ($valeur, $field) { + foreach ($valeur as $mot) { + if (!empty($mot)) + $q->orWhere($field, 'like', "%" . $mot . "%"); + } + }); + } + + } else { + if (is_array($fields) && count($fields) == 2) { + $query->whereHas($fields[0], function ($q) use ($value, $fields) { + $q->where($fields[1], 'like', "%" . $value . "%"); + }); + } else + $query->where($field, 'like', "%" . $value . "%"); + } break; case 'eq' : @@ -631,10 +647,16 @@ protected function read() } $this->messageTxt = "Ok"; + $this->beforeRead(); + return $this->index(); } + public function beforeRead() + { + } + protected function index() { $response = []; @@ -657,10 +679,15 @@ protected function index() } $response[$this->readerRootProperty] = (is_array($data) ? $data : $data->toArray()); + $this->afterRead(); return $this->success($response); } + public function afterRead() + { + } + public function store() { if ($this->isAdminable) { @@ -668,10 +695,15 @@ public function store() return $this->failure([config('laravext.extjs.reader.message_property') => $this->errorMsg['notAuthorized']], 200); } } + $this->beforeStore(); return $this->postCreate(); } + public function beforeStore() + { + } + public function create() { if ($this->isAdminable) { @@ -701,12 +733,17 @@ public function postCreate() if (isset($this->_data[$this->_model->getKeyName()])) { $rootData[config('laraextjs.extjs.client_id_property')] = $this->_data[$this->_model->getKeyName()]; } + $this->afterStore(); return $this->success([$this->readerRootProperty => $rootData]); } } + public function afterStore() + { + } + public function update($id) { @@ -715,10 +752,15 @@ public function update($id) return $this->failure([config('laravext.extjs.reader.message_property') => $this->errorMsg['notAuthorized']], 200); } } + $this->beforeUpdate(); return $this->putUpdate($id); } + public function beforeUpdate() + { + } + public function putUpdate($id) { if (count($this->_data) === 0) { @@ -755,6 +797,8 @@ public function putUpdate($id) ], 200); } + $this->afterUpdate(); + return $this->success([ $this->writerRootProperty => method_exists($model, 'filterByKey') ? $model->filterByKey()->first() : $model->toArray() ]); @@ -762,6 +806,10 @@ public function putUpdate($id) } + public function afterUpdate() + { + } + protected function find($id) { $record = $this->getModelBaseQuery()->where($this->_model->getKeyName(), $id)->first(); @@ -796,7 +844,7 @@ protected function destroy($id) return $this->failure([config('laravext.extjs.reader.message_property') => $this->errorMsg['notAuthorized']], 200); } } - + $this->beforeDelete(); $model = $this->_model->find($id); if (is_null($model)) { @@ -812,10 +860,19 @@ protected function destroy($id) 'key' => $id ], 200); } + $this->afterDelete(); return $this->success(); } + public function beforeDelete() + { + } + + public function afterDelete() + { + } + protected function batchStore(array $collection) { $responseData = []; @@ -901,4 +958,6 @@ protected function isAuthorize($verbe) return false; } + + }