forked from ornicar/dmBotPlugin
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdmBot.php
More file actions
executable file
·128 lines (103 loc) · 2.45 KB
/
Copy pathdmBot.php
File metadata and controls
executable file
·128 lines (103 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
class dmBot extends dmConfigurable
{
protected
$helper,
$baseUrl,
$pages;
public function __construct(dmHelper $helper, array $options)
{
$this->helper = $helper;
$this->configure($options);
}
public function getDefaultOptions()
{
return array(
'limit' => 20,
'only_active' => true
);
}
public function setBaseUrl($url)
{
$this->baseUrl = $url;
return $this;
}
public function init()
{
$this->pages = $this->getQuery()->fetchRecords();
return $this;
}
public function render($options = array())
{
$nbPages = $this->getNbPages();
$table = $this->helper->table($options)
->useStrip(false)
->head('#', 'Url', 'Status', 'Time');
foreach($this->getPages() as $index => $page)
{
$url = $this->getPageUrl($page);
$statusCode = $this->getPageStatusCode($page);
$table->body(
($index+1),
$this->helper->tag('span.link', array('data-status-code' => $statusCode),
$this->helper->link($url)->text($url)
),
'<span class="status"></span>',
'<span class="time"></span>'
);
}
return $table->render();
}
public function getPageUrl(DmPage $page)
{
return $this->baseUrl.'/'.$page->_getI18n('slug');
}
public function getPageStatusCode(DmPage $page)
{
$statusCode = 200;
if('main' === $page->get('module'))
{
if('error404' === $page->get('action') || !$page->get('is_active'))
{
$statusCode = 404;
}
elseif('signin' === $page->get('action') || $page->get('is_secure'))
{
$statusCode = 401;
}
}
return $statusCode;
}
public function getPages()
{
return $this->pages;
}
public function getNbPages()
{
return $this->pages->count();
}
protected function getQuery()
{
$query = dmDb::query('DmPage p')->withI18n();
if($this->getOption('limit'))
{
$query->limit($this->getOption('limit'));
}
if($this->getOption('only_active'))
{
$query->addWhere('pTranslation.is_active = ?', true);
}
foreach(array('slug', 'name', 'title') as $dbField)
{
if($this->getOption($dbField.'_pattern'))
{
$query->addWhere('pTranslation.'.$dbField.' LIKE ?', str_replace('*', '%', $this->getOption($dbField.'_pattern')));
}
}
return $query;
}
public function __toString()
{
return $this->render();
}
}