229 lines
6.6 KiB
PHP
229 lines
6.6 KiB
PHP
<?php
|
|
|
|
|
|
namespace app\models;
|
|
|
|
use app\services\Auth;
|
|
use app\services\JWT;
|
|
|
|
/**
|
|
* Class _Base
|
|
* @package app\models
|
|
* @property $id
|
|
* @property $first_name;
|
|
* @property $last_name;
|
|
* @property $media_key
|
|
* @property $document_key
|
|
* @property $publish_date
|
|
* @property string $password
|
|
* @property boolean $is_active
|
|
*/
|
|
class _Base extends \yii\db\ActiveRecord
|
|
{
|
|
//CUSTOM FUNCTIONS
|
|
public function storePassword($password)
|
|
{
|
|
$this->password = password_hash($password, PASSWORD_DEFAULT);
|
|
}
|
|
|
|
public function verifyPassword($password)
|
|
{
|
|
return password_verify($password, $this->password);
|
|
}
|
|
|
|
public function ftDate($field, $format = 'd.m.Y')
|
|
{
|
|
if ($this->{$field})
|
|
return date($format, strtotime($this->{$field}));
|
|
return '';
|
|
}
|
|
|
|
|
|
public static function recordExists($key, $name)
|
|
{
|
|
$result = self::find()->where([$key => $name]);
|
|
if (isset($_GET['id'])) {
|
|
$result = $result->andWhere(['!=', 'id', $_GET['id']]);
|
|
}
|
|
return $result->exists();
|
|
}
|
|
|
|
public static function exists($data, $ignoreId = true)
|
|
{
|
|
$result = self::find()->where($data);
|
|
if (!empty($_GET['id']) && $ignoreId) {
|
|
$result = $result->andWhere(['!=', 'id', $_GET['id']]);
|
|
}
|
|
return $result->exists();
|
|
}
|
|
|
|
public function statusSwitch($key, $color = null, $messages = null, $disabled = null, $updateArticleTable = null)
|
|
{
|
|
if (!$messages)
|
|
$messages = ['Статусът е включен', 'Статусът беше изключен'];
|
|
if ($this->hasProperty($key)) {
|
|
$checked = $this->{$key} == 1 ? "checked" : "";
|
|
return
|
|
'<label class="switch ' . ($disabled == 'disabled' ? ' disabled ' : '') . '" ' . ($color ? 'style="--switch-color: ' . $color . '"' : "") . '>
|
|
<input type="checkbox" onchange="updateStatus(this)"
|
|
data-status="' . $this->{$key} . '" ' . $checked . '
|
|
data-article-table="' . $updateArticleTable . '"
|
|
data-key="' . $key . '" data-model="' . get_class($this) . '"
|
|
data-id="' . $this->id . '"
|
|
data-yes="' . ($messages[0] ?? "") . '"
|
|
data-no="' . ($messages[1] ?? "") . '"/>
|
|
<span class="slider round"></span>
|
|
</label>';
|
|
}
|
|
return '';
|
|
}
|
|
|
|
public function getFullName()
|
|
{
|
|
return $this->first_name . ' ' . $this->last_name;
|
|
}
|
|
|
|
|
|
public function uploadSingleImage($postName, $path)
|
|
{
|
|
if (!empty($_FILES[$postName])) {
|
|
if ($this->id) {
|
|
$id = $this->id;
|
|
} else {
|
|
$last = self::find()->orderBy(['id' => SORT_DESC])->one();
|
|
$id = $last ? ($last->id + 1) : 1;
|
|
}
|
|
$imgName = str_replace('/', '_', $path) . '_' . $id . '.jpg';
|
|
$oldPath = $_FILES[$postName]['tmp_name'];
|
|
$newPath = $_SERVER['DOCUMENT_ROOT'] . "/_uploads/$path/" . $imgName;
|
|
move_uploaded_file($oldPath, $newPath);
|
|
}
|
|
}
|
|
|
|
public function getSingleImageSrc($path)
|
|
{
|
|
$imgName = str_replace('/', '_', $path) . '_' . $this->id . '.jpg';
|
|
$file = $_SERVER['DOCUMENT_ROOT'] . "/_uploads/$path/" . $imgName;
|
|
if (file_exists($file)) {
|
|
$src = "/_uploads/$path/" . $imgName;
|
|
return JWT::encode(['secure_img' => $src], JWT::SECRET_KEY);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
|
|
public function getFiles($ready = null)
|
|
{
|
|
$images = $this->hasMany(FileCms::class, ['media_key' => 'media_key']);
|
|
$data = [];
|
|
|
|
|
|
/**
|
|
* @var $image \app\models\FileCms;
|
|
*/
|
|
foreach ($images->all() as $image) {
|
|
$data[$image->object_key][$image->order_index] = $image->getImageData($ready);
|
|
}
|
|
foreach ($data as $object_key => $files) {
|
|
ksort($files);
|
|
$data[$object_key] = $files;
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
public function getMediaKey()
|
|
{
|
|
if (empty($this->media_key)) {
|
|
return uniqid() . 'm' . time() . 'k' . rand(1, 1000);
|
|
} else {
|
|
return $this->media_key;
|
|
}
|
|
}
|
|
|
|
public function getDocumentKey()
|
|
{
|
|
if (empty($this->document_key)) {
|
|
return uniqid() . 'd' . time() . 'k' . rand(1, 1000);
|
|
} else {
|
|
return $this->document_key;
|
|
}
|
|
}
|
|
|
|
public function getPublishDate()
|
|
{
|
|
if (empty($this->publish_date)) {
|
|
return date('Y-m-d');
|
|
} else {
|
|
return $this->publish_date;
|
|
}
|
|
}
|
|
|
|
public function formatDate($date, $format = 'd.m.Y')
|
|
{
|
|
if ($date) return date($format, strtotime($date));
|
|
}
|
|
|
|
/**
|
|
* @param $object_key
|
|
* @param $rez
|
|
* @return string|null
|
|
*/
|
|
public function getSrcOfSingleImage($object_key, $rez)
|
|
{
|
|
if ($this->media_key) {
|
|
/** @var \app\models\FileCms $image */
|
|
$image = $this->hasOne(FileCms::class, ['media_key' => 'media_key'])
|
|
->where(['object_key' => $object_key])->one();
|
|
|
|
if ($image) {
|
|
$src = '/_files/ready/' . $this->media_key . '/' . $image->object_key . '/' . $rez . '/' . $image->file_name;
|
|
if (file_exists($_SERVER['DOCUMENT_ROOT'] . $src)) {
|
|
return $src;
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* @param $object_key
|
|
* @param $rez
|
|
* @return array
|
|
*/
|
|
public function getSrcOfMultipleImages($object_key, $rez)
|
|
{
|
|
$srcArr = [];
|
|
if ($this->media_key) {
|
|
$images = $this->hasMany(FileCms::class, ['media_key' => 'media_key'])
|
|
->where(['object_key' => $object_key])->all();
|
|
|
|
|
|
/** @var \app\models\FileCms $image */
|
|
foreach ($images as $image) {
|
|
if ($image) {
|
|
$src = '/_files/ready/' . $this->media_key . '/' . $image->object_key . '/' . $rez . '/' . $image->file_name;
|
|
if (file_exists($_SERVER['DOCUMENT_ROOT'] . $src)) {
|
|
$srcArr[] = $src;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return $srcArr;
|
|
}
|
|
|
|
public static function getModel()
|
|
{
|
|
if (!empty($_GET['id'])) {
|
|
return self::findOne($_GET['id']);
|
|
} else {
|
|
return new self();
|
|
}
|
|
}
|
|
|
|
public function isSelected($ids, $id)
|
|
{
|
|
return in_array($id, $ids) ? 'selected' : '';
|
|
}
|
|
}
|