Source of file DuplicateElementMutation.php
Size: 3,560 Bytes - Last Modified: 2021-12-24T06:44:48+00:00
/var/www/docs.ssmods.com/process/src/_legacy/DuplicateElementMutation.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 | <?php namespace DNADesign\Elemental\GraphQL; use DNADesign\Elemental\Models\BaseElement; use DNADesign\Elemental\Models\ElementalArea; use DNADesign\Elemental\Services\ReorderElements; use GraphQL\Type\Definition\ResolveInfo; use GraphQL\Type\Definition\Type; use InvalidArgumentException; use Exception; use SilverStripe\Core\Injector\Injector; use SilverStripe\GraphQL\MutationCreator; use SilverStripe\GraphQL\OperationResolver; use SilverStripe\GraphQL\Scaffolding\StaticSchema; if (!class_exists(MutationCreator::class)) { return; } /** * @deprecated 4.8..5.0 Use silverstripe/graphql:^4 functionality. */ class DuplicateElementMutation extends MutationCreator implements OperationResolver { public function attributes() { return [ 'name' => 'duplicateBlock', 'description' => 'Duplicate an Element in this ElementalArea' ]; } public function type() { return $this->manager->getType(StaticSchema::inst()->typeNameForDataObject(BaseElement::class)); } public function args() { return [ 'id' => ['type' => Type::nonNull(Type::id())], ]; } public function resolve($object, array $args, $context, ResolveInfo $info) { // load element to clone $elementID = $args['id']; $element = BaseElement::get_by_id($elementID); if (!$element) { throw new InvalidArgumentException("Invalid BaseElementID: $elementID"); } // check can edit the elemental area $areaID = $element->ParentID; $area = ElementalArea::get_by_id($areaID); if (!$area) { throw new InvalidArgumentException("Invalid ParentID on BaseElement: $elementID"); } if (!$area->canEdit($context['currentUser'])) { throw new InvalidArgumentException( "The current user has insufficient permission to edit ElementalArea: $areaID" ); } if (!$element->canCreate($context['currentUser'])) { throw new InvalidArgumentException( "The current user has insufficient permission to create or duplicate BaseElement: $elementID" ); } try { // clone element $clone = $element->duplicate(false); $clone->Title = $this->newTitle($clone->Title); $clone->Sort = 0; // must be zeroed for reorder to work $area->Elements()->add($clone); // reorder $reorderer = Injector::inst()->create(ReorderElements::class, $clone); $reorderer->reorder($elementID); return $clone; } catch (Exception $e) { throw new Exception("Something went wrong when duplicating element: $elementID"); } } public function newTitle($title) { $hasCopyPattern = '/^.*(\scopy($|\s[0-9]+$))/'; $hasNumPattern = '/^.*(\s[0-9]+$)/'; $parts = []; // does $title end with 'copy' (ignoring numbers for now)? if (preg_match($hasCopyPattern, $title, $parts)) { $copy = $parts[1]; // does $title end with numbers? if (preg_match($hasNumPattern, $copy, $parts)) { $num = trim($parts[1]); $len = strlen($num); $inc = (int)$num + 1; return substr($title, 0, -$len) . "$inc"; } else { return $title . ' 2'; } } else { return $title . ' copy'; } } } |