/[drupal]/contributions/modules/ubercart/uc_importer/uc_importer_php4.module
ViewVC logotype

Contents of /contributions/modules/ubercart/uc_importer/uc_importer_php4.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.5 - (show annotations) (download) (as text)
Mon Nov 5 20:31:58 2007 UTC (2 years ago) by rszrama
Branch: MAIN
CVS Tags: DRUPAL-5--1-0-BETA-4, DRUPAL-5--1-0-BETA-5, DRUPAL-5--1-0-BETA-6, DRUPAL-5--1-0-BETA-7, DRUPAL-5--1-0-BETA-2, DRUPAL-5--1-0-BETA-3, DRUPAL-5--1-0-BETA2, DRUPAL-5--1-0-BETA1, DRUPAL-5--1-0-ALPHA-8, DRUPAL-5--1-0-RC-1, DRUPAL-5--1-0-RC-2, DRUPAL-5--1-1, DRUPAL-5--1-0-RC-3, DRUPAL-5--1-2, HEAD
Branch point for: DRUPAL-5, DRUPAL-6--2
Changes since 1.4: +1 -1 lines
File MIME type: text/x-php
Bulk update for Alpha 8.
1 <?php
2 // $Id: uc_importer_php4.module,v 1.4 2007/10/02 18:11:57 rszrama Exp $
3
4 /**
5 * @file
6 * Converts xml into product listings and vice versa.
7 */
8
9 /**
10 * Do not save objects if the database already contains their names.
11 */
12 define('UC_IMPORTER_DO_NOTHING', 0);
13
14 /**
15 * Replace objects if the database already contains their names.
16 */
17 define('UC_IMPORTER_REPLACE', 1);
18
19 /**
20 * Append '_#' to the names of objects already in the database.
21 */
22 define('UC_IMPORTER_INCREMENT', 2);
23
24 /******************************************************************************
25 * Drupal Hooks *
26 ******************************************************************************/
27
28 /**
29 * Implementation of hook_perm().
30 */
31 function uc_importer_perm(){
32 return array('import', 'export');
33 }
34
35 /**
36 * Implementation of hook_menu().
37 */
38 function uc_importer_menu($may_cache){
39 $items = array();
40
41 if ($may_cache){
42 $items[] = array('path' => 'admin/store/products/export',
43 'access' => user_access('export'),
44 'title' => t('Export'),
45 'callback' => 'uc_importer_export_page',
46 'type' => MENU_NORMAL_ITEM,
47 );
48 $items[] = array('path' => 'admin/store/products/import',
49 'access' => user_access('import'),
50 'title' => t('Import'),
51 'callback' => 'uc_importer_import_page',
52 'type' => MENU_NORMAL_ITEM,
53 );
54 $items[] = array('path' => 'admin/store/settings/importer',
55 'access' => user_access('import'),
56 'title' => t('Importer settings'),
57 'callback' => 'drupal_get_form',
58 'callback arguments' => array('uc_importer_admin_settings'),
59 'type' => MENU_NORMAL_ITEM,
60 );
61 }
62
63 return $items;
64 }
65
66 /******************************************************************************
67 * Menu Callbacks *
68 ******************************************************************************/
69
70 function uc_importer_admin_settings(){
71 $form = array();
72
73 $form['uc_importer_handle_duplicates'] = array('#type' => 'radios',
74 '#title' => t('How should similarly named items be handled during import?'),
75 '#options' => array(
76 UC_IMPORTER_DO_NOTHING => t('Do not save the new item.'),
77 UC_IMPORTER_REPLACE => t('Overwrite the existing item.'),
78 UC_IMPORTER_INCREMENT => t('Save the new item as a separate entity,'),
79 ),
80 '#default_value' => variable_get('uc_importer_handle_duplicates', UC_IMPORTER_DO_NOTHING),
81 );
82 $account = user_load(array('uid' => variable_get('uc_importer_user', 0)));
83 $form['uc_importer_user'] = array('#type' => 'textfield',
84 '#title' => t('Authored by'),
85 '#maxlength' => 60,
86 '#autocomplete_path' => 'user/autocomplete',
87 '#default_value' => $account ? $account->name : '',
88 '#description' => t('The "author" of imported products. Leave blank for %anonymous.', array('%anonymous' => variable_get('anonymous', t('Anonymous'))))
89 );
90
91 $form['buttons']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration') );
92 $form['buttons']['reset'] = array('#type' => 'submit', '#value' => t('Reset to defaults') );
93
94 if (!empty($_POST) && form_get_errors()) {
95 drupal_set_message(t('The settings have not been saved because of the errors.'), 'error');
96 }
97
98 return $form;
99 }
100
101 function uc_importer_admin_settings_validate($form_id, $form_values){
102 if (!empty($form_values['uc_importer_user']) && !($account = user_load(array('name' => $form_values['uc_importer_user'])))) {
103 // The use of empty() is mandatory in the context of usernames
104 // as the empty string denotes the anonymous user. In case we
105 // are dealing with an anonymous user we set the user ID to 0.
106 form_set_error('uc_importer_user', t('The username %name does not exist.', array('%name' => $form_values['uc_importer_user'])));
107 }
108 }
109
110 function uc_importer_admin_settings_submit($form_id, $form_values){
111 if ($op == t('Reset to defaults')) {
112 variable_del('uc_importer_handle_duplicates');
113 variable_del('uc_importer_user');
114 drupal_set_message(t('The configuration options have been reset to their default values.'));
115 }
116 else{
117 if ($account = user_load(array('name' => $form_values['uc_importer_user']))) {
118 variable_set('uc_importer_user', $account->uid);
119 }
120 else {
121 variable_set('uc_importer_user', 0);
122 }
123 variable_set('uc_importer_handle_duplicates', $form_values['uc_importer_handle_duplicates']);
124 drupal_set_message(t('The configuration options have been saved.'));
125 }
126 }
127
128 /**
129 * Wrapper function to generate a page to hold the export form.
130 */
131 function uc_importer_export_page(){
132 drupal_add_js(drupal_get_path('module', 'uc_product') .'/uc_product.js', 'module');
133 $output = '';
134 $nids = array();
135 $args = func_get_args();
136 foreach ($args as $nid){
137 if (is_numeric($nid)){
138 $nids[] = (int)$nid;
139 }
140 }
141 $settings = array(
142 'div' => '#products-selector',
143 'class' => 'product-ubrowser',
144 'vid' => variable_get('uc_catalog_vid', 0),
145 'filter' => implode(',', array_keys(uc_product_node_info())),
146 'search' => 'true',
147 'nids' => 'true',
148 'nodesg' => 'product',
149 'nodepl' => 'products',
150 'multi' => 'true',
151 'select' => 'buffer_products("'. base_path() .'","'. file_create_url('') .'")',
152 );
153
154 if (module_exists('uc_catalog')) {
155 $output .= ubrowser($settings, 'products-selector');
156 $output .= drupal_get_form('uc_importer_export_buffer_form', $nids);
157 }
158 else{
159 $output .= drupal_get_form('uc_importer_export_form');
160 }
161
162 return $output;
163 }
164
165 /**
166 * Wrapper function to generate a page to hold the import form.
167 */
168 function uc_importer_import_page(){
169 return drupal_get_form('uc_importer_import_form');
170 }
171
172 /******************************************************************************
173 * Module Functions *
174 ******************************************************************************/
175
176 function uc_importer_export_form(){
177 $form = array();
178
179 $products = array();
180 $result = db_query(db_rewrite_sql("SELECT nid, model FROM {uc_products}"));
181 while ($product = db_fetch_object($result)){
182 $products[$product->nid] = $product->model;
183 }
184 $form['nids'] = array('#type' => 'select',
185 '#multiple' => true,
186 '#title' => t('Products'),
187 '#options' => $products,
188 '#description' => t('Hold "Ctrl" to select multiple items.'),
189 );
190 $form['submit'] = array('#type' => 'submit', '#value' => t('Export'));
191
192 return $form;
193 }
194
195 function uc_importer_export_form_submit($form_id, $form_values){
196 if (count($form_values['nids'])){
197 $xml = uc_importer_export((array)$form_values['nids']);
198 if ($file = file_save_data($xml, file_directory_temp() .'/uc_export.xml', FILE_EXISTS_REPLACE)){
199 //drupal_set_message(print_r($file, true));
200 file_transfer($file, array(
201 'Content-Type: application/xml',
202 'Content-Length: '. filesize($file),
203 'Content-Disposition: attachment; filename="'. $file .'"',
204 ));
205 }
206 }
207 }
208
209 /**
210 * Form to collect the id numbers of all the store components to be exported.
211 */
212 function uc_importer_export_buffer_form($nids){
213 $form = array();
214
215 $buffer = '';
216 foreach ($nids as $nid){
217 $node = node_load($nid);
218 $buffer .= theme('imagecache', 'thumbnail', $node->field_image_cache['filepath']);
219 }
220 $form['#attributes'] = array('class' => 'product-buffer');
221 $form['thumbnails'] = array('#type' => 'markup',
222 '#value' => '<div id="buffer-images"></div>',
223 );
224 $form['products'] = array('#type' => 'hidden',
225 );
226 $form['reset'] = array('#type' => 'submit',
227 '#value' => t('Reset'),
228 );
229 $form['submit'] = array('#type' => 'submit',
230 '#value' => t('Export'),
231 );
232
233 return $form;
234 }
235
236 function uc_importer_export_buffer_form_submit($form_id, $form_values){
237 $item = menu_get_item(menu_get_active_item());
238 //drupal_set_message(print_r($form_values, true));
239 if ($form_values['op'] == t('Reset')){
240 return $item['path'];
241 }
242 else{
243 $products = array_filter(explode('/', $form_values['products']));
244 //drupal_set_message('<pre>'. print_r($products, true) .'</pre>');
245 $xml = uc_importer_export($products);
246 if ($file = file_save_data($xml, file_directory_temp() .'/uc_export.xml', FILE_EXISTS_REPLACE)){
247 //drupal_set_message(print_r($file, true));
248 file_transfer($file, array(
249 'Content-Type: application/xml',
250 'Content-Length: '. filesize($file),
251 'Content-Disposition: attachment; filename="'. $file .'"',
252 ));
253 }
254 }
255 }
256
257 /**
258 * Upload form for an XML file to be imported.
259 */
260 function uc_importer_import_form(){
261 $form = array();
262 $form['#attributes'] = array('enctype' => "multipart/form-data");
263
264 $form['file'] = array('#type' => 'file',
265 '#title' => t('Import XML File'),
266 );
267 $form['directory'] = array('#type' => 'textfield',
268 '#title' => t('Directory containing XML files'),
269 );
270 $form['submit'] = array('#type' => 'submit',
271 '#value' => t('Import'),
272 );
273
274 return $form;
275 }
276
277 /**
278 * Submit function for uc_importer_import_form().
279 */
280 function uc_importer_import_form_submit($form_id, $form_values){
281 $file = file_check_upload('file');
282 if ($file){
283 $file = file_save_upload($file);
284 drupal_set_message(t('File uploaded successfully.'));
285 //drupal_set_message('<pre>'. print_r($file, true) .'</pre>');
286 if ($xml = file_get_contents($file->filepath)){
287 uc_importer_import($xml);
288 }
289 }
290 else if ($form_values['directory']){
291 file_scan_directory(file_directory_path() .'/'. $form_values['directory'], '.*\.xml$', array('.', '..', 'CVS'), 'uc_import_directory_parse');
292 }else{
293 drupal_set_message(t('Error: File failed to upload.'), 'error');
294 }
295 }
296
297 /**
298 * Constructs the XML representation of the store from the ids given.
299 *
300 * @param $data
301 * Array containing the following keys:
302 * categories:
303 * List of term ids in the product catalog.
304 * attributes
305 * List of attribute ids.
306 * classes
307 * List of class ids.
308 * products
309 * List of node ids representing products.
310 * @return
311 * Path to XML file created from parameters.
312 */
313 function uc_importer_export($nids){
314 $data = array(
315 'categories' => array(),
316 'manufacturers' => array(),
317 'categories' => array(),
318 'attributes' => array(),
319 'classes' => array(),
320 'products' => array(),
321 );
322 foreach ($nids as $nid){
323 $data['products'][] = $nid;
324 $node = node_load($nid);
325 if (uc_product_class_load($node->type)){
326 $data['classes'][] = $node->type;
327 }
328 foreach ($node->taxonomy as $tid => $term){
329 if ($term->vid == variable_get('uc_catalog_vid', 0) && !in_array($term->tid, $data['categories'])){
330 foreach (taxonomy_get_parents_all($term->tid) as $parent){
331 // First $parent is $term, so no special case needed
332 $data['categories'][] = $parent->tid;
333 }
334 }
335 elseif ($term->vid == variable_get('uc_manufacturer_vid', 0)){
336 $data['manufacturers'][] = $tid;
337 }
338 }
339 if (module_exists('uc_attribute')){
340 $data['attributes'] += array_keys(uc_product_get_attributes($nid));
341 }
342 }
343 foreach ($data as $type => $ids){
344 $data[$type] = array_unique($ids);
345 }
346 //drupal_set_message('<pre>'. print_r($data, true) .'</pre>');
347 $xml = '<?xml version="1.0"?>'. "\n";
348 $xml .= '<store xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.ubercart.org http://www.ubercart.org/files/store.xsd">';
349 if (is_array($data['categories']) && !empty($data['categories'])){
350 $xml .= _uc_importer_export_categories($data['categories']);
351 }
352 if (is_array($data['manufacturers']) && !empty($data['manufacturers'])){
353 $xml .= _uc_importer_export_manufacturers($data['manufacturers']);
354 }
355 if (is_array($data['attributes']) && !empty($data['attributes'])){
356 $xml .= _uc_importer_export_attributes($data['attributes']);
357 }
358 if (is_array($data['classes']) && !empty($data['classes'])){
359 $xml .= _uc_importer_export_classes($data['classes']);
360 }
361 if (is_array($data['products']) && !empty($data['products'])){
362 $xml .= _uc_importer_export_products($data['products']);
363 }
364 if (is_array($data['orders']) && !empty($data['orders'])){
365 $xml .= _uc_importer_export_orders($data['orders']);
366 }
367 $xml .= '</store>';
368
369 //drupal_set_message(htmlspecialchars($xml));
370 return $xml;
371 }
372
373 /**
374 * Export categories as XML.
375 */
376 function _uc_importer_export_categories($categories){
377 $xml .= '<categories>';
378 foreach ($categories as $tid){
379 $xml .= '<category>';
380 $term = taxonomy_get_term($tid);
381 $parents = taxonomy_get_parents($tid);
382 $xml .= '<id>'. $term->tid .'</id>';
383 $xml .= '<name>'. htmlentities($term->name) .'</name>';
384 $xml .= '<description>'. htmlentities($term->description) .'</description>';
385 foreach ($parents as $parent){
386 $xml .= '<parent>'. $parent->tid .'</parent>';
387 }
388 $xml .= '</category>';
389 }
390 $xml .= '</categories>';
391 return $xml;
392 }
393
394 /**
395 * Export manufacturers as XML.
396 */
397 function _uc_importer_export_manufacturers($manufacturers){
398 $xml = '<manufacturers>';
399 foreach ($manufacturers as $tid){
400 $manufacturer = uc_manufacturer_load($tid);
401 $xml .= '<manufacturer>';
402 $xml .= '<id>'. $manufacturer->tid .'</id>';
403 $xml .= '<name>'. htmlentities($manufacturer->name) .'</name>';
404 $xml .= isset($manufacturer->url) && !empty($manufacturer->url) ? ('<url>'. $manufacturer->url .'</url>') : '';
405 $xml .= isset($manufacturer->phone_no) && !empty($manufacturer->phone_no) ? ('<phone_no>'. $manufacturer->phone_no .'</phone_no>') : '';
406 $xml .= isset($manufacturer->fax_no) && !empty($manufacturer->fax_no) ? ('<fax_no>'. $manufacturer->fax_no .'</fax_no>') : '';
407 $xml .= '</manufacturer>';
408 }
409 $xml .= '</manufacturers>';
410 return $xml;
411 }
412
413 /**
414 * Export product attributes as XML.
415 */
416 function _uc_importer_export_attributes($attributes){
417 $xml = '<attributes>';
418 foreach ($attributes as $aid){
419 $attribute = uc_attribute_load($aid, 'product');
420 $xml .= '<attribute>';
421 $xml .= '<id>'. $attribute->aid .'</id>';
422 $xml .= '<name>'. htmlentities($attribute->name) .'</name>';
423 $xml .= '<ordering>'. $attribute->ordering .'</ordering>';
424 $xml .= '<options>';
425 if (is_array($attribute->options)){
426 foreach ($attribute->options as $option){
427 $xml .= '<option>';
428 $xml .= '<id>'. $option->oid .'</id>';
429 $xml .= '<name>'. htmlentities($option->name) .'</name>';
430 $xml .= '<price>'. $option->price .'</price>';
431 $xml .= '<weight>'. $option->weight .'</weight>';
432 $xml .= '<ordering>'. $option->ordering .'</ordering>';
433 $xml .= '</option>';
434 }
435 }
436 $xml .= '</options>';
437 $xml .= '</attribute>';
438 }
439 $xml .= '</attributes>';
440 return $xml;
441 }
442
443 /**
444 * Export product node types as XML.
445 */
446 function _uc_importer_export_classes($classes){
447 $xml = '<classes>';
448 foreach ($classes as $pcid){
449 $class = uc_product_class_load($pcid);
450 $xml .= '<class>';
451 $xml .= '<id>'. $class->pcid .'</id>';
452 $xml .= '<name>'. $class->name .'</name>';
453 $xml .= '<description>'. $class->description .'</description>';
454 $xml .= '</class>';
455 }
456 $xml .= '</classes>';
457 return $xml;
458 }
459
460 /**
461 * Export products as XML.
462 */
463 function _uc_importer_export_products($products){
464 $xml = '<products>';
465 foreach ($products as $nid){
466 $xml .= '<product>';
467 $product = node_load($nid);
468 $xml .= '<unique_hash>'. $product->unique_hash .'</unique_hash>';
469 $xml .= '<id>'. $product->nid .'</id>';
470 $xml .= '<type>'. $product->type .'</type>';
471 $xml .= '<name>'. htmlentities($product->title) .'</name>';
472 $xml .= '<description>'. htmlentities($product->body) .'</description>';
473 $xml .= '<model>'. htmlentities($product->model) .'</model>';
474 if (module_exists('uc_manufacturer')){
475 $manufacturer = uc_product_get_manufacturer($product->nid);
476 $xml .= isset($manufacturer->tid) ? ('<manufacturer>'. htmlentities($manufacturer->name) .'</manufacturer>') : '';
477 }
478 $xml .= isset($product->list_price) ? ('<list_price>'. $product->list_price .'</list_price>') : '';
479 $xml .= isset($product->cost) ? ('<cost>'. $product->cost .'</cost>') : '';
480 $xml .= '<sell_price>'. $product->sell_price .'</sell_price>';
481 $xml .= '<weight>'. $product->weight .'</weight>';
482 if (isset($product->field_image_cache) && file_exists($product->field_image_cache[0]['filepath'])){
483 foreach ($product->field_image_cache as $image){
484 $xml .= '<image>'. $image['filepath'] .'</image>';
485 }
486 }
487 if (module_exists('content')){
488 $type = content_types($product->type);
489 if (count($type['fields'])){
490 $xml .= '<fields>';
491 foreach ($type['fields'] as $field){
492 if ($field['field_name'] != 'field_image_cache'){
493 $node_field = isset($product->$field['field_name']) ? $product->$field['field_name'] : array();
494 if (count($node_field)){
495 $xml .= '<field>';
496 $xml .= '<name>'. $field['field_name'] .'</name>';
497 foreach ($node_field as $columns){
498 $xml .= '<delta>';
499 foreach ($columns as $name => $value){
500 $xml .= '<'. $name .'>'. $value .'</'. $name .'>';
501 }
502 $xml .= '</delta>';
503 }
504 $xml .= '</field>';
505 }
506 }
507 }
508 $xml .= '</fields>';
509 }
510 }
511 if (module_exists('taxonomy')){
512 $terms = taxonomy_node_get_terms_by_vocabulary($product->nid, variable_get('uc_catalog_vid', 0));
513 $xml .= '<categories>';
514 foreach ($terms as $term){
515 $xml .= '<category>';
516 $xml .= '<id>'. $term->tid .'</id>';
517 $xml .= '</category>';
518 }
519 $xml .= '</categories>';
520 }
521 if (module_exists('uc_attribute')){
522 $attributes = uc_product_get_attributes($product->nid);
523 if (!empty($attributes)){
524 $xml .= '<attributes>';
525 foreach ($attributes as $attribute){
526 $xml .= '<attribute>';
527 $xml .= '<id>'. $attribute->aid .'</id>';
528 $xml .= '<name>'. htmlentities($attribute->name) .'</name>';
529 $xml .= '<ordering>'. $attribute->ordering .'</ordering>';
530 $xml .= '<default_option>'. $attribute->default_option .'</default_option>';
531 if (!empty($attribute->options)){
532 $xml .= '<options>';
533 foreach ($attribute->options as $option){
534 $xml .= '<option>';
535 $xml .= '<id>'. $option->oid .'</id>';
536 $xml .= '<name>'. htmlentities($option->name) .'</name>';
537 $xml .= '<price>'. $option->price .'</price>';
538 $xml .= '<weight>'. $option->weight .'</weight>';
539 $xml .= '<ordering>'. $option->ordering .'</ordering>';
540 $xml .= '</option>';
541 }
542 $xml .= '</options>';
543 }
544 $xml .= '</attribute>';
545 }
546 $xml .= '</attributes>';
547 }
548 $result = db_query("SELECT combination, model FROM {uc_product_adjustments} WHERE nid = %d", $product->nid);
549 if (db_num_rows($result)){
550 $xml .= '<adjustments>';
551 while ($adjustment = db_fetch_object($result)){
552 $xml .= '<adjustment>';
553 $xml .= '<combination>'. htmlentities($adjustment->combination) .'</combination>';
554 $xml .= '<model>'. htmlentities($adjustment->model) .'</model>';
555 $xml .= '</adjustment>';
556 }
557 $xml .= '</adjustments>';
558 }
559 }
560 $xml .= '</product>';
561 }
562 $xml .= '</products>';
563 return $xml;
564 }
565
566 /**
567 * Export orders as XML.
568 */
569 function _uc_importer_export_orders($orders){
570 $xml = '<orders>';
571 foreach ($orders as $order_id){
572 $order = uc_order_load($order_id);
573 if (!empty($order)){
574 $xml .= '<order>';
575 $xml .= '<order_status>'. uc_order_status_data($order->order_status, 'title') .'</order_status>';
576 $xml .= '<order_total>'. $order->order_total .'</order_total>';
577 $xml .= '<primary_email>'. htmlentities($order->primary_email) .'</primary_email>';
578 $xml .= '<delivery_first_name>'. htmlentities($order->delivery_first_name) .'</delivery_first_name>';
579 $xml .= '<delivery_last_name>'. htmlentities($order->delivery_last_name) .'</delivery_last_name>';
580 $xml .= '<delivery_phone>'. htmlentities($order->delivery_phone) .'</delivery_phone>';
581 $xml .= '<delivery_company>'. htmlentities($order->delivery_company) .'</delivery_company>';
582 $xml .= '<delivery_street1>'. htmlentities($order->delivery_street1) .'</delivery_street1>';
583 $xml .= '<delivery_street2>'. htmlentities($order->delivery_street2) .'</delivery_street2>';
584 $xml .= '<delivery_city>'. htmlentities($order->delivery_city) .'</delivery_city>';
585 $xml .= '<delivery_zone>'. $order->delivery_zone .'</delivery_zone>';
586 $xml .= '<delivery_zip>'. htmlentities($order->delivery_zip) .'</delivery_zip>';
587 $xml .= '<delivery_country>'. $order->delivery_country .'</delivery_country>';
588 $xml .= '<billing_first_name>'. htmlentities($order->billing_first_name) .'</billing_first_name>';
589 $xml .= '<billing_last_name>'. htmlentities($order->billing_last_name) .'</billing_last_name>';
590 $xml .= '<billing_phone>'. htmlentities($order->billing_phone) .'</billing_phone>';
591 $xml .= '<billing_company>'. htmlentities($order->billing_company) .'</billing_company>';
592 $xml .= '<billing_street1>'. htmlentities($order->billing_street1) .'</billing_street1>';
593 $xml .= '<billing_street2>'. htmlentities($order->billing_street2) .'</billing_street2>';
594 $xml .= '<billing_city>'. htmlentities($order->billing_city) .'</billing_city>';
595 $xml .= '<billing_zone>'. $order->billing_zone .'</billing_zone>';
596 $xml .= '<billing_zip>'. htmlentities($order->billing_zip) .'</billing_zip>';
597 $xml .= '<billing_country>'. $order->billing_country .'</billing_country>';
598 $xml .= '<payment_method>'. $order->payment_method .'</payment_method>';
599 if (!empty($order->products)){
600 $xml .= '<products>';
601 foreach ($order->products as $product){
602 $xml .= '<product>';
603 $xml .= '<qty>'. $product->qty .'</qty>';
604 $xml .= '<name>'. htmlentities($product->title) .'</name>';
605 if ($product->manufacturer){
606 $xml .= '<manufacturer>'. htmlentities($product->manufacturer) .'</manufacturer>';
607 }
608 $xml .= '<model>'. htmlentities($product->model) .'</model>';
609 $xml .= '<cost>'. $product->cost .'</cost>';
610 $xml .= '<price>'. $product->price .'</price>';
611 $xml .= '<weight>'. $product->weight .'</weight>';
612 $xml .= '<data>'. htmlentities($product->data) .'</data>';
613 $xml .= '</product>';
614 }
615 $xml .= '</products>';
616 $quote = $order->quote;
617 $xml .= '<quote>';
618 $xml .= '<method>'. $quote['method'] .'</method>';
619 $xml .= '<accessorials>'. $quote['accessorials'] .'</accessorials>';
620 $xml .= '<rate>'. $quote['rate'] .'</rate>';
621 $xml .= '<quote_form>'. htmlentities($quote['quote_form']) .'</quote_form>';
622 $xml .= '</quote>';
623 if (!empty($order->line_items)){
624 $xml .= '<line_items>';
625 foreach ($order->line_items as $line_item){
626 $xml .= '<line_item>';
627 $xml .= '<type>'. $line_item['type'] .'</type>';
628 $xml .= '<title>'. htmlentities($line_item['title']) .'</title>';
629 $xml .= '<amount>'. $line_item['amount'] .'</amount>';
630 $xml .= '<weight>'. $line_item['weight'] .'</weight>';
631 $xml .= '</line_item>';
632 }
633 $xml .= '</line_items>';
634 }
635 }
636 $xml .= '</order>';
637 }
638 }
639 $xml .= '</orders>';
640 return $xml;
641 }
642
643 /**
644 * Imports an XML document into the database.
645 *
646 * The script checks for objects that have the same names as those in the XML
647 * document. If it finds a duplicate, it may replace that object with the XML,
648 * create a new object with marker indicating its imported status, or abort the
649 * importing of that particular object.
650 */
651 function uc_importer_import($xml){
652 include_once(drupal_get_path('module', 'uc_store') .'/includes/simplexml.php');
653 global $user, $active_db;
654
655 $error = '';
656 /* $data = new DOMDocument();
657 $data->loadXML($xml);
658
659 if (!$data){
660 $error = "Error: Could not load XML.";
661 }
662 else{ */
663 /* if (!($data->schemaValidate('http://www.ubercart.org/files/store.xsd'))){
664 $error = "Error: XML is not validated by schema.";
665 }
666 else{ */
667 $id_map = array('categories' => array(), 'manufacturers' => array(), 'attributes' => array(), 'options' => array(), 'classes' => array(), 'products' => array());
668 $action = variable_get('uc_importer_handle_duplicates', UC_IMPORTER_DO_NOTHING);
669 $store = new JSimpleXML();
670 $store->loadString($xml);
671 if (module_exists('uc_catalog')){
672 $categories = array();
673 foreach ($store->document->categories[0]->category as $category_data){
674 $name = (string)$category_data->name[0]->data();
675 $result = db_query("SELECT tid FROM {term_data} WHERE name = '%s'", $name);
676 if (db_num_rows($result) && $action != UC_IMPORTER_INCREMENT){
677 $id_map['categories'][(string)$category_data->id[0]->data()] = db_result($result);
678 }
679 else{
680 $tid = db_next_id('{term_data}_tid');
681 $id_map['categories'][(string)$category_data->id[0]->data()] = $tid;
682 db_query("INSERT INTO {term_data} (tid, vid, name, description) VALUES (%d, %d, '%s', '%s')", $tid, variable_get('uc_catalog_vid', 0), (string)$category_data->name[0]->data(), (string)$category_data->description[0]->data());
683 }
684 }
685 foreach ($store->document->categories[0]->category as $category_data){
686 if (isset($id_map['categories'][(string)$category_data->parent[0]->data()]) || (string)$category_data->parent[0]->data() == 0){
687 switch ($action){
688 case UC_IMPORTER_INCREMENT:
689 case UC_IMPORTER_REPLACE:
690 db_query("INSERT IGNORE INTO {term_hierarchy} (tid, parent) VALUES (%d, %d)", $id_map['categories'][(string)$category_data->id[0]->data()], $id_map['categories'][(string)$category_data->parent[0]->data()]);
691 break;
692 }
693 }
694 }
695 db_query("DELETE FROM {term_hierarchy} WHERE tid = parent");
696 }
697 if (module_exists('uc_manufacturer')){
698 foreach ($store->document->manufacturers[0]->manufacturer as $manufacturer_data){
699 $manufacturer = new stdClass();
700 foreach ($manufacturer_data->children() as $datum){
701 $manufacturer->{$datum->name()} = (string)$datum->data();
702 }
703 $result = db_query("SELECT tid FROM {term_data} WHERE name LIKE '%s\\__' OR name LIKE '%s'", $manufacturer->name, $manufacturer->name);
704 if ($tid = db_result($result)){
705 switch ($action){
706 case UC_IMPORTER_REPLACE:
707 drupal_execute('taxonomy_form_term', (array)$manufacturer, variable_get('uc_manufacturer_vid', 0), array('tid' => $tid));
708 break;
709 case UC_IMPORTER_INCREMENT:
710 $manufacturer->name .= '_'. db_num_rows($result);
711 drupal_execute('taxonomy_form_term', (array)$manufacturer, variable_get('uc_manufacturer_vid', 0));
712 break;
713 }
714 }
715 else{
716 drupal_execute('taxonomy_form_term', (array)$manufacturer, variable_get('uc_manufacturer_vid', 0));
717 }
718 }
719 }
720 if (module_exists('uc_attribute')){
721 foreach ($store->document->attributes[0]->attribute as $attribute_data){
722 $attribute = new stdClass();
723 $attribute->name = html_entity_decode($attribute_data->name[0]->data());
724 $attribute->ordering = isset($attribute_data->ordering) ? (integer)$attribute_data->ordering[0]->data() : 0;
725 $result = db_query("SELECT aid FROM {uc_attributes} WHERE name LIKE '%s\\__' OR name LIKE '%s'", $attribute->name, $attribute->name);
726 if ($aid = db_result($result)){
727 if ($action == UC_IMPORTER_INCREMENT){
728 $attribute->name .= '_'. db_num_rows($result);
729 drupal_execute('uc_attribute_form', (array)$attribute);
730 }
731 }
732 else{
733 drupal_execute('uc_attribute_form', (array)$attribute);
734 $aid = db_result(db_query("SELECT aid FROM {uc_attributes} WHERE name = '%s'", $attribute->name));
735 }
736 if ($aid){
737 $id_map['attributes'][(string)$attribute_data->id] = $aid;
738 $attribute->options = array();
739 foreach ($attribute_data->options[0]->option as $option_data){
740 $option = new stdClass();
741 $option->name = html_entity_decode($option_data->name[0]->data());
742 $option->price = isset($option_data->price) ? (float)$option_data->price[0]->data() : 0;
743 $option->weight = isset($option_data->weight) ? (float)$option_data->weight[0]->data() : 0;
744 $option->ordering = isset($option_data->ordering) ? (integer)$option_data->ordering[0]->data() : 0;
745 $result = db_query("SELECT oid FROM {uc_attribute_options} WHERE aid = %d AND (name LIKE '%s\\__' OR name LIKE '%s')", $aid, $option->name, $option->name);
746 if ($oid = db_result($result)){
747 switch ($action){
748 case UC_IMPORTER_INCREMENT:
749 $option->name .= '_'. db_num_rows($result);
750 drupal_execute('uc_attribute_option_form', (array)$option, $aid);
751 $id_map['options'][(string)$option_data->id[0]->data()] = mysql_insert_id($active_db);
752 break;
753 case UC_IMPORTER_REPLACE:
754 case UC_IMPORTER_DO_NOTHING:
755 $id_map['options'][(string)$option_data->id[0]->data()] = $oid;
756 break;
757 }
758 }
759 else{
760 drupal_execute('uc_attribute_option_form', (array)$option, $aid);
761 $id_map['options'][(string)$option_data->id[0]->data()] = mysql_insert_id($active_db);
762 }
763 }
764 }
765 }
766 }
767 $class = 'class'; // keyword workaround
768 foreach ($store->document->classes[0]->$class as $class_data){
769 $class = new stdClass();
770 $class->pcid = (string)$class_data->id[0]->data();
771 $class->name = (string)$class_data->name[0]->data();
772 $class->description = (string)$class_data->description[0]->data();
773 $result = db_query("SELECT pcid FROM {uc_product_classes} WHERE pcid = '%s'", $class->pcid);
774 if ($pcid = db_result($result)){
775 switch ($action){
776 case UC_IMPORTER_REPLACE:
777 drupal_execute('uc_product_class_form', (array)$class, $class->pcid);
778 break;
779 case UC_IMPORTER_INCREMENT:
780 drupal_execute('uc_product_class_form', (array)$class);
781 break;
782 }
783 }
784 else{
785 drupal_execute('uc_product_class_form', (array)$class, $class->pcid);
786 }
787 }
788 foreach ($store->document->products[0]->product as $product_data){
789 $product = new stdClass();
790 watchdog('importer', '<pre>'. print_r($fix, true) .'</pre>');
791 if (!isset($product_data->unique_hash)){
792 $product_data->addChild('unique_hash', md5((string)$product_data->nid[0]->data() . (string)$product_data->model[0]->data() . (string)$product_data->list_price[0]->data() . (string)$product_data->cost[0]->data() . (string)$product_data->sell_price[0]->data() . (string)$product_data->weight[0]->data() . (string)$product_data->units[0]->data() . (string)$product_data->default_qty[0]->data() . time()));
793 }
794 if ($nid = db_result(db_query("SELECT nid FROM {uc_products} WHERE unique_hash LIKE '%s'", (string)$product_data->unique_hash[0]->data()))){
795 switch ($action){
796 case UC_IMPORTER_REPLACE:
797 $product->nid = $nid;
798 $product->revision = true;
799 $id_map['products'][(string)$product_data->id[0]->data()] = $nid;
800 break;
801 case UC_IMPORTER_INCREMENT:
802 $product->nid = $nid = db_next_id('{node}_nid');
803 $product_data->unique_hash->setData('');
804 $id_map['products'][(string)$product_data->id[0]->data()] = $nid;
805 break;
806 case UC_IMPORTER_DO_NOTHING:
807 $product->nid = $nid;
808 $id_map['products'][(string)$product_data->id[0]->data()] = $nid;
809 continue 2;
810 }
811 }
812 $product->type = (string)$product_data->type[0]->data();
813 $product->uid = $user->uid;
814 $product->log = t('Imported product from XML.');
815 $product->name = $user->name;
816 $product->status = 1;
817 $product->format = 3;
818 if (module_exists('uc_catalog')){
819 foreach ($product_data->categories[0]->category as $category_data){
820 $product->taxonomy[] = $id_map['categories'][(string)$category_data->id[0]->data()];
821 }
822 }
823 $product->unique_hash = (string)$product_data->unique_hash[0]->data();
824 $product->title = html_entity_decode((string)$product_data->name[0]->data());
825 $product->body = html_entity_decode((string)$product_data->description[0]->data());
826 $product->model = html_entity_decode((string)$product_data->model[0]->data());
827 if (module_exists('uc_manufacturer') && $manufacturer = variable_get('uc_manufacturer_vid', 0)){
828 $product->taxonomy['tags'][$manufacturer] = html_entity_decode((string)$product_data->manufacturer[0]->data());
829 }
830 $product->list_price = (float)$product_data->list_price[0]->data();
831 $product->cost = (float)$product_data->cost[0]->data();
832 $product->sell_price = (float)$product_data->sell_price[0]->data();
833 $product->weight = (float)$product_data->weight[0]->data();
834 $i = 0;
835 if (module_exists('imagefield')){
836 foreach ($product_data->image as $image){
837 $image_path = (string)$image->path[0]->data();
838 $path_info = pathinfo($image_path);
839 $local_path = file_create_path() .'/ubercart_images/'. $_SERVER['HTTP_HOST'];
840 if (!file_check_directory($local_path)){
841 $local_path = file_create_path() .'/ubercart_images';
842 }
843 $local_path .= '/'. urldecode(basename($image_path));
844 if (file_exists($local_path) || $size = file_put_contents($local_path, fopen($image_path, 'rb'))){
845 $product->field_image_cache[$i] = array(
846 'fid' => (file_exists($local_path) ? db_result(db_query("SELECT fid FROM {files} WHERE nid = %d AND filepath = '%s'", $nid, $local_path)) : 'upload'),
847 'title' => isset($image->title) ? html_entity_decode((string)$image->title[0]->data()) : '',
848 'alt' => isset($image->alt) ? html_entity_decode((string)$image->alt[0]->data()) : '',
849 'filename' => basename($image_path),
850 'filepath' => $local_path,
851 'filesize' => (file_exists($local_path) ? @filesize($local_path) : $size),
852 'filemime' => 'image/'. $path_info['extension'],
853 );
854 $i++;
855 }
856 }
857 }
858 if (isset($product_data->fields)){
859 $fields = array();
860 foreach ($product_data->fields[0]->field as $field_data){
861 foreach ($field_data->delta as $delta){
862 $columns = array();
863 foreach ($delta->children() as $value_data){
864 $columns[$value_data->name()] = html_entity_decode((string)$value_data->data());
865 }
866 $field_name = html_entity_decode((string)$field_data->name[0]->data());
867 if (!is_array($product->$field_name)){
868 $product->$field_name = array();
869 }
870 array_push($product->$field_name, $columns);
871 }
872 }
873 }
874 watchdog('importer', '<pre>'. print_r($product_data, true) .'</pre>');
875 watchdog('importer', '<pre>'. print_r($product, true) .'</pre>');
876 node_save($product);
877 if (!isset($product->nid)){
878 $product->nid = db_result(db_query("SELECT id FROM {sequences} WHERE name = '{node}_nid'"));
879 }
880 $id_map['products'][(string)$product_data->id[0]->data()] = $product->nid;
881 if (module_exists('uc_attribute')){
882 $attr_replace = array();
883 $attr_values = array();
884 $opt_replace = array();
885 $opt_values = array();
886 foreach ($product_data->attributes[0]->attribute as $attribute_data){
887 if (!isset($id_map['attributes'][(string)$attribute_data->id[0]->data()])){
888 $attribute = new stdClass();
889 $attribute->name = html_entity_decode((string)$attribute_data->name[0]->data());
890 $attribute->ordering = isset($attribute_data->ordering) ? (integer)$attribute_data->ordering[0]->data() : 0;
891 drupal_execute('uc_attribute_form', (array)$attribute);
892 $id_map['attributes'][(string)$attribute_data->id[0]->data()] = db_result(db_query("SELECT aid FROM {uc_attributes} WHERE name = '%s'", $attribute->name));
893 }
894 $attr_replace[] = '%d,%d,%d,%d';
895 $attr_values[] = $product->nid;
896 $attr_values[] = $id_map['attributes'][(string)$attribute_data->id[0]->data()];
897 $attr_values[] = isset($attribute_data->ordering) ? (integer)$attribute_data->ordering[0]->data() : 0;
898 foreach ($attribute_data->options[0]->option as $option_data){
899 if (!isset($id_map['options'][(string)$option_data->id[0]->data()])){
900 $option = new stdClass();
901 $option->name = html_entity_decode((string)$option_data->name[0]->data());
902 $option->price = $option_data->price[0]->data();
903 $option->weight = $option_data->weight[0]->data();
904 $option->ordering = isset($option_data->ordering) ? $option_data->ordering : 0;
905 drupal_execute('uc_attribute_option_form', (array)$option, $id_map['attributes'][(string)$attribute_data->id[0]->data()]);
906 $id_map['options'][(string)$option_data->id[0]->data()] = mysql_insert_id($active_db);
907 }
908 $opt_replace[] = '%d,%d,%f,%f,%d';
909 $opt_values[] = $product->nid;
910 $opt_values[] = $id_map['options'][(string)$option_data->id[0]->data()];
911 $opt_values[] = $option_data->price[0]->data();
912 $opt_values[] = $option_data->weight[0]->data();
913 $opt_values[] = isset($option_data->ordering) ? (integer)$option_data->ordering[0]->data() : 0;
914 }
915 $default_option = isset($attribute_data->default_option) ? (string)$attribute_data->default_option[0]->data() : $attribute_data->options->option[0]->id[0]->data();
916 $attr_values[] = $id_map['options'][$default_option];
917 }
918 if (count($attr_values)){
919 db_query("DELETE FROM {uc_product_attributes} WHERE nid = %d", $product->nid);
920 db_query("INSERT INTO {uc_product_attributes} (nid, aid, ordering, default_option) VALUES (". implode('),(', $attr_replace) .")", $attr_values);
921 }
922 if (count($opt_values)){
923 db_query("DELETE FROM {uc_product_options} WHERE nid = %d", $product->nid);
924 db_query("INSERT INTO {uc_product_options} (nid, oid, price, weight, ordering) VALUES (". implode('),(', $opt_replace) .")", $opt_values);
925 }
926 $adjustments = array('nid' => $product->nid, 'default' => $product->model, 'body' => array());
927 foreach ($product_data->adjustments[0]->adjustment as $adjustment_data){
928 $combination = array();
929 $old_combo = unserialize(html_entity_decode((string)$adjustment_data->combination[0]->data()));
930 if (is_array($old_combo)){
931 foreach ($old_combo as $aid => $oid){
932 $combination[$id_map['attributes'][$aid]] = $id_map['options'][$oid];
933 }
934 $adjustments['body'][] =