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

Re-freeze for 2024.48 #5018

Merged
merged 7 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix category translations
  • Loading branch information
Mh-Asmi committed Nov 27, 2024
commit 4b6f005fb3a9d88c738a264430c3ff7cd564db43
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ class StoreCategoryCommand implements Command
* @var array
*/
private $availableLanguages;

/**
* @var array
*/
private $translations;

public function __construct(
?int $parentId,
Expand All @@ -76,7 +81,8 @@ public function __construct(
int $priority,
?array $role,
?string $defaultLanguage = 'en',
array $availableanguages = []
array $availableanguages = [],
array $translations
) {
$this->parentId = $parentId;
$this->tag = $tag;
Expand All @@ -89,6 +95,7 @@ public function __construct(
$this->role = $role;
$this->defaultLanguage = $defaultLanguage;
$this->availableLanguages = $availableanguages;
$this->translations = $translations;
}

public static function createFromRequest(CategoryRequest $request): self
Expand All @@ -109,7 +116,8 @@ public static function createFromRequest(CategoryRequest $request): self
(int) $request->input('priority'),
$request->input('role'),
self::DEFAULT_LANUGAGE,
[]
[],
$request->input('translations')??[]
);
}

Expand Down Expand Up @@ -194,4 +202,11 @@ public function getAvailableLanguages(): array
{
return $this->availableLanguages;
}
/**
* @return array
*/
public function getTranslations(): array
{
return $this->translations;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ class UpdateCategoryCommand implements Command
* @var ?array
*/
private $availableLanguages;

/**
* @var array
*/
private $translations;

public function __construct(
int $categoryId,
Expand All @@ -81,7 +86,8 @@ public function __construct(
?int $priority,
?array $role,
?string $defaultLanguage,
?array $availableLanguages
?array $availableLanguages = [],
array $translations
) {
$this->categoryId = $categoryId;
$this->parentId = $parentId;
Expand All @@ -95,6 +101,7 @@ public function __construct(
$this->role = $role;
$this->defaultLanguage = $defaultLanguage;
$this->availableLanguages = $availableLanguages;
$this->translations = $translations;
}

public static function fromRequest(int $id, CategoryRequest $request, Category $current_category): self
Expand All @@ -111,7 +118,8 @@ public static function fromRequest(int $id, CategoryRequest $request, Category $
$request->has('priority')?$request->input('priority'):$current_category->priority,
$request->has('role')?$request->input('role'):$current_category->role,
self::DEFAULT_LANUGAGE,
[]
[],
$request->input('translations')??[]
);
}

Expand Down Expand Up @@ -174,4 +182,12 @@ public function getAvailableLanguages(): ?array
{
return $this->availableLanguages;
}

/**
* @return array
*/
public function getTranslations(): array
{
return $this->translations;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
namespace Ushahidi\Modules\V5\Actions\Category\Handlers;

use App\Bus\Action;
use App\Bus\Command\AbstractCommandHandler;
use App\Bus\Command\Command;
use Ushahidi\Modules\V5\Actions\Category\Commands\StoreCategoryCommand;
use Ushahidi\Modules\V5\Models\Category;
use Ushahidi\Modules\V5\Repository\Category\CategoryRepository;
use Illuminate\Support\Facades\Auth;
use Ushahidi\Modules\V5\Actions\V5CommandHandler;

class StoreCategoryCommandHandler extends AbstractCommandHandler
class StoreCategoryCommandHandler extends V5CommandHandler
{
private $categoryRepository;

Expand Down Expand Up @@ -46,19 +46,29 @@ public function __invoke(Action $action): int

$user_id = Auth::guard()->user()->id ?? null;

return $this->categoryRepository->store(
$parentId,
$user_id,
ucfirst($action->getTag()),
$slug,
$action->getType(),
$action->getDescription(),
$action->getColor(),
$action->getIcon(),
$action->getPriority(),
$action->getRole(),
$action->getDefaultLanguage(),
$action->getAvailableLanguages()
$category = $this->categoryRepository->store(
$parentId,
$user_id,
ucfirst($action->getTag()),
$slug,
$action->getType(),
$action->getDescription(),
$action->getColor(),
$action->getIcon(),
$action->getPriority(),
$action->getRole(),
$action->getDefaultLanguage(),
$action->getAvailableLanguages()
);

$this->saveTranslations(
$category,
$category->toArray(),
$action->getTranslations(),
$category->id,
'category'
);

return $category->id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
namespace Ushahidi\Modules\V5\Actions\Category\Handlers;

use App\Bus\Action;
use App\Bus\Command\AbstractCommandHandler;
use App\Bus\Command\Command;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Ushahidi\Modules\V5\Models\Category;
use Ushahidi\Modules\V5\Repository\Category\CategoryRepository;
use Ushahidi\Modules\V5\Actions\Category\Commands\UpdateCategoryCommand;
use Illuminate\Support\Facades\Auth;
use Ushahidi\Modules\V5\Actions\V5CommandHandler;

class UpdateCategoryCommandHandler extends AbstractCommandHandler
class UpdateCategoryCommandHandler extends V5CommandHandler
{
private $categoryRepository;

Expand Down Expand Up @@ -49,8 +48,15 @@ public function __invoke(Action $action): Category
$action->getDefaultLanguage(),
$action->getAvailableLanguages()
);

return $this->categoryRepository
$category = $this->categoryRepository
->findById($action->getCategoryId());
$this->updateTranslations(
$category,
$category->toArray(),
$action->getTranslations(),
$category->id,
'category'
);
return $category;
}
}
43 changes: 2 additions & 41 deletions src/Ushahidi/Modules/V5/Http/Controllers/CategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,25 +88,6 @@ public function store(CategoryRequest $request)

$category = $this->getCategory($id, Category::ALLOWED_FIELDS);

DB::beginTransaction();
try {
$errors = $this->saveTranslations(
$category,
$category->toArray(),
$request->input('translations', []),
$category->id,
'category'
);
if (!empty($errors)) {
DB::rollback();
return self::make422($errors, 'translation');
}
DB::commit();
} catch (\Exception $e) {
DB::rollback();
return self::make500($e->getMessage());
}

return new CategoryResource($category);
}

Expand All @@ -123,28 +104,8 @@ public function update(int $id, CategoryRequest $request)
$category = $this->getCategory($id, Category::ALLOWED_FIELDS, []);

$this->authorize('update', $category);
DB::beginTransaction();
try {
$command = UpdateCategoryCommand::fromRequest($id, $request, $category);
$category = $this->commandBus->handle($command);
$errors = $this->updateTranslations(
new Category(),
$category->toArray(),
$request->input('translations') ?? [],
$category->id,
'category'
);
if (!empty($errors)) {
DB::rollback();
// To do : change the return results to Exception
return self::make422($errors, 'translation');
}
DB::commit();
return new CategoryResource($category);
} catch (\Exception $e) {
DB::rollback();
throw $e;
}
$this->commandBus->handle(UpdateCategoryCommand::fromRequest($id, $request, $category));
return new CategoryResource($this->getCategory($id, Category::ALLOWED_FIELDS));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Ushahidi/Modules/V5/Http/Requests/CategoryRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function rules($data = [])
'role' => [
function ($attribute, $value, $fail) use ($parent) {
// ... and check if the role matches its parent
if ($parent && $parent->getAttribute('role') != $value) {
if ($parent && $parent['role'] != $value) {
return $fail(trans('validation.child_parent_role_match'));
}
if (is_array($value) && empty($value)) {
Expand Down
4 changes: 4 additions & 0 deletions src/Ushahidi/Modules/V5/Http/Resources/CategoryResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public function toArray($request)
}
if (isset($data['translations'])) {
$data['translations'] = (new TranslationCollection($this->translations))->toArray(null);
$data['enabled_languages'] = [
'default'=> $this->base_language,
'available' => $this->translations->groupBy('language')->keys()
];
}
$data['allowed_privileges']= $this->getResourcePrivileges();
return $data;
Expand Down
4 changes: 2 additions & 2 deletions src/Ushahidi/Modules/V5/Models/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Category extends BaseModel
* @var array
*/
protected $hidden = [
'description',
// 'description',
];

/**
Expand Down Expand Up @@ -114,7 +114,7 @@ public function parent()

public function children()
{
return $this->hasMany(Category::class, 'parent_id', 'id')->withoutGlobalScopes();
return $this->hasMany(Category::class, 'parent_id', 'id')->withoutGlobalScopes()->with('translations');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Ushahidi\Modules\V5\Repository\Category;

use Illuminate\Support\Collection;
use Ushahidi\Modules\V5\Models\Category;
use Ushahidi\Modules\V5\DTO\Paging;
use Ushahidi\Modules\V5\DTO\CategorySearchFields;
Expand Down Expand Up @@ -53,7 +52,7 @@ public function store(
?array $role,
string $defaultBaseLanguage,
array $availableLanguages
): int;
): Category;
public function slugExists(string $slug): bool;
public function update(
int $id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ public function findById(int $id, array $fields = [], array $with = []): Categor
$query->with($with);
}
$category = $query->first();
dd($category);

if (!$category instanceof Category) {
throw new NotFoundException('Category not found');
}
Expand Down Expand Up @@ -172,7 +170,7 @@ public function store(
?array $role,
string $defaultBaseLanguage,
array $availableLanguages
): int {
): Category {
$input = array_filter([
'parent_id' => $parentId,
'user_id' => $userId,
Expand All @@ -192,7 +190,7 @@ public function store(

$isSaved = $category->saveOrFail();

return $category->id;
return $category;
}

public function slugExists(string $slug): bool
Expand Down