8eadea0a95ab9ca87fa2fc5eab782224d0479727
[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
20 /**
21 * The model of contagged.
22 *
23 * @author Jochen Rau <j.rau@web.de>
24 * @package TYPO3
25 * @subpackage tx_contagged_model_mapper
26 */
27 class tx_contagged_model_mapper implements t3lib_Singleton {
28
29 var $conf; // the TypoScript configuration array
30 var $controller;
31
32 function tx_contagged_model_mapper($controller) {
33 $this->controller = $controller;
34 $this->conf = $controller->conf;
35 if (!is_object($this->cObj)) {
36 $this->cObj = t3lib_div::makeInstance('tslib_cObj');
37 }
38 }
39
40 /**
41 * Build an array of the entries in the specified table
42 *
43 * @param array $result: An result pointer of the database query
44 * @param string $dataSource: The identifier of the data source
45 * @return array An array with the data of the table
46 */
47 function getDataArray($result, $dataSource) {
48 $dataArray = array();
49 $dataSourceConfigArray = $this->conf['dataSources.'][$dataSource . '.'];
50
51 // add additional fields configured in the mapping configuration of the data source
52 $fieldsToMapArray = array();
53 foreach ($dataSourceConfigArray['mapping.'] as $fieldToMap => $value) {
54 $fieldsToMapArray[] = substr($fieldToMap, 0, -1);
55 }
56 $fieldsToMapfromTS = t3lib_div::trimExplode(',', $this->conf['fieldsToMap'], 1);
57 foreach ($fieldsToMapfromTS as $key => $fieldToMap) {
58 if (!t3lib_div::inArray($fieldsToMapArray, $fieldToMap)) {
59 $fieldsToMapArray[] = $fieldToMap;
60 }
61 }
62
63 // iterate through all data from the datasource
64 foreach ($result as $row) {
65 $termMain = $dataSourceConfigArray['mapping.']['term_main.']['field'] ? $dataSourceConfigArray['mapping.']['term_main.']['field'] : '';
66 $termReplace = $dataSourceConfigArray['mapping.']['term_replace.']['field'] ? $dataSourceConfigArray['mapping.']['term_replace.']['field'] : '';
67 $term = $row[$termReplace] ? $row[$termReplace] : $row[$termMain];
68 $mappedDataArray = array();
69 $mappedDataArray['term'] = $term;
70 $mappedDataArray['source'] = $dataSource;
71 foreach ($fieldsToMapArray as $field) {
72 $value = $dataSourceConfigArray['mapping.'][$field . '.'];
73 if ($value['value']) {
74 $mappedDataArray[$field] = $value['value'];
75 } elseif ($value['field']) {
76 $mappedDataArray[$field] = $row[$value['field']];
77 } else {
78 $mappedDataArray[$field] = NULL;
79 }
80 if ($value['stdWrap.']) {
81 $mappedDataArray[$field] = $this->cObj->stdWrap($mappedDataArray[$field], $value['stdWrap.']);
82 }
83 if ($field === 'link') {
84 $mappedDataArray[$field . '.']['additionalParams'] = $value['additionalParams'];
85 if ($value['additionalParams.']['stdWrap.']) {
86 $mappedDataArray[$field . '.']['additionalParams'] = $this->cObj->stdWrap($mappedDataArray[$field . '.']['additionalParams'], $value['additionalParams.']['stdWrap.']);
87 }
88 }
89 $GLOBALS['TSFE']->register['contagged_' . $field] = $mappedDataArray[$field];
90 }
91
92 // post processing
93 $mappedDataArray['term_alt'] = t3lib_div::trimExplode(chr(10), $row['term_alt'], 1);
94 // TODO: hook "mappingPostProcessing"
95
96 if (!empty($dataSourceConfigArray['mapping.']['uid.']['field'])) {
97 $dataArray[$row[$dataSourceConfigArray['mapping.']['uid.']['field']]] = $mappedDataArray;
98 } else {
99 $dataArray[] = $mappedDataArray;
100 }
101 }
102
103 return $dataArray;
104 }
105 }
106
107 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/contagged/model/class.tx_contagged_model_mapper.php']) {
108 include_once ($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/contagged/model/class.tx_contagged_model_mapper.php']);
109 }
110 ?>