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) { 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) { 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; global $database;
$database->Execute("UPDATE images SET rating=? WHERE id=?", array($rating, $image_id)); if($old_rating != $rating){
log_info("core-image", "Rating for Image #{$image_id} set to: ".$this->theme->rating_to_name($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()); add_event_listener(new Ratings());

View File

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

View File

@ -91,20 +91,19 @@ class TagEdit implements Extension {
if($event instanceof TagSetEvent) { if($event instanceof TagSetEvent) {
if($user->is_admin() || !$event->image->is_locked()) { 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($event instanceof SourceSetEvent) {
if($user->is_admin() || !$event->image->is_locked()) { 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($event instanceof LockSetEvent) {
if($user->is_admin()) { 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->locked);
$event->image->set_locked($event->locked);
} }
} }