$quick_updates_count['master_categories_id'][$products_id] = $products_id; $db->Execute("UPDATE " . TABLE_PRODUCTS . " SET master_categories_id='" . (int) $new_value . "', products_last_modified=now() WHERE products_id=" . (int) $products_id); zen_update_products_price_sorter((int) $products_id); // needed? } $quick_updates_count['categories_id'][$products_id] = $products_id; //$db->Execute("INSERT INTO " . TABLE_PRODUCTS_TO_CATEGORIES . " SET categories_id='" . (int)$new_value . "', products_id=" . (int)$products_id . " WHERE products_id=" . (int)$products_id) . " AND categories_id=" . (int)$_POST['quick_updates_old']['categories_id'][$products_id]); // changed INSERT INTO to UPDATE to prevent conflicts $db->Execute("UPDATE " . TABLE_PRODUCTS_TO_CATEGORIES . " SET categories_id='" . (int) $new_value . "', products_id=" . (int) $products_id . " WHERE products_id=" . (int) $products_id . " AND categories_id=" . (int) $_POST['quick_updates_old']['categories_id'][$products_id]); } } } if ($_POST['quick_updates_new']['master_categories_id']) { foreach ($_POST['quick_updates_new']['master_categories_id'] as $products_id => $new_value) { if ($_POST['quick_updates_new']['master_categories_id'][$products_id] != $_POST['quick_updates_old']['master_categories_id'][$products_id]) { if (zen_childs_in_category_count($new_value)) { $messageStack->add(TEXT_CATEGORY_WITH_CHILDS . ' ' . zen_get_category_name($new_value, (int) $_SESSION["languages_id"]) . ' [' . $new_value . ']', 'error'); continue; } // add invalid warning here?? (if the new master_cat is not linked) $quick_updates_count['master_categories_id'][$products_id] = $products_id; $db->Execute("UPDATE " . TABLE_PRODUCTS . " SET master_categories_id='" . (int) $new_value . "', products_last_modified=now() WHERE products_id=" . (int) $products_id); zen_update_products_price_sorter((int) $products_id); // needed? } } } // added for products_purchase_price and margin if ($_POST['quick_updates_new']['products_purchase_price']) { foreach ($_POST['quick_updates_new']['products_purchase_price'] as $products_id => $new_value) { if ($_POST['quick_updates_new']['products_purchase_price'][$products_id] != $_POST['quick_updates_old']['products_purchase_price'][$products_id]) {
if ($copy_from_linked == $copy_to_linked) { $messageStack->add_session(WARNING_DUPLICATE_PRODUCTS_TO_CATEGORY_LINKED, 'warning'); zen_redirect(zen_href_link(FILENAME_PRODUCTS_TO_CATEGORIES, 'products_filter=' . $products_filter . '¤t_category_id=' . $current_category_id)); } $check_category_from = $db->Execute("select products_id from " . TABLE_PRODUCTS_TO_CATEGORIES . " where categories_id='" . $copy_from_linked . "' limit 1"); $check_category_to = $db->Execute("select products_id from " . TABLE_PRODUCTS_TO_CATEGORIES . " where categories_id='" . $copy_to_linked . "' limit 1"); // check if from is valid category if ($check_category_from->RecordCount() < 1) { $zv_invalid_copy_linked = 'true'; $zv_complete_message_linked .= WARNING_COPY_ALL_PRODUCTS_TO_CATEGORY_FROM_LINKED . $copy_from_linked . ' '; } else { $zv_complete_message_linked .= SUCCESS_COPY_ALL_PRODUCTS_TO_CATEGORY_FROM_LINKED . $copy_from_linked . ' '; } // check if to is valid category if ($check_category_to->RecordCount() < 1) { if (zen_childs_in_category_count($copy_to_linked) > 0) { $zv_invalid_copy_linked = 'true'; $zv_complete_message_linked .= WARNING_COPY_ALL_PRODUCTS_TO_CATEGORY_TO_LINKED . $copy_to_linked . ' '; } } else { $zv_complete_message_linked .= SUCCESS_COPY_ALL_PRODUCTS_TO_CATEGORY_TO_LINKED . $copy_to_linked . ' '; } if ($zv_invalid_copy_linked == 'true') { $messageStack->add_session($zv_complete_message_linked, 'warning'); zen_redirect(zen_href_link(FILENAME_PRODUCTS_TO_CATEGORIES, 'products_filter=' . $products_filter . '¤t_category_id=' . $current_category_id)); } /////////////////////////////////////////////////////////////// // if either category was invalid nothing processes below /////////////////////////////////////////////////////////////// // get products to be linked from $products_to_categories_from_linked = $db->Execute("select products_id from " . TABLE_PRODUCTS_TO_CATEGORIES . " where categories_id='" . $copy_from_linked . "'");
function zen_childs_in_category_count($categories_id) { global $gBitDb; $categories_count = 0; $categories = $gBitDb->Execute("SELECT `categories_id`\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tFROM " . TABLE_CATEGORIES . "\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tWHERE `parent_id` = '" . (int) $categories_id . "'"); while (!$categories->EOF) { $categories_count++; $categories_count += zen_childs_in_category_count($categories->fields['categories_id']); $categories->MoveNext(); } return $categories_count; }
function zen_childs_in_category_count($categories_id) { global $db; $categories_count = 0; $categories = $db->Execute("select categories_id\n from " . TABLE_CATEGORIES . "\n where parent_id = '" . (int) $categories_id . "'"); while (!$categories->EOF) { $categories_count++; $categories_count += zen_childs_in_category_count($categories->fields['categories_id']); $categories->MoveNext(); } return $categories_count; }
function importCategory($data, $format) { // prepare data $validate = true; $ignore_column = $this->getFormatColumnIgnore($format['csv_format_type_id']); $ignore_id = $ignore_column['csv_column_id']; $delete_column = $this->getFormatColumnDelete($format['csv_format_type_id']); $delete_id = $delete_column['csv_column_id']; $category_name_flag = 0; $extra_depth = 0; $this->messageStack->reset(); // validate foreach ($format['columns'] as $key => $val) { if (!empty($val['csv_column_validate_function'])) { $validate_function = $val['csv_column_validate_function']; if ($this->{$validate_function}($data[$key], $val['csv_column_name']) === true) { $validate = $validate && true; } else { $validate = $validate && false; } } if ($val['csv_column_id'] == $delete_id) { if ($data[$key] == 1) { $delete_flag = true; } else { $delete_flag = false; } } // store category name if ($val['number'] > 0 && $val['language_id'] > 0) { $category_name[$val['number']][$val['language_id']] = $data[$key]; if ($val['language_id'] == MODULE_PRODUCT_CSV_PREFERRED_LANGUAGE_ID && $data[$key] != '') { $category_name_flag += pow(2, $val['number'] - 1); } elseif ($data[$key] != '') { $extra_depth = $extra_depth > $val['number'] ? $extra_depth : $val['number']; } } } // check if category is sequential switch ($category_name_flag) { case 1: $depth = 1; break; case 3: $depth = 2; break; case 7: $depth = 3; break; case 15: $depth = 4; break; case 31: $depth = 5; break; case 63: $depth = 6; break; case 127: $depth = 7; break; case 255: $depth = 8; break; case 511: $depth = 9; break; case 1023: $depth = 10; break; default: // check top level category if (($category_name_flag & 1) != 1) { $validate = $validate && false; $this->messageStack->add(PRODUCT_CSV_MESSAGE_NO_TOPLEVEL_CATEGORY, 'caution'); } else { $validate = $validate && false; $this->messageStack->add(PRODUCT_CSV_MESSAGE_NOT_SEQUENTIAL, 'caution'); } break; } if (is_numeric($depth) && $extra_depth > $depth) { $validate = $vaidate && false; $this->messageStack->add(PRODUCT_CSV_MESSAGE_NO_CATEGORY_NAME, 'caution'); } // return if validate is false if ($validate === false) { return false; } // check and insert category name $parent_id = 0; for ($i = 1; $i <= $depth; $i++) { $sql = 'SELECT c.categories_id FROM ' . TABLE_CATEGORIES . ' c LEFT JOIN ' . TABLE_CATEGORIES_DESCRIPTION . ' cd ON c.categories_id=cd.categories_id WHERE language_id=' . MODULE_PRODUCT_CSV_PREFERRED_LANGUAGE_ID . ' AND categories_name=\'' . zen_db_input($category_name[$i][MODULE_PRODUCT_CSV_PREFERRED_LANGUAGE_ID]) . '\' AND parent_id=' . $parent_id . ''; $category_record = $this->db->Execute($sql); if ($category_record->RecordCount() >= 1) { $category_id = $category_record->fields['categories_id']; foreach ($category_name[$i] as $language_id => $name) { if ($language_id == MODULE_PRODUCT_CSV_PREFERRED_LANGUAGE_ID) { continue; } $sql = 'SELECT categories_id FROM ' . TABLE_CATEGORIES_DESCRIPTION . ' WHERE language_id=' . $language_id . ' AND categories_name=\'' . zen_db_input($name) . '\' AND categories_id=' . $category_id . ''; $category_record = $this->db->Execute($sql); if ($category_record->RecordCount() == 0) { $this->messageStack->add(sprintf(PRODUCT_CSV_MESSAGE_NOT_MATCH, $name), 'caution'); return false; } } } else { // check child products $sql = 'SELECT categories_id FROM ' . TABLE_PRODUCTS_TO_CATEGORIES . ' WHERE categories_id=' . $parent_id . ''; $category_record = $this->db->Execute($sql); if ($category_record->RecordCount() == 0) { // insert new category $sql = 'INSERT INTO ' . TABLE_CATEGORIES . ' (date_added, last_modified, parent_id) VALUES (NOW(), NOW(), \'' . $parent_id . '\')'; $this->db->Execute($sql); $category_id = $this->db->Insert_ID(); foreach ($category_name[$i] as $language_id => $name) { $sql = 'INSERT INTO ' . TABLE_CATEGORIES_DESCRIPTION . ' (categories_id, language_id, categories_name) VALUES (\'' . $category_id . '\', \'' . $language_id . '\', \'' . zen_db_input($name) . '\')'; $this->db->Execute($sql); $this->messageStack->add(PRODUCT_CSV_MESSAGE_CREATE_CATEGORY, 'success'); } } else { $this->messageStack->add(PRODUCT_CSV_MESSAGE_CANNOT_ADD_CATEGORY, 'caution'); return false; } } $parent_id = $category_id; } foreach ($format['columns'] as $key => $val) { if ($val['csv_column_id'] == $ignore_id) { continue; } if ($val['csv_columns_dbtable'] == 'categories') { if ($val['csv_columns_dbcolumn'] == 'categories_status' && $data[$key] == '') { continue; } $sql = 'UPDATE ' . DB_PREFIX . $val['csv_columns_dbtable'] . ' SET ' . $val['csv_columns_dbcolumn'] . '=\'' . zen_db_input($data[$key]) . '\' WHERE categories_id=' . $category_id . ''; $this->db->Execute($sql); } elseif ($val['csv_columns_dbtable'] == 'categories_description' && $val['csv_columns_dbcolumn'] == 'categories_description') { if (isset($val['language_id'])) { $sql = 'UPDATE ' . DB_PREFIX . $val['csv_columns_dbtable'] . ' SET ' . $val['csv_columns_dbcolumn'] . '=\'' . zen_db_input($data[$key]) . '\' WHERE categories_id=' . $category_id . ' AND language_id=' . $language_id . ''; $this->db->Execute($sql); } } elseif ($val['csv_columns_dbtable'] == 'products' && $data[$key] != '') { if (zen_childs_in_category_count($category_id) > 0) { // category has sub category $this->messageStack->add(PRODUCT_CSV_MESSAGE_CANNOT_ADD_PRODUCT, 'caution'); return false; } else { // category doesnt have sub category $sql = 'SELECT products_id, master_categories_id FROM ' . TABLE_PRODUCTS . ' WHERE products_model=\'' . zen_db_input($data[$key]) . '\''; $product_record = $this->db->Execute($sql); // check products_model if ($product_record->RecordCount() == 1) { $products_id = $product_record->fields['products_id']; $master_categories_id = $product_record->fields['master_categories_id']; if ($delete_flag !== true) { if ($master_categories_id == 0) { // update master_categories_id $sql = 'UPDATE ' . TABLE_PRODUCTS . ' SET master_categories_id=' . $category_id . ' WHERE products_id=' . $products_id . ''; $this->db->Execute($sql); } $sql = 'SELECT products_id, categories_id FROM ' . TABLE_PRODUCTS_TO_CATEGORIES . ' WHERE products_id=' . $products_id . ' AND categories_id=' . $category_id . ''; $record = $this->db->Execute($sql); if ($record->RecordCount() == 0) { $sql = 'INSERT INTO ' . TABLE_PRODUCTS_TO_CATEGORIES . ' (products_id, categories_id) VALUES (\'' . $products_id . '\', \'' . $category_id . '\')'; $this->db->Execute($sql); } } else { // delete $sql = 'DELETE FROM ' . TABLE_PRODUCTS_TO_CATEGORIES . ' WHERE products_id=' . $products_id . ' AND categories_id=' . $category_id . ''; $this->db->Execute($sql); if ($master_categories_id == $category_id) { $sql = 'SELECT categories_id FROM ' . TABLE_PRODUCTS_TO_CATEGORIES . ' WHERE products_id=' . $products_id . ''; $record = $this->db->Execute($sql); if ($record->RecordCount() == 0) { $categories_id = 0; } else { $categories_id = $record->fields['categories_id']; } $sql = 'UPDATE ' . TABLE_PRODUCTS . ' SET master_categories_id=\'' . $categories_id . '\' WHERE products_id=' . $products_id . ''; $this->db->Execute($sql); } $this->messageStack->add(PRODUCT_CSV_MESSAGE_DELETE_CATEGORY, 'success'); } } else { $this->messageStack->add(PRODUCT_CSV_MESSAGE_NO_MODEL, 'caution'); return false; } } } elseif ($val['csv_columns_dbtable'] == 'meta_tags_categories_description') { if (isset($val['language_id'])) { $meta_tags[$val['language_id']][$val['csv_columns_db_column']] = $data[$key]; } } } if (isset($meta_tags)) { foreach ($meta_tags as $key => $val) { if (!empty($val['metatags_title']) || !empty($val['metatags_keywords']) || !empty($val['metatags_description'])) { $sql = 'SELECT * FROM ' . TABLE_METATAGS_CATEGORIES_DESCRIPTION . ' WHERE categories_id=' . $category_id . ' AND language_id=' . $key . ''; $meta_tags_record = $this->db->Execute($sql); if ($meta_tags_record->RecordCount() == 0) { $sql = 'INSERT INT ' . TABLE_METATAGS_CATEGORIES_DESCRIPTION . ' (categories_id, language_id) VALUES (\'' . $category_id . '\',\'' . $key . '\')'; $this->db->Execute($sql); } $sql = 'UPDATE ' . TABLE_METATAGS_CATEGORIES_DESCRIPTION . ' SET metatags_title=\'' . zen_db_input($val['metatags_title']) . '\', metatags_keywords=\'' . zen_db_input($val['metatags_keywords']) . '\', metatags_description=\'' . zen_db_input($val['metatags_description']) . '\' WHERE categories_id=' . $category_id . ' AND language_id=' . $key . ''; $this->db->Execute($sql); } else { // delete meta tags $sql = 'DELETE FROM ' . TABLE_METATAGS_CATEGORIES_DESCRIPTION . ' WHERE categories_id=' . $category_id . ' AND language_id=' . $key . ''; $this->db->Execute($sql); } } } $this->messageStack->add(PRODUCT_CSV_MESSAGE_SUCCESS, 'success'); return true; }