/** * @param int $product_id * @param array $data */ private function _clone_product_options($product_id, $data) { //Do not use before close review. //Note: This is done only after product cloning. This is not to be used on existing product. $this->db->query("DELETE FROM " . $this->db->table("product_options") . " WHERE product_id = '" . (int) $product_id . "'"); $this->db->query("DELETE FROM " . $this->db->table("product_option_descriptions") . " WHERE product_id = '" . (int) $product_id . "'"); $this->db->query("DELETE FROM " . $this->db->table("product_option_values") . " WHERE product_id = '" . (int) $product_id . "'"); $this->db->query("DELETE FROM " . $this->db->table("product_option_value_descriptions") . " WHERE product_id = '" . (int) $product_id . "'"); if (isset($data['product_option'])) { foreach ($data['product_option'] as $product_option) { $sql = "INSERT INTO " . $this->db->table("product_options") . " \n\t\t\t\t\t\tSET product_id = '" . (int) $product_id . "',\n\t\t\t\t\t\t\tsort_order = '" . (int) $product_option['sort_order'] . "'"; if ($product_option['attribute_id']) { $sql .= ", attribute_id = '" . (int) $product_option['attribute_id'] . "'"; } if ($product_option['group_id']) { $sql .= ", group_id = '" . (int) $product_option['group_id'] . "'"; } if ($product_option['element_type']) { $sql .= ", element_type = '" . $this->db->escape($product_option['element_type']) . "'"; } if ($product_option['required']) { $sql .= ", required = '" . (int) $product_option['required'] . "'"; } if ($product_option['regexp_pattern']) { $sql .= ", regexp_pattern = '" . $this->db->escape($product_option['regexp_pattern']) . "'"; } $this->db->query($sql); $product_option_id = $this->db->getLastId(); foreach ($product_option['language'] as $language_id => $language) { $this->language->replaceDescriptions('product_option_descriptions', array('product_option_id' => (int) $product_option_id, 'product_id' => (int) $product_id), array($language_id => array('name' => $language['name'], 'error_text' => $language['error_text']))); } if (isset($product_option['product_option_value'])) { //get product resources $rm = new AResourceManager(); foreach ($product_option['product_option_value'] as $pd_opt_vals) { $pd_opt_vals['price'] = str_replace(" ", "", $pd_opt_vals['price']); $this->db->query("INSERT INTO " . $this->db->table("product_option_values") . " \n\t\t\t\t\t\t\t\t\t\t\tSET product_option_id = '" . (int) $product_option_id . "',\n\t\t\t\t\t\t\t\t\t\t\t\tproduct_id = '" . (int) $product_id . "',\n\t\t\t\t\t\t\t\t\t\t\t\tsku = '" . $this->db->escape($pd_opt_vals['sku']) . "',\n\t\t\t\t\t\t\t\t\t\t\t\tquantity = '" . (int) $pd_opt_vals['quantity'] . "',\n\t\t\t\t\t\t\t\t\t\t\t\tsubtract = '" . (int) $pd_opt_vals['subtract'] . "',\n\t\t\t\t\t\t\t\t\t\t\t\tprice = '" . preformatFloat($pd_opt_vals['price'], $this->language->get('decimal_point')) . "',\n\t\t\t\t\t\t\t\t\t\t\t\tprefix = '" . $this->db->escape($pd_opt_vals['prefix']) . "',\n\t\t\t\t\t\t\t\t\t\t\t\tattribute_value_id = '" . $this->db->escape($pd_opt_vals['attribute_value_id']) . "',\n\t\t\t\t\t\t\t\t\t\t\t\tgrouped_attribute_data = '" . $this->db->escape($pd_opt_vals['grouped_attribute_data']) . "',\n\t\t\t\t\t\t\t\t\t\t\t\tgroup_id = '" . $this->db->escape($pd_opt_vals['group_id']) . "',\n\t\t\t\t\t\t\t\t\t\t\t\tsort_order = '" . (int) $pd_opt_vals['sort_order'] . "',\n\t\t\t\t\t\t\t\t\t\t\t\t`default` = '" . (int) $pd_opt_vals['default'] . "'"); $pd_opt_val_id = $this->db->getLastId(); // clone resources of option value if ($pd_opt_vals['product_option_value_id']) { $resources = $rm->getResourcesList(array('object_name' => 'product_option_value', 'object_id' => $pd_opt_vals['product_option_value_id'])); foreach ($resources as $r) { $rm->mapResource('product_option_value', $pd_opt_val_id, $r['resource_id']); } } foreach ($pd_opt_vals['language'] as $language_id => $lang_data) { $grouped_attribute_names = serialize($lang_data['children_options_names']); $this->language->replaceDescriptions('product_option_value_descriptions', array('product_option_value_id' => (int) $pd_opt_val_id, 'product_id' => (int) $product_id), array($language_id => array('name' => $lang_data['name'], 'grouped_attribute_names' => $grouped_attribute_names))); } } } } } $this->cache->remove('product'); }
private function _migrateImages($data = array(), $object_txt_id = '', $object_id = 0) { $objects = array('products' => 'Product', 'categories' => 'Category', 'manufacturers' => 'Brand'); if (!in_array($object_txt_id, array_keys($objects)) || !$data || !is_array($data)) { $this->addLog('Error: data array for object "' . $object_txt_id . '" wrong.'); return false; } $language_list = $this->language->getAvailableLanguages(); $rm = new AResourceManager(); $rm->setType('image'); //IMAGE PROCESSING $success_db = false; // sign of imported image from database query result(not generated additional) $data['image'] = (array) $data['image']; foreach ($data['image'] as $k => $source) { $src_exists = @getimagesize($source); // check is image exists if ($src_exists) { $image_basename = basename($source); $target = DIR_RESOURCE . 'image/' . $image_basename; if (($file = $this->downloadFile($source)) === false) { $this->is_error = true; if ($k == 'db') { $this->addLog($objects[$object_txt_id] . " " . $data['name'] . " File " . $source . " couldn't be uploaded."); } continue; } if (!$this->is_error) { if (!is_dir(DIR_RESOURCE . 'image/')) { mkdir(DIR_RESOURCE . 'image/', 0777); } if (!$this->writeToFile($file, $target)) { $this->is_error = true; if ($k == 'db') { $this->addLog("Cannot create " . $objects[$object_txt_id] . " " . $data['name'] . " ( " . $source . " ) file " . $target . " in resource/image folder "); } continue; } $resource = array('language_id' => $this->config->get('storefront_language_id'), 'name' => array(), 'title' => '', 'description' => '', 'resource_path' => $image_basename, 'resource_code' => ''); foreach ($language_list as $lang) { $resource['name'][$lang['language_id']] = str_replace('%20', ' ', $image_basename); } $resource_id = $rm->addResource($resource); if ($resource_id) { $rm->mapResource($object_txt_id, $object_id, $resource_id); $this->pic_count++; $success_db = $k == 'db' ? true : $success_db; } else { $this->addLog($this->db->error); continue; } } } } // end of processing images return true; }
public function map() { if (!$this->user->canModify('common/resource_library')) { $error = new AError(''); return $error->toJSONResponse('NO_PERMISSIONS_402', array('error_text' => sprintf($this->language->get('error_permission_modify'), 'common/resource_library'), 'reset_value' => true)); } //init controller data $this->extensions->hk_InitData($this, __FUNCTION__); $rm = new AResourceManager(); if (!empty($this->request->get['resource_id'])) { $this->request->post['resources'] = array($this->request->get['resource_id']); } foreach ($this->request->post['resources'] as $resource_id) { $rm->mapResource($this->request->get['object_name'], $this->request->get['object_id'], $resource_id); } //update controller data $this->extensions->hk_UpdateData($this, __FUNCTION__); $this->load->library('json'); $this->response->addJSONHeader(); $this->response->setOutput(AJson::encode(true)); }
/** * @param AResourceManager $rm * @param $object_txt_id * @param $object_id * @param string $image_basename * @param string $code * @return null */ private function _create_resource($rm, $object_txt_id, $object_id, $image_basename = '', $code = '') { $language_list = $this->language->getAvailableLanguages(); $resource = array('language_id' => $this->config->get('storefront_language_id'), 'name' => array(), 'title' => '', 'description' => '', 'resource_path' => $image_basename, 'resource_code' => $code); foreach ($language_list as $lang) { $resource['name'][$lang['language_id']] = str_replace('%20', ' ', $image_basename); } $resource_id = $rm->addResource($resource); if ($resource_id) { $rm->mapResource($object_txt_id, $object_id, $resource_id); return $resource_id; } else { return null; } }