<!doctype html>
<html lang="ru">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="/templates/css/bootstrap.min.css">
<link rel="stylesheet" href="/templates/css/font-awesome.min.css">
<link rel="stylesheet" href="/templates/css/select2.min.css">
<link rel="stylesheet" href="/templates/css/main.css">
<script src="/templates/js/jquery-3.1.1.min.js"></script>
<script src="/templates/js/bootstrap.min.js"></script>
<script src="/templates/js/select2.full.min.js"></script>
<script>
function alignCenter(elem)
{
elem.css({
position: "absolute",
left: ($(window).width() - elem.width()) / 2 + 'px',
top: ($(window).height() - elem.height()) / 2 + 'px'
});
}
$(function(){
alignCenter($('.list-action'));
$('#modal-add-save').click(function(){
var check = $('#modal-add-cruppu-number').val() == '';
var check = $('#modal-add-cruppu-name').val() == '';
var check = $('#modal-add-cruppu-id_children').val() == '';
var check = $('#modal-add-cruppu-id_educator').val() == '';
if(check){
alert('Введены не все данные');
return false;
}
$.ajax({
url: '/cruppu/add',
type: 'POST',
data: {
number: $('#modal-add-cruppu-number').val(),
name: $('#modal-add-cruppu-name').val(),
id_children: $('#modal-add-cruppu-id_children').val(),
id_educator: $('#modal-add-cruppu-id_educator').val(),
},
success: function(){
location.reload();
},
error: function(){
alert('При отправке на сервер возникла ошибка! Попытайтесь выполнить запрос позже');
}
});
});
$('.refresh').click(function(){
var id = $(this).data('id');
$.ajax({
url: '/cruppu/getcruppu',
type: 'POST',
data: {id: id},
success: function(data){
var obj = $.parseJSON(data);
$('#modal-ref-cruppu-id').val(obj.id);
$('#modal-ref-cruppu-number').val(obj.number);
$('#modal-ref-cruppu-name').val(obj.name);
$('#modal-ref-cruppu-id_children').val(obj.id_children);
$('#modal-ref-cruppu-id_educator').val(obj.id_educator);
$('#modal-ref-cruppu').modal();
}
});
});
$('#modal-ref-save').click(function(){
var check = $('#modal-ref-cruppu-number').val() == '' ||
$('#modal-ref-cruppu-name').val() == '' ||
$('#modal-ref-cruppu-id_children').val() == '' ||
$('#modal-ref-cruppu-id_educator').val() == '' ||;
if(check){
alert('Введены не все данные');
return false;
}
$.ajax({
url: '/cruppu/refresh',
type: 'POST',
data: {
id: $('#modal-ref-cruppu-id').val(),
name: $('#modal-ref-cruppu-number').val()
name: $('#modal-ref-cruppu-name').val()
name: $('#modal-ref-cruppu-id_children').val()
name: $('#modal-ref-cruppu-id_educator').val()
},
success: function(){
location.reload();
},
error: function(){
alert('При отправке на сервер возникла ошибка! Попытайтесь выполнить запрос позже');
}
});
});
$('.delete').click(function(){
var id = $(this).data('id');
$.ajax({
url: '/cruppu/delete',
type: 'POST',
data: {id: id},
success: function(){
location.reload();
},
error: function(){
alert('При отправке на сервер возникла ошибка! Попытайтесь выполнить запрос позже');
}
});
});
});
</script>
<title>Названия групп </title>
</head>
<body>
<div class="modal fade" id="modal-add-cruppu">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Добавление новой группы</h4>
</div>
<div class="modal-body">
<form class="form-horizontal" role="form">
<div class="form-group">
<label for="modal-add-cruppu-name" class="col-sm-2 control-label">Название новой группы</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="modal-add-cruppu-name" placeholder="Название группы">
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Закрыть</button>
<button type="button" class="btn btn-primary" id="modal-add-save">Сохранить</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<div class="modal fade" id="modal-ref-cruppu">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Добавление занятия</h4>
</div>
<div class="modal-body">
<form class="form-horizontal" role="form">
<input type="hidden" id="modal-ref-cruppu-id">
<div class="form-group">
<label for="modal-ref-cruppu-name" class="col-sm-2 control-label">Название группы</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="modal-ref-cruppu-name" placeholder="Название группы">
</div>
</div>
<div class="form-group">
<label for="modal-ref-cruppu-name" class="col-sm-2 control-label">Номер группы</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="modal-ref-cruppu-name" placeholder="Номер группы">
</div>
</div>
<label for="modal-ref-cruppu-id_children" class="col-sm-2 control-label">Код ребенка</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="modal-ref-cruppu-id_children" placeholder="Код ребенка">
</div>
</div>
<label for="modal-ref-cruppu-id_educator" class="col-sm-2 control-label">Код сотрудника</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="modal-ref-cruppu-id_educator" placeholder="Код сотрудника">
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Закрыть</button>
<button type="button" class="btn btn-primary" id="modal-ref-save">Сохранить</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<div class="container">
<div class="row">
<div class="row-button">
<a class="btn btn-default" href="/">На главную</a>
<button class="btn btn-success" data-toggle="modal" data-target="#modal-add-cruppu">
<i class="fa fa-plus"></i> Добавить
</button>
</div>
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>№</th>
<th>Название группы</th>
<th>Номер группы</th>
<th>Код ребенка</th>
<th>Код сотрудника</th>
<th>Действия</th>
</tr>
</thead>
<tbody>
<?php foreach($cruppus as $сruppu): ?>
<tr>
<td><?= $cruppu['id'] ?></td>
<td><?= $cruppu['number'] ?></td>
<td><?= $cruppu['name'] ?></td>
<td><?= $cruppu['id_children'] ?></td>
<td><?= $cruppu['id_educator'] ?></td>
<td>
<button class="btn btn-default btn-xs refresh" title="Изменить" data-id="<?= $cruppu['id'] ?>">
<i class="fa fa-pencil" aria-hidden="true"></i>
</button>
<button class="btn btn-default btn-xs delete" title="Удалить" data-id="<?= $cruppu['id'] ?>">
<i class="fa fa-times" aria-hidden="true"></i>
</button>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
</body>
</html>
<?php
class Cruppu
{
public static function getCruppus()
{
$db = DB::getConnection();
$query = "SELECT `id`,`number`, `name`, `id_children`, `id_educator` FROM `cruppu`";
$result = $db->query($query);
$result->setFetchMode(PDO::FETCH_ASSOC);
$cruppus = array();
$i = 0;
while($row = $result->fetch()){
$cruppus[$i]['id'] = $row['id'];
$cruppus[$i]['number'] = $row['number'];
$cruppus[$i]['name'] = $row['name'];
$cruppus[$i]['id_children'] = $row['id_children'];
$cruppus[$i]['id_educator'] = $row['id_educator'];
$i++;
}
return $cruppus;
}
public static function addCruppu($number, $name, $id_children, $id_educator )
{
$db = DB::getConnection();
$query = "INSERT INTO `cruppu` (`number`) VALUES (:number)";
$query = "INSERT INTO `cruppu` (`name`) VALUES (:name)";
$query = "INSERT INTO `cruppu` (`id_children`) VALUES (:id_children)";
$query = "INSERT INTO `cruppu` (`id_educator`) VALUES (:id_educator)";
$result = $db->prepare($query);
$result->execute(array('number' => $number));
$result->execute(array('name' => $name));
$result->execute(array('id_children' => $id_children));
$result->execute(array('id_educator' => $id_educator));
}
public static function getCruppu($id)
{
$db = DB::getConnection();
$query = "SELECT `id`,`number`,`name`,`id_children`,`id_educator` FROM `cruppu` WHERE `id` = :id";
$result = $db->prepare($query);
$result->execute(array('id' => $id));
return $result->fetch();
}
public static function refreshCruppu($id, $number, $name, $id_children, $id_educator)
{
$db = DB::getConnection();
$query = "UPDATE `cruppu` SET `number` = :number ,`name` = :name ,`id_children` = :id_children ,`id_educator` = :id_educator
WHERE `id` = :id";
$result = $db->prepare($query);
$result->execute(array('number' => $number,'id' => $id));
$result->execute(array('name' => $name,'id' => $id));
$result->execute(array('id_children' => $id_children,'id' => $id));
$result->execute(array('id_educator' => $id_educator,'id' => $id));
}
public static function deleteCruppu($id)
{
$db = DB::getConnection();
$query = "DELETE FROM `cruppu` WHERE `id` = :id";
$result = $db->prepare($query);
$result->execute(array('id' => $id));
}
}
<?php
class CruppuController
{
public function actionIndex()
{
$cruppus = Cruppu::getCruppus();
require_once ROOT . '/views/Cruppu/index.php';
}
public function actionAdd()
{
if(!empty($_POST)){
Cruppu::addCruppu($_POST['name'], $_POST['number'],$_POST['id_children'],$_POST['id_educator']);
}
}
public function actionGetCruppu()
{
if(!empty($_POST)){
$cruppu = Cruppu::getCruppu($_POST['id']);
echo json_encode($Cruppu);
}
}
public function actionRefresh()
{
if(!empty($_POST)){
Cruppu::refreshCruppu($_POST['id'] ,$_POST['name'], $_POST['number'],$_POST['id_children'],$_POST['id_educator']);
}
}
public function actionDelete()
{
if(!empty($_POST)){
Cruppu::deleteCruppu($_POST['id']);
}
}
}