示例#1
0
 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;
 }