<?php
/**
* @author moufer<moufer@163.com>
* @copyright www.modoer.com
* @version $Id 7 2008-09-04 18:29 $
*/
!defined('IN_MUDDER') && exit('Access Denied');

// 上传一张照片
function postPicture($post) {
}

// 删除照片 // idtype 为 myids 的类型（pid,uid,shopid）
function deletePicture($myids, $idtype='pid', $opuid=-1, $upItem=true, $credit=false, $delpic=true) {
    global $db, $dbpre;

    if(empty($myids)||!is_array($myids)) return;
    $showmsg = defined("IN_ADMIN") ? 'cpmsg' : 'redirect';
    $ids = "'".implode("','", $myids)."'";
    $delids = array();
    $query = $db->query("SELECT * FROM {$dbpre}pictures WHERE $idtype IN ($ids)");
    if(!function_exists('updateCredit')) {
        require_once(MUDDER_ROOT.'./member/include/fun_member.php');
    }
    while($row = $db->fetch_array($query)) {
        $access = defined('IN_ADMIN') || $opuid == -1 || $row['uid'] == $opuid;
        !$access and $showmsg('您没有足够的权限来完成本次操作。');
        $delids[] = $row['pid'];
        if($picture['status'] != 2) {
            $credit and $sqlset = updateCredit($row['uid'], 'deletepicture', 'sqlset');
            $sqlset .= ($sqlset ? ',' : '') .'pictures=pictures-1';
            $db->update("UPDATE {$dbpre}members SET $sqlset WHERE uid='{$row['uid']}'");
            $upItem and $db->update("UPDATE {$dbpre}shops SET pictures=pictures-1 WHERE sid='{$row['shopid']}'");
        }
        if($delpic) {
            @unlink(MUDDER_ROOT.'./uploads/pictures/'.$row['folder'].'/'.$row['filename']);
            @unlink(MUDDER_ROOT.'./uploads/pictures/'.$row['folder'].'/g_'.$row['filename']);
            @unlink(MUDDER_ROOT.'./uploads/pictures/'.$row['folder'].'/s_'.$row['filename']);
        }
    }
    $db->free_result($query);
    if($delids) {
        $ids = "'".implode("','", $delids)."'";
        $db->query("DELETE FROM {$dbpre}pictures WHERE pid IN ($ids)");
    }
}

// 审核新照片
function checkupPicture($pictureids) {
    global $db,$dbpre;

    if(empty($pictureids)||!is_array($pictureids)) return;
    $ids = "'".implode("','", $pictureids)."'";
    $upids = array();
    $query = $db->query("SELECT * FROM {$dbpre}pictures WHERE pid IN ($ids)");
    while($row = $db->fetch_array($query)) {
        if($row['status'] == 2) {
            $upids[] = $row['pid'];
            pictureBeRelated($row);
        }
    }
    if($upids) {
        $ids = "'".implode("','", $upids)."'";
        $db->query("UPDATE {$dbpre}pictures SET status=1 WHERE pid IN ($ids)");
    }
}

// 图片添加有效后，关联表的更新
function pictureBeRelated($row) {
    global $db, $dbpre, $_config;

    // 封面设置
    $thumbfile = $row['folder'].'/'.$row['filename'];
    if($_config['shopthumb'] == 3) { // auto
        $total = intval($db->get_value("SELECT pictures FROM {$dbpre}shops WHERE sid='{$row['shopid']}'"));
        empty($total) and $sqlset = "thumb='$thumbfile'";
    } elseif($_config['shopthumb'] != 2) { // always new picture
        $sqlset = "thumb='$thumbfile'";
    }
    // 增加统计数量
    $sqlset .= ($sqlset ? ',' : '') . 'pictures=pictures+1';
    $db->update("UPDATE {$dbpre}shops SET $sqlset WHERE sid='{$row['shopid']}'");

    // 更新会员积分和统计
    if(!function_exists('updateCredit')) {
        require_once(MUDDER_ROOT.'./member/include/fun_member.php');
    }
    $sqlset = updateCredit($row['uid'], 'addpicture', 'sqlset');
    $sqlset .= ($sqlset ? ', ' : '') . "pictures=pictures+1";
    $db->update("UPDATE {$dbpre}members SET $sqlset WHERE uid='{$row['uid']}'");
}

// 读取图片
function getItemPictures($shopid, $limit = '0,10') {
        global $db, $dbpre;
        if($shopid <= 0) return;
        $query = $db->query("SELECT * FROM {$dbpre}pictures WHERE shopid='$shopid' AND status=1 ORDER BY addtime DESC LIMIT $limit");
        while($row = $db->fetch_array($query)) {
            $pictures[] = $row;
        }
        return $pictures;
}
?>