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