1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stats\Models; |
4
|
|
|
|
5
|
|
|
use Joomla\Database\Query\LimitableInterface; |
6
|
|
|
use Joomla\Model\AbstractDatabaseModel; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Statistics database model |
10
|
|
|
* |
11
|
|
|
* @since 1.0 |
12
|
|
|
*/ |
13
|
|
|
class StatsModel extends AbstractDatabaseModel |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Loads the statistics data from the database. |
17
|
|
|
* |
18
|
|
|
* @param string $column A single column to filter on |
19
|
|
|
* |
20
|
|
|
* @return \array[] Array of data arrays. |
21
|
|
|
* |
22
|
|
|
* @since 1.0 |
23
|
|
|
* @throws \InvalidArgumentException |
24
|
|
|
*/ |
25
|
3 |
|
public function getItems($column = null) |
26
|
|
|
{ |
27
|
3 |
|
$db = $this->getDb(); |
28
|
3 |
|
$query = $db->getQuery(true); |
29
|
|
|
|
30
|
|
|
// Validate the requested column is actually in the table |
31
|
3 |
|
if ($column !== null) |
32
|
3 |
|
{ |
33
|
2 |
|
$columnList = $db->getTableColumns('#__jstats'); |
34
|
|
|
|
35
|
|
|
// The column should exist in the table and be part of the API |
36
|
2 |
|
if (!in_array($column, array_keys($columnList)) && !in_array($column, ['unique_id', 'modified'])) |
37
|
2 |
|
{ |
38
|
1 |
|
throw new \InvalidArgumentException('An invalid data source was requested.', 404); |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
return $db->setQuery( |
42
|
|
|
$query |
43
|
1 |
|
->select('*') |
44
|
1 |
|
->from($db->quoteName('#__jstats_counter_' . $column)) |
45
|
1 |
|
)->loadAssocList(); |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
$columnList = $db->getTableColumns('#__jstats'); |
49
|
1 |
|
$return = []; |
50
|
|
|
|
51
|
1 |
|
foreach (array_keys($columnList) as $column) |
52
|
|
|
{ |
53
|
|
|
// The column should exist in the table and be part of the API |
54
|
1 |
|
if (in_array($column, ['unique_id', 'modified'])) |
55
|
1 |
|
{ |
56
|
|
|
continue; |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
$return[$column] = $db->setQuery( |
60
|
1 |
|
$query->clear() |
61
|
1 |
|
->select('*') |
62
|
1 |
|
->from($db->quoteName('#__jstats_counter_' . $column)) |
63
|
1 |
|
)->loadAssocList(); |
64
|
1 |
|
} |
65
|
|
|
|
66
|
1 |
|
return $return; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Saves the given data. |
71
|
|
|
* |
72
|
|
|
* @param \stdClass $data Data object to save. |
73
|
|
|
* |
74
|
|
|
* @return void |
75
|
|
|
* |
76
|
|
|
* @since 1.0 |
77
|
|
|
*/ |
78
|
2 |
|
public function save($data) |
79
|
|
|
{ |
80
|
2 |
|
$db = $this->getDb(); |
81
|
|
|
|
82
|
|
|
// Set the modified date of the record |
83
|
2 |
|
$data->modified = (new \DateTime('now', new \DateTimeZone('UTC')))->format($db->getDateFormat()); |
84
|
|
|
|
85
|
|
|
// Check if a row exists for this unique ID and update the existing record if so |
86
|
2 |
|
$recordExists = $db->setQuery( |
87
|
2 |
|
$db->getQuery(true) |
88
|
2 |
|
->select('unique_id') |
89
|
2 |
|
->from('#__jstats') |
90
|
2 |
|
->where('unique_id = ' . $db->quote($data->unique_id)) |
91
|
2 |
|
)->loadResult(); |
92
|
|
|
|
93
|
|
|
if ($recordExists) |
94
|
2 |
|
{ |
95
|
1 |
|
$db->updateObject('#__jstats', $data, ['unique_id']); |
96
|
1 |
|
} |
97
|
|
|
else |
98
|
|
|
{ |
99
|
1 |
|
$db->insertObject('#__jstats', $data, ['unique_id']); |
|
|
|
|
100
|
|
|
} |
101
|
2 |
|
} |
102
|
|
|
} |
103
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: