<?php
class PageAdmin extends Page
{
public function __construct()
{
Classes::GetInstance("tpl")->cd("admin");
$this->_tplName = "index";
parent::__construct();
}
protected function BeforeAction()
{
if (!Classes::GetInstance("member")->checkAuth())
{
Classes::GetInstance("tpl")->set("errorInputData", Classes::GetInstance("member")->errorInputData);
$this->actionName = "ActionAuth";
}
}
public function ActionAuth()
{
$this->_tplName = "auth";
}
public function ActionLogout()
{
Classes::GetInstance("member")->Logout();
$this->ActionAuth();
}
public function ActionMembers()
{}
public function actionIndex($args)
{
}
public function actionMachines($args)
{
if (isset($args[0]))
{
if (($args[0] == 'del') && isset($args[1]) && ($delId = (int)$args[1]))
{
Classes::GetInstance('db')->delMachine($delId);
header("Location: ".$this->actionUri);
exit();
}
$id = (int)$args[0];
if ($id)
{
$machine = Classes::GetInstance('db')->GetMachinery($id);
}
else
{
$machine = new stdClass();
$machine->id = 0;
$machine->name = '';
$machine->description = '';
$machine->type_id = 0;
$this->tpl->set('add', 1);
}
$post = Classes::GetNew('userdata');
if ($post->int('formSubmit'))
{
$name = $post->str('name', 1);
$descr = $post->str('description', 1);
$typeId = $post->int('typeId');
if ($name && $descr && $typeId)
{
Classes::GetInstance('db')->saveMachine($id, $name, $descr, $typeId);
header("Location: " . $this->actionUri);
}
else
{
$this->tpl->set('error', 1);
$machine->name = $name;
$machine->description = $descr;
$machine->type_id = 0;
}
}
$this->tpl->set('machine', $machine);
$this->tpl->set('formShow', 1);
$this->tpl->set('typeList', Classes::GetInstance('db')->getTypeList());
}
$this->tpl->set('machines', Classes::GetInstance('db')->GetMachineryList());
$this->_tplName = "machines";
}
private function file($name)
{
if (!isset($_FILES[$name])) return null;
$file = $_FILES[$name];
if ($file['error']) return null;
if (false !== strrpos($file['name'], '.'))
{
$ext = strtolower(substr($file['name'], strrpos($file['name'], '.')+1));
}
else
{
$ext = '';
}
if (!in_array($ext, array('jpg', 'jpeg', 'gif', 'png'))) return null;
$file['ext'] = $ext;
return $file;
}
public function actionAccessorys($args)
{
if (isset($args[0]))
{
$id = (int)$args[0];
$post = Classes::GetNew('userdata');
if (($args[0] == 'del') && isset($args[1]) && ($delId = (int)$args[1]))
{
Classes::GetInstance('db')->delAccessorys($delId);
$dir = ROOT_PATH . 'files/' . $delId . '/';
Classes::GetInstance('filemanager')->rmdir($dir);
header("Location: " . $this->actionUri);
exit();
}
if ($id)
{
if (isset($args[1]) && isset($args[2]) && ($args[1] == 'del') && ($delId = (int)$args[2]))
{
Classes::GetInstance('db')->delAccess2Machine($delId);
header("Location: " . $this->actionUri . $id . '/');
exit();
}
if ($addTo = $post->int('addToMachinery'))
{
Classes::GetInstance('db')->addAccess2Machine($id, $addTo);
}
$component = Classes::GetInstance('db')->GetComponent($id);
$machineryList = Classes::GetInstance('db')->getMachineryList();
$machineryIn = Classes::GetInstance('db')->getMachineryForAccessory($id);
$count = count($machineryList);
for($i=0; $i<$count; $i++)
{
$find = false;
foreach ($machineryIn as $in)
{
if ($in->id == $machineryList[$i]->id)
{
$find = true;
}
}
if ($find)
{
unset($machineryList[$i]);
}
}
$this->tpl->set('machineryIn', $machineryIn);
$this->tpl->set('addToMachinery', $machineryList);
}
else
{
$component = new stdClass();
$component->id = 0;
$component->works_number = null;
$component->name = '';
$component->description = '';
$component->img_url = null;
$this->tpl->set('add', 1);
}
if ($post->int('formSubmit'))
{
$worksNumber = $post->int('works_number');
$name = $post->str('name', 1);
$descr = $post->str('description', 1);
$photo = $this->file('image');
if ($worksNumber && $name && $descr && ($id || $photo))
{
$id = Classes::GetInstance('db')->saveComponent($id, $worksNumber, $name, $descr, '');
$dir = ROOT_PATH . 'files/' . $id . '/';
Classes::GetInstance('filemanager')->rmdir($dir);
mkdir($dir, 0755, true);
$fileName = uniqid() . '.' . $photo['ext'];
move_uploaded_file($photo['tmp_name'], $dir . $fileName);
Classes::GetInstance('db')->saveComponent($id, $worksNumber, $name, $descr, BASE_URL . 'files/' . $id . '/' . $fileName);
header("Location: " . $this->actionUri);
}
else
{
$this->tpl->set('error', 1);
$component->works_number = $worksNumber;
$component->name = $name;
$component->description = $descr;
$component->type_id = 0;
}
}
$this->tpl->set('component', $component);
$this->tpl->set('formShow', 1);
}
$this->tpl->components = Classes::GetInstance('db')->GetComponentList();
$this->_tplName = "accessorys";
}
}
?>