|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* (c) Ruben Dorado <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace SiteAnalyzer; |
|
10
|
|
|
|
|
11
|
|
|
use Exception; |
|
12
|
|
|
/** |
|
13
|
|
|
* class OptionsDAO |
|
14
|
|
|
* |
|
15
|
|
|
* @package SiteAnalyzer |
|
16
|
|
|
* @author Ruben Dorado <[email protected]> |
|
17
|
|
|
* @copyright 2018 Ruben Dorado |
|
18
|
|
|
* @license http://www.opensource.org/licenses/MIT The MIT License |
|
19
|
|
|
*/ |
|
20
|
|
|
class OptionsDAO |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
/* |
|
24
|
|
|
* @param $pdo \PDO |
|
25
|
|
|
* @param $config Configuration |
|
26
|
|
|
*/ |
|
27
|
|
|
public static function checkOptionsTable($pdo, $config) { |
|
28
|
|
|
try { |
|
29
|
|
|
$db_options_table = $config->getOptionsTableName(); |
|
30
|
|
|
$stmt = $pdo->prepare("SELECT * FROM $db_options_table WHERE 1==0"); |
|
31
|
|
|
$stmt->execute(); |
|
32
|
|
|
} catch (Exception $e) { |
|
33
|
|
|
return false; |
|
34
|
|
|
} |
|
35
|
|
|
return true; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/* |
|
39
|
|
|
* @param $pdo \PDO |
|
40
|
|
|
* @param $config Configuration |
|
41
|
|
|
* |
|
42
|
|
|
*/ |
|
43
|
|
|
public static function countOptions($pdo, $config, $id, $options = []) { |
|
44
|
|
|
$user = null; |
|
45
|
|
|
$db_options_table = $config->getOptionsTableName(); |
|
46
|
|
|
|
|
47
|
|
|
if (array_key_exists('user', $options)) { |
|
48
|
|
|
$user = $options['user']; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$stmt = $pdo->prepare("INSERT INTO $db_options_table (id, time, user) VALUES (?, ?, ?)"); |
|
52
|
|
|
$stmt->execute([$id, time(), $user]); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/* |
|
56
|
|
|
* @param $pdo \PDO |
|
57
|
|
|
* @param $config Configuration |
|
58
|
|
|
* |
|
59
|
|
|
*/ |
|
60
|
|
|
public static function findIdByTimeUser($pdo, $config, $by = []) { |
|
61
|
|
|
$resp = []; |
|
62
|
|
|
$dbtable = $config->getOptionsTableName(); |
|
63
|
|
|
$qdata = []; |
|
64
|
|
|
$tquery = []; |
|
65
|
|
|
$keySql = {'from'=>"time >= ?", |
|
|
|
|
|
|
66
|
|
|
'to'=>"time <= ?", |
|
67
|
|
|
'user'=>"user = ?" }; |
|
68
|
|
|
|
|
69
|
|
|
for ($keySql as $key => $sql){ |
|
70
|
|
|
if (array_key_exists($key, $by)) { |
|
71
|
|
|
$qdata[] = $by[$key]; |
|
72
|
|
|
$tquery[] = $sql; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$sql = "SELECT id,time,user FROM $dbtable"; |
|
77
|
|
|
if (count($tquery) > 0) { |
|
78
|
|
|
$sql = $sql." WHERE ".join(" AND ", $tquery); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$stmt = $pdo->prepare($sql); |
|
82
|
|
|
$stmt->execute($qdata); |
|
83
|
|
|
while ($row = $stmt->fetch()) { |
|
84
|
|
|
$resp[] = [$row['id'], $row['time'], $row['user']]; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $resp; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/* |
|
91
|
|
|
* @param $pdo \PDO |
|
92
|
|
|
* @param $config Configuration |
|
93
|
|
|
* |
|
94
|
|
|
*/ |
|
95
|
|
|
public static function getHitsWithOptions($pdo, $config) { |
|
96
|
|
|
$resp = []; |
|
97
|
|
|
|
|
98
|
|
|
$dbOptionsTable = $config->getOptionsTableName(); |
|
99
|
|
|
$stmt = $pdo->prepare("SELECT o.id, o.time, o.user FROM $dbOptionsTable o"); |
|
100
|
|
|
$stmt->execute(); |
|
101
|
|
|
while ($row = $stmt->fetch()) { |
|
102
|
|
|
$resp[] = ['id'=>$row[0], 'time'=>$row[1], 'user'=>$row[2]]; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $resp; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|