[TASK] Replace deprecated methods, remove require_once, use namespaced core classes...
[TYPO3CMS/Extensions/contagged.git] / model / class.tx_contagged_model_mapper.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 * (c) 2007 Jochen Rau <j.rau@web.de>
5 * All rights reserved
6 * This script is part of the TYPO3 project. The TYPO3 project is
7 * free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 * The GNU General Public License can be found at
12 * http://www.gnu.org/copyleft/gpl.html.
13 * This script is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 * This copyright notice MUST APPEAR in all copies of the script!
18 ***************************************************************/
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * The model of contagged.
23 *
24 * @author Jochen Rau <j.rau@web.de>
25 * @package TYPO3
26 * @subpackage tx_contagged_model_mapper
27 */
28 class tx_contagged_model_mapper implements \TYPO3\CMS\Core\SingletonInterface {
29
30 var $conf; // the TypoScript configuration array
31 var $controller;
32
33 function tx_contagged_model_mapper($controller) {
34 $this->controller = $controller;
35 $this->conf = $controller->conf;
36 if (!is_object($this->cObj)) {
37 $this->cObj = GeneralUtility::makeInstance('TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer');
38 }
39 }
40
41 /**
42 * Build an array of the entries in the specified table
43 *
44 * @param array $result: An result pointer of the database query
45 * @param string $dataSource: The identifier of the data source
46 * @return array An array with the data of the table
47 */
48 function getDataArray($result, $dataSource) {
49 $dataArray = array();
50 $dataSourceConfigArray = $this->conf['dataSources.'][$dataSource . '.'];
51
52 // add additional fields configured in the mapping configuration of the data source
53 $fieldsToMapArray = array();
54 foreach ($dataSourceConfigArray['mapping.'] as $fieldToMap => $value) {
55 $fieldsToMapArray[] = substr($fieldToMap, 0, -1);
56 }
57 $fieldsToMapfromTS = GeneralUtility::trimExplode(',', $this->conf['fieldsToMap'], 1);
58 foreach ($fieldsToMapfromTS as $key => $fieldToMap) {
59 if (!GeneralUtility::inArray($fieldsToMapArray, $fieldToMap)) {
60 $fieldsToMapArray[] = $fieldToMap;
61 }
62 }
63
64 // iterate through all data from the datasource
65 foreach ($result as $row) {
66 $termMain = $dataSourceConfigArray['mapping.']['term_main.']['field'] ? $dataSourceConfigArray['mapping.']['term_main.']['field'] : '';
67 $termReplace = $dataSourceConfigArray['mapping.']['term_replace.']['field'] ? $dataSourceConfigArray['mapping.']['term_replace.']['field'] : '';
68 $term = $row[$termReplace] ? $row[$termReplace] : $row[$termMain];
69 $mappedDataArray = array();
70 $mappedDataArray['term'] = $term;
71 $mappedDataArray['source'] = $dataSource;
72 foreach ($fieldsToMapArray as $field) {
73 $value = $dataSourceConfigArray['mapping.'][$field . '.'];
74 if ($value['value']) {
75 $mappedDataArray[$field] = $value['value'];
76 } elseif ($value['field']) {
77 $mappedDataArray[$field] = $row[$value['field']];
78 } else {
79 $mappedDataArray[$field] = NULL;
80 }
81 if ($value['stdWrap.']) {
82 $mappedDataArray[$field] = $this->cObj->stdWrap($mappedDataArray[$field], $value['stdWrap.']);
83 }
84 if ($field === 'link') {
85 $mappedDataArray[$field . '.']['additionalParams'] = $value['additionalParams'];
86 if ($value['additionalParams.']['stdWrap.']) {
87 $mappedDataArray[$field . '.']['additionalParams'] = $this->cObj->stdWrap($mappedDataArray[$field . '.']['additionalParams'], $value['additionalParams.']['stdWrap.']);
88 }
89 }
90 $GLOBALS['TSFE']->register['contagged_' . $field] = $mappedDataArray[$field];
91 }
92
93 // post processing
94 $mappedDataArray['term_alt'] = GeneralUtility::trimExplode(chr(10), $row['term_alt'], 1);
95 // TODO: hook "mappingPostProcessing"
96
97 if (!empty($dataSourceConfigArray['mapping.']['uid.']['field'])) {
98 $dataArray[$row[$dataSourceConfigArray['mapping.']['uid.']['field']]] = $mappedDataArray;
99 } else {
100 $dataArray[] = $mappedDataArray;
101 }
102 }
103
104 return $dataArray;
105 }
106 }
107
108 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/contagged/model/class.tx_contagged_model_mapper.php']) {
109 include_once ($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/contagged/model/class.tx_contagged_model_mapper.php']);
110 }
111 ?>