this is to the find the person details

Comments

rahul_singh’s picture

<?php
/**
* Implementation of xml_import()
*/
function xml_import(){
/* $query = db_query("SELECT nid FROM node WHERE type = 'shop'");
while($result = db_fetch_object($query)){
node_delete($result->nid);
}
*/

$form = array(
'#attributes' => array(
'enctype' => "multipart/form-data"
),
);
$form['file_upload'] = array(
'#type' => 'file',
'#title' => t('Upload a zip file.'),
);
$form['submit']=array(
'#type' => 'submit',
'#value' => 'Import',
);
return $form;
}
/**
* Implementation of xml_import_validate()
*/
function xml_import_validate($form, &$form_state){
$fieldName = 'file_upload';
$limits = array (
'extensions' => 'zip',
);
$validators = array(
'file_validate_extensions' => array($limits['extensions']),
);

// If a file was uploaded, process it.
//if (isset($_FILES['files']) && is_uploaded_file($_FILES['files']['tmp_name'][$fieldName])) {
if (isset($_FILES['files'])) {
// attempt to save the uploaded file
$file = file_save_upload($fieldName,$validators = array());
// set error if file was not uploaded
if (!$file) {
form_set_error($fieldName, 'Error: file_save_upload() is not working properly.');
return;
}
// set files to form_state, to process when form is submitted
$form_state['values']['file'] = $file;
}
else {
// set error
form_set_error($fieldName, 'Error uploading file.');
return;
}
}
/**
* Implementation of xml_import_validate()
*/
function xml_import_submit($form, &$form_state){
// Removing all existing files and directory if any exists
// Reading file names from a folder.
if($handle = opendir(file_directory_path() . '/xmlfiles/')) {
// This is the correct way to loop over the directory.
$files_name = array();
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$files_name[] = $file;
//echo file_directory_path();exit;
if(is_file(file_directory_path() . '/xmlfiles/'.$file)){
unlink(file_directory_path() . '/xmlfiles/'.$file);
}else{
if($handles = opendir(file_directory_path() . '/xmlfiles/'.$file.'/')) {
while (false !== ($file1 = readdir($handles))) {
if ($file1 != "." && $file1 != "..") {
unlink(file_directory_path() . '/xmlfiles/'.$file.'/'.$file1);
$files_name1[] = $file1;
}
}
rmdir(file_directory_path() . '/xmlfiles/'.$file);
}
}

}
}
closedir($handle);
}
/* end */

//define your limits for the submission here
$limits = array (
'extensions' => 'zip',
);
$validators = array(
'file_validate_extensions' => array($limits['extensions']),
);
// Save new file uploads.
if ($file = file_save_upload('file_upload', $validators, file_directory_path())) {
// Remove the old zip files.
if(isset($form['values']['file_upload']) && file_exists($form['values']['file_upload'])){
file_delete($form['values']['file_upload']);
}
// The zip file was saved using file_save_upload() and was added to the
// files table as a temporary file. We'll make a copy and let the garbage
// collector delete the original upload.
$destination = file_directory_path();
if (file_copy($file, $destination, FILE_EXISTS_REPLACE)) {
//create a folder for files.
if(!is_dir(file_directory_path().'/xmlfiles')){
$folder = mkdir(file_directory_path().'/xmlfiles',0777);
}
// Extracting files.
$extracted = false;
$zip = new ZipArchive;
$res = $zip->open(file_directory_path() . '/' . $form_state['values']['file']->filename);
if ($res === TRUE) {
$zip->extractTo(file_directory_path() . '/' . 'xmlfiles/');
$zip->close();
$extracted = true;
}
// Deleting uploaded zip files.
if($extracted){
unlink(file_directory_path() . '/' . $form_state['values']['file']->filename);
}
// Reading file names from a folder.
if($handle = opendir(file_directory_path() . '/' . 'xmlfiles/')) {
// This is the correct way to loop over the directory.
$files_name = array();
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$files_name[] = $file;
}
}
closedir($handle);
$imported = import_xml_files($files_name);
//if($imported){
drupal_set_message(t('XML imported sucessfully.'),$type = 'status');
//}else{
//drupal_set_message(t('Sorry we cannot read your XML files.'),$type = 'error');
//}
}
}
else {
form_set_error('file_upload', t("Failed to upload the picture image; the %directory directory doesn't exist or is not writable.", array('%directory' => $destination)));
}
}
}

/**
* Implementation of import_xml_files($files_name)
*/
function import_xml_files($files_name){
foreach($files_name as $key => $filename){
if(!is_dir(file_directory_path() . '/' . 'xmlfiles/' . $filename)){// if file name is not a directory
$xml_filepath = file_directory_path() . '/xmlfiles/' . $filename;
//echo stripos($filename,'ESPRIT_US',0);exit;
$xmlarray = array();
if(stripos($filename,'ESPRIT_US',0) || stripos($filename,'Adidas',0)){// if Brands are Espirit or Adidas

//echo 'file name is espirite oradidas';exit;

$xmlarray = xml2array(file_get_contents($xml_filepath));
foreach($xmlarray['catalog']['product'] as $key=>$product){
$node = new stdClass;
$if_exist_nid = db_result(db_query("SELECT nid FROM node WHERE title = '%s'",$product['name']));
if($if_exist_nid > 0){
$node->nid = $if_exist_nid;
}else{
$node->nid = '';
}

$node->type = 'shop';
$node->uid = 1;
$node->status = 1;
$node->created = time();
$node->changed = time();
$node->title = $product['name'];// $product['programname'];
$node->name = 'admin';
$node->body = $product['description'];
if(stripos($filename,'ESPRIT_US',0)){
$str = $product['advertisercategory'];
$term_str = explode('>',$str);
if(count($term_str) > 0){
$node->taxonomy = getStringToTaxonomy($term_str);//change
}else{
$node->taxonomy = 83;// map this product into Uncategories category
}
}else if(stripos($filename,'Adidas',0)){
//$node->taxonomy = adidasStringToTaxonomy($product['advertisercategory']);//change
//ADIDAS/APPAREL MENS/TRAINING/SHORT SLEEVE TOPS
$str = $product['advertisercategory'];
$term_strs = explode('/',$str);
if(count($term_strs) > 0){
$cat = explode(' ',$term_strs[1]);
$main_cat = strtolower($cat[0]); // Main category
if($main_cat == 'accessory')
$main_cat = 'accessories';
$term_str[2] = $main_cat; // Main category
$gender_cat = strtolower($cat[1]);
if($gender_cat == 'mens')
$gender_cat = 'men';
$term_str[0] = $gender_cat; // Gender
$term_str[3] = strtolower($term_strs[3]); // Product category

$node->taxonomy = getStringToTaxonomy($term_str);//change
}else{
$node->taxonomy = 83;// map this product into Uncategories category
}
}

$node->pathauto_perform_alias = 1;
$node->field_pageurl[0]['value'] = $product['programurl'];
// Date
$node->field_date[0]['value'] = date('Y-m-d', strtotime($product['lastupdated']));//change
$node->field_date[0]['value2'] = date('Y-m-d', strtotime($product['lastupdated']));//change
$node->field_date[0]['timezone'] = 'UTC';
$node->field_date[0]['offset'] = 0;
$node->field_date[0]['offset2'] = 0;
$node->field_date[0]['timezone_db'] = 'UTC';
$node->field_date[0]['date_type'] = 'date';
$node->field_name[0]['value'] = $product['name'];
$node->field_description[0]['value'] = $product['description'];
$node->field_sku[0]['value'] = $product['sku'];
$node->field_upc[0]['value'] = $product['upc'];
$node->field_currency[0]['value'] = $product['currency'];
$node->field_saleprice[0]['value'] = $product['saleprice'];
$node->field_product_price[0]['value']= $product['price'];
$node->field_url[0]['value'] = $product['buyurl'];
$node->field_shop_image[0]['value'] = $product['imageurl'];
$node->field_url[0]['title'] = $product['name'];
$node->field_url[0]['url'] = $product['buyurl'];
$node->field_description[0]['value'] = $product['description'];
$node->field_instock[0]['value'] = $product['instock'];
$node-> field__unmapped_category[0]['value'] = $product['advertisercategory'];

node_save($node);
$i++;
/*if($key==20){
exit;
}*/
}
}

/**
*
*
*
*/
if(stripos($filename,'CoutureCandy_com',0) || stripos($filename,'Finishline_com',0)){// if Brands are Espirit or Adidas
$xmlarray = xml2array(file_get_contents($xml_filepath));
foreach($xmlarray['catalog']['product'] as $key=>$product){
$node = new stdClass;
$if_exist_nid = db_result(db_query("SELECT nid FROM node WHERE title = '%s'",$product['name']));
if($if_exist_nid > 0){
$node->nid = $if_exist_nid;
}else{
$node->nid = '';
}

$node->type = 'shop';
$node->uid = 1;
$node->status = 1;
$node->created = time();
$node->changed = time();
$node->title = $product['name'];// $product['programname'];
$node->name = 'admin';
$node->body = $product['description'];

if(stripos($filename,'CoutureCandy_com',0)){
/*
Women > Trend - 70s Revival
Women > Denim > Dark Wash Denim
Women > Denim > Medium Wash Denim
Women > Accessories > Jewelry > Necklaces
*/
$str = explode('>',strtolower($str));
if(count($str) > 0){
$term_str[2] = $str[2]; // Main category
$term_str[0] = $str[0]; // Gender
$term_str[3] = $str[3]; // Product category
$node->taxonomy = getStringToTaxonomy($term_str);//change
}else{
$node->taxonomy = 83;// map this product into Uncategories category
}

}else if(stripos($filename,'Finishline_com',0)){
/* Womens Apparel Jackets
Womens Apparel Pants
*/

$str = $product['advertisercategory'];
$cat = explode(' ',$str);
if(count($cat) > 0){
$main_cat = strtolower($cat[1]); // Main category
$term_str[2] = $main_cat; // Main category
$gender_cat = strtolower($cat[0]);
if($gender_cat == 'mens')
$gender_cat = 'men';
if($gender_cat == 'womens')
$gender_cat = 'women';
$term_str[0] = $gender_cat; // Gender
$term_str[3] = strtolower($cat[2]); // Product category

$node->taxonomy = getStringToTaxonomy($term_str);//change
}else{
$node->taxonomy = 83;// map this product into Uncategories category
}
}

$node->pathauto_perform_alias = 1;
$node->field_pageurl[0]['value'] = $product['programurl'];
// Date
$node->field_date[0]['value'] = date('Y-m-d', strtotime($product['lastupdated']));//change
$node->field_date[0]['value2'] = date('Y-m-d', strtotime($product['lastupdated']));//change
$node->field_date[0]['timezone'] = 'UTC';
$node->field_date[0]['offset'] = 0;
$node->field_date[0]['offset2'] = 0;
$node->field_date[0]['timezone_db'] = 'UTC';
$node->field_date[0]['date_type'] = 'date';
$node->field_name[0]['value'] = $product['name'];
$node->field_description[0]['value'] = $product['description'];
$node->field_sku[0]['value'] = $product['sku'];
$node->field_upc[0]['value'] = $product['upc'];
$node->field_currency[0]['value'] = $product['currency'];
$node->field_saleprice[0]['value'] = $product['saleprice'];
$node->field_product_price[0]['value']= $product['price'];
$node->field_url[0]['value'] = $product['buyurl'];
$node->field_shop_image[0]['value'] = $product['imageurl'];
$node->field_url[0]['title'] = $product['name'];
$node->field_url[0]['url'] = $product['buyurl'];
$node->field_description[0]['value'] = $product['description'];
$node->field_instock[0]['value'] = $product['instock'];
$node-> field__unmapped_category[0]['value'] = $product['advertisercategory'];

node_save($node);
$i++;
if($key==20){
break;
}
}
}
/*
*
* End of XML file
*/

/*
* Start to import Link share xml file
* cmp.xml
*/

if(stripos($filename,'cmp',0) || stripos($filename,'cmp',0)){// if Brands are Espirit or Adidas
$xmlarray = xml2array(file_get_contents($xml_filepath));
foreach($xmlarray['merchandiser']['product'] as $key=>$product){
$node = new stdClass;
$if_exist_nid = db_result(db_query("SELECT nid FROM node WHERE title = '%s'",$product['keywords']));
if($if_exist_nid > 0){
$node->nid = $if_exist_nid;
}else{
$node->nid = '';
}

$node->type = 'shop';
$node->uid = 1;
$node->status = 1;
$node->created = time();
$node->changed = time();
$node->title = $product['keywords'];// $product['programname'];
$node->name = 'admin';
$node->body = $product['brand'];//this is keyword of product that is to be brand name
if(stripos($filename,'cmp',0)){
/*
[category] => Array(
[primary] => Beauty // Main category
[secondary] => Special Shops - Men's Grooming // Product category
)
*
* attributeClass => Array(
* [Gender] => Male // Sub category
* )
*
*/

$term_str[2] = strtolower($product['category']['primary']); // Main category
if($product['attributeClass']['Gender'] == 'Male')
$gender_cat = 'men';
if($product['attributeClass']['Gender'] == 'Female')
$gender_cat = 'women';
$term_str[0] = $gender_cat; // Gender
$product_cat_str = explode('-', strtolower($product['category']['secondary']));
$term_str[3] = $product_cat_str[0]; // Product category
$node->taxonomy = getStringToTaxonomy($term_str);//change
}

$node->pathauto_perform_alias = 1;
$node->field_pageurl[0]['value'] = $product['URL']['product'];
// Date
$node->field_date[0]['value'] = date('Y-m-d', strtotime(date("m.d.y")));//change
$node->field_date[0]['value2'] = date('Y-m-d', strtotime(date("m.d.y")));//change
$node->field_date[0]['timezone'] = 'UTC';
$node->field_date[0]['offset'] = 0;
$node->field_date[0]['offset2'] = 0;
$node->field_date[0]['timezone_db'] = 'UTC';
$node->field_date[0]['date_type'] = 'date';
$node->field_name[0]['value'] = $product['keywords'];
$node->field_description[0]['value'] = $product['description']['long'];
$node->field_sku[0]['value'] = $product['0_attr']['sku_number']; // sku not available in XML
$node->field_upc[0]['value'] = $product['upc'];
$node->field_currency[0]['value'] = $product['price_attr']['currency'];
$node->field_saleprice[0]['value'] = $product['price']['sale'];
$node->field_product_price[0]['value']= $product['price']['retail'];
$node->field_url[0]['value'] = $product['URL']['product'];
$node->field_shop_image[0]['value'] = $product['URL']['productImage'];
$node->field_url[0]['title'] = $product['keywords'];
$node->field_url[0]['url'] = $product['URL']['product'];
$node->field_description[0]['value'] = $product['description']['long'];
$node->field_instock[0]['value'] = $product['shipping']['availability'];
$node-> field__unmapped_category[0]['value'] = $term_str[0] ."<<".$term_str[2]."<<<". $term_str[3];
node_save($node);
$i++;
if($key==20){
break;
}
$product_info[] = $product;
$product_info1[] = $node;
}

$f = fopen('sites/default/files/cmp - xmlarray.txt','w');
fwrite($f,'----------------------\n\n\\n');
fwrite($f,print_r($product_info,true));
fwrite($f,'----------------------\n\n\\n');
fwrite($f,'----------!!!!!!!!!!!!!!!!!!!!!!!!!!!!------------\n\n\\n');
fwrite($f,print_r($product_info1,true));

fclose($f);

}
/*
* End of cmp xml
*/
}
}
}
/**
*
*/
function _check_existing_word($subject,$matching_word){
$subject=strtolower($subject);
$matching_word=strtolower($matching_word);
$pattern = '/\b' . $matching_word . '\b/';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);
if(count($matches) > 0){
return TRUE;
}
return FALSE;
}

/* custom function for getting tid for relevant term name */
function _get_tid($term_name,$parrent_tid = 0){
$term_name = strtolower($term_name);
// Check if term is already exists
$tid = db_result(db_query("SELECT td.* FROM term_data td INNER join term_hierarchy th ON th.tid = td.tid WHERE th.parent = %d AND LOWER(td.name) = '%s' AND td.vid = 2",$parrent_tid,$term_name));
// Get synonyms category if exists for matching category
$query = db_query("SELECT * FROM synonyms_words");
while($result = db_fetch_object($query)){
$syn_arr[strtolower($result->name_synonyms_words)] = $result->matching_cat_tid;
}
if($tid > 0){
return $tid;
}else if($syn_arr[$term_name] > 0){
return $syn_arr[$term_name];
}else{
return FALSE;
}
}

/**
* Implementation of espiritStringToTaxonomy($str)
* File Name: 10369777-ESPRIT_US.xml
*/
function getStringToTaxonomy($term_str){
// 2-women>all products>1-jewelry>3-bracelets/bangle
$main_category = strtolower($term_str[2]); // Product main category in menu bar
$child_category_level1 = strtolower(implode(' or ',explode('/',$term_str[0]))); // Men or Women
$child_category_level2 = strtolower(implode(' or ',explode('/',$term_str[3]))); // Product category
// Initialize taxonomy array()
$taxonomy = array();
//Initialize main category
$tid = _get_tid($main_category,0);
if($tid > 0){
//echo $tid;exit;
$taxonomy[2][] = $tid;
$parrent_tid = $tid;
// Initialize child level first category
$tid = _get_tid($child_category_level1,$parrent_tid);
if($tid > 0){
$taxonomy[2][] = $tid;
$parrent_tid = $tid;
// Initialize child level second category
$tid = _get_tid($child_category_level2,$parrent_tid);
if($tid > 0){
$taxonomy[2][] = $tid;
}else{ // Add new taxonomy by existing name
$term = array(
'vid' => 2,
'name' => ucfirst($child_category_level2),
'parent' => $parrent_tid,
);
taxonomy_save_term($term);
$new_inserted_tid = db_result(db_query("SELECT MAX(tid) FROM term_data"));
_update_taxonomy_term($tid);
$taxonomy[2][] = $new_inserted_tid;
}
}elseif($child_category_level1 == 'unisex'){
// set this product in both (men,women) category
// for men
$parrent_tid_new = $parrent_tid;
$tid = _get_tid('men',$parrent_tid_new);
if($tid > 0){
$taxonomy[2][] = $tid; // men tid
$parrent_tid = $tid;
$tid = _get_tid($child_category_level2,$parrent_tid);
if($tid > 0){
$taxonomy[2][] = $tid;
}else{ // Add new taxonomy by existing name
$term = array(
'vid' => 2,
'name' => ucfirst($child_category_level2),
'parent' => $parrent_tid,
);
taxonomy_save_term($term);
$new_inserted_tid = db_result(db_query("SELECT MAX(tid) FROM term_data"));
_update_taxonomy_term($tid );
$taxonomy[2][] = $new_inserted_tid;
}
}

// for women
$tid = _get_tid('women',$parrent_tid_new);
if($tid > 0){
$taxonomy[2][] = $tid; // men tid
$parrent_tid = $tid;
$tid = _get_tid($child_category_level2,$parrent_tid);
if($tid > 0){
$taxonomy[2][] = $tid;
}else{ // Add new taxonomy by existing name
$term = array(
'vid' => 2,
'name' => ucfirst($child_category_level2),
'parent' => $parrent_tid,
);
taxonomy_save_term($term);
$tid = db_result(db_query("SELECT MAX(tid) FROM term_data"));
_update_taxonomy_term($tid);
$taxonomy[2][] = $tid;

}
}
}else{
// Add this product to uncategorize category
$taxonomy[2][] = 83; // 83 = tid of Uncategories term's id
}
}else{
// Add this product to uncategorize category
$taxonomy[2][] = 83; // 83 = tid of Uncategories term's id
}
return $taxonomy;
}
/**
* Implementation of xml2array($contents, $get_attributes=1, $priority = 'tag')
*/
function xml2array($contents, $get_attributes=1, $priority = 'tag'){
if(!$contents) return array();

if(!function_exists('xml_parser_create')) {
//print "'xml_parser_create()' function not found!";
return array();
}

//Get the XML parser of PHP - PHP must have this module for the parser to work
$parser = xml_parser_create('');
xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); # http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodi...
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, trim($contents), $xml_values);
xml_parser_free($parser);

if(!$xml_values) return;//Hmm...

//Initializations
$xml_array = array();
$parents = array();
$opened_tags = array();
$arr = array();

$current = &$xml_array; //Refference

//Go through the tags.
$repeated_tag_index = array();//Multiple tags with same name will be turned into an array
foreach($xml_values as $data) {
unset($attributes,$value);//Remove existing values, or there will be trouble

//This command will extract these variables into the foreach scope
// tag(string), type(string), level(int), attributes(array).
extract($data);//We could use the array by itself, but this cooler.

$result = array();
$attributes_data = array();

if(isset($value)) {
if($priority == 'tag') $result = $value;
else $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode
}

//Set the attributes too.
if(isset($attributes) and $get_attributes) {
foreach($attributes as $attr => $val) {
if($priority == 'tag') $attributes_data[$attr] = $val;
else $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
}
}

//See tag status and do the needed.
if($type == "open") {//The starting of the tag ''
$parent[$level-1] = &$current;
if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag
$current[$tag] = $result;
if($attributes_data) $current[$tag. '_attr'] = $attributes_data;
$repeated_tag_index[$tag.'_'.$level] = 1;

$current = &$current[$tag];

} else { //There was another element with the same tag name

if(isset($current[$tag][0])) {//If there is a 0th element it is already an array
$current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
$repeated_tag_index[$tag.'_'.$level]++;
} else {//This section will make the value an array if multiple tags with the same name appear together
$current[$tag] = array($current[$tag],$result);//This will combine the existing item and the new item together to make an array
$repeated_tag_index[$tag.'_'.$level] = 2;

if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
$current[$tag]['0_attr'] = $current[$tag.'_attr'];
unset($current[$tag.'_attr']);
}

}
$last_item_index = $repeated_tag_index[$tag.'_'.$level]-1;
$current = &$current[$tag][$last_item_index];
}

} elseif($type == "complete") { //Tags that ends in 1 line ''
//See if the key is already taken.
if(!isset($current[$tag])) { //New Key
$current[$tag] = $result;
$repeated_tag_index[$tag.'_'.$level] = 1;
if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data;

} else { //If taken, put all things inside a list(array)
if(isset($current[$tag][0]) and is_array($current[$tag])) {//If it is already an array...

// ...push the new element into that array.
$current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;

if($priority == 'tag' and $get_attributes and $attributes_data) {
$current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
}
$repeated_tag_index[$tag.'_'.$level]++;

} else { //If it is not an array...
$current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value
$repeated_tag_index[$tag.'_'.$level] = 1;
if($priority == 'tag' and $get_attributes) {
if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well

$current[$tag]['0_attr'] = $current[$tag.'_attr'];
unset($current[$tag.'_attr']);
}

if($attributes_data) {
$current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
}
}
$repeated_tag_index[$tag.'_'.$level]++; //0 and 1 index is already taken
}
}

} elseif($type == 'close') { //End of tag ''
$current = &$parent[$level-1];
}
}

return($xml_array);
}

function _update_taxonomy_term($tid){
db_query("INSERT INTO term_permissions_role (tid,rid) values($tid,1)");
db_query("INSERT INTO term_permissions_role (tid,rid) values($tid,2)");
}

<?php
/**
* Menu callback -- ask for confirmation of XML import
*/
function xml_import_confirm(&$form_state) {
return confirm_form($form,
t('Are you sure that you have putted your xml zip files on below location -
("root/sites/default/files/ftpxml") ?'),
t('Are you sure you want to import those xml files?'),
t('Are you sure you want to import those xml files?
This action cannot be undone.'),
t('Import'),
t('Cancel')
);
}

/**
* Execute node deletion
*/
function xml_import_confirm_submit($form, &$form_state) {
if ($form_state['values']['confirm']) {
xml_import_submit();
}
}

/**
* Implementation of xml_import_validate()
*/
function xml_import_submit(){
//exit;
/*
* Removing all existing files if any
*/
if($handles = opendir(file_directory_path() . '/ftpxml/')) {
while (false !== ($file = readdir($handles))) {
if ($file != "." && $file != "..") {
if(is_file(file_directory_path() . '/ftpxml/'.$file)){
if(stripos($file,'xml',0))
unlink(file_directory_path() . '/ftpxml/'.$file);
}
}
}
}

/*
* Extracting Zip files
*/
$zip = new ZipArchive;
if($handles = opendir(file_directory_path() . '/ftpxml/')) {
while (false !== ($file1 = readdir($handles))) {
if ($file1 != "." && $file1 != "..") {
if(is_file(file_directory_path() . '/ftpxml/'.$file1)){
if(stripos($file1,'zip',0)){
$res = $zip->open(file_directory_path() . '/ftpxml/'.$file1);
if ($res === TRUE) {
$zip->extractTo(file_directory_path() . '/ftpxml/');
$zip->close();
$extracted = true;
}
unlink(file_directory_path() . '/ftpxml/'.$file1);
}
}
}
}
}

/*
* Read each xml file and import them into database
*/

if($handle = opendir(file_directory_path() . '/' . 'ftpxml/')) {
// This is the correct way to loop over the directory.
$files_names = array();
while (false !== ($file2 = readdir($handle))) {
if ($file2 != "." && $file2 != "..") {
$files_names[] = $file2;
}
}
closedir($handle);
//print_r($files_names);exit;
$imported = import_xml_files($files_names);
if($imported){
drupal_set_message(t('XML imported sucessfully.'),$type = 'status');
}else{
drupal_set_message(t('Sorry we cannot read your XML files.'),$type = 'error');
}
}
}

/**
* Implementation of import_xml_files($files_name)
*/
function import_xml_files($files_name){
$flag = FALSE;
if(count($files_name) > 0){
foreach($files_name as $key => $filename){
if(!is_dir(file_directory_path() . '/' . 'ftpxml/' . $filename)){// if file name is not a directory
$xml_filepath = file_directory_path() . '/ftpxml/' . $filename;
//echo stripos($filename,'ESPRIT_US',0);exit;
$xmlarray = array();
if(stripos($filename,'ESPRIT_US',0) || stripos($filename,'Adidas',0)){// if Brands are Espirit or Adidas
$xmlarray = xml2array(file_get_contents($xml_filepath));
foreach($xmlarray['catalog']['product'] as $key=>$product){
$flag = TRUE;
$node = new stdClass;
$if_exist_nid = db_result(db_query("SELECT nid FROM node WHERE title = '%s'",$product['name']));
if($if_exist_nid > 0){
$node->nid = $if_exist_nid;
}else{
$node->nid = '';
}

$node->type = 'shop';
$node->uid = 1;
$node->status = 1;
$node->created = time();
$node->changed = time();
$node->title = $product['name'];// $product['programname'];
$node->name = 'admin';
$node->body = $product['description'];
if(stripos($filename,'ESPRIT_US',0)){
$str = $product['advertisercategory'];
$term_str = explode('>',$str);
if(count($term_str) > 0){
$node->taxonomy = getStringToTaxonomy($term_str);//change
}else{
$node->taxonomy = 83;// map this product into Uncategories category
}
}else if(stripos($filename,'Adidas',0)){
//$node->taxonomy = adidasStringToTaxonomy($product['advertisercategory']);//change
//ADIDAS/APPAREL MENS/TRAINING/SHORT SLEEVE TOPS
$str = $product['advertisercategory'];
$term_strs = explode('/',$str);
if(count($term_strs) > 0){
$cat = explode(' ',$term_strs[1]);
$main_cat = strtolower($cat[0]); // Main category
if($main_cat == 'accessory')
$main_cat = 'accessories';
$term_str[2] = $main_cat; // Main category
$gender_cat = strtolower($cat[1]);
if($gender_cat == 'mens')
$gender_cat = 'men';
$term_str[0] = $gender_cat; // Gender
$term_str[3] = strtolower($term_strs[3]); // Product category

$node->taxonomy = getStringToTaxonomy($term_str);//change
}else{
$node->taxonomy = 83;// map this product into Uncategories category
}
}

$node->pathauto_perform_alias = 1;
$node->field_pageurl[0]['value'] = $product['programurl'];
// Date
$node->field_date[0]['value'] = date('Y-m-d', strtotime($product['lastupdated']));//change
$node->field_date[0]['value2'] = date('Y-m-d', strtotime($product['lastupdated']));//change
$node->field_date[0]['timezone'] = 'UTC';
$node->field_date[0]['offset'] = 0;
$node->field_date[0]['offset2'] = 0;
$node->field_date[0]['timezone_db'] = 'UTC';
$node->field_date[0]['date_type'] = 'date';
$node->field_name[0]['value'] = $product['name'];
$node->field_description[0]['value'] = $product['description'];
$node->field_sku[0]['value'] = $product['sku'];
$node->field_upc[0]['value'] = $product['upc'];
$node->field_currency[0]['value'] = $product['currency'];
$node->field_saleprice[0]['value'] = $product['saleprice'];
$node->field_product_price[0]['value']= $product['price'];
$node->field_url[0]['value'] = $product['buyurl'];
$node->field_shop_image[0]['value'] = $product['imageurl'];
$node->field_url[0]['title'] = $product['name'];
$node->field_url[0]['url'] = $product['buyurl'];
$node->field_description[0]['value'] = $product['description'];
$node->field_instock[0]['value'] = $product['instock'];

node_save($node);
$i++;
//if($key==2){
// break;
//}
// $product_info_es[] = $product;
// $product_info_es1[] = $node;
}
/*$f = fopen('sites/default/files/textfiles/espirit - Adidas .txt','a+');
fwrite($f,'----------------------\n\n\\n');
fwrite($f,print_r($product_info_es,true));
fwrite($f,'----------------------\n\n\\n');
fwrite($f,'----------!!!!!!!!!!!!!!!!!!!!!!!!!!!!------------\n\n\\n');
fwrite($f,print_r($product_info_es1,true));
fclose($f);*/
}

/**
*
*
*
*/
if(stripos($filename,'CoutureCandy_com',0) || stripos($filename,'Finishline_com',0)){// if Brands are Espirit or Adidas
$xmlarray = xml2array(file_get_contents($xml_filepath));
foreach($xmlarray['catalog']['product'] as $key=>$product){
$flag = TRUE;
$node = new stdClass;
$if_exist_nid = db_result(db_query("SELECT nid FROM node WHERE title = '%s'",$product['name']));
if($if_exist_nid > 0){
$node->nid = $if_exist_nid;
}else{
$node->nid = '';
}

$node->type = 'shop';
$node->uid = 1;
$node->status = 1;
$node->created = time();
$node->changed = time();
$node->title = $product['name'];// $product['programname'];
$node->name = 'admin';
$node->body = $product['description'];

if(stripos($filename,'CoutureCandy_com',0)){
/*
Women > Trend - 70s Revival
Women > Denim > Dark Wash Denim
Women > Denim > Medium Wash Denim
Women > Accessories > Jewelry > Necklaces
*/
$str = explode('>',strtolower($str));
if(count($str) > 0){
$term_str[2] = $str[2]; // Main category
$term_str[0] = $str[0]; // Gender
$term_str[3] = $str[3]; // Product category
$node->taxonomy = getStringToTaxonomy($term_str);//change
}else{
$node->taxonomy = 83;// map this product into Uncategories category
}

}else if(stripos($filename,'Finishline_com',0)){
/* Womens Apparel Jackets
Womens Apparel Pants
*/

$str = $product['advertisercategory'];
$cat = explode(' ',$str);
if(count($cat) > 0){
$main_cat = strtolower($cat[1]); // Main category
$term_str[2] = $main_cat; // Main category
$gender_cat = strtolower($cat[0]);
if($gender_cat == 'mens')
$gender_cat = 'men';
if($gender_cat == 'womens')
$gender_cat = 'women';
$term_str[0] = $gender_cat; // Gender
$term_str[3] = strtolower($cat[2]); // Product category

$node->taxonomy = getStringToTaxonomy($term_str);//change
}else{
$node->taxonomy = 83;// map this product into Uncategories category
}
}

$node->pathauto_perform_alias = 1;
$node->field_pageurl[0]['value'] = $product['programurl'];
// Date
$node->field_date[0]['value'] = date('Y-m-d', strtotime($product['lastupdated']));//change
$node->field_date[0]['value2'] = date('Y-m-d', strtotime($product['lastupdated']));//change
$node->field_date[0]['timezone'] = 'UTC';
$node->field_date[0]['offset'] = 0;
$node->field_date[0]['offset2'] = 0;
$node->field_date[0]['timezone_db'] = 'UTC';
$node->field_date[0]['date_type'] = 'date';
$node->field_name[0]['value'] = $product['name'];
$node->field_description[0]['value'] = $product['description'];
$node->field_sku[0]['value'] = $product['sku'];
$node->field_upc[0]['value'] = $product['upc'];
$node->field_currency[0]['value'] = $product['currency'];
$node->field_saleprice[0]['value'] = $product['saleprice'];
$node->field_product_price[0]['value']= $product['price'];
$node->field_url[0]['value'] = $product['buyurl'];
$node->field_shop_image[0]['value'] = $product['imageurl'];
$node->field_url[0]['title'] = $product['name'];
$node->field_url[0]['url'] = $product['buyurl'];
$node->field_description[0]['value'] = $product['description'];
$node->field_instock[0]['value'] = $product['instock'];

node_save($node);
$i++;
//if($key==2){
// break;
//}
//$product_info_couter[] = $product;
// $product_info_couter1[] = $node;
}
/* $f = fopen('sites/default/files/textfiles/couture - Finishline .txt','a+');
fwrite($f,'----------------------\n\n\\n');
fwrite($f,print_r($product_info_couter,true));
fwrite($f,'----------------------\n\n\\n');
fwrite($f,'----------!!!!!!!!!!!!!!!!!!!!!!!!!!!!------------\n\n\\n');
fwrite($f,print_r($product_info_couter1,true));

fclose($f);*/
}
/*
*
* End of XML file
*/

/*
* Start to import Link share xml file
* cmp.xml
*/

if(stripos($filename,'cmp',0) || stripos($filename,'cmp',0)){// if Brands are Espirit or Adidas
$xmlarray = xml2array(file_get_contents($xml_filepath));
foreach($xmlarray['merchandiser']['product'] as $key=>$product){
$flag = TRUE;
$node = new stdClass;
$if_exist_nid = db_result(db_query("SELECT nid FROM node WHERE title = '%s'",$product['keywords']));
if($if_exist_nid > 0){
$node->nid = $if_exist_nid;
}else{
$node->nid = '';
}

$node->type = 'shop';
$node->uid = 1;
$node->status = 1;
$node->created = time();
$node->changed = time();
$node->title = $product['keywords'];// $product['programname'];
$node->name = 'admin';
$node->body = $product['brand'];//this is keyword of product that is to be brand name
if(stripos($filename,'cmp',0)){
/*
[category] => Array(
[primary] => Beauty // Main category
[secondary] => Special Shops - Men's Grooming // Product category
)
*
* attributeClass => Array(
* [Gender] => Male // Sub category
* )
*
*/

$term_str[2] = strtolower($product['category']['primary']); // Main category
if($product['attributeClass']['Gender'] == 'Male')
$gender_cat = 'men';
if($product['attributeClass']['Gender'] == 'Female')
$gender_cat = 'women';
$term_str[0] = $gender_cat; // Gender
$product_cat_str = explode('-', strtolower($product['category']['secondary']));
$term_str[3] = $product_cat_str[0]; // Product category
$node->taxonomy = getStringToTaxonomy($term_str);//change
}

$node->pathauto_perform_alias = 1;
$node->field_pageurl[0]['value'] = $product['URL']['product'];
// Date
$node->field_date[0]['value'] = date('Y-m-d', strtotime(date("m.d.y")));//change
$node->field_date[0]['value2'] = date('Y-m-d', strtotime(date("m.d.y")));//change
$node->field_date[0]['timezone'] = 'UTC';
$node->field_date[0]['offset'] = 0;
$node->field_date[0]['offset2'] = 0;
$node->field_date[0]['timezone_db'] = 'UTC';
$node->field_date[0]['date_type'] = 'date';
$node->field_name[0]['value'] = $product['keywords'];
$node->field_description[0]['value'] = $product['description']['long'];
$node->field_sku[0]['value'] = $product['0_attr']['sku_number']; // sku not available in XML
$node->field_upc[0]['value'] = $product['upc'];
$node->field_currency[0]['value'] = $product['price_attr']['currency'];
$node->field_saleprice[0]['value'] = $product['price']['sale'];
$node->field_product_price[0]['value']= $product['price']['retail'];
$node->field_url[0]['value'] = $product['URL']['product'];
$node->field_shop_image[0]['value'] = $product['URL']['productImage'];
$node->field_url[0]['title'] = $product['keywords'];
$node->field_url[0]['url'] = $product['URL']['product'];
$node->field_description[0]['value'] = $product['description']['long'];
$node->field_instock[0]['value'] = $product['shipping']['availability'];

node_save($node);
$i++;
//if($key==2){
// break;
//}
// $product_info_cmp[] = $product;
//$product_info_cmp1[] = $node;
}

/* $f = fopen('sites/default/files/textfiles/cmp - xmlarray.txt','w');
fwrite($f,'----------------------\n\n\\n');
fwrite($f,print_r($product_info_cmp,true));
fwrite($f,'----------------------\n\n\\n');
fwrite($f,'----------!!!!!!!!!!!!!!!!!!!!!!!!!!!!------------\n\n\\n');
fwrite($f,print_r($product_info_cmp1,true));

fclose($f); */

}
/*
* End of cmp xml
*/
}
}
}
return $flag;
}
/**
*
*/
function _check_existing_word($subject,$matching_word){
$subject=strtolower($subject);
$matching_word=strtolower($matching_word);
$pattern = '/\b' . $matching_word . '\b/';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);
if(count($matches) > 0){
return TRUE;
}
return FALSE;
}

/* custom function for getting tid for relevant term name */
function _get_tid($term_name,$parrent_tid = 0){
$term_name = strtolower($term_name);
// Check if term is already exists
$tid = db_result(db_query("SELECT td.* FROM term_data td INNER join term_hierarchy th ON th.tid = td.tid WHERE th.parent = %d AND LOWER(td.name) = '%s' AND td.vid = 2",$parrent_tid,$term_name));
// Get synonyms category if exists for matching category
$query = db_query("SELECT * FROM synonyms_words");
while($result = db_fetch_object($query)){
$syn_arr[strtolower($result->name_synonyms_words)] = $result->matching_cat_tid;
}
if($tid > 0){
return $tid;
}else if($syn_arr[$term_name] > 0){
return $syn_arr[$term_name];
}else{
return FALSE;
}
}

/**
* Implementation of espiritStringToTaxonomy($str)
* File Name: 10369777-ESPRIT_US.xml
*/
function getStringToTaxonomy($term_str){
// 2-women>all products>1-jewelry>3-bracelets/bangle
$main_category = strtolower($term_str[2]); // Product main category in menu bar
$child_category_level1 = strtolower(implode(' or ',explode('/',$term_str[0]))); // Men or Women
$child_category_level2 = strtolower(implode(' or ',explode('/',$term_str[3]))); // Product category
// Initialize taxonomy array()
$taxonomy = array();
//Initialize main category
$tid = _get_tid($main_category,0);
if($tid > 0){
//echo $tid;exit;
$taxonomy[2][] = $tid;
$parrent_tid = $tid;
// Initialize child level first category
$tid = _get_tid($child_category_level1,$parrent_tid);
if($tid > 0){
$taxonomy[2][] = $tid;
$parrent_tid = $tid;
// Initialize child level second category
$tid = _get_tid($child_category_level2,$parrent_tid);
if($tid > 0){
$taxonomy[2][] = $tid;
}else{ // Add new taxonomy by existing name
$term = array(
'vid' => 2,
'name' => ucfirst($child_category_level2),
'parent' => $parrent_tid,
);
taxonomy_save_term($term);
$new_inserted_tid = db_result(db_query("SELECT MAX(tid) FROM term_data"));
$taxonomy[2][] = $new_inserted_tid;
}
}elseif($child_category_level1 == 'unisex'){
// set this product in both (men,women) category
// for men
$parrent_tid_new = $parrent_tid;
$tid = _get_tid('men',$parrent_tid_new);
if($tid > 0){
$taxonomy[2][] = $tid; // men tid
$parrent_tid = $tid;
$tid = _get_tid($child_category_level2,$parrent_tid);
if($tid > 0){
$taxonomy[2][] = $tid;
}else{ // Add new taxonomy by existing name
$term = array(
'vid' => 2,
'name' => ucfirst($child_category_level2),
'parent' => $parrent_tid,
);
taxonomy_save_term($term);
$new_inserted_tid = db_result(db_query("SELECT MAX(tid) FROM term_data"));
$taxonomy[2][] = $new_inserted_tid;
}
}

// for women
$tid = _get_tid('women',$parrent_tid_new);
if($tid > 0){
$taxonomy[2][] = $tid; // men tid
$parrent_tid = $tid;
$tid = _get_tid($child_category_level2,$parrent_tid);
if($tid > 0){
$taxonomy[2][] = $tid;
}else{ // Add new taxonomy by existing name
$term = array(
'vid' => 2,
'name' => ucfirst($child_category_level2),
'parent' => $parrent_tid,
);
taxonomy_save_term($term);
$tid = db_result(db_query("SELECT MAX(tid) FROM term_data"));
$taxonomy[2][] = $tid;
}
}
}else{
// Add this product to uncategorize category
$taxonomy[2][] = 83; // 83 = tid of Uncategories term's id
}
}else{
// Add this product to uncategorize category
$taxonomy[2][] = 83; // 83 = tid of Uncategories term's id
}
return $taxonomy;
}
/**
* Implementation of xml2array($contents, $get_attributes=1, $priority = 'tag')
*/
function xml2array($contents, $get_attributes=1, $priority = 'tag'){
if(!$contents) return array();

if(!function_exists('xml_parser_create')) {
//print "'xml_parser_create()' function not found!";
return array();
}

//Get the XML parser of PHP - PHP must have this module for the parser to work
$parser = xml_parser_create('');
xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); # http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodi...
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, trim($contents), $xml_values);
xml_parser_free($parser);

if(!$xml_values) return;//Hmm...

//Initializations
$xml_array = array();
$parents = array();
$opened_tags = array();
$arr = array();

$current = &$xml_array; //Refference

//Go through the tags.
$repeated_tag_index = array();//Multiple tags with same name will be turned into an array
foreach($xml_values as $data) {
unset($attributes,$value);//Remove existing values, or there will be trouble

//This command will extract these variables into the foreach scope
// tag(string), type(string), level(int), attributes(array).
extract($data);//We could use the array by itself, but this cooler.

$result = array();
$attributes_data = array();

if(isset($value)) {
if($priority == 'tag') $result = $value;
else $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode
}

//Set the attributes too.
if(isset($attributes) and $get_attributes) {
foreach($attributes as $attr => $val) {
if($priority == 'tag') $attributes_data[$attr] = $val;
else $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
}
}

//See tag status and do the needed.
if($type == "open") {//The starting of the tag ''
$parent[$level-1] = &$current;
if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag
$current[$tag] = $result;
if($attributes_data) $current[$tag. '_attr'] = $attributes_data;
$repeated_tag_index[$tag.'_'.$level] = 1;

$current = &$current[$tag];

} else { //There was another element with the same tag name

if(isset($current[$tag][0])) {//If there is a 0th element it is already an array
$current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
$repeated_tag_index[$tag.'_'.$level]++;
} else {//This section will make the value an array if multiple tags with the same name appear together
$current[$tag] = array($current[$tag],$result);//This will combine the existing item and the new item together to make an array
$repeated_tag_index[$tag.'_'.$level] = 2;

if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
$current[$tag]['0_attr'] = $current[$tag.'_attr'];
unset($current[$tag.'_attr']);
}

}
$last_item_index = $repeated_tag_index[$tag.'_'.$level]-1;
$current = &$current[$tag][$last_item_index];
}

} elseif($type == "complete") { //Tags that ends in 1 line ''
//See if the key is already taken.
if(!isset($current[$tag])) { //New Key
$current[$tag] = $result;
$repeated_tag_index[$tag.'_'.$level] = 1;
if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data;

} else { //If taken, put all things inside a list(array)
if(isset($current[$tag][0]) and is_array($current[$tag])) {//If it is already an array...

// ...push the new element into that array.
$current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;

if($priority == 'tag' and $get_attributes and $attributes_data) {
$current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
}
$repeated_tag_index[$tag.'_'.$level]++;

} else { //If it is not an array...
$current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value
$repeated_tag_index[$tag.'_'.$level] = 1;
if($priority == 'tag' and $get_attributes) {
if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well

$current[$tag]['0_attr'] = $current[$tag.'_attr'];
unset($current[$tag.'_attr']);
}

if($attributes_data) {
$current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
}
}
$repeated_tag_index[$tag.'_'.$level]++; //0 and 1 index is already taken
}
}

} elseif($type == 'close') { //End of tag ''
$current = &$parent[$level-1];
}
}

return($xml_array);
}

<?php
/**
* Implementation of xml_import_menu()
*/
function xml_import_menu(){
$items['admin/content/xml-import'] = array(
'title' => 'XML Import',
'page callback' => 'drupal_get_form',
'page arguments' => array('xml_import'),
'access arguments' => array('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
'file' => 'custom-xml-import-by-file-upload.inc',
);
$items['admin/content/xml-imports'] = array(
'title' => 'Import XML by FTP',
'page callback' => 'drupal_get_form',
'page arguments' => array('xml_import_confirm'),
'access arguments' => array('administer site configuration'),
'file' => 'custom-xml-import-by-ftp-upload.inc',
'weight' => 1,
'type' => MENU_NORMAL_ITEM
);
return $items;
}

rahul_singh’s picture

rahul_singh’s picture

hi

rahul_singh’s picture

hi

rahul_singh’s picture

Title: About the local person details » We want to hw to use the third party api in drupal website ..................

we want to import the Xml file in our site hw we do codes for that if any one have example code then plz attach with this

budda’s picture

Status: Postponed » Postponed (maintainer needs more info)

What is a hw?

Your request makes little sense.

psynaptic’s picture

Version: » 6.x-1.0-alpha1
Status: Postponed (maintainer needs more info) » Closed (cannot reproduce)

This is outdated and doesn't make sense anyway.. closing.