@@ -32,6 +32,7 @@ protected function savePostValues(Post $post, array $post_content, int $post_id)
32
32
{
33
33
$ errors = [];
34
34
$ post ->valuesPostTag ()->delete ();
35
+ $ post ->valuesMedia ()->delete ();
35
36
foreach ($ post_content as $ stage ) {
36
37
if (!isset ($ stage ['fields ' ])) {
37
38
continue ;
@@ -40,7 +41,7 @@ protected function savePostValues(Post $post, array $post_content, int $post_id)
40
41
$ for_delete = false ;
41
42
$ type = $ field ['type ' ];
42
43
if (!isset ($ field ['value ' ])) {
43
- if ($ type === 'tags ' ) {
44
+ if ($ type === 'tags ' || $ type === ' media ' ) {
44
45
continue ;
45
46
}
46
47
$ for_delete = true ;
@@ -50,22 +51,22 @@ protected function savePostValues(Post $post, array $post_content, int $post_id)
50
51
// The reason is when a field value input is updated and then left empty (as long it's not required)
51
52
// the user wants to override the existing input value with an empty value.
52
53
if (!isset ($ field ['value ' ]['value ' ])) {
53
- if ($ type === 'tags ' ) {
54
+ if ($ type === 'tags ' || $ type === ' media ' ) {
54
55
continue ;
55
56
}
56
57
$ for_delete = true ;
57
58
}
58
59
59
-
60
-
61
60
if ($ type === 'tags ' ) {
62
- // To Do : delete the tags
63
61
$ type === 'tags ' ? 'tag ' : $ type ;
64
62
$ this ->savePostTags ($ post , $ field ['id ' ], $ field ['value ' ]['value ' ]);
65
63
continue ;
66
64
}
67
65
68
-
66
+ if ($ type === 'media ' ) {
67
+ $ this ->savePostMedia ($ post , $ field ['id ' ], $ field ['value ' ]['value ' ]);
68
+ continue ;
69
+ }
69
70
70
71
$ class_name = "Ushahidi\Modules\V5\Models\PostValues\Post " . ucfirst ($ type );
71
72
if (!class_exists ($ class_name ) &&
@@ -170,6 +171,22 @@ protected function savePostValues(Post $post, array $post_content, int $post_id)
170
171
return $ errors ;
171
172
}
172
173
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
+
173
190
174
191
protected function savePostTags ($ post , $ attr_id , $ tags )
175
192
{
0 commit comments