Exemplo n.º 1
0
 public function index()
 {
     $perPage = 999999;
     $page = @$_GET['page'] ? $_GET['page'] : 1;
     $start = ($page - 1) * $perPage;
     $where = [];
     $where[] = 1;
     $queryParam = [];
     if (!empty($_GET['type'])) {
         $where[] = "type = ?";
         $queryParam[] = $_GET['type'];
     }
     if (!empty($_GET['porcelain_type'])) {
         $where[] = "porcelain_type = ?";
         $queryParam[] = $_GET['porcelain_type'];
     }
     if (!empty($_GET['size'])) {
         $where[] = "size = ?";
         $queryParam[] = $_GET['size'];
     }
     if (!empty($_GET['size_unit'])) {
         $where[] = "size_unit = ?";
         $queryParam[] = $_GET['size_unit'];
     }
     if (!empty($_GET['style'])) {
         $where[] = "style = ?";
         $queryParam[] = $_GET['style'];
     }
     if (!empty($_GET['company'])) {
         $where[] = "company = ?";
         $queryParam[] = $_GET['company'];
     }
     if (!empty($_GET['is_group'])) {
         $where[] = "company = ?";
         $queryParam[] = $_GET['company'];
     }
     if (!empty($_GET['pattern_id'])) {
         $pattern = R::findOne("room_pattern", "id=?", [$_GET['pattern_id']]);
         if (isset($pattern["product_use"])) {
             $ids = trim($pattern["product_use"], ",");
             $ids = explode(",", $ids);
         } else {
             $ids = [0];
         }
         $where[] = "id IN (" . R::genSlots($ids) . ")";
         $queryParam = array_merge($queryParam, $ids);
     }
     $where = implode(" AND ", $where);
     $queryParam[] = $start;
     $queryParam[] = $perPage;
     $items = R::getAll('SELECT product.* FROM product WHERE ' . $where . ' ORDER BY is_hot DESC, is_new DESC, created_at DESC LIMIT ?,?', $queryParam);
     $count = R::count('product', $where, array_slice($queryParam, 0, -2));
     $maxPage = floor($count / $perPage) + ($count % $perPage == 0 ? 0 : 1);
     // $itemsExport = R::exportAll($items);
     $this->builds($items);
     header('Content-Type: application/json');
     echo json_encode(['items' => $items, 'page' => $page, 'maxPage' => $maxPage, 'total' => $count], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
     exit;
 }
Exemplo n.º 2
0
 /**
  * Insert or Update a bean
  *
  * @param OODBBean $bean
  * @param $data array
  * @return Result
  */
 private function saveBean($bean, $data)
 {
     // Handle File Field that may not in the $data, because Filename always go into $_FILES.
     foreach ($_FILES as $fieldName => $file) {
         $data[$fieldName] = $file["name"];
     }
     // Store Showing fields only
     $fields = $this->getShowFields();
     foreach ($fields as $field) {
         // Check is unique
         if ($field->isUnique()) {
             // Try to find duplicate beans
             $fieldName = $field->getName();
             $duplicateBeans = R::find($bean->getMeta('type'), " {$fieldName} = ? ", [$data[$field->getName()]]);
             if (count($duplicateBeans) > 0) {
                 $validateResult = "Email 已存在!";
             }
         }
         if ($field->getFieldRelation() == Field::MANY_TO_MANY) {
             // 1. Many to many
             // http://www.redbeanphp.com/many_to_many
             $keyName = "shared" . ucfirst($field->getName()) . "List";
             // Clear the current list (tableB_tableA)
             try {
                 $tableName = $this->getTableName() . "_" . $field->getName();
                 $idName = $this->getTableName() . "_id";
                 R::exec("DELETE FROM {$tableName} WHERE {$idName} = ?", [$bean->id]);
             } catch (\Exception $ex) {
             }
             // Clear the current list (tableA_tableB)
             try {
                 $tableName = $field->getName() . "_" . $this->getTableName();
                 $idName = $this->getTableName() . "_id";
                 R::exec("DELETE FROM {$tableName} WHERE {$idName} = ?", [$bean->id]);
             } catch (\Exception $ex) {
             }
             // If User have checked a value in checkbox
             if (isset($data[$field->getName()])) {
                 $valueList = $data[$field->getName()];
                 $slots = R::genSlots($valueList);
                 $relatedBeans = R::find($field->getName(), " id IN ({$slots})", $valueList);
                 foreach ($relatedBeans as $relatedBean) {
                     $bean->{$keyName}[] = $relatedBean;
                 }
             }
         } else {
             if ($field->getFieldRelation() == Field::ONE_TO_MANY) {
                 // TODO One to many
             } else {
                 if (!$field->isStorable()) {
                     // 2. If not storable, skip
                     continue;
                 } elseif ($field->getFieldRelation() == Field::NORMAL) {
                     // 3.Normal data field
                     $value = $field->getStoreValue($data);
                     if ($value == LouisCRUD::NULL) {
                         $value = null;
                     }
                     // Validate the value
                     if ($field->isStorable()) {
                         $validateResult = $field->validate($value, $data);
                     } else {
                         // TODO: check non-storable?
                         $validateResult = true;
                     }
                     // If validate failed, return result object.
                     if ($validateResult !== true) {
                         $result = new Result();
                         $result->id = @$bean->id;
                         $result->msg = $validateResult;
                         $result->fieldName = $field->getName();
                         $result->class = "callout-danger";
                         return $result;
                     }
                     // Set the value to the current bean directly
                     $bean->{$field->getName()} = $value;
                 }
             }
         }
     }
     // Store
     // TODO: Return result object
     $id = R::store($bean);
     $result = new Result();
     $result->id = $id;
     return $result;
 }
Exemplo n.º 3
0
 public function stat()
 {
     $perPage = 10;
     $page = @$_GET['page'] ? $_GET['page'] : 1;
     $start = ($page - 1) * $perPage;
     $where = [];
     $where[] = 1;
     $where = implode(" AND ", $where);
     $queryParam[] = $start;
     $queryParam[] = $perPage;
     $form = [];
     $form['room'] = '';
     $sub = 'SELECT SUM(product_room.view_count) FROM product_room WHERE product_room.product_id = product.id';
     $items = [];
     $count = 0;
     $maxPage = 0;
     if (!empty($_GET["room"])) {
         $form["room"] = $_GET["room"];
         $ids = R::getCol('SELECT product_id FROM product_room WHERE room_name = :room_name', [':room_name' => $_GET["room"]]);
         if (!empty($ids)) {
             $ids = array_unique($ids);
             $room = $_GET["room"];
             $sub .= " AND product_room.room_name = '{$room}' ";
             $where = ' id IN (' . R::genSlots($ids) . ')';
             $queryParam = array_merge($ids, $queryParam);
             $sql = 'SELECT *,(' . $sub . ') as total_room FROM product WHERE ' . $where . ' LIMIT ?,?';
             $items = R::getAll($sql, $queryParam);
             $count = R::count('product', $where, array_slice($queryParam, 0, -2));
             $maxPage = floor($count / $perPage) + ($count % $perPage == 0 ? 0 : 1);
         } else {
         }
     }
     $items = array_map(function ($item) {
         if (is_null($item["total_room"])) {
             $item["total_room"] = 0;
         }
         return $item;
     }, $items);
     $this->slim->render("room/stat_view.php", ['items' => $items, 'page' => $page, 'maxPage' => $maxPage, 'form' => $form]);
 }