1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\helpers; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\helpers\Html; |
7
|
|
|
|
8
|
|
|
class Util |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Convert TZ |
12
|
|
|
* |
13
|
|
|
* @param string $date |
14
|
|
|
* @param string $fromTimeZone |
15
|
|
|
* @param string $toTimeZone |
16
|
|
|
* @param string $format |
17
|
|
|
* @return string |
18
|
|
|
*/ |
19
|
|
|
public static function convertTz($date, $fromTimeZone, $toTimeZone, $format = 'Y-m-d H:i:s') |
20
|
|
|
{ |
21
|
|
|
$date = new \DateTime($date, new \DateTimeZone($fromTimeZone)); |
22
|
|
|
$date->setTimeZone(new \DateTimeZone($toTimeZone)); |
23
|
|
|
|
24
|
|
|
return $date->format($format); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Clear text |
29
|
|
|
* Can use for meta tags |
30
|
|
|
* |
31
|
|
|
* @param string $text |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
public static function clearText($text) |
35
|
|
|
{ |
36
|
|
|
$text = str_replace('"', '“', $text); |
37
|
|
|
return Html::encode(html_entity_decode(strip_tags($text))); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Make page title |
42
|
|
|
* |
43
|
|
|
* @param string $title |
44
|
|
|
* @param string $appendToEnd |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
public static function makePageTitle($title = '', $appendToEnd = '') |
48
|
|
|
{ |
49
|
|
|
$title = $title ? self::clearText($title) . ' / ' : ''; |
50
|
|
|
return $title . $appendToEnd; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Collect model errors |
55
|
|
|
* |
56
|
|
|
* @param Model $model the model to be validated |
57
|
|
|
* @param mixed $attributes list of attributes that should be validated. |
58
|
|
|
* @return array the error message array indexed by the attribute IDs. |
59
|
|
|
*/ |
60
|
|
|
public static function collectModelErrors($model, $attributes = null) |
61
|
|
|
{ |
62
|
|
|
$result = []; |
63
|
|
|
if ($attributes instanceof Model) { |
64
|
|
|
// validating multiple models |
65
|
|
|
$models = func_get_args(); |
66
|
|
|
$attributes = null; |
67
|
|
|
} else { |
68
|
|
|
$models = [$model]; |
69
|
|
|
} |
70
|
|
|
/* @var $model Model */ |
71
|
|
|
foreach ($models as $model) { |
72
|
|
|
foreach ($model->getErrors() as $attribute => $errors) { |
73
|
|
|
$result[\yii\helpers\Html::getInputId($model, $attribute)] = $errors; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
return $result; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|