PageRenderTime 47ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/phpshop1/WEB-INF/modules/product/lib/ps_csv.inc

http://phpshop.googlecode.com/
PHP | 328 lines | 211 code | 38 blank | 79 comment | 23 complexity | 85da7afad7a508ffc753dcd9f507b59c MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /*
  3. * The ps_csv class
  4. *
  5. * By John Syben
  6. * webme.co.nz
  7. *
  8. * This class allows for the adding of multiple
  9. * products and categories from a csv file
  10. *************************************************************************/
  11. class ps_csv {
  12. var $classname = "ps_csv";
  13. /**************************************************************************
  14. ** name: upload_csv()
  15. ** created by: John Syben
  16. ** modified by: nhyde
  17. ** A db table named 'csv' must exist with the product fields
  18. ** allocated their relative positions in the csv line
  19. ***************************************************************************/
  20. function upload_csv(&$d) {
  21. // handle the upload here
  22. /* fix by heiko 05.04.2005 */
  23. global $_FILES;
  24. $d["file_name"] = $_FILES["file"]["name"];
  25. $d["file_type"] = $_FILES["file"]["type"];
  26. $d["file_tmp_name"] = $_FILES["file"]["tmp_name"];
  27. $d["file"] =$_FILES["file"]["tmp_name"];
  28. $d["file_size"] =$_FILES["file"]["size"];
  29. /* end fix by heiko 05.04.2005 */
  30. // handle the upload here
  31. if (False == $this->handle_csv_upload($d) ) {
  32. return False;
  33. }
  34. // Get row positions of each element as set in csv table
  35. $db = new ps_DB;
  36. $q = "SELECT * FROM csv ";
  37. $db->query($q);
  38. $db->next_record();
  39. // Open csv file
  40. $file = $d["file"];
  41. $fp = fopen ($file,"r");
  42. $error="";
  43. $line=1;
  44. // Run through each line of file
  45. while ($data = fgetcsv ($fp, 1000, ",")) {
  46. // Check for SKU - required
  47. if (!$data[$db->f("csv_product_sku")-1])
  48. $this_error .= "No SKU, ";
  49. else
  50. $product_sku = $data[$db->f("csv_product_sku")-1];
  51. // Check for Product Name - required
  52. if (!$data[$db->f("csv_product_name")-1])
  53. $this_error .= "No Product Name, ";
  54. else
  55. $product_name = $data[$db->f("csv_product_name")-1];
  56. // Check for Price - required
  57. if (!$data[$db->f("csv_product_price")-1])
  58. $this_error .= "No Price, ";
  59. else
  60. $product_price = $data[$db->f("csv_product_price")-1];
  61. // Check for Category Path - required
  62. if (!$data[$db->f("csv_category_path")-1])
  63. $this_error .= "No Category Path, ";
  64. else
  65. $category_path = $data[$db->f("csv_category_path")-1];
  66. // If a required field was missing, add to error to main message and start next line
  67. // Otherwise add or update product
  68. if ($this_error) {
  69. $d["message"] .= "Line $line $this_error<br />";
  70. $this_error = "";
  71. }
  72. else {
  73. $timestamp = time();
  74. // See if sku exists. If so, update product - otherwise add product
  75. $dbp = new ps_DB;
  76. $q = "SELECT * FROM product ";
  77. $q .= "WHERE product_sku='$product_sku'";
  78. $dbp->query($q);
  79. if ($dbp->next_record()) { // SKU exists - update product
  80. // Update product information
  81. $q = "UPDATE product SET ";
  82. $q .= "product_s_desc='" . $data[$db->f("csv_product_s_desc")-1] . "',";
  83. $q .= "product_desc='" . $data[$db->f("csv_product_desc")-1] . "',";
  84. $q .= "product_thumb_image='" . $data[$db->f("csv_product_thumb_image")-1] . "',";
  85. $q .= "product_full_image='" . $data[$db->f("csv_product_full_image")-1] . "',";
  86. $q .= "product_weight='" . $data[$db->f("csv_product_weight")-1] . "',";
  87. $q .= "product_weight_uom='" . $data[$db->f("csv_product_weight_uom")-1] . "',";
  88. $q .= "product_length='" . $data[$db->f("csv_product_length")-1] . "',";
  89. $q .= "product_width='" . $data[$db->f("csv_product_width")-1] . "',";
  90. $q .= "product_height='" . $data[$db->f("csv_product_height")-1] . "',";
  91. $q .= "product_lwh_uom='" . $data[$db->f("csv_product_lwh_uom")-1] . "',";
  92. $q .= "product_in_stock='" . $data[$db->f("csv_product_in_stock")-1] . "',";
  93. $q .= "product_available_date='" . $data[$db->f("csv_product_available_date")-1] . "',";
  94. $q .= "product_special='" . $data[$db->f("csv_product_special")-1] . "',";
  95. $q .= "product_discount_id='" . $data[$db->f("csv_product_discount_id")-1] . "',";
  96. $q .= "product_name='" . $product_name . "', ";
  97. $q .= "mdate='" . $timestamp . "' ";
  98. $q .= "WHERE product_sku='" . $product_sku . "'";
  99. $dbu = new ps_DB;
  100. $dbu->query($q);
  101. // Get default shopper group ID
  102. $q = "SELECT shopper_group_id FROM shopper_group ";
  103. $q .= "WHERE shopper_group_name='-default-'";
  104. $dbsg = new ps_DB;
  105. $dbsg->query($q);
  106. $dbsg->next_record();
  107. // Update product price for default shopper group
  108. $q = "UPDATE product_price SET ";
  109. $q .= "product_price='" . $product_price . "',";
  110. $q .= "shopper_group_id='" . $dbsg->f("shopper_group_id") . "', ";
  111. $q .= "mdate='" . $timestamp . "' ";
  112. $q .= "WHERE product_id='" . $dbp->f("product_id") . "'";
  113. $dbpp = new ps_DB;
  114. $dbpp->query($q);
  115. // Add report for this line to message
  116. $d["message"] .= "Line: $line Updated Product SKU: $product_sku<br />";
  117. }
  118. else { // SKU does not exist - add new product
  119. // Add product information
  120. $q = "INSERT INTO product (vendor_id,product_parent_id,product_sku,";
  121. $q .= "product_name,product_desc,product_s_desc,";
  122. $q .= "product_thumb_image,product_full_image,";
  123. $q .= "product_publish,product_weight,product_weight_uom,";
  124. $q .= "product_length,product_width,product_height,product_lwh_uom,";
  125. $q .= "product_in_stock,";
  126. $q .= "product_available_date,product_special,product_discount_id,";
  127. $q .= "cdate,mdate) ";
  128. $q .= "VALUES ('1','0','";
  129. $q .= $data[$db->f("csv_product_sku")-1] . "','" . $data[$db->f("csv_product_name")-1] . "','";
  130. $q .= $data[$db->f("csv_product_desc")-1] . "','" . $data[$db->f("csv_product_s_desc")-1] . "','";
  131. $q .= $data[$db->f("csv_product_thumb_image")-1] . "','";
  132. $q .= $data[$db->f("csv_product_full_image")-1] . "','Y','";
  133. $q .= $data[$db->f("csv_product_weight")-1] . "','" . $data[$db->f("csv_product_weight_uom")-1] . "','";
  134. $q .= $data[$db->f("csv_product_length")-1] . "','" . $data[$db->f("csv_product_width")-1] . "','";
  135. $q .= $data[$db->f("csv_product_height")-1] . "','" . $data[$db->f("csv_product_lwh_uom")-1] . "','";
  136. $q .= $data[$db->f("csv_product_in_stock")-1] . "','";
  137. $q .= $data[$db->f("csv_product_available_date")-1] . "','";
  138. $q .= $data[$db->f("csv_product_special")-1] . "','";
  139. $q .= $data[$db->f("csv_product_discount_id")-1] . "','$timestamp','$timestamp')";
  140. $dbu = new ps_DB;
  141. $dbu->query($q);
  142. // Get the product ID we just created
  143. $q = "SELECT product_id FROM product ";
  144. $q .= "WHERE product_sku = '" . $product_sku . "' ";
  145. $q .= "AND vendor_id = '1' ";
  146. $q .= "AND cdate = $timestamp";
  147. $dbpi = new ps_DB;
  148. $dbpi->query($q);
  149. $dbpi->next_record();
  150. $product_id = $dbpi->f("product_id");
  151. // Use csv_category() method to confirm/add category tree for this product
  152. $category_id = $this->csv_category($data[$db->f("csv_category_path")-1]);
  153. // Create product/category xref
  154. $q = "INSERT INTO product_category_xref ";
  155. $q .= "(category_id,product_id) ";
  156. $q .= "VALUES ('" . $category_id . "','";
  157. $q .= $product_id . "')";
  158. $dbcat = new ps_DB;
  159. $dbcat->query($q);
  160. // Get default shopper group ID
  161. $q = "SELECT shopper_group_id FROM shopper_group ";
  162. $q .= "WHERE shopper_group_name='-default-'";
  163. $dbsg = new ps_DB;
  164. $dbsg->query($q);
  165. $dbsg->next_record();
  166. // Add product price for default shopper group
  167. $q = "INSERT INTO product_price ";
  168. $q .= "(product_price,product_id,shopper_group_id,mdate) ";
  169. $q .= "VALUES ('";
  170. $q .= $product_price . "','";
  171. $q .= $product_id . "','";
  172. $q .= $dbsg->f("shopper_group_id") . "','";
  173. $q .= $timestamp . "') ";
  174. $dbpp = new ps_DB;
  175. $dbpp->query($q);
  176. // Add report for this line to message
  177. $d["message"] .= "Line: $line Added Product SKU: $product_sku<br />";
  178. }
  179. }
  180. $line++;
  181. } // End while
  182. fclose ($fp);
  183. return True;
  184. } //End function upload_csv
  185. /**************************************************************************
  186. ** name: csv_category()
  187. ** created by: John Syben
  188. ** Creates categories from slash delimited line
  189. ***************************************************************************/
  190. function csv_category($line) {
  191. // Explode slash delimited category tree into array
  192. $category_list = explode("/", $line);
  193. $category_count = count($category_list);
  194. $db = new ps_DB;
  195. $category_parent_id = '0';
  196. // For each category in array
  197. for($i = 0; $i < $category_count; $i++) {
  198. // See if this category exists with it's parent in xref
  199. $q = "SELECT category.category_id FROM category,category_xref ";
  200. $q .= "WHERE category.category_name='" . $category_list[$i] . "' ";
  201. $q .= "AND category_xref.category_child_id=category.category_id ";
  202. $q .= "AND category_xref.category_parent_id='$category_parent_id'";
  203. $db->query($q);
  204. // If it does not exist, create it
  205. if ($db->next_record()) { // Category exists
  206. $category_id = $db->f("category_id");
  207. }
  208. else { // Category does not exist - create it
  209. $hash_secret="PHPShopIsCool";
  210. $category_id = md5(uniqid($hash_secret));
  211. $timestamp = time();
  212. $cat_url = strtolower($category_list[$i]);
  213. $cat_url = str_replace(" ", "",$cat_url);
  214. // Add category
  215. $q = "INSERT INTO category ";
  216. $q .= "(category_id,vendor_id,category_name,category_url,category_publish,cdate,mdate) ";
  217. $q .= "VALUES ('";
  218. $q .= $category_id . "','";
  219. $q .= "1', '";
  220. $q .= $category_list[$i] . "', '";
  221. $q .= "$cat_url', '";
  222. $q .= "Y', '";
  223. $q .= $timestamp . "', '";
  224. $q .= $timestamp . "')";
  225. $db->query($q);
  226. // Create xref with parent
  227. $q = "INSERT INTO category_xref ";
  228. $q .= "(category_parent_id, category_child_id) ";
  229. $q .= "VALUES ('";
  230. $q .= $category_parent_id . "', '";
  231. $q .= $category_id . "')";
  232. $db->query($q);
  233. }
  234. // Set this category as parent of next in line
  235. $category_parent_id = $category_id;
  236. } // end for
  237. // Return the last category_id which is where the product goes
  238. return $category_id;
  239. } // End function csv_category
  240. /**
  241. * Handle the upload of file "file".
  242. *
  243. * Longer, multi-line description here.
  244. *
  245. * @name handle_csv_upload
  246. * @author Nathan Hyde <nhyde@bigdrift.com>
  247. * @param array d posted items crammed into 1 arr
  248. * @returns boolean True of False
  249. */
  250. function handle_csv_upload(&$d) {
  251. $allowed_suffixes_arr = array(
  252. 0=> 'csv'
  253. ,1 => 'txt'
  254. // add more here if needed
  255. );
  256. $allowed_mime_types_arr = array(
  257. 0=> 'text/html'
  258. ,1 => 'text/plain'
  259. ,2 => 'application/octet-stream'
  260. // add more here if needed
  261. );
  262. $error = "";
  263. $strs = explode(".",$d["file_name"]);
  264. $count= count($strs);
  265. $extension = $strs[$count-1];
  266. if (False == in_array($extension, $allowed_suffixes_arr) ) {
  267. $d["error"] = "Suffix not allowed. Valid suffixes are: " . join(", ",$allowed_suffixes_arr);
  268. return False;
  269. }
  270. // test the mime type here
  271. if (False == in_array($d["file_type"], $allowed_mime_types_arr) ) {
  272. $d["error"] = "Mime type not accepted. Type for file uploaded: ".$d["file_type"];
  273. return False;
  274. }
  275. // do the moovin here :)
  276. // not necessary for us to do cause it's a temporary file, right?
  277. /**
  278. if (is_uploaded_file($d['file_tmp_name']) ) {
  279. copy($d['file_tmp_name'], "/place/to/put/uploaded/file");
  280. } else {
  281. echo "Possible file upload attack. Filename: " . $_FILES['userfile']['name'];
  282. }
  283. // ...or...
  284. move_uploaded_file($d["file_tmp_name"], "/place/to/put/uploaded/file");
  285. **/
  286. return True;
  287. }
  288. }
  289. ?>