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();
}