public static function getEvents(eZContentObjectTreeNode $node, array $parameters)
 {
     $events = array();
     $base = array('name' => $node->attribute('name'), 'main_node_id' => $node->attribute('main_node_id'), 'main_url_alias' => $node->attribute('url_alias'), 'fields' => array('attr_from_time_dt' => 0, 'attr_to_time_dt' => 0));
     try {
         $startDate = new DateTime('now', OCCalendarData::timezone());
         $startDate->setDate(date('Y', $parameters['search_from_timestamp']), date('n', $parameters['search_from_timestamp']), date('j', $parameters['search_from_timestamp']));
         $endDate = clone $startDate;
         $endDate->add(new DateInterval($parameters['interval']));
         $byDayInterval = new DateInterval('P1D');
         /** @var DateTime[] $byDayPeriod */
         $byDayPeriod = new DatePeriod($startDate, $byDayInterval, $endDate);
         $timeTable = self::getTimeTableFromNode($node);
         foreach ($byDayPeriod as $date) {
             $weekDay = $date->format('w');
             if (isset($timeTable[$weekDay])) {
                 foreach ($timeTable[$weekDay] as $value) {
                     $newEvent = $base;
                     $date->setTime($value['from_time']['hour'], $value['from_time']['minute']);
                     $newEvent['fields']['attr_from_time_dt'] = $date->format('Y-m-d\\TH:i:s\\Z');
                     $date->setTime($value['to_time']['hour'], $value['to_time']['minute']);
                     $newEvent['fields']['attr_to_time_dt'] = $date->format('Y-m-d\\TH:i:s\\Z');
                     $item = OCCalendarItem::fromEzfindResultArray($newEvent);
                     $events[] = $item;
                 }
             }
         }
     } catch (Exception $e) {
         eZDebug::writeError($e->getMessage(), __METHOD__);
     }
     return $events;
 }
 public function fetch()
 {
     $startDateArray = array('hour' => '00', 'minute' => '00', 'second' => '00', 'month' => $this->parameters['month'], 'day' => $this->parameters['day'], 'year' => $this->parameters['year']);
     $originalStartDateTime = DateTime::createFromFormat('H i s n j Y', implode(' ', $startDateArray), self::timezone());
     $this->parameters['picker_date'] = date(self::PICKER_DATE_FORMAT, $originalStartDateTime->getTimestamp());
     if ($this->parameters['interval'] == self::INTERVAL_MONTH) {
         $startDateArray['day'] = 1;
     }
     $startDateTime = DateTime::createFromFormat('H i s n j Y', implode(' ', $startDateArray), self::timezone());
     if (!$startDateTime instanceof DateTime) {
         throw new Exception("Data non valida");
     }
     $interval = new DateInterval($this->parameters['interval']);
     if (!$interval instanceof DateInterval) {
         throw new Exception("Intervallo non valido");
     }
     // start day
     $this->parameters['timestamp'] = $startDateTime->getTimestamp();
     $this->parameters['days_of_month'] = date('t', $startDateTime->getTimestamp());
     $this->parameters['start_weekday'] = date('w', $startDateTime->getTimestamp());
     $endOfMonthArray = array_merge($startDateArray, array('day' => $this->parameters['days_of_month']));
     $endOfMonthDateTime = DateTime::createFromFormat('H i s n j Y', implode(' ', $endOfMonthArray), self::timezone());
     $this->parameters['end_weekday'] = date('w', $endOfMonthDateTime->getTimestamp());
     $fromTimeStamp = $startDateTime->format('U');
     $this->parameters['search_from_solr'] = ezfSolrDocumentFieldBase::preProcessValue($startDateTime->getTimestamp(), 'date');
     $this->parameters['search_from_timestamp'] = $startDateTime->getTimestamp();
     // end day
     $endDateTime = clone $startDateTime;
     $endDateTime->add($interval);
     $endTimeStamp = $endDateTime->format('U');
     $this->parameters['search_to_solr'] = ezfSolrDocumentFieldBase::preProcessValue($endDateTime->getTimestamp() - 1, 'date');
     $this->parameters['search_to_timestamp'] = $endDateTime->getTimestamp();
     $this->parameters['search_to_picker_date'] = date(self::PICKER_DATE_FORMAT, $endDateTime->getTimestamp());
     // filter
     $this->filters[] = array('or', 'attr_from_time_dt:[' . $this->parameters['search_from_solr'] . ' TO ' . $this->parameters['search_to_solr'] . ']', 'attr_to_time_dt:[' . $this->parameters['search_from_solr'] . ' TO ' . $this->parameters['search_to_solr'] . ']', array('and', 'attr_from_time_dt:[* TO ' . $this->parameters['search_from_solr'] . ']', 'attr_to_time_dt:[' . $this->parameters['search_to_solr'] . ' TO *]'));
     $facets = array();
     //esempio: $this->parameters['Materia'] = '"Economia e diritto"';
     foreach (self::relatedParameters() as $fieldIdentifier => $fieldName) {
         if (isset($this->parameters[$fieldName]) && $this->parameters[$fieldName] !== false) {
             if (is_array($this->parameters[$fieldName])) {
                 $orFilter = array('or');
                 foreach ($this->parameters[$fieldName] as $value) {
                     $filterValue = addcslashes($value, '"');
                     $orFilter[] = "subattr_{$fieldIdentifier}___name____s:\"{$filterValue}\"";
                 }
                 $this->filters[] = $orFilter;
             } else {
                 $filterValue = addcslashes($this->parameters[$fieldName], '"');
                 $this->filters[] = "subattr_{$fieldIdentifier}___name____s:\"{$filterValue}\"";
             }
         }
         $facets[] = array('field' => "subattr_{$fieldIdentifier}___name____s", 'name' => $fieldName, 'limit' => 100, 'sort' => 'alpha');
     }
     if (is_array($this->parameters['filter'])) {
         $this->filters = array_merge($this->filters, $this->parameters['filter']);
     }
     $sortBy = array('attr_priority_si' => 'desc', 'attr_special_b' => 'desc');
     if (class_exists('ezfIndexEventDuration')) {
         $sortBy['extra_event_duration_s'] = 'asc';
     }
     $sortBy['attr_from_time_dt'] = 'asc';
     $solrFetchParams = array('SearchOffset' => 0, 'SearchLimit' => 1000, 'Facet' => $facets, 'SortBy' => $sortBy, 'Filter' => $this->filters, 'SearchContentClassID' => null, 'SearchSectionID' => null, 'SearchSubTreeArray' => $this->parameters['subtree'], 'AsObjects' => false, 'SpellCheck' => null, 'IgnoreVisibility' => null, 'Limitation' => null, 'BoostFunctions' => null, 'QueryHandler' => 'ezpublish', 'EnableElevation' => true, 'ForceElevation' => true, 'SearchDate' => null, 'DistributedSearch' => null, 'FieldsToReturn' => self::fieldsToReturn(), 'SearchResultClustering' => null, 'ExtendedAttributeFilter' => array());
     $solrSearch = new OCSolr();
     $solrResult = $solrSearch->search($this->parameters['query'], $solrFetchParams);
     //eZDebug::writeNotice( $this->filters, __METHOD__ );
     //eZDebug::writeNotice( $solrResult, __METHOD__ );
     $this->data['parameters'] = $this->parameters;
     $this->data['fetch_parameters'] = $solrFetchParams;
     $facetFields = $solrResult['SearchExtras']->attribute('facet_fields');
     //eZDebug::writeNotice( $solrResult['SearchExtras'], __METHOD__ );
     $resultFacets = array();
     foreach ($facets as $index => $facet) {
         if (isset($facetFields[$index]['queryLimit']) && !empty($facetFields[$index]['queryLimit'])) {
             if (isset($facetFields[$index]['fieldList'])) {
                 foreach ($facetFields[$index]['fieldList'] as $key => $value) {
                     $resultFacets[$facet['name']][] = $key;
                 }
             } else {
                 foreach ($facetFields[$index]['nameList'] as $key => $value) {
                     $resultFacets[$facet['name']][] = $key;
                 }
             }
         }
     }
     $this->data['search_facets'] = $this->sortFacets($resultFacets);
     $events = array();
     foreach ($solrResult['SearchResult'] as $item) {
         $event = OCCalendarItem::fromEzfindResultArray($item);
         if ($event->isValid()) {
             $events[] = $event;
         }
     }
     $timeTableEvents = $this->fetchTimeTableEvents();
     if (count($timeTableEvents)) {
         $events = self::reorderEvents(array_merge($events, $timeTableEvents));
     }
     //eZDebug::writeNotice( $events, __METHOD__ );
     $this->data['search_count'] = count($events);
     $eventsByDay = array();
     $byDayInterval = new DateInterval('P1D');
     $byDayPeriod = new DatePeriod($startDateTime, $byDayInterval, $endDateTime);
     foreach ($byDayPeriod as $date) {
         $identifier = $date->format(self::FULLDAY_IDENTIFIER_FORMAT);
         $calendarDay = new OCCalendarDay($identifier);
         $calendarDay->addEvents($events);
         $eventsByDay[$identifier] = $calendarDay;
     }
     $this->data['events'] = $events;
     $this->data['day_by_day'] = $eventsByDay;
     //eZDebug::writeNotice( $eventsByDay, __METHOD__ );
     //eZDebug::writeNotice( $this->data['search_facets'], __METHOD__ );
 }