Permalink
Browse files

Add slugs for comics

Will require this sql:
ALTER TABLE comics ADD COLUMN `slug` VARCHAR( 32 ) NOT NULL AFTER `title`;
ALTER TABLE comics ADD INDEX `byslug` (`slug`);
ALTER TABLE chapters ADD INDEX `byslug` (`slug`);
  • Loading branch information...
1 parent 888e094 commit 38e0361646e6a65641d3dcc7c17ee8b9630ebd68 @kemayo committed Jul 30, 2014
Showing with 38 additions and 14 deletions.
  1. +13 −4 include/admin.php
  2. +11 −7 include/common.php
  3. +5 −1 index.php
  4. +5 −2 install/setup.sql
  5. +1 −0 page_map.txt
  6. +3 −0 template/default/admin_comic.php
View
@@ -58,13 +58,19 @@
if(!$pub_date) {
die_error("Bad date");
}
+ if (!empty($_POST['slug']) || is_numeric($_POST['slug'])) {
+ die_error("Slug can't be just a number. That confuses things.");
+ }
if($comic) {
$comicid = $comic['comicid'];
if($db->quick("SELECT comicid FROM comics WHERE pub_date = %d AND comicid != %d", array($pub_date, $comicid))) {
die_error("There is already a comic with that exact date. Please choose a different date.");
}
- $db->query("UPDATE comics SET title=%s, pub_date=%d, filename=%s, chapterid=%d WHERE comicid=%d",
- array($_POST['title'], $pub_date, $_POST['filename'], $_POST['chapterid'], $comicid));
+ if($db->quick("SELECT comicid FROM comics WHERE slug = %s AND comicid != %d", array($_POST['slug'], $comicid))) {
+ die_error("There is already a comic with that slug. Please choose a different one.");
+ }
+ $db->query("UPDATE comics SET title=%s, slug=%s, pub_date=%d, filename=%s, chapterid=%d WHERE comicid=%d",
+ array($_POST['title'], $_POST['slug'], $pub_date, $_POST['filename'], $_POST['chapterid'], $comicid));
} else {
if($db->quick("SELECT comicid FROM comics WHERE pub_date = %d", $pub_date)) {
die_error("There is already a comic with that exact date. Please choose a different date.");
@@ -91,8 +97,8 @@
die_error("No filename");
}
$comicid = $db->insert_id(
- "INSERT INTO comics (title, pub_date, filename, chapterid) VALUES (%s, %d, %s, %d)",
- array($_POST['title'], $pub_date, $filename, $_POST['chapterid']));
+ "INSERT INTO comics (title, slug, pub_date, filename, chapterid) VALUES (%s, %d, %s, %d)",
+ array($_POST['title'], $_POST['slug'], $pub_date, $filename, $_POST['chapterid']));
}
$db->query(
"REPLACE INTO comics_text (comicid, description, transcript, alt_text) VALUES (%d, %s, %s, %s)",
@@ -158,6 +164,9 @@
if(!empty($_POST['closed'])) {
$status = STATUS_CLOSED;
}
+ if($db->quick("SELECT chapterid FROM chapters WHERE slug = %s AND chapterid != %d", array($_POST['slug'], $chapterid))) {
+ die_error("There is already a chapter with that slug. Please choose a different one.");
+ }
$db->query("UPDATE chapters SET title = %s, slug = %s, status = %d WHERE chapterid = %d",
array($_POST['title'], $_POST['slug'], $status, $chapterid));
View
@@ -113,13 +113,17 @@ function fetch_navigation($comic) {
global $db;
// comics that were published at the exact moment that this one was:
// $comics_now = $db->quick("SELECT COUNT(*) FROM comics WHERE pub_date = %d", $comic['pub_date']);
- return array(
- 'current' => $comic['comicid'],
- 'prev' => $db->quick("SELECT comicid FROM comics WHERE pub_date < %d AND pub_date <= UNIX_TIMESTAMP() ORDER BY pub_date DESC LIMIT 1", $comic['pub_date']),
- 'next' => $db->quick("SELECT comicid FROM comics WHERE pub_date > %d AND pub_date <= UNIX_TIMESTAMP() ORDER BY pub_date ASC LIMIT 1", $comic['pub_date']),
- 'first' => $db->quick("SELECT comicid FROM comics WHERE pub_date <= UNIX_TIMESTAMP() ORDER BY pub_date ASC LIMIT 1"),
- 'last' => $db->quick("SELECT comicid FROM comics WHERE pub_date <= UNIX_TIMESTAMP() ORDER BY pub_date DESC LIMIT 1"),
- );
+ $prev_comic = $db->fetch_first("SELECT comicid, slug FROM comics WHERE pub_date < %d AND pub_date <= UNIX_TIMESTAMP() ORDER BY pub_date DESC LIMIT 1", $comic['pub_date']);
+ $next_comic = $db->fetch_first("SELECT comicid, slug FROM comics WHERE pub_date > %d AND pub_date <= UNIX_TIMESTAMP() ORDER BY pub_date ASC LIMIT 1", $comic['pub_date']);
+ $first_comic = $db->fetch_first("SELECT comicid, slug FROM comics WHERE pub_date <= UNIX_TIMESTAMP() ORDER BY pub_date ASC LIMIT 1");
+ $last_comic = $db->fetch_first("SELECT comicid, slug FROM comics WHERE pub_date <= UNIX_TIMESTAMP() ORDER BY pub_date DESC LIMIT 1");
+
+ $current = $comic['slug'] ?: $comic['comicid'];
+ $prev = $prev_comic['slug'] ?: $prev_comic['comicid'];
+ $next = $next_comic['slug'] ?: $next_comic['comicid'];
+ $first = $first_comic['slug'] ?: $first_comic['comicid'];
+ $last = $last_comic['slug'] ?: $last_comic['comicid'];
+ return compact('current', 'prev', 'next', 'first', 'last');
}
function fetch_text($comicid) {
View
@@ -115,7 +115,11 @@
die;
}
// comic page
- $comic = $db->fetch_first("SELECT * FROM comics WHERE comicid = %d AND pub_date <= UNIX_TIMESTAMP()", $request[1]);
+ if (!is_numeric($request[1])) {
+ $comic = $db->fetch_first("SELECT * FROM comics WHERE slug = %s AND pub_date <= UNIX_TIMESTAMP()", $request[1]);
+ } else {
+ $comic = $db->fetch_first("SELECT * FROM comics WHERE comicid = %d AND pub_date <= UNIX_TIMESTAMP()", $request[1]);
+ }
if(!$comic) {
redirect("");
}
View
@@ -1,11 +1,13 @@
CREATE TABLE `comics` (
`comicid` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`title` VARCHAR( 255 ) NOT NULL ,
+`slug` VARCHAR( 32 ) NOT NULL ,
`filename` VARCHAR( 255 ) NOT NULL ,
`pub_date` INT NOT NULL ,
`chapterid` INT NOT NULL ,
INDEX `bychapter` ( `chapterid` , `pub_date` ) ,
-INDEX `bydate` ( `pub_date` )
+INDEX `bydate` ( `pub_date` ) ,
+INDEX `byslug` ( `slug` )
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE TABLE `comics_text` (
@@ -22,7 +24,8 @@ INDEX `bydate` ( `pub_date` )
`slug` VARCHAR( 32 ) NOT NULL ,
`order` TINYINT NOT NULL ,
`parentid` INT NOT NULL ,
- `status` TINYINT NOT NULL
+ `status` TINYINT NOT NULL ,
+ INDEX `byslug` ( `slug` )
) ENGINE = MYISAM;
CREATE TABLE `chapters_text` (
View
@@ -1,6 +1,7 @@
index
comic/[id]
+comic/[slug]
chapters/
chapter/[slug]
rants/
@@ -22,6 +22,9 @@
<input name="pub_date" class="datetime" value="<?php echo date('Y-m-d H:i:s', isset($pub_date) ? $pub_date : time()); ?>" />
<small>YYYY-MM-DD HH:MM:SS. Comics dated in the future will not be published until that time.</small>
+ <label>Slug</label>
+ <input name="slug" value="<?php echo isset($slug) ? $slug : ''; ?>" />
+
<label>Chapter</label>
<select name="chapterid">
<?php

0 comments on commit 38e0361

Please sign in to comment.