public function addAssociation() { $listId = $this->input->post('list_id'); $hash = $this->input->post('hash'); $url = $this->input->post('url'); $productName = $this->input->post('product_name'); $urlId = $this->input->post('urlId'); $this->load->model('product_model'); if (empty($listId) && empty($hash) && empty($url) && empty($productName)) { $this->jsonResponse(array('error' => true, 'message' => 'Bad Request.')); } if (filter_var($url, FILTER_VALIDATE_URL) === false) { $this->jsonResponse(array('error' => true, 'message' => 'Invalid Url')); } // Update Url if (!empty($urlId)) { $this->product_model->update_('product_url', array('url' => $url), array('id' => $urlId)); return $this->jsonResponse(array('error' => false)); } // Get Url id or Add new url $urlId = $this->product_model->getUniqueUrlIdOrCreateNewUrl($url); if (empty($urlId)) { return $this->jsonResponse(array('error' => true, 'message' => 'Internal server error.')); } // Remove All connections for UrlId $this->product_model->removeAssociations($urlId, $listId, $hash); $associationUrlIds = $this->product_model->getAssociationGroupUrlIds($listId, $hash); if (!empty($associationUrlIds)) { $insertBatch = array(); foreach ($associationUrlIds as $associationUrlId) { $insertBatch[] = array('group_hash' => $hash, 'unified_product_name' => $productName, 'first_product_url_id' => $urlId, 'second_product_url_id' => $associationUrlId, 'product_association_id' => $listId); } if (!empty($insertBatch)) { $this->product_model->create_batch_($this->product_model->getProductProductAssociationTable(), $insertBatch); } } else { $this->product_model->create_($this->product_model->getProductProductAssociationTable(), array('group_hash' => $hash, 'unified_product_name' => $productName, 'first_product_url_id' => $urlId, 'product_association_id' => $listId)); } return $this->jsonResponse(array('error' => false)); }