3 cx_require('lib', 'db.php');
4 cx_require('lib', 'setup.php');
5 cx_require('lib', 'markdown.php');
9 public $hero_image_alt;
11 public function __construct($dict) {
12 $this->hero_image = isset($dict['post_hero_image']) ? $dict['post_hero_image'] : null;
13 $this->hero_image_alt = isset($dict['post_hero_image_alt']) ? $dict['post_hero_image_alt'] : null;
29 public function __construct($dict) {
30 $this->id = $dict['post_id']; // FIXME, hide when not used?
31 $this->title = $dict['post_title'];
32 $this->slug = $dict['post_slug'];
33 $this->date = $dict['post_date'];
34 $this->is_page = $dict['post_is_page'];
35 $this->is_draft = $dict['post_is_draft'];
36 $this->nav_index = $dict['post_navigation_index'];
37 $this->data = $dict['post_data'];
38 $this->html_content = cx_markdown_generate_html($this->data);
39 $this->html_excerpt = null;
42 $segments = explode('---', $this->data, 2);
43 if (count($segments) > 1) {
44 $this->html_excerpt = cx_markdown_generate_html($segments[0]);
48 public function get_permalink_path() {
49 $post_permalink = $this->is_page ? '/' . $this->slug : '/' . date('Y', $this->date) . '/' . date('m', $this->date) . '/' . $this->slug;
50 return $post_permalink;
53 public function get_metadata() {
56 $doc = new DOMDocument();
57 $doc->loadHTML($this->html_content);
59 $image_tag = $doc->getElementsByTagName('img')[0];
61 if ($image_tag != null) {
62 $data['post_hero_image'] = $image_tag->getAttribute('src');
63 $data['post_hero_image_alt'] = htmlspecialchars($image_tag->getAttribute('alt'));
66 return new PostMetadata($data);
70 function cx_post_make_slug($title) {
71 $alnum_title = preg_replace('/[^A-Za-z0-9 ]?/', '', $title);
73 $slug_components = explode(' ', $alnum_title, 10);
74 $slug_components = array_filter($slug_components);
75 $slug_components = array_values($slug_components); // re-index
77 $slug = join('-', $slug_components);
78 $slug = strtolower($slug);
83 function cx_posts_add_post($site_id, $title, $slug, $date, $page, $draft, $nav_index, $data) {
84 $creation_time = $update_time = time();
87 $slug = cx_post_make_slug($title);
94 $sql = 'INSERT INTO posts (
102 post_navigation_index,
106 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);';
107 $id = cx_db_exec($sql, $site_id, $creation_time, $update_time, $slug, $date, $page, $draft, $nav_index, $title, $data, 1);
111 function cx_posts_update_post($post_id, $title, $slug, $date, $page, $draft, $nav_index, $data) {
112 $update_time = time();
115 $slug = cx_post_make_slug($title);
119 $date = $update_time;
123 SET post_update_time = ?,
128 post_navigation_index = ?,
131 WHERE post_id == ?;';
133 cx_db_exec($sql, $update_time, $slug, $date, $page, $draft, $nav_index, $title, $data, $post_id);
136 function cx_posts_delete_post($post_id) {
137 $sql = 'DELETE FROM posts
138 WHERE post_id == ?;';
140 cx_db_exec($sql, $post_id);
143 function cx_posts_get(int $limit = 0, int $offset = 0, bool $include_drafts = false) {
150 post_navigation_index,
154 WHERE post_is_page == FALSE';
156 if ($include_drafts == false) {
157 $sql .= ' AND post_is_draft == FALSE';
160 $sql .= ' ORDER BY post_date DESC';
163 $sql .= ' LIMIT ' . $limit;
167 $sql .= ' OFFSET ' . $offset;
172 foreach (cx_db_query($sql) as $post) {
173 $p = new Post($post);
178 function cx_posts_count(bool $include_drafts = false) {
180 COUNT(post_id) AS _count
182 WHERE post_is_page == FALSE';
184 if ($include_drafts == false) {
185 $sql .= ' AND post_is_draft == FALSE';
191 foreach (cx_db_query($sql) as $count_details) {
192 return $count_details['_count'];
198 function cx_posts_find_post($post_id) {
205 post_navigation_index,
212 foreach (cx_db_query($sql, $post_id) as $post) {
213 $p = new Post($post);
220 function cx_posts_find_article_id($post_slug, bool $include_drafts = false) {
225 AND post_is_page == FALSE';
227 if ($include_drafts == false) {
228 $sql .= ' AND post_is_draft == FALSE';
235 foreach (cx_db_query($sql, $post_slug) as $post) {
236 return $post['post_id'];
243 function cx_posts_find_page_id($post_slug, bool $include_drafts = false) {
248 AND post_is_page == TRUE';
250 if ($include_drafts == false) {
251 $sql .= ' AND post_is_draft == FALSE';
258 foreach (cx_db_query($sql, $post_slug) as $post) {
259 return $post['post_id'];
265 function cx_pages_get(bool $navigation_only = true, bool $include_drafts = false) {
272 post_navigation_index,
276 WHERE post_is_page == TRUE';
278 if ($navigation_only) {
279 $sql .= ' AND post_navigation_index != ""';
282 if ($include_drafts == false) {
283 $sql .= ' AND post_is_draft == FALSE';
286 $sql .= ' ORDER BY post_navigation_index ASC, post_date DESC';
290 foreach (cx_db_query($sql) as $post) {
291 $p = new Post($post);
296 cx_setup_register(1, function() {
297 cx_db_exec('CREATE TABLE posts (
298 post_id INTEGER PRIMARY KEY,
299 post_site_id INTEGER,
300 post_creation_time INTEGER,
301 post_update_time INTEGER,
304 post_is_page BOOLEAN,
305 post_is_draft BOOLEAN,
306 post_navigation_index INTEGER,
309 post_data_version INTEGER,
311 FOREIGN KEY(post_site_id) REFERENCES sites(site_id)