Example #1
0
 static function register($id, $key = false, $value = "")
 {
     // stop if variable already available
     //if(array_key_exists("pages", $GLOBALS['db_schema']) && in_array($key, $GLOBALS['db_schema']['pages'])) return;
     $page = new Page();
     $columns = $GLOBALS['db_schema']['pages'];
     $dbh = $page->getdbh();
     // check if the pages table exists
     $sql = "SELECT name FROM sqlite_master WHERE type='table' and name='pages'";
     $results = $dbh->prepare($sql);
     $results->execute();
     $table = $results->fetch(PDO::FETCH_ASSOC);
     // then check if the table exists
     if (!is_array($table)) {
         $keys = implode(", ", $columns);
         // FIX: The id needs to be setup as autoincrement
         $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");
     }
     // add the column if necessary
     if (array_key_exists("pages", $GLOBALS['db_schema']) && is_array($columns) && !in_array($key, $columns)) {
         $sql = "ALTER TABLE pages ADD COLUMN " . $key;
         $results = $dbh->prepare($sql);
         if ($results) {
             $results->execute();
         }
         // there's a case where the column may already exist, in which case this will be false...
         array_push($columns, $key);
     }
     // get existing page (again?)
     $sql = "SELECT * FROM 'pages' WHERE id='{$id}'";
     $results = $dbh->prepare($sql);
     if ($results) {
         $results->execute();
         $pages = $results->fetch(PDO::FETCH_ASSOC);
     } else {
         $pages = false;
     }
     // just create the key
     if (!$pages) {
         $newpage = new Page();
         $newpage->set('id', "{$id}");
         if ($key) {
             $newpage->set("{$key}", "{$value}");
         }
         $newpage->create();
     } else {
         if ($key) {
             $mypage = new Page($id);
             $existing = $mypage->get("{$key}");
             // allow empty strings to be returned
             if (is_null($existing)) {
                 $mypage->set("{$key}", "{$value}");
                 $mypage->update();
             }
         }
     }
 }