From 21dfba7e2c667435839a7d7b9125a4682cd92e43 Mon Sep 17 00:00:00 2001
From: Sebastian Castro <sebastian.castro@protonmail.com>
Date: Sat, 3 Aug 2019 14:50:10 +0200
Subject: [PATCH] Element import openhours

---
 .../Document/DailyTimeSlot.php                | 25 ++++++-----
 .../GeoDirectoryBundle/Document/OpenHours.php | 43 +++++++++++++++----
 .../custom-fields/mapping-ontology.html.twig  |  1 +
 .../Services/ElementImportOneService.php      | 19 +++++++-
 web/test.json                                 | 11 ++++-
 5 files changed, 75 insertions(+), 24 deletions(-)

diff --git a/src/Biopen/GeoDirectoryBundle/Document/DailyTimeSlot.php b/src/Biopen/GeoDirectoryBundle/Document/DailyTimeSlot.php
index 8f2c0578f..30d9e4b83 100755
--- a/src/Biopen/GeoDirectoryBundle/Document/DailyTimeSlot.php
+++ b/src/Biopen/GeoDirectoryBundle/Document/DailyTimeSlot.php
@@ -9,7 +9,7 @@
  * @license    MIT License
  * @Last Modified time: 2018-01-19 13:05:00
  */
- 
+
 
 namespace Biopen\GeoDirectoryBundle\Document;
 
@@ -19,26 +19,29 @@ use JMS\Serializer\Annotation\Expose;
 /** @MongoDB\EmbeddedDocument */
 class DailyTimeSlot
 {
-	/** 
+	/**
     * @Expose
     * @MongoDB\Field(type="date") */
     private $slot1start;
-    /** 
+    /**
     * @Expose
     * @MongoDB\Field(type="date") */
     private $slot1end;
-    /** 
+    /**
     * @Expose
     * @MongoDB\Field(type="date") */
 	private $slot2start;
-    /** 
+    /**
     * @Expose
     * @MongoDB\Field(type="date") */
     private $slot2end;
 
-	public function __construct()
+	public function __construct($slot1start = null, $slot1end = null, $slot2start = null, $slot2end = null)
 	{
-		
+		$this->slot1start = $slot1start;
+        $this->slot1end = $slot1end;
+        $this->slot2start = $slot2start;
+        $this->slot2end = $slot2end;
 	}
 
     public function toJson()
@@ -55,7 +58,7 @@ class DailyTimeSlot
         //return date_format($this->slot1start, 'H:i');
         return $this->slot1start;
     }
-    
+
 
     public function getSlot2Start()
     {
@@ -68,7 +71,7 @@ class DailyTimeSlot
         //return date_format($this->slot1end, 'H:i');
         return $this->slot1end;
     }
-    
+
 
     public function getSlot2End()
     {
@@ -84,7 +87,7 @@ class DailyTimeSlot
         $this->slot1start = $slot;
         return $this;
     }
-    
+
 
     public function setSlot2Start($slot)
     {
@@ -97,7 +100,7 @@ class DailyTimeSlot
         $this->slot1end = $slot;
         return $this;
     }
-    
+
 
     public function setSlot2End($slot)
     {
diff --git a/src/Biopen/GeoDirectoryBundle/Document/OpenHours.php b/src/Biopen/GeoDirectoryBundle/Document/OpenHours.php
index 0be5f30f7..db8fb5b5c 100755
--- a/src/Biopen/GeoDirectoryBundle/Document/OpenHours.php
+++ b/src/Biopen/GeoDirectoryBundle/Document/OpenHours.php
@@ -9,7 +9,7 @@
  * @license    MIT License
  * @Last Modified time: 2018-01-19 13:04:59
  */
- 
+
 
 namespace Biopen\GeoDirectoryBundle\Document;
 
@@ -19,35 +19,60 @@ use JMS\Serializer\Annotation\Expose;
 /** @MongoDB\EmbeddedDocument */
 class OpenHours
 {
-	/** 
+	protected $days = ['Mo' => 'Monday', 'Tu' => 'Tuesday', 'We' => 'Wednesday', 'Th' => 'Thursday', 'Fr' => 'Friday', 'Sa' => 'Saturday', 'Sun' => 'Sunday'];
+
+	/**
 	* @Expose
 	* @MongoDB\EmbedOne(targetDocument="Biopen\GeoDirectoryBundle\Document\DailyTimeSlot") */
 	private $Monday;
-	/** 
+
+	/**
 	* @Expose
 	* @MongoDB\EmbedOne(targetDocument="Biopen\GeoDirectoryBundle\Document\DailyTimeSlot") */
 	private $Tuesday;
-	/** 
+	/**
 	* @Expose
 	* @MongoDB\EmbedOne(targetDocument="Biopen\GeoDirectoryBundle\Document\DailyTimeSlot") */
 	private $Wednesday;
-	/** 
+	/**
 	* @Expose
 	* @MongoDB\EmbedOne(targetDocument="Biopen\GeoDirectoryBundle\Document\DailyTimeSlot") */
 	private $Thursday;
-	/** 
+	/**
 	* @Expose
 	* @MongoDB\EmbedOne(targetDocument="Biopen\GeoDirectoryBundle\Document\DailyTimeSlot") */
 	private $Friday;
-	/** 
+	/**
 	* @Expose
 	* @MongoDB\EmbedOne(targetDocument="Biopen\GeoDirectoryBundle\Document\DailyTimeSlot") */
 	private $Saturday;
-	/** 
+	/**
 	* @Expose
 	* @MongoDB\EmbedOne(targetDocument="Biopen\GeoDirectoryBundle\Document\DailyTimeSlot") */
 	private $Sunday;
 
+	public function __construct($openHours)
+  {
+    foreach ($openHours as $day => $timeSlotString) {
+    	$slot1start = null; $slot1end = null; $slot2start = null; $slot2end = null;
+    	$slots = explode(',', $timeSlotString);
+    	if (count($slots) > 0) list($slot1start, $slot1end) = $this->buildSlotsFrom($slots[0]);
+    	if (count($slots) == 2) list($slot2start, $slot2end) = $this->buildSlotsFrom($slots[1]);
+    	$dailySlot = new DailyTimeSlot($slot1start, $slot1end, $slot2start, $slot2end);
+    	$method = 'set' . $this->days[$day];
+    	$this->$method($dailySlot);
+    }
+    dump($this);
+  }
+
+  private function buildSlotsFrom($string)
+  {
+  	$times = explode('-',$string);
+  	$start = date_create_from_format('H:i', $times[0]);
+  	$end = date_create_from_format('H:i', $times[1]);
+  	return [$start, $end];
+  }
+
 	public function toJson() {
 		$result = '{';
 		if ($this->Monday) $result .= '"Mo":' . $this->Monday->toJson() . ',';
@@ -141,7 +166,7 @@ class OpenHours
 	}
 
 	/*public function __construct()
-	{		
+	{
 	}
 
 	public setDailyTimeSlot($day,$plage1,$plage2)
diff --git a/src/Biopen/GeoDirectoryBundle/Resources/views/admin/core_custom/custom-fields/mapping-ontology.html.twig b/src/Biopen/GeoDirectoryBundle/Resources/views/admin/core_custom/custom-fields/mapping-ontology.html.twig
index 0aae4de16..4f10a0126 100644
--- a/src/Biopen/GeoDirectoryBundle/Resources/views/admin/core_custom/custom-fields/mapping-ontology.html.twig
+++ b/src/Biopen/GeoDirectoryBundle/Resources/views/admin/core_custom/custom-fields/mapping-ontology.html.twig
@@ -64,6 +64,7 @@
     coreData.push({id: 'email', text: "Email de l'élement"});
     coreData.push({id: 'owner', text: "Email de l'utilisateur propriétaire de la fiche"});
     coreData.push({id: 'source', text: "Origine de l'élément (source)"});
+    coreData.push({id: 'openHours', text: "Horaire d'ouvertues (format GoGoCarto)"});
 
     allProperties = $.map(coreData, function(el) { return el.id });
 
diff --git a/src/Biopen/GeoDirectoryBundle/Services/ElementImportOneService.php b/src/Biopen/GeoDirectoryBundle/Services/ElementImportOneService.php
index 742a0fda4..531f497af 100755
--- a/src/Biopen/GeoDirectoryBundle/Services/ElementImportOneService.php
+++ b/src/Biopen/GeoDirectoryBundle/Services/ElementImportOneService.php
@@ -7,6 +7,7 @@ use Biopen\GeoDirectoryBundle\Document\Element;
 use Biopen\GeoDirectoryBundle\Document\ElementStatus;
 use Biopen\GeoDirectoryBundle\Document\ModerationState;
 use Biopen\GeoDirectoryBundle\Document\Coordinates;
+use Biopen\GeoDirectoryBundle\Document\OpenHours;
 use Biopen\GeoDirectoryBundle\Document\Option;
 use Biopen\GeoDirectoryBundle\Document\OptionValue;
 use Biopen\GeoDirectoryBundle\Document\UserInteractionContribution;
@@ -21,7 +22,7 @@ class ElementImportOneService
 
 	protected $optionIdsToAddToEachElement = [];
 
-	protected $coreFields = ['id', 'name', 'categories', 'streetAddress', 'addressLocality', 'postalCode', 'addressCountry', 'latitude', 'longitude', 'images', 'owner', 'source'];
+	protected $coreFields = ['id', 'name', 'categories', 'streetAddress', 'addressLocality', 'postalCode', 'addressCountry', 'latitude', 'longitude', 'images', 'owner', 'source', 'openHours'];
 	protected $privateDataProps;
 
 	/**
@@ -125,6 +126,7 @@ class ElementImportOneService
 
 		$this->createCategories($element, $row, $import);
 		$this->createImages($element, $row);
+		$this->createOpenHours($element, $row);
 		$this->saveCustomFields($element, $row);
 
 		if ($import->isDynamicImport()) { // keep the same status for the one who were deleted
@@ -158,7 +160,6 @@ class ElementImportOneService
 	private function saveCustomFields($element, $raw_data)
 	{
 		$customFields = array_diff(array_keys($raw_data), $this->coreFields);
-		$customFields = array_diff($customFields, ['lat', 'long', 'lon', 'lng', 'title', 'nom', 'categories', 'address']);
 		$customData = [];
     foreach ($customFields as $customField) {
 			if ($customField && is_string($customField)) $customData[$customField] = $raw_data[$customField];
@@ -193,6 +194,20 @@ class ElementImportOneService
 		}
 	}
 
+	private function createOpenHours($element, $row)
+	{
+		if (!isset($row['openHours']) || !$this->isAssociativeArray($row['openHours'])) return;
+		$element->setOpenHours(new OpenHours($row['openHours']));
+	}
+
+	private function isAssociativeArray($a) {
+    if (!is_array($a)) return false;
+    foreach(array_keys($a) as $key)
+      if (!is_int($key)) return true;
+    return false;
+  }
+
+
 	function startsWith($haystack, $needle)
 	{
 	  $length = strlen($needle);
diff --git a/web/test.json b/web/test.json
index 691af0f5f..d75b00961 100644
--- a/web/test.json
+++ b/web/test.json
@@ -1,7 +1,7 @@
 {
   "licence": "https://opendatacommons.org/licenses/odbl/summary/",
   "source": "Cap ou pas Cap",
-	"graph":[{
+	"data":[{
 		"id":"Rhm",
 		"name":"Entropie",
 		"coord":{"latitude":45.18679,"longitude":5.70548},
@@ -12,7 +12,14 @@
 		  { "name": "Numérique", "index": 1}
 		],
 		"website": "https:\/\/www.asso-entropie.fr\/",
-		"images": ["https:\/\/www.capoupascap.info\/wp-content\/uploads\/2018\/12\/entropie.png"]
+		"images": ["https:\/\/www.capoupascap.info\/wp-content\/uploads\/2018\/12\/entropie.png"],
+		"openHours": {
+			"Tu": "09:30-19:30",
+			"We": "09:30-19:30",
+			"Th": "09:30-13:00,15:00-19:30",
+			"Fr": "09:30-19:30",
+			"Sa": "09:30-19:30"
+		}
 	},
 	{
 		"id":"Ki",
-- 
GitLab