Created
December 13, 2016 23:39
-
-
Save jschroed91/648d363e44fd5adb2739c1caa80b0142 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getChapterContent(PDO $db) | |
{ | |
$sql = "SELECT id FROM Document WHERE slug = '2015-International--Building-Code' LIMIT 1"; | |
$stmt = $db->query($sql); | |
$books = $stmt->fetchAll(PDO::FETCH_ASSOC); | |
if (empty($books)) { | |
echo "\nFailure!"; | |
return false; | |
} | |
$bookId = $books[0]['id']; | |
$chapterContentSql = <<<'SQL' | |
SELECT c.* FROM Content c | |
JOIN Document d on d.id = c.documentID AND d.slug = '2015-International--Building-Code' | |
JOIN Types t on t.id = c.typeID AND t.typeName = 'chapter' | |
JOIN TextContent tc on tc.contentID = c.id | |
WHERE tc.text LIKE '%id="IBC2015_Ch07"%' | |
LIMIT 1 | |
SQL; | |
$stmt = $db->query($chapterContentSql); | |
$chapters = $stmt->fetchAll(PDO::FETCH_ASSOC); | |
if (empty($chapters)) { | |
echo "\nFailure!"; | |
return false; | |
} else { | |
echo "\nFound the chapter... now building... this takes a while..."; | |
} | |
return processContents($chapters, $bookId, $db); | |
} | |
/** | |
* @param array $contents | |
*/ | |
function processContents(array $contents, $documentId, PDO $db, $level = 0) | |
{ | |
$output = ''; | |
foreach ($contents as $contentRow) { | |
// Find the type... | |
$stmt = $db->query('SELECT * FROM TextContent WHERE contentID = '.$contentRow['id']); | |
$text = $stmt->fetchAll(PDO::FETCH_ASSOC); | |
if (!empty($text)) { | |
if (count($text) > 1) { | |
throw new \Exception('MULTIPLES!'); | |
} | |
$output .= processTextContent($contentRow, $text[0], $db); | |
} else { | |
$stmt = $db->query('SELECT * FROM FileContent WHERE contentID = '.$contentRow['id']); | |
$file = $stmt->fetchAll(PDO::FETCH_ASSOC); | |
if (!empty($file)) { | |
$output .= processFileContent($contentRow, $file[0], $db); | |
} else { | |
$stmt = $db->query('SELECT * FROM TableContent WHERE contentID = '.$contentRow['id']); | |
$table = $stmt->fetchAll(PDO::FETCH_ASSOC); | |
if (!empty($table)) { | |
$output .= processTableContent($contentRow, $table[0], $db); | |
} else { | |
throw new \Exception('NO CONTENT FOUND.'); | |
} | |
} | |
} | |
$stmt = $db->query(sprintf('SELECT * FROM Content WHERE documentID = %d AND parentContentID = %d', $documentId, $contentRow['id'])); | |
$childContents = $stmt->fetchAll(PDO::FETCH_ASSOC); | |
if (!empty($childContents)) { | |
$output .= processContents($childContents, $documentId, $db, $level + 1); | |
} | |
} | |
return $output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment