function save_note($action, $data)
{
    $nt = new DataBaseTable('notes', true, DATACONF);
    if ($action == 'edit' && !empty($_GET['nid'])) {
        $data['nid'] = $_GET['nid'];
        if ($nid = $nt->updateData($data)) {
            return $nid;
        } else {
            return false;
        }
    } elseif ($action != "remove") {
        if ($nid = $nt->putData($data)) {
            return $nid;
        } else {
            return false;
        }
    } else {
        if ($nid = $nt->deleteData($data)) {
            return $nid;
        } else {
            return false;
        }
    }
}
function put_defaults($admin, $guest, $settings)
{
    $settings_tbl = new DataBaseTable('settings', true, dirname(__FILE__) . '/dataconnect/connect.ini');
    $type_tbl = new DataBaseTable('types', true, dirname(__FILE__) . '/dataconnect/connect.ini');
    $user_tbl = new DataBaseTable('users', true, dirname(__FILE__) . '/dataconnect/connect.ini');
    //TODO add descriptions
    $sys_types[0] = array('name' => "Book");
    $sys_types[1] = array('name' => 'Volume');
    $sys_types[2] = array('name' => 'Episode/Act');
    $sys_types[3] = array('name' => 'Issue');
    foreach ($sys_types as $types) {
        $type_rows[] = $type_tbl->putData($types);
    }
    if ($admin['pass1'] == $admin['pass2']) {
        $okay = 0;
        $totrows = 0;
        foreach ($settings as $setting['key'] => $setting['value']) {
            if ($row = $settings_tbl->putData($setting)) {
                $okay++;
            }
            $totrows++;
        }
        foreach ($guest as $key => $value) {
            if ($key != 'name') {
                $admin[$key] = $value;
                $root[$key] = $value;
            }
        }
        if ($guest = $user_tbl->putData($guest)) {
            $okay++;
        }
        $totrows++;
        $admin['password'] = crypt($admin['pass2']);
        $admin['level'] = 1;
        $root['name'] = "root";
        $root['level'] = 1;
        if ($root = $user_tbl->putData($root) && ($admin = $user_tbl->putData($admin))) {
            $okay++;
        }
        $totrows++;
        if ($okay == $totrows) {
            return true;
        }
    }
}
function save_asset($action, $data)
{
    $con = new DataBaseTable('content', true, DATACONF);
    $tags = new MCTags();
    if (!empty($_GET['cid'])) {
        if (empty($data['tags'])) {
            $tags->delAllByCon($_GET['cid']);
        } else {
            $tarr = explode(",", $data['tags']);
            $tags->changeAssoc($_GET['cid'], $tarr);
        }
    }
    //file uploads should be handled elsewhere...
    if ($action == 'drop') {
        $tags->delAllByCon($_GET['cid']);
        if ($cid = $con->deleteData($data)) {
            return $cid . " dropped";
        } else {
            return $cid . "could not be removed!";
        }
    } elseif (!empty($_GET['cid'])) {
        $data['cid'] = $_GET['cid'];
        if ($cid = $con->updateData($data)) {
            return $cid;
        } else {
            return false;
        }
    } else {
        if ($cid = $con->putData($data)) {
            return $cid;
        } else {
            return false;
        }
    }
}
function add_art($action, $data, $site_settings)
{
    $raw = file_get_contents($site_settings->project_dir . "/" . $data['temp_name']);
    unlink($site_settings->project_dir . "/" . $data['temp_name']);
    $image = imagecreatefromstring($raw);
    //TODO crop and/or resize image base on ttid?
    $user = new DataBaseTable("users", true, DATACONF);
    $user = $user->getData("uid:`= {$data['uid']}`");
    $user = $user->fetch(PDO::FETCH_ASSOC);
    if (!is_dir($site_settings->project_dir . "/" . storagename($user['name']))) {
        mkdir($site_settings->project_dir . "/" . storagename($user['name']));
    }
    $file = storagename($user['name']) . "/" . storagename($data['title']) . ".png";
    $art = new DataBaseTable("art", true, DATACONF);
    if (imagepng($image, $site_settings->project_dir . "/" . $file, 9)) {
        $data['uri'] = $file;
        if ($aid = $art->putData($data)) {
            return $aid . " added successfully!";
        } else {
            return $aid . " could not be added!";
        }
    } else {
        return $data['temp_name'] . " could not be opened for processing!";
    }
}