public function delete($id = 0) { //get data product. $dgClass = new dg(); $products = $dgClass->getProducts(); $categories = $dgClass->getProductCategories(); //get id products if (isset($_POST['ids']) && $_POST['ids'] != '') { $ids = $_POST['ids']; } else { if ($id != '' && (int) $id > 0) { $ids = array($id); } else { $ids = array(); } } if (count($ids) > 0) { //remove products. if (count($products) > 0) { $content['products'] = array(); foreach ($products as $product) { if (!in_array($product->id, $ids)) { $content['products'][] = $product; } } } $content = json_encode($content); $path = dirname(ROOT) . DS . 'data' . DS . 'products.json'; $dgClass->WriteFile($path, $content); //remove categories. if (count($categories) > 0) { $category_data = array(); foreach ($categories as $category) { if (!in_array($category->product_id, $ids)) { $category_data[] = $category; } } $category_data = json_encode($category_data); $path = dirname(ROOT) . DS . 'data' . DS . 'product_categories.json'; $dgClass->WriteFile($path, $category_data); } } $dgClass->redirect('index.php/product'); }
public function save() { $data = $_POST['product']; $attributes = array(); if (isset($data['fields']) && count($data['fields']) > 0) { $attributes['name'] = array(); $attributes['prices'] = array(); $attributes['titles'] = array(); $attributes['type'] = array(); $i = 0; foreach ($data['fields'] as $filed) { $attributes['name'][$i] = $filed['name']; $attributes['prices'][$i] = $filed['prices']; $attributes['titles'][$i] = $filed['titles']; $attributes['type'][$i] = $filed['type']; $i++; } } unset($data['fields']); $data['attributes'] = $attributes; $dgClass = new dg(); $content = array('products' => array()); // get id if ($data['id'] == 0) { $id = 1; $products = $dgClass->getProducts(); if (count($products) > 0) { foreach ($products as $product) { if ($product->id > $id) { $id = $product->id; } } $id = $id + 1; } $data['id'] = $id; $products[] = $data; $content['products'] = $products; } else { $products = $dgClass->getProducts(); $is_new = true; if (count($products) > 0) { foreach ($products as $product) { if ($product->id == $data['id']) { $content['products'][] = $data; $is_new = false; } else { $content['products'][] = $product; } } } if ($is_new === true) { $products[] = $data; $content['products'] = $products; } } $content = str_replace('\\\\', '', json_encode($content)); // write file $path = dirname(ROOT) . DS . 'data' . DS . 'products.json'; $check = $dgClass->WriteFile($path, $content); // update categories. $categories = $_POST['category']; if (count($categories)) { $category = $dgClass->getProductCategories(); $data_cate = array(); $cid = 0; foreach ($category as $val) { if ($val->product_id != $data['id']) { $data_cate[] = $val; if ($val->id > $cid) { $cid = $val->id; } } } foreach ($categories as $val) { $cid++; $catedt = new stdClass(); $catedt->id = $cid; $catedt->product_id = $data['id']; $catedt->cate_id = $val; $data_cate[] = $catedt; } // write file $path = dirname(ROOT) . DS . 'data' . DS . 'product_categories.json'; $check = $dgClass->WriteFile($path, json_encode($data_cate)); } if ($check === false) { $dgClass->redirect('index.php/product/edit/' . $data['id'] . '/0'); } else { $dgClass->redirect('index.php/product/edit/' . $data['id'] . '/1'); } }