private function getData()
 {
     /**
      * @todo
      */
     $on_page = 10;
     $data = array();
     $m = new shopCartItemsModel();
     $sql = "SELECT SUM(quantity) as qty, product_id FROM shop_cart_items WHERE " . $this->getTimeQuery() . ' GROUP BY product_id ORDER BY qty DESC';
     $items = $m->query($sql)->fetchAll('product_id', true);
     if ($items) {
         $other = 0;
         if (count($items) > $on_page) {
             $items1 = array();
             $i = 0;
             foreach ($items as $product_id => $quantity) {
                 if ($i++ < $on_page) {
                     $items1[$product_id] = $quantity;
                 } else {
                     $other += $quantity;
                 }
             }
             $items = $items1;
             unset($items1);
         }
         $pm = new shopProductModel();
         $products = $pm->select('id, name')->where('id IN(?)', array(array_keys($items)))->fetchAll('id', true);
         foreach ($items as $product_id => $quantity) {
             $data[] = array('label' => ifset($products[$product_id], _wp('(no name)')), 'value' => $quantity, 'id' => $product_id);
         }
         if ($other) {
             $data[] = array('label' => _wp('Other...'), 'value' => $other, 'id' => 'other');
         }
     }
     return $data;
 }
 private function workupList(&$list, $fields)
 {
     if (!$list) {
         return;
     }
     foreach ($list as &$v) {
         $v['icon'] = shopProductStocksLogModel::getIcon($v['type']);
         if (!$v['description']) {
             if ($v['after_count'] === null) {
                 $v['description'] = _w('In stock value updated to ∞');
             } else {
                 $v['description'] = sprintf(_w('In stock value updated to %d'), $v['after_count']);
             }
         } else {
             if ($v['type'] == self::TYPE_ORDER) {
                 $v['description'] = sprintf(_w($v['description']), '<a href="?action=orders#/order/' . $v['order_id'] . '/">' . shopHelper::encodeOrderId($v['order_id']) . '</a>');
             }
         }
     }
     unset($v);
     $stock_ids = array();
     foreach ($list as $v) {
         $stock_ids[] = $v['stock_id'];
     }
     $model = new shopStockModel();
     $stocks = $model->getByField('id', array_unique($stock_ids), 'id');
     foreach ($list as &$v) {
         if (isset($stocks[$v['stock_id']])) {
             $v['stock_name'] = $stocks[$v['stock_id']]['name'];
         }
     }
     unset($v);
     foreach ($fields as $f) {
         if ($f == 'sku_name') {
             $sku_ids = array();
             foreach ($list as $v) {
                 $sku_ids[] = $v['sku_id'];
             }
             $model = new shopProductSkusModel();
             $skus = $model->select('id,sku,name')->where("id IN(" . implode(',', array_unique($sku_ids)) . ")")->fetchAll('id');
             foreach ($list as &$v) {
                 if (isset($skus[$v['sku_id']])) {
                     $v['sku_name'] = $skus[$v['sku_id']]['name'];
                     if ($v['sku_name']) {
                         if ($skus[$v['sku_id']]['sku']) {
                             $v['sku_name'] .= ' (' . $skus[$v['sku_id']]['sku'] . ')';
                         }
                     } else {
                         if ($skus[$v['sku_id']]['sku']) {
                             $v['sku_name'] = $skus[$v['sku_id']]['sku'];
                         }
                     }
                 }
             }
             unset($v);
         }
         if ($f == 'product_name') {
             $product_ids = array();
             foreach ($list as $v) {
                 $product_ids[] = $v['product_id'];
             }
             $model = new shopProductModel();
             $products = $model->select('id,name')->where("id IN (" . implode(',', array_unique($product_ids)) . ")")->fetchAll('id');
             foreach ($list as &$v) {
                 if (isset($products[$v['product_id']])) {
                     $v['product_name'] = $products[$v['product_id']]['name'];
                 }
             }
             unset($v);
         }
     }
 }
 public function productsAutocomplete($q, $limit = null)
 {
     $limit = $limit !== null ? $limit : $this->limit;
     $product_model = new shopProductModel();
     $q = $product_model->escape($q, 'like');
     $fields = 'id, name AS value, price, count, sku_id';
     $products = $product_model->select($fields)->where("name LIKE '{$q}%'")->limit($limit)->fetchAll('id');
     $count = count($products);
     if ($count < $limit) {
         $product_skus_model = new shopProductSkusModel();
         $product_ids = array_keys($product_skus_model->select('id, product_id')->where("sku LIKE '{$q}%'")->limit($limit)->fetchAll('product_id'));
         if ($product_ids) {
             $data = $product_model->select($fields)->where('id IN (' . implode(',', $product_ids) . ')')->limit($limit - $count)->fetchAll('id');
             // not array_merge, because it makes first reset numeric keys and then make merge
             $products = $products + $data;
         }
     }
     // try find with LIKE %query%
     if (!$products) {
         $products = $product_model->select($fields)->where("name LIKE '%{$q}%'")->limit($limit)->fetchAll();
     }
     $currency = wa()->getConfig()->getCurrency();
     foreach ($products as &$p) {
         $p['price_str'] = wa_currency($p['price'], $currency);
     }
     unset($p);
     if (waRequest::get('with_sku_name')) {
         $sku_ids = array();
         foreach ($products as $p) {
             $sku_ids[] = $p['sku_id'];
         }
         $product_skus_model = new shopProductSkusModel();
         $skus = $product_skus_model->getByField('id', $sku_ids, 'id');
         $sku_names = array();
         foreach ($skus as $sku_id => $sku) {
             $name = '';
             if ($sku['name']) {
                 $name = $sku['name'];
                 if ($sku['sku']) {
                     $name .= ' (' . $sku['sku'] . ')';
                 }
             } else {
                 $name = $sku['sku'];
             }
             $sku_names[$sku_id] = $name;
         }
         foreach ($products as &$p) {
             $p['sku_name'] = $sku_names[$p['sku_id']];
         }
         unset($p);
     }
     return array_values($products);
 }