Example #1
0
 /**
  * @param int|null|array $category
  * @param bool $withExt -1 — без изображений, false — без расширенных полей, true — с расширенными полями
  * @param int $page
  * @param int|null $perPage
  * @param null|bool $visible
  * @param bool $withSubcategories
  * @param bool|array $ids
  * @return Item[]|bool
  */
 public static function getList($category = null, $withExt = false, $page = 1, $perPage = null, $visible = null, $withSubcategories = false, $ids = null)
 {
     if ($withSubcategories and !is_null($category)) {
         return self::getList(Category::getSubtree($category), $withExt === true, $page, $perPage, $visible, false, $ids);
     }
     $db = MySQL::getInstance();
     $query = 'SELECT SQL_CALC_FOUND_ROWS * FROM `catalog_items`';
     $where = [];
     if (is_array($category)) {
         foreach ($category as $k => $v) {
             $category[$k] = $db->escape($v);
         }
         $where[] = "`category` IN ('" . implode("','", $category) . "')";
     } elseif (!is_null($category)) {
         $where[] = "`category`='" . $db->escape($category) . "'";
     }
     if (!is_null($visible)) {
         $where[] = "`visible`=" . ($visible ? '1' : '0');
     }
     if ($ids) {
         foreach ($ids as $k => $v) {
             $ids[$k] = $db->escape($v);
         }
         $where[] = "`id` IN ('" . implode("','", $ids) . "')";
     }
     if (!empty($where)) {
         $query .= ' WHERE ' . implode(' AND ', $where);
     }
     $sort = self::getSort();
     switch ($sort) {
         case 'new':
             $query .= ' ORDER BY `id` DESC';
             break;
         case 'price':
             $query .= ' ORDER BY `price`';
             break;
         case null:
             $query .= " ORDER BY FIND_IN_SET( `id`, '" . implode(',', $ids) . "' )";
             break;
     }
     if ($perPage) {
         $query .= " LIMIT " . intval(($page - 1) * $perPage) . "," . intval($perPage);
     }
     $data = $db->fetch($query);
     self::$count = $db->getFoundRows();
     if (empty($data)) {
         return false;
     }
     $res = [];
     $k = 1;
     $ids = [];
     foreach ($data as $i) {
         $item = new self();
         $item->id = $i['id'];
         $item->name = $i['name'];
         $item->category = $i['category'];
         $item->visible = $i['visible'] ? true : false;
         $item->price = $i['price'] ? $i['price'] : null;
         $item->sale = $i['sale'] ? $i['sale'] : null;
         $item->shortdesc = $i['shortdesc'];
         $item->description = $i['description'];
         $item->link = $i['link'];
         $item->created = $i['created'];
         $item->humanDate = Locales::getInstance()->getDateFromMysql($i['created']);
         if ($withExt) {
             $item->loadedExt = true;
             // не правда, но чуть ниже получим
         }
         $res[$k] = $item;
         $ids[$k] = $db->escape($i['id']);
         $k++;
     }
     $keys = array_flip($ids);
     // изображения
     if ($withExt === -1) {
         return $res;
     }
     $imgData = $db->fetch("SELECT `id`,`item`,`main` FROM `catalog_images` WHERE `item` IN ('" . implode("','", $ids) . "')");
     foreach ($imgData as $i) {
         $res[$keys[$i['item']]]->images[] = ['main' => $i['main'] ? true : false, 'id' => $i['id']];
     }
     // exts
     if (!$withExt) {
         return $res;
     }
     $extData = $db->fetch("SELECT `item`,`catalog_items_ext`.`ext` AS `ext_id`,`catalog_ext`.`name` AS `ext_name`," . "`catalog_ext`.`set` AS `ext_type`,`catalog_ext_sets`.`id` AS `set_id`,`catalog_ext_sets`.`name` AS `set_value`," . "`catalog_items_ext`.`value` AS `ext_value`," . "`catalog_ext`.`position` AS `ext_position`,`catalog_ext_sets`.`position` AS `set_position`" . "FROM `catalog_items_ext` LEFT JOIN `catalog_ext` ON `catalog_items_ext`.`ext`=`catalog_ext`.`id` " . "LEFT JOIN `catalog_ext_sets` ON `catalog_items_ext`.`setvalue`=`catalog_ext_sets`.`id` " . "WHERE `item` IN ('" . implode("','", $ids) . "') " . "ORDER BY `item`");
     if (!empty($extData)) {
         foreach ($extData as $ext) {
             if ($ext['ext_value']) {
                 $res[$keys[$ext['item']]]->ext[$ext['ext_id']] = $ext;
             } else {
                 if (!isset($res[$keys[$ext['item']]]->ext[$ext['ext_id']])) {
                     $res[$keys[$ext['item']]]->ext[$ext['ext_id']] = [];
                 }
                 $res[$keys[$ext['item']]]->ext[$ext['ext_id']][$ext['set_id']] = $ext;
             }
         }
     }
     return $res;
 }