Advertisement
Guest User

gd-star-rating/code/db/widgetizer.php

a guest
Aug 30th, 2013
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 35.13 KB | None | 0 0
  1. <?php
  2.  
  3. function gdsr_widget_convert_select($instance) {
  4.     $select_post_types = array();
  5.     if ($instance['select'] == "postpage") $select_post_types = array("post", "page");
  6.     else $select_post_types = explode ("|", $instance['select']);
  7.     return $select_post_types;
  8. }
  9.  
  10. function gdsr_get_public_post_types() {
  11.     global $gdsr;
  12.     $post_types = array();
  13.     if ($gdsr->wp_version > 29) {
  14.         $options = array("public" => true);
  15.         $types = get_post_types($options, "objects");
  16.         foreach ($types as $id => $p) $post_types[$p->name] = $p->labels->name;
  17.     } else {
  18.         $post_types = array("post" => "Posts", "page" => "Pages");
  19.     }
  20.     return $post_types;
  21. }
  22.  
  23. class GDSRX {
  24.     function compile_query($query) {
  25.         $sql = "select ".$query["select"]." from ".$query["from"];
  26.  
  27.         if (trim($query["where"]) != "") $sql.= " where ".$query["where"];
  28.         if (trim($query["group"]) != "") $sql.= " group by ".$query["group"];
  29.         if (trim($query["order"]) != "") $sql.= " order by ".$query["order"];
  30.         if (trim($query["limit"]) != "") $sql.= " limit ".$query["limit"];
  31.  
  32.         return $sql;
  33.     }
  34.  
  35.     function get_trend_data($ids, $grouping = "post", $type = "article", $period = "over", $last = 1, $over = 30, $multi_id = 0) {
  36.         global $wpdb, $table_prefix;
  37.         $strtodate = gdFunctionsGDSR::mysql_version();
  38.         $strtodate = $strtodate == 4 ? $strtodate = "date_add(d.vote_date, interval 0 day)" : $strtodate = "str_to_date(d.vote_date, '%Y-%m-%d')";
  39.  
  40.         if ($period == "over") $where = sprintf("%s BETWEEN DATE_SUB(NOW(), INTERVAL %s DAY) AND DATE_SUB(NOW(), INTERVAL %s DAY)", $strtodate, $last + $over, $last);
  41.         else $where = sprintf("%s BETWEEN DATE_SUB(NOW(), INTERVAL %s DAY) AND NOW()", $strtodate, $last);
  42.  
  43.         $from = $join = $sql = "";
  44.         switch ($grouping) {
  45.             case "post":
  46.                 $select = $type == "multis" ? "d.post_id" : "d.id";
  47.                 break;
  48.             case "user":
  49.                 $select = "u.id";
  50.                 $join = "p.id = d.id and p.post_status = 'publish' and u.id = p.post_author and ";
  51.                 break;
  52.             case "category":
  53.                 $select = "t.term_id";
  54.                 $join = "p.id = d.id and p.post_status = 'publish' and t.term_taxonomy_id = r.term_taxonomy_id and r.object_id = p.id and t.taxonomy = 'category' and t.term_id = x.term_id and ";
  55.                 break;
  56.         }
  57.  
  58.         if ($type == "multis") {
  59.             switch ($grouping) {
  60.                 case "post":
  61.                     $from = sprintf("%sgdsr_multis_trend d", $table_prefix);
  62.                     break;
  63.                 case "user":
  64.                     $from = sprintf("%s u, %sposts p, %sgdsr_multis_trend d", $wpdb->users, $table_prefix, $table_prefix);
  65.                     break;
  66.                 case "category":
  67.                     $from = sprintf("%sterm_taxonomy t, %sterm_relationships r, %sterms x, %sposts p, %sgdsr_multis_trend d", $table_prefix, $table_prefix, $table_prefix, $table_prefix, $table_prefix);
  68.                     break;
  69.             }
  70.  
  71.             $sql = sprintf("SELECT %s as id, sum(d.total_votes_users) as user_voters, sum(d.average_rating_users * d.total_votes_users) as user_votes, sum(d.total_votes_visitors) as visitor_voters, sum(d.average_rating_visitors * d.total_votes_visitors) as visitor_votes FROM %s WHERE %s%s and %s in (%s) and multi_id = %s group by %s order by %s asc",
  72.                 $select, $from, $join, $where, $select, $ids, $multi_id, $select, $select);
  73.         } else {
  74.             switch ($grouping) {
  75.                 case "post":
  76.                     $from = sprintf("%sgdsr_votes_trend d", $table_prefix);
  77.                     break;
  78.                 case "user":
  79.                     $from = sprintf("%s u, %sposts p, %sgdsr_votes_trend d", $wpdb->users, $table_prefix, $table_prefix);
  80.                     break;
  81.                 case "category":
  82.                     $from = sprintf("%sterm_taxonomy t, %sterm_relationships r, %sterms x, %sposts p, %sgdsr_votes_trend d", $table_prefix, $table_prefix, $table_prefix, $table_prefix, $table_prefix);
  83.                     break;
  84.             }
  85.  
  86.             $sql = sprintf("SELECT %s as id, sum(d.user_voters) as user_voters, sum(d.user_votes) as user_votes, sum(d.visitor_voters) as visitor_voters, sum(d.visitor_votes) as visitor_votes FROM %s WHERE %s%s and d.vote_type = '%s' AND %s IN (%s) GROUP BY %s ORDER BY %s asc",
  87.                 $select, $from, $join, $where, $type == "thumbs" ? "artthumb" : "article", $select, $ids, $select, $select);
  88.         }
  89.  
  90.         return $wpdb->get_results($sql);
  91.     }
  92.  
  93.     function get_trend_calculation($ids, $grouping = "post", $show = "total", $last = 1, $over = 30, $source = "article", $multi_id = 0) {
  94.         global $wpdb, $table_prefix;
  95.         $data_over = $data_last = $votes_over = $voters_over = array();
  96.         $data_last = GDSRX::get_trend_data($ids, $grouping, $source, "last", $last, $over, $multi_id);
  97.         $data_over = GDSRX::get_trend_data($ids, $grouping, $source, "over", $last, $over, $multi_id);
  98.  
  99.         if (count($data_last) == 0) $votes_last = $voters_last = array();
  100.         if (count($data_over) == 0) $votes_over = $voters_over = array();
  101.  
  102.         for ($i = 0; $i < count($data_over); $i++) {
  103.             $row_over = $data_over[$i];
  104.  
  105.             if ($show == "total") {
  106.                 $votes_over[$row_over->id] = $row_over->user_votes + $row_over->visitor_votes;
  107.                 $voters_over[$row_over->id] = $row_over->user_voters + $row_over->visitor_voters;
  108.             }
  109.             if ($show == "visitors") {
  110.                 $votes_over[$row_over->id] = $row_over->visitor_votes;
  111.                 $voters_over[$row_over->id] = $row_over->visitor_voters;
  112.             }
  113.             if ($show == "users") {
  114.                 $votes_over[$row_over->id] = $row_over->user_votes ;
  115.                 $voters_over[$row_over->id] = $row_over->user_voters;
  116.             }
  117.         }
  118.  
  119.         for ($i = 0; $i < count($data_last); $i++) {
  120.             $row_last = $data_last[$i];
  121.            
  122.             if ($show == "total") {
  123.                 $votes_last[$row_last->id] = $row_last->user_votes + $row_last->visitor_votes;
  124.                 $voters_last[$row_last->id] = $row_last->user_voters + $row_last->visitor_voters;
  125.             }
  126.             if ($show == "visitors") {
  127.                 $votes_last[$row_last->id] = $row_last->visitor_votes;
  128.                 $voters_last[$row_last->id] = $row_last->visitor_voters;
  129.             }
  130.             if ($show == "users") {
  131.                 $votes_last[$row_last->id] = $row_last->user_votes ;
  132.                 $voters_last[$row_last->id] = $row_last->user_voters;
  133.             }
  134.         }
  135.  
  136.         foreach ($votes_last as $key => $value) {
  137.             if (!isset($votes_over[$key])) {
  138.                 $votes_over[$key] = $voters_over[$key] = 0;
  139.             }
  140.         }
  141.  
  142.         foreach ($votes_over as $key => $value) {
  143.             if (!isset($votes_last[$key])) {
  144.                 $votes_last[$key] = $voters_last[$key] = 0;
  145.             }
  146.         }
  147.  
  148.         $trends = array();
  149.         foreach ($votes_last as $key => $value) {
  150.             $trends[$key] = new TrendValue($votes_last[$key], $voters_last[$key], $votes_over[$key], $voters_over[$key], $last, $over);
  151.         }
  152.        
  153.         return $trends;
  154.     }
  155.  
  156.     function get_totals_thumbs($widget, $min = 0) {
  157.         global $table_prefix;
  158.         $where = array("p.id = d.post_id", "p.post_status = 'publish'");
  159.         $select = "count(*) as count, 0 as voters, 0 as rating, 0 as bayes_rating, 0 as max_rating, 0 as percentage";
  160.  
  161.         if ($widget["show"] == "total") {
  162.             $select.= ", (d.user_recc_plus + d.visitor_recc_plus - d.user_recc_minus - d.visitor_recc_minus) as score";
  163.             $select.= ", (d.user_recc_plus + d.visitor_recc_plus + d.user_recc_minus + d.visitor_recc_minus) as votes";
  164.             $where[] = "(d.user_recc_plus + d.visitor_recc_plus + d.user_recc_minus + d.visitor_recc_minus) > ".$min;
  165.         }
  166.         if ($widget["show"] == "visitors") {
  167.             $select.= ", (d.visitor_recc_plus - d.visitor_recc_minus) as score";
  168.             $select.= ", (d.visitor_recc_plus + d.visitor_recc_minus) as votes";
  169.             $where[] = "(d.visitor_recc_plus + d.visitor_recc_minus) > ".$min;
  170.         }
  171.         if ($widget["show"] == "users") {
  172.             $select.= ", (d.user_recc_plus - d.user_recc_minus) as score";
  173.             $select.= ", (d.user_recc_plus + d.user_recc_minus) as votes";
  174.             $where[] = "(d.user_recc_plus + d.user_recc_minus) > ".$min;
  175.         }
  176.  
  177.         if ($widget["select"] != "" && $widget["select"] != "postpage")
  178.             $where[] = "p.post_type = '".$widget["select"]."'";
  179.  
  180.         $query = array(
  181.             "select" => $select,
  182.             "from" => sprintf("%sposts p, %sgdsr_data_article d", $table_prefix, $table_prefix),
  183.             "where" => join(" and ", $where),
  184.             "group" => "",
  185.             "order" => "",
  186.             "limit" => ""
  187.         );
  188.  
  189.         return $query;
  190.     }
  191.  
  192.     function get_totals_standard($widget, $min = 0) {
  193.         global $table_prefix;
  194.         $where = array("p.id = d.post_id", "p.post_status = 'publish'");
  195.         $select = "count(*) as count, 0 as rating, 0 as bayes_rating, 0 as max_rating, 0 as percentage";
  196.  
  197.         if ($widget["show"] == "total") {
  198.             $select.= ", sum(d.user_voters) + sum(d.visitor_voters) as voters, sum(d.user_votes) + sum(d.visitor_votes) as votes";
  199.             $where[] = "(d.user_voters + d.visitor_voters) > ".$min;
  200.         }
  201.         if ($widget["show"] == "visitors") {
  202.             $select.= ", sum(d.visitor_voters) as voters, sum(d.visitor_votes) as votes";
  203.             $where[] = "d.visitor_voters > ".$min;
  204.         }
  205.         if ($widget["show"] == "users") {
  206.             $select.= ", sum(d.user_voters) as voters, sum(d.user_votes) as votes";
  207.             $where[] = "d.user_voters > ".$min;
  208.         }
  209.  
  210.         if ($widget["select"] != "" && $widget["select"] != "postpage")
  211.             $where[] = "p.post_type = '".$widget["select"]."'";
  212.  
  213.         $query = array(
  214.             "select" => $select,
  215.             "from" => sprintf("%sposts p, %sgdsr_data_article d", $table_prefix, $table_prefix),
  216.             "where" => join(" and ", $where),
  217.             "group" => "",
  218.             "order" => "",
  219.             "limit" => ""
  220.         );
  221.  
  222.         return $query;
  223.     }
  224.  
  225.     function get_widget_multis($widget, $min = 0) {
  226.         global $table_prefix;
  227.  
  228.         $grouping = $widget["grouping"];
  229.         $cats = $widget["category"];
  230.         $cats_in = $widget["category_toponly"] == 0;
  231.         if ($cats_in && $cats != "0" && $cats != "") {
  232.             $subs = gdWPGDSR::get_subcategories_ids($widget["category"]);
  233.             $subs[] = $cats;
  234.             $cats = join(",", $subs);
  235.         }
  236.         if ($widget["categories"] != "") {
  237.             $cats = $widget["categories"];
  238.             $cats_in = true;
  239.         }
  240.  
  241.         $where = array();
  242.         $select = $from = $group = "";
  243.  
  244.         if ($widget["bayesian_calculation"] == "0") $min = 0;
  245.         if ($widget["min_votes"] > $min) $min = $widget["min_votes"];
  246.         if ($min == 0 && $widget["hide_empty"] == "1") $min = 1;
  247.         if ($widget["hide_noreview"] == "1") $where[] = "d.average_review > 0";
  248.  
  249.         $where[] = "p.id = d.post_id";
  250.         $where[] = "d.multi_id = ".$widget["source_set"];
  251.         $where[] = "p.post_status = 'publish'";
  252.  
  253.         $extras = ", 0 as votes, 0 as voters, 0 as rating, 0 as bayesian, '' as item_trend_rating, '' as item_trend_voting, '' as permalink, '' as tense, '' as rating_stars, '' as bayesian_stars, '' as review_stars";
  254.  
  255.         if (($cats != "" && $cats != "0") || $grouping == 'category'){
  256.             $from = sprintf("%sterm_taxonomy t, %sterm_relationships r, ", $table_prefix, $table_prefix);
  257.             $where[] = "t.term_taxonomy_id = r.term_taxonomy_id";
  258.             $where[] = "r.object_id = p.id";
  259.         }
  260.         if ($cats != "" && $cats != "0") {
  261.             $where[] = "t.taxonomy = 'category'";
  262.             if ($cats_in) $where[] = "t.term_id in (".$cats.")";
  263.             else $where[] = "t.term_id = ".$cats;
  264.         }
  265.  
  266.         $col_id = "p.id";
  267.         $col_title = "p.post_title";
  268.         if ($grouping == 'taxonomy') {
  269.             $from.= sprintf("%sterm_taxonomy tt, %sterm_relationships tr, %sterms tx, ", $table_prefix, $table_prefix, $table_prefix);
  270.             $where[] = "tt.taxonomy = '".$widget["taxonomy"]."'";
  271.             $where[] = "tt.term_taxonomy_id = tr.term_taxonomy_id";
  272.             $where[] = "tt.term_id = tx.term_id";
  273.             $where[] = "tr.object_id = p.id";
  274.             $select = "tx.name as title, tx.term_id, tx.slug, count(*) as counter, sum(d.average_rating_users * d.total_votes_users) as user_votes, sum(d.average_rating_visitors * d.total_votes_visitors) as visitor_votes, sum(d.total_votes_users) as user_voters, sum(d.total_votes_visitors) as visitor_voters";
  275.             $select.= ", sum(d.average_review) as review";
  276.             $group = $col_id = "tt.term_id";
  277.             $col_title = "tx.name";
  278.         } else if ($grouping == 'category') {
  279.             $from.= sprintf("%sterms x, ", $table_prefix);
  280.             $where[] = "t.taxonomy = 'category'";
  281.             $where[] = "t.term_id = x.term_id";
  282.             $select = "x.name as title, x.term_id, x.slug, count(*) as counter, sum(d.average_rating_users * d.total_votes_users) as user_votes, sum(d.average_rating_visitors * d.total_votes_visitors) as visitor_votes, sum(d.total_votes_users) as user_voters, sum(d.total_votes_visitors) as visitor_voters";
  283.             $select.= ", sum(d.average_review) as review";
  284.             $group = $col_id = "t.term_id";
  285.             $col_title = "x.name";
  286.         } else if ($grouping == 'user') {
  287.             $from.= sprintf("%s u, ", $wpdb->users);
  288.             $where[] = "u.id = p.post_author";
  289.             $select = "u.display_name as title, u.user_nicename as slug, u.id, count(*) as counter, sum(d.average_rating_users * d.total_votes_users) as user_votes, sum(d.average_rating_visitors * d.total_votes_visitors) as visitor_votes, sum(d.total_votes_users) as user_voters, sum(d.total_votes_visitors) as visitor_voters";
  290.             $select.= ", sum(d.average_review) as review";
  291.             $group = $col_id = "u.id";
  292.             $col_title = "u.display_name";
  293.         } else {
  294.             $select = "p.id as post_id, p.post_name as slug, p.post_author as author, p.post_title as title, p.post_type, p.post_date, d.*, 1 as counter, d.average_rating_users * d.total_votes_users as user_votes, d.average_rating_visitors * d.total_votes_visitors as visitor_votes, d.total_votes_users as user_voters, d.total_votes_visitors as visitor_voters";
  295.             $select.= ", d.average_review as review";
  296.         }
  297.  
  298.         if ($grouping != 'post' && $widget["min_count"] > 0)
  299.             $group.= " having count(*) >= ".$widget["min_count"];
  300.  
  301.         if (is_array($widget["select"])) {
  302.             $where[] = "p.post_type in ('".join("', '", $widget["select"])."')";
  303.         } else {
  304.             if ($widget["select"] != "" && $widget["select"] != "postpage")
  305.                 $where[] = "p.post_type = '".$widget["select"]."'";
  306.         }
  307.  
  308.         if ($min > 0) {
  309.             if ($widget["show"] == "total") $where[] = "(d.total_votes_users + d.total_votes_visitors) >= ".$min;
  310.             if ($widget["show"] == "visitors") $where[] = "d.total_votes_visitors >= ".$min;
  311.             if ($widget["show"] == "users") $where[] = "d.total_votes_users >= ".$min;
  312.         }
  313.  
  314.         if ($widget["order"] == "desc" || $widget["order"] == "asc")
  315.             $sort = $widget["order"];
  316.         else
  317.             $sort = "desc";
  318.  
  319.         if ($widget["last_voted_days"] == "") $widget["last_voted_days"] = 0;
  320.         if ($widget["last_voted_days"] > 0) {
  321.             $where[] = "TO_DAYS(CURDATE()) - ".$widget["last_voted_days"]." <= TO_DAYS(d.last_voted)";
  322.         }
  323.  
  324.         if ($widget["publish_date"] == "range") {
  325.             $where[] = "p.post_date >= '".$widget["publish_range_from"]."' and p.post_date <= '".$widget["publish_range_to"]."'";
  326.         } else if ($widget["publish_date"] == "month") {
  327.             $month = $widget["publish_month"];
  328.             if ($month != "" && $month != "0") {
  329.                 $where[] = "year(p.post_date) = ".substr($month, 0, 4);
  330.                 $where[] = "month(p.post_date) = ".substr($month, 4, 2);
  331.             }
  332.         } else if ($widget["publish_date"] == "lastd") {
  333.             if ($widget["publish_days"] > 0)
  334.                 $where[] = "TO_DAYS(CURDATE()) - ".$widget["publish_days"]." <= TO_DAYS(p.post_date)";
  335.         }
  336.         $select = "p.post_content, p.post_excerpt, '' as content, '' as excerpt, ".$select;
  337.  
  338.         $col = $widget["column"];
  339.         if ($col == "title") $col = $col_title;
  340.         else if ($col == "review") $col = "d.average_review";
  341.         else if ($col == "rating" || $col == "bayesian") {
  342.             if ($widget["show"] == "total") $col = "(d.average_rating_users * d.total_votes_users + d.average_rating_visitors * d.total_votes_visitors)/(d.total_votes_users + d.total_votes_visitors)";
  343.             if ($widget["show"] == "visitors") $col = "(d.average_rating_visitors * d.total_votes_visitors)/d.total_votes_visitors";
  344.             if ($widget["show"] == "users") $col = "(d.average_rating_users * d.total_votes_users)/d.total_votes_users";
  345.         } else if ($col == "voters") {
  346.             if ($widget["show"] == "total") $col = "d.total_votes_users + d.total_votes_visitors";
  347.             if ($widget["show"] == "visitors") $col = "d.total_votes_visitors";
  348.             if ($widget["show"] == "users") $col = "d.total_votes_users";
  349.         }
  350.         else if ($col == "counter" && $grouping != "post") $col = "count(*)";
  351.         else $col = $col_id;
  352.  
  353.         $query = array(
  354.             "select" => "distinct ".$select.$extras,
  355.             "from" => sprintf("%s%sposts p, %sgdsr_multis_data d", $from, $table_prefix, $table_prefix),
  356.             "where" => join(" and ", $where),
  357.             "group" => $group,
  358.             "order" => sprintf("%s %s", $col, $sort),
  359.             "limit" => "0, ".$widget["rows"]
  360.         );
  361.  
  362.         return $query;
  363.     }
  364.  
  365.     function get_widget_thumbs($widget, $min = 0) {
  366.         global $table_prefix;
  367.  
  368.         $grouping = $widget["grouping"];
  369.         $cats = $widget["category"];
  370.         $cats_in = $widget["category_toponly"] == 0;
  371.         $select = $from = $group = "";
  372.         $where = array("p.id = d.post_id", "p.post_status = 'publish'");
  373.         $extras = ", 0 as votes, 0 as voters, 0 as rating, 0 as bayesian, '' as item_trend_rating, '' as item_trend_voting, '' as permalink, '' as tense, '' as rating_stars, '' as bayesian_stars, '' as review_stars";
  374.  
  375.         if ($cats_in && $cats != "0") {
  376.             $subs = gdWPGDSR::get_subcategories_ids($widget["category"]);
  377.             $subs[] = $cats;
  378.             $cats = join(",", $subs);
  379.         }
  380.  
  381.         if ($widget["categories"] != "") {
  382.             $cats = $widget["categories"];
  383.             $cats_in = true;
  384.         }
  385.  
  386.         if ($widget["bayesian_calculation"] == "0") $min = 0;
  387.         if ($widget["min_votes"] > $min) $min = $widget["min_votes"];
  388.         if ($min == 0 && $widget["hide_empty"] == "1") $min = 1;
  389.  
  390.         if (($cats != "" && $cats != "0") || $grouping == 'category'){
  391.             $from = sprintf("%sterm_taxonomy t, %sterm_relationships r, ", $table_prefix, $table_prefix);
  392.             $where[] = "t.term_taxonomy_id = r.term_taxonomy_id";
  393.             $where[] = "r.object_id = p.id";
  394.         }
  395.         if ($cats != "" && $cats != "0") {
  396.             $where[] = "t.taxonomy = 'category'";
  397.             if ($cats_in) $where[] = "t.term_id in (".$cats.")";
  398.             else $where[] = "t.term_id = ".$cats;
  399.         }
  400.  
  401.         $col_id = "p.id";
  402.         $col_title = "p.post_title";
  403.         if ($grouping == 'taxonomy') {
  404.             $from.= sprintf("%sterm_taxonomy tt, %sterm_relationships tr, %sterms tx, ", $table_prefix, $table_prefix, $table_prefix);
  405.             $where[] = "tt.taxonomy = '".$widget["taxonomy"]."'";
  406.             $where[] = "tt.term_taxonomy_id = tr.term_taxonomy_id";
  407.             $where[] = "tt.term_id = tx.term_id";
  408.             $where[] = "tr.object_id = p.id";
  409.             $select = "tx.name as title, tx.term_id, tx.slug, count(*) as counter, sum(d.user_recc_plus) as user_recc_plus, sum(d.visitor_recc_plus) as visitor_recc_plus, sum(d.user_recc_minus) as user_recc_minus, sum(d.visitor_recc_minus) as visitor_recc_minus";
  410.             $select.= ", sum(d.review) as review";
  411.             $group = $col_id = "tt.term_id";
  412.             $col_title = "tx.name";
  413.         } else if ($grouping == 'category') {
  414.             $from.= sprintf("%sterms x, ", $table_prefix);
  415.             $where[] = "t.taxonomy = 'category'";
  416.             $where[] = "t.term_id = x.term_id";
  417.             $select = "x.name as title, x.term_id, x.slug, count(*) as counter, sum(d.user_recc_plus) as user_recc_plus, sum(d.visitor_recc_plus) as visitor_recc_plus, sum(d.user_recc_minus) as user_recc_minus, sum(d.visitor_recc_minus) as visitor_recc_minus";
  418.             $select.= ", sum(d.review) as review";
  419.             $group = $col_id = "t.term_id";
  420.             $col_title = "x.name";
  421.         } else if ($grouping == 'user') {
  422.             $from.= sprintf("%s u, ", $wpdb->users);
  423.             $where[] = "u.id = p.post_author";
  424.             $select = "u.display_name as title, u.user_nicename as slug, u.id, count(*) as counter, sum(d.user_recc_plus) as user_recc_plus, sum(d.visitor_recc_plus) as visitor_recc_plus, sum(d.user_recc_minus) as user_recc_minus, sum(d.visitor_recc_minus) as visitor_recc_minus";
  425.             $select.= ", sum(d.review) as review";
  426.             $group = $col_id = "u.id";
  427.             $col_title = "u.display_name";
  428.         } else {
  429.             $select = "p.id as post_id, p.post_name as slug, p.post_author as author, p.post_title as title, p.post_type, p.post_date, d.*, 1 as counter";
  430.         }
  431.  
  432.         if ($grouping != 'post' && $widget["min_count"] > 0)
  433.             $group.= " having count(*) >= ".$widget["min_count"];
  434.  
  435.         if (is_array($widget["select"])) {
  436.             $where[] = "p.post_type in ('".join("', '", $widget["select"])."')";
  437.         } else {
  438.             if ($widget["select"] != "" && $widget["select"] != "postpage")
  439.                 $where[] = "p.post_type = '".$widget["select"]."'";
  440.         }
  441.  
  442.         if ($min > 0) {
  443.             if ($widget["show"] == "total") $where[] = "(d.user_recc_plus + d.user_recc_minus + d.visitor_recc_plus + d.visitor_recc_minus) >= ".$min;
  444.             if ($widget["show"] == "visitors") $where[] = "(d.visitor_recc_plus + d.visitor_recc_minus) >= ".$min;
  445.             if ($widget["show"] == "users") $where[] = "(d.user_recc_plus + d.user_recc_minus) >= ".$min;
  446.         }
  447.         if ($widget["hide_noreview"] == "1") $where[] = "d.review > -1";
  448.  
  449.         $sort = ($widget["order"] == "desc" || $widget["order"] == "asc") ? $widget["order"] : $sort = "desc";
  450.  
  451.         if ($widget["last_voted_days"] == "") $widget["last_voted_days"] = 0;
  452.         if ($widget["last_voted_days"] > 0) {
  453.             $where[] = "TO_DAYS(CURDATE()) - ".$widget["last_voted_days"]." <= TO_DAYS(d.last_voted_recc)";
  454.         }
  455.  
  456.         if ($widget["publish_date"] == "range") {
  457.             $where[] = "p.post_date >= '".$widget["publish_range_from"]."' and p.post_date <= '".$widget["publish_range_to"]."'";
  458.         } else if ($widget["publish_date"] == "month") {
  459.             $month = $widget["publish_month"];
  460.             if ($month != "" && $month != "0") {
  461.                 $where[] = "year(p.post_date) = ".substr($month, 0, 4);
  462.                 $where[] = "month(p.post_date) = ".substr($month, 4, 2);
  463.             }
  464.         } else if ($widget["publish_date"] == "lastd") {
  465.             if ($widget["publish_days"] > 0)
  466.                 $where[] = "TO_DAYS(CURDATE()) - ".$widget["publish_days"]." <= TO_DAYS(p.post_date)";
  467.         }
  468.         $select = "p.post_content, p.post_excerpt, '' as content, '' as excerpt, ".$select;
  469.  
  470.         $col = $widget["column"];
  471.         if ($col == "title") $col = $col_title;
  472.         else if ($col == "review") $col = "d.review";
  473.         else if ($col == "rating" || $col == "bayesian") {
  474.             if ($widget["show"] == "total") $col = "d.user_recc_plus - d.user_recc_minus + d.visitor_recc_plus - d.visitor_recc_minus";
  475.             if ($widget["show"] == "visitors") $col = "d.visitor_recc_plus - d.visitor_recc_minus";
  476.             if ($widget["show"] == "users") $col = "d.user_recc_plus - d.user_recc_minus";
  477.         }
  478.         else if ($col == "voters") {
  479.             if ($widget["show"] == "total") $col = "d.user_recc_plus + d.user_recc_minus + d.visitor_recc_plus + d.visitor_recc_minus";
  480.             if ($widget["show"] == "visitors") $col = "d.visitor_recc_plus + d.visitor_recc_minus";
  481.             if ($widget["show"] == "users") $col = "d.user_recc_plus + d.user_recc_minus";
  482.         }
  483.         else if ($col == "counter" && $grouping != "post") $col = "count(*)";
  484.         else $col = $col_id;
  485.         $ordering = sprintf("order by %s %s", $col, $sort);
  486.  
  487.         $query = array(
  488.             "select" => "distinct ".$select.$extras,
  489.             "from" => sprintf("%s%sposts p, %sgdsr_data_article d", $from, $table_prefix, $table_prefix),
  490.             "where" => join(" and ", $where),
  491.             "group" => $group,
  492.             "order" => sprintf("%s %s", $col, $sort),
  493.             "limit" => "0, ".$widget["rows"]
  494.         );
  495.  
  496.         return $query;
  497.     }
  498.  
  499.     function get_widget_standard($widget, $min = 0) {
  500.         global $wpdb, $table_prefix;
  501.  
  502.         $grouping = $widget["grouping"];
  503.         $cats = $widget["category"];
  504.         $cats_in = $widget["category_toponly"] == 0;
  505.         $select = $from = $group = "";
  506.         $where = array("p.id = d.post_id", "p.post_status = 'publish'");
  507.         $extras = ", 0 as votes, 0 as voters, 0 as rating, 0 as bayesian, '' as item_trend_rating, '' as item_trend_voting, '' as permalink, '' as tense, '' as rating_stars, '' as bayesian_stars, '' as review_stars";
  508.  
  509.         if ($cats_in && $cats != "0") {
  510.             $subs = gdWPGDSR::get_subcategories_ids($widget["category"]);
  511.             $subs[] = $cats;
  512.             $cats = join(",", $subs);
  513.         }
  514.  
  515.         if (isset($widget["categories"]) && $widget["categories"] != "") {
  516.             $cats = $widget["categories"];
  517.             $cats_in = true;
  518.         }
  519.  
  520.         if ($widget["bayesian_calculation"] == "0") $min = 0;
  521.         if ($widget["min_votes"] > $min) $min = $widget["min_votes"];
  522.         if ($min == 0 && $widget["hide_empty"] == "1") $min = 1;
  523.  
  524.         if (($cats != "" && $cats != "0") || $grouping == 'category'){
  525.             $from = sprintf("%sterm_taxonomy t, %sterm_relationships r, ", $table_prefix, $table_prefix);
  526.             $where[] = "t.term_taxonomy_id = r.term_taxonomy_id";
  527.             $where[] = "r.object_id = p.id";
  528.         }
  529.         if ($cats != "" && $cats != "0") {
  530.             $where[] = "t.taxonomy = 'category'";
  531.             if ($cats_in) $where[] = "t.term_id in (".$cats.")";
  532.             else $where[] = "t.term_id = ".$cats;
  533.         }
  534.  
  535.         $col_id = "p.id";
  536.         $col_title = "p.post_title";
  537.         if ($grouping == 'taxonomy') {
  538.             $from.= sprintf("%sterm_taxonomy tt, %sterm_relationships tr, %sterms tx, ", $table_prefix, $table_prefix, $table_prefix);
  539.             $where[] = "tt.taxonomy = '".$widget["taxonomy"]."'";
  540.             $where[] = "tt.term_taxonomy_id = tr.term_taxonomy_id";
  541.             $where[] = "tt.term_id = tx.term_id";
  542.             $where[] = "tr.object_id = p.id";
  543.             $select = "tx.name as title, tx.term_id, tx.slug, count(*) as counter, sum(d.user_votes) as user_votes, sum(d.visitor_votes) as visitor_votes, sum(d.user_voters) as user_voters, sum(d.visitor_voters) as visitor_voters";
  544.             $select.= ", sum(d.review) as review";
  545.             $group = $col_id = "tt.term_id";
  546.             $col_title = "tx.name";
  547.         } else if ($grouping == 'category') {
  548.             $from.= sprintf("%sterms x, ", $table_prefix);
  549.             $where[] = "t.taxonomy = 'category'";
  550.             $where[] = "t.term_id = x.term_id";
  551.             $select = "x.name as title, x.term_id, x.slug, count(*) as counter, sum(d.user_votes) as user_votes, sum(d.visitor_votes) as visitor_votes, sum(d.user_voters) as user_voters, sum(d.visitor_voters) as visitor_voters";
  552.             $select.= ", sum(d.review) as review";
  553.             $group = $col_id = "t.term_id";
  554.             $col_title = "x.name";
  555.         } else if ($grouping == 'user') {
  556.             $from.= sprintf("%s u, ", $wpdb->users);
  557.             $where[] = "u.id = p.post_author";
  558.             $select = "u.display_name as title, u.user_nicename as slug, u.id, count(*) as counter, sum(d.user_votes) as user_votes, sum(d.visitor_votes) as visitor_votes, sum(d.user_voters) as user_voters, sum(d.visitor_voters) as visitor_voters";
  559.             $select.= ", sum(d.review) as review";
  560.             $group = $col_id = "u.id";
  561.             $col_title = "u.display_name";
  562.         } else {
  563.             $select = "p.id as post_id, p.post_name as slug, p.post_author as author, p.post_title as title, p.post_type, p.post_date, d.*, 1 as counter";
  564.         }
  565.  
  566.         if ($grouping != 'post' && $widget["min_count"] > 0)
  567.             $group.= " having count(*) >= ".$widget["min_count"];
  568.  
  569.         if (is_array($widget["select"])) {
  570.             $where[] = "p.post_type in ('".join("', '", $widget["select"])."')";
  571.         } else {
  572.             if ($widget["select"] != "" && $widget["select"] != "postpage")
  573.                 $where[] = "p.post_type = '".$widget["select"]."'";
  574.         }
  575.  
  576.         if ($min > 0) {
  577.             if ($widget["show"] == "total") $where[] = "(d.user_voters + d.visitor_voters) >= ".$min;
  578.             if ($widget["show"] == "visitors") $where[] = "d.visitor_voters >= ".$min;
  579.             if ($widget["show"] == "users") $where[] = "d.user_voters >= ".$min;
  580.         }
  581.         if ($widget["hide_noreview"] == "1") $where[] = "d.review > -1";
  582.  
  583.         $sort = ($widget["order"] == "desc" || $widget["order"] == "asc") ? $widget["order"] : $sort = "desc";
  584.  
  585.         if ($widget["last_voted_days"] == "") $widget["last_voted_days"] = 0;
  586.         if ($widget["last_voted_days"] > 0) {
  587.             $where[] = "TO_DAYS(CURDATE()) - ".$widget["last_voted_days"]." <= TO_DAYS(d.last_voted)";
  588.         }
  589.  
  590.         if ($widget["publish_date"] == "range") {
  591.             $where[] = "p.post_date >= '".$widget["publish_range_from"]."' and p.post_date <= '".$widget["publish_range_to"]."'";
  592.         } else if ($widget["publish_date"] == "month") {
  593.             $month = $widget["publish_month"];
  594.             if ($month != "" && $month != "0") {
  595.                 $where[] = "year(p.post_date) = ".substr($month, 0, 4);
  596.                 $where[] = "month(p.post_date) = ".substr($month, 4, 2);
  597.             }
  598.         } else if ($widget["publish_date"] == "lastd") {
  599.             if ($widget["publish_days"] > 0)
  600.                 $where[] = "TO_DAYS(CURDATE()) - ".$widget["publish_days"]." <= TO_DAYS(p.post_date)";
  601.         }
  602.         $select = "p.post_content, p.post_excerpt, '' as content, '' as excerpt, ".$select;
  603.  
  604.         $col = $widget["column"];
  605.         if ($col == "title") $col = $col_title;
  606.         else if ($col == "review") $col = "d.review";
  607.         else if ($col == "rating" || $col == "bayesian") {
  608.             if ($widget["show"] == "total") $col = "(d.user_votes + d.visitor_votes)/(d.user_voters + d.visitor_voters)";
  609.             if ($widget["show"] == "visitors") $col = "d.visitor_votes/d.visitor_voters";
  610.             if ($widget["show"] == "users") $col = "d.user_votes/d.user_voters";
  611.         } else if ($col == "voters") {
  612.             if ($widget["show"] == "total") $col = "d.user_votes + d.visitor_votes";
  613.             if ($widget["show"] == "visitors") $col = "d.visitor_votes";
  614.             if ($widget["show"] == "users") $col = "d.user_votes";
  615.         } else if ($col == "counter" && $grouping != "post") $col = "count(*)";
  616.         else $col = $col_id;
  617.         $ordering = sprintf("order by %s %s", $col, $sort);
  618.  
  619.         $query = array(
  620.             "select" => "distinct ".$select.$extras,
  621.             "from" => sprintf("%s%sposts p, %sgdsr_data_article d", $from, $table_prefix, $table_prefix),
  622.             "where" => join(" and ", $where),
  623.             "group" => $group,
  624.             "order" => sprintf("%s %s", $col, $sort),
  625.             "limit" => "0, ".$widget["rows"]
  626.         );
  627.  
  628.         return $query;
  629.     }
  630.  
  631.     function get_widget_comments($widget, $post_id) {
  632.         global $table_prefix;
  633.  
  634.         $where = array();
  635.         $select = "p.comment_id, p.comment_author, p.comment_author_email, p.comment_author_url, p.comment_date, p.comment_content, p.user_id, d.*";
  636.         $extras = ", 0 as votes, 0 as voters, 0 as rating, '' as permalink, '' as tense, '' as rating_stars";
  637.         $min = $widget["min_votes"];
  638.         if ($min == 0 && $widget["hide_empty"] == "1") $min = 1;
  639.  
  640.         $where[] = "d.post_id = ".$post_id;
  641.         $where[] = "p.comment_id = d.comment_id";
  642.  
  643.         if ($min > 0) {
  644.             if ($widget["show"] == "total") $where[] = "(d.user_voters + d.visitor_voters) >= ".$min;
  645.             if ($widget["show"] == "visitors") $where[] = "d.visitor_voters >= ".$min;
  646.             if ($widget["show"] == "users") $where[] = "d.user_voters >= ".$min;
  647.         }
  648.  
  649.         if ($widget["order"] == "desc" || $widget["order"] == "asc")
  650.             $sort = $widget["order"];
  651.         else
  652.             $sort = "desc";
  653.  
  654.         if ($widget["last_voted_days"] == "") $widget["last_voted_days"] = 0;
  655.         if ($widget["last_voted_days"] > 0) {
  656.             $where[] = "TO_DAYS(CURDATE()) - ".$widget["last_voted_days"]." <= TO_DAYS(d.last_voted)";
  657.         }
  658.  
  659.         $sql = sprintf("select distinct %s%s from %scomments p, %sgdsr_data_comment d where %s limit 0, %s",
  660.                 $select, $extras, $table_prefix, $table_prefix, join(" and ", $where), $widget["rows"]);
  661.  
  662.         return $sql;
  663.     }
  664. }
  665.  
  666. class TrendValue {
  667.     var $votes_last = 0;
  668.     var $voters_last = 0;
  669.     var $rating_last = 0;
  670.     var $votes_over = 0;
  671.     var $voters_over = 0;
  672.     var $rating_over = 0;
  673.  
  674.     var $trend_rating = 0;
  675.     var $trend_voting = 0;
  676.     var $day_rate_voters = 0;
  677.  
  678.     function TrendValue($v_last, $r_last, $v_over, $r_over, $last = 1, $over = 30) {
  679.         $this->votes_last = $v_last;
  680.         $this->voters_last = $r_last;
  681.         $this->votes_over = $v_over;
  682.         $this->voters_over = $r_over;
  683.  
  684.         if ($over > 0) $this->day_rate_voters = $last / $over;
  685.  
  686.         $this->Calculate();
  687.     }
  688.  
  689.     function Calculate() {
  690.         if ($this->voters_last > 0) $this->rating_last = @number_format($this->votes_last / $this->voters_last, 1);
  691.         if ($this->voters_over > 0) $this->rating_over = @number_format($this->votes_over / $this->voters_over, 1);
  692.  
  693.         if ($this->rating_last > $this->rating_over ) $this->trend_rating = 1;
  694.         else if ($this->rating_last < $this->rating_over ) $this->trend_rating = -1;
  695.  
  696.         if ($this->voters_last > ($this->voters_over * $this->day_rate_voters)) $this->trend_voting = 1;
  697.         else if ($this->voters_last < ($this->voters_over * $this->day_rate_voters)) $this->trend_voting = -1;
  698.     }
  699. }
  700.  
  701. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement