/**
  * This builds an array for one menu entry
  *
  * @access	protected
  *
  * @param	array		$entry: The entry's array from tx_dlf_document->getLogicalStructure
  * @param	boolean		$recursive: Whether to include the child entries
  *
  * @return	array		HMENU array for menu entry
  */
 protected function getMenuEntry($entry, $recursive = FALSE)
 {
     $entryArray = array();
     // Set "title", "volume", "type" and "pagination" from $entry array.
     $entryArray['title'] = $entry['label'];
     $entryArray['volume'] = $entry['volume'];
     if (empty($entryArray['volume'])) {
         $entryArray['volume'] = $entry['orderlabel'];
     }
     $entryArray['type'] = tx_dlf_helper::translate($entry['type'], 'tx_dlf_structures', $this->conf['pages']);
     $entryArray['pagination'] = $entry['pagination'];
     $entryArray['_OVERRIDE_HREF'] = '';
     $entryArray['doNotLinkIt'] = 1;
     $entryArray['ITEM_STATE'] = 'NO';
     // Build menu links based on the $entry['points'] array.
     if (!empty($entry['points']) && tx_dlf_helper::testInt($entry['points'])) {
         $entryArray['_OVERRIDE_HREF'] = $this->pi_linkTP_keepPIvars_url(array('page' => $entry['points']), TRUE, FALSE, $this->conf['targetPid']);
         $entryArray['doNotLinkIt'] = 0;
     } elseif (!empty($entry['points']) && is_string($entry['points'])) {
         $entryArray['_OVERRIDE_HREF'] = $this->pi_linkTP_keepPIvars_url(array('id' => $entry['points'], 'page' => 1), TRUE, FALSE, $this->conf['targetPid']);
         $entryArray['doNotLinkIt'] = 0;
     } elseif (!empty($entry['targetUid'])) {
         $entryArray['_OVERRIDE_HREF'] = $this->pi_linkTP_keepPIvars_url(array('id' => $entry['targetUid'], 'page' => 1), TRUE, FALSE, $this->conf['targetPid']);
         $entryArray['doNotLinkIt'] = 0;
     }
     // Set "ITEM_STATE" to "CUR" if this entry points to current page.
     if (in_array($entry['id'], $this->activeEntries)) {
         $entryArray['ITEM_STATE'] = 'CUR';
     }
     // Build sub-menu if available and called recursively.
     if ($recursive == TRUE && !empty($entry['children'])) {
         // Build sub-menu only if one of the following conditions apply:
         // 1. "expAll" is set for menu
         // 2. Current menu node is in rootline
         // 3. Current menu node points to another file
         // 4. Current menu node has no corresponding images
         if (!empty($this->conf['menuConf.']['expAll']) || $entryArray['ITEM_STATE'] == 'CUR' || is_string($entry['points']) || empty($this->doc->smLinks['l2p'][$entry['id']])) {
             $entryArray['_SUB_MENU'] = array();
             foreach ($entry['children'] as $child) {
                 // Set "ITEM_STATE" to "ACT" if this entry points to current page and has sub-entries pointing to the same page.
                 if (in_array($child['id'], $this->activeEntries)) {
                     $entryArray['ITEM_STATE'] = 'ACT';
                 }
                 $entryArray['_SUB_MENU'][] = $this->getMenuEntry($child, TRUE);
             }
         }
         // Append "IFSUB" to "ITEM_STATE" if this entry has sub-entries.
         $entryArray['ITEM_STATE'] = $entryArray['ITEM_STATE'] == 'NO' ? 'IFSUB' : $entryArray['ITEM_STATE'] . 'IFSUB';
     }
     return $entryArray;
 }
 /**
  * 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;
     }
 }
 /**
  * Get metadata configuration from database
  *
  * @access	protected
  *
  * @return	void
  */
 protected function loadConfig()
 {
     $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery('tx_dlf_metadata.index_name AS index_name,tx_dlf_metadata.wrap AS wrap,tx_dlf_metadata.is_listed AS is_listed,tx_dlf_metadata.is_sortable AS is_sortable', 'tx_dlf_metadata', '(tx_dlf_metadata.is_listed=1 OR tx_dlf_metadata.is_sortable=1) AND tx_dlf_metadata.pid=' . intval($this->conf['pages']) . tx_dlf_helper::whereClause('tx_dlf_metadata'), '', 'tx_dlf_metadata.sorting ASC', '');
     while ($resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) {
         if ($resArray['is_listed']) {
             $this->metadata[$resArray['index_name']] = array('wrap' => $resArray['wrap'], 'label' => tx_dlf_helper::translate($resArray['index_name'], 'tx_dlf_metadata', $this->conf['pages']));
         }
         if ($resArray['is_sortable']) {
             $this->sortables[$resArray['index_name']] = tx_dlf_helper::translate($resArray['index_name'], 'tx_dlf_metadata', $this->conf['pages']);
         }
     }
 }
 /**
  * Prepares the metadata array for output
  *
  * @access	protected
  *
  * @param	array		$metadataArray: The metadata array
  *
  * @return	string		The metadata array ready for output
  */
 protected function printMetadata(array $metadataArray)
 {
     // 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/metadata/template.tmpl'), '###TEMPLATE###');
     }
     $output = '';
     $subpart['block'] = $this->cObj->getSubpart($this->template, '###BLOCK###');
     // Get list of metadata to show.
     $metaList = array();
     $result = $GLOBALS['TYPO3_DB']->exec_SELECTquery('tx_dlf_metadata.index_name AS index_name,tx_dlf_metadata.is_listed AS is_listed,tx_dlf_metadata.wrap AS wrap', 'tx_dlf_metadata', 'tx_dlf_metadata.pid=' . intval($this->conf['pages']) . tx_dlf_helper::whereClause('tx_dlf_metadata'), '', 'tx_dlf_metadata.sorting', '');
     while ($resArray = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($result)) {
         if ($this->conf['showFull'] || $resArray['is_listed']) {
             $metaList[$resArray['index_name']] = array('wrap' => $resArray['wrap'], 'label' => tx_dlf_helper::translate($resArray['index_name'], 'tx_dlf_metadata', $this->conf['pages']));
         }
     }
     // Save original data array.
     $cObjData = $this->cObj->data;
     // Parse the metadata arrays.
     foreach ($metadataArray as $metadata) {
         $markerArray['###METADATA###'] = '';
         // Reset content object's data array.
         $this->cObj->data = $cObjData;
         // Load all the metadata values into the content object's data array.
         foreach ($metadata as $index_name => $value) {
             if (is_array($value)) {
                 $this->cObj->data[$index_name] = implode($this->conf['separator'], $value);
             } else {
                 $this->cObj->data[$index_name] = $value;
             }
         }
         // Process each metadate.
         foreach ($metaList as $index_name => $metaConf) {
             $parsedValue = '';
             $fieldwrap = $this->parseTS($metaConf['wrap']);
             do {
                 $value = @array_shift($metadata[$index_name]);
                 if ($index_name == 'title') {
                     // Get title of parent document if needed.
                     if (empty($value) && $this->conf['getTitle'] && $this->doc->parentId) {
                         $superiorTitle = tx_dlf_document::getTitle($this->doc->parentId, TRUE);
                         if (!empty($superiorTitle)) {
                             $value = '[' . $superiorTitle . ']';
                         }
                     }
                     if (!empty($value)) {
                         $value = htmlspecialchars($value);
                         // Link title to pageview.
                         if ($this->conf['linkTitle'] && $metadata['_id']) {
                             $details = $this->doc->getLogicalStructure($metadata['_id']);
                             $value = $this->pi_linkTP($value, array($this->prefixId => array('id' => $this->doc->uid, 'page' => !empty($details['points']) ? intval($details['points']) : 1)), TRUE, $this->conf['targetPid']);
                         }
                     }
                 } elseif ($index_name == 'owner' && !empty($value)) {
                     // Translate name of holding library.
                     $value = htmlspecialchars(tx_dlf_helper::translate($value, 'tx_dlf_libraries', $this->conf['pages']));
                 } elseif ($index_name == 'type' && !empty($value)) {
                     // Translate document type.
                     $value = htmlspecialchars(tx_dlf_helper::translate($value, 'tx_dlf_structures', $this->conf['pages']));
                 } elseif ($index_name == 'collection' && !empty($value)) {
                     // Translate collection.
                     $value = htmlspecialchars(tx_dlf_helper::translate($value, 'tx_dlf_collections', $this->conf['pages']));
                 } elseif ($index_name == 'language' && !empty($value)) {
                     // Translate ISO 639 language code.
                     $value = htmlspecialchars(tx_dlf_helper::getLanguageName($value));
                 } elseif (!empty($value)) {
                     // Sanitize value for output.
                     $value = htmlspecialchars($value);
                 }
                 // Hook for getting a customized value (requested by SBB).
                 foreach ($this->hookObjects as $hookObj) {
                     if (method_exists($hookObj, 'printMetadata_customizeMetadata')) {
                         $hookObj->printMetadata_customizeMetadata($value);
                     }
                 }
                 $value = $this->cObj->stdWrap($value, $fieldwrap['value.']);
                 if (!empty($value)) {
                     $parsedValue .= $value;
                 }
             } while (count($metadata[$index_name]));
             if (!empty($parsedValue)) {
                 $field = $this->cObj->stdWrap(htmlspecialchars($metaConf['label']), $fieldwrap['key.']);
                 $field .= $parsedValue;
                 $markerArray['###METADATA###'] .= $this->cObj->stdWrap($field, $fieldwrap['all.']);
             }
         }
         $output .= $this->cObj->substituteMarkerArray($subpart['block'], $markerArray);
     }
     return $this->cObj->substituteSubpart($this->template, '###BLOCK###', $output, TRUE);
 }