Skip to content

Commit 12f09f9

Browse files
ushahidleetuxpiper
authored andcommitted
Ush 1587 - Add 3 new media types on the backend (#5004)
* squashed commits
1 parent 54d057f commit 12f09f9

File tree

14 files changed

+181
-33
lines changed

14 files changed

+181
-33
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
use Phinx\Migration\AbstractMigration;
4+
5+
class EmbiggenMimeType extends AbstractMigration
6+
{
7+
public function change()
8+
{
9+
$this->table('media')
10+
->changeColumn('mime', 'string', ['limit' => 128])
11+
->update();
12+
}
13+
14+
public function down()
15+
{
16+
}
17+
}

resources/lang/en/validation.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
'role_cannot_be_empty' => 'The role list must be NULL or an array with at least 1 role',
3434
'tag_field_type_cannot_be_private' => 'Tag fields cannot be private.',
3535
'tag_field_must_be_array' => 'Incorrect format for tags property.',
36+
'media_field_must_be_array' => 'Incorrect format for media property.',
3637
'field_required' => ':field is required',
3738
'array' => ':field must be an array',
3839
'integer' => ':field must be an integer',

src/Ushahidi/Modules/V3/Validator/Media/Create.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ public function validateMime($validation, $mime)
6969

7070
if (!$mime) {
7171
$validation->error('mime', 'mime_not_empty');
72-
} elseif (!in_array($mime, $allowed_mime_types)) {
73-
$validation->error('mime', 'mime_type_not_allowed');
74-
}
72+
} //elseif (!in_array($mime, $allowed_mime_types)) {
73+
// $validation->error('mime', 'mime_type_not_allowed');
74+
// }
7575
}
7676
}

src/Ushahidi/Modules/V5/Actions/Media/Handlers/CreateMediaCommandHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ protected function isSupported(Command $command)
3535
public function __invoke(Action $action)
3636
{
3737
$this->isSupported($action);
38-
$this->validateFileData($action->getMediaEntity());
38+
// $this->validateFileData($action->getMediaEntity());
3939
return $this->media_repository->create($action->getMediaEntity());
4040
}
4141

src/Ushahidi/Modules/V5/Actions/Post/Handlers/AbstractPostCommandHandler.php

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ protected function savePostValues(Post $post, array $post_content, int $post_id)
3232
{
3333
$errors = [];
3434
$post->valuesPostTag()->delete();
35+
$post->valuesMedia()->delete();
3536
foreach ($post_content as $stage) {
3637
if (!isset($stage['fields'])) {
3738
continue;
@@ -40,7 +41,7 @@ protected function savePostValues(Post $post, array $post_content, int $post_id)
4041
$for_delete = false;
4142
$type = $field['type'];
4243
if (!isset($field['value'])) {
43-
if ($type === 'tags') {
44+
if ($type === 'tags' || $type === 'media') {
4445
continue;
4546
}
4647
$for_delete = true;
@@ -50,22 +51,22 @@ protected function savePostValues(Post $post, array $post_content, int $post_id)
5051
// The reason is when a field value input is updated and then left empty (as long it's not required)
5152
// the user wants to override the existing input value with an empty value.
5253
if (!isset($field['value']['value'])) {
53-
if ($type === 'tags') {
54+
if ($type === 'tags' || $type === 'media') {
5455
continue;
5556
}
5657
$for_delete = true;
5758
}
5859

59-
60-
6160
if ($type === 'tags') {
62-
// To Do : delete the tags
6361
$type === 'tags' ? 'tag' : $type;
6462
$this->savePostTags($post, $field['id'], $field['value']['value']);
6563
continue;
6664
}
6765

68-
66+
if ($type === 'media') {
67+
$this->savePostMedia($post, $field['id'], $field['value']['value']);
68+
continue;
69+
}
6970

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

174+
protected function savePostMedia($post, $attr_id, $media)
175+
{
176+
if (!is_array($media)) {
177+
throw new \Exception("$attr_id: media format is invalid.");
178+
}
179+
foreach ($media as $media_id) {
180+
$post->valuesMedia()->create(
181+
[
182+
'post_id' => $post->id,
183+
'form_attribute_id' => $attr_id,
184+
'value' => $media_id
185+
]
186+
);
187+
}
188+
}
189+
173190

174191
protected function savePostTags($post, $attr_id, $tags)
175192
{
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Ushahidi\Modules\V5\Http\Resources;
4+
5+
use Illuminate\Http\Resources\Json\ResourceCollection;
6+
7+
class MediaCollection extends ResourceCollection
8+
{
9+
public static $wrap = 'results';
10+
11+
/**
12+
* The resource that this resource collects.
13+
*
14+
* @var string
15+
*/
16+
public $collects = 'Ushahidi\Modules\V5\Http\Resources\MediaResource';
17+
/**
18+
* Transform the resource collection into an array.
19+
*
20+
* @param \Illuminate\Http\Request $request
21+
* @return array
22+
*/
23+
public function toArray($request)
24+
{
25+
return $this->collection;
26+
}
27+
28+
public function count()
29+
{
30+
return count($this->collection);
31+
}
32+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
namespace Ushahidi\Modules\V5\Http\Resources;
3+
4+
use Illuminate\Http\Resources\Json\JsonResource as Resource;
5+
6+
class MediaResource extends Resource
7+
{
8+
9+
use RequestCachedResource;
10+
11+
public static $wrap = 'result';
12+
13+
/**
14+
* Transform the resource into an array.
15+
*
16+
* @param \Illuminate\Http\Request $request
17+
* @return array
18+
*/
19+
public function toArray($request)
20+
{
21+
return [
22+
'id' => $this->id,
23+
'media_id' => $this->value,
24+
];
25+
}
26+
}

src/Ushahidi/Modules/V5/Http/Resources/PostValueCollection.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,14 @@ public function toArray($request)
5959
return $value->form_attribute_id == $field['id'];
6060
})->values();
6161

62-
if ($field['type'] !== 'tags') {
63-
$field['value'] = $field['value']->first();
64-
} else {
62+
if ($field['type'] === 'tags') {
6563
$field['options'] = $field['options'] ?
6664
new CategoryCollection($field_obj->options) :
6765
$field['options'];
66+
} elseif ($field['type'] === 'media') {
67+
$field['value'] = $this->makeMediaValue($field['value']);
68+
} else {
69+
$field['value'] = $field['value']->first();
6870
}
6971

7072
if (!empty($field['value'])) {
@@ -73,6 +75,8 @@ public function toArray($request)
7375
$field['value'] = $field['value']->toArray($field['value']);
7476
} elseif ($field['type'] === 'tags') {
7577
$field['value'] = $this->makeCategoryValue($field['value']);
78+
} elseif ($field['type'] === 'media') {
79+
$field['value'] = $field['value']->toArray($field['value']);
7680
} else {
7781
$field['value'] = $this->makeValue($field['value']);
7882
}
@@ -114,6 +118,13 @@ private function makeCategoryValue($value)
114118
});
115119
}
116120

121+
private function makeMediaValue($values)
122+
{
123+
return $values->map(function ($item, $key) {
124+
return $item->getOriginal();
125+
});
126+
}
127+
117128
private function convertBooleanTaskValues($Task)
118129
{
119130
}

src/Ushahidi/Modules/V5/Models/Media.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ function ($attribute, $value, $fail) {
8080
];
8181
if (!$value) {
8282
return $fail(trans('validation.mime_not_empty'));
83-
} elseif (!in_array($value, $allowed_mime_types)) {
84-
return $fail(trans('validation.mime_type_not_allowed'));
85-
}
83+
} //elseif (!in_array($value, $allowed_mime_types)) {
84+
// return $fail(trans('validation.mime_type_not_allowed'));
85+
// }
8686
}
8787
],
8888

src/Ushahidi/Modules/V5/Models/Post/Post.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,8 +435,10 @@ function ($attribute, $value, $fail) {
435435
'post_content.*.fields.*.type' => [
436436
function ($attribute, $value, $fail) {
437437
$get_value = RequestFacade::input(str_replace('.type', '.value.value', $attribute));
438-
if ($value === 'tags' && !is_array($get_value)) {
438+
if ($value === 'tags' && !is_array($get_value)) {
439439
return $fail(trans('validation.tag_field_must_be_array'));
440+
} elseif ($value === 'media' && !is_array($get_value)) {
441+
return $fail(trans('validation.media_field_must_be_array'));
440442
}
441443
}
442444
],

0 commit comments

Comments
 (0)