public function copyListings($listing_id_arr, $tocat_id_arr, $copy_option_arr, $tmp_listing = false, &$fieldsData = array()) { $dispatcher = JDispatcher::getInstance(); JTable::addIncludePath(JPATH_ADMINISTRATOR . "/components/com_judirectory/tables"); $db = JFactory::getDbo(); $user = JFactory::getUser(); $catTable = JTable::getInstance("Category", "JUDirectoryTable"); $table = $this->getTable(); if (empty($listing_id_arr)) { return false; } if (empty($tocat_id_arr)) { return false; } set_time_limit(0); $assetTable = JTable::getInstance('Asset', 'JTable'); $commentTable = JTable::getInstance("Comment", "JUDirectoryTable"); $reportTable = JTable::getInstance("Report", "JUDirectoryTable"); $subscriptionTable = JTable::getInstance("Subscription", "JUDirectoryTable"); $logTable = JTable::getInstance("Log", "JUDirectoryTable"); $total_copied_listings = 0; foreach ($tocat_id_arr as $tocat_id) { $catTable->reset(); if (!$catTable->load($tocat_id)) { continue; } $assetName = 'com_judirectory.category.' . (int) $tocat_id; $canDoCreate = $user->authorise('judir.listing.create', $assetName); if (!$canDoCreate) { JError::raiseWarning(401, JText::sprintf('COM_JUDIRECTORY_CAN_NOT_CREATE_LISTING_IN_THIS_CATEGORY', $catTable->title)); continue; } foreach ($listing_id_arr as $listing_id) { $table->reset(); if (!$table->load($listing_id)) { continue; } $oldTable = $table; $table->id = 0; $table->cat_id = $tocat_id; do { $query = $db->getQuery(true); $query->SELECT('COUNT(*)'); $query->FROM('#__judirectory_listings AS listing'); $query->JOIN('', '#__judirectory_listings_xref AS listingxref ON listingxref.listing_id = listing.id'); $query->JOIN('', '#__judirectory_categories AS c ON listingxref.cat_id = c.id'); $query->WHERE('c.id = ' . $tocat_id); $query->WHERE('listing.alias = "' . $table->alias . '"'); $db->setQuery($query); $sameAliasListing = $db->loadResult(); if ($sameAliasListing) { $table->title = JString::increment($table->title); $table->alias = JApplication::stringURLSafe(JString::increment($table->alias, 'dash')); } } while ($sameAliasListing); if ($table->style_id == -1) { $old_cat_id = JUDirectoryFrontHelperCategory::getMainCategoryId($listing_id); if ($old_cat_id != $tocat_id) { $oldTemplateStyleObject = JUDirectoryFrontHelperTemplate::getTemplateStyleOfCategory($old_cat_id); $newTemplateStyleObject = JUDirectoryFrontHelperTemplate::getTemplateStyleOfCategory($tocat_id); if ($oldTemplateStyleObject->template_id != $newTemplateStyleObject->template_id) { if (in_array('keep_template_params', $copy_option_arr) && $tmp_listing == false) { $table->style_id = $oldTemplateStyleObject->style_id; } else { if ($tmp_listing == false) { $table->template_params = ''; } } } } } if (!in_array('copy_rates', $copy_option_arr) && $tmp_listing == false) { $table->rating = 0; $table->total_votes = 0; } if (!in_array('copy_hits', $copy_option_arr) && $tmp_listing == false) { $table->hits = 0; } if (in_array('copy_permission', $copy_option_arr)) { $assetTable->reset(); if ($assetTable->loadByName('com_judirectory.listing.' . $listing_id)) { $table->setRules($assetTable->rules); } else { $table->setRules('{}'); } } else { $table->setRules('{}'); } if (!$table->check()) { continue; } $result = $dispatcher->trigger('onContentBeforeCopy', array($this->option . '.' . $this->name, $table, $oldTable, $copy_option_arr)); if (in_array(false, $result, true)) { $this->setError($table->getError()); return false; } if ($table->store()) { $table->checkIn(); $total_copied_listings++; } else { continue; } $newListingId = $table->id; $query = "INSERT INTO #__judirectory_listings_xref (listing_id, cat_id, main) VALUES({$newListingId}, {$tocat_id}, 1)"; $db->setQuery($query); $db->execute(); $ori_fieldgroup_id = JUDirectoryHelper::getFieldGroupIdByListingId($listing_id); $copy_extra_fields = in_array("copy_extra_fields", $copy_option_arr); if ($copy_extra_fields) { $copy_extra_fields = $ori_fieldgroup_id == $catTable->fieldgroup_id ? true : false; } $query = $db->getQuery(true); $query->select("field.*"); $query->from("#__judirectory_fields AS field"); $query->select("plg.folder"); $query->join("", "#__judirectory_plugins AS plg ON field.plugin_id = plg.id"); if ($copy_extra_fields && $ori_fieldgroup_id) { $query->where("field.group_id IN (1, {$ori_fieldgroup_id})"); } else { $query->where("field.group_id = 1"); } $query->order('field.group_id, field.ordering'); $db->setQuery($query); $fields = $db->loadObjectList(); foreach ($fields as $field) { $fieldObj = JUDirectoryFrontHelperField::getField($field, $listing_id); $fieldObj->onCopy($newListingId, $fieldsData); } if (in_array('copy_related_listings', $copy_option_arr)) { $query = "INSERT INTO `#__judirectory_listings_relations` (listing_id, listing_id_related, ordering) SELECT {$newListingId}, listing_id_related, ordering FROM `#__judirectory_listings_relations` WHERE listing_id = {$listing_id}"; $db->setQuery($query); $db->execute(); } if (in_array('copy_rates', $copy_option_arr)) { $ratingMapping = array(); $query = "SELECT * FROM #__judirectory_rating WHERE listing_id = {$listing_id}"; $db->setQuery($query); $ratings = $db->loadObjectList(); if (count($ratings)) { $criteriagroup_id = JUDirectoryHelper::getCriteriaGroupIdByListingId($listing_id); foreach ($ratings as $rating) { $oldRatingId = $rating->id; $rating->id = 0; $rating->listing_id = $newListingId; if ($db->insertObject('#__judirectory_rating', $rating, 'id')) { if (JUDirectoryHelper::hasMultiRating() && $criteriagroup_id && $criteriagroup_id == $catTable->criteriagroup_id) { JUDirectoryMultiRating::copyCriteriaValue($rating->id, $oldRatingId); } $ratingMapping[$oldRatingId] = $rating->id; } } } } if (in_array('copy_comments', $copy_option_arr)) { $query = "SELECT id FROM #__judirectory_comments WHERE listing_id=" . $listing_id . " AND parent_id = 1"; $db->setQuery($query); $commentIds = $db->loadColumn(); $commentMapping = array(); while (!empty($commentIds)) { $commentId = array_shift($commentIds); $query = "SELECT id FROM #__judirectory_comments WHERE listing_id=" . $listing_id . " AND parent_id = {$commentId}"; $db->setQuery($query); $_commentIds = $db->loadColumn(); foreach ($_commentIds as $_commentId) { if (!in_array($_commentId, $commentIds)) { array_push($commentIds, $_commentId); } } $commentTable->load($commentId, true); $commentTable->id = 0; $commentTable->listing_id = $newListingId; $commentTable->parent_id = isset($commentMapping[$commentTable->parent_id]) ? $commentMapping[$commentTable->parent_id] : 0; if (in_array('copy_rates', $copy_option_arr)) { $commentTable->rating_id = isset($ratingMapping[$commentTable->rating_id]) ? $ratingMapping[$commentTable->rating_id] : 0; } $commentTable->store(); $new_comment_id = $commentTable->id; $commentMapping[$commentId] = $new_comment_id; $query = "SELECT * FROM #__judirectory_reports WHERE `item_id` = {$commentId} AND `type` = 'comment'"; $db->setQuery($query); $reports = $db->loadObjectList(); if ($reports) { foreach ($reports as $report) { $reportTable->reset(); if ($reportTable->bind($report) && $reportTable->check()) { $reportTable->id = 0; $reportTable->item_id = $new_comment_id; $reportTable->store(); } else { continue; } } } $query = "SELECT * FROM #__judirectory_subscriptions WHERE `item_id` = {$commentId} AND `type` = 'comment'"; $db->setQuery($query); $subscriptions = $db->loadObjectList(); if ($subscriptions) { foreach ($subscriptions as $subscription) { $subscriptionTable->reset(); if ($subscriptionTable->bind($subscription) && $subscriptionTable->check()) { $subscriptionTable->id = 0; $subscriptionTable->item_id = $new_comment_id; $subscriptionTable->store(); } else { continue; } } } } } if (in_array('copy_reports', $copy_option_arr)) { $query = "SELECT * FROM #__judirectory_reports WHERE `item_id` = {$listing_id} AND `type` = 'listing'"; $db->setQuery($query); $reports = $db->loadObjectList(); if ($reports) { foreach ($reports as $report) { $reportTable->reset(); if ($reportTable->bind($report) && $reportTable->check()) { $reportTable->id = 0; $reportTable->item_id = $newListingId; $reportTable->store(); } else { continue; } } } } if (in_array('copy_subscriptions', $copy_option_arr)) { $query = "SELECT * FROM #__judirectory_subscriptions WHERE `item_id` = {$listing_id} AND `type` = 'listing'"; $db->setQuery($query); $subscriptions = $db->loadObjectList(); if ($subscriptions) { foreach ($subscriptions as $subscription) { $subscriptionTable->reset(); if ($subscriptionTable->bind($subscription) && $subscriptionTable->check()) { $subscriptionTable->id = 0; $subscriptionTable->item_id = $newListingId; $subscriptionTable->store(); } else { continue; } } } } if (in_array('copy_logs', $copy_option_arr)) { $query = "SELECT * FROM #__judirectory_logs WHERE (`listing_id` = {$listing_id})"; $db->setQuery($query); $logs = $db->loadObjectList(); if ($logs) { foreach ($logs as $log) { $logTable->reset(); if ($logTable->bind($log) && $logTable->check()) { $logTable->id = 0; $logTable->item_id = $newListingId; $logTable->listing_id = $newListingId; $logTable->store(); } else { continue; } } } } if ($tmp_listing) { return $newListingId; } $this->cleanCache(); $dispatcher->trigger('onContentAfterCopy', array($this->option . '.' . $this->name, $table, $oldTable, $copy_option_arr)); } } return $total_copied_listings; }
public static function rebuildRating($listing_id) { $db = JFactory::getDbo(); $params = self::getParams(null, $listing_id); $onlyCalculateLastRating = $params->get('only_calculate_last_rating', 0); if ($onlyCalculateLastRating == 1) { $query = $db->getQuery(true); $query->select('r.id, r.listing_id, r.score'); $query->select('cm.approved AS comment_approved'); $query->from('#__judirectory_rating AS r'); $query->join('LEFT', '#__judirectory_comments AS cm ON cm.rating_id = r.id'); $query->where('r.`created` = (SELECT MAX(created) FROM `#__judirectory_rating` AS r2 WHERE r2.listing_id = r.listing_id AND r2.user_id = r.user_id AND r.user_id > 0)'); $query->where('r.listing_id = ' . $listing_id); $query->group('r.created, r.listing_id, r.user_id'); } else { $query = $db->getQuery(true); $query->select('r.id, r.listing_id, r.score'); $query->select('cm.approved AS comment_approved'); $query->from('#__judirectory_rating AS r'); $query->join('LEFT', '#__judirectory_comments AS cm ON cm.rating_id = r.id'); $query->where('r.listing_id = ' . $listing_id); } $db->setQuery($query); $ratings = $db->loadObjectList(); if ($ratings) { $totalScore = 0; $totalVotes = 0; if (JFile::exists(JPATH_SITE . '/components/com_judirectory/fields/multirating/multirating.class.php')) { require_once JPATH_SITE . '/components/com_judirectory/fields/multirating/multirating.class.php'; } foreach ($ratings as $rating) { if (self::hasMultiRating()) { $ratingScore = JUDirectoryMultiRating::rebuildRating($rating); } else { $ratingScore = $rating->score; } if ($rating->comment_approved !== 0) { $totalScore += $ratingScore; $totalVotes++; } } if ($onlyCalculateLastRating == 1) { $avgScore = $totalScore / $totalVotes; $query = $db->getQuery(true); $query->update('#__judirectory_listings'); $query->set('rating = ' . $avgScore); $query->set('total_votes = ' . $totalVotes); $query->where('id = ' . $listing_id); } else { $query = $db->getQuery(true); $query->update('#__judirectory_listings'); $query->set('rating = (SELECT AVG(score) FROM #__judirectory_rating WHERE listing_id=' . $listing_id . ')'); $query->set('total_votes = ' . $totalVotes); $query->where('id = ' . $listing_id); } $db->setQuery($query); $db->execute(); return false; } }
public function updateRating($data) { $user = JFactory::getUser(); $created = JFactory::getDate()->toSql(); $criteriaArray = isset($data['criteria_array']) ? $data['criteria_array'] : array(); $listingId = $data['listing_id']; $commentId = $data['comment_id']; $ratingObj = $this->getRating($listingId, $commentId); $ratingId = $ratingObj->id; $ratingScoreOld = $ratingObj->score; $ratingScoreNew = JUDirectoryFrontHelperRating::calculateRatingScore($data, $listingId, $criteriaArray); $dataRating = array('id' => $ratingId, 'user_id' => $user->id, 'comment_id' => $commentId, 'score' => $ratingScoreNew, 'created' => $created, 'listing_id' => $listingId); JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_judirectory/tables'); $ratingTable = JTable::getInstance('Rating', 'JUDirectoryTable'); $ratingTable->bind($dataRating); if (!$ratingTable->check()) { return false; } $ratingStore = $ratingTable->store(); if (!$ratingStore) { return false; } if ($ratingStore && count($criteriaArray) > 0) { $oldCriteriaIdArr = $this->getCriteriasId($ratingId); $criteriaIdArr = array(); foreach ($criteriaArray as $criteria) { $criteriaIdArr[] = $criteria->id; } $flagUpdate = false; if (count(array_diff($criteriaIdArr, $oldCriteriaIdArr)) == 0) { $flagUpdate = true; } if ($flagUpdate == false) { $this->deleteCriteriasByRatingId($ratingId); } foreach ($criteriaArray as $criteria) { if ($flagUpdate == true) { $this->updateCriteriaValue($ratingId, $criteria->id, $criteria->value); } else { JUDirectoryMultiRating::insertCriteriaValue($ratingId, $criteria->id, $criteria->value); } } } $listingItem = JUDirectoryHelper::getListingById($listingId); $rating = ($listingItem->rating * $listingItem->total_votes + $ratingScoreNew - $ratingScoreOld) / $listingItem->total_votes; $db = JFactory::getDbo(); $query = $db->getQuery(true); $query->update('#__judirectory_listings'); $query->set('rating = ' . $rating); $query->where('id = ' . $listingId); $db->setQuery($query); $db->execute(); $session = JFactory::getSession(); $timeNow = JFactory::getDate()->toSql(); $timeNowStamp = strtotime($timeNow); $inputCookie = JFactory::getApplication()->input->cookie; $config = JFactory::getConfig(); $cookie_domain = $config->get('cookie_domain', ''); $cookie_path = $config->get('cookie_path', '/'); $inputCookie->set('judir-listing-rated-' . $listingId, $timeNowStamp, time() + 864000, $cookie_path, $cookie_domain); $session->set('judir-listing-rated-' . $listingId, $timeNowStamp); $logData = array('user_id' => $user->id, 'event' => 'listing.rate', 'item_id' => $listingId, 'listing_id' => $listingId, 'value' => $ratingScoreNew, 'reference' => $ratingTable->id); JUDirectoryFrontHelperLog::addLog($logData); return true; }