This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/ext/approval/main.php

259 lines
8.8 KiB
PHP
Raw Normal View History

2020-01-26 13:19:35 +00:00
<?php declare(strict_types=1);
2019-10-14 18:35:33 +00:00
abstract class ApprovalConfig
{
const VERSION = "ext_approval_version";
const IMAGES = "approve_images";
const COMMENTS = "approve_comments";
}
class Approval extends Extension
{
2020-01-26 13:19:35 +00:00
/** @var ApprovalTheme */
protected $theme;
2019-10-14 18:35:33 +00:00
public function onInitExt(InitExtEvent $event)
{
global $config;
$config->set_default_bool(ApprovalConfig::IMAGES, false);
$config->set_default_bool(ApprovalConfig::COMMENTS, false);
}
public function onPageRequest(PageRequestEvent $event)
{
global $page, $user;
if ($event->page_matches("approve_image") && $user->can(Permissions::APPROVE_IMAGE)) {
// Try to get the image ID
$image_id = int_escape($event->get_arg(0));
if (empty($image_id)) {
$image_id = isset($_POST['image_id']) ? $_POST['image_id'] : null;
}
if (empty($image_id)) {
throw new SCoreException("Can not approve image: No valid Image ID given.");
}
self::approve_image($image_id);
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect(make_link("post/view/" . $image_id));
}
if ($event->page_matches("disapprove_image") && $user->can(Permissions::APPROVE_IMAGE)) {
// Try to get the image ID
$image_id = int_escape($event->get_arg(0));
if (empty($image_id)) {
$image_id = isset($_POST['image_id']) ? $_POST['image_id'] : null;
}
if (empty($image_id)) {
throw new SCoreException("Can not disapprove image: No valid Image ID given.");
}
self::disapprove_image($image_id);
$page->set_mode(PageMode::REDIRECT);
2019-10-14 18:35:33 +00:00
$page->set_redirect(make_link("post/view/".$image_id));
}
}
2019-10-14 19:40:15 +00:00
public function onSetupBuilding(SetupBuildingEvent $event)
{
$this->theme->display_admin_block($event);
}
2019-10-14 18:35:33 +00:00
public function onAdminBuilding(AdminBuildingEvent $event)
{
$this->theme->display_admin_form();
}
public function onAdminAction(AdminActionEvent $event)
{
global $database, $user;
$action = $event->action;
$event->redirect = true;
2019-11-02 19:57:34 +00:00
if ($action==="approval") {
2019-10-14 18:35:33 +00:00
$approval_action = $_POST["approval_action"];
switch ($approval_action) {
case "approve_all":
$database->set_timeout(300000); // These updates can take a little bit
2019-11-02 19:57:34 +00:00
$database->execute(
$database->scoreql_to_sql(
2019-11-03 18:28:38 +00:00
"UPDATE images SET approved = SCORE_BOOL_Y, approved_by_id = :approved_by_id WHERE approved = SCORE_BOOL_N"
),
2019-10-14 18:35:33 +00:00
["approved_by_id"=>$user->id]
);
break;
case "disapprove_all":
2019-10-14 18:35:33 +00:00
$database->set_timeout(300000); // These updates can take a little bit
$database->execute($database->scoreql_to_sql(
2019-11-02 19:57:34 +00:00
"UPDATE images SET approved = SCORE_BOOL_N, approved_by_id = NULL WHERE approved = SCORE_BOOL_Y"
));
2019-10-14 18:35:33 +00:00
break;
default:
break;
}
}
}
public function onDisplayingImage(DisplayingImageEvent $event)
{
global $user, $page, $config;
2019-10-14 18:35:33 +00:00
2019-11-02 19:57:34 +00:00
if ($config->get_bool(ApprovalConfig::IMAGES) && $event->image->approved===false && !$user->can(Permissions::APPROVE_IMAGE)) {
2019-10-14 18:35:33 +00:00
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect(make_link("post/list"));
}
}
public function onPageSubNavBuilding(PageSubNavBuildingEvent $event)
{
global $user;
2019-11-02 19:57:34 +00:00
if ($event->parent=="posts") {
if ($user->can(Permissions::APPROVE_IMAGE)) {
$event->add_nav_link("posts_unapproved", new Link('/post/list/approved%3Ano/1'), "Pending Approval", null, 60);
2019-10-14 18:35:33 +00:00
}
}
}
const SEARCH_REGEXP = "/^approved:(yes|no)/";
public function onSearchTermParse(SearchTermParseEvent $event)
{
2019-10-14 19:40:15 +00:00
global $user, $database, $config;
2019-10-14 18:35:33 +00:00
2019-11-02 19:57:34 +00:00
if ($config->get_bool(ApprovalConfig::IMAGES)) {
2019-10-14 19:40:15 +00:00
$matches = [];
2019-10-14 18:35:33 +00:00
2019-10-14 19:40:15 +00:00
if (is_null($event->term) && $this->no_approval_query($event->context)) {
$event->add_querylet(new Querylet($database->scoreql_to_sql("approved = SCORE_BOOL_Y ")));
}
2019-10-14 18:35:33 +00:00
2020-01-26 16:38:26 +00:00
if (is_null($event->term)) {
return;
}
2019-10-14 19:40:15 +00:00
if (preg_match(self::SEARCH_REGEXP, strtolower($event->term), $matches)) {
if ($user->can(Permissions::APPROVE_IMAGE) && $matches[1] == "no") {
$event->add_querylet(new Querylet($database->scoreql_to_sql("approved = SCORE_BOOL_N ")));
} else {
$event->add_querylet(new Querylet($database->scoreql_to_sql("approved = SCORE_BOOL_Y ")));
}
2019-10-14 18:35:33 +00:00
}
}
}
public function onHelpPageBuilding(HelpPageBuildingEvent $event)
{
global $user, $config;
2019-10-14 18:35:33 +00:00
if ($event->key===HelpPages::SEARCH) {
if ($user->can(Permissions::APPROVE_IMAGE) && $config->get_bool(ApprovalConfig::IMAGES)) {
2019-10-14 18:35:33 +00:00
$block = new Block();
$block->header = "Approval";
$block->body = $this->theme->get_help_html();
$event->add_block($block);
}
}
}
private function no_approval_query(array $context): bool
{
foreach ($context as $term) {
if (preg_match(self::SEARCH_REGEXP, $term)) {
return false;
}
}
return true;
}
public static function approve_image($image_id)
{
global $database, $user;
2019-11-02 19:57:34 +00:00
$database->execute(
$database->scoreql_to_sql(
2019-11-03 18:28:38 +00:00
"UPDATE images SET approved = SCORE_BOOL_Y, approved_by_id = :approved_by_id WHERE id = :id AND approved = SCORE_BOOL_N"
),
2019-10-14 18:35:33 +00:00
["approved_by_id"=>$user->id, "id"=>$image_id]
);
}
public static function disapprove_image($image_id)
{
2020-01-26 13:19:35 +00:00
global $database;
2019-11-02 19:57:34 +00:00
$database->execute(
$database->scoreql_to_sql(
2019-11-03 18:28:38 +00:00
"UPDATE images SET approved = SCORE_BOOL_N, approved_by_id = NULL WHERE id = :id AND approved = SCORE_BOOL_Y"
),
["id"=>$image_id]
);
}
2019-10-14 18:35:33 +00:00
public function onImageAdminBlockBuilding(ImageAdminBlockBuildingEvent $event)
{
2019-10-14 19:40:15 +00:00
global $user, $config;
2019-11-02 19:57:34 +00:00
if ($user->can(Permissions::APPROVE_IMAGE) && $config->get_bool(ApprovalConfig::IMAGES)) {
$event->add_part($this->theme->get_image_admin_html($event->image));
2019-10-14 18:35:33 +00:00
}
}
public function onBulkActionBlockBuilding(BulkActionBlockBuildingEvent $event)
{
2019-10-14 19:40:15 +00:00
global $user, $config;
2019-10-14 18:35:33 +00:00
if ($user->can(Permissions::APPROVE_IMAGE)&& $config->get_bool(ApprovalConfig::IMAGES)) {
2019-11-02 19:57:34 +00:00
if (in_array("approved:no", $event->search_terms)) {
$event->add_action("bulk_approve_image", "Approve", "a");
} else {
$event->add_action("bulk_disapprove_image", "Disapprove");
}
2019-10-14 18:35:33 +00:00
}
}
public function onBulkAction(BulkActionEvent $event)
{
global $page, $user;
2019-10-14 18:35:33 +00:00
switch ($event->action) {
case "bulk_approve_image":
if ($user->can(Permissions::APPROVE_IMAGE)) {
$total = 0;
foreach ($event->items as $image) {
self::approve_image($image->id);
$total++;
}
$page->flash("Approved $total items");
2019-10-14 18:35:33 +00:00
}
break;
case "bulk_disapprove_image":
if ($user->can(Permissions::APPROVE_IMAGE)) {
$total = 0;
foreach ($event->items as $image) {
self::disapprove_image($image->id);
$total++;
}
$page->flash("Disapproved $total items");
}
break;
2019-10-14 18:35:33 +00:00
}
}
public function onDatabaseUpgrade(DatabaseUpgradeEvent $event)
2019-10-14 18:35:33 +00:00
{
2019-11-03 19:04:57 +00:00
global $database;
2019-10-14 18:35:33 +00:00
2019-11-03 19:04:57 +00:00
if ($this->get_version(ApprovalConfig::VERSION) < 1) {
2019-10-14 18:35:33 +00:00
$database->Execute($database->scoreql_to_sql(
"ALTER TABLE images ADD COLUMN approved SCORE_BOOL NOT NULL DEFAULT SCORE_BOOL_N"
));
$database->Execute($database->scoreql_to_sql(
"ALTER TABLE images ADD COLUMN approved_by_id INTEGER NULL"
));
$database->Execute("CREATE INDEX images_approved_idx ON images(approved)");
2019-11-03 19:04:57 +00:00
$this->set_version(ApprovalConfig::VERSION, 1);
2019-10-14 18:35:33 +00:00
}
}
}