public function product() { $perPage = 10; $token = $this->slim->request->params('token'); if (empty($token)) { header('Content-Type: application/json'); echo json_encode(['error' => 'REQUIRE_AUTHORIZE'], JSON_UNESCAPED_SLASHES); exit; } $account = R::findOne("account", "token = ?", [$token]); if (!$account) { header('Content-Type: application/json'); echo json_encode(['error' => 'AUTHORIZE_FAILED'], JSON_UNESCAPED_SLASHES); exit; } $page = @$_GET['page'] ? $_GET['page'] : 1; $start = ($page - 1) * $perPage; $where = []; $queryParam = []; $where[] = "account_product.account_id = ?"; $queryParam[] = $account['id']; if (!empty($_GET['type'])) { $where[] = "product.type = ?"; $queryParam[] = $_GET['type']; } if (!empty($_GET['size'])) { $where[] = "product.size = ?"; $queryParam[] = $_GET['size']; } if (!empty($_GET['style'])) { $where[] = "product.style = ?"; $queryParam[] = $_GET['style']; } if (!empty($_GET['company'])) { $where[] = "product.company = ?"; $queryParam[] = $_GET['company']; } $where = implode(" AND ", $where); // $queryParam[] = $start; // $queryParam[] = $perPage; $items = R::getAll("SELECT product.* FROM account_product RIGHT JOIN product ON account_product.product_id = product.id WHERE " . $where . " LIMIT ?,?", array_merge($queryParam, [$start, $perPage])); $count = R::getCell("SELECT COUNT(product.id) FROM account_product RIGHT JOIN product ON account_product.product_id = product.id WHERE " . $where, $queryParam); $maxPage = floor($count / $perPage) + ($count % $perPage == 0 ? 0 : 1); //$itemsExport = R::exportAll($items); $itemsExport = $items; $this->buildProducts($itemsExport); header('Content-Type: application/json'); echo json_encode(['items' => $itemsExport, 'page' => $page, 'maxPage' => $maxPage, 'total' => $count], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); exit; }
public function save() { if (!$this->emptyAttr('id')) { $ecatalog = R::findOne('ecatalog', 'id=?', [$this->getAttr('id')]); $ecatalog->updated_at = date('Y-m-d H:i:s'); } else { $ecatalog = R::dispense('ecatalog'); $ecatalog->created_at = date('Y-m-d H:i:s'); $ecatalog->updated_at = date('Y-m-d H:i:s'); $ecatalog->sort_order = (int) R::getCell("SELECT MAX(sort_order) FROM ecatalog") + 1; } $ecatalog->name = $this->getAttr('name'); $ecatalog->is_new = $this->getAttr('is_new'); if (!$this->emptyAttr('pdf') && is_uploaded_file($this->attr['pdf']['tmp_name'])) { $pdf = $this->getAttr('pdf'); $pdf_path = $this->generateName('ecatalog_pdf_') . '.pdf'; $cover_path = $this->generateName('ecatalog_cover_') . '.jpeg'; $im = new \Imagick($pdf['tmp_name'] . '[0]'); $im->setImageBackgroundColor('white'); // $im = $im->flattenImages(); $im = $im->mergeImageLayers(\Imagick::LAYERMETHOD_FLATTEN); $im->setImageFormat('jpeg'); if ($im->getImageColorspace() == \Imagick::COLORSPACE_CMYK) { $im->setImageColorspace(\Imagick::COLORSPACE_SRGB); } $im->thumbnailImage(512, 0); $im->writeImage('upload/' . $cover_path); $im->clear(); $im->destroy(); move_uploaded_file($pdf['tmp_name'], 'upload/' . $pdf_path); $this->pushDeleteWhenSuccess('upload/' . $ecatalog->pdf_path); $this->pushDeleteWhenSuccess('upload/' . $ecatalog->cover_path); $this->pushDeleteWhenFailed('upload/' . $pdf_path); $this->pushDeleteWhenFailed('upload/' . $cover_path); $ecatalog->pdf_path = $pdf_path; $ecatalog->cover_path = $cover_path; } $success = R::store($ecatalog); if ($success) { $this->handlerSuccess(); } else { $this->handlerFailed(); } return $success; }
public function save() { if (!$this->emptyAttr('id')) { $news = R::findOne('news', 'id=?', [$this->getAttr('id')]); $news->updated_at = date('Y-m-d H:i:s'); } else { $news = R::dispense('news'); $news->created_at = date('Y-m-d H:i:s'); $news->updated_at = date('Y-m-d H:i:s'); $news->sort_order = (int) R::getCell("SELECT MAX(sort_order) FROM news") + 1; } $news->name = $this->getAttr('name'); $oldPicture = null; $thumb = null; if (!$this->emptyAttr('picture') && $this->attr['picture']->uploaded) { $picture = $this->getAttr('picture'); $picture->file_new_name_body = $this->generateName("news_picture_"); $picture->image_resize = true; $picture->image_convert = 'jpeg'; // $picture->image_x = 964; // $picture->image_y = 1024; $picture->image_y = 1024; $xDes = round($picture->image_src_x * $picture->image_y / $picture->image_src_y); if ($xDes > 964) { $picture->image_ratio = true; $picture->image_x = 964; $picture->image_ratio_crop = true; } else { $picture->image_ratio_x = true; } // $picture->image_ratio_y = true; $picture->process('upload/'); $oldPicture = $news->picture; $news->picture = $picture->file_dst_name; } $oldThumb = null; $thumb = null; if (!$this->emptyAttr('thumb') && $this->attr['thumb']->uploaded) { $thumb = $this->getAttr('thumb'); $thumb->file_new_name_body = $this->generateName("news_thumb_"); $thumb->image_resize = true; $thumb->image_convert = 'jpeg'; // $thumb->image_x = 460; // $thumb->image_y = 520; //$thumb->image_ratio_y = true; $thumb->image_y = 520; $xDes = round($thumb->image_src_x * $thumb->image_y / $thumb->image_src_y); if ($xDes > 460) { $thumb->image_ratio = true; $thumb->image_x = 460; $thumb->image_ratio_crop = true; } else { $thumb->image_ratio_x = true; } $thumb->process('upload/'); $oldThumb = $news->thumb; $news->thumb = $thumb->file_dst_name; } $news->content = $this->getAttr('content', ''); $news->link = $this->getAttr('link', ''); $success = R::store($news); if ($success) { if (!is_null($oldPicture)) { @unlink('upload/' . $oldPicture); } if (!is_null($oldThumb)) { @unlink('upload/' . $oldThumb); } } return $success; }
public function build(&$item) { $item['picture_url'] = $this->getBaseUrl() . '/upload/' . $item['picture']; $item['thumb_url'] = $this->getBaseUrl() . '/upload/' . $item['thumb']; $item['icon_1_url'] = $this->getBaseUrl() . '/icon/' . $item['icon_1']; $item['icon_2_url'] = $this->getBaseUrl() . '/icon/' . $item['icon_2']; $item['icon_3_url'] = $this->getBaseUrl() . '/icon/' . $item['icon_3']; $item['icon_4_url'] = $this->getBaseUrl() . '/icon/' . $item['icon_4']; if (!is_file('icon/' . $item['icon_1'])) { $item['icon_1_url'] = null; } if (!is_file('icon/' . $item['icon_2'])) { $item['icon_2_url'] = null; } if (!is_file('icon/' . $item['icon_3'])) { $item['icon_3_url'] = null; } if (!is_file('icon/' . $item['icon_4'])) { $item['icon_4_url'] = null; } $item['total_view'] = R::getCell('SELECT SUM(view_count) FROM product_view WHERE product_id=?', [$item['id']]) | '0'; $item['total_add'] = R::getCell('SELECT SUM(add_count) FROM product_add WHERE product_id=?', [$item['id']]) | '0'; }
public function count() { if (is_null($this->sql)) { return R::count($this->table); } $this->sql = 'SELECT COUNT(*) FROM ' . $this->table . ' ' . $this->alias . $this->sql; $result = R::getCell($this->sql, $this->params); $this->sql = null; return $result; }