Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
курc.docx
Скачиваний:
0
Добавлен:
01.07.2025
Размер:
606.17 Кб
Скачать

If(item.ClassList.Contains('popupWrapBox'))

{

item.style.display = 'none';

return true;

}

}

}

var CommonView = new CommonView(),

CommonModel = new CommonModel(),

Validator = new Validator();

GroupView.prototype = CommonView;

StudentView.prototype = CommonView;

GroupModel.prototype = CommonModel;

StudentModel.prototype = CommonModel;

GroupController.prototype.Validator = Validator;

StudentController.prototype.Validator = Validator;

var GroupModel = new GroupModel(),

StudentModel = new StudentModel(),

GroupView = new GroupView(),

StudentView = new StudentView();

var Group = new GroupController({GroupModel, StudentModel}, GroupView);

var Student = new StudentController({GroupModel, StudentModel}, StudentView);

window.onload = () =>

{

Group.showAllGroupsRequest();

};

/db/main.php

<?php

require_once '../connect.php';

$db = new DatabaseController();

$response = new Response(file_get_contents('php://input'));

$validator = new Validator();

$data = $response->getIncomingData();

$action = $response->getAction();

$controllerName = $response->getTargetController();

switch ($controllerName)

{

case 'groupController':

require_once MAIN_LINK.'/db/controllers/'.$controllerName.'.php';

break;

case 'studentController':

require_once MAIN_LINK.'/db/controllers/'.$controllerName.'.php';

break;

}

if(class_exists($controllerName, false))

{

if(!empty($action))

{

$controller = new $controllerName($db, $data, $validator, $response);

if(method_exists($controller, $action))

{

$response->setData(call_user_func(array($controller, $action)));

$response->done();

}

$response->setError(101);

}

$response->setError(100);

}

$response->setError(102);

/db/controllers/controllersTemplate.php

<?php

abstract class controllersTemplate

{

protected $data = '';

protected $db = '';

protected $validator = '';

protected $response = '';

public function __construct($db, $data, $validator, $response)

{

$this->data = $data;

$this->db = $db;

$this->validator = $validator;

$this->response = $response;

}

}

/db/controllers/ groupController.php

<?php

class groupController extends controllersTemplate

{

public function getAllGroupsStudents()

{

$groups = $this->db->select('*', 'groups', '1');

$specialityKeys = $this->db->select('*', 'specialityKeys', '1');

$students = $this->db->select('s.*, gs.`groupId`', 'students `s` LEFT JOIN groupStudents gs ON s.`id` = gs.`studentId`', '1');

return ['groupsArr' => $groups, 'studentsArr' => $students, 'specialityKeys' => $specialityKeys];

}

public function getGroupById($groupId)

{

$group = [];

if(!empty($groupId))

{

$group = $this->db->select('*', 'groups', "`id` = {$groupId}")[0];

}

return ['group' => $group];

}

function checkGroupExist($groupId)

{

if(!empty($groupId))

{

$group = $this->db->select('*', 'groups', "`id` = {$groupId}");

if(!empty($group))

{

return true;

}

}

$this->response->setError(113);

}

public function createGroup()

{

$groupName = $this->data['name'];

$groupCurator = $this->data['curatorNameLast'];

$groupSpeciality = $this->data['speciality'];

$params = [];

if(empty($groupName) || !$this->validator->validateGroupName($groupName))

{

$this->response->setError(103);

}

if(empty($groupCurator) || !$this->validator->validateUserFirstLastSurName($groupCurator))

{

$this->response->setError(104);

}

if(!in_array($groupSpeciality, [1,2,3,4,5,6]))

{

$this->response->setError(105);

}

$params['name'] = $groupName;

$params['curatorNameLast'] = $groupCurator;

$params['speciality'] = $groupSpeciality;

$groupId = $this->db->insert('`groups`', $params);

return $this->getGroupById($groupId);

}

public function editGroup()

{

$groupName = $this->data['name'];

$groupCurator = $this->data['curatorNameLast'];

$groupSpeciality = $this->data['speciality'];

$groupId = $this->data['groupId'];

$params = [];

if(empty($groupId))

{

$this->response->setError(106);

}

if(empty($groupName) || !$this->validator->validateGroupName($groupName))

{

$this->response->setError(103);

}

if(empty($groupCurator) || !$this->validator->validateUserFirstLastSurName($groupCurator))

{

$this->response->setError(104);

}

if(!in_array($groupSpeciality, [1,2,3,4,5,6]))

{

$this->response->setError(105);

}

$this->checkGroupExist($groupId);

$params['name'] = $groupName;

$params['curatorNameLast'] = $groupCurator;

$params['speciality'] = $groupSpeciality;

$params['id'] = $groupId;

$this->db->update('`groups`', "`name` = ?, `curatorNameLast` = ?, `speciality` = ?", '`id` = ?', $params);

return $this->getGroupById($groupId);

}

function deleteGroup()

{

$groupId = $this->data['groupId'];

if(empty($groupId))

{

$this->response->setError(106);

}

$this->checkGroupExist($groupId);

$this->db->delete('`groups`', '`id` = ?',[$groupId]);

return true;

}

function exchangeGroupStudents()

{

$groupId = $this->data['groupId'];

$newGroupId = $this->data['newGroupId'];

$params = [];

if(empty($groupId) || empty($newGroupId))

{

$this->response->setError(106);

}

$this->checkGroupExist($groupId);

$params['groupId'] = $groupId;

$this->db->update('`groupStudents`', "`groupId` = {$newGroupId}", '`groupId` = ?', $params);

return true;

}

function emptyGroup()

{

$groupId = $this->data['groupId'];

if(empty($groupId))

{

$this->response->setError(106);

}

$this->checkGroupExist($groupId);

$this->db->delete('`groupStudents`', '`groupId` = ?',[$groupId]);

return true;

}

}

/db/controllers/ studentController.php

<?php

class studentController extends controllersTemplate

{

function getStudentById($studentId)

{

$student = [];

if(!empty($studentId))

{

$student = $this->db->select('s.*, g.id as `groupId`, g.name as `groupName`',

'students `s` left JOIN groupStudents gs ON s.`id` = gs.`studentId` left JOIN groups g on g.`id` = gs.`groupId`',

"s.`id` = {$studentId}")[0];

}

return ['student' => $student];

}

function checkStudentExist($studentId)

{

if(!empty($studentId))

{

$student = $this->db->select('*', 'students', "`id` = {$studentId}");

if(!empty($student))

{

return true;

}

}

$this->response->setError(112);

}

function createStudent()

{

$userFirstName = $this->data['firstName'];

$userLastName = $this->data['lastName'];

$userSurName = $this->data['surName'];

$userBirthday = $this->data['birtday'];

$groupId = $this->data['groupId'];

$gender = $this->data['gender'];

$params = [];

if(empty($userFirstName) || !$this->validator->validateUserFirstLastSurName($userFirstName))

{

$this->response->setError(107);

}

if(empty($userLastName) || !$this->validator->validateUserFirstLastSurName($userLastName))

{

$this->response->setError(108);

}

if(empty($userSurName) || !$this->validator->validateUserFirstLastSurName($userSurName))

{

$this->response->setError(109);

}

if(empty($userBirthday) || !$this->validator->validateBirthday($userBirthday))

{

$this->response->setError(110);

}

if(empty($groupId))

{

$this->response->setError(106);

}

$params['firstName'] = $userFirstName;

$params['lastName'] = $userLastName;

$params['surName'] = $userSurName;

$params['birtday'] = $userBirthday;

$params['gender'] = $gender;

$params['stydyYear'] = 2017;

$studentId = $this->db->insert('`students`', $params);

$params = [];

$params['studentId'] = $studentId;

$params['groupId'] = $groupId;

$this->db->insert('`groupStudents`', $params);

return $this->getStudentById($studentId);

}

function deleteStudent()

{

$studentId = $this->data['studentId'];

if(empty($studentId))

{

$this->response->setError(106);

}

$this->checkStudentExist($studentId);

$this->db->delete('`groupStudents`', '`studentId` = ?',[$studentId]);

$this->db->delete('`students`', '`id` = ?',[$studentId]);

return true;

}

function editStudent()

{

$userFirstName = $this->data['firstName'];

$userLastName = $this->data['lastName'];

$userSurName = $this->data['surName'];

$userBirthday = $this->data['birtday'];

$groupId = $this->data['groupId'];

$gender = $this->data['gender'];

$studentId = $this->data['studentId'];

$params = [];

if(empty($userFirstName) || !$this->validator->validateUserFirstLastSurName($userFirstName))

{

$this->response->setError(107);

}

if(empty($userLastName) || !$this->validator->validateUserFirstLastSurName($userLastName))

{

$this->response->setError(108);

}

if(empty($userSurName) || !$this->validator->validateUserFirstLastSurName($userSurName))

{

$this->response->setError(109);

}

if(empty($userBirthday) || !$this->validator->validateBirthday($userBirthday))

{

$this->response->setError(110);

}

if(empty($groupId))

{

$this->response->setError(106);

}

if(empty($studentId))

{

$this->response->setError(111);

}

$this->checkStudentExist($studentId);

$params['firstName'] = $userFirstName;

$params['lastName'] = $userLastName;

$params['surName'] = $userSurName;

$params['birtday'] = $userBirthday;

$params['gender'] = $gender;

$params['id'] = $studentId;

$this->db->update('`students`', "`firstName` = ?, `lastName` = ?, `surName` = ?, `birtday` = ?, `gender` = ?", '`id` = ?', $params);

$checkStudentInGroup = $this->db->select('*', 'groupStudents', "studentId = {$studentId}");

if(!empty($checkStudentInGroup))

{

$params = [];

$params['groupId'] = $groupId;

$params['studentId'] = $studentId;

$this->db->update('`groupStudents`', "`groupId` = ?", '`studentId` = ?', $params);

}

else

{

$params = [];

$params['studentId'] = $studentId;

$params['groupId'] = $groupId;

$this->db->insert('`groupStudents`', $params);

}

return $this->getStudentById($studentId);

}

}

/db/classes/ Response.php

<?php

class Response

{

private $code;

private $data;

private $incoming;

private $message = 'Success';

public function __construct($input)

{

$input = json_decode($input, true);

$this->code = '000';

$this->data = [];

$this->incoming = $input['requestInfo'];

}

public function done()

{

die(json_encode(get_object_vars($this)));

}

public function setData($data)

{

$this->data = $data;

}

public function setIncoming($info)

{

$this->incoming = $info;

}

public function getAction()

{

return $this->incoming['action'];

}

public function getResponseCode()

{

return $this->code;

}

public function getIncomingData()

{

return $this->incoming['data'];

}

public function getTargetController()

{

return $this->incoming['controller'];

}

public function setError($error)

{

$this->code = $error;

switch($error)

{

case 100: $this->message = 'Empty action'; break;

case 101: $this->message = 'Unefined action method'; break;

case 102: $this->message = 'Unefined class'; break;

case 103: $this->message = 'Empty group name'; break;

case 104: $this->message = 'Empty group curator'; break;

case 105: $this->message = 'Empty group speciality'; break;

case 106: $this->message = 'Empty group id'; break;

case 107: $this->message = 'Empty user first name'; break;

case 108: $this->message = 'Empty user last name'; break;

case 109: $this->message = 'Empty user surname'; break;

case 110: $this->message = 'Empty user birthdaty'; break;

case 111: $this->message = 'Empty student id'; break;

case 112: $this->message = 'Student not exist'; break;

case 113: $this->message = 'Group not exist'; break;

}

$this->done();

}

}

/db/classes/ DatabaseController.php

<?php

class DatabaseController

{

private $conn = '';

public function __construct() {

$this->conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

}

public function getConn() {

return $this->conn;

}

public function getParamType($param) {

$type = '';

if (is_int($param)) {

$type .= 'i';

} elseif (is_float($param)) {

$type .= 'd';

} elseif (is_string($param)) {

$type .= 's';

} else {

$type .= 'b';

}

return $type;

}

public function insert($table, $params, $pre_fields = false, $pre_marks = false) {

$types = '';

$fields = [];

$questMarks = [];

foreach ($params as $field => $param) {

$types .= $this->getParamType($param);

$fields[] = $field;

$questMarks[] = '?';

if (is_string($param)) {

unset($p);

$p = strip_tags($params[$field]);

$params[$field] = &$p;

} else {

$params[$field] = &$params[$field];

}

}

array_unshift($params, $types);

$fields = implode(',', $fields);

$questMarks = implode(',', $questMarks);

if ($pre_fields) {

$fields = $pre_fields;

}

if ($pre_marks) {

$questMarks = $pre_marks;

}

$sql = "INSERT INTO {$table} ({$fields}) VALUES ({$questMarks})";

$query = $this->conn->prepare($sql);

$ref = new ReflectionClass('mysqli_stmt');

$method = $ref->getMethod("bind_param");

$method->invokeArgs($query, $params);

$query->execute();

$meta = $query->result_metadata();

$lastId = $this->conn->insert_id;

return $lastId;

}

public function select($fields, $from, $where = false, $params = false) {

$where = empty($where) ? '1' : $where;

$sql = "SELECT {$fields} FROM {$from} WHERE {$where}";

$query = $this->conn->prepare($sql);

if (!empty($params)) {

$types = '';

foreach ($params as $field => $param) {

$types .= $this->getParamType($param);

$params[$field] = &$params[$field];

}

$ref = new ReflectionClass('mysqli_stmt');

$method = $ref->getMethod("bind_param");

array_unshift($params, $types);

$method->invokeArgs($query, $params);

}

$query->execute();

$meta = $query->result_metadata();

$params = [];

while ($field = $meta->fetch_field()) {

$params[] = &$row[$field->name];

}

call_user_func_array(array($query, 'bind_result'), $params);

$result = array();

while ($query->fetch()) {

$c = array();

foreach ($row as $key => $val) {

$c[$key] = $val;

}

$result[] = $c;

}

if ($result == null) {

$result = [];

}

$query->close();

return $result;

}

public function update($table, $set, $where, $params) {

if (isset($where) && !empty($where)) {

$sql = "UPDATE {$table} SET {$set} WHERE {$where}";

$query = $this->conn->prepare($sql);

if (!empty($params)) {

$types = '';

foreach ($params as $field => $param) {

$types .= $this->getParamType($param);

if (is_string($param)) {

unset($p);

$p = strip_tags($param);

$params[$field] = &$p;

} else {

$params[$field] = &$params[$field];

}

}

$ref = new ReflectionClass('mysqli_stmt');

$method = $ref->getMethod("bind_param");

array_unshift($params, $types);

$method->invokeArgs($query, $params);

}

$query->execute();

}

return;

}

public function delete($table, $where, $params, $what = null) {

if (isset($where) && !empty($where)) {

$sql = "DELETE {$what} FROM {$table} WHERE {$where}";

$query = $this->conn->prepare($sql);

if (false === $query) {

error_log($sql);

error_log(json_encode($params));

error_log(mysqli_error($this->conn));

}

if (!empty($params)) {

$types = '';

foreach ($params as $field => $param) {

$types .= $this->getParamType($param);

$params[$field] = &$params[$field];

}

$ref = new ReflectionClass('mysqli_stmt');

$method = $ref->getMethod("bind_param");

array_unshift($params, $types);

$method->invokeArgs($query, $params);

}

$query->execute();

}

return;

}

}