2019-10-14 18:35:33 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
abstract class ApprovalConfig
|
|
|
|
{
|
|
|
|
const VERSION = "ext_approval_version";
|
|
|
|
const IMAGES = "approve_images";
|
|
|
|
const COMMENTS = "approve_comments";
|
|
|
|
}
|
|
|
|
|
|
|
|
class Approval extends Extension
|
|
|
|
{
|
|
|
|
public function onInitExt(InitExtEvent $event)
|
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
|
|
|
|
$config->set_default_bool(ApprovalConfig::IMAGES, false);
|
|
|
|
$config->set_default_bool(ApprovalConfig::COMMENTS, false);
|
|
|
|
|
|
|
|
if ($config->get_int(ApprovalConfig::VERSION) < 1) {
|
|
|
|
$this->install();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2019-11-02 04:45:28 +00:00
|
|
|
$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)
|
|
|
|
{
|
2019-10-14 19:46:42 +00:00
|
|
|
global $config;
|
|
|
|
|
2019-10-14 18:35:33 +00:00
|
|
|
$this->theme->display_admin_form();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onAdminAction(AdminActionEvent $event)
|
|
|
|
{
|
|
|
|
global $database, $user;
|
|
|
|
|
|
|
|
$action = $event->action;
|
|
|
|
$event->redirect = true;
|
|
|
|
if($action==="approval") {
|
|
|
|
$approval_action = $_POST["approval_action"];
|
|
|
|
switch ($approval_action) {
|
|
|
|
case "approve_all":
|
|
|
|
$database->set_timeout(300000); // These updates can take a little bit
|
|
|
|
$database->execute($database->scoreql_to_sql(
|
|
|
|
"UPDATE images SET approved = SCORE_BOOL_Y, approved_by_id = :approved_by_id WHERE approved = SCORE_BOOL_N"),
|
|
|
|
["approved_by_id"=>$user->id]
|
|
|
|
);
|
|
|
|
break;
|
2019-11-02 04:45:28 +00:00
|
|
|
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(
|
|
|
|
"UPDATE images SET approved = SCORE_BOOL_N, approved_by_id = NULL WHERE approved = SCORE_BOOL_Y"));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onDisplayingImage(DisplayingImageEvent $event)
|
|
|
|
{
|
2019-10-14 19:46:42 +00:00
|
|
|
global $user, $page, $config;
|
2019-10-14 18:35:33 +00:00
|
|
|
|
2019-10-14 19:46:42 +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;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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-10-14 19:40:15 +00:00
|
|
|
if($config->get_bool(ApprovalConfig::IMAGES)) {
|
|
|
|
$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
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2019-10-14 19:46:42 +00:00
|
|
|
global $user, $config;
|
2019-10-14 18:35:33 +00:00
|
|
|
if ($event->key===HelpPages::SEARCH) {
|
2019-10-14 19:46:42 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
$database->execute($database->scoreql_to_sql(
|
|
|
|
"UPDATE images SET approved = SCORE_BOOL_Y, approved_by_id = :approved_by_id WHERE id = :id AND approved = SCORE_BOOL_N"),
|
|
|
|
["approved_by_id"=>$user->id, "id"=>$image_id]
|
|
|
|
);
|
|
|
|
}
|
2019-11-02 04:45:28 +00:00
|
|
|
|
|
|
|
public static function disapprove_image($image_id)
|
|
|
|
{
|
|
|
|
global $database, $user;
|
|
|
|
|
|
|
|
$database->execute($database->scoreql_to_sql(
|
|
|
|
"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 04:45:28 +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
|
|
|
|
2019-11-02 04:45:28 +00:00
|
|
|
if ($user->can(Permissions::APPROVE_IMAGE)&& $config->get_bool(ApprovalConfig::IMAGES)) {
|
|
|
|
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 $user;
|
|
|
|
|
|
|
|
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++;
|
|
|
|
}
|
|
|
|
flash_message("Approved $total items");
|
|
|
|
}
|
|
|
|
break;
|
2019-11-02 04:45:28 +00:00
|
|
|
case "bulk_disapprove_image":
|
|
|
|
if ($user->can(Permissions::APPROVE_IMAGE)) {
|
|
|
|
$total = 0;
|
|
|
|
foreach ($event->items as $image) {
|
|
|
|
self::disapprove_image($image->id);
|
|
|
|
$total++;
|
|
|
|
}
|
|
|
|
flash_message("Disapproved $total items");
|
|
|
|
}
|
|
|
|
break;
|
2019-10-14 18:35:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private function install()
|
|
|
|
{
|
|
|
|
global $database, $config;
|
|
|
|
|
|
|
|
if ($config->get_int(ApprovalConfig::VERSION) < 1) {
|
|
|
|
$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)");
|
|
|
|
$config->set_int(ApprovalConfig::VERSION, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|