]> git.bts.cx Git - cx.git/blob - cx/lib/site.php
Basic support for pagination
[cx.git] / cx / lib / site.php
1 <?php
2
3 cx_require('lib', 'db.php');
4 cx_require('lib', 'setup.php');
5
6 function _cx_default_site_details(...$args) {
7         $sql = 'SELECT '. join(',', $args) .'
8                 FROM sites
9                 LIMIT 1;';
10
11         foreach (cx_db_query($sql) as $site) {
12                 return $site;
13         }
14 }
15
16 function cx_sites_add_site($url, $title, $byline, $copyright) {
17         $sql = 'INSERT INTO sites (
18                         site_url,
19                         site_title,
20                         site_byline,
21                         site_copyright
22                 )
23                 VALUES (?, ?, ?, ?);';
24         $site_id = cx_db_exec($sql, $url, $title, $byline, $copyright);
25         return $site_id;
26 }
27
28 function cx_sites_site_add_user($site_id, $user_id, $owner = false) {
29         $sql = 'INSERT INTO site_authorship (
30                         site_authorship_site_id,
31                         site_authorship_user_id,
32                         site_authorship_owner
33                 )
34                 VALUES (?, ?, ?);';
35         cx_db_exec($sql, $site_id, $user_id, $owner);
36 }
37
38 function cx_site_name() {
39         $details = _cx_default_site_details('site_title');
40         return $details['site_title'];
41 }
42
43 function cx_site_byline() {
44         $details = _cx_default_site_details('site_byline');
45         return $details['site_byline'];
46 }
47
48 function cx_site_author() {
49         $details = _cx_default_site_details('site_copyright');
50         return $details['site_copyright'];
51 }
52
53 function cx_site_url() {
54         $details = _cx_default_site_details('site_url');
55         return $details['site_url'];
56 }
57
58 function cx_site_posts_per_page() {
59         return 5;
60 }
61
62 cx_setup_register(1, function() {
63         cx_db_exec('CREATE TABLE sites (
64                         site_id INTEGER PRIMARY KEY,
65                         site_url STRING,
66                         site_title STRING,
67                         site_byline STRING,
68                         site_copyright STRING
69                 );');
70         
71         cx_db_exec('CREATE TABLE site_metadata (
72                         site_metadata_site_id INTEGER,
73                         site_metadata_key STRING,
74                         site_metadata_value STRING,
75
76                         PRIMARY KEY (site_metadata_site_id, site_metadata_key),
77                         FOREIGN KEY (site_metadata_site_id) REFERENCES sites(site_id)
78                 );');
79
80         cx_db_exec('CREATE TABLE site_authorship (
81                         site_authorship_id INTEGER PRIMARY KEY,
82                         site_authorship_site_id INTEGER,
83                         site_authorship_user_id INTEGER,
84                         site_authorship_owner BOOLEAN,
85
86                         FOREIGN KEY(site_authorship_site_id) REFERENCES sites(site_id),
87                         FOREIGN KEY(site_authorship_user_id) REFERENCES users(user_id)
88                 );');
89 });