Server_AccessControl/models/common.php
2020-10-06 14:27:47 +07:00

175 lines
6.5 KiB
PHP

<?php
namespace app\models;
use Yii;
class common extends \yii\db\ActiveRecord {
public function formatName($name) {
return preg_replace('/[^a-zA-Z0-9 _-]/', '', $this->vn2latin($name, true));
}
public function vn2latin($cs, $tolower = false) {
/* Mảng chứa tất cả ký tự có dấu trong Tiếng Việt */
$marTViet = array("à", "á", "", "", "ã", "â", "", "", "", "", "", "ă",
"", "", "", "", "", "è", "é", "", "", "", "ê", "",
"ế", "", "", "",
"ì", "í", "", "", "ĩ",
"ò", "ó", "", "", "õ", "ô", "", "", "", "", "", "ơ",
"", "", "", "", "",
"ù", "ú", "", "", "ũ", "ư", "", "", "", "", "",
"", "ý", "", "", "",
"đ",
"À", "Á", "", "", "Ã", "Â", "", "", "", "", "", "Ă",
"", "", "", "", "",
"È", "É", "", "", "", "Ê", "", "", "", "", "",
"Ì", "Í", "", "", "Ĩ",
"Ò", "Ó", "", "", "Õ", "Ô", "", "", "", "", "", "Ơ", "", "", "", "", "",
"Ù", "Ú", "", "", "Ũ", "Ư", "", "", "", "", "",
"", "Ý", "", "", "",
"Đ", " ");
/* Mảng chứa tất cả ký tự không dấu tương ứng với mảng $marTViet bên trên */
$marKoDau = array("a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
"a", "a", "a", "a", "a", "a",
"e", "e", "e", "e", "e", "e", "e", "e", "e", "e", "e",
"i", "i", "i", "i", "i",
"o", "o", "o", "o", "o", "o", "o", "o", "o", "o", "o", "o",
"o", "o", "o", "o", "o",
"u", "u", "u", "u", "u", "u", "u", "u", "u", "u", "u",
"y", "y", "y", "y", "y",
"d",
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A",
"A", "A", "A", "A", "A",
"E", "E", "E", "E", "E", "E", "E", "E", "E", "E", "E",
"I", "I", "I", "I", "I",
"O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O",
"U", "U", "U", "U", "U", "U", "U", "U", "U", "U", "U",
"Y", "Y", "Y", "Y", "Y",
"D", "-");
if ($tolower) {
return strtolower(str_replace($marTViet, $marKoDau, $cs));
}
return str_replace($marTViet, $marKoDau, $cs);
}
public function formatTime($time, $format = "d/m/Y") {
// $now = time();
// $range = $now - $time;
// if ($range > 60 * 60 * 12) {
return date($format, $time);
// } else {
// return Yii::$app->formatter->asRelativeTime($time);
// }
}
//Upload
public function UploadFile($file, $fileTypes, $dir) {
$LocalPath = "data/" . $dir;
$RootFolder = Yii::getAlias('@webroot') . "/" . $LocalPath;
$name = $_FILES[$file]["name"];
$array = explode(".", $name);
$nr = count($array);
$ext = $array[$nr - 1];
$fileName = preg_replace('/[^a-zA-Z0-9 _-]/', '', $this->vn2latin($array[0], true));
$destfile = time() . "_" . $fileName . "." . $ext;
if (!empty($_FILES)) {
$tempFile = $_FILES[$file]['tmp_name'];
$targetPath = $RootFolder;
if (!file_exists($targetPath)) {
@mkdir($targetPath, 0777, true);
}
$targetFile = $targetPath . '/' . $destfile;
$targetFileLocal = $LocalPath . '/' . $destfile;
// Validate the file type
$fileParts = pathinfo($_FILES[$file]['name']);
if (in_array(strtoupper($fileParts['extension']), $fileTypes)) {
move_uploaded_file($tempFile, $targetFile);
if (file_exists($targetFile)) {
return $targetFileLocal;
} else {
return false;
}
} else {
return 2;
}
}
}
//Move file
public function MoveFile($file, $folder) {
$exp = explode("/", $file);
$t = [];
foreach ($exp as $key => $value) {
if ($key < count($exp) - 1) {
$t[] = $value;
}
}
$folderOld = implode("/", $t);
$root = \Yii::getAlias('@app') . '/web/data/uploads' . trim($folderOld);
chdir($root);
$nameArr = explode(".", basename($file));
$name = $this->formatName($nameArr[0]);
$ext = $nameArr[count($nameArr) - 1];
rename(basename($file), $name . "." . $ext);
$tempdir = $root . "/" . $name . "." . $ext;
if (file_exists($tempdir)) {
$destfile = basename($tempdir);
$dir = Yii::getAlias('@webroot') . "/data/uploads" . $folder;
$file_dir = $dir . '/' . $destfile;
if (!file_exists($dir)) {
@mkdir($dir, 0777, true);
}
$output = shell_exec("mv {$tempdir} {$file_dir}");
$link = $folder . "/" . $destfile;
} else {
$link = $file;
}
return $link;
}
//File size
public function filesize_formatted($path) {
$size = filesize($path);
$units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
$power = $size > 0 ? floor(log($size, 1024)) : 0;
return number_format($size / pow(1024, $power), 2, '.', ',') . " " . $units[$power];
}
public function getExtension($file) {
$f = basename($file);
$ls = explode(".", $file);
return strtolower($ls[count($ls) - 1]);
}
public function convertIntToTime($seconds) {
$hours = floor($seconds / 3600);
$mins = floor($seconds / 60 % 60);
$secs = floor($seconds % 60);
if ($hours > 0) {
return sprintf('%02d:%02d:%02d', $hours, $mins, $secs);
} else {
return sprintf('%02d:%02d', $mins, $secs);
}
}
public function visiblePharse($stt, $tab, $pharse) {
$visible = false;
if ($stt != 0) {
if ($stt == -1) {
if ($tab == $pharse) {
$visible = true;
}
} else {
$visible = true;
}
} else {
$visible = true;
}
return $visible;
}
}