]>
git.bts.cx Git - cx.git/blob - cx/lib/posts.php
988d00eb008c457b873b67876d33f581213b9bba
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;
27 public function __construct($dict) {
28 $this->id
= $dict['post_id']; // FIXME, hide when not used?
29 $this->title
= $dict['post_title'];
30 $this->slug
= $dict['post_slug'];
31 $this->date
= $dict['post_date'];
32 $this->is_draft
= $dict['post_is_draft'];
33 $this->data
= $dict['post_data'];
34 $this->html_content
= cx_markdown_generate_html($this->data
);
35 $this->html_excerpt
= null;
38 $segments = explode('---', $this->data
, 2);
39 if (count($segments) > 1) {
40 $this->html_excerpt
= cx_markdown_generate_html($segments[0]);
44 public function get_metadata() {
47 $doc = new DOMDocument();
48 $doc->loadHTML($this->html_content
);
50 $image_tag = $doc->getElementsByTagName('img')[0];
52 if ($image_tag != null) {
53 $data['post_hero_image'] = $image_tag->getAttribute('src');
54 $data['post_hero_image_alt'] = htmlspecialchars($image_tag->getAttribute('alt'));
57 return new PostMetadata($data);
61 function cx_post_make_slug($title) {
62 $alnum_title = preg_replace('/[^A-Za-z0-9 ]?/', '', $title);
64 $slug_components = explode(' ', $alnum_title, 10);
65 $slug_components = array_filter($slug_components);
66 $slug_components = array_values($slug_components); // re-index
68 $slug = join('-', $slug_components);
69 $slug = strtolower($slug);
74 function cx_posts_add_post($site_id, $title, $slug, $date, $draft, $data) {
75 $creation_time = $update_time = time();
78 $slug = cx_post_make_slug($title);
85 $sql = 'INSERT INTO posts (
96 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);';
97 cx_db_exec($sql, $site_id, $creation_time, $update_time, $slug, $date, false, $draft, $title, $data, 1);
100 function cx_posts_update_post($post_id, $title, $slug, $date, $draft, $data) {
101 $update_time = time();
104 $slug = cx_post_make_slug($title);
108 $date = $update_time;
112 SET post_update_time = ?,
118 WHERE post_id == ?;';
120 cx_db_exec($sql, $update_time, $slug, $date, $draft, $title, $data, $post_id);
123 function cx_posts_delete_post($post_id) {
124 $sql = 'DELETE FROM posts
125 WHERE post_id == ?;';
127 cx_db_exec($sql, $post_id);
130 function cx_posts_get(int $limit = 0, bool $include_drafts = false) {
139 WHERE post_is_page == FALSE';
141 if ($include_drafts == false) {
142 $sql .= ' AND post_is_draft == FALSE';
145 $sql .= ' ORDER BY post_date DESC';
148 $sql .= ' LIMIT ' . $limit;
153 foreach (cx_db_query($sql) as $post) {
154 $p = new Post($post);
159 function cx_posts_find_post($post_id) {
168 WHERE post_is_page == FALSE
172 foreach (cx_db_query($sql, $post_id) as $post) {
173 $p = new Post($post);
180 function cx_posts_find_post_id($post_slug) {
187 foreach (cx_db_query($sql, $post_slug) as $post) {
188 return $post['post_id'];
194 function cx_pages_get() {
203 WHERE post_is_page == TRUE
204 AND post_is_draft == FALSE
205 ORDER BY post_creation_time DESC;';
207 foreach (cx_db_query($sql) as $post) {
208 $p = new Post($post);
213 cx_setup_register(1, function() {
214 cx_db_exec('CREATE TABLE posts (
215 post_id INTEGER PRIMARY KEY,
216 post_site_id INTEGER,
217 post_creation_time INTEGER,
218 post_update_time INTEGER,
221 post_is_page BOOLEAN,
222 post_is_draft BOOLEAN,
225 post_data_version INTEGER,
227 FOREIGN KEY(post_site_id) REFERENCES sites(site_id)