Skip to content

Commit

Permalink
Merge branch 'develop' into USH-1592
Browse files Browse the repository at this point in the history
  • Loading branch information
Mh-Asmi authored Oct 29, 2024
2 parents 52ffd19 + 6556659 commit c0877dd
Show file tree
Hide file tree
Showing 14 changed files with 181 additions and 33 deletions.
17 changes: 17 additions & 0 deletions database/migrations/phinx/20241021194747_embiggen_mime_type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

use Phinx\Migration\AbstractMigration;

class EmbiggenMimeType extends AbstractMigration
{
public function change()
{
$this->table('media')
->changeColumn('mime', 'string', ['limit' => 128])
->update();
}

public function down()
{
}
}
1 change: 1 addition & 0 deletions resources/lang/en/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
'role_cannot_be_empty' => 'The role list must be NULL or an array with at least 1 role',
'tag_field_type_cannot_be_private' => 'Tag fields cannot be private.',
'tag_field_must_be_array' => 'Incorrect format for tags property.',
'media_field_must_be_array' => 'Incorrect format for media property.',
'field_required' => ':field is required',
'array' => ':field must be an array',
'integer' => ':field must be an integer',
Expand Down
6 changes: 3 additions & 3 deletions src/Ushahidi/Modules/V3/Validator/Media/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ public function validateMime($validation, $mime)

if (!$mime) {
$validation->error('mime', 'mime_not_empty');
} elseif (!in_array($mime, $allowed_mime_types)) {
$validation->error('mime', 'mime_type_not_allowed');
}
} //elseif (!in_array($mime, $allowed_mime_types)) {
// $validation->error('mime', 'mime_type_not_allowed');
// }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected function isSupported(Command $command)
public function __invoke(Action $action)
{
$this->isSupported($action);
$this->validateFileData($action->getMediaEntity());
// $this->validateFileData($action->getMediaEntity());
return $this->media_repository->create($action->getMediaEntity());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ protected function savePostValues(Post $post, array $post_content, int $post_id)
{
$errors = [];
$post->valuesPostTag()->delete();
$post->valuesMedia()->delete();
foreach ($post_content as $stage) {
if (!isset($stage['fields'])) {
continue;
Expand All @@ -40,7 +41,7 @@ protected function savePostValues(Post $post, array $post_content, int $post_id)
$for_delete = false;
$type = $field['type'];
if (!isset($field['value'])) {
if ($type === 'tags') {
if ($type === 'tags' || $type === 'media') {
continue;
}
$for_delete = true;
Expand All @@ -50,22 +51,22 @@ protected function savePostValues(Post $post, array $post_content, int $post_id)
// The reason is when a field value input is updated and then left empty (as long it's not required)
// the user wants to override the existing input value with an empty value.
if (!isset($field['value']['value'])) {
if ($type === 'tags') {
if ($type === 'tags' || $type === 'media') {
continue;
}
$for_delete = true;
}



if ($type === 'tags') {
// To Do : delete the tags
$type === 'tags' ? 'tag' : $type;
$this->savePostTags($post, $field['id'], $field['value']['value']);
continue;
}


if ($type === 'media') {
$this->savePostMedia($post, $field['id'], $field['value']['value']);
continue;
}

$class_name = "Ushahidi\Modules\V5\Models\PostValues\Post" . ucfirst($type);
if (!class_exists($class_name) &&
Expand Down Expand Up @@ -170,6 +171,22 @@ protected function savePostValues(Post $post, array $post_content, int $post_id)
return $errors;
}

protected function savePostMedia($post, $attr_id, $media)
{
if (!is_array($media)) {
throw new \Exception("$attr_id: media format is invalid.");
}
foreach ($media as $media_id) {
$post->valuesMedia()->create(
[
'post_id' => $post->id,
'form_attribute_id' => $attr_id,
'value' => $media_id
]
);
}
}


protected function savePostTags($post, $attr_id, $tags)
{
Expand Down
32 changes: 32 additions & 0 deletions src/Ushahidi/Modules/V5/Http/Resources/MediaCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Ushahidi\Modules\V5\Http\Resources;

use Illuminate\Http\Resources\Json\ResourceCollection;

class MediaCollection extends ResourceCollection
{
public static $wrap = 'results';

/**
* The resource that this resource collects.
*
* @var string
*/
public $collects = 'Ushahidi\Modules\V5\Http\Resources\MediaResource';
/**
* Transform the resource collection into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return $this->collection;
}

public function count()
{
return count($this->collection);
}
}
26 changes: 26 additions & 0 deletions src/Ushahidi/Modules/V5/Http/Resources/MediaResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
namespace Ushahidi\Modules\V5\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource as Resource;

class MediaResource extends Resource
{

use RequestCachedResource;

public static $wrap = 'result';

/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'media_id' => $this->value,
];
}
}
17 changes: 14 additions & 3 deletions src/Ushahidi/Modules/V5/Http/Resources/PostValueCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ public function toArray($request)
return $value->form_attribute_id == $field['id'];
})->values();

if ($field['type'] !== 'tags') {
$field['value'] = $field['value']->first();
} else {
if ($field['type'] === 'tags') {
$field['options'] = $field['options'] ?
new CategoryCollection($field_obj->options) :
$field['options'];
} elseif ($field['type'] === 'media') {
$field['value'] = $this->makeMediaValue($field['value']);
} else {
$field['value'] = $field['value']->first();
}

if (!empty($field['value'])) {
Expand All @@ -73,6 +75,8 @@ public function toArray($request)
$field['value'] = $field['value']->toArray($field['value']);
} elseif ($field['type'] === 'tags') {
$field['value'] = $this->makeCategoryValue($field['value']);
} elseif ($field['type'] === 'media') {
$field['value'] = $field['value']->toArray($field['value']);
} else {
$field['value'] = $this->makeValue($field['value']);
}
Expand Down Expand Up @@ -114,6 +118,13 @@ private function makeCategoryValue($value)
});
}

private function makeMediaValue($values)
{
return $values->map(function ($item, $key) {
return $item->getOriginal();
});
}

private function convertBooleanTaskValues($Task)
{
}
Expand Down
6 changes: 3 additions & 3 deletions src/Ushahidi/Modules/V5/Models/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ function ($attribute, $value, $fail) {
];
if (!$value) {
return $fail(trans('validation.mime_not_empty'));
} elseif (!in_array($value, $allowed_mime_types)) {
return $fail(trans('validation.mime_type_not_allowed'));
}
} //elseif (!in_array($value, $allowed_mime_types)) {
// return $fail(trans('validation.mime_type_not_allowed'));
// }
}
],

Expand Down
4 changes: 3 additions & 1 deletion src/Ushahidi/Modules/V5/Models/Post/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -436,8 +436,10 @@ function ($attribute, $value, $fail) {
'post_content.*.fields.*.type' => [
function ($attribute, $value, $fail) {
$get_value = RequestFacade::input(str_replace('.type', '.value.value', $attribute));
if ($value === 'tags' && !is_array($get_value)) {
if ($value === 'tags' && !is_array($get_value)) {
return $fail(trans('validation.tag_field_must_be_array'));
} elseif ($value === 'media' && !is_array($get_value)) {
return $fail(trans('validation.media_field_must_be_array'));
}
}
],
Expand Down
55 changes: 46 additions & 9 deletions src/Ushahidi/Modules/V5/Models/PostValues/PostMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,30 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Ushahidi\Modules\V5\Models\Helpers\HideTime;

class PostMedia extends PostValue
{
public $table = 'post_media';

/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = [
'created',
];

/**
* @var array
*/
protected $fillable = [
'post_id',
'form_attribute_id',
'value'
];

/**
* Get the error messages for the defined validation rules.
*
Expand All @@ -18,7 +37,7 @@ class PostMedia extends PostValue
public function validationMessages()
{
return [];
}//end validationMessages()
}

/**
* Return all validation rules
Expand All @@ -27,12 +46,30 @@ public function validationMessages()
*/
public function getRules()
{
$rules = [
'value' => [
'numeric',
Rule::exists('media', 'id')
],
return [
'post_id' => 'required|exists:posts,id',
'value' => 'required|exists:media,id',
'form_attribute_id' => 'required|exists:form_attribute,id'
];
return array_merge(parent::getRules(), $rules);
}//end getRules()
}//end class
}
public function attribute()
{
return $this->hasOne('Ushahidi\Modules\V5\Models\Attribute', 'id', 'form_attribute_id');
}

public function media()
{
return $this->hasOne('Ushahidi\Modules\V5\Models\Media', 'id', 'value');
}

public function post()
{
return $this->hasOne('Ushahidi\Modules\V5\Models\Post\Post', 'id', 'post_id');
}

public function getCreatedAttribute($value)
{
$time = HideTime::hideTime($value, $this->survey ? $this->survey->hide_time : true);
return self::makeDate($time);
}
}
4 changes: 3 additions & 1 deletion src/Ushahidi/Modules/V5/Requests/PostRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,10 @@ function ($attribute, $value, $fail) {
'post_content.*.fields.*.type' => [
function ($attribute, $value, $fail) {
$get_value = RequestFacade::input(str_replace('.type', '.value.value', $attribute));
if ($value === 'tags' && !is_array($get_value)) {
if ($value === 'tags' && !is_array($get_value)) {
return $fail(trans('validation.tag_field_must_be_array'));
} elseif ($value === 'media' && !is_array($get_value)) {
return $fail(trans('validation.media_field_must_be_array'));
}
}
],
Expand Down
9 changes: 6 additions & 3 deletions src/Ushahidi/Modules/V5/Requests/SurveyRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,13 @@ public function messages()
'validation.regex',
['field' => trans('fields.tasks.fields.response_private')]
),

// 'tasks.*.fields.*.response_private' => [
// @TODO add this custom validator for canMakePrivate
// [[$this, 'canMakePrivate'], [':value', $type]]
// ]


'base_language.max' => trans(
'validation.max',
[
Expand All @@ -173,7 +173,7 @@ public function messages()
)
];
}

private function postMethodRules()
{
return [
Expand Down Expand Up @@ -237,6 +237,9 @@ private function postMethodRules()
'number',
'relation',
'upload',
'image',
'audio',
'document',
'video',
'markdown',
'tags',
Expand Down
6 changes: 3 additions & 3 deletions tests/Integration/v5/posts/posts.v5.feature
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ Feature: Testing the Posts API
"id": 14,
"type": "media",
"value": {
"value": null
"value": [null]
}
},
{
Expand Down Expand Up @@ -391,7 +391,7 @@ Feature: Testing the Posts API
"id": 14,
"type": "media",
"value": {
"value": null
"value": [null]
}
}
],
Expand Down Expand Up @@ -618,7 +618,7 @@ Feature: Testing the Posts API
"id": 14,
"type": "media",
"value": {
"value": null
"value": [null]
}
}
],
Expand Down

0 comments on commit c0877dd

Please sign in to comment.