Skip to content

Commit

Permalink
making Page:register a bit more generic
Browse files Browse the repository at this point in the history
  • Loading branch information
tracend committed Nov 14, 2013
1 parent ac6748b commit 23fbb3f
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions app/models/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ function get_page_from_path( $uri ) {

static function register($id, $key=false, $value="") {


// stop if variable already available
//if(array_key_exists($table, $GLOBALS['pages']) && array_key_exists($key, $GLOBALS['pages'][$table])) return false;
if( !isset( $GLOBALS['db_schema'] ) ) $GLOBALS['db_schema'] = array();
if(array_key_exists("pages", $GLOBALS['db_schema']) && in_array($key, $GLOBALS['db_schema']['pages'])) return;

$page = new Page();
$dbh= $page->getdbh();
Expand All @@ -65,11 +65,30 @@ static function register($id, $key=false, $value="") {

// then check if the table exists
if(!is_array($table)){
//$page->create_table("pages", implode(",", array_keys( $page->rs )) );
$keys = implode(", ", array_keys( $page->rs ));
// FIX: The id needs to be setup as autoincrement
$page->create_table("pages", "id INTEGER PRIMARY KEY ASC, title, content, path, date, tags, template");
$keys = str_replace("id,", "id INTEGER PRIMARY KEY ASC,", $keys);
$page->create_table("pages", $keys );
//$page->create_table("pages", "id INTEGER PRIMARY KEY ASC, title, content, path, date, tags, template");
} else {
// get the existing schema
//$sql = "PRAGMA TABLE_INFO('pages')";
$sql = "SELECT * FROM pages LIMIT 1";
$results = $dbh->prepare($sql);
$results->execute();
$pages = $results->fetch(PDO::FETCH_ASSOC);
$GLOBALS['db_schema']['pages'] = array_keys( $pages );
}

// add the column if necessary
if( !in_array($key, $GLOBALS['db_schema']['pages']) ){
$sql = "ALTER TABLE pages ADD COLUMN ". $key;
$results = $dbh->prepare($sql);
$results->execute();
array_push( $GLOBALS['db_schema']['pages'], $key );
}

// this last query is debatable...
$sql = "SELECT * FROM 'pages' WHERE id='$id'";
$results = $dbh->prepare($sql);
$results->execute();
Expand Down

0 comments on commit 23fbb3f

Please sign in to comment.