Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 2024.45 #5012

Merged
merged 6 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

use Phinx\Migration\AbstractMigration;

class CreatePhoneFieldTable extends AbstractMigration
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* https://book.cakephp.org/phinx/0/en/migrations.html
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* addCustomColumn
* renameColumn
* addIndex
* addForeignKey
*
* Any other destructive changes will result in an error when trying to
* rollback the migration.
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
$this->table('post_phone')
->addColumn('post_id', 'integer')
->addColumn('form_attribute_id', 'integer')
->addColumn('value', 'string', [
'limit' => 32,
])
->addColumn('created', 'integer', ['default' => 0])
->addForeignKey('post_id', 'posts', 'id', [
'delete' => 'CASCADE',
'update' => 'CASCADE',
])
->create();
}
}
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 @@ -33,6 +33,14 @@ protected function isSupported(Query $query)
public function __invoke($query) //: array
{
$this->isSupported($query);
return $this->collection_repository->findById($query->getId());
return $this->collection_repository->findById(
$query->getId(),
false,
array_unique(array_merge(
$query->getFields(),
$query->getFieldsForRelationship()
)),
$query->getWithRelationship()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,41 +46,13 @@ protected function isSupported(Query $query)
*/
public function __invoke($query) //: LengthAwarePaginator
{
// TODO: This is redundant, we should be able to remove this
$this->isSupported($query);

$search_fields = $query->getSearchData();

$search = new SearchData();

// TODO: Move this to the Query class
$user = Auth::guard()->user();

$search->setFilter('is_saved_search', false);
$search->setFilter('with_post_count', true);

// Querying Values
$search->setFilter('keyword', $search_fields->q());
$search->setFilter('role', $search_fields->role());
$search->setFilter('is_admin', $search_fields->role() === "admin");
$search->setFilter('user_id', $user->id ?? null);

// Paging Values
$limit = $query->getLimit();

$search->setFilter('limit', $limit);
$search->setFilter('skip', $limit * ($query->getPage() - 1));

// Sorting Values
$search->setFilter('sort', $query->getSortBy());
$search->setFilter('order', $query->getOrder());

$this->collection_repository->setSearchParams($search);

// TODO: We shouldn't let the repository return a Laravel paginator instance,
// this should be created in the controller
$result = $this->collection_repository->fetch();

return $result;
$only_fields = array_unique(array_merge($query->getFields(), $query->getFieldsForRelationship()));
return $this->collection_repository->paginate(
$query->getPaging(),
$query->getSearchFields(),
$only_fields,
$query->getWithRelationship()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
namespace Ushahidi\Modules\V5\Actions\Collection\Queries;

use App\Bus\Query\Query;
use Ushahidi\Modules\V5\Traits\OnlyParameter\QueryWithOnlyParameter;
use Illuminate\Http\Request;
use Ushahidi\Modules\V5\Models\Set as CollectionModel;

class FetchCollectionByIdQuery implements Query
{

use QueryWithOnlyParameter;

/**
* int
Expand All @@ -15,10 +18,24 @@ class FetchCollectionByIdQuery implements Query

public function __construct(int $id = 0)
{

$this->id = $id;
}

public static function fromRequest(int $id, Request $request): self
{
if ($id <= 0) {
throw new \InvalidArgumentException('Id must be a positive number');
}
$query = new self($id);
$query->addOnlyParameteresFromRequest(
$request,
CollectionModel::COLLECTION_ALLOWED_FIELDS,
CollectionModel::ALLOWED_RELATIONSHIPS,
CollectionModel::REQUIRED_FIELDS
);
return $query;
}

public function getId(): int
{
return $this->id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,33 @@

use App\Bus\Query\Query;
use Ushahidi\Modules\V5\DTO\CollectionSearchFields;
use Ushahidi\Modules\V5\Traits\OnlyParameter\QueryWithOnlyParameter;
use Ushahidi\Modules\V5\Traits\HasPaginate;
use Ushahidi\Modules\V5\Traits\HasSearchFields;
use Illuminate\Http\Request;
use Ushahidi\Modules\V5\Models\Set as CollectionModel;

class FetchCollectionQuery implements Query
{
use QueryWithOnlyParameter;
use HasPaginate;
use HasSearchFields;

const DEFAULT_LIMIT = 0;
const DEFAULT_ORDER = "DESC";
const DEFAULT_SORT_BY = "featured";

private $limit;
private $page;
private $sortBy;
private $order;
private $search_data;

public function __construct(
int $limit,
int $page,
string $sortBy,
string $order,
CollectionSearchFields $search_data
) {
$this->limit = $limit;
$this->page = $page;
$this->sortBy = $sortBy;
$this->order = $order;
$this->search_data = $search_data;
}

public function getLimit(): int
{
return $this->limit > 0 ? $this->limit : config('paging.default_laravel_pageing_limit');
}

public function getPage(): int
{
return $this->page;
}

public function getSortBy(): string
{
return $this->sortBy;
}

public function getOrder(): string
{
return $this->order;
}

public function getSearchData()
public static function fromRequest(Request $request): self
{
return $this->search_data;
$query = new self();
$query->setPaging($request, self::DEFAULT_SORT_BY, self::DEFAULT_ORDER, self::DEFAULT_LIMIT);
$query->setSearchFields(new CollectionSearchFields($request));
$query->addOnlyParameteresFromRequest(
$request,
CollectionModel::COLLECTION_ALLOWED_FIELDS,
CollectionModel::ALLOWED_RELATIONSHIPS,
CollectionModel::REQUIRED_FIELDS
);
return $query;
}
}
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
15 changes: 15 additions & 0 deletions src/Ushahidi/Modules/V5/Actions/Post/HandlePostOnlyParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ public function addHydrateRelationships(Post $post, array $hydrates)
];
$relations['enabled_languages'] = true;
break;
case 'post_media':
$post->post_media = $post->valuesPostMedia->first();
if ($post->post_media) {
$media = $post->post_media->toArray();
unset($media['post']);
// convert it to field structure
$media_field = $media['attribute'];
unset($media['attribute']);
unset($media['translations']);
$media_field['value'] = $media;
$post->post_media = $media_field;
}
break;
}
}
return $post;
Expand Down Expand Up @@ -113,6 +126,8 @@ public function hideUnwantedRelationships(Post $post, array $hydrates)
$post->makeHidden('valuesPostsMedia');
$post->makeHidden('valuesPostsSet');
$post->makeHidden('valuesPostTag');
$post->makeHidden('valuesPostMedia');


// hide source relationships
if (!in_array('message', $hydrates)) {
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,21 @@ protected function isSupported(Query $query)
);
}


/**
* @param FetchSavedSearchByIdQuery $query
* @return array
*/
public function __invoke($query) //: array
{
$this->isSupported($query);
return $this->saved_search_repository->findById($query->getId(), 1);
return $this->saved_search_repository->findById(
$query->getId(),
1,
array_unique(array_merge(
$query->getFields(),
$query->getFieldsForRelationship()
)),
$query->getWithRelationship()
);
}
}
Loading
Loading