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

151 lines
4.1 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 PartnersController extends ApiController
{
public function getPartners($app, $get = null, $post = null) {
//$protocol = stripos($_SERVER['SERVER_PROTOCOL'],'https') === 0 ? 'https://' : 'http://';
$protocol = (self::is_ssl()) ? 'https://' : 'http://';
$host = $protocol . $_SERVER['SERVER_NAME'];
$error = false;
$errors = [];
if ($user_id = $_SESSION['id']) {
$partners = [];
$pdo = $app->container->get('pdo');
$mdb = $app->container->get('mysql');
$app->response->header('Content-Type', 'application/json');
$app->response->setStatus(200);
$app->response->setBody(json_encode([
'success' => !$error,
'user_id' => (int)$user_id,
'count' => count($partners),
'partners' => $partners
], JSON_UNESCAPED_UNICODE));
$app->stop();
}
$app->response->header('Content-Type', 'application/json');
$app->response->setBody(json_encode([
'success' => false,
'user_id' => $_SESSION['id'],
'errors' => $errors
], JSON_UNESCAPED_UNICODE));
$app->stop();
}
public function getPartnerEmployee($app, $get = null, $post = null) {
$error = false;
$errors = [];
$id = null;
if (isset($get['id']))
$id = $get['id'];
if (!is_null($id) && ($user_id = $_SESSION['id'])) {
$mdb = $app->container->get('mysql');
$partners = new \Partners();
$employee = $partners->getEmployee($id);
$app->response->header('Content-Type', 'application/json');
$app->response->setStatus(200);
$app->response->setBody(json_encode([
'success' => !$error,
'user_id' => (int)$user_id,
'item' => $employee
], JSON_UNESCAPED_UNICODE));
$app->stop();
}
$app->response->header('Content-Type', 'application/json');
$app->response->setBody(json_encode([
'success' => false,
'user_id' => $_SESSION['id'],
'errors' => $errors
], JSON_UNESCAPED_UNICODE));
$app->stop();
}
public function getPartnersList($app, $get = null, $post = null) {
$error = false;
$errors = [];
if ($user_id = $_SESSION['id']) {
// Открываем соединение с БД
$mdb = $app->container->get('mysql');
$only_active = true;
$with_managers = false;
if ($get['filters']) {
$filters = $get['filters'];
if (isset($filters['only_active'])) {
if (boolval($filters['only_active']))
$only_active = true;
else
$only_active = false;
}
$filters = $get['filters'];
if (isset($filters['with_managers'])) {
if (boolval($filters['with_managers']))
$with_managers = true;
else
$with_managers = false;
}
}
$list = [];
$groups = [];
$partners = new \Partners();
$partners_list = $partners->getPartners($only_active, $with_managers, false);
foreach ($partners_list as $partner) {
$partner_id = (int)$partner['id'];
$groups[(int)$partner_id] = $partner['name'];
$employees_list = $partners->getEmployees($partner['id'], $only_active, $with_managers, false);
foreach ($employees_list as $employee) {
if ($partner_id == $employee['partner_id']) {
$list[] = [
'id' => (int)$employee['id'],
'name' => $employee['name'],
'position' => $employee['position'],
'group_id' => $partner_id,
];
}
}
}
$app->response->header('Content-Type', 'application/json');
$app->response->setStatus(200);
$app->response->setBody(json_encode([
'success' => !$error,
'user_id' => (int)$user_id,
'list' => $list,
'groups' => $groups
], JSON_UNESCAPED_UNICODE));
$app->stop();
}
$app->response->header('Content-Type', 'application/json');
$app->response->setBody(json_encode([
'success' => false,
'user_id' => $_SESSION['id'],
'errors' => $errors
], JSON_UNESCAPED_UNICODE));
$app->stop();
}
}