Airship\Cabin\Bridge\Blueprint\CustomPages::createPage PHP Method

createPage() public method

Create a new page (and initial page version)
public createPage ( string $cabin, string $path, array $post = [], boolean $publish = false, boolean $raw = true ) : boolean
$cabin string
$path string
$post array
$publish boolean
$raw boolean
return boolean
    public function createPage(string $cabin, string $path, array $post = [], bool $publish = false, bool $raw = true) : bool
    {
        $this->db->beginTransaction();
        // Get the ID for the parent directory
        if (!empty($path)) {
            $directory_id = $this->getParentDirFromStr($path, $cabin);
        } else {
            $directory_id = null;
        }
        // Create the new page
        $pageId = $this->db->insertGet('airship_custom_page', ['active' => $publish, 'cabin' => $cabin, 'directory' => $directory_id, 'url' => $post['url'], 'cache' => !empty($post['cache'])], 'pageid');
        // Create the first version of the new page
        $this->db->insert('airship_custom_page_version', ['page' => $pageId, 'uniqueid' => $this->uniqueId('airship_custom_page_version'), 'published' => $publish, 'raw' => $raw, 'formatting' => $post['format'] ?? 'HTML', 'bridge_user' => $this->getActiveUserId(), 'metadata' => !empty($post['metadata']) ? \json_encode($post['metadata']) : '[]', 'body' => $post['page_body'] ?? '']);
        return $this->db->commit();
    }

Usage Example

Example #1
0
 /**
  * Create a new page in the current directory
  *
  * @param string $cabin
  * @param string $path
  * @param array $post
  * @return mixed
  */
 protected function processNewPage(string $cabin, string $path, array $post = []) : bool
 {
     $expected = ['url', 'format', 'page_body', 'save_btn', 'metadata'];
     if (!\Airship\all_keys_exist($expected, $post)) {
         return false;
     }
     $url = $path . '/' . \str_replace('/', '_', $post['url']);
     if (!empty($post['ignore_collisions']) && $this->detectCollisions($url, $cabin)) {
         $this->storeLensVar('post_response', ['message' => \__('The given filename might conflict with another route in this Airship.'), 'status' => 'error']);
         return false;
     }
     $raw = $this->isSuperUser() ? !empty($post['raw']) : false;
     if ($this->can('publish')) {
         $publish = $post['save_btn'] === 'publish';
     } elseif ($this->can('create')) {
         $publish = false;
     } else {
         $this->storeLensVar('post_response', ['message' => \__('You do not have permission to create new pages.'), 'status' => 'error']);
         return false;
     }
     if ($this->pg->createPage($cabin, $path, $post, $publish, $raw)) {
         \Airship\redirect($this->airship_cabin_prefix . '/pages/' . $cabin, ['dir' => $path]);
     }
     return true;
 }