<?php
namespace App\Controller\Services;
use App\AppBundle;
use Pimcore\Controller\Configuration\ResponseHeader;
use Pimcore\Controller\FrontendController;
use Pimcore\Model\Asset;
use SaintElmos\BaseBundle\Services\ImageResponsive;
use SaintElmos\BaseBundle\Templating\Helper\FrontendHelper;
use SaintElmos\BaseBundle\Utils\SETConf;
use SaintElmos\BaseBundle\Controller\BaseController;
use Symfony\Component\HttpFoundation\Request;
use \Pimcore\Model\DataObject;
class SchemaorgController extends FrontendController
{
public function jsonAction(Request $request,\Pimcore\Config\Config $websiteConfig)
{
//if ($this->editmode){
// $this->document->setProperty('snippet_exclude', 'bool', 'true')->save();
//}
// echo "<pre>";print_r($this->document->getParent());
$pimObject = $this->document->getEditable("business_workingDaysAndHours_objectGeolocationFolder");
$alias = "";
$openingHoursSpecification = [];
$weiter = false;
if(!empty($pimObject)){
$objectPath = (string) $pimObject;
$objectAlias = DataObject::getByPath(trim($objectPath));
if(!empty($objectAlias)){
$alias = AppBundle::getValue($objectAlias, 'getAlias');
}
$oID = $pimObject->getId();
$weiter = true;
}
if(!empty($alias))
{
$oID = $alias->getId();
$weiter = true;
}
if($weiter)
{
if ($oID)
{
$new_object = DataObject::getById($oID);
$days = array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
$timepattern = "/^[0-9]{1}[0-9]{1}:[0-5]{1}[0-9]{1}-[0-9]{1}[0-9]{1}:[0-5]{1}[0-9]{1}/";
$open_per_day = [];
//..get these values from the fields in Database for "this" store
$open_per_day[0] = AppBundle::getValue($new_object, 'getMonday'); // object_localized_data_15.monday
$open_per_day[1] = AppBundle::getValue($new_object, 'getTuesday'); // object_localized_data_15.tuesday
$open_per_day[2] = AppBundle::getValue($new_object, 'getWednesday'); // object_localized_data_15.wednesday
$open_per_day[3] = AppBundle::getValue($new_object, 'getThursday'); // object_localized_data_15.thursday
$open_per_day[4] = AppBundle::getValue($new_object, 'getFriday'); // object_localized_data_15.friday
$open_per_day[5] = AppBundle::getValue($new_object, 'getSaturday'); // object_localized_data_15.saturday
$open_per_day[6] = AppBundle::getValue($new_object, 'getSunday'); // object_localized_data_15.sunday
// put "ALL" times in "one" array
$times = [];
foreach ($open_per_day as $index => $value) {
$val = explode("|",$value);
foreach ($val as $v) {
$v = str_replace('–', '-', $v);
if (preg_match($timepattern, trim($v))) {
if(count($times)) {
$is_day_added = false;
foreach ($times as $t_index => $t) {
if($t['openHours'] == $v) {
$times[$t_index]['days'][] = $days[$index];
$is_day_added = true;
}
}
if($is_day_added === false) {
$times[] = [
"days" => [$days[$index]],
"openHours" => $v
];
}
} else {
$times[] = [
"days" => [$days[$index]],
"openHours" => $v
];
}
}
}
}
$openingHoursSpecificationArray = [];
// Iterate through each element in the $openingHoursArray
foreach ($times as $index => $entry) {
$object = [];
$object['@type'] = 'OpeningHoursSpecification';
foreach ($entry['days'] as $day) {
$object['dayOfWeek'][] = $day;
}
$times = explode("-", $entry['openHours']);
$object['opens'] = $times[0];
$object['closes'] = $times[1];
$openingHoursSpecificationArray[] = $object;
}
$openingHoursSpecification = $openingHoursSpecificationArray;
}
}
$parentAction = explode("::",$this->document->getParent()->getController());
$parentAction = str_replace("Action","",$parentAction[1]);
return $this->renderTemplate("services/Schemaorg/strukturierte-daten.html.twig", ["parentAction" => $parentAction, "openingHoursSpecification" => $openingHoursSpecification]);
}
}