|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TPageGlobalizationCharsetBehavior class file. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Brad Anderson <[email protected]> |
|
7
|
|
|
* @link https://github.com/pradosoft/prado |
|
8
|
|
|
* @license https://github.com/pradosoft/prado/blob/master/LICENSE |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Prado\Util\Behaviors; |
|
12
|
|
|
|
|
13
|
|
|
use Prado\Prado; |
|
14
|
|
|
use Prado\TPropertyValue; |
|
15
|
|
|
use Prado\Web\UI\WebControls\TMetaTag; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* TPageGlobalizationCharsetBehavior class. |
|
19
|
|
|
* |
|
20
|
|
|
* TPageGlobalizationCharsetBehavior attaches to pages and adds charset |
|
21
|
|
|
* meta to the head from globalization. If there is no globalization, |
|
22
|
|
|
* the default charset is used, 'utf-8'. |
|
23
|
|
|
* @author Brad Anderson <[email protected]> |
|
24
|
|
|
* @since 4.2.0 |
|
25
|
|
|
*/ |
|
26
|
|
|
class TPageGlobalizationCharsetBehavior extends \Prado\Util\TBehavior |
|
27
|
|
|
{ |
|
28
|
|
|
/** @var bool check the existing meta tags for the charset before adding it */ |
|
29
|
|
|
private $_checkMetaCharset = false; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* This handles the TPage.OnInitComplete event to place no-cache |
|
33
|
|
|
* meta in the head. |
|
34
|
|
|
* @return array of events as keys and methods as values |
|
35
|
|
|
*/ |
|
36
|
|
|
public function events() |
|
37
|
|
|
{ |
|
38
|
|
|
return ['OnInitComplete' => 'addCharsetMeta']; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* This method places no-cache meta in the head. |
|
43
|
|
|
* @param object $page object raising the event |
|
44
|
|
|
* @param mixed $param the parameter of the raised event |
|
45
|
|
|
*/ |
|
46
|
|
|
public function addCharsetMeta($page, $param) |
|
|
|
|
|
|
47
|
|
|
{ |
|
48
|
|
|
if ($this->getEnabled() && ($head = $page->getHead())) { |
|
49
|
|
|
$hasCharset = false; |
|
50
|
|
|
$metatags = $head->getMetaTags(); |
|
51
|
|
|
if ($this->_checkMetaCharset) { |
|
52
|
|
|
foreach ($metatags as $meta) { |
|
53
|
|
|
if (empty($meta->getHttpEquiv()) && empty($meta->getContent()) && empty($meta->getName()) && empty($meta->getScheme()) && !empty($meta->getCharset())) { |
|
54
|
|
|
$hasCharset = true; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
if (!$hasCharset) { |
|
59
|
|
|
$charset = 'utf-8'; |
|
60
|
|
|
if ($globalization = Prado::getApplication()->getGlobalization()) { |
|
61
|
|
|
$charset = $globalization->getCharset(); |
|
62
|
|
|
} |
|
63
|
|
|
$meta = new TMetaTag(); |
|
64
|
|
|
$meta->setCharset($charset); |
|
65
|
|
|
$metatags->add($meta); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return bool checks existing meta tags for no cache |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getCheckMetaCharset() |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->_checkMetaCharset; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param bool $value checks existing meta tags for no cache |
|
80
|
|
|
*/ |
|
81
|
|
|
public function setCheckMetaCharset($value) |
|
82
|
|
|
{ |
|
83
|
|
|
$this->_checkMetaCharset = TPropertyValue::ensureBoolean($value); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.