Index: units/reviews/reviews_event_handler.php =================================================================== --- units/reviews/reviews_event_handler.php (revision 15111) +++ units/reviews/reviews_event_handler.php (working copy) @@ -424,30 +424,40 @@ */ function updateSubitemCounters(&$event) { + if ( $event->Special == '-item' ) { + // ignore Main Item Copy/Pasting and Deleting + return; + } + + $object =& $event->getObject(); + /* @var $object kDBItem */ + $parent_prefix = $this->Application->getUnitOption($event->Prefix, 'ParentPrefix'); - $main_object =& $this->Application->recallObject($parent_prefix, null, Array ('raise_warnings' => 0)); - /* @var $main_object kCatDBItem */ + $parent_table = $this->Application->getUnitOption($parent_prefix, 'TableName'); - if (!$main_object->isLoaded()) { - // deleting main item / cloning main item - return ; + if ( $object->IsTempTable() ) { + $parent_table = $this->Application->GetTempName($parent_table, 'prefix:' . $object->Prefix); } - $object =& $event->getObject(); // for temp tables - /* @var $object kDBItem */ + $fields_hash = Array ('CachedReviewsQty' => 0, 'CachedRating' => 0, 'CachedVotesQty' => 0); // 1. update review counter $sql = 'SELECT COUNT(ReviewId) - FROM '.$object->TableName.' - WHERE ItemId = '.$main_object->GetDBField('ResourceId'); - $review_count = $this->Conn->GetOne($sql); - $main_object->SetDBField('CachedReviewsQty', $review_count); + FROM ' . $object->TableName . ' + WHERE ItemId = ' . $object->GetDBField('ItemId'); + $fields_hash['CachedReviewsQty'] = $this->Conn->GetOne($sql); // 2. update votes counter + rating $rating = $object->GetDBField('Rating'); - $avg_rating = $main_object->GetDBField('CachedRating'); - $votes_count = $main_object->GetDBField('CachedVotesQty'); + $sql = 'SELECT CachedRating, CachedVotesQty + FROM ' . $parent_table . ' + WHERE ResourceId = ' . $object->GetDBField('ItemId'); + $parent_data = $this->Conn->GetRow($sql); + + $avg_rating = $parent_data['CachedRating']; + $votes_count = $parent_data['CachedVotesQty']; + switch ($event->Name) { case 'OnAfterItemCreate': // adding new review with rating $this->changeRating($avg_rating, $votes_count, $rating, '+'); @@ -462,9 +472,11 @@ $this->changeRating($avg_rating, $votes_count, $rating, '+'); break; } - $main_object->SetDBField('CachedRating', "$avg_rating"); // covert $avg_rating value to string - $main_object->SetDBField('CachedVotesQty', $votes_count); - $main_object->Update(); + + $fields_hash['CachedRating'] = "$avg_rating"; + $fields_hash['CachedVotesQty'] = $votes_count; + + $this->Conn->doUpdate($fields_hash, $parent_table, 'ResourceId = ' . $object->GetDBField('ItemId')); } /** @@ -477,16 +489,21 @@ */ function changeRating(&$avg_rating, &$votes_count, $rating, $operation) { - if ($rating < 1 || $rating > 5) { - return ; + if ( $rating < 1 || $rating > 5 ) { + return; } - if ($operation == '+') { + if ( $operation == '+' ) { $avg_rating = (($avg_rating * $votes_count) + $rating) / ($votes_count + 1); ++$votes_count; } else { - $avg_rating = (($avg_rating * $votes_count) - $rating) / ($votes_count - 1); + if ( $votes_count > 1 ) { // escape division by 0 + $avg_rating = (($avg_rating * $votes_count) - $rating) / ($votes_count - 1); + } + else { + $avg_rating = (($avg_rating * $votes_count) - $rating) / 1; + } --$votes_count; } }