|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ProcessWire\GraphQL\Field\Mutation; |
| 4 | + |
| 5 | +use Youshido\GraphQL\Field\AbstractField; |
| 6 | +use Youshido\GraphQL\Execution\ResolveInfo; |
| 7 | +use Youshido\GraphQL\Exception\ValidationException; |
| 8 | +use Youshido\GraphQL\Exception\ResolveException; |
| 9 | +use Youshido\GraphQL\Config\Field\FieldConfig; |
| 10 | +use Youshido\GraphQL\Field\InputField; |
| 11 | +use Youshido\GraphQL\Type\Scalar\IntType; |
| 12 | +use Youshido\GraphQL\Type\NonNullType; |
| 13 | + |
| 14 | +use ProcessWire\Template; |
| 15 | +use ProcessWire\Page; |
| 16 | +use ProcessWire\NullPage; |
| 17 | +use ProcessWire\Field; |
| 18 | +use ProcessWire\FieldtypePage; |
| 19 | + |
| 20 | +use ProcessWire\GraphQL\Type\Object\TemplatedPageType; |
| 21 | +use ProcessWire\GraphQL\Type\Input\TemplatedPage\UpdateInputType; |
| 22 | + |
| 23 | +class UpdateTemplatedPage extends AbstractField { |
| 24 | + |
| 25 | + protected $template; |
| 26 | + |
| 27 | + public function __construct(Template $template) |
| 28 | + { |
| 29 | + $this->template = $template; |
| 30 | + parent::__construct([]); |
| 31 | + } |
| 32 | + |
| 33 | + public function getName() |
| 34 | + { |
| 35 | + $typeName = ucfirst(TemplatedPageType::normalizeName($this->template->name)); |
| 36 | + return "update{$typeName}"; |
| 37 | + } |
| 38 | + |
| 39 | + public function getType() |
| 40 | + { |
| 41 | + return new TemplatedPageType($this->template); |
| 42 | + } |
| 43 | + |
| 44 | + public function getDescription() |
| 45 | + { |
| 46 | + return "Allows you to update Pages with template `{$this->template->name}`."; |
| 47 | + } |
| 48 | + |
| 49 | + public function build(FieldConfig $config) |
| 50 | + { |
| 51 | + $config->addArgument(new InputField([ |
| 52 | + 'name' => 'id', |
| 53 | + 'type' => new NonNullType(new IntType()), |
| 54 | + ])); |
| 55 | + |
| 56 | + $config->addArgument(new InputField([ |
| 57 | + 'name' => 'page', |
| 58 | + 'type' => new NonNullType(new UpdateInputType($this->template)), |
| 59 | + ])); |
| 60 | + } |
| 61 | + |
| 62 | + public function resolve($value, array $args, ResolveInfo $info) |
| 63 | + { |
| 64 | + // prepare neccessary variables |
| 65 | + $pages = \ProcessWire\wire('pages'); |
| 66 | + $sanitizer = \ProcessWire\wire('sanitizer'); |
| 67 | + $fields = \ProcessWire\wire('fields'); |
| 68 | + $values = (array) $args['page']; |
| 69 | + $id = (integer) $args['id']; |
| 70 | + $p = $pages->get($id); |
| 71 | + $p->of(false); |
| 72 | + |
| 73 | + /*********************************************\ |
| 74 | + * * |
| 75 | + * Don't ever take sides against the family! * |
| 76 | + * * |
| 77 | + \*********************************************/ |
| 78 | + if (isset($values['parent'])) { |
| 79 | + |
| 80 | + // find the parent |
| 81 | + $parentSelector = $values['parent']; |
| 82 | + $parent = $pages->find($sanitizer->selectorValue($parentSelector))->first(); |
| 83 | + |
| 84 | + // if no parent then no good. No child should born without a parent! |
| 85 | + if (!$parent || $parent instanceof NullPage) throw new ValidationException("Could not find the `parent` page with `$parentSelector`."); |
| 86 | + |
| 87 | + // make sure user is allowed to add children to this parent |
| 88 | + $legalAddTemplates = Utils::moduleConfig()->legalAddTemplates; |
| 89 | + if (!$legalAddTemplates->has($parent->template)) throw new ValidationException("You are not allowed to add children to the parent: '$parentSelector'."); |
| 90 | + |
| 91 | + // make sure parent is allowed as a parent for this page |
| 92 | + $parentTemplates = $this->template->parentTemplates; |
| 93 | + if (count($parentTemplates) && !in_array($parent->template->id, $parentTemplates)) throw new ValidationException("`parent` is not allowed as a parent."); |
| 94 | + |
| 95 | + // make sure parent is allowed to have children |
| 96 | + if ($parent->template->noChildren === 1) throw new ValidationException("`parent` is not allowed to have children."); |
| 97 | + |
| 98 | + // make sure the page is allowed as a child for parent |
| 99 | + $childTemplates = $parent->template->childTemplates; |
| 100 | + if (count($childTemplates) && !in_array($this->template->id, $childTemplates)) throw new ValidationException("not allowed to be a child for `parent`."); |
| 101 | + } |
| 102 | + |
| 103 | + if (isset($values['name'])) { |
| 104 | + |
| 105 | + // check if the name is valid |
| 106 | + $name = $sanitizer->pageName($values['name']); |
| 107 | + if (!$name) throw new ValidationException('value for `name` field is invalid,'); |
| 108 | + |
| 109 | + // find out if the name is taken |
| 110 | + $taken = $pages->find("parent=$parent, name=$name")->count(); |
| 111 | + if ($taken) throw new ValidationException('`name` is already taken.'); |
| 112 | + } |
| 113 | + |
| 114 | + // update the values from client |
| 115 | + foreach ($values as $fieldName => $value) { |
| 116 | + $field = $fields->get($fieldName); |
| 117 | + if (!$field instanceof Field) continue; |
| 118 | + if ($field->type->className() === 'FieldtypePage') $value = implode('|', $value); |
| 119 | + $p->$fieldName = $value; |
| 120 | + } |
| 121 | + |
| 122 | + // save the page to db |
| 123 | + if ($p->save()) return $pages->get("$p"); |
| 124 | + |
| 125 | + // If we did not return till now then no good! |
| 126 | + throw new ResolveException("Could not create page `$name` with template `{$this->template->name}`"); |
| 127 | + } |
| 128 | + |
| 129 | +} |
0 commit comments