<?php

// settings_data_profile.php - PHProjekt Version 5.0
// copyright  ©  2000-2005 Albrecht Guenther  ag@phprojekt.com
// www.phprojekt.com
// Author: Franz Graf, $Author: paolo $
// $Id: settings_data_profile.php,v 1.7 2005/06/20 14:53:33 paolo Exp $

// check whether the lib has been included - authentication!
if (!defined('lib_included')) die('Please use settings.php!');


// Update:
if ($_REQUEST['profile_id'] && $_REQUEST['profile_id'] > 0) {

    // Check permission:
    // The user may update/delete this profile if it already exists
    // and if the von-entry equals with the userÄs id
    $query = "SELECT COUNT(*)
                FROM ".DB_PREFIX."profile
               WHERE ID = '".$_REQUEST['profile_id']."'
                 AND von = '".$user_ID."'";
    $result = db_query($query) or db_die();
    $row = db_fetch_row($result);

    if ($row and $row[0] > 0) {
        // okay - it's the user's profile let him do what he wants
        if ($_REQUEST['action_write_profile']) {
            update_profile($profile_id, $profile_name, $profile_users);
        }
        else if ($_REQUEST['action_delete_profile']) {
            delete_profile($profile_id);
        }
    }
}
else {
    insert_profile($profile_id, $profile_name, $profile_users);
}



// ---------------------------------------

/**
* Delete the profile identified by the given ID
* It is NOT checked, if the profile really belongs to that user!
* This HAS to be done before.
*
* @param int $profile_id    ID of the profile to delete
*/
function delete_profile($profile_id) {
    $query = "DELETE FROM ".DB_PREFIX."profile
                    WHERE ID = '$profile_id'";
    db_query($query) or db_die();
}


/*
* Insert a new profile.
* Parameterchecks are NOT performed at this point.
* If the array of usernames is empty, the insert does NOT get executed.
*
* @author       Franz Graf
* @param int    $profile_id     ID of the profile to update
* @param string $profile_name   (new) Name of this profile
* @param array  $profile_users  array of user-shortnames(!)
*/
function insert_profile ($profile_id, $profile_name, $profile_users) {
    global $user_ID, $dbIDnull;
    if (empty($profile_name) && count($profile_users) == 0) {
        return;
    }
    $query = "SELECT bezeichnung
                FROM ".DB_PREFIX."profile
               WHERE  von = '".$user_ID."'";
    $result = db_query($query);
    $row= db_fetch_row($result);
    $profile_name = trim($profile_name);
    if($profile_name==$row[0])message_stack_in(__('A Profile with the same name already exists'),'settings','error');
    else {
    if ($profile_name == '') {
        $profile_name = date('His');
    }
    $profile_users = serialize($profile_users);
    $query = xss("INSERT INTO ".DB_PREFIX."profile
                          (ID,        von,        bezeichnung,     personen)
                   VALUES ($dbIDnull, '$user_ID', '$profile_name', '$profile_users')");
    db_query($query) or db_die();
    }
}

/*
* Update an already existing profile of this user.
* Parameterchecks are NOT performed at this point.
*
* @author       Franz Graf
* @param int    $profile_id     ID of the profile to update
* @param string $profile_name   (new) Name of this profile
* @param array  $profile_users  array of user-shortnames(!)
*/
function update_profile($profile_id, $profile_name, $profile_users) {
    global $user_ID;

    $profile_users = serialize($profile_users);

    if (empty($profile_name) && count($profile_users) == 0) {
        return;
    }

    $query = xss("UPDATE ".DB_PREFIX."profile
                 SET bezeichnung = '$profile_name',
                     personen    = '$profile_users'
               WHERE ID = '$profile_id'");
    db_query($query) or db_die();
}

?>
