tags/source/rating/locked should only update/log if different than current

This commit is contained in:
Daku 2012-01-21 00:17:07 +00:00
parent 7049b3bf4d
commit 3338ff0420
3 changed files with 53 additions and 47 deletions

View file

@ -64,7 +64,7 @@ class Ratings implements Extension {
}
if($event instanceof RatingSetEvent) {
$this->set_rating($event->image->id, $event->rating);
$this->set_rating($event->image->id, $event->rating, $event->image->rating);
}
if($event instanceof ImageInfoBoxBuildingEvent) {
@ -205,10 +205,12 @@ class Ratings implements Extension {
}
}
private function set_rating($image_id, $rating) {
private function set_rating($image_id, $rating, $old_rating) {
global $database;
$database->Execute("UPDATE images SET rating=? WHERE id=?", array($rating, $image_id));
log_info("core-image", "Rating for Image #{$image_id} set to: ".$this->theme->rating_to_name($rating));
if($old_rating != $rating){
$database->Execute("UPDATE images SET rating=? WHERE id=?", array($rating, $image_id));
log_info("core-image", "Rating for Image #{$image_id} set to: ".$this->theme->rating_to_name($rating));
}
}
}
add_event_listener(new Ratings());

View file

@ -368,25 +368,29 @@ class Image {
/**
* Set the image's source URL
*/
public function set_source($source) {
public function set_source($source, $old_source) {
global $database;
if(empty($source)) $source = null;
$database->execute("UPDATE images SET source=:source WHERE id=:id", array("source"=>$source, "id"=>$this->id));
log_info("core-image", "Source for Image #{$this->id} set to: ".$source);
if($old_source != $source){
$database->execute("UPDATE images SET source=:source WHERE id=:id", array("source"=>$source, "id"=>$this->id));
log_info("core-image", "Source for Image #{$this->id} set to: ".$source);
}
}
public function is_locked() {
return ($this->locked === true || $this->locked == "Y" || $this->locked == "t");
}
public function set_locked($tf) {
public function set_locked($tf, $old_sln) {
global $database;
$ln = $tf ? "Y" : "N";
$sln = $database->engine->scoreql_to_sql("SCORE_BOOL_$ln");
$sln = str_replace("'", "", $sln);
$sln = str_replace('"', "", $sln);
$database->execute("UPDATE images SET locked=:yn WHERE id=:id", array("yn"=>$sln, "id"=>$this->id));
log_info("core-image", "Locked status of Image #{$this->id} set to: ".$sln);
if($old_sln != $sln){
$database->execute("UPDATE images SET locked=:yn WHERE id=:id", array("yn"=>$sln, "id"=>$this->id));
log_info("core-image", "Setting Image #{$this->id} lock to: {$event->locked}".$sln);
}
}
/**
@ -405,48 +409,49 @@ class Image {
/**
* Set the tags for this image
*/
public function set_tags($tags) {
public function set_tags($tags, $old_tags) {
global $database;
$tags = Tag::resolve_list($tags);
assert(is_array($tags));
assert(count($tags) > 0);
// delete old
$this->delete_tags_from_image();
// insert each new tags
foreach($tags as $tag) {
$id = $database->get_one(
$database->engine->scoreql_to_sql(
"SELECT id FROM tags WHERE SCORE_STRNORM(tag) = SCORE_STRNORM(:tag)"
),
array("tag"=>$tag));
if(empty($id)) {
// a new tag
$database->execute(
"INSERT INTO tags(tag) VALUES (:tag)",
$new_tags = implode(" ", $tags);
if($old_tags != $new_tags){
// delete old
$this->delete_tags_from_image();
// insert each new tags
foreach($tags as $tag) {
$id = $database->get_one(
$database->engine->scoreql_to_sql(
"SELECT id FROM tags WHERE SCORE_STRNORM(tag) = SCORE_STRNORM(:tag)"
),
array("tag"=>$tag));
if(empty($id)) {
// a new tag
$database->execute(
"INSERT INTO tags(tag) VALUES (:tag)",
array("tag"=>$tag));
$database->execute(
"INSERT INTO image_tags(image_id, tag_id)
VALUES(:id, (SELECT id FROM tags WHERE tag = :tag))",
array("id"=>$this->id, "tag"=>$tag));
}
else {
// user of an existing tag
$database->execute(
"INSERT INTO image_tags(image_id, tag_id) VALUES(:iid, :tid)",
array("iid"=>$this->id, "tid"=>$id));
}
$database->execute(
"INSERT INTO image_tags(image_id, tag_id)
VALUES(:id, (SELECT id FROM tags WHERE tag = :tag))",
array("id"=>$this->id, "tag"=>$tag));
$database->engine->scoreql_to_sql(
"UPDATE tags SET count = count + 1 WHERE SCORE_STRNORM(tag) = SCORE_STRNORM(:tag)"
),
array("tag"=>$tag));
}
else {
// user of an existing tag
$database->execute(
"INSERT INTO image_tags(image_id, tag_id) VALUES(:iid, :tid)",
array("iid"=>$this->id, "tid"=>$id));
}
$database->execute(
$database->engine->scoreql_to_sql(
"UPDATE tags SET count = count + 1 WHERE SCORE_STRNORM(tag) = SCORE_STRNORM(:tag)"
),
array("tag"=>$tag));
}
log_info("core-image", "Tags for Image #{$this->id} set to: ".implode(" ", $tags));
$database->cache->delete("image-{$this->id}-tags");
log_info("core-image", "Tags for Image #{$this->id} set to: ".implode(" ", $tags));
$database->cache->delete("image-{$this->id}-tags");
}
}
/**

View file

@ -91,20 +91,19 @@ class TagEdit implements Extension {
if($event instanceof TagSetEvent) {
if($user->is_admin() || !$event->image->is_locked()) {
$event->image->set_tags($event->tags);
$event->image->set_tags($event->tags, $event->image->get_tag_list());
}
}
if($event instanceof SourceSetEvent) {
if($user->is_admin() || !$event->image->is_locked()) {
$event->image->set_source($event->source);
$event->image->set_source($event->source, $event->image->source);
}
}
if($event instanceof LockSetEvent) {
if($user->is_admin()) {
log_debug("tag_edit", "Setting Image #{$event->image->id} lock to: {$event->locked}");
$event->image->set_locked($event->locked);
$event->image->set_locked($event->locked, $event->image->locked);
}
}