Example #1
0
/**
*   Diaplay the product catalog items.
*
*   @return string      HTML for product catalog.
*/
function PAYPAL_ProductList($cat = 0, $search = '')
{
    global $_TABLES, $_CONF, $_PP_CONF, $LANG_PP, $_USER, $_PLUGINS, $_IMAGE_TYPE, $_GROUPS;
    USES_paypal_class_product();
    if (SEC_hasRights('paypal.admin')) {
        $isAdmin = true;
    } else {
        $isAdmin = false;
    }
    $my_groups = implode(',', $_GROUPS);
    $cat_name = '';
    $breadcrumbs = '';
    $img_url = '';
    $display = '';
    if ($cat != 0) {
        $breadcrumbs = PAYPAL_Breadcrumbs($cat);
        $cat = (int) $cat;
        $A = DB_fetchArray(DB_query("SELECT cat_name, image\n                FROM {$_TABLES['paypal.categories']}\n                WHERE cat_id='{$cat}' " . COM_getPermSQL('AND')), false);
        if (!empty($A)) {
            $cat_name = $A['cat_name'];
            if (!empty($A['image']) && is_file($_CONF['path_html'] . $_PP_CONF['pi_name'] . '/images/categories/' . $A['image'])) {
                $img_url = PAYPAL_URL . '/images/categories/' . $A['image'];
            }
        }
    }
    // Display categories
    if (isset($_PP_CONF['cat_columns']) && $_PP_CONF['cat_columns'] > 0) {
        $sql = "SELECT cat.cat_id, cat.cat_name, count(prod.id) AS cnt \n            FROM {$_TABLES['paypal.categories']} cat\n            LEFT JOIN {$_TABLES['paypal.products']} prod\n                ON prod.cat_id = cat.cat_id\n            WHERE cat.enabled = '1' AND cat.parent_id = '{$cat}' \n                AND prod.enabled = '1' " . COM_getPermSQL('AND', 0, 2, 'cat') . " GROUP BY cat.cat_id\n            ORDER BY cat.cat_name";
        //HAVING cnt > 0
        //echo $sql;die;
        $CT = new Template(PAYPAL_PI_PATH . '/templates');
        $CT->set_file(array('table' => 'category_table.thtml', 'row' => 'category_row.thtml', 'category' => 'category.thtml'));
        $CT->set_var('width', floor(100 / $_PP_CONF['cat_columns']));
        if ($breadcrumbs != '') {
            $CT->set_var('breadcrumbs', $breadcrumbs);
        }
        if ($img_url != '') {
            $CT->set_var('catimg_url', $img_url);
        }
        $res = DB_query($sql);
        $A = array();
        while ($C = DB_fetchArray($res, false)) {
            $A[$C['cat_id']] = array($C['cat_name'], $C['cnt']);
        }
        // Now get categories from plugins
        foreach ($_PLUGINS as $pi_name) {
            $function = 'USES_' . $pi_name . '_paypal';
            if (function_exists($function)) {
                $function();
                $function = 'plugin_paypal_getcategories_' . $pi_name;
                if (function_exists($function)) {
                    $pi_cats = $function();
                    foreach ($pi_cats as $catid => $data) {
                        $A[$catid] = $data;
                    }
                }
            }
        }
        $i = 1;
        $nrows = count($A);
        foreach ($A as $category => $info) {
            $CT->set_var(array('category_name' => $info[0], 'category_link' => PAYPAL_URL . '/index.php?category=' . urlencode($category)));
            /*if ($category == $cat) {
                  $CT->set_var('curr', 'current');
                  $cat_name = $info[0];
              } else {
                  $CT->set_var('curr', 'other');
              }*/
            $CT->parse('catrow', 'category', true);
            if ($i % $_PP_CONF['cat_columns'] == 0) {
                $CT->parse('categories', 'row', true);
                $CT->set_var('catrow', '');
            }
            $i++;
        }
        if ($nrows % $_PP_CONF['cat_columns'] != 0) {
            $CT->parse('categories', 'row', true);
        }
        $display .= $CT->parse('', 'table');
    }
    /*$sortby_opts = array(
            'name' => $LANG_PP['name'],
            'price' => $LANG_PP['price'],
            'dt_add' => $LANG_PP['dt_add'],
        );
        switch ($_REQUEST['sortby']){
        case 'name':
        case 'price':
        case 'dt_add':
            $sortby = $_REQUEST['sortby'];
            break;
        default:
            $sortby = $_PP_CONF['order'];
            break;
        }
        $sortby_options = '';
        foreach ($sortby_opts as $value=>$text) {
            $sel = $value == $sortby ? ' selected="selected"' : '';
            $sortby_options .= "<option value=\"$value\" $sel>$text</option>\n";
        }
    
        $sortdir = $_REQUEST['sortdir'] == 'DESC' ? 'DESC' : 'ASC';*/
    $sortby = $_PP_CONF['order'];
    $sortdir = 'ASC';
    // Get products from database. "c.enabled is null" is to allow products
    // with no category defined
    $sql = " FROM {$_TABLES['paypal.products']} p\n            LEFT JOIN {$_TABLES['paypal.categories']} c\n                ON p.cat_id = c.cat_id\n            WHERE p.enabled=1 \n            AND (\n                (c.enabled=1 " . COM_getPermSQL('AND', 0, 2, 'c') . ")\n                OR c.enabled IS NULL\n                )\n            AND (\n                p.track_onhand = 0 OR p.onhand > 0 OR p.oversell < 2\n                )";
    $pagenav_args = array();
    // If applicable, limit by category
    if (!empty($_REQUEST['category'])) {
        $cat_list = $_REQUEST['category'];
        $cat_list .= PAYPAL_recurseCats('PAYPAL_callbackCatCommaList', 0, $_REQUEST['category']);
        if (!empty($cat_list)) {
            $sql .= " AND c.cat_id IN ({$cat_list})";
        }
        $pagenav_args[] = 'category=' . urlencode($_REQUEST['category']);
    } else {
        $cat_list = '';
    }
    // If applicable, limit by search string
    if (!empty($_REQUEST['search_name'])) {
        $srch = DB_escapeString($_REQUEST['search_name']);
        $sql .= " AND (p.name like '%{$srch}%' OR \n                p.short_description like '%{$srch}%' OR\n                p.description like '%{$srch}%' OR\n                p.keywords like '%{$srch}%')";
        //if (!$isAdmin) $sql .= " AND p.grp_access IN ($my_groups) ";
        $pagenav_args[] = 'search_name=' . urlencode($_REQUEST['search_name']);
    }
    // If applicable, order by
    $sql .= " ORDER BY {$sortby} {$sortdir}";
    // If applicable, handle pagination of query
    if (isset($_PP_CONF['prod_per_page']) && $_PP_CONF['prod_per_page'] > 0) {
        // Count products from database
        $res = DB_query('SELECT COUNT(*) as cnt ' . $sql);
        $x = DB_fetchArray($res, false);
        if (isset($x['cnt'])) {
            $count = (int) $x['cnt'];
        } else {
            $count = 0;
        }
        // Make sure page requested is reasonable, if not, fix it
        if (!isset($_REQUEST['page']) || $_REQUEST['page'] <= 0) {
            $_REQUEST['page'] = 1;
        }
        $page = (int) $_REQUEST['page'];
        $start_limit = ($page - 1) * $_PP_CONF['prod_per_page'];
        if ($start_limit > $count) {
            $page = ceil($count / $_PP_CONF['prod_per_page']);
        }
        // Add limit for pagination (if applicable)
        if ($count > $_PP_CONF['prod_per_page']) {
            $sql .= " LIMIT {$start_limit}, {$_PP_CONF['prod_per_page']}";
        }
    }
    // Re-execute query with the limit clause in place
    $res = DB_query('SELECT DISTINCT p.id ' . $sql);
    // Create product template
    $product = new Template(PAYPAL_PI_PATH . '/templates');
    $product->set_file(array('start' => 'product_list_start.thtml', 'end' => 'product_list_end.thtml', 'product' => 'product_list_item.thtml', 'download' => 'buttons/btn_download.thtml', 'login_req' => 'buttons/btn_login_req.thtml', 'btn_details' => 'buttons/btn_details.thtml'));
    if ($nrows == 0 && COM_isAnonUser()) {
        $product->set_var('anon_and_empty', 'true');
    }
    $product->set_var(array('pi_url' => PAYPAL_URL, 'user_id' => $_USER['uid'], 'currency' => $_PP_CONF['currency']));
    if (!empty($cat_name)) {
        $product->set_var('title', $cat_name);
    } else {
        $product->set_var('title', $LANG_PP['blocktitle']);
    }
    /*$product->set_var('sortby_options', $sortby_options);
      if ($sortdir == 'DESC') {
          $product->set_var('sortdir_desc_sel', ' selected="selected"');
      } else {
          $product->set_var('sortdir_asc_sel', ' selected="selected"');
      }
      $product->set_var('sortby', $sortby);
      $product->set_var('sortdir', $sortdir);*/
    $display .= $product->parse('', 'start');
    // Create an empty product object
    $P = new Product();
    if ($_PP_CONF['ena_ratings'] == 1) {
        $PP_ratedIds = RATING_getRatedIds('paypal');
    }
    // Display each product
    while ($A = DB_fetchArray($res, false)) {
        $P->Read($A['id']);
        if ($_PP_CONF['ena_ratings'] == 1 && $P->rating_enabled == 1) {
            if (in_array($A['id'], $PP_ratedIds)) {
                $static = true;
                $voted = 1;
            } elseif (plugin_canuserrate_paypal($A['id'], $_USER['uid'])) {
                $static = false;
                $voted = 0;
            } else {
                $static = true;
                $voted = 0;
            }
            $rating_box = RATING_ratingBar('paypal', $A['id'], $P->votes, $P->rating, $voted, 5, $static, 'sm');
            $product->set_var('rating_bar', $rating_box);
        } else {
            $product->set_var('rating_bar', '');
        }
        $product->set_var(array('id' => $A['id'], 'name' => $P->name, 'short_description' => PLG_replacetags($P->short_description), 'img_cell_width' => $_PP_CONF['max_thumb_size'] + 20, 'encrypted' => '', 'item_url' => COM_buildURL(PAYPAL_URL . '/detail.php?id=' . $A['id']), 'img_cell_width' => $_PP_CONF['max_thumb_size'] + 20, 'track_onhand' => $P->track_onhand ? 'true' : '', 'qty_onhand' => $P->onhand));
        if ($P->price > 0) {
            //$product->set_var('price', COM_numberFormat($P->price, 2));
            $product->set_var('price', $P->currency->Format($P->price));
        } else {
            $product->clear_var('price');
        }
        if ($isAdmin) {
            $product->set_var('is_admin', 'true');
            $product->set_var('pi_admin_url', PAYPAL_ADMIN_URL);
            $product->set_var('edit_icon', "{$_CONF['layout_url']}/images/edit.{$_IMAGE_TYPE}");
        }
        $pic_filename = DB_getItem($_TABLES['paypal.images'], 'filename', "product_id = '{$A['id']}'");
        if ($pic_filename) {
            $product->set_var('small_pic', PAYPAL_ImageUrl($pic_filename));
        } else {
            $product->set_var('small_pic', '');
        }
        // FIXME: If a user purchased once with no expiration, this query
        // will not operate correctly
        /*$time = DB_getItem($_TABLES['paypal.purchases'], 
                      'MAX(UNIX_TIMESTAMP(expiration))',
                      "user_id = {$_USER['uid']} AND product_id ='{$A['id']}'");
          */
        $product->set_block('product', 'BtnBlock', 'Btn');
        if (!$P->hasAttributes()) {
            // Buttons only show in the list if there are no options to select
            $buttons = $P->PurchaseLinks();
            foreach ($buttons as $name => $html) {
                $product->set_var('button', $html);
                $product->parse('Btn', 'BtnBlock', true);
            }
        } else {
            if ($_PP_CONF['ena_cart']) {
                // If the product has attributes, then the cart must be
                // enabled to allow purchasing
                $button = $product->parse('', 'btn_details') . '&nbsp;';
                $product->set_var('button', $button);
                $product->parse('Btn', 'BtnBlock', true);
            }
        }
        $display .= $product->parse('', 'product');
        $product->clear_var('Btn');
    }
    // Get products from plugins.
    // For now, this hack shows plugins only on the first page, since
    // they're not included in the page calculation.
    if ($page == 1 && empty($cat_list)) {
        // Get the currency class for formatting prices
        USES_paypal_class_currency();
        $Cur = new ppCurrency($_PP_CONF['currency']);
        $product->clear_var('rating_bar');
        // no ratings for plugins (yet)
        foreach ($_PLUGINS as $pi_name) {
            $status = LGLIB_invokeService($pi_name, 'getproducts', array(), $plugin_data, $svc_msg);
            if ($status != PLG_RET_OK || empty($plugin_data)) {
                continue;
            }
            foreach ($plugin_data as $A) {
                // Reset button values
                $buttons = '';
                $product->set_var(array('id' => $A['id'], 'name' => $A['name'], 'short_description' => $A['short_description'], 'display' => '; display: none', 'small_pic' => '', 'encrypted' => '', 'item_url' => $A['url'], 'track_onhand' => ''));
                if ($A['price'] > 0) {
                    $product->set_var('price', $Cur->Format($A['price']));
                } else {
                    $product->clear_var('price');
                }
                if ($A['price'] > 0 && $_USER['uid'] == 1 && !$_PP_CONF['anon_buy']) {
                    $buttons .= $product->set_var('', 'login_req') . '&nbsp;';
                } elseif ($A['prod_type'] > PP_PROD_PHYSICAL && $A['price'] == 0) {
                    // Free items or items purchases and not expired, download.
                    $buttons .= $product->set_var('', 'download') . '&nbsp;';
                } elseif (is_array($A['buttons'])) {
                    // Buttons for everyone else
                    $product->set_block('product', 'BtnBlock', 'Btn');
                    foreach ($A['buttons'] as $type => $html) {
                        $product->set_var('button', $html);
                        $product->parse('Btn', 'BtnBlock', true);
                    }
                }
                //$product->set_var('buttons', $buttons);
                $display .= $product->parse('', 'product');
                $product->clear_var('Btn');
            }
            // foreach plugin_data
        }
        // foreach $_PLUGINS
    }
    // if page == 1
    $pagenav_args = empty($pagenav_args) ? '' : '?' . implode('&', $pagenav_args);
    // Display pagination
    if (isset($_PP_CONF['prod_per_page']) && $_PP_CONF['prod_per_page'] > 0 && $count > $_PP_CONF['prod_per_page']) {
        $product->set_var('pagination', COM_printPageNavigation(PAYPAL_URL . '/index.php' . $pagenav_args, $page, ceil($count / $_PP_CONF['prod_per_page'])));
    } else {
        $product->set_var('pagination', '');
    }
    $display .= $product->parse('', 'end');
    return $display;
}