Joywork/api/v2/app/src/controllers/DocsController.php
2026-05-22 21:21:54 +03:00

273 lines
7.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\v2\Controller;
class DocsController extends ApiController
{
public function getVariables($app, $get = null, $post = null, $need_return = false) {
$error = false;
$errors = [];
$section = null;
if (isset($get['section']))
$section = $get['section'];
$list = [];
if (($user_id = $_SESSION['id']) && $section) {
// Открываем соединение с БД
$mdb = $app->container->get('mysql');
$user = new \User();
$agency_id = $user->getUserAgencyID($user_id);
$source_id = (isset($get['source_id'])) ? (int)$get['source_id'] : null;
$system_vars = false;
if (isset($get['system']))
$system_vars = boolval($get['system']);
$fields = \Docs::getVariablesList($agency_id, $section, $system_vars, $source_id);
foreach ($fields as $group => $variables) {
foreach ($variables as $variable => $data) {
$field_id = $data['id'];
$list[$field_id] = [
'id' => (int)$field_id,
'label' => $data['title'],
'name' => $data['name'],
'variable' => $data['variable'],
'value' => html_entity_decode($data['value']),
'default' => $data['default'],
'author' => $data['author'],
'datetime' => date('Y-m-d H:i:s', strtotime($data['datetime'])),
'created_by' => (int)$data['created_by'],
'updated_by' => (int)$data['updated_by']
];
}
}
if ($need_return) {
return $list;
} else {
$app->response->header('Content-Type', 'application/json');
$app->response->setStatus(200);
$app->response->setBody(json_encode([
'success' => true,
'user_id' => $user_id,
'list' => $list,
], JSON_UNESCAPED_UNICODE));
$app->stop();
}
}
if ($need_return) {
return false;
} else {
$app->response->header('Content-Type', 'application/json');
$app->response->setStatus(200);
$app->response->setBody(json_encode([
'success' => false,
'user_id' => $_SESSION['id'],
'errors' => $errors,
], JSON_UNESCAPED_UNICODE));
$app->stop();
}
}
public function getValidation($app, $get = null, $post = null, $need_return = false) {
$errors = [];
$body = $app->request->getBody();
$data = json_decode(stripcslashes($body), true);
$section = null;
if (isset($get['section']))
$section = $get['section'];
$variables = null;
if (isset($data['variables']))
$variables = $data['variables'];
if (($user_id = $_SESSION['id']) && $variables) {
// Открываем соединение с БД
$mdb = $app->container->get('mysql');
$data = [];
$section_id = ($section == "objects" || $section == 1) ? 1 : ($section == "clients" || $section == 2) ? 2 : 0;
foreach ($variables as $key => $variable) {
$data['ids'][] = (int)$variable['id'];
$data['names'][] = strtolower(trim($variable['name']));
$data['sections'][] = (int)$section_id;
$data['titles'][] = trim($variable['label']);
$data['defaults'][] = trim($variable['value']);
}
$is_valid = true;
$results = \Docs::validateInnerFields($data, true);
if (isset($results['errors'])) {
if (is_array($results['errors'])) {
$errors = array_merge($errors, $results['errors']);
$is_valid = false;
}
}
if ($need_return) {
return $results;
} else {
$app->response->header('Content-Type', 'application/json');
$app->response->setStatus(200);
$app->response->setBody(json_encode([
'success' => true,
'user_id' => $user_id,
'is_valid' => $is_valid,
'errors' => $errors,
], JSON_UNESCAPED_UNICODE));
$app->stop();
}
}
if ($need_return) {
return false;
} else {
$app->response->header('Content-Type', 'application/json');
$app->response->setStatus(200);
$app->response->setBody(json_encode([
'success' => false,
'user_id' => $_SESSION['id'],
'errors' => $errors,
], JSON_UNESCAPED_UNICODE));
$app->stop();
}
}
public function getDocsList($app, $get = null, $post = null, $need_return = false) {
$errors = [];
$results = [];
if ($user_id = $_SESSION['id']) {
// Открываем соединение с БД
$mdb = $app->container->get('mysql');
if ($blanks = \Docs::getInnerBlanks()) {
$types = \Docs::getBlankTypes();
foreach ($blanks as $blank) {
$results[] = [
'id' => $blank['id'],
'name' => $blank['name'],
'type' => ((array_key_exists($blank['type'], $types)) ? $types[$blank['type']] : 'Не указан'),
'author' => $blank['author'],
'datetime' => $blank['datetime'],
];
}
}
if ($need_return) {
return $results;
} else {
$app->response->header('Content-Type', 'application/json');
$app->response->setStatus(200);
$app->response->setBody(json_encode([
'success' => true,
'user_id' => $user_id,
'list' => $results
], JSON_UNESCAPED_UNICODE));
$app->stop();
}
}
if ($need_return) {
return false;
} else {
$app->response->header('Content-Type', 'application/json');
$app->response->setStatus(200);
$app->response->setBody(json_encode([
'success' => false,
'user_id' => $_SESSION['id'],
'errors' => $errors,
], JSON_UNESCAPED_UNICODE));
$app->stop();
}
}
public function addVariables($app, $get = null, $post = null) {
$errors = [];
$body = $app->request->getBody();
$data = json_decode(stripcslashes($body), true);
$section = null;
if (isset($get['section']))
$section = $get['section'];
$list = [];
$variables = $data['variables'];
if (($user_id = $_SESSION['id']) && $section && count($variables)) {
// Открываем соединение с БД
$mdb = $app->container->get('mysql');
$user = new \User();
$agency_id = $user->getUserAgencyID($user_id);
$source_id = (isset($get['source_id'])) ? (int)$get['source_id'] : null;
$data = [];
$section_id = ($section == "objects" || $section == 1) ? 1 : ($section == "clients" || $section == 2) ? 2 : 0;
foreach ($variables as $key => $variable) {
$data['ids'][] = (int)$variable['id'];
$data['names'][] = strtolower(trim($variable['name']));
$data['sections'][] = (int)$section_id;
$data['titles'][] = trim($variable['label']);
$data['defaults'][] = trim($variable['value']);
}
if ($results = \Docs::addEditFields($data)) {
$system_vars = false;
if (isset($get['system']))
$system_vars = boolval($get['system']);
$fields = \Docs::getVariablesList($agency_id, $section, $system_vars, $source_id);
foreach ($fields as $group => $variables) {
foreach ($variables as $variable => $data) {
$field_id = $data['id'];
$list[$field_id] = [
'id' => (int)$field_id,
'label' => $data['title'],
'name' => $data['name'],
'variable' => $data['variable'],
'value' => $data['value'],
'default' => $data['default'],
'author' => $data['author'],
'datetime' => date('Y-m-d H:i:s', strtotime($data['datetime'])),
'created_by' => (int)$data['created_by'],
'updated_by' => (int)$data['updated_by']
];
}
}
} else {
$errors = array_merge($errors, $results);
}
$app->response->header('Content-Type', 'application/json');
$app->response->setStatus(200);
$app->response->setBody(json_encode([
'success' => true,
'user_id' => $user_id,
'list' => $list,
], JSON_UNESCAPED_UNICODE));
$app->stop();
}
$app->response->header('Content-Type', 'application/json');
$app->response->setStatus(200);
$app->response->setBody(json_encode([
'success' => false,
'user_id' => $_SESSION['id'],
'errors' => $errors,
], JSON_UNESCAPED_UNICODE));
$app->stop();
}
}