home *** CD-ROM | disk | FTP | other *** search
/ Practical Internet Web Designer 89 / PIWD89.iso / pc / CONTENTS / ECOMMERCE / TUTORIAL_FILES / EASY_POPULATE_262_MS2_UNZIPPED / EASYPOPULATEPHP.BAK < prev    next >
Text File  |  2003-09-08  |  70KB  |  1,886 lines

  1. <?php
  2.  
  3. // Current EP Version
  4. $curver = '2.62-MS2';
  5.  
  6. /*
  7.   $Id: easypopulate.php,v 1.18 2002/12/05 14:34:18 elarifr Exp $
  8.  
  9.  
  10. //*******************************
  11. //*******************************
  12. // E D I T   H I S T O R Y
  13. //*******************************
  14. //*******************************
  15. Easy Populate
  16.  
  17. The point of easy populate is to let someone use an excel file exported to
  18. a tab delimited file to set up their entire store:
  19. categories, products, manufacturers, quantities, and prices.
  20.  
  21. -----------------------------------
  22. Modified by Tim Wasson - Wasson65 (wasson65@nc.rr.com) to:
  23.  accept category/subcategory names
  24.  allow reordering of columns in csv file
  25.  accept manufacturer name or id
  26.  some minor code simplification
  27.  accept and set status of the product if desired
  28.  changed all # to // comment markers so KDE's Kate will syntax highlight correctly
  29.  added support for default images for products, categories, and categories.
  30.  added support for exporting a csv file that can be modified and sent back in.
  31. -----------------------------------
  32. 1.1 Changes
  33.  Fixed a stupid bug, I didn't change the references to easypopulate.php from excel.php
  34.  Added note in the docs that if the Excel import is already done, don't need to do the alter table
  35.  Removed the extra semicolon on the end of the line in the csv download.  It prevented you from exporting and importing a file.
  36. -----------------------------------
  37. 1.2 Fixes
  38. More bugs fixed
  39. ___________________________________
  40. 1.3 Fixes
  41. Added another link to put csv file in temp file for access via tools->files, for some windows machines that refuse to dl right...
  42. -----------------------------------
  43. 1.4 Fixes
  44. Switchted to tabs for delimiters
  45. Strip cr's and tab's from strings before exporting
  46. Added explicit end of row field
  47. Added ability to split a big file into smaller files in the temp dir
  48. Preserve double quotes, single quotes, and apostrophes and commas
  49. Removed references to category_root, it's no longer required
  50. ------------------------------------
  51. 1.5 Fixes
  52. Changed --EOR-- to EOREOR for better excel usability.
  53. Made script accept "EOREOR" or EOREOR without the quotes.
  54. If inserting a new product, delete any product_descriptions with that product_id to avoid an error if old data was still present.
  55. ------------------------------------
  56. 1.6 Fixes
  57. Ooops, manufacturer_id variable misspellings meant that mfg wasn't getting set or updated
  58. Whe I re-arranged the code, I left out the call to actually put the data into the products table.  Ooops again...
  59. ------------------------------------
  60. 1.61 Fixes
  61. One more manufacturer id name fix.
  62. ------------------------------------
  63. Skipped to 2.0 because of the big jump in functionality
  64. ------------------------------------
  65. 2.0
  66. Made EP handle magic-quotes
  67. Thanks to Joshua Dechant aka dreamscape, for this fix
  68. Rewrote the categories part to handle any number of categories
  69. ------------------------------------
  70. 2.1
  71. Fix split files not splitting.
  72. Change from "file" to "fgets" to read the file to be split to avoid out of memory problems... hopefully
  73. ------------------------------------
  74. 2.2
  75. Added multi-language support. - thanks to elari, who wrote all the code around handling all active langs in OSC
  76. Added category names assumed to be in default language - thanks to elari again!  who wrote all that code as well
  77. Fixed bug where files wouldn't split because the check for EOREOR was too specific.
  78. Added separate file for functions tep_get_uploaded_file and friends so that older snapshots will have it and work.
  79. Finally updated the docs since they sucked
  80. Moved product_model field to the start of each row because sometimes, if the image name was empty, the parsing would get confused
  81. ------------------------------------
  82. 2.3
  83. Thanks to these sponsors - their financial support made this release possible!
  84. Support for more than one output file format with variable numbers of columns
  85.     Sponsored by Ted Joffs
  86.  
  87. Support for Separate Price per Customer mod
  88.     Sponsored by Alan Pace
  89.  
  90. Support for Linda's Header Controller v2.0
  91.     Sponsored by Stewart MacKenzie
  92.  
  93. Removed quotes around all the fields on export.
  94. Added configuration variable so you can turn off the qoutes -> control codes replacement
  95. Merged Elari's changes to not hardcode language id's
  96. ------------------------------------
  97. 2.31
  98. Bugfix for single language non-english name/desc not being put into the output file.
  99. The code was still checking for product_name_1 instead of product_name_$langid.
  100. ------------------------------------
  101. 2.32 - never released into the wild
  102. Added config var $zero_qty_inactive, defaulted to true.
  103. This will make zero qty items inactive by default.
  104. ---- STILL NEED TO DEBUG THIS! ----
  105. ------------------------------------
  106. 2.4
  107. Support for Froogle downloads to EP.
  108.     Sponsored by Ted Joffs
  109. Changed comments - it's not Multiple Price per Product, it's
  110. Separate Price per Customer.
  111. ------------------------------------
  112. 2.41beta
  113. Fixed bugs with Froogle:
  114. 1. Category not getting built right
  115. 2. Strip HTML from name/description
  116. 3. Handle SearchEngineFriendly URL's
  117.  
  118. Adding "Delete" capability via EP. -- NOT COMPLETE
  119. Fixed bug - the Model/Category would give SQL errors
  120. Fixed bug - Items with no manufacturer were getting a man of '' (empty string)
  121. Fixed bug - When trying to import, all items gave a "Deleting product" message but no db changes
  122.     This was because I'd tried inserting the delete functionality and didn't finish it.
  123.     Commented it out for now.
  124. Added Date_added, fixed Date_available
  125. Fixed active/inactive status settings
  126. Fixed bug with misnamed item for Linda's Header Controller support
  127. Fixed bug with SQL syntax error with new products
  128. These following 3 fixes thanks to Yassen Yotov
  129.     Fixed bug where the default image name vars weren't declared global in function walk()
  130.     Added set_time_limit call, it won't cover all cases, but hopefully many. commented out to avoid
  131.         complaints with safe mode servers
  132.     Fixed hardcoded catalog/temp/ in output string for splitting files
  133. ------------------------------------
  134. 2.5
  135. DJZeon found a bug where product URL was getting lost because I always deleted and inserted the product description info - fixed
  136. Same bug also was causing times viewed to be reset to zero because I always deleted and inserted the product descriiption.
  137. Added the multi-image lines from Debbie and Nickie - Thanks!
  138. Changed the output file name to make more sense, now it looks like EP2003Mar20-09:45.txt
  139. ------------------------------------
  140. 2.51
  141. No code changes, bump version because I forgot to update the docs about the high-to-low category order
  142. ------------------------------------
  143. 2.53
  144. Bug fixes?
  145. ------------------------------------
  146. 2.60
  147. Fix froogle categories in reverse order bug
  148. Comment out mimage lines that were causing problems for people in 2.53
  149. Added separator configuration variable so you can pick the separator character to use.
  150. Made Froogle download look for an applicable specials price
  151. Froogle downloads have "froogle" at the start of the file name
  152. You can now specify a file in the temp directory and it will upload that instead of uploading via the browser
  153. ------------------------------------
  154. 2.61-MS2
  155. Bug fixes thanks to frozenlightning.com
  156. Replaced tep_array_merge with array_merge to bring up to MS2 Standards.
  157. Modified by Deborah Carney, inspired by the Think Tank to be included in the CRE Loaded 6
  158. New support will be found at http://phesis.co.uk in the forums, as well as at forums.oscommerce.com in the Contributions section.  This script was/is written by volunteers, please don't email or PM them, more answers are available in the forums if you search.  If you want EP to do something, someone else probably already asked....
  159. Known issue:  html in the product description gets messed up, not sure how to fix it.  
  160.  
  161. 2.62-MS2
  162. Modified by Karlheinz Meier on 08.August.2003
  163. As some people happen to have something like DIFFERENT_Prefix_TABLES in their databases,
  164. normally there is a file called /admin/includes/database_tables.php
  165. which defines nice varibales for them; you can for example make prefixed_databases, or live backups or so.
  166. As some programmers did use DIRECT table names, this was changed.
  167. And now if you use the "separte price..."-module and have an article where there is NO special price,
  168. the import process won't die
  169.  
  170. Derived from the Excel Import 1.51 by:
  171.  
  172. ukrainianshop.net
  173.  
  174.   Copyright (c) 2002-2003 Tim Wasson
  175.   Released under the GNU General Public License
  176. */
  177. //*******************************
  178. //*******************************
  179. // E N D
  180. // E D I T   H I S T O R Y
  181. //*******************************
  182. //*******************************
  183.  
  184. //
  185. //*******************************
  186. //*******************************
  187. // C O N F I G U R A T I O N
  188. // V A R I A B L E S
  189. //*******************************
  190. //*******************************
  191.  
  192. // **** Temp directory ****
  193. // if you changed your directory structure from stock and do not have /catalog/temp/, then you'll need to change this accordingly.
  194. //
  195. $tempdir = "temp/";
  196. $tempdir2 = "temp/";
  197.  
  198. //**** File Splitting Configuration ****
  199. // we attempt to set the timeout limit longer for this script to avoid having to split the files
  200. // NOTE:  If your server is running in safe mode, this setting cannot override the timeout set in php.ini
  201. // uncomment this if you are not on a safe mode server and you are getting timeouts
  202. //set_time_limit(330);
  203.  
  204. // if you are splitting files, this will set the maximum number of records to put in each file.
  205. // if you set your php.ini to a long time, you can make this number bigger
  206. global $maxrecs;
  207. $maxrecs = 300; // default, seems to work for most people.  Reduce if you hit timeouts
  208. //$maxrecs = 4; // for testing
  209.  
  210. //**** Image Defaulting ****
  211. global $default_images, $default_image_manufacturer, $default_image_product, $default_image_category;
  212.  
  213. // set them to your own default "We don't have any picture" gif
  214. //$default_image_manufacturer = 'no_image_manufacturer.gif';
  215. //$default_image_product = 'no_image_product.gif';
  216. //$default_image_category = 'no_image_category.gif';
  217.  
  218. // or let them get set to nothing
  219. $default_image_manufacturer = '';
  220. $default_image_product = '';
  221. $default_image_category = '';
  222.  
  223. //**** Status Field Setting ****
  224. // Set the v_status field to "Inactive" if you want the status=0 in the system
  225. // Set the v_status field to "Delete" if you want to remove the item from the system <- THIS IS NOT WORKING YET!
  226. // If zero_qty_inactive is true, then items with zero qty will automatically be inactive in the store.
  227. global $active, $inactive, $zero_qty_inactive, $deleteit;
  228. $active = 'Active';
  229. $inactive = 'Inactive';
  230. //$deleteit = 'Delete'; // not functional yet
  231. $zero_qty_inactive = true;
  232.  
  233. //**** Size of products_model in products table ****
  234. // set this to the size of your model number field in the db.  We check to make sure all models are no longer than this value.
  235. // this prevents the database from getting fubared.  Just making this number bigger won't help your database!  They must match!
  236. global $modelsize;
  237. $modelsize = 15;
  238.  
  239. //**** Price includes tax? ****
  240. // Set the v_price_with_tax to
  241. // 0 if you want the price without the tax included
  242. // 1 if you want the price to be defined for import & export including tax.
  243. global $price_with_tax;
  244. $price_with_tax = true;
  245.  
  246. // **** Quote -> Escape character conversion ****
  247. // If you have extensive html in your descriptions and it's getting mangled on upload, turn this off
  248. // set to 1 = replace quotes with escape characters
  249. // set to 0 = no quote replacement
  250. global $replace_quotes;
  251. $replace_quotes = true;
  252.  
  253. // **** Field Separator ****
  254. // change this if you can't use the default of tabs
  255. // Tab is the default, comma and semicolon are commonly supported by various progs
  256. // Remember, if your descriptions contain this character, you will confuse EP!
  257. global $separator;
  258. $separator = "\t"; // tab is default
  259. //$separator = ","; // comma
  260. //$separator = ";"; // semi-colon
  261. //$separator = "~"; // tilde
  262. //$separator = "-"; // dash
  263. //$separator = "*"; // splat
  264.  
  265. // **** Max Category Levels ****
  266. // change this if you need more or fewer categories
  267. global $max_categories;
  268. $max_categories = 3; // 7 is default
  269.  
  270.  
  271.  
  272.  
  273. // ****************************************
  274. // Froogle configuration variables
  275. // -- YOU MUST CONFIGURE THIS!  IT WON'T WORK OUT OF THE BOX!
  276. // ****************************************
  277.  
  278. // **** Froogle product info page path ****
  279. // We can't use the tep functions to create the link, because the links will point to the admin, since that's where we're at.
  280. // So put the entire path to your product_info.php page here
  281. global $froogle_product_info_path;
  282. $froogle_product_info_path = "http://www.your-domain/catalog/product_info.php";
  283.  
  284. // **** Froogle product image path ****
  285. // Set this to the path to your images directory
  286. global $froogle_image_path;
  287. $froogle_image_path = "http://www.your-domain/catalog/images/";
  288.  
  289. // **** Froogle - search engine friendly setting
  290. // if your store has SEARCH ENGINE FRIENDLY URLS set, then turn this to true
  291. // I did it this way because I'm having trouble with the code seeing the constants
  292. // that are defined in other places.
  293. global $froogle_SEF_urls;
  294. $froogle_SEF_urls = true;
  295.  
  296.  
  297. // ****************************************
  298. // End Froogle configuration variables
  299. // ****************************************
  300.  
  301. //*******************************
  302. //*******************************
  303. // E N D
  304. // C O N F I G U R A T I O N
  305. // V A R I A B L E S
  306. //*******************************
  307. //*******************************
  308.  
  309.  
  310. //*******************************
  311. //*******************************
  312. // S T A R T
  313. // INITIALIZATION
  314. //*******************************
  315. //*******************************
  316.  
  317.  
  318. require('includes/application_top.php');
  319. require('includes/database_tables.php');
  320.  
  321. //*******************************
  322. // If you are running a pre-Nov1-2002 snapshot of OSC, then we need this include line to avoid
  323. // errors like:
  324. //   undefined function tep_get_uploaded_file
  325.  if (!function_exists(tep_get_uploaded_file)){
  326.     include ('easypopulate_functions.php');
  327.  }
  328. //*******************************
  329.  
  330. global $filelayout, $filelayout_count, $filelayout_sql, $langcode, $fileheaders;
  331.  
  332. // these are the fields that will be defaulted to the current values in the database if they are not found in the incoming file
  333. global $default_these;
  334. $default_these = array(
  335.     'v_products_image',
  336.     #'v_products_mimage',
  337.     #'v_products_bimage',
  338.     #'v_products_subimage1',
  339.     #'v_products_bsubimage1',
  340.     #'v_products_subimage2',
  341.     #'v_products_bsubimage2',
  342.     #'v_products_subimage3',
  343.     #'v_products_bsubimage3',
  344.     'v_categories_id',
  345.     'v_products_price',
  346.     'v_products_quantity',
  347.     'v_products_weight',
  348.     'v_date_avail',
  349.     'v_instock',
  350.     'v_tax_class_title',
  351.     'v_manufacturers_name',
  352.     'v_manufacturers_id',
  353.     'v_products_dim_type',
  354.     'v_products_length',
  355.     'v_products_width',
  356.     'v_products_height'
  357.     );
  358.  
  359. //elari check default language_id from configuration table DEFAULT_LANGUAGE
  360. $epdlanguage_query = tep_db_query("select languages_id, name from " . TABLE_LANGUAGES . " where code = '" . DEFAULT_LANGUAGE . "'");
  361. if (tep_db_num_rows($epdlanguage_query)) {
  362.     $epdlanguage = tep_db_fetch_array($epdlanguage_query);
  363.     $epdlanguage_id   = $epdlanguage['languages_id'];
  364.     $epdlanguage_name = $epdlanguage['name'];
  365. } else {
  366.     Echo 'Strange but there is no default language to work... That may not happen, just in case... ';
  367. }
  368.  
  369. $langcode = ep_get_languages();
  370.  
  371. if ( $dltype != '' ){
  372.     // if dltype is set, then create the filelayout.  Otherwise it gets read from the uploaded file
  373.     ep_create_filelayout($dltype); // get the right filelayout for this download
  374. }
  375. //*******************************
  376. //*******************************
  377. // E N D
  378. // INITIALIZATION
  379. //*******************************
  380. //*******************************
  381.  
  382.  
  383. if ( $download == 'stream' or  $download == 'tempfile' ){
  384.     //*******************************
  385.     //*******************************
  386.     // DOWNLOAD FILE
  387.     //*******************************
  388.     //*******************************
  389.     $filestring = ""; // this holds the csv file we want to download
  390.  
  391.     if ( $dltype=='froogle' ){
  392.         // set the things froogle wants at the top of the file
  393.         $filestring .= "# html_escaped=YES\n";
  394.         $filestring .= "# updates_only=NO\n";
  395.         $filestring .= "# product_type=OTHER\n";
  396.         $filestring .= "# quoted=YES\n";
  397.     }
  398.  
  399.     $result = tep_db_query($filelayout_sql);
  400.     $row =  tep_db_fetch_array($result);
  401.  
  402.     // Here we need to allow for the mapping of internal field names to external field names
  403.     // default to all headers named like the internal ones
  404.     // the field mapping array only needs to cover those fields that need to have their name changed
  405.     if ( count($fileheaders) != 0 ){
  406.         $filelayout_header = $fileheaders; // if they gave us fileheaders for the dl, then use them
  407.     } else {
  408.         $filelayout_header = $filelayout; // if no mapping was spec'd use the internal field names for header names
  409.     }
  410.     //We prepare the table heading with layout values
  411.     foreach( $filelayout_header as $key => $value ){
  412.         $filestring .= $key . $separator;
  413.     }
  414.     // now lop off the trailing tab
  415.     $filestring = substr($filestring, 0, strlen($filestring)-1);
  416.  
  417.     // set the type
  418.     if ( $dltype == 'froogle' ){
  419.         $endofrow = "\n";
  420.     } else {
  421.         // default to normal end of row
  422.         $endofrow = $separator . 'EOREOR' . "\n";
  423.     }
  424.     $filestring .= $endofrow;
  425.  
  426.     $num_of_langs = count($langcode);
  427.     while ($row){
  428.  
  429.  
  430.         // if the filelayout says we need a products_name, get it
  431.         // build the long full froogle image path
  432.         $row['v_products_fullpath_image'] = $froogle_image_path . $row['v_products_image'];
  433.         // Other froogle defaults go here for now
  434.         $row['v_froogle_instock']         = 'Y';
  435.         $row['v_froogle_shipping']         = '';
  436.         $row['v_froogle_upc']             = '';
  437.         $row['v_froogle_color']            = '';
  438.         $row['v_froogle_size']            = '';
  439.         $row['v_froogle_quantitylevel']        = '';
  440.         $row['v_froogle_manufacturer_id']    = '';
  441.         $row['v_froogle_exp_date']        = '';
  442.         $row['v_froogle_product_type']        = 'OTHER';
  443.         $row['v_froogle_delete']        = '';
  444.         $row['v_froogle_currency']        = 'USD';
  445.         $row['v_froogle_offer_id']        = $row['v_products_model'];
  446.         $row['v_froogle_product_id']        = $row['v_products_model'];
  447.  
  448.         // names and descriptions require that we loop thru all languages that are turned on in the store
  449.         foreach ($langcode as $key => $lang){
  450.             $lid = $lang['id'];
  451.  
  452.             // for each language, get the description and set the vals
  453.             $sql2 = "SELECT *
  454.                 FROM ".TABLE_PRODUCTS_DESCRIPTION."
  455.                 WHERE
  456.                     products_id = " . $row['v_products_id'] . " AND
  457.                     language_id = '" . $lid . "'
  458.                 ";
  459.             $result2 = tep_db_query($sql2);
  460.             $row2 =  tep_db_fetch_array($result2);
  461.  
  462.             // I'm only doing this for the first language, since right now froogle is US only.. Fix later!
  463.             // adding url for froogle, but it should be available no matter what
  464.             if ($froogle_SEF_urls){
  465.                 // if only one language
  466.                 if ($num_of_langs == 1){
  467.                     $row['v_froogle_products_url_' . $lid] = $froogle_product_info_path . '/products_id/' . $row['v_products_id'];
  468.                 } else {
  469.                     $row['v_froogle_products_url_' . $lid] = $froogle_product_info_path . '/products_id/' . $row['v_products_id'] . '/language/' . $lid;
  470.                 }
  471.             } else {
  472.                 if ($num_of_langs == 1){
  473.                     $row['v_froogle_products_url_' . $lid] = $froogle_product_info_path . '?products_id=' . $row['v_products_id'];
  474.                 } else {
  475.                     $row['v_froogle_products_url_' . $lid] = $froogle_product_info_path . '?products_id=' . $row['v_products_id'] . '&language=' . $lid;
  476.                 }
  477.             }
  478.  
  479.             $row['v_products_name_' . $lid]     = $row2['products_name'];
  480.             $row['v_products_description_' . $lid]     = $row2['products_description'];
  481.             $row['v_products_url_' . $lid]         = $row2['products_url'];
  482.  
  483.             // froogle advanced format needs the quotes around the name and desc
  484.             $row['v_froogle_products_name_' . $lid] = '"' . strip_tags(str_replace('"','""',$row2['products_name'])) . '"';
  485.             $row['v_froogle_products_description_' . $lid] = '"' . strip_tags(str_replace('"','""',$row2['products_description'])) . '"';
  486.  
  487.             // support for Linda's Header Controller 2.0 here
  488.             if(isset($filelayout['v_products_head_title_tag_' . $lid])){
  489.                 $row['v_products_head_title_tag_' . $lid]     = $row2['products_head_title_tag'];
  490.                 $row['v_products_head_desc_tag_' . $lid]     = $row2['products_head_desc_tag'];
  491.                 $row['v_products_head_keywords_tag_' . $lid]     = $row2['products_head_keywords_tag'];
  492.             }
  493.             // end support for Header Controller 2.0
  494.         }
  495.  
  496.         // for the categories, we need to keep looping until we find the root category
  497.  
  498.         // start with v_categories_id
  499.         // Get the category description
  500.         // set the appropriate variable name
  501.         // if parent_id is not null, then follow it up.
  502.         // we'll populate an aray first, then decide where it goes in the
  503.         $thecategory_id = $row['v_categories_id'];
  504.         $fullcategory = ''; // this will have the entire category stack for froogle
  505.         for( $categorylevel=1; $categorylevel<$max_categories+1; $categorylevel++){
  506.             if ($thecategory_id){
  507.                 $sql2 = "SELECT categories_name
  508.                     FROM ".TABLE_CATEGORIES_DESCRIPTION."
  509.                     WHERE
  510.                         categories_id = " . $thecategory_id . " AND
  511.                         language_id = " . $epdlanguage_id ;
  512.  
  513.                 $result2 = tep_db_query($sql2);
  514.                 $row2 =  tep_db_fetch_array($result2);
  515.                 // only set it if we found something
  516.                 $temprow['v_categories_name_' . $categorylevel] = $row2['categories_name'];
  517.                 // now get the parent ID if there was one
  518.                 $sql3 = "SELECT parent_id
  519.                     FROM ".TABLE_CATEGORIES."
  520.                     WHERE
  521.                         categories_id = " . $thecategory_id;
  522.                 $result3 = tep_db_query($sql3);
  523.                 $row3 =  tep_db_fetch_array($result3);
  524.                 $theparent_id = $row3['parent_id'];
  525.                 if ($theparent_id != ''){
  526.                     // there was a parent ID, lets set thecategoryid to get the next level
  527.                     $thecategory_id = $theparent_id;
  528.                 } else {
  529.                     // we have found the top level category for this item,
  530.                     $thecategory_id = false;
  531.                 }
  532.                 //$fullcategory .= " > " . $row2['categories_name'];
  533.                 $fullcategory = $row2['categories_name'] . " > " . $fullcategory;
  534.             } else {
  535.                 $temprow['v_categories_name_' . $categorylevel] = '';
  536.             }
  537.         }
  538.         // now trim off the last ">" from the category stack
  539.         $row['v_category_fullpath'] = substr($fullcategory,0,strlen($fullcategory)-3);
  540.  
  541.         // temprow has the old style low to high level categories.
  542.         $newlevel = 1;
  543.         // let's turn them into high to low level categories
  544.         for( $categorylevel=6; $categorylevel>0; $categorylevel--){
  545.             if ($temprow['v_categories_name_' . $categorylevel] != ''){
  546.                 $row['v_categories_name_' . $newlevel++] = $temprow['v_categories_name_' . $categorylevel];
  547.             }
  548.         }
  549.         // if the filelayout says we need a manufacturers name, get it
  550.         if (isset($filelayout['v_manufacturers_name'])){
  551.             if ($row['v_manufacturers_id'] != ''){
  552.                 $sql2 = "SELECT manufacturers_name
  553.                     FROM ".TABLE_MANUFACTURERS."
  554.                     WHERE
  555.                     manufacturers_id = " . $row['v_manufacturers_id']
  556.                     ;
  557.                 $result2 = tep_db_query($sql2);
  558.                 $row2 =  tep_db_fetch_array($result2);
  559.                 $row['v_manufacturers_name'] = $row2['manufacturers_name'];
  560.             }
  561.         }
  562.  
  563.  
  564.         // If you have other modules that need to be available, put them here
  565.  
  566.  
  567.         // this is for the separate price per customer module
  568.         if (isset($filelayout['v_customer_price_1'])){
  569.             $sql2 = "SELECT
  570.                     customers_group_price,
  571.                     customers_group_id
  572.                 FROM
  573.                     ".TABLE_PRODUCTS_GROUPS."
  574.                 WHERE
  575.                 products_id = " . $row['v_products_id'] . "
  576.                 ORDER BY
  577.                 customers_group_id"
  578.                 ;
  579.             $result2 = tep_db_query($sql2);
  580.             $ll = 1;
  581.             $row2 =  tep_db_fetch_array($result2);
  582.             while( $row2 ){
  583.                 $row['v_customer_group_id_' . $ll]     = $row2['customers_group_id'];
  584.                 $row['v_customer_price_' . $ll]     = $row2['customers_group_price'];
  585.                 $row2 = tep_db_fetch_array($result2);
  586.                 $ll++;
  587.             }
  588.         }
  589.         if ($dltype == 'froogle'){
  590.             // For froogle, we check the specials prices for any applicable specials, and use that price
  591.             // by grabbing the specials id descending, we always get the most recently added special price
  592.             // I'm checking status because I think you can turn off specials
  593.             $sql2 = "SELECT
  594.                     specials_new_products_price
  595.                 FROM
  596.                     ".TABLE_SPECIALS."
  597.                 WHERE
  598.                 products_id = " . $row['v_products_id'] . " and
  599.                 status = 1 and
  600.                 expires_date < CURRENT_TIMESTAMP
  601.                 ORDER BY
  602.                     specials_id DESC"
  603.                 ;
  604.             $result2 = tep_db_query($sql2);
  605.             $ll = 1;
  606.             $row2 =  tep_db_fetch_array($result2);
  607.             if( $row2 ){
  608.                 // reset the products price to our special price if there is one for this product
  609.                 $row['v_products_price']     = $row2['specials_new_products_price'];
  610.             }
  611.         }
  612.  
  613.         //elari -
  614.         //We check the value of tax class and title instead of the id
  615.         //Then we add the tax to price if $price_with_tax is set to 1
  616.         $row_tax_multiplier         = tep_get_tax_class_rate($row['v_tax_class_id']);
  617.         $row['v_tax_class_title']     = tep_get_tax_class_title($row['v_tax_class_id']);
  618.         $row['v_products_price']     = $row['v_products_price'] +
  619.                             ($price_with_tax * round($row['v_products_price'] * $row_tax_multiplier / 100,2));
  620.  
  621.  
  622.         // Now set the status to a word the user specd in the config vars
  623.         if ( $row['v_status'] == '1' ){
  624.             $row['v_status'] = $active;
  625.         } else {
  626.             $row['v_status'] = $inactive;
  627.         }
  628.  
  629.         // remove any bad things in the texts that could confuse EasyPopulate
  630.         $therow = '';
  631.         foreach( $filelayout as $key => $value ){
  632.             //echo "The field was $key<br>";
  633.  
  634.             $thetext = $row[$key];
  635.             // kill the carriage returns and tabs in the descriptions, they're killing me!
  636.             $thetext = str_replace("\r",' ',$thetext);
  637.             $thetext = str_replace("\n",' ',$thetext);
  638.             $thetext = str_replace("\t",' ',$thetext);
  639.             // and put the text into the output separated by tabs
  640.             $therow .= $thetext . $separator;
  641.         }
  642.  
  643.         // lop off the trailing tab, then append the end of row indicator
  644.         $therow = substr($therow,0,strlen($therow)-1) . $endofrow;
  645.  
  646.         $filestring .= $therow;
  647.         // grab the next row from the db
  648.         $row =  tep_db_fetch_array($result);
  649.     }
  650.  
  651.     #$EXPORT_TIME=time();
  652.     $EXPORT_TIME = strftime('%Y%b%d-%H%I');
  653.     if ($dltype=="froogle"){
  654.         $EXPORT_TIME = "FroogleEP" . $EXPORT_TIME;
  655.     } else {
  656.         $EXPORT_TIME = "EP" . $EXPORT_TIME;
  657.     }
  658.  
  659.     // now either stream it to them or put it in the temp directory
  660.     if ($download == 'stream'){
  661.         //*******************************
  662.         // STREAM FILE
  663.         //*******************************
  664.         header("Content-type: application/vnd.ms-excel");
  665.         header("Content-disposition: attachment; filename=$EXPORT_TIME.txt");
  666.         header("Pragma: no-cache");
  667.         header("Expires: 0");
  668.         echo $filestring;
  669.         die();
  670.     } else {
  671.         //*******************************
  672.         // PUT FILE IN TEMP DIR
  673.         //*******************************
  674.         $tmpfname = DIR_FS_DOCUMENT_ROOT . $tempdir . "$EXPORT_TIME.txt";
  675.         //unlink($tmpfname);
  676.         $fp = fopen( $tmpfname, "w+");
  677.         fwrite($fp, $filestring);
  678.         fclose($fp);
  679.         echo "You can get your file in the Tools/Files under " . $tempdir . "EP" . $EXPORT_TIME . ".txt";
  680.         die();
  681.     }
  682. }   // *** END *** download section
  683. ?>
  684.  
  685. <!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
  686. <html <?php echo HTML_PARAMS; ?>>
  687. <head>
  688. <meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
  689. <title><?php echo TITLE; ?></title>
  690. <link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
  691. </head>
  692. <body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF">
  693. <?php require(DIR_WS_INCLUDES . 'header.php'); ?>
  694.  
  695. <table border="0" width="100%" cellspacing="2" cellpadding="2">
  696. <tr>
  697. <td width="<?php echo BOX_WIDTH; ?>" valign="top" height="27">
  698. <table border="0" width="<?php echo BOX_WIDTH; ?>" cellspacing="1" cellpadding="1" class="columnLeft">
  699. <?php require(DIR_WS_INCLUDES . 'column_left.php');?>
  700. </table></td>
  701. <td class="pageHeading" valign="top"><?php
  702. echo "Easy Populate $curver - Default Language : " . $epdlanguage_name . '(' . $epdlanguage_id .')' ;
  703. ?>
  704.  
  705. <p class="smallText">
  706.  
  707. <?php
  708.  
  709. if ($localfile or (is_uploaded_file($usrfl) && $split==0)) {
  710.     //*******************************
  711.     //*******************************
  712.     // UPLOAD AND INSERT FILE
  713.     //*******************************
  714.     //*******************************
  715.  
  716.     if ($usrfl){
  717.         // move the file to where we can work with it
  718.         $file = tep_get_uploaded_file('usrfl');
  719.         if (is_uploaded_file($file['tmp_name'])) {
  720.             tep_copy_uploaded_file($file, DIR_FS_DOCUMENT_ROOT . $tempdir);
  721.         }
  722.  
  723.         echo "<p class=smallText>";
  724.         echo "File uploaded. <br>";
  725.         echo "Temporary filename: " . $usrfl . "<br>";
  726.         echo "User filename: " . $usrfl_name . "<br>";
  727.         echo "Size: " . $usrfl_size . "<br>";
  728.  
  729.         // get the entire file into an array
  730.         $readed = file(DIR_FS_DOCUMENT_ROOT . $tempdir . $usrfl_name);
  731.     }
  732.     if ($localfile){
  733.         // move the file to where we can work with it
  734.         $file = tep_get_uploaded_file('usrfl');
  735.         if (is_uploaded_file($file['tmp_name'])) {
  736.             tep_copy_uploaded_file($file, DIR_FS_DOCUMENT_ROOT . $tempdir);
  737.         }
  738.  
  739.         echo "<p class=smallText>";
  740.         echo "Filename: " . $localfile . "<br>";
  741.  
  742.         // get the entire file into an array
  743.         $readed = file(DIR_FS_DOCUMENT_ROOT . $tempdir . $localfile);
  744.     }
  745.  
  746.     // now we string the entire thing together in case there were carriage returns in the data
  747.     $newreaded = "";
  748.     foreach ($readed as $read){
  749.         $newreaded .= $read;
  750.     }
  751.  
  752.     // now newreaded has the entire file together without the carriage returns.
  753.     // if for some reason excel put qoutes around our EOREOR, remove them then split into rows
  754.     $newreaded = str_replace('"EOREOR"', 'EOREOR', $newreaded);
  755.     $readed = explode( $separator . 'EOREOR',$newreaded);
  756.  
  757.  
  758.     // Now we'll populate the filelayout based on the header row.
  759.     $theheaders_array = explode( $separator, $readed[0] ); // explode the first row, it will be our filelayout
  760.     $lll = 0;
  761.     $filelayout = array();
  762.     foreach( $theheaders_array as $header ){
  763.         $cleanheader = str_replace( '"', '', $header);
  764.     //    echo "Fileheader was $header<br><br><br>";
  765.         $filelayout[ $cleanheader ] = $lll++; //
  766.     }
  767.     unset($readed[0]); //  we don't want to process the headers with the data
  768.  
  769.     // now we've got the array broken into parts by the expicit end-of-row marker.
  770.     array_walk($readed, 'walk');
  771.  
  772. }
  773.  
  774. if (is_uploaded_file($usrfl) && $split==1) {
  775.     //*******************************
  776.     //*******************************
  777.     // UPLOAD AND SPLIT FILE
  778.     //*******************************
  779.     //*******************************
  780.     // move the file to where we can work with it
  781.     $file = tep_get_uploaded_file('usrfl');
  782.     //echo "Trying to move file...";
  783.     if (is_uploaded_file($file['tmp_name'])) {
  784.         tep_copy_uploaded_file($file, DIR_FS_DOCUMENT_ROOT . $tempdir);
  785.     }
  786.  
  787.     $infp = fopen(DIR_FS_DOCUMENT_ROOT . $tempdir . $usrfl_name, "r");
  788.  
  789.     //toprow has the field headers
  790.     $toprow = fgets($infp,32768);
  791.  
  792.     $filecount = 1;
  793.  
  794.     echo "Creating file EP_Split" . $filecount . ".txt ...  ";
  795.     $tmpfname = DIR_FS_DOCUMENT_ROOT . $tempdir . "EP_Split" . $filecount . ".txt";
  796.     $fp = fopen( $tmpfname, "w+");
  797.     fwrite($fp, $toprow);
  798.  
  799.     $linecount = 0;
  800.     $line = fgets($infp,32768);
  801.     while ($line){
  802.         // walking the entire file one row at a time
  803.         // but a line is not necessarily a complete row, we need to split on rows that have "EOREOR" at the end
  804.         $line = str_replace('"EOREOR"', 'EOREOR', $line);
  805.         fwrite($fp, $line);
  806.         if (strpos($line, 'EOREOR')){
  807.             // we found the end of a line of data, store it
  808.             $linecount++; // increment our line counter
  809.             if ($linecount >= $maxrecs){
  810.                 echo "Added $linecount records and closing file... <Br>";
  811.                 $linecount = 0; // reset our line counter
  812.                 // close the existing file and open another;
  813.                 fclose($fp);
  814.                 // increment filecount
  815.                 $filecount++;
  816.                 echo "Creating file EP_Split" . $filecount . ".txt ...  ";
  817.                 $tmpfname = DIR_FS_DOCUMENT_ROOT . $tempdir . "EP_Split" . $filecount . ".txt";
  818.                 //Open next file name
  819.                 $fp = fopen( $tmpfname, "w+");
  820.                 fwrite($fp, $toprow);
  821.             }
  822.         }
  823.         $line=fgets($infp,32768);
  824.     }
  825.     echo "Added $linecount records and closing file...<br><br> ";
  826.     fclose($fp);
  827.     fclose($infp);
  828.  
  829.     echo "You can download your split files in the Tools/Files under /catalog/temp/";
  830.  
  831. }
  832.  
  833. ?>
  834.       </p>
  835.  
  836.       <table width="75%" border="2">
  837.         <tr>
  838.           <td width="75%">
  839.            <FORM ENCTYPE="multipart/form-data" ACTION="easypopulate.php?split=0" METHOD=POST>
  840.               <p>
  841.                 <div align = "left">
  842.                 <p><b>Upload EP File</b></p>
  843.                 <p>
  844.                   <INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="100000000">
  845.                   <p></p>
  846.                   <input name="usrfl" type="file" size="50">
  847.                   <input type="submit" name="buttoninsert" value="Insert into db"><br>
  848.                 </p>
  849.               </div>
  850.  
  851.               </form>
  852.  
  853.            <FORM ENCTYPE="multipart/form-data" ACTION="easypopulate.php?split=1" METHOD=POST>
  854.               <p>
  855.                 <div align = "left">
  856.                 <p><b>Split EP File</b></p>
  857.                 <p>
  858.                   <INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="1000000000">
  859.                   <p></p>
  860.                   <input name="usrfl" type="file" size="50">
  861.                   <input type="submit" name="buttonsplit" value="Split file"><br>
  862.                 </p>
  863.               </div>
  864.  
  865.              </form>
  866.  
  867.            <FORM ENCTYPE="multipart/form-data" ACTION="easypopulate.php" METHOD=POST>
  868.               <p>
  869.                 <div align = "left">
  870.                 <p><b>Import from Temp Dir (<? echo $tempdir; ?>)</b></p>
  871.         <p class="smallText">
  872.         <INPUT TYPE="text" name="localfile" size="50">
  873.                   <input type="submit" name="buttoninsert" value="Insert into db"><br>
  874.                 </p>
  875.               </div>
  876.  
  877.              </form>
  878.  
  879.  
  880.  
  881.  
  882.         <p><b>Download EP and Froogle Files</b></p>
  883.  
  884.           <!-- Download file links -  Add your custom fields here -->
  885.       <a href="easypopulate.php?download=stream&dltype=full">Download <b>Complete</b> tab-delimited .txt file to edit</a><br>
  886.       <a href="easypopulate.php?download=stream&dltype=priceqty">Download <b>Model/Price/Qty</b> tab-delimited .txt file to edit</a><br>
  887.       <a href="easypopulate.php?download=stream&dltype=category">Download <b>Model/Category</b> tab-delimited .txt file to edit</a><br>
  888.       <a href="easypopulate.php?download=stream&dltype=froogle">Download <b>Froogle</b> tab-delimited .txt file</a><br>
  889.  
  890.         <p><b>Create EP and Froogle Files in Temp Dir (<? echo $tempdir; ?>)</b></p>
  891.       <a href="easypopulate.php?download=tempfile&dltype=full">Create Complete tab-delimited .txt file in temp dir</a><br>
  892.           <a href="easypopulate.php?download=tempfile&dltype=priceqty"">Create Model/Price/Qty tab-delimited .txt file in temp dir</a><br>
  893.           <a href="easypopulate.php?download=tempfile&dltype=category">Create Model/Category tab-delimited .txt file in temp dir</a><br>
  894.       <a href="easypopulate.php?download=tempfile&dltype=froogle">Create Froogle tab-delimited .txt file in temp dir</a><br>
  895.       </td>
  896.     </tr>
  897.       </table>
  898.     </td>
  899.  </tr>
  900. </table>
  901.  
  902. <?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
  903.  
  904. <p>†</p>
  905. <p>†</p><p><br>
  906. </p></body>
  907. </html>
  908.  
  909. <?php
  910.  
  911. function ep_get_languages() {
  912.     $languages_query = tep_db_query("select languages_id, code from " . TABLE_LANGUAGES . " order by sort_order");
  913.     // start array at one, the rest of the code expects it that way
  914.     $ll =1;
  915.     while ($ep_languages = tep_db_fetch_array($languages_query)) {
  916.         //will be used to return language_id en language code to report in product_name_code instead of product_name_id
  917.         $ep_languages_array[$ll++] = array(
  918.                     'id' => $ep_languages['languages_id'],
  919.                     'code' => $ep_languages['code']
  920.                     );
  921.     }
  922.     return $ep_languages_array;
  923. };
  924.  
  925. function tep_get_tax_class_rate($tax_class_id) {
  926.     $tax_multiplier = 0;
  927.     $tax_query = tep_db_query("select SUM(tax_rate) as tax_rate from " . TABLE_TAX_RATES . " WHERE  tax_class_id = '" . $tax_class_id . "' GROUP BY tax_priority");
  928.     if (tep_db_num_rows($tax_query)) {
  929.         while ($tax = tep_db_fetch_array($tax_query)) {
  930.             $tax_multiplier += $tax['tax_rate'];
  931.         }
  932.     }
  933.     return $tax_multiplier;
  934. };
  935.  
  936. function tep_get_tax_title_class_id($tax_class_title) {
  937.     $classes_query = tep_db_query("select tax_class_id from " . TABLE_TAX_CLASS . " WHERE tax_class_title = '" . $tax_class_title . "'" );
  938.     $tax_class_array = tep_db_fetch_array($classes_query);
  939.     $tax_class_id = $tax_class_array['tax_class_id'];
  940.     return $tax_class_id ;
  941. }
  942.  
  943. function print_el( $item2 ) {
  944.     echo " | " . substr(strip_tags($item2), 0, 10);
  945. };
  946.  
  947. function print_el1( $item2 ) {
  948.     echo sprintf("| %'.4s ", substr(strip_tags($item2), 0, 80));
  949. };
  950. function ep_create_filelayout($dltype){
  951.     global $filelayout, $filelayout_count, $filelayout_sql, $langcode, $fileheaders, $max_categories;
  952.     // depending on the type of the download the user wanted, create a file layout for it.
  953.     $fieldmap = array(); // default to no mapping to change internal field names to external.
  954.     switch( $dltype ){
  955.     case 'full':
  956.         // The file layout is dynamically made depending on the number of languages
  957.         $iii = 0;
  958.         $filelayout = array(
  959.             'v_products_model'        => $iii++,
  960.             'v_products_image'        => $iii++,
  961.             );
  962.  
  963.         foreach ($langcode as $key => $lang){
  964.             $l_id = $lang['id'];
  965.             // uncomment the head_title, head_desc, and head_keywords to use
  966.             // Linda's Header Tag Controller 2.0
  967.             //echo $langcode['id'] . $langcode['code'];
  968.             $filelayout  = array_merge($filelayout , array(
  969.                     'v_products_name_' . $l_id        => $iii++,
  970.                     'v_products_description_' . $l_id    => $iii++,
  971.                     'v_products_url_' . $l_id    => $iii++,
  972.             //        'v_products_head_title_tag_'.$l_id    => $iii++,
  973.             //        'v_products_head_desc_tag_'.$l_id    => $iii++,
  974.             //        'v_products_head_keywords_tag_'.$l_id    => $iii++,
  975.                     ));
  976.         }
  977.  
  978.  
  979.         // uncomment the customer_price and customer_group to support multi-price per product contrib
  980.  
  981.         $filelayout  = array_merge($filelayout , array(
  982.             'v_products_price'        => $iii++,
  983.             'v_products_weight'        => $iii++,
  984.             'v_date_avail'            => $iii++,
  985.             'v_date_added'            => $iii++,
  986.             'v_products_quantity'        => $iii++,
  987.             'v_customer_price_1'        => $iii++,
  988.             'v_customer_group_id_1'        => $iii++,
  989.             'v_customer_price_2'        => $iii++,
  990.             'v_customer_group_id_2'        => $iii++,
  991.             #'v_customer_price_3'        => $iii++,
  992.             #'v_customer_group_id_3'        => $iii++,
  993.             #'v_customer_price_4'        => $iii++,
  994.             #'v_customer_group_id_4'        => $iii++,
  995.             'v_manufacturers_name'        => $iii++,
  996.             ));
  997.  
  998.         // build the categories name section of the array based on the number of categores the user wants to have
  999.         for($i=1;$i<$max_categories+1;$i++){
  1000.             $filelayout = array_merge($filelayout, array('v_categories_name_' . $i => $iii++));
  1001.         }
  1002.  
  1003.         $filelayout = array_merge($filelayout, array(
  1004.             'v_tax_class_title'        => $iii++,
  1005.             'v_status'            => $iii++,
  1006.             ));
  1007.  
  1008.         $filelayout_sql = "SELECT
  1009.             p.products_id as v_products_id,
  1010.             p.products_model as v_products_model,
  1011.             p.products_image as v_products_image,
  1012.             p.products_price as v_products_price,
  1013.             p.products_weight as v_products_weight,
  1014.             p.products_date_available as v_date_avail,
  1015.             p.products_date_added as v_date_added,
  1016.             p.products_tax_class_id as v_tax_class_id,
  1017.             p.products_quantity as v_products_quantity,
  1018.             p.manufacturers_id as v_manufacturers_id,
  1019.             subc.categories_id as v_categories_id,
  1020.             p.products_status as v_status
  1021.             FROM
  1022.             ".TABLE_PRODUCTS." as p,
  1023.             ".TABLE_CATEGORIES." as subc,
  1024.             ".TABLE_PRODUCTS_TO_CATEGORIES." as ptoc
  1025.             WHERE
  1026.             p.products_id = ptoc.products_id AND
  1027.             ptoc.categories_id = subc.categories_id
  1028.             ";
  1029.  
  1030.         break;
  1031.     case 'priceqty':
  1032.         $iii = 0;
  1033.         // uncomment the customer_price and customer_group to support multi-price per product contrib
  1034.         $filelayout = array(
  1035.             'v_products_model'        => $iii++,
  1036.             'v_products_price'        => $iii++,
  1037.             'v_products_quantity'        => $iii++,
  1038.             'v_customer_price_1'        => $iii++,
  1039.             'v_customer_group_id_1'        => $iii++,
  1040.             'v_customer_price_2'        => $iii++,
  1041.             'v_customer_group_id_2'        => $iii++,
  1042.             #'v_customer_price_3'        => $iii++,
  1043.             #'v_customer_group_id_3'        => $iii++,
  1044.             #'v_customer_price_4'        => $iii++,
  1045.             #'v_customer_group_id_4'        => $iii++,
  1046.                 );
  1047.         $filelayout_sql = "SELECT
  1048.             p.products_id as v_products_id,
  1049.             p.products_model as v_products_model,
  1050.             p.products_price as v_products_price,
  1051.             p.products_quantity as v_products_quantity
  1052.             FROM
  1053.             ".TABLE_PRODUCTS." as p
  1054.             ";
  1055.  
  1056.         break;
  1057.  
  1058.     case 'category':
  1059.         // The file layout is dynamically made depending on the number of languages
  1060.         $iii = 0;
  1061.         $filelayout = array(
  1062.             'v_products_model'        => $iii++,
  1063.         );
  1064.  
  1065.         // build the categories name section of the array based on the number of categores the user wants to have
  1066.         for($i=1;$i<$max_categories+1;$i++){
  1067.             $filelayout = array_merge($filelayout, array('v_categories_name_' . $i => $iii++));
  1068.         }
  1069.  
  1070.  
  1071.         $filelayout_sql = "SELECT
  1072.             p.products_id as v_products_id,
  1073.             p.products_model as v_products_model,
  1074.             subc.categories_id as v_categories_id
  1075.             FROM
  1076.             ".TABLE_PRODUCTS." as p,
  1077.             ".TABLE_CATEGORIES." as subc,
  1078.             ".TABLE_PRODUCTS_TO_CATEGORIES." as ptoc            
  1079.             WHERE
  1080.             p.products_id = ptoc.products_id AND
  1081.             ptoc.categories_id = subc.categories_id
  1082.             ";
  1083.         break;
  1084.  
  1085.     case 'froogle':
  1086.         // this is going to be a little interesting because we need
  1087.         // a way to map from internal names to external names
  1088.         //
  1089.         // Before it didn't matter, but with froogle needing particular headers,
  1090.         // The file layout is dynamically made depending on the number of languages
  1091.         $iii = 0;
  1092.         $filelayout = array(
  1093.             'v_froogle_products_url_1'            => $iii++,
  1094.             );
  1095.         //
  1096.         // here we need to get the default language and put
  1097.         $l_id = 1; // dummy it in for now.
  1098. //        foreach ($langcode as $key => $lang){
  1099. //            $l_id = $lang['id'];
  1100.             $filelayout  = array_merge($filelayout , array(
  1101.                     'v_froogle_products_name_' . $l_id        => $iii++,
  1102.                     'v_froogle_products_description_' . $l_id    => $iii++,
  1103.                     ));
  1104. //        }
  1105.         $filelayout  = array_merge($filelayout , array(
  1106.             'v_products_price'        => $iii++,
  1107.             'v_products_fullpath_image'    => $iii++,
  1108.             'v_category_fullpath'        => $iii++,
  1109.             'v_froogle_offer_id'        => $iii++,
  1110.             'v_froogle_instock'        => $iii++,
  1111.             'v_froogle_ shipping'        => $iii++,
  1112.             'v_manufacturers_name'        => $iii++,
  1113.             'v_froogle_ upc'        => $iii++,
  1114.             'v_froogle_color'        => $iii++,
  1115.             'v_froogle_size'        => $iii++,
  1116.             'v_froogle_quantitylevel'    => $iii++,
  1117.             'v_froogle_product_id'        => $iii++,
  1118.             'v_froogle_manufacturer_id'    => $iii++,
  1119.             'v_froogle_exp_date'        => $iii++,
  1120.             'v_froogle_product_type'    => $iii++,
  1121.             'v_froogle_delete'        => $iii++,
  1122.             'v_froogle_currency'        => $iii++,
  1123.                 ));
  1124.         $iii=0;
  1125.         $fileheaders = array(
  1126.             'product_url'        => $iii++,
  1127.             'name'            => $iii++,
  1128.             'description'        => $iii++,
  1129.             'price'            => $iii++,
  1130.             'image_url'        => $iii++,
  1131.             'category'        => $iii++,
  1132.             'offer_id'        => $iii++,
  1133.             'instock'        => $iii++,
  1134.             'shipping'        => $iii++,
  1135.             'brand'            => $iii++,
  1136.             'upc'            => $iii++,
  1137.             'color'            => $iii++,
  1138.             'size'            => $iii++,
  1139.             'quantity'        => $iii++,
  1140.             'product_id'        => $iii++,
  1141.             'manufacturer_id'    => $iii++,
  1142.             'exp_date'        => $iii++,
  1143.             'product_type'        => $iii++,
  1144.             'delete'        => $iii++,
  1145.             'currency'        => $iii++,
  1146.             );
  1147.         $filelayout_sql = "SELECT
  1148.             p.products_id as v_products_id,
  1149.             p.products_model as v_products_model,
  1150.             p.products_image as v_products_image,
  1151.             p.products_price as v_products_price,
  1152.             p.products_weight as v_products_weight,
  1153.             p.products_date_added as v_date_avail,
  1154.             p.products_tax_class_id as v_tax_class_id,
  1155.             p.products_quantity as v_products_quantity,
  1156.             p.manufacturers_id as v_manufacturers_id,
  1157.             subc.categories_id as v_categories_id
  1158.             FROM
  1159.             ".TABLE_PRODUCTS." as p,
  1160.             ".TABLE_CATEGORIES." as subc,
  1161.             ".TABLE_PRODUCTS_TO_CATEGORIES." as ptoc
  1162.             WHERE
  1163.             p.products_id = ptoc.products_id AND
  1164.             ptoc.categories_id = subc.categories_id
  1165.             ";
  1166.         break;
  1167.  
  1168.     }
  1169.     $filelayout_count = count($filelayout);
  1170.  
  1171. }
  1172.  
  1173.  
  1174. function walk( $item1 ) {
  1175.     global $filelayout, $filelayout_count, $modelsize;
  1176.     global $active, $inactive, $langcode, $default_these, $deleteit, $zero_qty_inactive;
  1177.         global $epdlanguage_id, $price_with_tax, $replace_quotes;
  1178.     global $default_images, $default_image_manufacturer, $default_image_product, $default_image_category;
  1179.     global $separator, $max_categories;
  1180.     // first we clean up the row of data
  1181.  
  1182.     // chop blanks from each end
  1183.     $item1 = ltrim(rtrim($item1));
  1184.  
  1185.     // blow it into an array, splitting on the tabs
  1186.     $items = explode($separator, $item1);
  1187.  
  1188.     // make sure all non-set things are set to '';
  1189.     // and strip the quotes from the start and end of the stings.
  1190.     // escape any special chars for the database.
  1191.     foreach( $filelayout as $key=> $value){
  1192.         $i = $filelayout[$key];
  1193.         if (isset($items[$i]) == false) {
  1194.             $items[$i]='';
  1195.         } else {
  1196.             // Check to see if either of the magic_quotes are turned on or off;
  1197.             // And apply filtering accordingly.
  1198.             if (function_exists('ini_get')) {
  1199.                 //echo "Getting ready to check magic quotes<br>";
  1200.                 if (ini_get('magic_quotes_runtime') == 1){
  1201.                     // The magic_quotes_runtime are on, so lets account for them
  1202.                     // check if the last character is a quote;
  1203.                     // if it is, chop off the quotes.
  1204.                     if (substr($items[$i],-1) == '"'){
  1205.                         $items[$i] = substr($items[$i],2,strlen($items[$i])-4);
  1206.                     }
  1207.                     // now any remaining doubled double quotes should be converted to one doublequote
  1208.                     $items[$i] = str_replace('\"\"',""",$items[$i]);
  1209.                     if ($replace_quotes){
  1210.                         $items[$i] = str_replace('\"',""",$items[$i]);
  1211.                         $items[$i] = str_replace("\'","'",$items[$i]);
  1212.                     }
  1213.                 } else { // no magic_quotes are on
  1214.                     // check if the last character is a quote;
  1215.                     // if it is, chop off the 1st and last character of the string.
  1216.                     if (substr($items[$i],-1) == '"'){
  1217.                         $items[$i] = substr($items[$i],1,strlen($items[$i])-2);
  1218.                     }
  1219.                     // now any remaining doubled double quotes should be converted to one doublequote
  1220.                     $items[$i] = str_replace('""',""",$items[$i]);
  1221.                     if ($replace_quotes){
  1222.                         $items[$i] = str_replace('"',""",$items[$i]);
  1223.                         $items[$i] = str_replace("'","'",$items[$i]);
  1224.                     }
  1225.                 }
  1226.             }
  1227.         }
  1228.     }
  1229. /*
  1230.     if ( $items['v_status'] == $deleteit ){
  1231.         // they want to delete this product.
  1232.         echo "Deleting product " . $items['v_products_model'] . " from the database<br>";
  1233.         // Get the ID
  1234.  
  1235.         // kill in the products_to_categories
  1236.  
  1237.         // Kill in the products table
  1238.  
  1239.         return; // we're done deleteing!
  1240.     }
  1241. */
  1242.     // now do a query to get the record's current contents
  1243.     $sql = "SELECT
  1244.         p.products_id as v_products_id,
  1245.         p.products_model as v_products_model,
  1246.         p.products_image as v_products_image,
  1247.         p.products_price as v_products_price,
  1248.         p.products_weight as v_products_weight,
  1249.         p.products_date_added as v_date_avail,
  1250.         p.products_tax_class_id as v_tax_class_id,
  1251.         p.products_quantity as v_products_quantity,
  1252.         p.manufacturers_id as v_manufacturers_id,
  1253.         subc.categories_id as v_categories_id
  1254.         FROM
  1255.         ".TABLE_PRODUCTS." as p,
  1256.         ".TABLE_CATEGORIES." as subc,
  1257.         ".TABLE_PRODUCTS_TO_CATEGORIES." as ptoc
  1258.         WHERE
  1259.         p.products_id = ptoc.products_id AND
  1260.         p.products_model = '" . $items[$filelayout['v_products_model']] . "' AND
  1261.         ptoc.categories_id = subc.categories_id
  1262.         ";
  1263.  
  1264.     $result = tep_db_query($sql);
  1265.     $row =  tep_db_fetch_array($result);
  1266.  
  1267.  
  1268.     while ($row){
  1269.         // OK, since we got a row, the item already exists.
  1270.         // Let's get all the data we need and fill in all the fields that need to be defaulted to the current values
  1271.         // for each language, get the description and set the vals
  1272.         foreach ($langcode as $key => $lang){
  1273.             //echo "Inside defaulting loop";
  1274.             //echo "key is $key<br>";
  1275.             //echo "langid is " . $lang['id'] . "<br>";
  1276. //            $sql2 = "SELECT products_name, products_description
  1277. //                FROM ".TABLE_PRODUCTS_DESCRIPTION."
  1278. //                WHERE
  1279. //                    products_id = " . $row['v_products_id'] . " AND
  1280. //                    language_id = '" . $lang['id'] . "'
  1281. //                ";
  1282.             $sql2 = "SELECT *
  1283.                 FROM ".TABLE_PRODUCTS_DESCRIPTION."
  1284.                 WHERE
  1285.                     products_id = " . $row['v_products_id'] . " AND
  1286.                     language_id = '" . $lang['id'] . "'
  1287.                 ";
  1288.             $result2 = tep_db_query($sql2);
  1289.             $row2 =  tep_db_fetch_array($result2);
  1290.                         // Need to report from ......_name_1 not ..._name_0
  1291.             $row['v_products_name_' . $lang['id']]         = $row2['products_name'];
  1292.             $row['v_products_description_' . $lang['id']]     = $row2['products_description'];
  1293.             $row['v_products_url_' . $lang['id']]         = $row2['products_url'];
  1294.  
  1295.             // support for Linda's Header Controller 2.0 here
  1296.             if(isset($filelayout['v_products_head_title_tag_' . $lang['id'] ])){
  1297.                 $row['v_products_head_title_tag_' . $lang['id']]     = $row2['products_head_title_tag'];
  1298.                 $row['v_products_head_desc_tag_' . $lang['id']]     = $row2['products_head_desc_tag'];
  1299.                 $row['v_products_head_keywords_tag_' . $lang['id']]     = $row2['products_head_keywords_tag'];
  1300.             }
  1301.             // end support for Header Controller 2.0
  1302.         }
  1303.  
  1304.         // start with v_categories_id
  1305.         // Get the category description
  1306.         // set the appropriate variable name
  1307.         // if parent_id is not null, then follow it up.
  1308.         $thecategory_id = $row['v_categories_id'];
  1309.  
  1310.         for( $categorylevel=1; $categorylevel<$max_categories+1; $categorylevel++){
  1311.             if ($thecategory_id){
  1312.                 $sql2 = "SELECT categories_name
  1313.                     FROM ".TABLE_CATEGORIES_DESCRIPTION."
  1314.                     WHERE
  1315.                         categories_id = " . $thecategory_id . " AND
  1316.                         language_id = " . $epdlanguage_id ;
  1317.  
  1318.                 $result2 = tep_db_query($sql2);
  1319.                 $row2 =  tep_db_fetch_array($result2);
  1320.                 // only set it if we found something
  1321.                 $temprow['v_categories_name_' . $categorylevel] = $row2['categories_name'];
  1322.                 // now get the parent ID if there was one
  1323.                 $sql3 = "SELECT parent_id
  1324.                     FROM ".TABLE_CATEGORIES."
  1325.                     WHERE
  1326.                         categories_id = " . $thecategory_id;
  1327.                 $result3 = tep_db_query($sql3);
  1328.                 $row3 =  tep_db_fetch_array($result3);
  1329.                 $theparent_id = $row3['parent_id'];
  1330.                 if ($theparent_id != ''){
  1331.                     // there was a parent ID, lets set thecategoryid to get the next level
  1332.                     $thecategory_id = $theparent_id;
  1333.                 } else {
  1334.                     // we have found the top level category for this item,
  1335.                     $thecategory_id = false;
  1336.                 }
  1337.             } else {
  1338.                     $temprow['v_categories_name_' . $categorylevel] = '';
  1339.             }
  1340.         }
  1341.         // temprow has the old style low to high level categories.
  1342.         $newlevel = 1;
  1343.         // let's turn them into high to low level categories
  1344.         for( $categorylevel=$max_categories+1; $categorylevel>0; $categorylevel--){
  1345.             if ($temprow['v_categories_name_' . $categorylevel] != ''){
  1346.                 $row['v_categories_name_' . $newlevel++] = $temprow['v_categories_name_' . $categorylevel];
  1347.             }
  1348.         }
  1349.  
  1350.         if ($row['v_manufacturers_id'] != ''){
  1351.             $sql2 = "SELECT manufacturers_name
  1352.                 FROM ".TABLE_MANUFACTURERS."
  1353.                 WHERE
  1354.                 manufacturers_id = " . $row['v_manufacturers_id']
  1355.                 ;
  1356.             $result2 = tep_db_query($sql2);
  1357.             $row2 =  tep_db_fetch_array($result2);
  1358.             $row['v_manufacturers_name'] = $row2['manufacturers_name'];
  1359.         }
  1360.  
  1361.         //elari -
  1362.         //We check the value of tax class and title instead of the id
  1363.         //Then we add the tax to price if $price_with_tax is set to true
  1364.         $row_tax_multiplier = tep_get_tax_class_rate($row['v_tax_class_id']);
  1365.         $row['v_tax_class_title'] = tep_get_tax_class_title($row['v_tax_class_id']);
  1366.         if ($price_with_tax){
  1367.             $row['v_products_price'] = $row['v_products_price'] + round($row['v_products_price']* $row_tax_multiplier / 100,2);
  1368.         }
  1369.  
  1370.         // now create the internal variables that will be used
  1371.         // the $$thisvar is on purpose: it creates a variable named what ever was in $thisvar and sets the value
  1372.         foreach ($default_these as $thisvar){
  1373.             $$thisvar    = $row[$thisvar];
  1374.         }
  1375.  
  1376.         $row =  tep_db_fetch_array($result);
  1377.     }
  1378.  
  1379.     // this is an important loop.  What it does is go thru all the fields in the incoming file and set the internal vars.
  1380.     // Internal vars not set here are either set in the loop above for existing records, or not set at all (null values)
  1381.     // the array values are handled separatly, although they will set variables in this loop, we won't use them.
  1382.     foreach( $filelayout as $key => $value ){
  1383.         $$key = $items[ $value ];
  1384.     }
  1385.  
  1386.         // so how to handle these?  we shouldn't built the array unless it's been giving to us.
  1387.     // The assumption is that if you give us names and descriptions, then you give us name and description for all applicable languages
  1388.     foreach ($langcode as $lang){
  1389.         //echo "Langid is " . $lang['id'] . "<br>";
  1390.         $l_id = $lang['id'];
  1391.         if (isset($filelayout['v_products_name_' . $l_id ])){
  1392.             //we set dynamically the language values
  1393.             $v_products_name[$l_id]     = $items[$filelayout['v_products_name_' . $l_id]];
  1394.             $v_products_description[$l_id]     = $items[$filelayout['v_products_description_' . $l_id ]];
  1395.             $v_products_url[$l_id]         = $items[$filelayout['v_products_url_' . $l_id ]];
  1396.             // support for Linda's Header Controller 2.0 here
  1397.             if(isset($filelayout['v_products_head_title_tag_' . $l_id])){
  1398.                 $v_products_head_title_tag[$l_id]     = $items[$filelayout['v_products_head_title_tag_' . $l_id]];
  1399.                 $v_products_head_desc_tag[$l_id]     = $items[$filelayout['v_products_head_desc_tag_' . $l_id]];
  1400.                 $v_products_head_keywords_tag[$l_id]     = $items[$filelayout['v_products_head_keywords_tag_' . $l_id]];
  1401.             }
  1402.             // end support for Header Controller 2.0
  1403.         }
  1404.     }
  1405.     //elari... we get the tax_clas_id from the tax_title
  1406.     //on screen will still be displayed the tax_class_title instead of the id....
  1407.     if ( isset( $v_tax_class_title) ){
  1408.         $v_tax_class_id          = tep_get_tax_title_class_id($v_tax_class_title);
  1409.     }
  1410.     //we check the tax rate of this tax_class_id
  1411.         $row_tax_multiplier = tep_get_tax_class_rate($v_tax_class_id);
  1412.  
  1413.     //And we recalculate price without the included tax...
  1414.     //Since it seems display is made before, the displayed price will still include tax
  1415.     //This is same problem for the tax_clas_id that display tax_class_title
  1416.     if ($price_with_tax){
  1417.         $v_products_price        = round( $v_products_price / (1 + ( $row_tax_multiplier * $price_with_tax/100) ), 2);
  1418.     }
  1419.  
  1420.     // if they give us one category, they give us all 6 categories
  1421.     unset ($v_categories_name); // default to not set.
  1422.     if ( isset( $filelayout['v_categories_name_1'] ) ){
  1423.         $newlevel = 1;
  1424.         for( $categorylevel=6; $categorylevel>0; $categorylevel--){
  1425.             if ( $items[$filelayout['v_categories_name_' . $categorylevel]] != ''){
  1426.                 $v_categories_name[$newlevel++] = $items[$filelayout['v_categories_name_' . $categorylevel]];
  1427.             }
  1428.         }
  1429.         while( $newlevel < $max_categories+1){
  1430.             $v_categories_name[$newlevel++] = ''; // default the remaining items to nothing
  1431.         }
  1432.     }
  1433.  
  1434.     if (ltrim(rtrim($v_products_quantity)) == '') {
  1435.         $v_products_quantity = 1;
  1436.     }
  1437.     if ($v_date_avail == '') {
  1438.         $v_date_avail = "CURRENT_TIMESTAMP";
  1439.     } else {
  1440.         // we put the quotes around it here because we can't put them into the query, because sometimes
  1441.         //   we will use the "current_timestamp", which can't have quotes around it.
  1442.         $v_date_avail = '"' . $v_date_avail . '"';
  1443.     }
  1444.  
  1445.     if ($v_date_added == '') {
  1446.         $v_date_added = "CURRENT_TIMESTAMP";
  1447.     } else {
  1448.         // we put the quotes around it here because we can't put them into the query, because sometimes
  1449.         //   we will use the "current_timestamp", which can't have quotes around it.
  1450.         $v_date_added = '"' . $v_date_added . '"';
  1451.     }
  1452.  
  1453.  
  1454.     // default the stock if they spec'd it or if it's blank
  1455.     $v_db_status = '1'; // default to active
  1456.     if ($v_status == $inactive){
  1457.         // they told us to deactivate this item
  1458.         $v_db_status = '0';
  1459.     }
  1460.     if ($zero_qty_inactive && $v_products_quantity == 0) {
  1461.         // if they said that zero qty products should be deactivated, let's deactivate if the qty is zero
  1462.         $v_db_status = '0';
  1463.     }
  1464.  
  1465.     if ($v_manufacturer_id==''){
  1466.         $v_manufacturer_id="NULL";
  1467.     }
  1468.  
  1469.     if (trim($v_products_image)==''){
  1470.         $v_products_image = $default_image_product;
  1471.     }
  1472.  
  1473.     if (strlen($v_products_model) > $modelsize ){
  1474.         echo "<font color='red'>" . strlen($v_products_model) . $v_products_model . "... ERROR! - Too many characters in the model number.<br>
  1475.             12 is the maximum on a standard OSC install.<br>
  1476.             Your maximum product_model length is set to $modelsize<br>
  1477.             You can either shorten your model numbers or increase the size of the field in the database.</font>";
  1478.         die();
  1479.     }
  1480.  
  1481.     // OK, we need to convert the manufacturer's name into id's for the database
  1482.     if ( isset($v_manufacturers_name) && $v_manufacturers_name != '' ){
  1483.         $sql = "SELECT man.manufacturers_id
  1484.             FROM ".TABLE_MANUFACTURERS." as man
  1485.             WHERE
  1486.                 man.manufacturers_name = '" . $v_manufacturers_name . "'";
  1487.         $result = tep_db_query($sql);
  1488.         $row =  tep_db_fetch_array($result);
  1489.         if ( $row != '' ){
  1490.             foreach( $row as $item ){
  1491.                 $v_manufacturer_id = $item;
  1492.             }
  1493.         } else {
  1494.             // to add, we need to put stuff in categories and categories_description
  1495.             $sql = "SELECT MAX( manufacturers_id) max FROM ".TABLE_MANUFACTURERS;
  1496.             $result = tep_db_query($sql);
  1497.             $row =  tep_db_fetch_array($result);
  1498.             $max_mfg_id = $row['max']+1;
  1499.             // default the id if there are no manufacturers yet
  1500.             if (!is_numeric($max_mfg_id) ){
  1501.                 $max_mfg_id=1;
  1502.             }
  1503.  
  1504.             // Uncomment this query if you have an older 2.2 codebase
  1505.             /*
  1506.             $sql = "INSERT INTO ".TABLE_MANUFACTURERS."(
  1507.                 manufacturers_id,
  1508.                 manufacturers_name,
  1509.                 manufacturers_image
  1510.                 ) VALUES (
  1511.                 $max_mfg_id,
  1512.                 '$v_manufacturers_name',
  1513.                 '$default_image_manufacturer'
  1514.                 )";
  1515.             */
  1516.  
  1517.             // Comment this query out if you have an older 2.2 codebase
  1518.             $sql = "INSERT INTO ".TABLE_MANUFACTURERS."(
  1519.                 manufacturers_id,
  1520.                 manufacturers_name,
  1521.                 manufacturers_image,
  1522.                 date_added,
  1523.                 last_modified
  1524.                 ) VALUES (
  1525.                 $max_mfg_id,
  1526.                 '$v_manufacturers_name',
  1527.                 '$default_image_manufacturer',
  1528.                 CURRENT_TIMESTAMP,
  1529.                 CURRENT_TIMESTAMP
  1530.                 )";
  1531.             $result = tep_db_query($sql);
  1532.             $v_manufacturer_id = $max_mfg_id;
  1533.         }
  1534.     }
  1535.     // if the categories names are set then try to update them
  1536.     if ( isset($v_categories_name_1)){
  1537.         // start from the highest possible category and work our way down from the parent
  1538.         $v_categories_id = 0;
  1539.         $theparent_id = 0;
  1540.         for ( $categorylevel=$max_categories+1; $categorylevel>0; $categorylevel-- ){
  1541.             $thiscategoryname = $v_categories_name[$categorylevel];
  1542.             if ( $thiscategoryname != ''){
  1543.                 // we found a category name in this field
  1544.  
  1545.                 // now the subcategory
  1546.                 $sql = "SELECT cat.categories_id
  1547.                     FROM ".TABLE_CATEGORIES." as cat, 
  1548.                          ".TABLE_CATEGORIES_DESCRIPTION." as des
  1549.                     WHERE
  1550.                         cat.categories_id = des.categories_id AND
  1551.                         des.language_id = $epdlanguage_id AND
  1552.                         cat.parent_id = " . $theparent_id . " AND
  1553.                         des.categories_name = '" . $thiscategoryname . "'";
  1554.                 $result = tep_db_query($sql);
  1555.                 $row =  tep_db_fetch_array($result);
  1556.                 if ( $row != '' ){
  1557.                     foreach( $row as $item ){
  1558.                         $thiscategoryid = $item;
  1559.                     }
  1560.                 } else {
  1561.                     // to add, we need to put stuff in categories and categories_description
  1562.                     $sql = "SELECT MAX( categories_id) max FROM ".TABLE_CATEGORIES;
  1563.                     $result = tep_db_query($sql);
  1564.                     $row =  tep_db_fetch_array($result);
  1565.                     $max_category_id = $row['max']+1;
  1566.                     if (!is_numeric($max_category_id) ){
  1567.                         $max_category_id=1;
  1568.                     }
  1569.                     $sql = "INSERT INTO ".TABLE_CATEGORIES."(
  1570.                         categories_id,
  1571.                         categories_image,
  1572.                         parent_id,
  1573.                         sort_order,
  1574.                         date_added,
  1575.                         last_modified
  1576.                         ) VALUES (
  1577.                         $max_category_id,
  1578.                         '$default_image_category',
  1579.                         $theparent_id,
  1580.                         0,
  1581.                         CURRENT_TIMESTAMP
  1582.                         ,CURRENT_TIMESTAMP
  1583.                         )";
  1584.                     $result = tep_db_query($sql);
  1585.                     $sql = "INSERT INTO ".TABLE_CATEGORIES_DESCRIPTION."(
  1586.                             categories_id,
  1587.                             language_id,
  1588.                             categories_name
  1589.                         ) VALUES (
  1590.                             $max_category_id,
  1591.                             '$epdlanguage_id',
  1592.                             '$thiscategoryname'
  1593.                         )";
  1594.                     $result = tep_db_query($sql);
  1595.                     $thiscategoryid = $max_category_id;
  1596.                 }
  1597.                 // the current catid is the next level's parent
  1598.                 $theparent_id = $thiscategoryid;
  1599.                 $v_categories_id = $thiscategoryid; // keep setting this, we need the lowest level category ID later
  1600.             }
  1601.         }
  1602.     }
  1603.  
  1604.     if ($v_products_model != "") {
  1605.         //   products_model exists!
  1606.         array_walk($items, 'print_el');
  1607.  
  1608.         // First we check to see if this is a product in the current db.
  1609.         $result = tep_db_query("SELECT products_id FROM ".TABLE_PRODUCTS." WHERE (products_model = '". $v_products_model . "')");
  1610.  
  1611.         if (tep_db_num_rows($result) == 0)  {
  1612.             //   insert into products
  1613.  
  1614.             $sql = "SELECT MAX( products_id) max FROM ".TABLE_PRODUCTS;
  1615.             $result = tep_db_query($sql);
  1616.             $row =  tep_db_fetch_array($result);
  1617.             $max_product_id = $row['max']+1;
  1618.             if (!is_numeric($max_product_id) ){
  1619.                 $max_product_id=1;
  1620.             }
  1621.             $v_products_id = $max_product_id;
  1622.             echo "<font color='green'> !New Product!</font><br>";
  1623.  
  1624.             $query = "INSERT INTO ".TABLE_PRODUCTS." (
  1625.                     products_image,
  1626.                     products_model,
  1627.                     products_price,
  1628.                     products_status,
  1629.                     products_last_modified,
  1630.                     products_date_added,
  1631.                     products_date_available,
  1632.                     products_tax_class_id,
  1633.                     products_weight,
  1634.                     products_quantity,
  1635.                     manufacturers_id)
  1636.                         VALUES (
  1637.                             '$v_products_image',";
  1638.  
  1639.             // unmcomment these lines if you are running the image mods
  1640.             /*
  1641.                 $query .=        . $v_products_mimage . '", "'
  1642.                             . $v_products_bimage . '", "'
  1643.                             . $v_products_subimage1 . '", "'
  1644.                             . $v_products_bsubimage1 . '", "'
  1645.                             . $v_products_subimage2 . '", "'
  1646.                             . $v_products_bsubimage2 . '", "'
  1647.                             . $v_products_subimage3 . '", "'
  1648.                             . $v_products_bsubimage3 . '", "'
  1649.             */
  1650.  
  1651.             $query .="                '$v_products_model',
  1652.                                 '$v_products_price',
  1653.                                 '$v_db_status',
  1654.                                 CURRENT_TIMESTAMP,
  1655.                                 $v_date_added,
  1656.                                 $v_date_avail,
  1657.                                 '$v_tax_class_id',
  1658.                                 '$v_products_weight',
  1659.                                 '$v_products_quantity',
  1660.                                 '$v_manufacturer_id')
  1661.                             ";
  1662.                 $result = tep_db_query($query);
  1663.         } else {
  1664.             // existing product, get the id from the query
  1665.             // and update the product data
  1666.             $row =  tep_db_fetch_array($result);
  1667.             $v_products_id = $row['products_id'];
  1668.             echo "<font color='black'> Updated</font><br>";
  1669.             $row =  tep_db_fetch_array($result);
  1670.             $query = 'UPDATE '.TABLE_PRODUCTS.'
  1671.                     SET
  1672.                     products_price="'.$v_products_price.
  1673.                     '" ,products_image="'.$v_products_image;
  1674.  
  1675.             // uncomment these lines if you are running the image mods
  1676. /*
  1677.                 $query .=
  1678.                     '" ,products_mimage="'.$v_products_mimage.
  1679.                     '" ,products_bimage="'.$v_products_bimage.
  1680.                     '" ,products_subimage1="'.$v_products_subimage1.
  1681.                     '" ,products_bsubimage1="'.$v_products_bsubimage1.
  1682.                     '" ,products_subimage2="'.$v_products_subimage2.
  1683.                     '" ,products_bsubimage2="'.$v_products_bsubimage2.
  1684.                     '" ,products_subimage3="'.$v_products_subimage3.
  1685.                     '" ,products_bsubimage3="'.$v_products_bsubimage3;
  1686. */
  1687.  
  1688.             $query .= '", products_weight="'.$v_products_weight .
  1689.                     '", products_tax_class_id="'.$v_tax_class_id . 
  1690.                     '", products_date_available= ' . $v_date_avail .
  1691.                     ', products_date_added= ' . $v_date_added .
  1692.                     ', products_last_modified=CURRENT_TIMESTAMP
  1693.                     , products_quantity="' . $v_products_quantity .  
  1694.                     '" ,manufacturers_id=' . $v_manufacturer_id . 
  1695.                     ' , products_status=' . $v_db_status . '
  1696.                     WHERE
  1697.                         (products_id = "'. $v_products_id . '")';
  1698.  
  1699.             $result = tep_db_query($query);
  1700.         }
  1701.  
  1702.         // the following is common in both the updating an existing product and creating a new product
  1703.                 if ( isset($v_products_name)){
  1704.             foreach( $v_products_name as $key => $name){
  1705.                             if ($name!=''){
  1706.                     $sql = "SELECT * FROM ".TABLE_PRODUCTS_DESCRIPTION." WHERE
  1707.                             products_id = $v_products_id AND
  1708.                             language_id = " . $key;
  1709.                     $result = tep_db_query($sql);
  1710.                     if (tep_db_num_rows($result) == 0) {
  1711.                         // nope, this is a new product description
  1712.                         $result = tep_db_query($sql);
  1713.                         $sql =
  1714.                             "INSERT INTO ".TABLE_PRODUCTS_DESCRIPTION."
  1715.                                 (products_id,
  1716.                                 language_id,
  1717.                                 products_name,
  1718.                                 products_description,
  1719.                                 products_url)
  1720.                                 VALUES (
  1721.                                     '" . $v_products_id . "',
  1722.                                     " . $key . ",
  1723.                                     '" . $name . "',
  1724.                                     '". $v_products_description[$key] . "',
  1725.                                     '". $v_products_url[$key] . "'
  1726.                                     )";
  1727.                         // support for Linda's Header Controller 2.0
  1728.                         if (isset($v_products_head_title_tag)){
  1729.                             // override the sql if we're using Linda's contrib
  1730.                             $sql =
  1731.                                 "INSERT INTO ".TABLE_PRODUCTS_DESCRIPTION."
  1732.                                     (products_id,
  1733.                                     language_id,
  1734.                                     products_name,
  1735.                                     products_description,
  1736.                                     products_url,
  1737.                                     products_head_title_tag,
  1738.                                     products_head_desc_tag,
  1739.                                     products_head_keywords_tag)
  1740.                                     VALUES (
  1741.                                         '" . $v_products_id . "',
  1742.                                         " . $key . ",
  1743.                                         '" . $name . "',
  1744.                                         '". $v_products_description[$key] . "',
  1745.                                         '". $v_products_url[$key] . "',
  1746.                                         '". $v_products_head_title_tag[$key] . "',
  1747.                                         '". $v_products_head_desc_tag[$key] . "',
  1748.                                         '". $v_products_head_keywords_tag[$key] . "')";
  1749.                         }
  1750.                         // end support for Linda's Header Controller 2.0
  1751.                         $result = tep_db_query($sql);
  1752.                     } else {
  1753.                         // already in the description, let's just update it
  1754.                         $sql =
  1755.                             "UPDATE ".TABLE_PRODUCTS_DESCRIPTION." SET
  1756.                                 products_name='$name',
  1757.                                 products_description='".$v_products_description[$key] . "',
  1758.                                 products_url='" . $v_products_url[$key] . "'
  1759.                             WHERE
  1760.                                 products_id = '$v_products_id' AND
  1761.                                 language_id = '$key'";
  1762.                         // support for Lindas Header Controller 2.0
  1763.                         if (isset($v_products_head_title_tag)){
  1764.                             // override the sql if we're using Linda's contrib
  1765.                             $sql =
  1766.                                 "UPDATE ".TABLE_PRODUCTS_DESCRIPTION." SET
  1767.                                     products_name = '$name',
  1768.                                     products_description = '".$v_products_description[$key] . "',
  1769.                                     products_url = '" . $v_products_url[$key] ."',
  1770.                                     products_head_title_tag = '" . $v_products_head_title_tag[$key] ."',
  1771.                                     products_head_desc_tag = '" . $v_products_head_desc_tag[$key] ."',
  1772.                                     products_head_keywords_tag = '" . $v_products_head_keywords_tag[$key] ."'
  1773.                                 WHERE
  1774.                                     products_id = '$v_products_id' AND
  1775.                                     language_id = '$key'";
  1776.                         }
  1777.                         // end support for Linda's Header Controller 2.0
  1778.                         $result = tep_db_query($sql);
  1779.                     }
  1780.                 }
  1781.             }
  1782.         }
  1783.         if (isset($v_categories_id)){
  1784.             //find out if this product is listed in the category given
  1785.             $result_incategory = tep_db_query('SELECT
  1786.                         '.TABLE_PRODUCTS_TO_CATEGORIES.'.products_id,
  1787.                         '.TABLE_PRODUCTS_TO_CATEGORIES.'.categories_id
  1788.                         FROM
  1789.                             '.TABLE_PRODUCTS_TO_CATEGORIES.'
  1790.                         WHERE
  1791.                         '.TABLE_PRODUCTS_TO_CATEGORIES.'.products_id='.$v_products_id.' AND
  1792.                         '.TABLE_PRODUCTS_TO_CATEGORIES.'.categories_id='.$v_categories_id);
  1793.  
  1794.             if (tep_db_num_rows($result_incategory) == 0) {
  1795.                 // nope, this is a new category for this product
  1796.                 $res1 = tep_db_query('INSERT INTO '.TABLE_PRODUCTS_TO_CATEGORIES.' (products_id, categories_id)
  1797.                             VALUES ("' . $v_products_id . '", "' . $v_categories_id . '")');
  1798.             } else {
  1799.                 // already in this category, nothing to do!
  1800.             }
  1801.         }
  1802.         // for the separate prices per customer module
  1803.         $ll=1;
  1804.  
  1805.         if (isset($v_customer_price_1)){
  1806.             
  1807.             if (($v_customer_group_id_1 == '') AND ($v_customer_price_1 != ''))  {
  1808.                 echo "<font color=red>ERROR - v_customer_group_id and v_customer_price must occur in pairs</font>";
  1809.                 die();
  1810.             }
  1811.             // they spec'd some prices, so clear all existing entries
  1812.             $result = tep_db_query('
  1813.                         DELETE
  1814.                         FROM
  1815.                             '.TABLE_PRODUCTS_GROUPS.'
  1816.                         WHERE
  1817.                             products_id = ' . $v_products_id
  1818.                         );
  1819.             // and insert the new record
  1820.             if ($v_customer_price_1 != ''){
  1821.                 $result = tep_db_query('
  1822.                             INSERT INTO
  1823.                                 '.TABLE_PRODUCTS_GROUPS.'
  1824.                             VALUES
  1825.                             (
  1826.                                 ' . $v_customer_group_id_1 . ',
  1827.                                 ' . $v_customer_price_1 . ',
  1828.                                 ' . $v_products_id . ',
  1829.                                 ' . $v_products_price .'
  1830.                                 )'
  1831.                             );
  1832.             }
  1833.             if ($v_customer_price_2 != ''){
  1834.                 $result = tep_db_query('
  1835.                             INSERT INTO
  1836.                                 '.TABLE_PRODUCTS_GROUPS.'
  1837.                             VALUES
  1838.                             (
  1839.                                 ' . $v_customer_group_id_2 . ',
  1840.                                 ' . $v_customer_price_2 . ',
  1841.                                 ' . $v_products_id . ',
  1842.                                 ' . $v_products_price . '
  1843.                                 )'
  1844.                             );
  1845.             }
  1846.             if ($v_customer_price_3 != ''){
  1847.                 $result = tep_db_query('
  1848.                             INSERT INTO
  1849.                                 '.TABLE_PRODUCTS_GROUPS.'
  1850.                             VALUES
  1851.                             (
  1852.                                 ' . $v_customer_group_id_3 . ',
  1853.                                 ' . $v_customer_price_3 . ',
  1854.                                 ' . $v_products_id . ',
  1855.                                 ' . $v_products_price . '
  1856.                                 )'
  1857.                             );
  1858.             }
  1859.             if ($v_customer_price_4 != ''){
  1860.                 $result = tep_db_query('
  1861.                             INSERT INTO
  1862.                                 '.TABLE_PRODUCTS_GROUPS.'
  1863.                             VALUES
  1864.                             (
  1865.                                 ' . $v_customer_group_id_4 . ',
  1866.                                 ' . $v_customer_price_4 . ',
  1867.                                 ' . $v_products_id . ',
  1868.                                 ' . $v_products_price . '
  1869.                                 )'
  1870.                             );
  1871.             }
  1872.  
  1873.         }
  1874.  
  1875.     } else {
  1876.         // this record was missing the product_model
  1877.         array_walk($items, 'print_el');
  1878.         echo "<p class=smallText>No products_model field in record. This line was not imported <br>";
  1879.         echo "<br>";
  1880.     }
  1881. // end of row insertion code
  1882. }
  1883.  
  1884.  
  1885. require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>
  1886.