flash messages everywhere~

This commit is contained in:
Shish 2012-06-10 04:21:03 +01:00
parent 61754b8dfe
commit 239e028662
10 changed files with 50 additions and 11 deletions

View file

@ -689,6 +689,12 @@ function set_prefixed_cookie($name, $value, $time, $path) {
/**
* Set (or extend) a flash-message cookie
*
* This can optionally be done at the same time as saving a log message with log_*()
*
* Generally one should flash a message in onPageRequest and log a message wherever
* the action actually takes place (eg onWhateverElse) - but much of the time, actions
* are taken from within onPageRequest...
*/
function flash_message(/*string*/ $text, /*string*/ $type="info") {
$current = get_prefixed_cookie("flash_message");
@ -819,17 +825,31 @@ define("SCORE_LOG_NOTSET", 0);
/**
* A shorthand way to send a LogEvent
*
* When parsing a user request, a flash message should give info to the user
* When taking action, a log event should be stored by the server
* Quite often, both of these happen at once, hence log_*() having $flash
*
* $flash = null (default) - log to server only, no flash message
* $flash = true - show the message to the user as well
* $flash = "some string" - log the message, flash the string
*/
function log_msg(/*string*/ $section, /*int*/ $priority, /*string*/ $message) {
function log_msg(/*string*/ $section, /*int*/ $priority, /*string*/ $message, $flash=null) {
send_event(new LogEvent($section, $priority, $message));
if($flash === True) {
flash_message($message);
}
else if(!is_null($flash)) {
flash_message($flash);
}
}
// More shorthand ways of logging
function log_debug(/*string*/ $section, /*string*/ $message) {log_msg($section, SCORE_LOG_DEBUG, $message);}
function log_info(/*string*/ $section, /*string*/ $message) {log_msg($section, SCORE_LOG_INFO, $message);}
function log_warning(/*string*/ $section, /*string*/ $message) {log_msg($section, SCORE_LOG_WARNING, $message);}
function log_error(/*string*/ $section, /*string*/ $message) {log_msg($section, SCORE_LOG_ERROR, $message);}
function log_critical(/*string*/ $section, /*string*/ $message) {log_msg($section, SCORE_LOG_CRITICAL, $message);}
function log_debug( /*string*/ $section, /*string*/ $message, $flash=null) {log_msg($section, SCORE_LOG_DEBUG, $message, $flash);}
function log_info( /*string*/ $section, /*string*/ $message, $flash=null) {log_msg($section, SCORE_LOG_INFO, $message, $flash);}
function log_warning( /*string*/ $section, /*string*/ $message, $flash=null) {log_msg($section, SCORE_LOG_WARNING, $message, $flash);}
function log_error( /*string*/ $section, /*string*/ $message, $flash=null) {log_msg($section, SCORE_LOG_ERROR, $message, $flash);}
function log_critical(/*string*/ $section, /*string*/ $message, $flash=null) {log_msg($section, SCORE_LOG_CRITICAL, $message, $flash);}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\

View file

@ -101,9 +101,12 @@ class AdminPage extends Extension {
assert(strlen($query) > 1);
log_warning("admin", "Mass deleting: $query");
$count = 0;
foreach(Image::find_images(0, 1000000, Tag::explode($query)) as $image) {
send_event(new ImageDeletionEvent($image));
$count++;
}
log_debug("admin", "Deleted $count images", true);
$page->set_mode("redirect");
$page->set_redirect(make_link("post/list"));
@ -113,6 +116,7 @@ class AdminPage extends Extension {
private function lowercase_all_tags() {
global $database;
$database->execute("UPDATE tags SET tag=lower(tag)");
log_warning("admin", "Set all tags to lowercase", true);
return true;
}
@ -126,6 +130,7 @@ class AdminPage extends Extension {
)
");
$database->Execute("DELETE FROM tags WHERE count=0");
log_warning("admin", "Re-counted tags", true);
return true;
}

View file

@ -46,7 +46,7 @@ class AliasEditor extends Extension {
if($user->can("manage_alias_list")) {
if(isset($_POST['oldtag'])) {
$database->execute("DELETE FROM aliases WHERE oldtag=:oldtag", array("oldtag" => $_POST['oldtag']));
log_info("alias_editor", "Deleted alias for ".$_POST['oldtag']);
log_info("alias_editor", "Deleted alias for ".$_POST['oldtag'], true);
$page->set_mode("redirect");
$page->set_redirect(make_link("alias/list"));
@ -87,6 +87,7 @@ class AliasEditor extends Extension {
$tmp = $_FILES['alias_file']['tmp_name'];
$contents = file_get_contents($tmp);
$this->add_alias_csv($database, $contents);
log_info("alias_editor", "Imported aliases from file", true); # FIXME: how many?
$page->set_mode("redirect");
$page->set_redirect(make_link("alias/list"));
}
@ -109,7 +110,7 @@ class AliasEditor extends Extension {
}
else {
$database->execute("INSERT INTO aliases(oldtag, newtag) VALUES(:oldtag, :newtag)", $pair);
log_info("alias_editor", "Added alias for {$event->oldtag} -> {$event->newtag}");
log_info("alias_editor", "Added alias for {$event->oldtag} -> {$event->newtag}", true);
}
}

View file

@ -140,6 +140,7 @@ class CommentList extends Extension {
// FIXME: post, not args
if($event->count_args() === 3) {
send_event(new CommentDeletionEvent($event->get_arg(1)));
flash_message("Deleted comment");
$page->set_mode("redirect");
if(!empty($_SERVER['HTTP_REFERER'])) {
$page->set_redirect($_SERVER['HTTP_REFERER']);
@ -163,6 +164,7 @@ class CommentList extends Extension {
foreach($cids as $cid) {
send_event(new CommentDeletionEvent($cid));
}
flash_message("Deleted $num comments");
$page->set_mode("redirect");
$page->set_redirect(make_link("admin"));

View file

@ -98,6 +98,7 @@ class ExtManager extends Extension {
if($event->get_arg(0) == "set" && $user->check_auth_token()) {
if(is_writable("data/config")) {
$this->set_things($_POST);
log_warning("ext_manager", "Active extensions changed", true);
$page->set_mode("redirect");
$page->set_redirect(make_link("ext_manager"));
}

View file

@ -60,7 +60,14 @@ class Favorites extends Extension {
if($event->page_matches("change_favorite") && !$user->is_anonymous() && $user->check_auth_token()) {
$image_id = int_escape($_POST['image_id']);
if((($_POST['favorite_action'] == "set") || ($_POST['favorite_action'] == "unset")) && ($image_id > 0)) {
send_event(new FavoriteSetEvent($image_id, $user, ($_POST['favorite_action'] == "set")));
if($_POST['favorite_action'] == "set") {
send_event(new FavoriteSetEvent($image_id, $user, true));
log_debug("favourite", "Favourite set for $image_id", "Favourite added");
}
else {
send_event(new FavoriteSetEvent($image_id, $user, false));
log_debug("favourite", "Favourite removed for $image_id", "Favourite removed");
}
}
$page->set_mode("redirect");
$page->set_redirect(make_link("post/view/$image_id"));

View file

@ -33,6 +33,7 @@ class Featured extends Extension {
$id = int_escape($_POST['image_id']);
if($id > 0) {
$config->set_int("featured_id", $id);
log_info("featured", "Featured image set to $id", "Featured image set");
$page->set_mode("redirect");
$page->set_redirect(make_link("post/view/$id"));
}

View file

@ -174,7 +174,7 @@ class NumericScore extends Extension {
public function onNumericScoreSet(NumericScoreSetEvent $event) {
global $user;
log_debug("numeric_score", "Rated Image #{$event->image_id} as {$event->score}");
log_debug("numeric_score", "Rated Image #{$event->image_id} as {$event->score}", true);
$this->add_vote($event->image_id, $user->id, $event->score);
}

View file

@ -130,7 +130,7 @@ class PrivMsg extends Extension {
else if(($pm["to_id"] == $user->id) || $user->can("view_other_pms")) {
$database->execute("DELETE FROM private_message WHERE id = :id", array("id" => $pm_id));
$database->cache->delete("pm-count-{$user->id}");
log_info("pm", "Deleted PM #$pm_id");
log_info("pm", "Deleted PM #$pm_id", "PM deleted");
$page->set_mode("redirect");
$page->set_redirect($_SERVER["HTTP_REFERER"]);
}
@ -143,6 +143,7 @@ class PrivMsg extends Extension {
$subject = $_POST["subject"];
$message = $_POST["message"];
send_event(new SendPMEvent(new PM($from_id, $_SERVER["REMOTE_ADDR"], $to_id, $subject, $message)));
flash_message("PM sent");
$page->set_mode("redirect");
$page->set_redirect($_SERVER["HTTP_REFERER"]);
}

View file

@ -184,6 +184,7 @@ class Setup extends Extension {
if($event->get_arg(0) == "save" && $user->check_auth_token()) {
send_event(new ConfigSaveEvent($config));
$config->save();
flash_message("Config saved");
$page->set_mode("redirect");
$page->set_redirect(make_link("setup"));