| 
									
										
										
										
											2025-01-11 20:04:32 +01:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use Grocy\Helpers\BaseBarcodeLookupPlugin; | 
					
						
							|  |  |  | use GuzzleHttp\Client; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* | 
					
						
							|  |  |  | 	To use this plugin, configure it in data/config.php like this: | 
					
						
							|  |  |  | 	Setting('STOCK_BARCODE_LOOKUP_PLUGIN', 'OpenFoodFactsBarcodeLookupPlugin'); | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class OpenFoodFactsBarcodeLookupPlugin extends BaseBarcodeLookupPlugin | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-12 13:58:47 +01:00
										 |  |  | 	public const PLUGIN_NAME = 'Open Food Facts'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-11 20:04:32 +01:00
										 |  |  | 	protected function ExecuteLookup($barcode) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2025-02-03 18:55:33 +01:00
										 |  |  | 		$productNameFieldLocalized = 'product_name_' . substr(GROCY_LOCALE, 0, 2); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-11 20:04:32 +01:00
										 |  |  | 		$webClient = new Client(['http_errors' => false]); | 
					
						
							| 
									
										
										
										
											2025-02-03 18:55:33 +01:00
										 |  |  | 		$response = $webClient->request('GET', 'https://world.openfoodfacts.org/api/v2/product/' . preg_replace('/[^0-9]/', '', $barcode) . '?fields=product_name,image_url,' . $productNameFieldLocalized, ['headers' => ['User-Agent' => 'GrocyOpenFoodFactsBarcodeLookupPlugin/1.0 (https://grocy.info)']]); | 
					
						
							| 
									
										
										
										
											2025-01-11 20:04:32 +01:00
										 |  |  | 		$statusCode = $response->getStatusCode(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Guzzle throws exceptions for connection errors, so nothing to do on that here
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		$data = json_decode($response->getBody()); | 
					
						
							|  |  |  | 		if ($statusCode == 404 || $data->status != 1) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			// Nothing found for the given barcode
 | 
					
						
							|  |  |  | 			return null; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			$imageUrl = ''; | 
					
						
							|  |  |  | 			if (isset($data->product->image_url) && !empty($data->product->image_url)) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				$imageUrl = $data->product->image_url; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-12 13:58:47 +01:00
										 |  |  | 			// Take the preset user setting or otherwise simply the first existing location
 | 
					
						
							|  |  |  | 			$locationId = $this->Locations[0]->id; | 
					
						
							|  |  |  | 			if ($this->UserSettings['product_presets_location_id'] != -1) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				$locationId = $this->UserSettings['product_presets_location_id']; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// Take the preset user setting or otherwise simply the first existing quantity unit
 | 
					
						
							|  |  |  | 			$quId = $this->QuantityUnits[0]->id; | 
					
						
							|  |  |  | 			if ($this->UserSettings['product_presets_qu_id'] != -1) | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 				$quId = $this->UserSettings['product_presets_qu_id']; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-03 18:55:33 +01:00
										 |  |  | 			// Use the localized product name, if provided
 | 
					
						
							|  |  |  | 			$name = $data->product->product_name; | 
					
						
							| 
									
										
										
										
											2025-03-04 20:38:10 +01:00
										 |  |  | 			if (isset($data->product->$productNameFieldLocalized) && !empty($data->product->$productNameFieldLocalized)) | 
					
						
							| 
									
										
										
										
											2025-02-03 18:55:33 +01:00
										 |  |  | 			{ | 
					
						
							|  |  |  | 				$name = $data->product->$productNameFieldLocalized; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-04 20:38:10 +01:00
										 |  |  | 			// Remove non-ASCII characters in product name (whyever a product name should have them at all)
 | 
					
						
							| 
									
										
										
										
											2025-06-07 18:00:38 +02:00
										 |  |  | 			$name = preg_replace('/[^a-zA-Z0-9äöüÄÖÜß ]/', '', $name); | 
					
						
							| 
									
										
										
										
											2025-03-04 20:38:10 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-11 20:04:32 +01:00
										 |  |  | 			return [ | 
					
						
							| 
									
										
										
										
											2025-02-03 18:55:33 +01:00
										 |  |  | 				'name' => $name, | 
					
						
							| 
									
										
										
										
											2025-01-12 13:58:47 +01:00
										 |  |  | 				'location_id' => $locationId, | 
					
						
							|  |  |  | 				'qu_id_purchase' => $quId, | 
					
						
							|  |  |  | 				'qu_id_stock' => $quId, | 
					
						
							|  |  |  | 				'__qu_factor_purchase_to_stock' => 1, | 
					
						
							|  |  |  | 				'__barcode' => $barcode, | 
					
						
							|  |  |  | 				'__image_url' => $imageUrl | 
					
						
							| 
									
										
										
										
											2025-01-11 20:04:32 +01:00
										 |  |  | 			]; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |