-
Notifications
You must be signed in to change notification settings - Fork 612
/
Category.php
105 lines (87 loc) · 3.31 KB
/
Category.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\gql\resolvers\mutations;
use Craft;
use craft\db\Table;
use craft\elements\Category as CategoryElement;
use craft\gql\base\ElementMutationResolver;
use craft\gql\base\StructureMutationTrait;
use craft\helpers\Db;
use craft\models\CategoryGroup;
use GraphQL\Error\Error;
use GraphQL\Type\Definition\ResolveInfo;
use Throwable;
/**
* Class Category
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.5.0
*/
class Category extends ElementMutationResolver
{
use StructureMutationTrait;
/** @inheritdoc */
protected array $immutableAttributes = ['id', 'uid', 'groupId'];
/**
* Save a category using the passed arguments.
*
* @param mixed $source
* @param array $arguments
* @param mixed $context
* @param ResolveInfo $resolveInfo
* @return CategoryElement
* @throws Throwable if reasons.
*/
public function saveCategory(mixed $source, array $arguments, mixed $context, ResolveInfo $resolveInfo): CategoryElement
{
/** @var CategoryGroup $categoryGroup */
$categoryGroup = $this->getResolutionData('categoryGroup');
$canIdentify = !empty($arguments['id']) || !empty($arguments['uid']);
$elementService = Craft::$app->getElements();
if ($canIdentify) {
if (!empty($arguments['uid'])) {
$category = $elementService->createElementQuery(CategoryElement::class)->uid($arguments['uid'])->one();
} else {
$category = $elementService->getElementById($arguments['id'], CategoryElement::class);
}
if (!$category) {
throw new Error('No such category exists');
}
} else {
$category = $elementService->createElement(['type' => CategoryElement::class, 'groupId' => $categoryGroup->id]);
}
if ($category->groupId != $categoryGroup->id) {
throw new Error('Impossible to change the group of an existing category');
}
$this->requireSchemaAction('categorygroups.' . $categoryGroup->uid, 'save');
$category = $this->populateElementWithData($category, $arguments, $resolveInfo);
$category = $this->saveElement($category);
$this->performStructureOperations($category, $arguments);
return $elementService->getElementById($category->id, CategoryElement::class);
}
/**
* Delete a category identified by the arguments.
*
* @param mixed $source
* @param array $arguments
* @param mixed $context
* @param ResolveInfo $resolveInfo
* @throws Throwable if reasons.
*/
public function deleteCategory(mixed $source, array $arguments, mixed $context, ResolveInfo $resolveInfo): void
{
$categoryId = $arguments['id'];
$elementService = Craft::$app->getElements();
$category = $elementService->getElementById($categoryId, CategoryElement::class);
if (!$category) {
return;
}
$categoryGroupUid = Db::uidById(Table::CATEGORYGROUPS, $category->groupId);
$this->requireSchemaAction('categorygroups.' . $categoryGroupUid, 'delete');
$elementService->deleteElementById($categoryId);
}
}