View difference between Paste ID: PiqF99Gn and WfBfMUbw
SHOW: | | - or go back to the newest paste.
1
 
2
  
3
global $wp_query;
4
$etape = $wp_query->query['impetape'];
5
$valeur = $wp_query->query['impvaleur'];
6
ini_set("max_execution_time", 0); 
7
	echo '######## ETAPE : SQL ############ <br />';
8
	
9
	
10-
	$sql = 'SELECT id, traites, nb_fichiers FROM suivi_cron WHERE dateexec LIKE "'.date("Y-m-d").'%" AND etatcron="sql"';
10+
11-
	$sth = $dbh->query($sql);
11+
12-
	$r = $sth->fetchObject();
12+
13-
	if(!$r){
13+
14-
		echo 'Aucun fichier partenaire téléchargé <br />';
14+
15-
		exit;
15+
16-
	}
16+
17-
	$idsuivi = $r->id;
17+
18-
	if($r->traites==$r->nb_fichiers){
18+
19-
		echo 'Tout les produits ont été importés <br />';
19+
20-
		$sql = 'UPDATE suivi_cron SET etatcron="fini" WHERE id='.$idsuivi;
20+
21-
		$dbh->exec($sql);
21+
22
			
23-
		$suppr=0;
23+
24-
		$sql = 'SELECT count(*) as n FROM produits WHERE date_maj<'.date(Ymd);
24+
25-
		$sth = $dbh->query($sql);
25+
26-
		$r = $sth->fetchObject();
26+
27-
		$suppr = $r->n;
27+
28-
		$nbSuppr = 0;
28+
29-
		$sql = 'SELECT id_post FROM produits WHERE date_maj<'.date(Ymd);
29+
30-
		$sth = $dbh->query($sql);
30+
31-
		while($r = $sth->fetchObject()){
31+
32-
			wp_delete_post($r->id_post, true);
32+
33-
			$nbSuppr++;
33+
34-
		}
34+
35-
		$sql = 'DELETE FROM produits WHERE date_maj<'.date(Ymdhis);
35+
36-
		$dbh->exec($sql);
36+
37-
		echo $suppr.' produits &agrave; supprimer';
37+
38-
		echo '<br />';
38+
39-
		echo $nbSuppr.' produits supprim&eacute;s';
39+
40-
		echo '<br />';
40+
41-
		exit;
41+
42-
	}
42+
43-
	$n = $r->traites+1;
43+
44
			
45-
	$dom = new DomDocument();
45+
46
			Le mot suivant est la couleur
47-
	if(!$dom->load('import/flux'.$n.'.xml')){
47+
48-
		exit;
48+
49-
	}
49+
50-
	//$dom->load('import/source.xml');
50+
51-
	//$dom->load('http://flux.netaffiliation.com/catalogue.php?maff=2008ED97S3CE151D3F1161&type=xml&ordre=1,2,5,12,14,15,17,18,19,20,46,21,22,57,58,60,13&noms=');
51+
52
			$descriptif = str_replace('  ', ' ', $descriptif); //On supprime les espaces consécutifs (on le fait deux fois pour supprimer si il y en a 2, 3 ou plus.
53
			$descriptif = trim($descriptif);
54
			$chaineNom = strtolower($product->nom);
55
			//$chaineNom = str_replace('chaussures', '', $chaineNom);
56
			//$chaineNom = trim($chaineNom);
57
			$p1 = strpos($descriptif, $chaineNom);
58
			$p2 = strlen($chaineNom);
59
			$position = $p1+$p2;
60
			$descriptif = substr($descriptif, $position); //On récupère tout ce qui est APRES le nom du produit dans la descr.
61
			$supRub = strtolower($product->rubrori);
62
			//$supRub = str_replace('chaussures', '', $supRub);
63
			$descriptif = str_replace($supRub, '', $descriptif);
64
			$descriptif = trim(str_replace(strtolower($product->marque), '', $descriptif), ' . ');
65
			
66
			//On prend  la couleur qui est censée être le premier mot du descriptif maintenant
67
			$couleur = substr($descriptif, 0, strpos($descriptif, 'disponible en taille', 1));
68
			$descriptif = str_replace($couleur, '', $descriptif);//On l'enlève du descriptif
69
			
70
			$arrayTrad = array(
71
				'army'=>'Militaire',
72
				'azure blue'=>'Bleu azure',
73
				'bijou blue'=>'Bijou bleu',
74
				'black'=>'Noir',
75
				'black olive'=>'Olive noire',
76
				'blackberry'=>'Mûre',
77
				'blue'=>'Bleu',
78
				'bluebeach'=>'Bleu océan',
79
				'bone'=>'Os',
80
				'brindle'=>'Bringé',
81
				'brown'=>'Marron',
82
				'chocolate'=>'Chocolat',
83
				'dark-brown'=>'Marron',
84
				'emerald'=>'Emeraude',
85
				'fuchsia'=>'Fushia',
86
				'khaki'=>'Kaki',
87
				'pink'=>'Rose',
88
				'roses'=>'Rose',
89
				'yellow'=>'Jaune',
90
				'white'=>'Blanc'
91
				);
92
			if(strpos($couleur, '/')!==false){
93
				$couleur = split('/', $couleur);
94
			}else{
95
				$couleur = array($couleur);
96
			}
97
			
98
			$taxColor = array(); //Tableau qui permettra de faire le lien entre les tailles et le produit dans tax_input
99
			
100
			foreach($couleur as $color){
101
				$color = ucfirst(strtolower(trim($color)));
102
				$color_san = sanitize_title($color);
103
				if(array_key_exists($color_san, $arrayTrad)){
104
					$color = $arrayTrad[$color_san];
105
					$color_san = $color;
106
				}
107
				if(!term_exists($color, 'couleur')){
108
					wp_insert_term($color, 'couleur', array(
109
						'description'=>$color,
110
						'slug'=>$color_san));
111
				}
112
				array_push($taxColor, $color);
113
			}
114
			
115
			//La catégorie :
116
			$categorie = trim(substr($descriptif, 0, strpos($descriptif, '.')));
117
			
118
			$descriptif = trim(str_replace($categorie.'.', '', $descriptif), ' . ');
119
			
120
			$categorie = trim(str_replace('disponible en taille', '', $categorie));
121
			$categorie = strtolower($categorie);
122
			
123
			$taxCat = array(); //Tableau qui permettra de faire le lien entre les tailles et le produit dans tax_input
124
			
125
			/*if(!term_exists($categorie, 'categorie')){
126
				wp_insert_term($categorie, 'categorie', array(
127
					'description'=> $categorie,
128
					'slug'=> sanitize_title($categorie)));
129
			}
130
			array_push($taxCat, $categorie);*/
131
			
132
			//On traite la sous catégorie pour les menus :
133
			$rubori = strtolower($product->rubrori);
134
			$rubori = str_replace('chaussures', '', $rubori);
135
			$rubori = str_replace(strtolower($product->marque), '', $rubori);
136
			$splitCat = split('>', $rubori);
137
			$parentCat = 0; 
138
			foreach($splitCat as $cat){
139
				$cat = trim($cat);
140
				echo 'CATEGORIE : '.$cat.' <br />';
141
				if(sizeof($cat)>3){
142
					$idCat = term_exists(ucfirst($cat), 'cat_xml');
143
					if(!$idCat){
144
						$idCat = wp_insert_term(ucfirst($cat), 'cat_xml', array(
145
							'description'=>ucfirst($categorie).' '.sanitize_title($cat),
146
							'slug'=>sanitize_title($cat),
147
							'parent'=>intval($parentCat)));
148
						echo 'On ajoute la catégorie '.$cat.' dont le parent est '.$parentCat.' <br />';
149
					}
150
					//$parentCat = intval($idCat['term_id']);
151
					//$tax = term_exists(ucfirst($cat), 'categorie');
152
					$categorie = $cat; //La dernière categorie est celle que l'on doit tagger pour le produit
153
				}
154
			}
155
			
156
			/*$arrayParents = array(10614=>'homme', 10615=>'femme', 10616=>'enfant');
157
			$arrayCategories['femme']=array('boots');
158
			//$cat est le dernier morceau
159
			if(!empty($cat) && !in_array($cat, $arrayCategories[$splitCat[0]])){
160
				echo 'Catégorie non-traitée : '.$splitCat[0].'=>'.$cat.'<br />';
161
				continue;
162
				echo 'Continue foiré <br />';
163
			}
164
			array_push($taxCat, ucfirst($categorie));*/
165
					
166
			/*$categories = get_terms( 'wpsc_product_category', 'hide_empty=0&parent=' . $category_id );
167
			$idCat = 0;
168
			foreach ( $categories as $category ) {
169
				if($categorie==$category->name){
170
					$idCat = $category->term_id;
171
				}
172
			}*/
173
			/*if($idCat==0){
174
				wp_insert_term(ucfirst($product->categorie), 'wpsc_product_category', array(
175
						'description'=>$product->categorie,
176
						'slug'=>sanitize_title($product->categorie)));	
177
				}
178
			}*/
179
			
180
			
181
			//La taille :
182
			if( strpos($descriptif, '.') ){
183
				$taille = trim(substr($descriptif, 0, strpos($descriptif, '.')));
184
			}else{
185
				$taille = trim(substr($descriptif, 0));
186
			}
187
			
188
			$descriptif = trim(str_replace($taille, '', $descriptif), ' . ');
189
			
190
			$split = split(',', $taille);
191
			
192
			$taxTaille = array(); //Tableau qui permettra de faire le lien entre les tailles et le produit dans tax_input
193
			
194
			foreach($split as $taille){
195
				$taille = trim(strtolower($taille));
196
				
197
				if(strpos($taille, '1/2')===false){ //Si la pointure est de forme 18 1/2 on ne fait pas de split
198
					$s2 = split('/', $taille);
199
				}else{								//Sinon elle peut être de forme 18 ou 18/19, donc on split
200
					$s2 = array($taille);
201
				}
202
				foreach($s2 as $t){
203
					$t = trim($t);
204
					if(intval($t)>0){			//La taille est forcément un chiffre
205
						if(!term_exists($t, 'taille')){
206
							wp_insert_term($t, 'taille', array(
207
								'description'=>'Pointure '.$t,
208
								'slug'=>sanitize_title($t)));	
209
						}
210
						array_push($taxTaille, $t);
211
					}
212
				}
213
			}
214
			
215
			
216
					
217
			$nom = str_replace(strtolower($product->marque), '', strtolower($product->nom));
218
			$nom = str_replace(strtolower($product->rubrori), '', $nom);
219
			$nom = str_replace(strtolower($product->rubrique), '', $nom);
220
			
221
			$nom_san = sanitize_title($nom);
222
			
223
			$sql = 'SELECT id_post FROM produits WHERE idneta='.$product->idneta.' AND id_partenaire=1';
224
			$sth = $dbh->query($sql);
225
			$r = $sth->fetchObject();
226
			if($r->id_post){
227
				$updateReq = true;
228
				$idPost = $r->id_post;
229
			}else{
230
				$idPost = false;
231
			}
232
			
233
			$descriptif .= '<br /> <img src="'.$product->image400.'" />';
234
			
235
			
236
			
237
			echo 'Ajout article : '.$nom.' Nom san : '.$nom_san.' '.$categorie.'<br />';
238
			$my_post = array(
239
			 'ID' => $idPost,
240
			 'post_title' => $nom,
241
			 'post_name' => $nom_san,
242
			 'post_content' => $descriptif,
243
			 'post_status' => 'publish',
244
			 'post_author' => 1,
245
			 'post_type' => 'produit',
246
			 'post_parent'=>0,
247
				'tax_input' => array('couleur'=>$taxColor, 'taille'=>$taxTaille, 'cat_xml'=>$categorie, 'marque'=>$taxMarque)
248
			);
249
			
250
			
251
			$lastID = wp_insert_post( $my_post );//*/
252
				
253
			if($updateReq===false){
254
			// Insert the post into the database
255
				$sql = 'INSERT INTO produits (id_post, ean, idneta, id_partenaire) VALUES ("'.$lastID.'", "'.$product->ean.'", "'.$product->idneta.'", 1)';
256
				$dbh->exec($sql);
257
				$insert++;
258
			}else{
259
				$sql = 'UPDATE produits SET ean="'.$product->ean.'", idneta="'.$product->idneta.'" WHERE id='.$r->id_post;
260
				$dbh->exec($sql);
261
				$update++;
262
			}//*/
263
			
264
			$i++;
265
		}//if strpos chaussure
266
	}