|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Ushahidi\Modules\V5\Actions\Post; |
| 4 | + |
| 5 | +use Ushahidi\Modules\V5\Models\Post\Post; |
| 6 | +use Ushahidi\Contracts\Sources; |
| 7 | +use Ushahidi\Modules\V5\Http\Resources\LockCollection; |
| 8 | +use Ushahidi\Modules\V5\Models\Contact; |
| 9 | +use Illuminate\Support\Collection; |
| 10 | +use Ushahidi\Modules\V5\Http\Resources\PostValueCollection; |
| 11 | + |
| 12 | +trait HandlePostOnlyParameters |
| 13 | +{ |
| 14 | + public function addHydrateRelationships(Post $post, array $hydrates) |
| 15 | + { |
| 16 | + foreach ($hydrates as $hydrate) { |
| 17 | + switch ($hydrate) { |
| 18 | + case 'color': |
| 19 | + $post->color = $post->survey ? $post->survey->color : null; |
| 20 | + break; |
| 21 | + case 'categories': |
| 22 | + // $result['categories'] = $post->categories; |
| 23 | + break; |
| 24 | + case 'completed_stages': |
| 25 | + $post->completed_stages = $post->postStages; |
| 26 | + |
| 27 | + break; |
| 28 | + case 'post_content': |
| 29 | + $post->post_content = $this->getResourcePostContent($post); |
| 30 | + break; |
| 31 | + case 'translations': |
| 32 | + break; |
| 33 | + case 'contact': |
| 34 | + $post->contact = null; |
| 35 | + if ($post->source === Sources::WHATSAPP) { |
| 36 | + if ($this->userHasManagePostPermissions()) { |
| 37 | + if (isset($post->metadata['contact'])) { |
| 38 | + $post->contact = (new Contact)->fill($post->metadata['contact']); |
| 39 | + } |
| 40 | + } else { |
| 41 | + if (isset($post->metadata['contact']['id'])) { |
| 42 | + $post->contact = (new Contact)->fill(['id'=>$post->metadata['contact']['id']]); |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + if ($post->message) { |
| 47 | + if ($this->userHasManagePostPermissions()) { |
| 48 | + $post->contact = $post->message->contact; |
| 49 | + } else { |
| 50 | + $post->contact = $post->message->contact->setVisible(["id"]); |
| 51 | + } |
| 52 | + } |
| 53 | + break; |
| 54 | + case 'locks': |
| 55 | + $post->locks = new LockCollection($post->locks); |
| 56 | + break; |
| 57 | + |
| 58 | + case 'source': |
| 59 | + $message = $post->message; |
| 60 | + $post->source = $post->source ?? ($message && isset($message->type) |
| 61 | + ? $message->type |
| 62 | + : Post::DEFAULT_SOURCE_TYPE); |
| 63 | + |
| 64 | + break; |
| 65 | + |
| 66 | + case 'data_source_message_id': |
| 67 | + $post->data_source_message_id = null; |
| 68 | + $message = $post->message; |
| 69 | + if ($message) { |
| 70 | + $post->data_source_message_id = $message->data_source_message_id ?? null; |
| 71 | + } |
| 72 | + break; |
| 73 | + case 'message': |
| 74 | + if ($post->message && !$this->userHasManagePostPermissions()) { |
| 75 | + $post->message->makeHidden("contact"); |
| 76 | + } |
| 77 | + break; |
| 78 | + case 'enabled_languages': |
| 79 | + $post->enabled_languages = [ |
| 80 | + 'default' => $post->base_language, |
| 81 | + 'available' => $post->translations->groupBy('language')->keys() |
| 82 | + ]; |
| 83 | + $relations['enabled_languages'] = true; |
| 84 | + break; |
| 85 | + } |
| 86 | + } |
| 87 | + return $post; |
| 88 | + } |
| 89 | + public function hideFieldsUsedByRelationships(Post $post, array $fields = []) |
| 90 | + { |
| 91 | + foreach ($fields as $field) { |
| 92 | + $post->offsetUnset($field); |
| 93 | + } |
| 94 | + $post->offsetUnset('values_int'); |
| 95 | + |
| 96 | + return $post; |
| 97 | + } |
| 98 | + public function hideUnwantedRelationships(Post $post, array $hydrates) |
| 99 | + { |
| 100 | + // hide post_content relationships |
| 101 | + $post->makeHidden('survey'); |
| 102 | + $post->makeHidden('valuesVarchar'); |
| 103 | + $post->makeHidden('valuesInt'); |
| 104 | + $post->makeHidden('valuesText'); |
| 105 | + $post->makeHidden('valuesDatetime'); |
| 106 | + $post->makeHidden('valuesDecimal'); |
| 107 | + $post->makeHidden('valuesDecimal'); |
| 108 | + $post->makeHidden('valuesGeometry'); |
| 109 | + $post->makeHidden('valuesMarkdown'); |
| 110 | + $post->makeHidden('valuesMedia'); |
| 111 | + $post->makeHidden('valuesPoint'); |
| 112 | + $post->makeHidden('valuesRelation'); |
| 113 | + $post->makeHidden('valuesPostsMedia'); |
| 114 | + $post->makeHidden('valuesPostsSet'); |
| 115 | + $post->makeHidden('valuesPostTag'); |
| 116 | + |
| 117 | + // hide source relationships |
| 118 | + if (!in_array('message', $hydrates)) { |
| 119 | + $post->makeHidden('message'); |
| 120 | + } |
| 121 | + |
| 122 | + // hide completed_stages relationships |
| 123 | + $post->makeHidden('postStages'); |
| 124 | + |
| 125 | + |
| 126 | + return $post; |
| 127 | + } |
| 128 | + |
| 129 | + private function getResourcePostContent($post) |
| 130 | + { |
| 131 | + $values = $post->getPostValues(); // Calling method on Post Model |
| 132 | + $no_values = $values->count() === 0 ? true : false; |
| 133 | + $col = new Collection([ |
| 134 | + 'values' => $values, |
| 135 | + 'tasks' => $post->survey ? $post->survey->tasks : [] |
| 136 | + ]); |
| 137 | + if ($post->survey) { |
| 138 | + $post_content = new PostValueCollection($col); |
| 139 | + } else { |
| 140 | + $post_content = Collection::make([]); |
| 141 | + } |
| 142 | + return $post_content; |
| 143 | + } |
| 144 | + |
| 145 | + public function handleSourceField($post) |
| 146 | + { |
| 147 | + if (!$post->source) { |
| 148 | + $message = $post->message; |
| 149 | + $post->source = ($message && isset($message->type) |
| 150 | + ? $message->type |
| 151 | + : Post::DEFAULT_SOURCE_TYPE); |
| 152 | + } |
| 153 | + return $post; |
| 154 | + } |
| 155 | +} |
0 commit comments