<?php
/*******************************************************************************/
//                          InstantCMS v1.8                                    //
//                      http://www.instantcms.ru/                              //
//                         component "Poster"                                  //
//                       written by Marat Fatikhov                             //
//                      (nickname Марат on a site)                             //
//                       E-mail: f-marat@mail.ru                               //
//                                                                             //
//                      LICENSED BY GNU/GPL v2                                 //
//                                                                             //
/********************************************************************************/
sleep(2);

//header('Content-Type: text/html; charset=windows-1251');
header('Cache-Control: no-cache');
session_start();

define("VALID_CMS", 1);
define('PATH', $_SERVER['DOCUMENT_ROOT']);

include(PATH.'/core/cms.php');

$inCore = cmsCore::getInstance();

$inCore->loadClass('db');
$inCore->loadClass('user');

$inDB   = cmsDatabase::getInstance();
$inUser = cmsUser::getInstance();

$inUser->update();

if (!$inUser->id) { die('Access denied'); }

$inCore->loadModel('poster');
$model = new cms_model_poster();

//проверяем входные параметры
$i_go = $inCore->request('i_go', 'str' , '');
$event_id = $inCore->request('event_id', 'int', 0);
if(!$i_go || !$event_id){ return false; }

//берем из БД кто идет
$who_goes_str = $inDB->get_field('cms_torg_items', "id = '{$event_id}'", 'who_goes');

$i_go_but=false;

 if($who_goes_str != ''){
             $who_goes_str = trim($who_goes_str);
             $who_goes = explode(',', $who_goes_str);
             //удаляем или добавляем юзера
             switch($i_go){
                  case "true" :
                      if(!in_array($inUser->id, $who_goes)){
                         $who_goes[] = $inUser->id;
                      }
                      break;
                  case "false" :
                      $key = array_search($inUser->id, $who_goes);
                      unset($who_goes[$key]);
                      break;
             }
             //добавляем в БД
             $model->updateWhoGoes($who_goes, $event_id);

             if(in_array($inUser->id, $who_goes)){ $i_go_but=true; }
             if( $who_goes && is_array($who_goes)){
                  $who_goes_str = implode(',', $who_goes);
                  $result = $inDB->query("SELECT u.id, u.login, u.nickname, p.gender
                                          FROM `cms_users` u
                                          LEFT JOIN `cms_user_profiles` p ON p.user_id = u.id
                                          WHERE u.id IN ({$who_goes_str}) AND u.is_deleted=0 AND u.is_locked=0
                                          ");
                  if(!$inDB->num_rows($result)){
                      $who_goes = '';
                  }else{
                       $who_goes = array();
                       while($user = $inDB->fetch_assoc($result)){
                            switch($user['gender']){
                                case 'm': $gender_img = '/components/users/images/male.png'; break;
                                case 'f': $gender_img = '/components/users/images/female.png'; break;
                                default : $gender_img = '/components/users/images/male.png'; break;
                            }
                            $who_goes[] = '<a style="padding:1px; height:16px; line-height:16px; background:url('.$gender_img.') no-repeat left center; padding-left:18px;" href="'.cmsUser::getProfileURL($user['login']).'" class="user_gender_link">'.$user['nickname'].'</a>';
                       }
                       $who_goes = implode(', ', $who_goes);
                  }
             }
}else{
     if($i_go == 'true'){
         $user_id = $inUser->id;
         //добавляем в БД
         $who_goes_arr = array();
         $who_goes_arr[] = $user_id;
         $model->updateWhoGoes($who_goes_arr, $event_id);

         $gender = $inDB->get_field('cms_user_profiles', "user_id = '{$user_id}'", 'gender');
         switch($gender){
            case 'm': $gender_img = '/components/users/images/male.png'; break;
            case 'f': $gender_img = '/components/users/images/female.png'; break;
            default : $gender_img = '/components/users/images/male.png'; break;
         }
        $who_goes[] = '<a style="padding:1px; height:16px; line-height:16px; background:url('.$gender_img.') no-repeat left center; padding-left:18px;" href="'.cmsUser::getProfileURL($inUser->login).'" class="user_gender_link">'.$inUser->nickname.'</a>';
        $who_goes = implode(', ', $who_goes);
        $i_go_but=true;
     }else{
       $who_goes = '';
     }
}
//формируем ответ
$reply = '<strong>Кто пойдет:</strong><div>';
if($who_goes){
    $reply .= $who_goes.'</div>';
}else{
    $reply .= 'Пока никто не присоединился.</div>';
}
if($i_go_but){
    $reply .= '<input type="button" name="not_go" value="Не пойду!" onclick="change_go(false, '.$event_id.')" style="margin-top:20px;">';
}else{
    $reply .= '<input type="button" name="i_go" value="Я пойду!" onclick="change_go(true, '.$event_id.')" style="margin-top:20px;">';
}



echo $reply;

?>
