diff --git a/v5/Http/Controllers/PostController.php b/v5/Http/Controllers/PostController.php index f588b02943..f50b7a8540 100644 --- a/v5/Http/Controllers/PostController.php +++ b/v5/Http/Controllers/PostController.php @@ -37,9 +37,9 @@ protected function ignoreInput() * @return mixed * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function show(int $id) + public function show(Request $request, int $id) { - $post = Post::withPostValues()->where('id', $id)->first(); + $post = Post::withPostValues()->where('id', $id)->first(POST::selectModelFields($request)); if (!$post) { return self::make404(); @@ -55,9 +55,9 @@ public function show(int $id) * @return PostCollection * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function index() + public function index(Request $request) { - return new PostCollection(Post::withPostValues()->paginate(20)); + return new PostCollection(Post::withPostValues()->paginate(20, POST::selectModelFields($request))); } //end index() private function getUser() diff --git a/v5/Http/Controllers/SurveyController.php b/v5/Http/Controllers/SurveyController.php index c3f4ec4450..edd18217de 100644 --- a/v5/Http/Controllers/SurveyController.php +++ b/v5/Http/Controllers/SurveyController.php @@ -21,14 +21,14 @@ class SurveyController extends V5Controller * @return mixed * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function show(int $id) + public function show(Request $request, int $id) { - $survey = Survey::find($id); + $survey = Survey::find($id, Survey::selectModelFields($request)); if (!$survey) { return self::make404(); } return new SurveyResource($survey); - }//end show() + } //end show() /** @@ -37,10 +37,10 @@ public function show(int $id) * @return SurveyCollection * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function index() + public function index(Request $request) { - return new SurveyCollection(Survey::all()); - }//end index() + return new SurveyCollection(Survey::all(Survey::selectModelFields($request))); + } //end index() /** * Display the specified resource. @@ -123,11 +123,11 @@ public function store(Request $request) 'field' ); } - }//end foreach - }//end if + } //end foreach + } //end if return new SurveyResource($survey); - }//end store() + } //end store() /** * Display the specified resource. @@ -169,7 +169,7 @@ public function update(int $id, Request $request) $survey->load('tasks'); return new SurveyResource($survey); - }//end update() + } //end update() /** * @param array $input_tasks @@ -204,7 +204,7 @@ private function updateTasks(array $input_tasks, Survey $survey) 'task' ); $this->updateFields(($stage['fields'] ?? []), $stage_model); - }//end foreach + } //end foreach $input_tasks_collection = new Collection($input_tasks); $survey->load('tasks'); @@ -216,7 +216,7 @@ private function updateTasks(array $input_tasks, Survey $survey) foreach ($tasks_to_delete as $task_to_delete) { Stage::where('id', $task_to_delete->id)->delete(); } - }//end updateTasks() + } //end updateTasks() private function isArrayOfNumbers(array $arr) { @@ -264,7 +264,7 @@ private function updateFields(array $input_fields, Stage $stage) ) ); $added_fields[] = $field_model->id; - }//end if + } //end if $this->updateTranslations( $field_model, @@ -273,7 +273,7 @@ private function updateFields(array $input_fields, Stage $stage) $field_model->id, 'field' ); - }//end foreach + } //end foreach $input_fields_collection = new Collection($input_fields); $stage->load('fields'); @@ -285,7 +285,7 @@ private function updateFields(array $input_fields, Stage $stage) foreach ($fields_to_delete as $field_to_delete) { Attribute::where('id', $field_to_delete->id)->delete(); } - }//end updateFields() + } //end updateFields() /** @@ -313,5 +313,5 @@ public function delete(int $id, Request $request) $survey->delete(); return response()->json(['result' => ['deleted' => $id]]); - }//end delete() + } //end delete() }//end class diff --git a/v5/Http/Resources/BaseResource.php b/v5/Http/Resources/BaseResource.php index 66d1cdde20..bd4bd48f89 100644 --- a/v5/Http/Resources/BaseResource.php +++ b/v5/Http/Resources/BaseResource.php @@ -16,57 +16,17 @@ use Illuminate\Http\Request; use Illuminate\Http\Resources\Json\Resource; +use v5\Traits\HasHydrate; +use v5\Traits\HasOnlyParameters; class BaseResource extends Resource { + use HasHydrate; + use HasOnlyParameters; + public static $wrap = 'result'; - public function getHydrate($relationships, Request $request) - { - $only_original = self::toHydrate($request, $relationships); - return array_filter($only_original, function ($o) use ($relationships) { - return in_array($o, $relationships); - }); - } - - public static function toHydrate(Request $request, $relationships) - { - $to_hydrate = $relationships; - if ($request->has('hydrate') && !$request->get('hydrate')) { - $to_hydrate = []; - } - if ($request->get('hydrate')) { - $to_hydrate = explode(',', $request->get('hydrate')); - } - return $to_hydrate; - } - - public static function onlyOriginal($request, $approved_fields) - { - $only_original = $approved_fields; - if ($request->query('format') === 'minimal') { - $only_original = ['id', 'name', 'description', 'translations']; - } elseif ($request->get('only')) { - $only_original = explode(',', $request->get('only')); - } - return $only_original; - } - - public static function includeFields($request, $approved_fields = []) - { - $fields = $approved_fields; - if ($request->has('only') && !$request->get('only')) { - return []; - } - $only_original = self::onlyOriginal($request, $approved_fields); - if (count($only_original) > 0) { - $fields = array_filter($only_original, function ($f) use ($approved_fields) { - return in_array($f, $approved_fields); - }); - } - return $fields; - } - + protected function setResourceFields($fields) { $result = []; diff --git a/v5/Models/BaseModel.php b/v5/Models/BaseModel.php index a92740a178..a55a51c8ec 100644 --- a/v5/Models/BaseModel.php +++ b/v5/Models/BaseModel.php @@ -6,6 +6,7 @@ use Illuminate\Support\Str; use Illuminate\Support\Facades\Validator; use Illuminate\Support\Facades\DB; +use v5\Traits\HasOnlyParameters; /** * Class ResourceModel @@ -14,7 +15,8 @@ */ class BaseModel extends Model { - + use HasOnlyParameters; + protected $validationRules = []; /** diff --git a/v5/Models/Post/Post.php b/v5/Models/Post/Post.php index 90a88c2e1d..ce5e3d6b9c 100644 --- a/v5/Models/Post/Post.php +++ b/v5/Models/Post/Post.php @@ -1,4 +1,5 @@ 'report', 'locale' => 'en_US', @@ -220,7 +221,7 @@ public function validationMessages() ] ) ]; - }//end validationMessages() + } //end validationMessages() /** * Get the error messages for the defined *bulk* validation rules. @@ -247,7 +248,7 @@ private function bulkValidationMessages() ['field' => 'id'] ), ]; - }//end bulkValidationMessages() + } //end bulkValidationMessages() /** * Get the error messages for the defined *bulk* validation rules. @@ -273,7 +274,7 @@ public function bulkPatchValidationMessages() ) ] ); - }//end bulkValidationMessages() + } //end bulkValidationMessages() /** * Get the error messages for the defined *bulk* validation rules. @@ -308,7 +309,7 @@ public function getRules() 'title' => [ 'required', 'max:150', - 'regex:'.LegacyValidator::REGEX_STANDARD_TEXT, + 'regex:' . LegacyValidator::REGEX_STANDARD_TEXT, ], 'slug' => [ 'required', @@ -362,7 +363,7 @@ function ($attribute, $value, $fail) { 'locale', 'post_date' ]; - }//end getRules() + } //end getRules() /** * Get the post's translation. @@ -370,7 +371,7 @@ function ($attribute, $value, $fail) { public function translations() { return $this->morphMany('v5\Models\Translation', 'translatable'); - }//end translations() + } //end translations() public function getUserIdAttribute($value) @@ -558,7 +559,7 @@ protected static function valueTypesRelationships() 'Point', 'Relation', 'PostsMedia', -// 'PostsSet', + // 'PostsSet', 'PostTag' ]; return array_map(function ($t) { @@ -642,7 +643,6 @@ public function valuesMedia() public function valuesPoint() { return $this->hasMany('v5\Models\PostValues\PostPoint', 'post_id', 'id'); - ; } public function valuesRelation() @@ -673,4 +673,22 @@ public function postStages() { return $this->hasMany('v5\Models\PostStages', 'post_id', 'id'); } + + /** + * get the required cloumns . + * + * @param Request $request + * @return array + */ + public static function selectModelFields(Request $request): array + { + return self::includeFields($request, (new Post())->fillable, [ + 'id', + 'parent_id', + 'base_language', + 'form_id', + 'status', + + ]); + } }//end class diff --git a/v5/Models/Survey.php b/v5/Models/Survey.php index 197578f43d..4f2e063674 100644 --- a/v5/Models/Survey.php +++ b/v5/Models/Survey.php @@ -7,6 +7,7 @@ use Illuminate\Support\Facades\Input; use Ushahidi\App\Validator\LegacyValidator; use Ushahidi\Core\Tools\Permissions\InteractsWithFormPermissions; +use Illuminate\Http\Request; class Survey extends BaseModel { @@ -235,7 +236,7 @@ public function validationMessages() // [[$this, 'canMakePrivate'], [':value', $type]] // ] ]; - }//end validationMessages() + } //end validationMessages() /** * Return all validation rules @@ -249,7 +250,7 @@ public function getRules() 'required', 'min:2', 'max:255', - 'regex:'.LegacyValidator::REGEX_STANDARD_TEXT, + 'regex:' . LegacyValidator::REGEX_STANDARD_TEXT, ], 'description' => [ 'string', @@ -267,7 +268,7 @@ public function getRules() 'targeted_survey' => ['boolean'], 'tasks.*.label' => [ 'required', - 'regex:'.LegacyValidator::REGEX_STANDARD_TEXT, + 'regex:' . LegacyValidator::REGEX_STANDARD_TEXT, ], 'tasks.*.type' => [Rule::in( [ @@ -347,7 +348,7 @@ function ($attribute, $value, $fail) { // should be removing that arbitrary limit since it's pretty rare // for it to be needed ]; - }//end getRules() + } //end getRules() public function canMakePrivate($value, $type) { @@ -378,7 +379,7 @@ public function getCanCreateAttribute() { $can_create = $this->getCanCreateRoles($this->id); return $can_create['roles']; - }//end getCanCreateAttribute() + } //end getCanCreateAttribute() private function getCanCreateRoles($form_id) @@ -391,7 +392,7 @@ private function getCanCreateRoles($form_id) */ $form_repo = service('repository.form'); return $form_repo->getRolesThatCanCreatePosts($form_id); - }//end getCanCreateRoles() + } //end getCanCreateRoles() /** @@ -413,9 +414,9 @@ public function tasks() 'v5\Models\Stage', 'form_id' ) - ->where('form_stages.show_when_published', '=', '1') - ->where('form_stages.task_is_internal_only', '=', '0'); - }//end tasks() + ->where('form_stages.show_when_published', '=', '1') + ->where('form_stages.task_is_internal_only', '=', '0'); + } //end tasks() /** @@ -424,7 +425,7 @@ public function tasks() public function translations() { return $this->morphMany('v5\Models\Translation', 'translatable'); - }//end translations() + } //end translations() /** * Get the survey color. @@ -448,4 +449,17 @@ public function setColorAttribute($value) $this->attributes['color'] = ltrim($value, '#'); } } + /** + * get the required columns . + * + * @param Request $request + * @return array + */ + public static function selectModelFields(Request $request): array + { + return self::includeFields($request, (new Survey())->fillable, [ + 'id', + 'base_language', + ]); + } }//end class diff --git a/v5/Traits/HasHydrate.php b/v5/Traits/HasHydrate.php new file mode 100644 index 0000000000..6314e77e7f --- /dev/null +++ b/v5/Traits/HasHydrate.php @@ -0,0 +1,44 @@ +has('hydrate') && !$request->get('hydrate')) { + $to_hydrate = []; + } + if ($request->get('hydrate')) { + $to_hydrate = explode(',', $request->get('hydrate')); + } + return $to_hydrate; + } + + /** + * get the approved hedrate relationships + * + * @param array $relationships + * @param Request $request + * @return array + */ + public function getHydrate(array $relationships, Request $request): array + { + $only_original = self::toHydrate($request, $relationships); + return array_filter($only_original, function ($o) use ($relationships) { + return in_array($o, $relationships); + }); + } +} diff --git a/v5/Traits/HasOnlyParameters.php b/v5/Traits/HasOnlyParameters.php new file mode 100644 index 0000000000..444735dc1d --- /dev/null +++ b/v5/Traits/HasOnlyParameters.php @@ -0,0 +1,47 @@ +query('format') === 'minimal') { + $only_original = ['id', 'name', 'description', 'translations']; + } elseif ($request->get('only')) { + $only_original = explode(',', $request->get('only')); + } + return $only_original; + } + + /** + * get the approved only fields + * @param Request $request + * @param array $approved_fields + * @param array $required_fields + * @return array + */ + public static function includeFields(Request $request, array $approved_fields = [], array $required_fields = []) + { + $only_fields = $approved_fields; + if ($request->has('only') && !$request->get('only')) { + return []; + } + $only_original = self::onlyOriginal($request, $approved_fields); + if (count($only_original) > 0) { + $only_fields = array_filter($only_original, function ($f) use ($approved_fields) { + return in_array($f, $approved_fields); + }); + } + return array_merge($required_fields, $only_fields); + } +}