<?php
/**
 * 
 * @file class_mydata.php
 * @copyright (c) 2005 - 2007 eifr.com
 * @license http://www.gnu.org/copyleft/gpl.html gnu/gpl
 * eifr is free software
 */

require_once(EIFR_ROOT . 'libraries/template.php');

class ComMydata {
    function editRecordsUserPwd() {
        global $db, $lang, $url;
        global $password, $password_old;

        $password_old = md5($password_old);
        $sql1 = "select * from " . TB_USER . " where password='$password_old' and " . TF_USER_ID . "=" . getCookie(COOKIE_UID) . "";
        $rs1 = $db -> Execute($sql1);
        if (!$rs1 -> EOF) {
            $password = md5($password);
            $sql = "update " . TB_USER . " set " . TF_USER_PASS . "='$password' where " . TF_USER_ID . "=" . getCookie(COOKIE_UID) . "";
            $rs = $db -> Execute($sql);
            if ($rs) {
                Common::showMsg($lang['saveSuccess'], 'success', DIR_INSTALL . 'admin/' . $url . '?act=edit_user_pwd');
            } else {
                Common::showErrorMsg($lang['saveFail']);
            } 
            $rs -> Close();
        } 
        $rs1 -> Close();
    } 

    function editRecordsUserAll() {
        global $db, $lang, $url;

        $parameter = '';
        $param_arr = array();
        if (kPost('style_id') != '') $param_arr['style_id'] = kPost('style_id');
        if (kPost('only_login') != '') $param_arr['only_login'] = kPost('only_login');
        if ($param_arr) $parameter = serialize($param_arr);

        $record_arr = array('email' => kPost('email'),
            'nickname' => kPost('nickname'),
            'realname' => kPost('realname'),
            'tel' => kPost('tel'),
            'fax' => kPost('fax'),
            'mobile' => kPost('mobile'),
            'address_id' => kPost('address_id'),
            'fileurl' => kPost('fileurl'),
            'parameter' => $parameter,
            );

        Common::checkEditDataIsExist(TB_USER, 'email', kPost('email'), kPost('email_old'));

        if (dbUpdate(TB_USER, $record_arr, TF_USER_ID . "=" . getCookie(COOKIE_UID))) {
            setCookie(COOKIE_USERSTYLE, kPost('style_id'));
            Common::showMsg($lang['saveSuccess'], 'success', DIR_INSTALL . 'admin/' . $url . '?act=edit_user_all');
        } else {
            Common::showErrorMsg($lang['saveFail']);
        } 
    } 

    function layout_edit_user_pwd() {
        global $db, $url;

        $toolbar = Common::createToolbar($url, 'save', 'mydata_edit.php?act=save_user_pwd');
        $clTemplate = new Template(getAdminTemplateDir($url)); 
        // common
        $clTemplate -> assign_vars(array('sys_charset' => SYS_CHARSET,
                'style_id' => SYS_DEFAULT_STYLE,
                'adminStylePath' => SYS_ADMIN_STYLE_PATH,
                'menu_nav' => Common::createAdminMenuNav($url),
                'toolbar' => $toolbar
                ));

        $clTemplate -> set_filenames(array('body' => 'user_edit_pwd.tpl.html'));
        $clTemplate -> pparse("body");
    } 

    function layout_edit_user_all() {
        global $db, $url;
        global $selectid, $selectValue;

        $toolbar = Common::createToolbar($url, 'save', 'mydata_edit.php?act=save_user_all');
        $clTemplate = new Template(getAdminTemplateDir($url)); 
        // common
        $clTemplate -> assign_vars(array('sys_charset' => SYS_CHARSET,
                'style_id' => SYS_DEFAULT_STYLE,
                'adminStylePath' => SYS_ADMIN_STYLE_PATH,
                'sys_dir' => DIR_INSTALL,
                'menu_nav' => Common::createAdminMenuNav($url),
                'toolbar' => $toolbar
                ));

        $sql = "select * from " . TB_USER . " where " . TF_USER_ID . "=" . getCookie(COOKIE_UID) . "";
        $rs = $db -> Execute($sql);
        if (!$rs -> EOF) {
            $param_arr = unserialize($rs -> fields['parameter']); 
            $style_id = '';
            $only_login = '';
            if (is_array($param_arr)) {
                if (array_key_exists('style_id', $param_arr)) $style_id = $param_arr['style_id'];
                if (array_key_exists('only_login', $param_arr)) $only_login = $param_arr['only_login'];
            }
            // data
            $clTemplate -> assign_vars(array('selectid' => $selectid,
                    'email' => $rs -> fields['email'],
                    'email_old' => $rs -> fields['email'],
                    'username' => $rs -> fields[TF_USER_NAME],
                    'nickname' => $rs -> fields['nickname'],
                    'realname' => $rs -> fields['realname'],
                    'fileurl' => $rs -> fields['fileurl'],
                    'show_img' => $rs -> fields['fileurl'] ? "<img src='" . CSafeData::getImgSrc($rs -> fields['fileurl']) . "' align='absmiddle'>" : '',
                    'tel' => $rs -> fields['tel'],
                    'fax' => $rs -> fields['fax'],
                    'mobile' => $rs -> fields['mobile'],
                    'list_address' => Common::creatClassSelect(TB_ADDRESS, TF_ADDRESS_ID, $rs -> fields[TFR_ADDRESS_ID]),
                    'list_style' => Common::creatSelect(TB_STYLE, 'id', 'title', $style_id),
                    'list_only_login' => $this -> creatOnlyLoginRadio($only_login),
                    'list_class' => Common::creatClassSelect(TB_USER_CLASS, TF_USER_CLASS_ID, $rs -> fields[TFR_USER_CLASS_ID])
                    ));
        } 
        $rs -> Close();

        $clTemplate -> set_filenames(array('body' => 'user_edit_all.tpl.html'));
        $clTemplate -> pparse("body");
    } 

    function creatOnlyLoginRadio($status = 0) {
        global $lang;
        $rtn = '';
        $status_arr = array(0, 1);
        $status_txt_arr = array('no', 'yes');
        if (!is_numeric($status)) $status = -1;
        for($i = 0; $i < 2;$i++) {
            $e_s = '';
            if ($status_arr[$i] == $status) {
                $e_s = ' checked';
                $status = '';
            } 
            $showvalue = $status_txt_arr[$i];
            if (array_key_exists($status_txt_arr[$i], $lang)) $showvalue = $lang[$status_txt_arr[$i]];
            $rtn .= "<input type='radio' name='only_login' value='" . $status_arr[$i] . "' " . $e_s . ">" . $showvalue . "&nbsp;\n";
        } 
        return $rtn;
    } 
} 

?>