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

updatePage() public method

Create a new page (and initial page version)
public updatePage ( integer $pageId, array $post = [], boolean $publish = false, boolean | null $raw = null, boolean $cache = false ) : boolean
$pageId integer
$post array
$publish boolean (This is set to FALSE by the Landing if the permission check fails)
$raw boolean | null
$cache boolean
return boolean
    public function updatePage(int $pageId, array $post = [], bool $publish = false, $raw = null, bool $cache = false) : bool
    {
        $this->db->beginTransaction();
        if ($publish) {
            $this->db->update('airship_custom_page', ['active' => true, 'cache' => $cache], ['pageid' => $pageId]);
        }
        if ($raw === null) {
            $raw = $this->db->cell('SELECT raw FROM airship_custom_page_version WHERE page = ? ORDER BY versionid DESC LIMIT 1', $pageId);
        }
        $last_copy = $this->db->row('SELECT * FROM airship_custom_page_version WHERE page = ? ORDER BY versionid DESC LIMIT 1', $pageId);
        // Short circuit the needless inserts
        if ($last_copy['raw'] === (bool) $raw && $last_copy['formatting'] === (string) $post['format'] && \json_decode($last_copy['metadata'], true) === $post['metadata'] && $last_copy['body'] === (string) $post['page_body']) {
            // Are we publishing a previously unpublished edition?
            if ($publish && !$last_copy['publish']) {
                $this->db->update('airship_custom_page_version', ['published' => $publish], ['page' => $pageId]);
            }
            \Airship\clear_cache();
            return $this->db->commit();
        }
        $meta = !empty($post['metadata']) ? \json_encode($post['metadata']) : '[]';
        // Create the next version of the new page
        $this->db->insert('airship_custom_page_version', ['page' => (int) $pageId, 'published' => (bool) $publish, 'uniqueid' => $this->uniqueId('airship_custom_page_version'), 'raw' => (bool) $raw, 'formatting' => (string) $post['format'] ?? 'HTML', 'bridge_user' => (int) $this->getActiveUserId(), 'metadata' => (string) $meta, 'body' => (string) ($post['page_body'] ?? '')]);
        if ($publish) {
            // Nuke the page cache
            \Airship\clear_cache();
        }
        return $this->db->commit();
    }

Usage Example

Exemplo n.º 1
0
 /**
  * Create a new page in the current directory
  *
  * @param int $pageId
  * @param array $post
  * @param string $cabin
  * @param string $dir
  * @return mixed
  */
 protected function processEditPage(int $pageId, array $post = [], string $cabin = '', string $dir = '') : bool
 {
     $required = ['format', 'page_body', 'save_btn', 'metadata'];
     if (!\Airship\all_keys_exist($required, $post)) {
         return false;
     }
     if ($this->isSuperUser()) {
         $raw = !empty($post['raw']);
     } else {
         $raw = null;
         // Don't set
     }
     $cache = !empty($post['cache']);
     if ($this->can('publish')) {
         $publish = $post['save_btn'] === 'publish';
     } elseif ($this->can('update')) {
         $publish = false;
     } else {
         $this->storeLensVar('post_response', ['message' => \__('You do not have permission to edit pages.'), 'status' => 'error']);
         return false;
     }
     if ($this->pg->updatePage($pageId, $post, $publish, $raw, $cache)) {
         \Airship\redirect($this->airship_cabin_prefix . '/pages/' . $cabin, ['dir' => $dir]);
     }
     return true;
 }