76 lines
1.9 KiB
PHP
76 lines
1.9 KiB
PHP
<?php
|
|
require_once $_SERVER["DOCUMENT_ROOT"]."/sys/widget.php";
|
|
|
|
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
|
|
|
|
class Increment {
|
|
private $mysqli;
|
|
private string $page;
|
|
function __construct($page) {
|
|
$this->mysqli = new mysqli("localhost", "increment", apache_getenv("INCREMENT_PASS"), "www_increment");
|
|
$this->mysqli->set_charset("utf8mb4");
|
|
$this->page = $page->url;
|
|
}
|
|
function get_count() : int {
|
|
$query = $this->mysqli->prepare("SELECT count FROM increments WHERE page=?");
|
|
$query->bind_param('s', $this->page);
|
|
$query->execute();
|
|
$result = $query->get_result();
|
|
if ($result->num_rows >= 1) {
|
|
$row = $result->fetch_assoc();
|
|
return intval($row['count']);
|
|
} else {
|
|
$query = $this->mysqli->prepare("INSERT INTO increments (page,count) VALUES (?,0)");
|
|
$query->bind_param('s', $this->page);
|
|
$query->execute();
|
|
return 0;
|
|
}
|
|
}
|
|
function increment() {
|
|
$query = $this->mysqli->prepare("UPDATE increments SET count=count+1 WHERE page=?");
|
|
$query->bind_param('s', $this->page);
|
|
$query->execute();
|
|
}
|
|
function handle_increment() {
|
|
if ($_SERVER['REQUEST_METHOD'] == "POST") {
|
|
if (isset($_POST['increment'])) {
|
|
$this->get_count();
|
|
$this->increment();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
class IncrementButton implements Widget {
|
|
private int $_count;
|
|
|
|
function __construct() {
|
|
$this->_count = 0;
|
|
}
|
|
|
|
static function new() {
|
|
return new IncrementButton();
|
|
}
|
|
|
|
function count($count) {
|
|
$this->_count = $count;
|
|
return $this;
|
|
}
|
|
|
|
public function html() : string {
|
|
$increments = strval($this->_count) . " increment";
|
|
if ($this->_count != 1)
|
|
$increments .= "s";
|
|
|
|
return <<<EOF
|
|
<form class="no-style-form" method="post">
|
|
<input type="hidden" name="increment" value="true" />
|
|
<button type="submit" title="Increment (like) this page">
|
|
<img class="icon" role="presentation" src="/assets/hicolor/up.png" />
|
|
<span>$increments</span>
|
|
</button>
|
|
</form>
|
|
EOF;
|
|
}
|
|
}
|
|
?>
|