Beispiel #1
0
 private function add_product_rows_query_from(JDatabaseQuery $query)
 {
     $db = JFactory::getDbo();
     $j_user = JFactory::getUser();
     $query->from('#__ecommercewd_products AS T_PRODUCTS');
     $query->leftJoin('(SELECT * FROM #__ecommercewd_categories WHERE published = 1) AS T_CATEGORIES ON T_CATEGORIES.id = T_PRODUCTS.category_id');
     $query->leftJoin('(SELECT * FROM #__ecommercewd_manufacturers WHERE published = 1) AS T_MANUFACTURERS ON T_MANUFACTURERS.id = T_PRODUCTS.manufacturer_id');
     $query->leftJoin('(SELECT * FROM #__ecommercewd_labels WHERE published = 1) AS T_LABELS ON T_LABELS.id = T_PRODUCTS.label_id');
     $query->leftJoin('(SELECT * FROM #__ecommercewd_taxes WHERE published = 1) AS T_TAXES ON T_TAXES.id = T_PRODUCTS.tax_id');
     $query->leftJoin('(SELECT * FROM #__ecommercewd_discounts WHERE published = 1) AS T_DISCOUNTS ON T_PRODUCTS.discount_id = T_DISCOUNTS.id');
     $query->leftJoin('(SELECT product_id, COUNT(id) AS reviews_count FROM #__ecommercewd_feedback WHERE published = 1 GROUP BY product_id) AS T_FEEDBACK ON T_FEEDBACK.product_id = T_PRODUCTS.id');
     $query->leftJoin('(SELECT product_id, AVG(rating) AS rating FROM #__ecommercewd_ratings GROUP BY product_id) AS T_RATINGS ON T_RATINGS.product_id = T_PRODUCTS.id');
     // tags
     $query->leftJoin('
         (
             SELECT
                 T_PRODUCT_TAGS.product_id,
                 CONCAT(",", GROUP_CONCAT(T_TAGS.name SEPARATOR ","), ",") as tag_names
             FROM
                 #__ecommercewd_tags AS T_TAGS
                 LEFT JOIN #__ecommercewd_producttags AS T_PRODUCT_TAGS ON T_PRODUCT_TAGS.tag_id = T_TAGS.id
                 GROUP BY product_id)
         AS T_PRODUCT_TAGS ON T_PRODUCT_TAGS.product_id = T_PRODUCTS.id');
     // rating
     if (WDFHelper::is_user_logged_in() == true) {
         $user_identification = 'j_user_id = ' . $j_user->id;
     } else {
         $user_ip_address = WDFUtils::get_client_ip_address();
         $user_identification = 'user_ip_address = ' . $db->quote($user_ip_address);
     }
     $query->leftJoin('(
             SELECT
                 product_id,
                 COUNT(rating) AS ratings_count
             FROM
                 #__ecommercewd_ratings
             WHERE ' . $user_identification . '
             GROUP BY product_id
         ) AS T_USER_RATINGS ON T_USER_RATINGS.product_id = T_PRODUCTS.id');
     if (WDFHelper::is_user_logged_in()) {
         $j_user = JFactory::getUser();
         $user_identification = 'j_user_id = ' . $j_user->id;
     } else {
         $order_product_rand_ids = WDFInput::cookie_get_array('order_product_rand_ids');
         if (empty($order_product_rand_ids) == false) {
             $user_identification = 'j_user_id = 0 AND rand_id IN (' . implode(',', $order_product_rand_ids) . ')';
         } else {
             $user_identification = '0';
         }
     }
     $query->leftJoin('(SELECT product_id, COUNT(id) AS products_in_cart FROM #__ecommercewd_orderproducts WHERE ' . $user_identification . ' AND order_id = 0 GROUP BY product_id) AS T_ORDERPRODUCTS ON T_ORDERPRODUCTS.product_id = T_PRODUCTS.id');
     return $query;
 }
Beispiel #2
0
 public function ajax_rate_product()
 {
     WDFInput::set('tmpl', 'component');
     $model_options = WDFHelper::get_model('options');
     $options = $model_options->get_options();
     $db = JFactory::getDbo();
     $query = $db->getQuery(true);
     $j_user = JFactory::getUser();
     $product_id = WDFInput::get('product_id', 0, 'int');
     $rating_data = WDFJson::decode(stripslashes(WDFInput::get('rating_data_json')));
     $rating = (double) $rating_data->rating;
     // check privileges
     $can_rate = true;
     if ($options->feedback_enable_guest_feedback == 0 && WDFHelper::is_user_logged_in() == false) {
         $can_rate = false;
         $msg = WDFText::get('MSG_LOG_IN_TO_RATE');
     } else {
         $query->clear();
         $query->select('id');
         $query->from('#__ecommercewd_ratings');
         if (WDFHelper::is_user_logged_in() == true) {
             $query->where('j_user_id = ' . $j_user->id);
         } else {
             $query->where('user_ip_address = ' . $db->quote(WDFUtils::get_client_ip_address()));
         }
         $query->where('product_id = ' . $product_id);
         $db->setQuery($query);
         $rating_rows = $db->loadObjectList();
         if ($db->getErrorNum()) {
             $can_rate = false;
             $msg = WDFText::get('MSG_FAILED_TO_RATE');
         }
         if ($rating_rows != null && count($rating_rows) > 0) {
             $can_rate = false;
             $msg = WDFText::get('MSG_YOU_HAVE_ALREADY_RATED_THIS_PRODUCT');
         }
     }
     if ($can_rate == true) {
         // save rating
         $query->clear();
         $query->insert('#__ecommercewd_ratings (j_user_id, user_ip_address, product_id, rating,`date`)');
         $query_values = array();
         $query_values[] = $db->quote($j_user->id);
         $query_values[] = $db->quote(WDFUtils::get_client_ip_address());
         $query_values[] = $db->quote($product_id);
         $query_values[] = $db->quote($rating);
         $query_values[] = 'NOW()';
         $query->values(implode(',', $query_values));
         $db->setQuery($query);
         $db->query();
         if ($db->getErrorNum()) {
             $msg = WDFText::get('MSG_FAILED_TO_RATE');
         } else {
             $msg = WDFText::get('MSG_SUCCESSFULLY_RATED');
         }
     }
     // get average rating
     $query->clear();
     $query->select('FORMAT(AVG(rating), 1)');
     $query->from('#__ecommercewd_ratings');
     $query->where('product_id = ' . $product_id);
     $db->setQuery($query);
     $average_rating = $db->loadResult();
     if ($db->getErrorNum()) {
         $msg = $db->getErrorMsg();
     }
     // return data
     $data = array();
     $data['msg'] = $msg;
     $data['rating'] = $average_rating;
     echo WDFJson::encode($data);
     die;
 }