/**
  * The main method of the PlugIn
  *
  * @access	public
  *
  * @param	string		$content: The PlugIn content
  * @param	array		$conf: The PlugIn configuration
  *
  * @return	void
  */
 public function main($content = '', $conf = array())
 {
     if (\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('encrypted') != '' && \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('hashed') != '') {
         $core = tx_dlf_helper::decrypt(\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('encrypted'), \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('hashed'));
     }
     if (!empty($core)) {
         $url = trim(tx_dlf_solr::getSolrUrl($core), '/') . '/suggest/?q=' . tx_dlf_solr::escapeQuery(\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('q'));
         if ($stream = fopen($url, 'r')) {
             $content .= stream_get_contents($stream);
             fclose($stream);
         }
     }
     echo $content;
 }
 /**
  * The main method of the PlugIn
  *
  * @access	public
  *
  * @param	string		$content: The PlugIn content
  * @param	array		$conf: The PlugIn configuration
  *
  * @return	string		The content that is displayed on the website
  */
 public function main($content, $conf)
 {
     $this->init($conf);
     // Disable caching for this plugin.
     $this->setCache(FALSE);
     // Quit without doing anything if required variables are not set.
     if (empty($this->conf['solrcore'])) {
         if (TYPO3_DLOG) {
             \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_search->main(' . $content . ', [data])] Incomplete plugin configuration', $this->extKey, SYSLOG_SEVERITY_WARNING, $conf);
         }
         return $content;
     }
     if (!isset($this->piVars['query']) && empty($this->piVars['extQuery'])) {
         // Add javascript for search suggestions if enabled and jQuery autocompletion is available.
         if (!empty($this->conf['suggest'])) {
             $this->addAutocompleteJS();
         }
         // Load template file.
         if (!empty($this->conf['templateFile'])) {
             $this->template = $this->cObj->getSubpart($this->cObj->fileResource($this->conf['templateFile']), '###TEMPLATE###');
         } else {
             $this->template = $this->cObj->getSubpart($this->cObj->fileResource('EXT:dlf/plugins/search/template.tmpl'), '###TEMPLATE###');
         }
         // Configure @action URL for form.
         $linkConf = array('parameter' => $GLOBALS['TSFE']->id, 'forceAbsoluteUrl' => 1);
         // Fill markers.
         $markerArray = array('###ACTION_URL###' => $this->cObj->typoLink_URL($linkConf), '###LABEL_QUERY###' => $this->pi_getLL('label.query'), '###LABEL_SUBMIT###' => $this->pi_getLL('label.submit'), '###FIELD_QUERY###' => $this->prefixId . '[query]', '###QUERY###' => '', '###FULLTEXTSWITCH###' => $this->addFulltextSwitch(), '###FIELD_DOC###' => $this->conf['searchIn'] == 'document' || $this->conf['searchIn'] == 'all' ? $this->addCurrentDocument() : '', '###FIELD_COLL###' => $this->conf['searchIn'] == 'collection' || $this->conf['searchIn'] == 'all' ? $this->addCurrentCollection() : '', '###ADDITIONAL_INPUTS###' => $this->addEncryptedCoreName(), '###FACETS_MENU###' => $this->addFacetsMenu());
         // Get additional fields for extended search.
         $extendedSearch = $this->addExtendedSearch();
         // Display search form.
         $content .= $this->cObj->substituteSubpart($this->cObj->substituteMarkerArray($this->template, $markerArray), '###EXT_SEARCH_ENTRY###', $extendedSearch);
         return $this->pi_wrapInBaseClass($content);
     } else {
         // Instantiate search object.
         $solr = tx_dlf_solr::getInstance($this->conf['solrcore']);
         if (!$solr->ready) {
             if (TYPO3_DLOG) {
                 \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_search->main(' . $content . ', [data])] Apache Solr not available', $this->extKey, SYSLOG_SEVERITY_ERROR, $conf);
             }
             return $content;
         }
         // Build label for result list.
         $label = $this->pi_getLL('search', '', TRUE);
         if (!empty($this->piVars['query'])) {
             $label .= htmlspecialchars(sprintf($this->pi_getLL('for', ''), $this->piVars['query']));
         }
         // Prepare query parameters.
         $params = array();
         // Set search query.
         if (!empty($this->conf['fulltext']) && !empty($this->piVars['fulltext'])) {
             // Search in fulltext field if applicable.
             $query = 'fulltext:(' . tx_dlf_solr::escapeQuery($this->piVars['query']) . ')';
             // Add highlighting for fulltext.
             $params['hl'] = 'true';
             $params['hl.fl'] = 'fulltext';
         } else {
             // Retain given search field if valid.
             $query = tx_dlf_solr::escapeQueryKeepField($this->piVars['query'], $this->conf['pages']);
         }
         // Add extended search query.
         if (!empty($this->piVars['extQuery']) && is_array($this->piVars['extQuery'])) {
             $allowedOperators = array('AND', 'OR', 'NOT');
             $allowedFields = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $this->conf['extendedFields'], TRUE);
             for ($i = 0; $i < count($this->piVars['extQuery']); $i++) {
                 if (!empty($this->piVars['extQuery'][$i])) {
                     if (in_array($this->piVars['extOperator'][$i], $allowedOperators) && in_array($this->piVars['extField'][$i], $allowedFields)) {
                         if (!empty($query)) {
                             $query .= ' ' . $this->piVars['extOperator'][$i] . ' ';
                         }
                         $query .= tx_dlf_indexing::getIndexFieldName($this->piVars['extField'][$i], $this->conf['pages']) . ':(' . tx_dlf_solr::escapeQuery($this->piVars['extQuery'][$i]) . ')';
                     }
                 }
             }
         }
         // Add filter query for faceting.
         if (!empty($this->piVars['fq'])) {
             $params['fq'] = $this->piVars['fq'];
         }
         // Add filter query for in-document searching.
         if ($this->conf['searchIn'] == 'document' || $this->conf['searchIn'] == 'all') {
             if (!empty($this->piVars['id']) && \TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($this->piVars['id'])) {
                 $params['fq'][] = 'uid:(' . $this->piVars['id'] . ') OR partof:(' . $this->piVars['id'] . ')';
                 $label .= htmlspecialchars(sprintf($this->pi_getLL('in', ''), tx_dlf_document::getTitle($this->piVars['id'])));
             }
         }
         // Add filter query for in-collection searching.
         if ($this->conf['searchIn'] == 'collection' || $this->conf['searchIn'] == 'all') {
             if (!empty($this->piVars['collection']) && \TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($this->piVars['collection'])) {
                 $index_name = tx_dlf_helper::getIndexName($this->piVars['collection'], 'tx_dlf_collections', $this->conf['pages']);
                 $params['fq'][] = 'collection_faceting:("' . tx_dlf_solr::escapeQuery($index_name) . '")';
                 $label .= sprintf($this->pi_getLL('in', '', TRUE), tx_dlf_helper::translate($index_name, 'tx_dlf_collections', $this->conf['pages']));
             }
         }
         // Add filter query for collection restrictions.
         if ($this->conf['collections']) {
             $collIds = explode(',', $this->conf['collections']);
             $collIndexNames = array();
             foreach ($collIds as $collId) {
                 $collIndexNames[] = tx_dlf_solr::escapeQuery(tx_dlf_helper::getIndexName(intval($collId), 'tx_dlf_collections', $this->conf['pages']));
             }
             // Last value is fake and used for distinction in $this->addCurrentCollection()
             $params['fq'][] = 'collection_faceting:("' . implode('" OR "', $collIndexNames) . '" OR "FakeValueForDistinction")';
         }
         // Set search parameters.
         $solr->limit = max(intval($this->conf['limit']), 1);
         $solr->cPid = $this->conf['pages'];
         $solr->params = $params;
         // Perform search.
         $results = $solr->search($query);
         $results->metadata = array('label' => $label, 'description' => '<p class="tx-dlf-search-numHits">' . htmlspecialchars(sprintf($this->pi_getLL('hits', ''), $solr->numberOfHits, count($results))) . '</p>', 'thumbnail' => '', 'options' => $results->metadata['options']);
         $results->save();
         // Clean output buffer.
         \TYPO3\CMS\Core\Utility\GeneralUtility::cleanOutputBuffers();
         // Keep some plugin variables.
         $linkConf['parameter'] = $this->conf['targetPid'];
         if (!empty($this->piVars['order'])) {
             $linkConf['additionalParams'] = \TYPO3\CMS\Core\Utility\GeneralUtility::implodeArrayForUrl($this->prefixId, array('order' => $this->piVars['order'], 'asc' => !empty($this->piVars['asc']) ? '1' : '0'), '', TRUE, FALSE);
         }
         // Send headers.
         header('Location: ' . \TYPO3\CMS\Core\Utility\GeneralUtility::locationHeaderUrl($this->cObj->typoLink_URL($linkConf)));
         // Flush output buffer and end script processing.
         ob_end_flush();
         exit;
     }
 }
 /**
  * This returns the full record of any list element
  *
  * @access	protected
  *
  * @param	mixed		$element: The list element
  *
  * @return	mixed		The element's full record
  */
 protected function getRecord($element)
 {
     if (is_array($element) && array_keys($element) == array('u', 'h', 's', 'p')) {
         // Return already processed record if possible.
         if (!empty($this->records[$element['u']])) {
             return $this->records[$element['u']];
         }
         $record = array('uid' => $element['u'], 'page' => 1, 'preview' => '', 'subparts' => $element['p']);
         // Check if it's a list of database records or Solr documents.
         if (!empty($this->metadata['options']['source']) && $this->metadata['options']['source'] == 'collection') {
             // Get document's thumbnail and metadata from database.
             $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery('tx_dlf_documents.uid AS uid,tx_dlf_documents.thumbnail AS thumbnail,tx_dlf_documents.metadata AS metadata', 'tx_dlf_documents', '(tx_dlf_documents.uid=' . intval($record['uid']) . ' OR tx_dlf_documents.partof=' . intval($record['uid']) . ')' . tx_dlf_helper::whereClause('tx_dlf_documents'), '', '', '');
             // Process results.
             while ($resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) {
                 // Prepare document's metadata.
                 $metadata = unserialize($resArray['metadata']);
                 if (!empty($metadata['type'][0]) && \TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($metadata['type'][0])) {
                     $metadata['type'][0] = tx_dlf_helper::getIndexName($metadata['type'][0], 'tx_dlf_structures', $this->metadata['options']['pid']);
                 }
                 if (!empty($metadata['owner'][0]) && \TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($metadata['owner'][0])) {
                     $metadata['owner'][0] = tx_dlf_helper::getIndexName($metadata['owner'][0], 'tx_dlf_libraries', $this->metadata['options']['pid']);
                 }
                 if (!empty($metadata['collection']) && is_array($metadata['collection'])) {
                     foreach ($metadata['collection'] as $i => $collection) {
                         if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($collection)) {
                             $metadata['collection'][$i] = tx_dlf_helper::getIndexName($metadata['collection'][$i], 'tx_dlf_collections', $this->metadata['options']['pid']);
                         }
                     }
                 }
                 // Add metadata to list element.
                 if ($resArray['uid'] == $record['uid']) {
                     $record['thumbnail'] = $resArray['thumbnail'];
                     $record['metadata'] = $metadata;
                 } elseif (($key = tx_dlf_helper::array_search_recursive($resArray['uid'], $record['subparts'], TRUE)) !== FALSE) {
                     $record['subparts'][$key] = array('uid' => $resArray['uid'], 'page' => 1, 'preview' => !empty($record['subparts'][$key]['h']) ? $record['subparts'][$key]['h'] : '', 'thumbnail' => $resArray['thumbnail'], 'metadata' => $metadata);
                 }
             }
         } elseif (!empty($this->metadata['options']['source']) && $this->metadata['options']['source'] == 'search') {
             if ($this->solrConnect()) {
                 // Get document's thumbnail and metadata from Solr index.
                 $result = $this->solr->service->search('uid:' . tx_dlf_solr::escapeQuery($record['uid']), 0, $this->solr->limit);
                 // Process results.
                 foreach ($result->response->docs as $resArray) {
                     // Prepare document's metadata.
                     $metadata = array();
                     foreach ($this->solrConfig as $index_name => $solr_name) {
                         if (!empty($resArray->{$solr_name})) {
                             $metadata[$index_name] = is_array($resArray->{$solr_name}) ? $resArray->{$solr_name} : array($resArray->{$solr_name});
                         }
                     }
                     // Add metadata to list elements.
                     if ($resArray->toplevel) {
                         $record['thumbnail'] = $resArray->thumbnail;
                         $record['metadata'] = $metadata;
                     } elseif (($key = tx_dlf_helper::array_search_recursive($resArray->id, $record['subparts'], TRUE)) !== FALSE) {
                         $record['subparts'][$key] = array('uid' => $resArray->uid, 'page' => $resArray->page, 'preview' => !empty($record['subparts'][$key]['h']) ? $record['subparts'][$key]['h'] : '', 'thumbnail' => $resArray->thumbnail, 'metadata' => $metadata);
                     }
                 }
             }
         }
         // Save record for later usage.
         $this->records[$element['u']] = $record;
     } else {
         if (TYPO3_DLOG) {
             \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_list->getRecord([data])] No UID of list element to fetch full record', $this->extKey, SYSLOG_SEVERITY_NOTICE, $element);
         }
         $record = $element;
     }
     return $record;
 }