Initial import

This commit is contained in:
Admin Nasledstvo
2026-05-01 20:52:04 +03:00
commit ac168868ee
10028 changed files with 2337954 additions and 0 deletions
@@ -0,0 +1,187 @@
<?php
namespace app\controllers;
use app\models\CmsRoles;
use app\models\CmsRr;
use app\models\Help;
use app\models\News;
use app\models\UserSession;
use app\services\Auth;
use app\services\openid\IdServer;
class AdminGlobalController extends BaseController
{
public $layout = 'cms';
public $userTypeKey = 'admin-global';
public function beforeAction($action)
{
$admin = Auth::userAdminGlobal();
if (!$admin) {
$_SESSION['admin-global-page'] = $_SERVER['REQUEST_URI'];
return $this->redirect('/admin-global');
} else {
if ($admin && $admin->open_id) {
$us = UserSession::find()->where(['sub' => $admin->open_id])->one();
if (!$us) {
Auth::userAdminGlobalLogout();
return $this->redirect('/admin-global');
}
}
}
return parent::beforeAction($action);
}
public function actionIndex($page)
{
return $this->render('index/' . $page);
}
public function actionHelpKeysRemote($page)
{
if ($page == 'prepare-help') {
if (!empty($_GET['help_keys'])) {
if ($_GET['help_keys'] == 'active') {
$_SESSION['help_keys'] = 1;
$message = 'Ключовете за помощна информация са показани';
\Yii::$app->flash('success', $message);
}
if ($_GET['help_keys'] == 'inactive') {
if (!empty($_SESSION['help_keys'])) {
unset($_SESSION['help_keys']);
}
$message = 'Ключовете за помощна информация са скрити';
\Yii::$app->flash('success', $message);
}
echo json_encode(['success' => 1]);
exit;
}
}
if ($page == 'set-help') {
if (!empty($_POST['key'])) {
$help = Help::find()->where(['key' => $_POST['key']])->one();
if (!$help) {
$help = new Help();
$help->key = $_POST['key'];
if (!empty($_POST['title'])) {
$help->title = $_POST['title'];
}
$help->save();
}
echo json_encode(['url_to' => '/admin-global/index/help/?o=w&id=' . $help->id]);
exit;
}
echo json_encode(['error' => 1]);
exit;
}
if ($page == 'get-list') {
if (!empty($_GET['keys'])) {
$helpKeys = Help::find()->where(['IN', 'key', explode(',', $_GET['keys'])])->all();
$list = [];
foreach ($helpKeys as $helpKey) {
$list[] = ['key' => $helpKey->key, 'id' => $helpKey->id];
}
echo json_encode(['list' => $list]);
}
}
if ($page == 'get-content') {
return $this->renderPartial('help-info-content');
}
if ($page == 'delete-help') {
if (!empty($_POST['id'])) {
$help = Help::findOne($_POST['id']);
if ($help) {
$help->delete();
\Yii::$app->flash('success', 'Помощната информация беше изтрита');
echo json_encode(['url_to' => '/admin-global/index/help/']);
exit;
}
}
}
exit;
}
public function actionWebPortal($page)
{
return $this->render('web-portal/' . $page);
}
public function actionTour($page)
{
return $this->render('tour/' . $page);
}
public function actionMobileApp($page)
{
return $this->render('mobile-app/' . $page);
}
public function actionNomenclature($page)
{
return $this->render('nomenclature/' . $page);
}
public function actionNomenclatureRegister($page)
{
return $this->render('nomenclature-register/' . $page);
}
public function actionProducts($page)
{
return $this->render('products/' . $page);
}
public function actionOrders($page)
{
return $this->render('orders/' . $page);
}
public function actionUser($page)
{
return $this->render('user/' . $page);
}
public function actionRoleRights()
{
if (!empty($_GET['role_id'])) {
$roleRights = CmsRr::find()->innerJoinWith('right')->where(['role_id' => $_GET['role_id'], 'is_active' => 1])->all();
$rightsIds = [];
foreach ($roleRights as $rr) {
$rightsIds[] = $rr->right_id;
}
return $this->asJson(['rights' => $rightsIds]);
}
return $this->asJson([]);
}
public function actionLogout()
{
$admin = Auth::userAdminGlobal();
if ($admin->open_id) {
$q = "";
if (!empty($_SESSION['id_token_hint'])) {
$redirect = \Yii::$app->params['cms'] . '/partner-register-login/';
$q = '?id_token_hint=' . $_SESSION['id_token_hint'] . '&post_logout_redirect_uri=' . $redirect;
}
header('Location: ' . \Yii::$app->params['sso_logout_url'] . $q);
exit;
} else {
Auth::userAdminGlobalLogout();
return $this->redirect('/cms-admin');
}
}
public function actionCheck()
{
echo password_hash('marko84', PASSWORD_DEFAULT);
exit;
}
}
@@ -0,0 +1,76 @@
<?php
namespace app\controllers;
use app\models\RegisterObjectFiles;
use app\services\api\NomenclatureService;
use app\services\api\Sync;
use app\services\api\SyncCategory;
use app\services\JWT;
use yii\web\Controller;
class ApiController extends Controller
{
public function actionEndpoints()
{
return $this->renderPartial('endpoints');
}
public function actionCategories()
{
header('Access-Control-Allow-Origin: *');
return $this->asJson(NomenclatureService::categorySelect());
}
public function actionCategoriesTree()
{
return $this->asJson(NomenclatureService::categoryTree());
}
public function actionObjectTemplates()
{
return $this->asJson(NomenclatureService::objectTemplates($_GET['sc_id'] ?? null));
}
public function actionFilePreviewDelivery($id)
{
if ($id) {
$decode = JWT::decode($id, JWT::SECRET_KEY);
/** @var RegisterObjectFiles $objectFile */
$objectFile = RegisterObjectFiles::findOne($decode->id);
if ($objectFile) {
if ($objectFile->file_content_type == 1 && $objectFile->extension == 'pdf') {
return $this->renderPartial('pdf-preview', ['objectFile' => $objectFile]);
}
}
}
}
public function actionStreamFile($id)
{
$decode = JWT::decode($id, JWT::SECRET_KEY);
/** @var RegisterObjectFiles $objectFile */
$objectFile = RegisterObjectFiles::findOne($decode->id);
return $this->renderPartial('stream-file-content', ['objectFile' => $objectFile]);
}
public function actionRenderModel($id)
{
$decode = JWT::decode($id, JWT::SECRET_KEY);
/** @var RegisterObjectFiles $objectFile */
$objectFile = RegisterObjectFiles::findOne($decode->id);
//echo '<h1 style="color: #FFFFFF">'.$objectFile->id.'</h1>';
//exit;
if ($objectFile) {
if ($objectFile->file_content_type == 4 && $objectFile->extension == 'glb') {
return $this->renderPartial('render-3d-model', ['id' => $id]);
}
}
}
}
@@ -0,0 +1,37 @@
<?php
namespace app\controllers;
use app\services\api\SyncCategory;
use app\services\api\SyncObject;
use yii\web\Controller;
class ApiSyncController extends Controller
{
public function beforeAction($action)
{
date_default_timezone_set('Europe/Sofia');
return parent::beforeAction($action); // TODO: Change the autogenerated stub
}
public function actionMainCategory($actionType) {
return $this->asJson((new SyncCategory())->container("main_$actionType"));
}
public function actionSubCategory($actionType) {
return $this->asJson((new SyncCategory())->container("sub_$actionType"));
}
public function actionObjectTemplate($actionType) {
return $this->asJson(['type' => $actionType]);
}
public function actionSetObject() {
return $this->asJson((new SyncObject())->container('setObject'));
}
public function actionUnsetObject() {
return $this->asJson((new SyncObject())->container('unsetObject'));
}
}
@@ -0,0 +1,21 @@
<?php
namespace app\controllers;
use yii\web\Controller;
class BaseController extends Controller
{
public $layout = 'public';
public $userType = '';
public $pageTitle = '';
public $userTypeKey = '';
public function beforeAction($action)
{
date_default_timezone_set('Europe/Sofia');
return parent::beforeAction($action);
}
}
@@ -0,0 +1,461 @@
<?php
namespace app\controllers;
use app\models\DocsCms;
use app\models\FileCms;
use app\services\Auth;
use app\services\JWT;
use app\widgets\services\Image;
use yii\base\BaseObject;
class FileController extends BaseController
{
public function beforeAction($action)
{
if (!Auth::userAdminGlobal() && !Auth::userPartner()) {
exit;
}
date_default_timezone_set('Europe/Sofia');
return parent::beforeAction($action);
}
public function actionGetImg($hash)
{
$decode = JWT::decode($hash, JWT::SECRET_KEY);
$file = $_SERVER['DOCUMENT_ROOT'] . $decode->secure_img;
header('Expires: 0');
if (\Yii::$app->getRefresh()) {
header("Cache-Control: no-cache, must-revalidate");
} else {
header('Cache-Control: must-revalidate, max-age=31536000');
}
header('Pragma: public');
header('Content-Length: ' . filesize($file));
header("Content-Type: image/jpeg");
readfile($file);
exit;
}
public function actionRemoveImg()
{
$hash = $_POST['hash'];
$decode = JWT::decode($hash, JWT::SECRET_KEY);
$file = $_SERVER['DOCUMENT_ROOT'] . $decode->secure_img;
if (file_exists($file)) {
unlink($file);
return $this->asJson(['success' => true]);
}
}
public function actionUploadFileCms()
{
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$media_key = $_POST['media_key'];
/** @var \app\models\_Base | null $model_class */
$model_class = $_POST['model_class'] ?? null;
$id = $_POST['id'] ?? null;
$object_key = $_POST['object_key'];
$order_index = $_POST['order_index'];
$dir_raw = $_SERVER['DOCUMENT_ROOT'] . '/_files/raw';
$dir_media = $_SERVER['DOCUMENT_ROOT'] . '/_files/raw/' . $media_key;
$dir_object = $_SERVER['DOCUMENT_ROOT'] . '/_files/raw/' . $media_key . '/' . $object_key;
$has_article = $_POST['has_article'] ?? null;
if (!file_exists($dir_raw))
mkdir($dir_raw, 0777);
if (!file_exists($dir_media))
mkdir($dir_media, 0777);
if (!file_exists($dir_object))
mkdir($dir_object, 0777);
/* foreach ($_FILES as $id => $fileObject) {
if (!empty($fileObject)) {
$extension = pathinfo($fileObject['name'], PATHINFO_EXTENSION);
$fileCms = new FileCms();
$fileCms->media_key = $media_key;
$fileCms->object_key = $object_key;
$fileCms->file_name = $id . '_' . time() . '.' . $extension;
$fileCms->file_extension = $extension;
$fileCms->file_size = $fileObject['size'];
$fileCms->file_type = $fileObject['type'];
$fileCms->order_index = $order_index;
$fileCms->date_time = date('Y-m-d H:i:s');
if ($has_article)
$fileCms->has_article = 1;
if ($fileObject['type'] == 'image/jpeg' || $fileObject['type'] == 'image/jpg' || $fileObject['type'] == 'image/png') {
$image = new Image();
$image->crateThumb($media_key, $object_key, $fileCms->file_name, $fileCms->file_type, $fileObject['tmp_name']);
}
move_uploaded_file($fileObject['tmp_name'], $dir_object . '/' . $fileCms->file_name);
$fileCms->save();
$fileData = $fileCms->getImageData();
return $this->asJson([
'id' => $fileCms->id,
'fileType' => $fileData['type'],
'srcRaw' => $fileData['src_raw'],
'fileName' => $fileCms->file_name,
'mediaKey' => $media_key
]);
}
} */
foreach ($_FILES as $id => $fileObject) {
if (!empty($fileObject)) {
$extension = pathinfo($fileObject['name'], PATHINFO_EXTENSION);
$fileCms = new FileCms();
$fileCms->media_key = $media_key;
$fileCms->object_key = $object_key;
$fileCms->file_name = $id . '_' . time() . '.' . $extension;
$fileCms->file_extension = $extension;
$fileCms->file_size = $fileObject['size'];
$fileCms->file_type = $fileObject['type'];
$fileCms->order_index = $order_index;
$fileCms->date_time = date('Y-m-d H:i:s');
if ($has_article) $fileCms->has_article = 1;
// Resize if image
if (in_array($fileObject['type'], ['image/jpeg', 'image/jpg', 'image/png'])) {
// Load original image
switch ($fileObject['type']) {
case 'image/jpeg':
case 'image/jpg':
$srcImage = imagecreatefromjpeg($fileObject['tmp_name']);
break;
case 'image/png':
$srcImage = imagecreatefrompng($fileObject['tmp_name']);
break;
}
if ($srcImage) {
$maxWidth = 1080;
$maxHeight = 650;
$width = imagesx($srcImage);
$height = imagesy($srcImage);
// Calculate proportional size
$ratio = min($maxWidth / $width, $maxHeight / $height);
$newWidth = (int)($width * $ratio);
$newHeight = (int)($height * $ratio);
// Create new resized image
$resizedImage = imagecreatetruecolor($newWidth, $newHeight);
// Preserve transparency for PNG
if ($fileObject['type'] === 'image/png') {
imagealphablending($resizedImage, false);
imagesavealpha($resizedImage, true);
}
// Resample
imagecopyresampled($resizedImage, $srcImage, 0, 0, 0, 0,
$newWidth, $newHeight, $width, $height);
// Save resized image
switch ($fileObject['type']) {
case 'image/jpeg':
case 'image/jpg':
imagejpeg($resizedImage, $dir_object . '/' . $fileCms->file_name, 90);
break;
case 'image/png':
imagepng($resizedImage, $dir_object . '/' . $fileCms->file_name);
break;
}
imagedestroy($srcImage);
imagedestroy($resizedImage);
}
// Optional: generate thumbnail
$image = new Image();
$image->crateThumb($media_key, $object_key, $fileCms->file_name, $fileCms->file_type, $dir_object . '/' . $fileCms->file_name);
} else {
// Non-images: move file directly
move_uploaded_file($fileObject['tmp_name'], $dir_object . '/' . $fileCms->file_name);
}
$fileCms->save();
$fileData = $fileCms->getImageData();
return $this->asJson([
'id' => $fileCms->id,
'fileType' => $fileData['type'],
'srcRaw' => $fileData['src_raw'],
'fileName' => $fileCms->file_name,
'mediaKey' => $media_key
]);
}
}
exit;
}
}
public function actionDeleteFileCms()
{
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$del_ids = json_decode($_POST['del_ids']);
$files = FileCms::find()->where(['IN', 'id', $del_ids])->all();
/** @var FileCms $file */
foreach ($files as $file) {
$resolutions = $file->resolutionArray;
$resolutions[] = 'thumb';
$fileToDelete = $_SERVER['DOCUMENT_ROOT'] . "/_files/raw/$file->media_key/$file->object_key/$file->file_name";
if (file_exists($fileToDelete)) {
foreach ($resolutions as $rez) {
$readyToDelete = $_SERVER['DOCUMENT_ROOT'] . "/_files/ready/$file->media_key/$file->object_key/$rez/$file->file_name";
if (file_exists($readyToDelete)) {
unlink($readyToDelete);
}
}
unlink($fileToDelete);
$file->delete();
}
}
if (sizeof($del_ids) > 1) {
return $this->asJson(['success' => sizeof($del_ids) . ' файла бяха премахнати успешно']);
} else {
return $this->asJson(['success' => 'Файлът беше успешно премахнат']);
}
}
}
public function actionUpdateFileIndexes()
{
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$indexes = json_decode($_POST['indexes']);
foreach ($indexes as $index) {
$file = FileCms::findOne($index->id);
$file->order_index = $index->order_index;
$file->save();
}
echo json_encode(['success' => true]);
}
exit;
}
public function actionUploadCrop()
{
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$media_key = $_POST['media_key'];
$object_key = $_POST['object_key'];
$file_name = $_POST['file_name'];
$resolutions = json_decode($_POST['resolutions']);
$image = new Image();
foreach ($_FILES as $crop => $file) {
$image->createCrop($crop, $file, $media_key, $object_key);
}
$fileCms = FileCms::find()->where([
'media_key' => $media_key,
'object_key' => $object_key,
'file_name' => $file_name
])->one();
if ($fileCms) {
$resolutionArray = $fileCms->resolutionArray;
foreach ($resolutions as $resolution) {
if (!in_array($resolution, $resolutionArray)) {
$resolutionArray[] = $resolution;
}
}
$fileCms->resolutions = json_encode($resolutionArray);
$fileCms->save();
$files = [];
foreach ($resolutionArray as $r) {
$files[$r] = "/_files/ready/$media_key/$object_key/$r/$file_name";
}
echo json_encode([
'resolutions' => $resolutionArray,
'files' => $files
]);
exit;
}
}
exit;
}
public function actionDeleteCrop()
{
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$del_id = $_POST['del_id'];
$rez = $_POST['rez'];
$file = FileCms::findOne($del_id);
if ($file) {
$filePath = $_SERVER['DOCUMENT_ROOT'] . "/_files/ready/$file->media_key/$file->object_key/$rez/$file->file_name";
if (file_exists($filePath))
unlink($filePath);
$file->deleteResolution($rez);
$file->save();
$files = [];
foreach ($file->resolutionArray as $r) {
$files[$r] = "/_files/ready/$file->media_key/$file->object_key/$r/$file->file_name";
}
echo json_encode([
'resolutions' => $file->resolutionArray,
'files' => $files
]);
}
}
exit;
}
public function actionClearTemporary()
{
if (!Auth::userAdminGlobal())
exit;
$temporary = FileCms::find()
->where(['and',
['IS', 'has_article', NULL],
['<', 'date_time', date('Y-m-d H:i:s', (time() - (60 * 60 * 2)))]
]);
$rows = $temporary->all();
$media_keys = $temporary->select('media_key')->distinct()->all();
$deleted = 0;
foreach ($media_keys as $file) {
$dirRaw = $_SERVER['DOCUMENT_ROOT'] . '/_files/raw/' . $file->media_key;
$dirReady = $_SERVER['DOCUMENT_ROOT'] . '/_files/ready/' . $file->media_key;
if (file_exists($dirRaw))
system("rm -rf " . escapeshellarg($dirRaw));
if (file_exists($dirReady))
system("rm -rf " . escapeshellarg($dirReady));
}
foreach ($rows as $row) {
$row->delete();
$deleted++;
}
echo $deleted . ' temporary file(s) cleaned';
exit;
}
// DOCS
public function actionDocumentAdd()
{
if (!Auth::userAdminGlobal() && !Auth::userPartner())
exit;
$p = json_decode(\Yii::$app->request->getRawBody());
if (!empty($p->modelId) && !empty($p->modelClass) && !empty($p->documentKey)) {
/** @var \app\models\_Base $model */
$model = $p->modelClass::findOne($p->modelId);
if ($model) {
if ($model->document_key != $p->documentKey) {
$model->document_key = $p->documentKey;
$model->save();
}
}
$document = new DocsCms();
$document->document_key = $p->documentKey;
$document->save();
echo json_encode(['document_id' => $document->id]);
exit;
} else {
echo json_encode(['error' => true, 'message' => 'Missing parameter']);
}
exit;
}
public function actionDocumentUpdate()
{
if (!Auth::userAdminGlobal() && !Auth::userPartner())
exit;
$p = json_decode(\Yii::$app->request->getRawBody());
if (!empty($p->id) && !empty($p->key)) {
$document = DocsCms::findOne($p->id);
$document->{$p->key} = $p->value ?? null;
$document->save();
echo json_encode(['document_id' => $document->id]);
exit;
} else {
echo json_encode(['error' => true, 'message' => 'Missing parameter']);
}
exit;
}
public function actionDocumentFileDelete() {
if (!Auth::userAdminGlobal() && !Auth::userPartner())
exit;
$p = json_decode(\Yii::$app->request->getRawBody());
$model = DocsCms::findOne($p->id);
if($model && $model->{$p->deleteField}) {
$file = $_SERVER['DOCUMENT_ROOT']. $model->{$p->deleteField};
if(file_exists($file)) {
unlink($file);
}
$model->{$p->deleteField} = null;
$model->save();
echo json_encode(['success' => true, 'file' => $file]);
}
exit;
}
public function actionDocumentFileUpload()
{
if (!Auth::userAdminGlobal() && !Auth::userPartner())
exit;
if (!empty($_FILES['file'])) {
$file = $_FILES['file'];
$id = $_POST['id'];
$update = $_POST['update'];
$model = DocsCms::findOne($id);
$extension = pathinfo($file['name'], PATHINFO_EXTENSION);;
if ($model && $model->document_key) {
$docs = $_SERVER['DOCUMENT_ROOT'] . '/_files/docs';
$document_key = $docs . '/' . $model->document_key;
$dirId = $document_key . '/' . $model->id;
$dir = $dirId . '/' . $update;
if (!file_exists($docs))
mkdir($docs, 0777);
if (!file_exists($document_key))
mkdir($document_key, 0777);
if (!file_exists($dirId))
mkdir($dirId, 0777);
if (!file_exists($dir))
mkdir($dir, 0777);
$filePath = $dir . '/' . time() . '_' . $file['name'];
move_uploaded_file($file['tmp_name'], $filePath);
$model->{$update} = str_replace($_SERVER['DOCUMENT_ROOT'], '', $filePath);
if($update == 'file_name')
{
$model->extension = $extension;
}
if($update == 'file_name_en') {
$model->extension_en = $extension;
}
$model->save();
echo json_encode(['id' => $model->id, 'file_path' => $model->{$update}, 'update' => $update, 'extension' => $extension]);
exit;
}
}
exit;
}
public function actionDocumentDelete() {
if (!Auth::userAdminGlobal() && !Auth::userPartner())
exit;
$p = json_decode(\Yii::$app->request->getRawBody());
$model = DocsCms::findOne($p->id);
if($model) {
$dir = $_SERVER['DOCUMENT_ROOT'] . '/_files/docs/' . $model->document_key . '/' . $model->id;
if(file_exists($dir)) {
system("rm -rf " . escapeshellarg($dir));
}
$model->delete();
echo json_encode(['success']);
}
exit;
}
}
@@ -0,0 +1,27 @@
<?php
namespace app\controllers;
use app\services\Auth;
use yii\web\Controller;
class MediaController extends BaseController
{
public function beforeAction($action)
{
return parent::beforeAction($action);
}
public function actionPlay($id)
{
return $this->renderPartial('play', ['id' => $id]);
}
public function actionPlayMobile($id)
{
return $this->renderPartial('play_mobile', ['id' => $id]);
}
}
@@ -0,0 +1,98 @@
<?php
namespace app\controllers;
use app\models\CmsRr;
use app\models\UserSession;
use app\services\Auth;
use app\widgets\services\UserRight;
class PartnerController extends BaseController
{
public $layout = 'cms';
public $userTypeKey = 'partner';
public $partnerKey = '';
/** @var \app\models\UserPartner */
public $partner;
public function beforeAction($action)
{
$partner = Auth::userPartner();
if (!$partner) {
header('Location: /partner/');
exit;
} else {
if($partner && $partner->open_id) {
$us = UserSession::find()->where(['sub' => $partner->open_id])->one();
if(!$us) {
Auth::userPartnerLogout();
return $this->redirect('/partner');
}
}
}
$this->partner = $partner;
UserRight::setPartner($this->partner);
return parent::beforeAction($action);
}
public function actionIndex($page)
{
if(!$this->partner) {
$redirect = \Yii::$app->params['cms'] . '/partner-register-login/';
header('Location: '. $redirect);
exit;
}
return $this->render("index/$page");
}
public function actionWebsite($page)
{
if ($page == 'users' && !$this->partner->getRightsIds(1))
return $this->redirect('/partner/index/dashboard');
return $this->render("website/$page");
}
public function actionUser($page)
{
return $this->render("user/$page");
}
public function actionLogout()
{
$partner = Auth::userPartner();
if($partner->open_id) {
$q = "";
if(!empty($_SESSION['id_token_hint'])) {
$redirect = \Yii::$app->params['cms'] . '/partner-register-login/';
$q = '?id_token_hint='.$_SESSION['id_token_hint'].'&post_logout_redirect_uri='.$redirect;
}
header('Location: '. \Yii::$app->params['sso_logout_url'].$q);
exit;
}
exit;
}
public function actionRoleRights()
{
if (!empty($_GET['role_id'])) {
$roleRights = CmsRr::find()->innerJoinWith('right')->where(['role_id' => $_GET['role_id'], 'is_active' => 1])->all();
$rightsIds = [];
foreach ($roleRights as $rr) {
$rightsIds[] = $rr->right_id;
}
return $this->asJson(['rights' => $rightsIds]);
}
return $this->asJson([]);
}
}
@@ -0,0 +1,87 @@
<?php
namespace app\controllers;
use app\services\Auth;
use app\services\openid\OpenIdService;
use yii\web\Controller;
use app\services\Validate;
use app\models\UserAdminGlobal;
use app\models\UserPartner;
class PublicController extends BaseController
{
public function actionIndex()
{
return $this->redirect('public/partner-register-login');
}
public function actionAdminGlobalLogin()
{
//echo password_hash('cmsadmin23', PASSWORD_DEFAULT);
//exit;
if (Auth::userAdminGlobal())
return $this->redirect('/admin-global/index/dashboard');
Validate::login(UserAdminGlobal::class, function ($user) {
$default = '/admin-global/index/dashboard';
if(!empty($_SESSION['admin-global-page'])) {
$default = $_SESSION['admin-global-page'];
unset($_SESSION['admin-global-page']);
}
Auth::userAdminGlobalLogin($user->id, $default);
});
$this->userType = 'Глобален CMS администратор';
$this->pageTitle = 'Вход - Глобален CMS администратор';
$this->userTypeKey = 'admin-global';
return $this->render('login', ['idServer' => false]);
}
public function actionPartnerLogin()
{
$partner = Auth::userPartner();
if ($partner) {
return $this->redirect('/partner/index/dashboard');
}
Validate::login(UserPartner::class, function (UserPartner $user) {
Auth::userPartnerLogin($user->id, '/partner/index/dashboard');
});
$this->userType = 'Организация (Партньор)';
$this->userTypeKey = 'partner';
$this->pageTitle = \Yii::t('cms', 'Вход - Организация');
return $this->render('login', ['idServer' => true]);
}
public function actionPartnerRegisterLogin() {
$partner = Auth::userPartner();
if (!$partner) {
if(!empty($_SESSION['id_token_hint'])) {
unset($_SESSION['id_token_hint']);
}
(new OpenIdService())->authenticationServerCheckout();
} else {
return $this->redirect('/partner/index/dashboard');
}
exit;
}
public function actionAdminRegisterLogin() {
$admin = Auth::userAdminCms();
if (!$admin) {
if(!empty($_SESSION['id_token_hint'])) {
unset($_SESSION['id_token_hint']);
}
(new OpenIdService())->authenticationServerCheckout(true);
} else {
return $this->redirect('/admin-global/index/dashboard');
}
exit;
}
}
@@ -0,0 +1,15 @@
<?php
namespace app\controllers;
use yii\web\Controller;
class RegisterController extends Controller {
public function actionUpdatePartner() {
}
public function actionUpdatePartnerUser() {
}
}
@@ -0,0 +1,584 @@
<?php
namespace app\controllers;
use app\models\Categories;
use app\models\CmsRoles;
use app\models\CommonFields;
use app\models\Expositions;
use app\models\ExpositionsObjects;
use app\models\Help;
use app\models\History;
use app\models\ObjectTemplate;
use app\models\ObjectTemplateField;
use app\models\ObjectTemplates;
use app\models\Positions;
use app\models\QrValidators;
use app\models\RegisterObjects;
use app\models\settlement\Ekatte;
use app\models\UserSession;
use app\services\Auth;
use yii\base\BaseObject;
use yii\web\Controller;
class RemoteController extends Controller
{
public function beforeAction($action)
{
date_default_timezone_set('Europe/Sofia');
$global = Auth::userAdminGlobal();
$partner = Auth::userPartner();
$cms = Auth::userAdminCms();
if (!$global && !$partner && !$cms)
exit;
return parent::beforeAction($action); // TODO: Change the autogenerated stub
}
public function actionRoleRights()
{
$rights = [];
if (!empty($_GET['role_id'])) {
$role = CmsRoles::findOne($_GET['role_id']);
if ($role) {
$rights = $role->getRightsIds();
}
}
echo json_encode(['rights' => $rights]);
exit;
}
public function actionGetHistory()
{
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$table = $_POST['table'];
$id = $_POST['id'];
$data = History::find()->where(['table_name' => $table, 'history_id' => $id])
->orderBy(['date_time' => SORT_DESC])->all();
$historyData = [];
foreach ($data as $d) {
$row = [
'<div style="color: var(--base-background-dark); font-size: 14px"><b>' . ($d->action == 1 ? 'Създаване' : 'Редакция') . '</b></div>',
'<div>' . $d->user . '</div>',
'<div style="font-style: italic; color: darkorange">' . date('d.m.Y H:i', strtotime($d->date_time)) . '<div>'
];
$historyData[] = implode('', $row);
}
if (sizeof($historyData) == 0) {
$historyData[] = 'Историята е празна';
}
return $this->asJson($historyData);
}
return $this->asJson([]);
}
public function actionNewArticlePosition()
{
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (!empty($_POST['group_id'])) {
$model = Positions::findOne($_POST['group_id']);
if ($model) {
$newPosition = new Positions();
$newPosition->group_id = $model->id;
$newPosition->order_index = $model->getNextOrderIndex();
$newPosition->save();
\Yii::$app->flash('success', 'Позицията е добавена успешно');
echo json_encode(['success' => true]);
exit;
}
}
\Yii::$app->flash('error', 'Възникна грешка, позицията не беше добавена');
echo json_encode(['success' => false]);
exit;
}
exit;
}
public function actionNewSubCategory()
{
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (!empty($_POST['parent_id'])) {
$model = Categories::findOne($_POST['parent_id']);
if ($model) {
$orderIndex = 0;
if ($model->id) {
$last = Categories::find()->where(['parent_id' => $model->id])->orderBy(['order_index' => SORT_DESC])->one();
if ($last)
$orderIndex = $last->order_index;
}
$subCategory = new Categories();
$subCategory->parent_id = $model->id;
$subCategory->order_index = ++$orderIndex;
$subCategory->save();
echo json_encode([
'success' => true,
'id' => $subCategory->id,
'orderIndex' => $subCategory->order_index,
'msg' => 'Подкатегорията е добавена успешно'
]);
exit;
}
}
echo json_encode(['success' => false, 'msg' => 'Възникна грешка, подкатегорията не беше добавена']);
exit;
}
exit;
}
public function actionNewCommonFieldOption()
{
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (!empty($_POST['parent_id'])) {
$model = CommonFields::findOne($_POST['parent_id']);
if ($model) {
$orderIndex = 0;
if ($model->id) {
$last = CommonFields::find()->where(['parent_id' => $model->id])->orderBy(['order_index' => SORT_DESC])->one();
if ($last)
$orderIndex = $last->order_index;
}
$commonFieldOption = new CommonFields();
$commonFieldOption->parent_id = $model->id;
$commonFieldOption->order_index = ++$orderIndex;
$commonFieldOption->save();
echo json_encode([
'success' => true,
'id' => $commonFieldOption->id,
'orderIndex' => $commonFieldOption->order_index,
'msg' => 'Опцията е добавена успешно'
]);
exit;
}
}
echo json_encode(['success' => false, 'msg' => 'Възникна грешка, полето не беше добавено']);
exit;
}
exit;
}
public function actionNewDynamicField()
{
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (!empty($_POST['ot_id'])) {
$model = ObjectTemplate::findOne($_POST['ot_id']);
if ($model) {
$orderIndex = ObjectTemplateField::getNextOrderIndex($_POST['ot_id']);
$objectType = new ObjectTemplateField();
$objectType->ot_id = $model->id;
$objectType->order_index = $orderIndex;
$objectType->save();
echo json_encode([
'success' => true,
'id' => $objectType->id,
'orderIndex' => $objectType->order_index,
'msg' => 'Динамичното поле е добавена успешно'
]);
exit;
}
}
echo json_encode(['success' => false, 'msg' => 'Възникна грешка, динамичното поле не беше добавено']);
exit;
}
exit;
}
public function actionUpdateCategoryIndexes()
{
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
echo json_encode($_POST);
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$indexes = json_decode($_POST['indexes']);
foreach ($indexes as $index) {
$file = Categories::findOne($index->id);
$file->order_index = $index->order_index;
$file->save();
}
echo json_encode(['success' => true]);
}
exit;
}
public function actionUpdatePositionsIndexes()
{
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
echo json_encode($_POST);
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$indexes = json_decode($_POST['indexes']);
foreach ($indexes as $index) {
$position = Positions::findOne($index->id);
$position->order_index = $index->order_index;
$position->save();
}
echo json_encode(['success' => true]);
}
exit;
}
public function actionUpdateCommonFieldIndexes()
{
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
echo json_encode($_POST);
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$indexes = json_decode($_POST['indexes']);
foreach ($indexes as $index) {
$file = CommonFields::findOne($index->id);
$file->order_index = $index->order_index;
$file->save();
}
echo json_encode(['success' => true]);
}
exit;
}
public function actionUpdateDynamicFieldsIndexes()
{
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$indexes = json_decode($_POST['indexes']);
foreach ($indexes as $index) {
$model = ObjectTemplateField::findOne($index->id);
$model->order_index = $index->order_index;
$model->save();
}
echo json_encode(['success' => true]);
}
exit;
}
public function actionUpdateDynamicTypeValues()
{
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$option = null;
if (!empty($_POST['id'])) {
$option = ObjectTemplateField::findOne($_POST['id']);
if (empty($_POST['remove'])) {
//UPDATE
if (!empty($_POST['bg']))
$option->name = $_POST['bg'];
if (!empty($_POST['en']))
$option->ts_en_name = $_POST['en'];
$option->save();
} else {
//DELETE
$option->delete();
}
} //CREATE
else if (!empty($_POST['parent_id'])) {
$model = ObjectTemplateField::findOne($_POST['parent_id']);
if ($model) {
$option = new ObjectTemplateField();
if (!empty($_POST['bg']))
$option->name = $_POST['bg'];
if (!empty($_POST['en']))
$option->ts_en_name = $_POST['en'];
$option->parent_id = $_POST['parent_id'];
$option->save();
}
}
$response = ['success' => true];
if ($option) {
$response['id'] = $option->id;
}
echo json_encode($response);
}
exit;
}
public function actionGetArticlePositions()
{
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$positionGroups = [];
foreach (Positions::find()->where(['IS', 'group_id', null])->all() as $group) {
$positions = [];
foreach ($group->positions as $position) {
$positions[] = [
'id' => $position->id,
'checked' => $position->{'position_article_key'} == $_POST['article_key'],
'checked_fa' => !empty($position->{'position_article_key'})
&& $position->{'position_article_key'} != $_POST['article_key']
];
}
$positionGroups[] = [
"group" => $group->group_name,
'positions' => $positions
];
}
echo json_encode($positionGroups);
}
exit;
}
public function actionUpdateArticlePosition()
{
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$position = Positions::findOne($_POST['id']);
if ($position) {
if ($_POST['checked'] == "true") {
$position->{'position_article_key'} = $_POST['article_key'];
} else {
$position->{'position_article_key'} = NULL;
}
$position->save();
echo json_encode(['message' => 'Позицията е актуализирана успешно']);
}
}
exit;
}
public function actionUpdateListIndexes()
{
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
echo json_encode(['message' => 'Позицията е актуализирана успешно']);
}
exit;
}
public function actionGetObjectTypes()
{
//return $this->asJson(ObjectTemplate::getResponseData());
}
public function actionGetAutocompleteObjects()
{
if (!empty($_GET['q'])) {
$ekatte = RegisterObjects::find()->where(['LIKE', 'name', $_GET['q'] . '%', false])->limit(10)->all();
$data = [];
foreach ($ekatte as $row) {
$data[] = [
'object_id' => $row->id,
'name' => '<div class="flex row-result"><div class="img-container"><img src="' . $row->getMainImgFile() . '" style="height: 80px" ></div><div style="padding-top: 25px; margin-left: 10px">' . $row->name . '</div></div>',
];
}
return $this->asJson($data);
}
if (!empty($_GET['settlement_id'])) {
$row = RegisterObjects::findOne($_GET['settlement_id']);
if ($row)
return $this->asJson([
'object_id' => $row->id,
'name' => $row->name . '<img src="' . $row->getMainImgFile() . '" width="80px">',
]);
die('Wrong settlement_id');
}
die('Missing get parameter "q" or "settlement_id"');
}
public function actionGetExpositionObjects()
{
if (!empty($_GET['exposition_remote_list'])) {
$id = $_GET['exposition_remote_list'];
$exposition = Expositions::findOne($id);
$html = '';
foreach ($exposition->objectList as $expositionObject) {
$html .= '<div class="flex row-result">
<div class="img-container flex flex-center">
<img src="' . $expositionObject->object->getMainImgFile() . '" style="height: 80px">
</div>
<div style="padding-top: 30px; margin-left: 10px">
<a href="' . $expositionObject->object->getUrl() . '">' . $expositionObject->object->name . '</a>
</div>
<div onclick="removeObject(this)" data-object-id="' . $expositionObject->object_id . '"
data-exposition-id="' . $exposition->id . '" class="remove-object"><i class="la la-remove"></i></div>
</div>';
}
echo $html;
exit;
}
}
public function actionAddObjectToExposition()
{
$p = (object)$_POST;
if (!empty($p->{'exposition_id'}) && !empty($p->{'object_id'}) && empty($p->{'delete'})) {
$eo = ExpositionsObjects::find()->where(['exposition_id' => $p->{'exposition_id'}, 'object_id' => $p->{'object_id'}])->one();
if (!$eo) {
$eo = new ExpositionsObjects();
$eo->object_id = $p->{'object_id'};
$eo->exposition_id = $p->{'exposition_id'};
$eo->save();
echo json_encode(['success' => 1, 'message' => '']);
exit;
} else {
echo json_encode(['error' => 1, 'message' => 'Object already added']);
exit;
}
}
}
public function actionRemoveObjectFromExposition()
{
$p = (object)$_POST;
if (!empty($p->{'exposition_id'}) && !empty($p->{'object_id'})) {
$eo = ExpositionsObjects::find()->where(['exposition_id' => $p->{'exposition_id'}, 'object_id' => $p->{'object_id'}])->one();
if ($eo)
$eo->delete();
echo json_encode(['success' => 1, 'message' => 'Object is removed']);
exit;
}
}
public function actionCheckActivity()
{
$admin = Auth::userAdminGlobal();
if ($admin && $admin->open_id) {
$us = UserSession::find()->where(['sub' => $admin->open_id])->one();
if (!$us) {
Auth::userAdminGlobalLogout();
return $this->asJson(['status' => 'inactive', 'redirect' => '/admin-global/']);
} else {
return $this->asJson(['status' => 'active']);
}
}
$partner = Auth::userPartner();
if ($partner && $partner->open_id) {
$us = UserSession::find()->where(['sub' => $partner->open_id])->one();
if (!$us) {
Auth::userPartnerLogout();
return $this->asJson(['status' => 'inactive', 'redirect' => '/partner/']);
} else {
return $this->asJson(['status' => 'active']);
}
}
}
public function actionGetIp()
{
echo $_SERVER['REMOTE_ADDR'];
exit;
}
public function actionHelpKeysRemote($page)
{
if ($page == 'prepare-help') {
if (!empty($_GET['help_keys'])) {
if ($_GET['help_keys'] == 'active') {
$_SESSION['help_keys'] = 1;
$message = 'Ключовете за помощна информация са показани';
\Yii::$app->flash('success', $message);
}
if ($_GET['help_keys'] == 'inactive') {
if (!empty($_SESSION['help_keys'])) {
unset($_SESSION['help_keys']);
}
$message = 'Ключовете за помощна информация са скрити';
\Yii::$app->flash('success', $message);
}
echo json_encode(['success' => 1]);
exit;
}
}
if ($page == 'set-help') {
if (!empty($_POST['key'])) {
$help = Help::find()->where(['key' => $_POST['key']])->one();
if (!$help) {
$help = new Help();
$help->key = $_POST['key'];
if (!empty($_POST['title'])) {
$help->title = $_POST['title'];
}
$help->save();
}
echo json_encode(['url_to' => '/admin-global/index/help/?o=w&id=' . $help->id]);
exit;
}
echo json_encode(['error' => 1]);
exit;
}
if ($page == 'get-list') {
if (!empty($_GET['keys'])) {
$helpKeys = Help::find()->where(['IN', 'key', explode(',', $_GET['keys'])])->all();
$list = [];
foreach ($helpKeys as $helpKey) {
$list[] = ['key' => $helpKey->key, 'id' => $helpKey->id];
}
echo json_encode(['list' => $list]);
}
}
if ($page == 'get-content') {
return $this->renderPartial('help-info-content');
}
if ($page == 'delete-help') {
if (!empty($_POST['id'])) {
$help = Help::findOne($_POST['id']);
if ($help) {
$help->delete();
\Yii::$app->flash('success', 'Помощната информация беше изтрита');
echo json_encode(['url_to' => '/admin-global/index/help/']);
exit;
}
}
}
exit;
}
public function actionChangeLocale()
{
if (!empty($_POST['lg'])) {
setcookie('cookie_lg', $_POST['lg'], time() + (86400 * 1), "/");
echo json_encode(['success' => true, 'lg' => $_POST['lg']]);
exit;
}
}
public function actionUpdateQrValidators()
{
if (!empty($_POST['partner_id'])) {
$partner_id = $_POST['partner_id'];
if(!empty($_POST['validators'])) {
$validators = explode(',', $_POST['validators']);
/** @var \app\models\QrValidators[] $added */
$added = QrValidators::find()->where(['partner_id' => $partner_id])->all();
$current = [];
//delete;
foreach ($added as $v) {
$current[] = $v->user_id;
if (!in_array($v->user_id, $validators)) {
$v->delete();
}
}
//add new
foreach ($validators as $id) {
if (!in_array($id, $current)) {
$qrv = new QrValidators();
$qrv->partner_id = $partner_id;
$qrv->user_id = $id;
$qrv->save();
}
}
echo json_encode(['success' => 1]);
} else {
$added = QrValidators::find()->where(['partner_id' => $partner_id])->all();
foreach ($added as $item) {
$item->delete();
}
echo json_encode(['success' => 1]);
}
//add new
}
exit;
}
}
@@ -0,0 +1,43 @@
<?php
namespace app\controllers;
use app\extensions\XLSXReader;
use app\models\settlement\Ekatte;
use app\models\settlement\Oblast;
use app\models\settlement\Obstina;
use yii\base\BaseObject;
class SettlementsController extends BaseController
{
public function actionGetData()
{
if (!empty($_GET['q'])) {
$ekatte = Ekatte::find()->where(['LIKE', 'name', $_GET['q'] . '%', false])->limit(20)->all();
$data = [];
foreach ($ekatte as $row) {
$data[] = [
'settlement_id' => $row->id,
'name' => $row->name. ', обл.' . $row->relOblast->name . ' общ. ' . $row->relObstina->name
];
}
return $this->asJson($data);
}
if (!empty($_GET['settlement_id'])) {
$row = Ekatte::findOne($_GET['settlement_id']);
if ($row)
return $this->asJson([
'settlement_id' => $row->id,
'name' => $row->name. ', обл.' . $row->relOblast->name . ' общ. ' . $row->relObstina->name
]);
die('Wrong settlement_id');
}
die('Missing get parameter "q" or "settlement_id"');
}
}