Skip to content

Commit

Permalink
Merge pull request pimcore#394 from skoch98/add-update-user-and-versi…
Browse files Browse the repository at this point in the history
…on-suppression

Add userId and omitVersionCreate to data-object mutation-type
  • Loading branch information
weisswurstkanone authored Jul 28, 2021
2 parents 6438370 + 14a858e commit 5753773
Showing 1 changed file with 74 additions and 11 deletions.
85 changes: 74 additions & 11 deletions src/GraphQL/Mutation/MutationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
use Pimcore\Model\DataObject\ClassDefinition;
use Pimcore\Model\DataObject\Concrete;
use Pimcore\Model\Document;
use Pimcore\Model\Element\AbstractElement;
use Pimcore\Model\Factory;
use Pimcore\Model\Version;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class MutationType extends ObjectType
Expand Down Expand Up @@ -195,12 +197,15 @@ public function buildUpdateDocumentMutation(&$config, $context, $mutationType, $
'key' => ['type' => Type::nonNull(Type::string())],
'path' => ['type' => Type::string()],
'parentId' => ['type' => Type::int()],
'published' => ['type' => Type::boolean(), 'description' => 'Default is true!']
'published' => ['type' => Type::boolean(), 'description' => 'Default is true!'],
'userId' => ['type' => Type::int()]
];
} else {
$args = [
'id' => ['type' => Type::int()],
'fullpath' => ['type' => Type::string()]
'fullpath' => ['type' => Type::string()],
'omitVersionCreate' => ['type' => Type::boolean()],
'userId' => ['type' => Type::int()]
];
}

Expand Down Expand Up @@ -265,7 +270,7 @@ public function buildUpdateDocumentMutation(&$config, $context, $mutationType, $

$tags = [];
if (isset($args['input'])) {
self::{$inputProcessorFn}($value, $args, $context, $info, $element, $processors);
$me->{$inputProcessorFn}($value, $args, $context, $info, $element, $processors);
if (isset($args['input']['tags']) && ($tag_input = $args['input']['tags'])) {
$tags = $me->getTagsFromInput($tag_input);
if (false === $tags) {
Expand All @@ -276,7 +281,8 @@ public function buildUpdateDocumentMutation(&$config, $context, $mutationType, $
}
}
}
$element->save();

$me->saveElement($element, $args);

if ($tags) {
$me->setTags('document', $element->getId(), $tags);
Expand Down Expand Up @@ -368,6 +374,7 @@ public function getDocumentLinkMutationInputType($context, &$processors = [])
public static function processDocumentLinkMutationInput($value, $args, $context, ResolveInfo $info, $element, $processors)
{
$inputValues = $args['input'];

foreach ($inputValues as $key => $value) {
if ($key == 'object') {
Logger::debug('test');
Expand All @@ -377,6 +384,9 @@ public static function processDocumentLinkMutationInput($value, $args, $context,
$element->setObject($target);
} elseif ($key == 'tags') {
//skip it to process in callee method
} elseif ($key == 'href') {
$element->setDirect($value);
$element->setLinktype('direct');
} else {
$setter = 'set' . ucfirst($key);

Expand Down Expand Up @@ -559,6 +569,7 @@ public function buildDataObjectMutations(&$config = [], $context = [])
'parentId' => ['type' => Type::int()],
'published' => ['type' => Type::boolean(), 'description' => 'Default is true!'],
'omitMandatoryCheck' => ['type' => Type::boolean()],
'userId' => ['type' => Type::int()],
'type' => ['type' => Type::string()],
'input' => $inputType,
], 'resolve' => static function ($value, $args, $context, ResolveInfo $info) use ($entity, $modelFactory, $processors, $localeService, $me) {
Expand Down Expand Up @@ -632,7 +643,7 @@ public function buildDataObjectMutations(&$config = [], $context = [])
}
}

$newInstance->save();
$me->saveElement($newInstance, $args);

if ($tags) {
$me->setTags('object', $newInstance->getId(), $tags);
Expand Down Expand Up @@ -690,6 +701,8 @@ public function buildDataObjectMutations(&$config = [], $context = [])
'fullpath' => ['type' => Type::string()],
'defaultLanguage' => ['type' => Type::string()],
'omitMandatoryCheck' => ['type' => Type::boolean()],
'omitVersionCreate' => ['type' => Type::boolean()],
'userId' => ['type' => Type::int()],
'input' => ['type' => $inputType],
], 'resolve' => $this->getUpdateObjectResolver($processors, $localeService, null, $this->omitPermissionCheck)
];
Expand Down Expand Up @@ -840,7 +853,7 @@ public function getUpdateObjectResolver($processors, $localeService, $object = n
}
}

$object->save();
$me->saveElement($object, $args);

if ($tags) {
$me->setTags('object', $object->getId(), $tags);
Expand Down Expand Up @@ -906,6 +919,7 @@ public function buildCreateAssetMutation(&$config, $context)
'path' => ['type' => Type::string()],
'parentId' => ['type' => Type::int()],
'type' => ['type' => Type::nonNull(Type::string()), 'description' => 'image or whatever'],
'userId' => ['type' => Type::int()],
'input' => $this->getGraphQlService()->getAssetTypeDefinition('asset_input'),
], 'resolve' => static function ($value, $args, $context, ResolveInfo $info) use ($omitPermissionCheck, $me) {
$parent = null;
Expand Down Expand Up @@ -960,7 +974,8 @@ public function buildCreateAssetMutation(&$config, $context)
}
}
}
$newInstance->save();

$me->saveElement($newInstance, $args);

if ($tags) {
$me->setTags('asset', $newInstance->getId(), $tags);
Expand Down Expand Up @@ -1023,7 +1038,9 @@ public function buildUpdateAssetMutation(&$config, $context)
'args' => [
'id' => ['type' => Type::int()],
'fullpath' => ['type' => Type::string()],
'input' => $this->getGraphQlService()->getAssetTypeDefinition('asset_input'),
'omitVersionCreate' => ['type' => Type::boolean()],
'userId' => ['type' => Type::int()],
'input' => $this->getGraphQlService()->getAssetTypeDefinition('asset_input')
], 'resolve' => static function ($value, $args, $context, ResolveInfo $info) use ($me) {
$element = $me->getElementByTypeAndIdOrPath($args, 'asset');

Expand All @@ -1049,7 +1066,8 @@ public function buildUpdateAssetMutation(&$config, $context)
}
}
}
$element->save();

$me->saveElement($element, $args);

if ($tags) {
$me->setTags('asset', $element->getId(), $tags);
Expand Down Expand Up @@ -1091,7 +1109,8 @@ public function buildCreateFolderMutation($type, &$config, $context)

$args = [
'path' => ['type' => Type::string()],
'parentId' => ['type' => Type::int()]
'parentId' => ['type' => Type::int()],
'userId' => ['type' => Type::int()]
];

if ($type === 'asset') {
Expand Down Expand Up @@ -1170,6 +1189,11 @@ public function getCreateFolderResolver($elementType)

$newInstance->setParentId($parent->getId());

if (isset($args['userId'])) {
$newInstance->setUserOwner($args['userId']);
$newInstance->setUserModification($args['userId']);
}

$newInstance->save();

return [
Expand Down Expand Up @@ -1224,7 +1248,8 @@ public function buildUpdateFolderMutation($type, &$config, $context)
'args' => [
'id' => ['type' => Type::int()],
'fullpath' => ['type' => Type::string()],
'input' => ['type' => $inputType],
'userId' => ['type' => Type::int()],
'input' => ['type' => $inputType]
], 'resolve' => static function ($value, $args, $context, ResolveInfo $info) use ($type, $omitPermissionCheck, $me) {
try {
/** @var $configuration Configuration */
Expand All @@ -1245,6 +1270,10 @@ public function buildUpdateFolderMutation($type, &$config, $context)
$element->$setter($argValue);
}

if (isset($args['userId'])) {
$element->setUserModification($args['userId']);
}

$element->save();
} catch (\Exception $e) {
return [
Expand Down Expand Up @@ -1465,4 +1494,38 @@ public function isEmpty()
{
return !$this->config['fields'];
}

/**
* @param AbstractElement|Asset|DataObject|Document
* @param array $options
*/
protected function saveElement($element, $options): void
{
if (
isset($options['userId'])
&& empty($element->getId())
&& method_exists($element, 'setUserOwner')
) {
$element->setUserOwner($options['userId']);
}

if (
isset($options['userId'])
&& method_exists($element, 'setUserModification')
) {
$element->setUserModification($options['userId']);
}

$omitVersionCreateBefore = Version::$disabled;

if (isset($options['omitVersionCreate']) && $options['omitVersionCreate']) {
Version::disable();
}

$element->save();

if (isset($options['omitVersionCreate']) && $options['omitVersionCreate'] && !$omitVersionCreateBefore) {
Version::enable();
}
}
}

0 comments on commit 5753773

Please sign in to comment.