init
This commit is contained in:
174
models/common.php
Normal file
174
models/common.php
Normal file
@@ -0,0 +1,174 @@
|
||||
<?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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user