game/_incl_data/class/Clan/Register.php

137 lines
4.0 KiB
PHP
Raw Normal View History

<?php
namespace Clan;
use Core\Config;
use Core\Db;
use Uploader;
class Register
{
const REGISTER_COST_EKR = 50;
private string $name;
private int $align = 0;
private Uploader $logo;
private int $newClanId = 0;
/**
* @return int
*/
public function getNewClanId(): int
{
return $this->newClanId;
}
public function __construct()
{
if (!isset($_POST['newclanname'], $_POST['newclanalign'], $_FILES['newclanlogo'])) {
return;
}
$this->setName($_POST['newclanname']);
$this->setLogo();
$this->setAlign(intval($_POST['newclanalign']));
$this->new();
}
private function hasGoodName(): bool
{
return Db::getValue('select count(id) from clan where name = ?', [$this->name]) === 0;
}
public function new()
{
if (!$this->hasGoodName()) {
return;
}
$imageId = $this->logo->saveToDb();
if (!$imageId) {
echo $this->logo::$error;
return;
}
Db::sql('insert into clan (name, align, logo) values (?,?,?)', [$this->name, $this->align, $imageId]);
$this->newClanId = Db::lastInsertId();
}
public static function approve(int $clanId)
{
Db::sql('update clan set status = 1, time_reg = unix_timestamp() where id = ?', [$clanId]);
}
public static function reject(int $clanId)
{
Db::sql('delete from clan where id = ?', [$clanId]);
}
public static function printForm(): string
{
$img = Config::img() . '/i/align';
return <<<REGISTERFORM
<style>
div.regclangrid [type=radio] {
position: absolute;
opacity: 0;
width: 0;
height: 0;
}
/* IMAGE STYLES */
div.regclangrid [type=radio] + img {
cursor: pointer;
}
/* CHECKED STYLES */
div.regclangrid [type=radio]:checked + img {
outline: 1px solid brown;
padding: 2px;
}
div.regclangrid {
display: grid;
grid-template-columns: 100px auto;
}
</style>
<form method="POST" enctype="multipart/form-data">
<div class="regclangrid">
<div><label for="clanname">Название:</label></div>
<div><input name="newclanname" id="clanname"></div>
<div><label for="clanlogo">Логотип:</label></div>
<div><input type="file" name="newclanlogo" accept="image/*" id="newclanlogo"> размер: 24×15, формат: png или gif, до 30Кб</div>
<div>Склонность:</div>
<div>
<label><input type="radio" name="newclanalign" value="0" checked><img src="$img/align0.gif" alt="none"></label>
<label><input type="radio" name="newclanalign" value="1"><img src="$img/align1.gif" alt="light"></label>
<label><input type="radio" name="newclanalign" value="3"><img src="$img/align3.gif" alt="dark"></label>
<label><input type="radio" name="newclanalign" value="7"><img src="$img/align7.gif" alt="neutral"></label>
</div>
<div></div>
<div><button type="submit">Подать заявку</button></div>
</div>
</form>
REGISTERFORM;
}
private function setName(string $name)
{
$this->name = $name;
}
private function setAlign(int $align)
{
if (!in_array($align, [0, 1, 3, 7])) {
$align = 0;
}
$this->align = $align;
}
private function setLogo()
{
$this->logo = new Uploader('newclanlogo');
$this->logo->setMaxFileSize(.03);
$this->logo->setDimensions(24, 15);
$this->logo->setExtentions(['gif', 'png']);
}
}