<?php

define('_IN_JOHNCMS', 1);

$textl = 'Ballary redaktirlemek';
$headmod = 'point';
require_once '../incfiles/core.php';
require_once '../incfiles/head.php';


if (9 == $rights) {
    $arr_id = (isset($_POST['arr_id']) ? $_POST['arr_id'] : '');
    $balls = (isset($_POST['balls']) ? (int)$_POST['balls'] : 0);
    $view = (isset($_POST['view']) ? (int)$_POST['view'] : 0);
    $yes = (isset($_POST['yes']) && 1 == $_POST['yes'] ? (int)$_POST['yes'] : 0);
    
    if (isset($_POST['subm'])) {
        if (!empty($view)) {
            if (empty($arr_id)) {
                $viewing = '<h4 class="rmenu">Ulanyjyny saýlaň!</h4>';
            } else {
                $view_balans = array();
                foreach ($arr_id as $id) {
                    $arr = mysql_fetch_array(mysql_query("SELECT `name`,`balans` FROM `users` WHERE `id`='$id';"));
                    $view_balans[] = '<div class="menu">'. $arr[0] .' - '. $arr[1] .'</div>';
                }
                $viewing = '<h4 class="bmenu">Ballaryna seretmek!<hr />'. implode($view_balans) .'</h4>';
            }
        } else {
            $errors = array();
            if (empty($arr_id))
                $errors[] = '<h4 class="rmenu">Ulanyjyny saýlaň!</h4>';
            if (empty($balls)) {
                $errors[] = '<h4 class="rmenu">Dolduryljak summany giriziň!</h4>';
            } elseif (strlen($balls) > 7) {
                $errors[] = '<h4 class="rmenu">Ball bermek maks 7 uzynlykda bolmaly!</h4>';
            }
            if (!empty($errors)) {
                $viewing = implode($errors);
            } else {
                if (1 == $yes) {
                    foreach ($arr_id as $id) {
                        mysql_query("UPDATE `users` SET `balans`=`balans`+'$balls' WHERE `id`=". intval($id) .";");
                    }
                    header('Refresh:1; url='. $_SERVER['REQUEST_URI']);
                    $viewing = '<h4 class="gmenu">Ball goşuldy!</h4>';
                } else {
                    foreach ($arr_id as $id) {
                        mysql_query("UPDATE `users` SET `balans`=`balans`-'$balls' WHERE `id`=". intval($id) .";");
                    }
                    header('Refresh:1; url='. $_SERVER['REQUEST_URI']);
                    $viewing = '<h4 class="rmenu">Ball öçürildi!</h4>';
                }
            }
        }
    }
    
    $sql = "SELECT `id`,`name` FROM `users`;";
    if ($res = mysql_query($sql)) {
        $viewing .= '<h4 class="bmenu">Ulanyjyny saýlaň<hr /><div class="menu">'.
            '<form action="'. $_SERVER['REQUEST_URI'] .'" method="post">'.
            '<select size="5" multiple="multiple" name="arr_id[]">';
        while ($us_name = mysql_fetch_array($res)) {
            $viewing .= '<option value="'. $us_name['id'] .'">'. $us_name['name'] .'</option>';
        }
        $viewing .= '</select></div></h4><h4 class="bmenu"><input type="checkbox" name="view" value="1" /> Ballara seretmek<hr />'.
            '<div class="menu"><input type="text" maxlength="7" size="5" name="balls" '. (empty($balls) ? 
            'placeholder="Smumma"' : 'value="'. $balls .'"') .'" /><p>'.
            '<input type="radio" name="yes" value="1" checked="checked" />goşmak'.
            '<input type="radio" name="yes" value="0" />öçürmek</p>'.
            '<p><input type="submit" name="subm" value="Ýerine ýetirmek" /></p></form></div></h4>';
    }
    echo $viewing;
} else {
    echo '<h4 class="rmenu">Ýalňyşlyk ýüze çykdy!</h4>';
}
