Example #1
0
/**
 * retrieve message for when someone tries to add an expired item to cart
 *
 * @return string message
 */
function OnlineStore_getExpiryNotification()
{
    $id = (int) $_REQUEST['id'];
    $p = dbRow('select * from products where id=' . $id);
    $product = Product::getInstance($id, $p, true);
    $typeid = $p['product_type_id'];
    $nfile = USERBASE . '/ww.cache/products/templates/expiry_notification_' . $typeid;
    if (!file_exists($nfile)) {
        $t = dbRow('select template_expired_notification from products_types where id=' . $typeid);
        $template = strlen($t['template_expired_notification']) > 4 ? $t['template_expired_notification'] : '' . __('This product has expired. You cannot add it to the cart.') . '';
        file_put_contents($nfile, $template);
    }
    require_once SCRIPTBASE . '/ww.incs/common.php';
    $smarty = Products_setupSmarty();
    $smarty->assign('product', $product);
    $smarty->assign('product_id', $product->get('id'));
    $smarty->assign('_name', __FromJson($product->name));
    $smarty->assign('_stock_number', $product->stock_number);
    return $smarty->fetch($nfile);
}
Example #2
0
 /**
  * produce a HTML version of the product
  *
  * @param string  $product     the product to render
  * @param string  $template    multi-view product or single-view?
  * @param boolean $add_wrapper wrap in div.products-product before return
  *
  * @return string html of the product
  */
 function render($product, $template = 'singleview', $add_wrapper = true)
 {
     global $DBVARS, $PAGEDATA;
     $GLOBALS['products_template_used'] = $template;
     if (isset($DBVARS['online_store_currency'])) {
         $csym = $DBVARS['online_store_currency'];
     }
     $smarty = Products_setupSmarty();
     $smarty->assign('product', $product);
     $smarty->assign('product_id', $product->get('id'));
     $smarty->assign('_name', __FromJson($product->name));
     $smarty->assign('_stock_number', $product->stock_number);
     if (!is_array(@$this->data_fields)) {
         $this->data_fields = array();
     }
     $productVals = array();
     foreach ($this->data_fields as $f) {
         $f->n = preg_replace('/[^a-zA-Z0-9\\-_]/', '_', $f->n);
         $val = $product->get($f->n);
         $required = @$f->r ? ' required' : '';
         switch ($f->t) {
             case 'checkbox':
                 // {
                 $val = $val ? __('Yes') : __('No');
                 $smarty->assign($f->n, $val);
                 break;
                 // }
             // }
             case 'colour':
                 // {
                 if (@$f->u) {
                     // user-definable
                     WW_addScript('/j/mColorPicker/mColorPicker.js');
                     $h = '<input class="color-picker" ' . 'name="products_values_' . $f->n . '" ' . 'style="height:20px;width:20px;" ' . 'value="' . htmlspecialchars($val) . '" ' . 'data-text="hidden"/>' . '<style>#mColorPickerFooter,#mColorPickerImg{display:none}</style>';
                     WW_addInlineScript('$(".color-picker")' . '.mColorPicker({"imageFolder":"/j/mColorPicker/images/"});');
                 } else {
                     $h = 'TODO';
                 }
                 $smarty->assign($f->n, $h);
                 break;
                 // }
             // }
             case 'date':
                 // {
                 if (@$f->u) {
                     // user-definable
                     $smarty->assign($f->n, '<input class="product-field date ' . $f->n . $required . '" name="' . 'products_values_' . $f->n . '"/>');
                     $format = @$f->e ? $f->e : 'yy-mm-dd';
                     $y = date('Y');
                     WW_addInlineScript('$("input[name=products_values_' . $f->n . ']").datepicker({' . '"dateFormat":"' . $format . '",' . 'changeYear:true,changeMonth:true,yearRange:"1900:' . $y . '"' . '});');
                     WW_addInlineScript('$("input.hasDatepicker").each(function() {' . 'if (this.value!="") return;' . '$(this).datepicker("setDate", "+0");' . '});');
                 } else {
                     $val = Core_dateM2H($val);
                     $smarty->assign($f->n, $val);
                 }
                 break;
                 // }
             // }
             case 'hidden':
                 // {
                 $smarty->assign($f->n, '<input type="hidden" name="products_values_' . $f->n . '" value="' . htmlspecialchars($val) . '"/>');
                 break;
                 // }
             // }
             case 'selectbox':
                 // {
                 if (@$f->u) {
                     $valid_entries = explode("\n", $val);
                     foreach ($valid_entries as $k => $v) {
                         $v = trim($v);
                         if ($v == '') {
                             unset($valid_entries[$k]);
                         } else {
                             $valid_entries[$k] = $v;
                         }
                     }
                     if (!count($valid_entries)) {
                         $valid_entries = explode("\n", $f->e);
                     }
                     $h = '<select name="products_values_' . $f->n . '" class="' . $required . '">';
                     $translateable = @$f->tr && 1;
                     foreach ($valid_entries as $e) {
                         $e = trim($e);
                         if ($e == '' || !in_array($e, $valid_entries)) {
                             continue;
                         }
                         $o = $e;
                         $p = '';
                         if (strpos($e, '|') !== false) {
                             $bits = explode('|', $e);
                             $e = $bits[0];
                             $p = 'price="' . (int) $bits[1] . '"';
                         }
                         $h .= '<option ' . $p . ' value="' . htmlspecialchars($o) . '"';
                         if ($translateable) {
                             $h .= ' class="__"';
                         }
                         $h .= '>' . htmlspecialchars($e) . '</option>';
                     }
                     $h .= '</select>';
                 } else {
                     $val = preg_replace('/\\|.*/', '', $val);
                     $h = $val;
                 }
                 $smarty->assign($f->n, $h);
                 break;
                 // }
             // }
             case 'selected-image':
                 // {
                 $smarty->assign($f->n, '<input type="hidden" name="products_values_' . $f->n . '" ' . 'class="product-field ' . $f->n . $required . '"/>');
                 break;
                 // }
             // }
             case 'textarea':
                 // { textarea
                 if (@$f->u) {
                     $val = trim(preg_replace('/<[^>]*>/', '', $val));
                     $smarty->assign($f->n, '<textarea class="product-field ' . $f->n . $required . '" name="products_values_' . $f->n . '">' . htmlspecialchars($val) . '</textarea>');
                 } else {
                     $smarty->assign($f->n, $val);
                 }
                 break;
                 // }
             // }
             case 'user':
                 // {
                 $u = User::getInstance($val, false, false);
                 $val = $u ? $u->get('name') : 'no name';
                 $smarty->assign($f->n, $val);
                 break;
                 // }
             // }
             default:
                 // { everything else
                 if (@$f->u) {
                     $smarty->assign($f->n, '<input class="product-field ' . $f->n . $required . '" value="' . htmlspecialchars($val) . '" name="products_values_' . $f->n . '"/>');
                 } else {
                     $smarty->assign($f->n, $val);
                 }
                 // }
         }
         $productVals[$f->n] = $val;
         $PAGEDATA->title = str_replace('{{$' . $f->n . '}}', $val, $PAGEDATA->title);
     }
     if (isset($PAGEDATA->vars['products_pagedescriptionoverride']) && $PAGEDATA->vars['products_pagedescriptionoverride']) {
         $desc = preg_replace('/<[^>]*>/', '', $productVals['description']);
         $desc = trim(preg_replace('/\\s+/m', ' ', $desc));
         $PAGEDATA->description = substr($desc, 0, 153) . '...';
     }
     if (isset($product->ean)) {
         $smarty->assign('_ean', $product->ean);
     }
     // { $_name, $_stock_number, $_ean
     $PAGEDATA->title = str_replace(array('{{$_name}}', '{{$_stock_number}}', '{{$_ean}}'), array($product->get('_name'), $product->get('_stock_number'), $product->vals['ean']), $PAGEDATA->title);
     // }
     $html = '';
     if ($add_wrapper) {
         $classes = array('products-product');
         if ($this->stock_control) {
             $classes[] = 'stock-control';
         }
         $html .= '<div class="' . join(' ', $classes) . '" id="products-' . $product->get('id') . '">';
     }
     $html .= $smarty->fetch(USERBASE . '/ww.cache/products/templates/types_' . $template . '_' . $this->id);
     if ($template == 'singleview') {
         $PAGEDATA->vars['header_html'] = (isset($PAGEDATA->vars['header_html']) ? $PAGEDATA->vars['header_html'] : '') . '<link rel="canonical" href="' . htmlspecialchars($product->getRelativeUrl()) . '" />';
         if ($this->allow_comments) {
             $html .= Core_commentsShow('http://' . $_SERVER['HTTP_HOST'] . $product->getRelativeURL());
         }
     }
     if ($add_wrapper) {
         $html .= '</div>';
     }
     return $html;
 }
Example #3
0
 /**
  * render a list of products to HTML
  *
  * @param object $PAGEDATA      the page object
  * @param int    $start         offset
  * @param int    $limit         how many products to show
  * @param string $order_by      what field to order the search by
  * @param int    $order_dir     order ascending or descending
  * @param int    $limit_start   lowest $start offset allowed
  * @param int    $enabledFilter whether to allow enabled/disabled products
  *
  * @return string the HTML of the products list
  */
 function render($PAGEDATA, $start = 0, $limit = 0, $order_by = '', $order_dir = 0, $limit_start = 0, $enabledFilter = 0)
 {
     global $cdnprefix;
     $c = '';
     // { sort based on $order_by
     $md5 = md5('ps-sorted-' . join(',', $this->product_ids) . '|' . $order_by . '|' . $order_dir . '|' . $enabledFilter);
     $tmpprods = -1;
     if ($order_dir != 2) {
         $tmpprods = Core_cacheLoad('products', $md5, -1);
     }
     if ($tmpprods == -1) {
         if ($order_by != '') {
             $native = substr($order_by, 0, 1) === '_';
             $tmpprods1 = array();
             $prods = $this->product_ids;
             $sql = 'select id';
             if (!$native) {
                 $sql .= ',data_fields';
             }
             $sql .= ' from products where id in (' . join(', ', $this->product_ids) . ')';
             if ($enabledFilter == 0) {
                 $sql .= ' and enabled';
             }
             if ($enabledFilter == 1) {
             }
             if ($enabledFilter == 2) {
                 $sql .= ' and !enabled';
             }
             if ($native) {
                 $sql .= ' order by ' . substr($order_by, 1, strlen($order_by) - 1);
                 if ($order_dir == 1) {
                     $sql .= ' desc';
                 }
             }
             $values = dbAll($sql, '', 'products');
             if ($native) {
                 $tmpprods = array();
                 if (is_array($values)) {
                     foreach ($values as $v) {
                         $tmpprods[] = $v['id'];
                     }
                     if ($order_dir == 2) {
                         shuffle($tmpprods);
                     }
                 }
             } else {
                 if (is_array($values)) {
                     foreach ($values as $v) {
                         $vals = json_decode($v['data_fields'], true);
                         $key2 = '';
                         foreach ($vals as $v2) {
                             if ($v2['n'] == $order_by) {
                                 $key2 = __FromJSON($v2['v']);
                             }
                         }
                         if (!isset($tmpprods1[$key2])) {
                             $tmpprods1[$key2] = array();
                         }
                         $tmpprods1[$key2][] = $v['id'];
                     }
                 }
                 if ($order_dir == 1) {
                     krsort($tmpprods1);
                 } else {
                     if ($order_dir == 0) {
                         ksort($tmpprods1);
                     } else {
                         if ($order_dir == 2) {
                             shuffle($tmpprods1);
                         }
                     }
                 }
                 $tmpprods = array();
                 foreach ($tmpprods1 as $pids) {
                     foreach ($pids as $pid) {
                         $tmpprods[] = $pid;
                     }
                 }
                 foreach ($prods as $key => $pid) {
                     $tmpprods[] = $pid;
                 }
             }
         } else {
             $tmpprods = $this->product_ids;
         }
         Core_cacheSave('products', $md5, $tmpprods);
     }
     // }
     // { sanitise the limits
     $cnt = count($tmpprods);
     if (!$limit) {
         $limit = $cnt;
         $start = 0;
     } else {
         if ($start && $start >= count($this->product_ids)) {
             $start = $cnt - $limit;
         }
     }
     // }
     // { build array of items
     $prevnext = '';
     $total_found = count($tmpprods);
     if ($cnt == $limit) {
         $prods =& $tmpprods;
     } else {
         $prods = array();
         for ($i = $start; $i < $limit + $start; ++$i) {
             if (isset($tmpprods[$i])) {
                 $prods[] = $tmpprods[$i];
             }
         }
         $prefix = '';
         if ($PAGEDATA->vars['products_what_to_show'] == 2) {
             $cat = ProductCategory::getInstance($PAGEDATA->vars['products_category_to_show']);
             if ($cat) {
                 $prefix = $cat->getRelativeUrl();
             }
         }
         if (!$prefix) {
             $prefix = $PAGEDATA->getRelativeUrl();
         }
         if ($start > $limit_start) {
             $prevnext .= '<a class="products-prev" href="' . $prefix . '?start=' . ($start - $limit) . '">' . __('Previous') . '</a>';
         }
         if ($limit && $start + $limit < $cnt) {
             if ($start) {
                 $prevnext .= ' | ';
             }
             $prevnext .= '<a class="products-next" href="' . $prefix . '?start=' . ($start + $limit) . '">' . __('Next') . '</a>';
         }
     }
     $prevnext = '<div class="products-pagination">' . $prevnext . '</div>';
     // }
     // { see if there are search results
     if (isset($PAGEDATA->vars['products_add_a_search_box']) && $PAGEDATA->vars['products_add_a_search_box']) {
         $c .= '<div class="products-num-results">' . __('<strong>%1</strong> results found.', array($total_found), 'core') . '</div>';
     }
     // }
     if (!isset($PAGEDATA->vars['products_show_multiple_with'])) {
         $PAGEDATA->vars['products_show_multiple_with'] = 0;
     }
     $prods = array_unique($prods);
     switch ($PAGEDATA->vars['products_show_multiple_with']) {
         case 1:
             // { horizontal table, headers on top
             $c .= Product_datatableMultiple($prods, 'horizontal');
             break;
             // }
         // }
         case 2:
             // { vertical table, headers on left
             $c .= Product_datatableMultiple($prods, 'vertical');
             break;
             // }
         // }
         case 3:
             // { map view
             WW_addScript('products');
             WW_addCSS('/ww.plugins/products/products.css');
             return '<div id="products-mapview"></div>';
             // }
         // }
         case 4:
             // { carousel
             WW_addScript('products');
             $c = '<div id="products-carousel"><ul id="products-carousel-slider">';
             foreach ($prods as $pid) {
                 $product = Product::getInstance($pid, false, $enabledFilter);
                 if ($product && isset($product->id) && $product->id) {
                     $typeID = $product->get('product_type_id');
                     $type = ProductType::getInstance($typeID);
                     if (!$type) {
                         $c .= '<li>' . __('Missing Product Type: %1', array($typeID), 'core') . '</li>';
                     } else {
                         $c .= '<li id="products-' . $product->id . '" class="products-product">' . $type->render($product, 'multiview', 0) . '</li>';
                     }
                 }
             }
             $c .= '</ul></div>';
             WW_addScript('/j/jsor-jcarousel-7bb2e0a/jquery.jcarousel.min.js');
             WW_addCSS('/ww.plugins/products/products.css');
             return $c;
             // }
         // }
         default:
             // { use template
             if (count($prods)) {
                 // display the first item's header
                 $product = Product::getInstance($prods[0], false, $enabledFilter);
                 $type = ProductType::getInstance($product->get('product_type_id'));
                 if ($type) {
                     $smarty = Products_setupSmarty();
                     $c .= $smarty->fetch(USERBASE . '/ww.cache/products/templates/types_multiview_' . $type->id . '_header');
                 }
             }
             foreach ($prods as $pid) {
                 $product = Product::getInstance($pid, false, $enabledFilter);
                 if ($product && isset($product->id) && $product->id) {
                     $typeID = $product->get('product_type_id');
                     $type = ProductType::getInstance($typeID);
                     if (!$type) {
                         $c .= __('Missing Product Type: %1', array($typeID), 'core');
                     } else {
                         if (isset($_REQUEST['product_id'])) {
                             $c .= $type->render($product, 'singleview');
                         } else {
                             $c .= $type->render($product, 'multiview');
                         }
                     }
                 }
             }
             if (isset($type) && $type && count($prods)) {
                 // display first item's header
                 $smarty = Products_setupSmarty();
                 $c .= $smarty->fetch(USERBASE . '/ww.cache/products/templates/types_multiview_' . $type->id . '_footer');
             }
             // }
     }
     $categories = '';
     if (!isset($_REQUEST['products-search']) && isset($this->subCategories) && count($this->subCategories) && !@$PAGEDATA->vars['products_dont_show_sub_categories']) {
         $categories = '<ul class="products-categories categories">';
         foreach ($this->subCategories as $cr) {
             $cat = ProductCategory::getInstance($cr['id']);
             $categories .= '<li><a href="' . $cat->getRelativeUrl() . '">';
             $icon = '/products/categories/' . $cr['id'] . '/icon.png';
             if (file_exists(USERBASE . 'f' . $icon)) {
                 $subcatW = (int) $cat->vals['thumbsize_w'];
                 $subcatH = (int) $cat->vals['thumbsize_h'];
                 $categories .= '<img src="' . $cdnprefix . '/a/f=getImg/w=' . $subcatW . '/h=' . $subcatH . '/fmt=' . filemtime(USERBASE . 'f' . $icon) . $icon . '"/>';
             }
             $categories .= '<span>' . htmlspecialchars($cr['name']) . '</span>' . '</a></li>';
         }
         $categories .= '</ul>';
     }
     return $categories . $prevnext . '<div class="products">' . $c . '</div>' . $prevnext;
 }