<commit_before><?php

namespace Biopen\GeoDirectoryBundle\Services;
 
use Doctrine\ODM\MongoDB\DocumentManager;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\ProgressBar;
use Biopen\GeoDirectoryBundle\Document\Element;
use Biopen\GeoDirectoryBundle\Document\ElementStatus;
use Biopen\GeoDirectoryBundle\Document\ModerationState;
use Biopen\GeoDirectoryBundle\Document\Coordinates;
use Biopen\GeoDirectoryBundle\Document\OptionValue;

class ImportColibrisLmcService
{    
	private $em;
	private $mappingTableIds;
	private $converter;
	private $geocoder;
	private $mainsCategories;

	/**
     * Constructor
     */
    public function __construct(DocumentManager $documentManager, $converter, $geocoder)
    {
    	 $this->em = $documentManager;
    	 $this->converter = $converter;
    	 $this->geocoder = $geocoder->using('google_maps');
    }

	public function import($fileName, $geocode = false, OutputInterface $output = null)
	{
	  // Getting php array of data from CSV
	  $data = $this->converter->convert($fileName, ',');

	  $this->createOptionsMappingTable();	
	  
	  // Define the size of record, the frequency for persisting the data and the current index of records
	  $size = count($data);
	  $batchSize = 50;
	  $i = 1;
	  
	  if ($output) 
	  {
		  $output->writeln('Element to import length : ' . count($data));
		  // Starting progress
		  $progress = new ProgressBar($output, $size);
		  $progress->start();
	  }

	  foreach($data as $row) 
	  {
	      $new_element = new Element();

	      $new_element->setName($row['Nom']);	      

		   $address = $row['Address'] ? $row['Address'] . ', ' : '';
		   $address .= $row['Code postal'] ? $row['Code postal'] . ' ' : '';
		   $address .= $row['Ville']; 

	      $new_element->setAddress($address);
	      $new_element->setPostalCode($row['Code postal']);         
	      $new_element->setDescription($row['Description']);
	      if (strlen($row['Téléphone']) >= 9) $new_element->setTel($row['Téléphone']);
	      
	      if ($row['Site web'] != 'http://' && $row['Site web'] != "https://") $new_element->setWebSite($row['Site web']);
	      
	      $new_element->setMail($row['Email de contact (export uniquement)']);
	      $new_element->setOpenHoursMoreInfos($row['Horaires']);
	      $new_element->setStatus(ElementStatus::AddedByAdmin);

	      $lat = 0;
			$lng = 0;

	      if (strlen($row['Lattitude']) > 2 && strlen($row['Longitude']) > 2)
	      {
		      $lat = $row['Lattitude'];
		      $lng = $row['Longitude'];
		   }
		   else
		   {		      
		      if ($geocode)
		      {
		      	try 
			      {
			      	$result = $this->geocoder->geocode($address)->first();
			      	$lat = $result->getLatitude();
			      	$lng = $result->getLongitude();	
			      }
			      catch (\Exception $error) { }      
		      }
		   } 

		   if ($lat == 0 || $lng == 0) $new_element->setModerationState(ModerationState::GeolocError);

		   $new_element->setCoordinates(new Coordinates((float)$lat, (float)$lng));
	      
	      $this->parseOptionValues($new_element, $row);

			// Persisting the current user<commit_msg>Adds sourceKey import<commit_after><?php

namespace Biopen\GeoDirectoryBundle\Services;
 
use Doctrine\ODM\MongoDB\DocumentManager;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\ProgressBar;
use Biopen\GeoDirectoryBundle\Document\Element;
use Biopen\GeoDirectoryBundle\Document\ElementStatus;
use Biopen\GeoDirectoryBundle\Document\ModerationState;
use Biopen\GeoDirectoryBundle\Document\Coordinates;
use Biopen\GeoDirectoryBundle\Document\OptionValue;

class ImportColibrisLmcService
{    
	private $em;
	private $mappingTableIds;
	private $converter;
	private $geocoder;
	private $mainsCategories;

	/**
     * Constructor
     */
    public function __construct(DocumentManager $documentManager, $converter, $geocoder)
    {
    	 $this->em = $documentManager;
    	 $this->converter = $converter;
    	 $this->geocoder = $geocoder->using('google_maps');
    }

	public function import($fileName, $geocode = false, OutputInterface $output = null)
	{
	  // Getting php array of data from CSV
	  $data = $this->converter->convert($fileName, ',');

	  $this->createOptionsMappingTable();	
	  
	  // Define the size of record, the frequency for persisting the data and the current index of records
	  $size = count($data);
	  $batchSize = 50;
	  $i = 1;
	  
	  if ($output) 
	  {
		  $output->writeln('Element to import length : ' . count($data));
		  // Starting progress
		  $progress = new ProgressBar($output, $size);
		  $progress->start();
	  }

	  foreach($data as $row) 
	  {
	      $new_element = new Element();

	      $new_element->setName($row['Nom']);	      

		   $address = $row['Address'] ? $row['Address'] . ', ' : '';
		   $address .= $row['Code postal'] ? $row['Code postal'] . ' ' : '';
		   $address .= $row['Ville']; 

	      $new_element->setAddress($address);
	      $new_element->setPostalCode($row['Code postal']);         
	      $new_element->setDescription($row['Description']);
	      if (strlen($row['Téléphone']) >= 9) $new_element->setTel($row['Téléphone']);
	      
	      if ($row['Site web'] != 'http://' && $row['Site web'] != "https://") $new_element->setWebSite($row['Site web']);
	      
	      $new_element->setMail($row['Email de contact (export uniquement)']);
	      $new_element->setOpenHoursMoreInfos($row['Horaires']);
	      $new_element->setStatus(ElementStatus::AddedByAdmin);
	      $new_element->setSourceKey($row['Source']);

	      $lat = 0;
			$lng = 0;

	      if (strlen($row['Lattitude']) > 2 && strlen($row['Longitude']) > 2)
	      {
		      $lat = $row['Lattitude'];
		      $lng = $row['Longitude'];
		   }
		   else
		   {		      
		      if ($geocode)
		      {
		      	try 
			      {
			      	$result = $this->geocoder->geocode($address)->first();
			      	$lat = $result->getLatitude();
			      	$lng = $result->getLongitude();	
			      }
			      catch (\Exception $error) { }      
		      }
		   } 

		   if ($lat == 0 || $lng == 0) $new_element->setModerationState(ModerationState::GeolocError);

		   $new_element->setCoordinates(new Coordinates((float)$lat, (float)$lng));
	      
	      $this->parseOptionValues($new_element, $row);

			// Persisting the current user
{'fraction_non_alphanumeric': 0.16057585825027684, 'fraction_numerical': 0.0031640563202024997, 'mean_word_length': 3.0787096774193548, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 62, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7151121', 'n_tokens_mistral': 2209, 'n_tokens_neox': 1967, 'n_words': 364}