<?php



/**

 *      [Discuz!] (C)2001-2099 Comsenz Inc.

 *      This is NOT a freeware, use is subject to license terms

 *

 *      $Id: space_notice.php 34047 2013-09-25 04:41:45Z nemohou $

 */



if(!defined('IN_DISCUZ')) {

	exit('Access Denied');

}



$perpage = 30;

$perpage = mob_perpage($perpage);

$_GET['type'] = 'friend';

$page = empty($_GET['page'])?0:intval($_GET['page']);

if($page<1) $page = 1;

$start = ($page-1)*$perpage;



ckstart($start, $perpage);



$list = array();

$mynotice = $count = 0;

$multi = '';



if(empty($_G['member']['category_num']['manage']) && !in_array($_G['adminid'], array(1,2,3))) {

	unset($_G['notice_structure']['manage']);

}

$view = (!empty($_GET['view']) && (isset($_G['notice_structure'][$_GET[view]]) || in_array($_GET['view'], array('userapp'))))?$_GET['view']:'mypost';

$actives = array($view=>' class="a"');

$opactives[$view] = 'class="a"';

$categorynum = $newprompt = array();

if($view == 'userapp') {



	space_merge($space, 'status');



	if($_GET['op'] == 'del') {

		$appid = intval($_GET['appid']);

		C::t('common_myinvite')->delete_by_appid_touid($appid, $_G['uid']);

		showmessage('do_success', "home.php?mod=space&do=notice&view=userapp&quickforward=1");

	}



	$filtrate = 0;

	$count = 0;

	$apparr = array();

	$type = intval($_GET['type']);

	foreach(C::t('common_myinvite')->fetch_all_by_touid($_G['uid']) as $value) {

		$count++;

		$key = md5($value['typename'].$value['type']);

		$apparr[$key][] = $value;

		if($filtrate) {

			$filtrate--;

		} else {

			if($count < $perpage) {

				if($type && $value['appid'] == $type) {

					$list[$key][] = $value;

				} elseif(!$type) {

					$list[$key][] = $value;

				}

			}

		}

	}

	$mynotice = $count;



} else {

        $_GET['type'] = 'friend';

        if(empty($_G['uid'])){

                odz_error();

        }

	if(!empty($_GET['ignore'])) {

		C::t('home_notification')->ignore($_G['uid']);

	}



	foreach (array('wall', 'piccomment', 'blogcomment', 'clickblog', 'clickpic', 'sharecomment', 'doing', 'friend', 'credit', 'bbs', 'system', 'thread', 'task', 'group') as $key) {

		$noticetypes[$key] = lang('notification', "type_$key");

	}



	$isread = in_array($_GET['isread'], array(0, 1)) ? intval($_GET['isread']) : 0;

	$category = $type = '';

	if(isset($_G['notice_structure'][$view])) {

		if(!in_array($view, array('mypost', 'interactive'))) {

			$category = $view;

		} else {

			$deftype = $_G['notice_structure'][$view][0];

			if($_G['member']['newprompt_num']) {

				foreach($_G['notice_structure'][$view] as $subtype) {

					if($_G['member']['newprompt_num'][$subtype]) {

						$deftype = $subtype;

						break;

					}

				}

			}

			$type = in_array($_GET['type'], $_G['notice_structure'][$view]) ? trim($_GET['type']) : $deftype;

		}

	}

	$wherearr = array();

    // 版本区分

    include(DISCUZ_ROOT.'source/discuz_version.php');

    if(DISCUZ_VERSION != 'X2.5'){

        $new = -1;

    }



	if(!empty($type)) {

		$wherearr[] = "`type`='$type'";

	}



	$sql = ' AND '.implode(' AND ', $wherearr);





	$newnotify = false;

	$count = C::t('home_notification')->count_by_uid($_G['uid'], $new, $type, $category);



	if($count) {

		if($new == 1 && $perpage == 30) {

			$perpage = 200;

		}

		foreach(C::t('home_notification')->fetch_all_by_uid($_G['uid'], $new, $type, $start, $perpage, $category) as $value) {

			if($value['new']) {

				$newnotify = true;

				$value['style'] = 'color:#000;font-weight:bold;';

			} else {

				$value['style'] = '';

			}

			$value['rowid'] = '';

			if(in_array($value['type'], array('friend', 'poke'))) {

				$value['rowid'] = ' id="'.($value['type'] == 'friend' ? 'pendingFriend_' : 'pokeQuery_').$value['authorid'].'" ';

			}

			if($value['from_num'] > 0) $value['from_num'] = $value['from_num'] - 1;

                        

			$list[$value['id']] = $value;

		}



		$multi = '';

		$multi = multi($count, $perpage, $page, "home.php?mod=space&do=$do&isread=1");

	}

        

       // print_r($list);die;

        

        

        //是否是好友

        $result = array();

        $friends = DB::fetch_all('SELECT fuid FROM '.DB::table('home_friend').' WHERE uid = '.$_G['uid']);

        if(!empty($list)){

                //通知人uid

                foreach($list as $k => $v){

                    $uids[] = $v['authorid'];

                }

                //好友id

                foreach( $friends as $k=>$v){

                        $fuids[] = $v['fuid'];

                }

        

                $query = DB::query('SELECT c.uid,c.gender,c.birthyear,c.birthmonth,c.birthday ,c.affectivestatus,f.sightml  FROM '.DB::table('common_member_profile').' as c inner join '.DB::table('common_member_field_forum').' as f '.

                    " on c.uid=f.uid WHERE c.uid in (".implode(',', $uids).")");

                

                while($row = DB::fetch($query)){

                        foreach($list as $k => $v ){



                                if( $v['authorid'] == $row['uid'] && $v['from_idtype'] == 'friendrequest' ){

                                        $space_data = getuserbyuid($v['authorid'], 1);

                                        $result[] = array(

                                                'id'   => $v['id'],

                                                'fuid' => $v['authorid'],

                                                'username'=>$v['author'],

                                                'gender'=> empty($row['gender'])?'0':$row['gender'],

                                                'sightml'=>$row['sightml']?(strip_tags($row['sightml'])?strip_tags($row['sightml']):odz_lang('no_message_left')):odz_lang('no_message_left'),

                                                'age'=>$row["birthyear"]?(string)age($row["birthyear"].'-'.$row["birthmonth"].'-'.$row["birthday"]):'',

                                                'affectivestatus'=>$row['affectivestatus']?$row['affectivestatus']:odz_lang('secrect'),

                                                'avatar'=>avatar($v['authorid'],small,true),

                                                'datetime'=>date('Y-m-d',$v['dateline']),

                                                'note'  => str_replace('&nbsp;','',preg_replace('/<a .*?>(.*?)<\/a>/i','',$v['note'])),

                                                'isfriends'=>in_array($v['authorid'],$fuids)?'1':'0',

                                                'regdate'=>date('Y-m-d',$space_data['regdate']),

                                        );

                                }

                        }

                }

                

        }

        include_once (DISCUZ_ROOT.'source/discuz_version.php');

        //访问接口更新将通知用户id 所有通知改为已读  new =0 type = friend  category=2

        if(DISCUZ_VERSION == 'X2.5'){

            $res    =  DB::query('UPDATE '.DB::table('home_notification')." SET new=0 WHERE type = 'friend' AND uid = {$_G['uid']}");

        }else{

            $res    =  DB::query('UPDATE '.DB::table('home_notification')." SET new=0 WHERE type = 'friend' AND category=2 AND uid = {$_G['uid']}");

        }

        odz_result(array('list'=>$result));

        

	if($newnotify) {

		C::t('home_notification')->ignore($_G['uid'], $type, $category, true, true);

		if($_G['setting']['cloud_status']) {

			$noticeService = Cloud::loadClass('Service_Client_Notification');

			$noticeService->setNoticeFlag($_G['uid'], TIMESTAMP);

		}

	}

	helper_notification::update_newprompt($_G['uid'], ($type ? $type : $category));

	if($_G['setting']['my_app_status']) {

		$mynotice = C::t('common_myinvite')->count_by_touid($_G['uid']);

	}

	if($_G['member']['newprompt']) {

		$recountprompt = 0;

		foreach($_G['member']['category_num'] as $promptnum) {

			$recountprompt += $promptnum;

		}

		$recountprompt += $mynotice;

		if($recountprompt == 0) {

			C::t('common_member')->update($_G['uid'], array('newprompt' => 0));

		}

	}

        

	$readtag = array($type => ' class="a"');

}



dsetcookie('promptstate_'.$_G['uid'], $newprompt, 31536000);

include_once template("diy:home/space_notice");



?>