Example #1
0
 /**
  * Prepare a page to be stored: update its folder, name, template, header and content
  *
  * @param \Grav\Common\Page\Page $page
  * @param bool                   $clean_header
  */
 protected function preparePage(\Grav\Common\Page\Page $page, $clean_header = false, $language = null)
 {
     $input = $this->post;
     if (isset($input['order'])) {
         $order = max(0, (int) isset($input['order']) ? $input['order'] : $page->value('order'));
         $ordering = $order ? sprintf('%02d.', $order) : '';
         $slug = empty($input['folder']) ? $page->value('folder') : (string) $input['folder'];
         $page->folder($ordering . $slug);
     }
     if (isset($input['name']) && !empty($input['name'])) {
         $type = (string) strtolower($input['name']);
         $name = preg_replace('|.*/|', '', $type);
         if ($language) {
             $name .= '.' . $language;
         } else {
             $language = $this->grav['language'];
             if ($language->enabled()) {
                 $name .= '.' . $language->getLanguage();
             }
         }
         $name .= '.md';
         $page->name($name);
         $page->template($type);
         // unset some header things, template for now as we've just set that
         if (isset($input['header']['template'])) {
             unset($input['header']['template']);
         }
     }
     // Special case for Expert mode: build the raw, unset content
     if (isset($input['frontmatter']) && isset($input['content'])) {
         $page->raw("---\n" . (string) $input['frontmatter'] . "\n---\n" . (string) $input['content']);
         unset($input['content']);
     }
     if (isset($input['header'])) {
         $header = $input['header'];
         foreach ($header as $key => $value) {
             if ($key == 'metadata') {
                 foreach ($header['metadata'] as $key2 => $value2) {
                     if (isset($input['toggleable_header']['metadata'][$key2]) && !$input['toggleable_header']['metadata'][$key2]) {
                         $header['metadata'][$key2] = '';
                     }
                 }
             } elseif ($key == 'taxonomy') {
                 foreach ($header[$key] as $taxkey => $taxonomy) {
                     if (is_array($taxonomy) && count($taxonomy) == 1 && trim($taxonomy[0]) == '') {
                         unset($header[$key][$taxkey]);
                     }
                 }
             } else {
                 if (isset($input['toggleable_header'][$key]) && !$input['toggleable_header'][$key]) {
                     $header[$key] = null;
                 }
             }
         }
         if ($clean_header) {
             $header = Utils::arrayFilterRecursive($header, function ($k, $v) {
                 return !(is_null($v) || $v === '');
             });
         }
         $page->header((object) $header);
         $page->frontmatter(Yaml::dump((array) $page->header()));
     }
     // Fill content last because it also renders the output.
     if (isset($input['content'])) {
         $page->rawMarkdown((string) $input['content']);
     }
 }
Example #2
0
 /**
  * Returns the page creating it if it does not exist.
  *
  * @param $path
  * @return Page
  */
 protected function getPage($path)
 {
     /** @var Pages $pages */
     $pages = $this->grav['pages'];
     if ($path && $path[0] != '/') {
         $path = "/{$path}";
     }
     $page = $path ? $pages->dispatch($path, true) : $pages->root();
     if (!$page) {
         $slug = basename($path);
         $ppath = dirname($path);
         // Find or create parent(s).
         $parent = $this->getPage($ppath != '/' ? $ppath : '');
         // Create page.
         $page = new Page();
         $page->parent($parent);
         $page->filePath($parent->path() . '/' . $slug . '/' . $page->name());
         // Add routing information.
         $pages->addPage($page, $path);
         // Determine page type.
         if (isset($this->session->{$page->route()})) {
             // Found the type and header from the session.
             $data = $this->session->{$page->route()};
             $page->name($data['type'] . '.md');
             $page->header(['title' => $data['title']]);
             $page->frontmatter(Yaml::dump((array) $page->header()));
         } else {
             // Find out the type by looking at the parent.
             $type = $parent->childType() ? $parent->childType() : $parent->blueprints()->get('child_type', 'default');
             $page->name($type . CONTENT_EXT);
             $page->header();
         }
         $page->modularTwig($slug[0] == '_');
     }
     return $page;
 }
Example #3
0
 /**
  * Returns the page creating it if it does not exist.
  *
  * @param $path
  *
  * @return Page
  */
 protected function getPage($path)
 {
     /** @var Pages $pages */
     $pages = $this->grav['pages'];
     if ($path && $path[0] != '/') {
         $path = "/{$path}";
     }
     $page = $path ? $pages->dispatch($path, true) : $pages->root();
     if (!$page) {
         $slug = basename($path);
         if ($slug == '') {
             return null;
         }
         $ppath = dirname($path);
         // Find or create parent(s).
         $parent = $this->getPage($ppath != '/' ? $ppath : '');
         // Create page.
         $page = new Page();
         $page->parent($parent);
         $page->filePath($parent->path() . '/' . $slug . '/' . $page->name());
         // Add routing information.
         $pages->addPage($page, $path);
         // Set if Modular
         $page->modularTwig($slug[0] == '_');
         // Determine page type.
         if (isset($this->session->{$page->route()})) {
             // Found the type and header from the session.
             $data = $this->session->{$page->route()};
             $visible = isset($data['visible']) && $data['visible'] != '' ? (bool) $data['visible'] : $this->guessVisibility($page);
             $header = ['title' => $data['title'], 'visible' => $visible];
             if ($data['type'] == 'modular') {
                 $header['body_classes'] = 'modular';
             }
             $name = $page->modular() ? str_replace('modular/', '', $data['type']) : $data['type'];
             $page->name($name . '.md');
             $page->header($header);
             $page->frontmatter(Yaml::dump((array) $page->header()));
         } else {
             // Find out the type by looking at the parent.
             $type = $parent->childType() ? $parent->childType() : $parent->blueprints()->get('child_type', 'default');
             $page->name($type . CONTENT_EXT);
             $page->header();
         }
         $page->modularTwig($slug[0] == '_');
     }
     return $page;
 }
Example #4
0
 /**
  * Returns the page creating it if it does not exist.
  *
  * @param $path
  *
  * @return Page
  */
 public function getPage($path)
 {
     /** @var Pages $pages */
     $pages = $this->grav['pages'];
     if ($path && $path[0] != '/') {
         $path = "/{$path}";
     }
     $page = $path ? $pages->dispatch($path, true) : $pages->root();
     if (!$page) {
         $slug = basename($path);
         if ($slug == '') {
             return null;
         }
         $ppath = str_replace('\\', '/', dirname($path));
         // Find or create parent(s).
         $parent = $this->getPage($ppath != '/' ? $ppath : '');
         // Create page.
         $page = new Page();
         $page->parent($parent);
         $page->filePath($parent->path() . '/' . $slug . '/' . $page->name());
         // Add routing information.
         $pages->addPage($page, $path);
         // Set if Modular
         $page->modularTwig($slug[0] == '_');
         // Determine page type.
         if (isset($this->session->{$page->route()})) {
             // Found the type and header from the session.
             $data = $this->session->{$page->route()};
             // Set the key header value
             $header = ['title' => $data['title']];
             if (isset($data['visible'])) {
                 if ($data['visible'] == '' || $data['visible']) {
                     // if auto (ie '')
                     $children = $page->parent()->children();
                     foreach ($children as $child) {
                         if ($child->order()) {
                             // set page order
                             $page->order(1000);
                             break;
                         }
                     }
                 }
                 if ($data['visible'] == 1 && !$page->order()) {
                     $header['visible'] = $data['visible'];
                 }
             }
             if ($data['name'] == 'modular') {
                 $header['body_classes'] = 'modular';
             }
             $name = $page->modular() ? str_replace('modular/', '', $data['name']) : $data['name'];
             $page->name($name . '.md');
             // Fire new event to allow plugins to manipulate page frontmatter
             $this->grav->fireEvent('onAdminCreatePageFrontmatter', new Event(['header' => &$header]));
             $page->header($header);
             $page->frontmatter(Yaml::dump((array) $page->header(), 10, 2, false));
         } else {
             // Find out the type by looking at the parent.
             $type = $parent->childType() ? $parent->childType() : $parent->blueprints()->get('child_type', 'default');
             $page->name($type . CONTENT_EXT);
             $page->header();
         }
         $page->modularTwig($slug[0] == '_');
     }
     return $page;
 }
 /**
  * Prepare a page to be stored: update its folder, name, template, header and content
  *
  * @param \Grav\Common\Page\Page $page
  * @param bool                   $clean_header
  */
 protected function preparePage(\Grav\Common\Page\Page $page, $clean_header = false)
 {
     $input = $this->post;
     $order = max(0, (int) isset($input['order']) ? $input['order'] : $page->value('order'));
     $ordering = $order ? sprintf('%02d.', $order) : '';
     $slug = empty($input['folder']) ? $page->value('folder') : (string) $input['folder'];
     $page->folder($ordering . $slug);
     if (isset($input['type']) && !empty($input['type'])) {
         $type = (string) strtolower($input['type']);
         $name = preg_replace('|.*/|', '', $type) . '.md';
         $page->name($name);
         $page->template($type);
     }
     // Special case for Expert mode: build the raw, unset content
     if (isset($input['frontmatter']) && isset($input['content'])) {
         $page->raw("---\n" . (string) $input['frontmatter'] . "\n---\n" . (string) $input['content']);
         unset($input['content']);
     }
     if (isset($input['header'])) {
         $header = $input['header'];
         if ($clean_header) {
             $header = Utils::arrayFilterRecursive($header, function ($k, $v) {
                 return !(is_null($v) || $v === '');
             });
         }
         $page->header((object) $header);
     }
     // Fill content last because it also renders the output.
     if (isset($input['content'])) {
         $page->rawMarkdown((string) $input['content']);
     }
 }