1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\helpers; |
4
|
|
|
|
5
|
|
|
use yii\helpers\Html; |
6
|
|
|
use yii\helpers\FileHelper; |
7
|
|
|
use yii\web\UploadedFile; |
8
|
|
|
|
9
|
|
|
class Util |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Convert TZ |
13
|
|
|
* |
14
|
|
|
* @param string $date |
15
|
|
|
* @param string $fromTimeZone |
16
|
|
|
* @param string $toTimeZone |
17
|
|
|
* @param string $format |
18
|
|
|
* @return string |
19
|
|
|
*/ |
20
|
|
|
public static function convertTz($date, $fromTimeZone, $toTimeZone, $format = 'Y-m-d H:i:s') |
21
|
|
|
{ |
22
|
|
|
$date = new \DateTime($date, new \DateTimeZone($fromTimeZone)); |
23
|
|
|
$date->setTimeZone(new \DateTimeZone($toTimeZone)); |
24
|
|
|
|
25
|
|
|
return $date->format($format); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Clear text |
30
|
|
|
* Can use for meta tags |
31
|
|
|
* |
32
|
|
|
* @param string $text |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
|
|
public static function clearText($text) |
36
|
|
|
{ |
37
|
|
|
$text = str_replace('"', '“', $text); |
38
|
|
|
return Html::encode(html_entity_decode(strip_tags($text))); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Make page title |
43
|
|
|
* |
44
|
|
|
* @param string $title |
45
|
|
|
* @param string $appendToEnd |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
public static function makePageTitle($title = '', $appendToEnd = '') |
49
|
|
|
{ |
50
|
|
|
$title = $title ? self::clearText($title) . ' / ' : ''; |
51
|
|
|
return $title . $appendToEnd; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Collect model errors |
56
|
|
|
* |
57
|
|
|
* @param Model $model the model to be validated |
58
|
|
|
* @return array the error message array indexed by the attribute IDs. |
59
|
|
|
*/ |
60
|
|
|
public static function collectModelErrors($model) |
61
|
|
|
{ |
62
|
|
|
$result = []; |
63
|
|
|
/* @var $model Model */ |
64
|
|
|
$models = [$model]; |
65
|
|
|
foreach ($models as $model) { |
66
|
|
|
foreach ($model->getErrors() as $attribute => $errors) { |
67
|
|
|
$result[Html::getInputId($model, $attribute)] = $errors; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
return $result; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Create manually UploadedFile instance by file path |
75
|
|
|
* |
76
|
|
|
* @param string $name the original name of the file being uploaded |
77
|
|
|
* @param string $file file path |
78
|
|
|
* @return UploadedFile |
79
|
|
|
*/ |
80
|
|
|
public static function makeUploadedFile($name, $file) |
81
|
|
|
{ |
82
|
|
|
$tmpFile = tempnam(sys_get_temp_dir(), 'app'); |
83
|
|
|
file_put_contents($tmpFile, file_get_contents($file)); |
84
|
|
|
|
85
|
|
|
$_FILES[$name] = [ |
86
|
|
|
'name' => pathinfo($file, PATHINFO_BASENAME), |
87
|
|
|
'tmp_name' => $tmpFile, |
88
|
|
|
'type' => FileHelper::getMimeType($tmpFile), |
89
|
|
|
'size' => filesize($tmpFile), |
90
|
|
|
'error' => 0, |
91
|
|
|
]; |
92
|
|
|
|
93
|
|
|
return UploadedFile::getInstanceByName($name); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|