public function addProductToProductLists() { $productUrl = $this->input->post('productUrl'); $productListId = $this->input->post('listId'); // Check All passed params if (empty($productUrl) || empty($productListId)) { $response = array('error' => true, 'message' => 'Bad Request'); $this->output->set_content_type('application/json')->set_output(json_encode($response)); return; } $productLists = $this->product_model->filterProductLists($productListId); // Show error is such lists does not exist if (empty($productLists)) { $response = array('error' => true, 'message' => 'List does not exist'); $this->output->set_content_type('application/json')->set_output(json_encode($response)); return; } $toBeRemoved = $this->product_model->getListsAndProductToBeRemoved($productUrl, $productLists); // Delete Products From Product Lists if (!empty($toBeRemoved)) { foreach ($toBeRemoved as $item) { if (($key = array_search($item->list_id, $productLists)) !== false) { unset($productLists[$key]); } $this->product_model->delete_('product_list_items', array('id' => $item->item_id)); } } // Add product to Product List if (!empty($productLists)) { $insertBatch = array(); foreach ($productLists as $productListId) { $insertBatch[] = array('product_list_id' => $productListId, 'url' => $productUrl); } $this->product_model->create_batch_('product_list_items', $insertBatch); } $this->output->set_content_type('application/json')->set_output(json_encode(array('error' => false))); }