1
|
|
|
<?php |
|
|
|
|
2
|
|
|
$defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1; |
3
|
|
|
|
4
|
|
|
class News extends Base { |
|
|
|
|
5
|
|
|
protected $table = 'news'; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* We allow changing the database for shared accounts across pools |
9
|
|
|
* Load the config on construct so we can assign the DB name |
10
|
|
|
* @param config array MPOS configuration |
11
|
|
|
* @return none |
|
|
|
|
12
|
|
|
**/ |
13
|
|
|
public function __construct($config) { |
14
|
|
|
$this->setConfig($config); |
15
|
|
|
$this->table = $this->config['db']['shared']['news'] . '.' . $this->table; |
|
|
|
|
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Get activation status of post |
20
|
|
|
* @param id int News ID |
21
|
|
|
* @return bool true or false |
22
|
|
|
**/ |
23
|
|
|
public function getActive($id) { |
24
|
|
|
$this->debug->append("STA " . __METHOD__, 5); |
25
|
|
|
return $this->getSingle($id, 'active', 'id'); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Switch activation status |
30
|
|
|
* @param id int News ID |
31
|
|
|
* @return bool true or false |
32
|
|
|
**/ |
33
|
|
|
public function toggleActive($id) { |
34
|
|
|
$this->debug->append("STA " . __METHOD__, 5); |
35
|
|
|
$field = array('name' => 'active', 'type' => 'i', 'value' => !$this->getActive($id)); |
36
|
|
|
return $this->updateSingle($id, $field); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get all active news |
41
|
|
|
**/ |
42
|
|
View Code Duplication |
public function getAllActive() { |
|
|
|
|
43
|
|
|
$this->debug->append("STA " . __METHOD__, 4); |
44
|
|
|
$stmt = $this->mysqli->prepare("SELECT n.*, a.username AS author FROM $this->table AS n LEFT JOIN " . $this->user->getTableName() . " AS a ON a.id = n.account_id WHERE active = 1 ORDER BY time DESC"); |
|
|
|
|
45
|
|
|
if ($stmt && $stmt->execute() && $result = $stmt->get_result()) |
46
|
|
|
return $result->fetch_all(MYSQLI_ASSOC); |
47
|
|
|
return $this->sqlError('E0040'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get all news |
52
|
|
|
**/ |
53
|
|
View Code Duplication |
public function getAll() { |
|
|
|
|
54
|
|
|
$this->debug->append("STA " . __METHOD__, 4); |
55
|
|
|
$stmt = $this->mysqli->prepare("SELECT n.*, a.username AS author FROM $this->table AS n LEFT JOIN " . $this->user->getTableName() . " AS a ON a.id = n.account_id ORDER BY time DESC"); |
56
|
|
|
if ($stmt && $stmt->execute() && $result = $stmt->get_result()) |
57
|
|
|
return $result->fetch_all(MYSQLI_ASSOC); |
58
|
|
|
return $this->sqlError('E0039'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get a specific news entry |
63
|
|
|
**/ |
64
|
|
|
public function getEntry($id) { |
65
|
|
|
$this->debug->append("STA " . __METHOD__, 4); |
66
|
|
|
$stmt = $this->mysqli->prepare("SELECT * FROM $this->table WHERE id = ?"); |
67
|
|
|
if ($stmt && $stmt->bind_param('i', $id) && $stmt->execute() && $result = $stmt->get_result()) |
68
|
|
|
return $result->fetch_assoc(); |
69
|
|
|
return $this->sqlError('E0038'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Update a news entry |
74
|
|
|
**/ |
75
|
|
|
public function updateNews($id, $header, $content, $active=0) { |
76
|
|
|
$this->debug->append("STA " . __METHOD__, 4); |
77
|
|
|
$stmt = $this->mysqli->prepare("UPDATE $this->table SET content = ?, header = ?, active = ? WHERE id = ?"); |
78
|
|
|
if ($stmt && $stmt->bind_param('ssii', $content, $header, $active, $id) && $stmt->execute() && $stmt->affected_rows == 1) |
79
|
|
|
return true; |
80
|
|
|
return $this->sqlError('E0037'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
View Code Duplication |
public function deleteNews($id) { |
|
|
|
|
84
|
|
|
$this->debug->append("STA " . __METHOD__, 4); |
85
|
|
|
if (!is_int($id)) return false; |
86
|
|
|
$stmt = $this->mysqli->prepare("DELETE FROM $this->table WHERE id = ?"); |
87
|
|
|
if ($this->checkStmt($stmt) && $stmt->bind_param('i', $id) && $stmt->execute() && $stmt->affected_rows == 1) |
88
|
|
|
return true; |
89
|
|
|
return $this->sqlError('E0036'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Add a new mews entry to the table |
94
|
|
|
* @param type string Type of the notification |
95
|
|
|
* @return bool |
96
|
|
|
**/ |
97
|
|
|
public function addNews($account_id, $aData, $active=false) { |
98
|
|
|
$this->debug->append("STA " . __METHOD__, 4); |
99
|
|
|
if (empty($aData['header'])) return false; |
100
|
|
|
if (empty($aData['content'])) return false; |
101
|
|
|
if (!is_int($account_id)) return false; |
102
|
|
|
if (@$aData['active']) $active = true; |
103
|
|
|
$stmt = $this->mysqli->prepare("INSERT INTO $this->table (account_id, header, content, active) VALUES (?,?,?,?)"); |
104
|
|
|
if ($stmt && $stmt->bind_param('issi', $account_id, $aData['header'], $aData['content'], $active) && $stmt->execute()) |
105
|
|
|
return true; |
106
|
|
|
return $this->sqlError('E0035'); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$news = new News($config); |
111
|
|
|
$news->setDebug($debug); |
112
|
|
|
$news->setMysql($mysqli); |
113
|
|
|
$news->setUser($user); |
114
|
|
|
$news->setErrorCodes($aErrorCodes); |
115
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.