|
9 | 9 |
|
10 | 10 | class ProcessGraphQLConfig extends Moduleconfig {
|
11 | 11 |
|
| 12 | + const generatorName = 'graphql_generate_pages'; |
| 13 | + |
12 | 14 | public function getDefaults()
|
13 | 15 | {
|
14 | 16 | return array(
|
@@ -120,6 +122,74 @@ public static function isLegalTemplateName($name)
|
120 | 122 | return true;
|
121 | 123 | }
|
122 | 124 |
|
| 125 | + public function generatePages() |
| 126 | + { |
| 127 | + |
| 128 | + $pageNames = [ |
| 129 | + 'graphql' => 'GraphQL', |
| 130 | + 'graphiql' => 'GraphiQL', |
| 131 | + ]; |
| 132 | + |
| 133 | + // copy the template files to site/templates folder |
| 134 | + try { |
| 135 | + $from = $this->config->paths->ProcessGraphQL . 'templates/'; |
| 136 | + $to = $this->config->paths->templates; |
| 137 | + $this->files->copy($from, $to); |
| 138 | + $this->message(sprintf($this->_('Created template files: %s'), 'graphql.php & graphiql.php')); |
| 139 | + } catch(\Exception $e) { |
| 140 | + $this->error($e->getMessage()); |
| 141 | + } |
| 142 | + |
| 143 | + foreach($pageNames as $name => $title) { |
| 144 | + |
| 145 | + // create the templates |
| 146 | + try { |
| 147 | + $template = $this->wire(new Template()); |
| 148 | + $template->name = $name; |
| 149 | + $fieldgroup = $this->wire(new Fieldgroup()); |
| 150 | + $fieldgroup->name = $template->name; |
| 151 | + if ($this->fields->get('title') instanceof Field) $fieldgroup->add('title'); |
| 152 | + $fieldgroup->save(); |
| 153 | + $template->fieldgroup = $fieldgroup; |
| 154 | + $template->roles = array($this->roles->getGuestRole()->id); |
| 155 | + $template->noAppendTemplateFile = true; |
| 156 | + $template->noPrependTemplateFile = true; |
| 157 | + $template->save(); |
| 158 | + $this->message(sprintf($this->_('Added template and fieldgroup: %s'), $name)); |
| 159 | + } catch(\Exception $e) { |
| 160 | + $this->error($e->getMessage()); |
| 161 | + continue; |
| 162 | + } |
| 163 | + |
| 164 | + // create the pages |
| 165 | + try { |
| 166 | + $p = $this->wire(new Page()); |
| 167 | + $p->template = $this->templates->get($name); |
| 168 | + $p->name = $name; |
| 169 | + $p->parent = $this->pages->get(1); // root page |
| 170 | + $p->title = $title; |
| 171 | + $p->save(); |
| 172 | + $this->message(sprintf($this->_('Created page: %s'), $name)); |
| 173 | + } catch(\Exception $e) { |
| 174 | + $this->error($e->getMessage()); |
| 175 | + continue; |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + public function readyToGeneratePages() |
| 181 | + { |
| 182 | + $generatorName = self::generatorName; |
| 183 | + if ($this->input->post->$generatorName) $this->generatePages(); |
| 184 | + if (is_file($this->config->paths->templates . 'graphql.php')) return false; |
| 185 | + if (is_file($this->config->paths->templates . 'graphiql.php')) return false; |
| 186 | + if ($this->templates->get('graphql') instanceof Template) return false; |
| 187 | + if ($this->templates->get('graphiql') instanceof Template) return false; |
| 188 | + if (!$this->pages->get('/graphql/') instanceof NullPage) return false; |
| 189 | + if (!$this->pages->get('/graphiql/') instanceof NullPage) return false; |
| 190 | + return true; |
| 191 | + } |
| 192 | + |
123 | 193 | /**
|
124 | 194 | * Build module configuration.
|
125 | 195 | * @return InputFields ProcessWire Inputfields form.
|
@@ -219,6 +289,23 @@ public function getInputFields()
|
219 | 289 | }
|
220 | 290 | $inputfields->add($f);
|
221 | 291 |
|
| 292 | + // GRAPHQL PAGES GENERATOR |
| 293 | + if ($this->readyToGeneratePages()) { |
| 294 | + $fSet = $this->modules->get('InputfieldFieldset'); |
| 295 | + $fSet->label = 'GraphQL Pages Generator'; |
| 296 | + $fSet->collapsed = Inputfield::collapsedYes; |
| 297 | + |
| 298 | + // graphql template name |
| 299 | + $f = $this->modules->get('InputfieldSubmit'); |
| 300 | + $f->label = 'Generate'; |
| 301 | + $f->description = 'Generates the graphql and graphiql templates, template files and pages.'; |
| 302 | + $f->attr('name', self::generatorName); |
| 303 | + $f->attr('value', 'Generate'); |
| 304 | + $fSet->add($f); |
| 305 | + |
| 306 | + $inputfields->add($fSet); |
| 307 | + } |
| 308 | + |
222 | 309 | // ADVANCED
|
223 | 310 | $fSet = $this->modules->get('InputfieldFieldset');
|
224 | 311 | $fSet->label = 'Advanced';
|
|
0 commit comments