PageRenderTime 80ms CodeModel.GetById 2ms app.highlight 67ms RepoModel.GetById 1ms app.codeStats 1ms

/kb_tv_migrate/mod1/index.php

https://github.com/michael-cannon/typo3-extensions
PHP | 1246 lines | 804 code | 94 blank | 348 comment | 94 complexity | 97784d03dae5a00b53f3c58d0fc379f9 MD5 | raw file
   1<?php
   2/***************************************************************
   3*  Copyright notice
   4*
   5*  (c) 2004 Bernhard Kraft (kraftb@kraftb.at)
   6*  All rights reserved
   7*
   8*  This script is part of the TYPO3 project. The TYPO3 project is
   9*  free software; you can redistribute it and/or modify
  10*  it under the terms of the GNU General Public License as published by
  11*  the Free Software Foundation; either version 2 of the License, or
  12*  (at your option) any later version.
  13*
  14*  The GNU General Public License can be found at
  15*  http://www.gnu.org/copyleft/gpl.html.
  16*
  17*  This script is distributed in the hope that it will be useful,
  18*  but WITHOUT ANY WARRANTY; without even the implied warranty of
  19*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20*  GNU General Public License for more details.
  21*
  22*  This copyright notice MUST APPEAR in all copies of the script!
  23***************************************************************/
  24/**
  25 * Module 'TV Migrate' for the 'kb_tv_migrate' extension.
  26 *
  27 * @author	Bernhard Kraft <kraftb@kraftb.at>
  28 */
  29/**
  30 * [CLASS/FUNCTION INDEX of SCRIPT]
  31 *
  32 *
  33 *
  34 *  104: class tx_kbtvmigrate_module1 extends t3lib_SCbase
  35 *  112:     function init()
  36 *  129:     function menuConfig()
  37 *  146:     function main()
  38 *  212:     function printContent()
  39 *  223:     function moduleContent()
  40 *  307:     function error($type)
  41 *
  42 *              SECTION: OUTPUT
  43 *  358:     function printColmapForm($similar)
  44 *  435:     function printDSTOForm($similar)
  45 *
  46 *              SECTION: SAVE
  47 *  532:     function saveColmapToDB($similar, $data)
  48 *  618:     function saveDSTOConfToPageheaders($similar, $data)
  49 *
  50 *              SECTION: ARRAY MODIFICATION
  51 *  695:     function rec_setDataArByDSTO($treePart, &$dataAr)
  52 *  715:     function rec_simplifyDSTOInTree(&$treePart)
  53 *  768:     function rec_setDSTOTree(&$treePart, $setPages, $ds, $to)
  54 *  789:     function setUsedT3Columns(&$similar)
  55 *  819:     function pagesWithSameDSTO_possible($combos)
  56 *  907:     function dsto_compare($row, $dsto)
  57 *  927:     function array_compare($a, $b)
  58 *
  59 *              SECTION: TO/DS METHODS
  60 *  958:     function getDS($row)
  61 *  973:     function getTO($row)
  62 *  990:     function getDSName($ds_id)
  63 * 1004:     function getTOName($to_id)
  64 * 1019:     function getDSTOCombinations($id, &$combos)
  65 *
  66 *              SECTION: TEMPLATE METHODS
  67 * 1064:     function getTemplateCombinations($id)
  68 * 1080:     function getTemplateCombination(&$combos, $row)
  69 * 1098:     function rec_getTemplateCombination(&$combos, $rows)
  70 *
  71 *              SECTION: Record methods
  72 * 1119:     function getPageTree($id)
  73 *
  74 *              SECTION: STORAGE FOLDERS AND TEMPLATES
  75 * 1149:     function findingStorageFolderIds()
  76 * 1183:     function getStorageFolders()
  77 *
  78 *              SECTION: EXTRA METHODS
  79 * 1212:     function getItemLabel($items, $val)
  80 *
  81 * TOTAL FUNCTIONS: 29
  82 * (This index is automatically created/updated by the extension "extdeveval")
  83 *
  84 */
  85
  86
  87
  88	// DEFAULT initialization of a module [BEGIN]
  89unset($MCONF);
  90require ('conf.php');
  91require ($BACK_PATH.'init.php');
  92require ($BACK_PATH.'template.php');
  93$LANG->includeLLFile('EXT:kb_tv_migrate/mod1/locallang.php');
  94#include ('locallang.php');
  95require_once (PATH_t3lib.'class.t3lib_scbase.php');
  96require_once (PATH_t3lib.'class.t3lib_treeview.php');
  97require_once (PATH_t3lib.'class.t3lib_tceforms.php');
  98require_once (PATH_t3lib.'class.t3lib_tcemain.php');
  99require_once (t3lib_extMgm::extPath('templavoila').'class.tx_templavoila_xmlrelhndl.php');
 100require_once (t3lib_extMgm::extPath('kb_tv_migrate').'class.tx_kbtvmigrate_xmlrelhndl.php');
 101$BE_USER->modAccess($MCONF,1);	// This checks permissions and exits if the users has no permission for entry.
 102	// DEFAULT initialization of a module [END]
 103
 104class tx_kbtvmigrate_module1 extends t3lib_SCbase {
 105	var $pageinfo;
 106
 107	/**
 108	 * Initializes the object
 109	 *
 110	 * @return	void
 111	 */
 112	function init()	{
 113		global $BE_USER,$LANG,$BACK_PATH,$TCA_DESCR,$TCA,$CLIENT,$TYPO3_CONF_VARS;
 114
 115		parent::init();
 116
 117		/*
 118		if (t3lib_div::_GP('clear_all_cache'))	{
 119			$this->include_once[]=PATH_t3lib.'class.t3lib_tcemain.php';
 120		}
 121		*/
 122
 123		set_time_limit(600);
 124	}
 125
 126	/**
 127	 * Adds items to the ->MOD_MENU array. Used for the function menu selector.
 128	 *
 129	 * @return	void
 130	 */
 131	function menuConfig()	{
 132		global $LANG;
 133		$this->MOD_MENU = Array (
 134			'function' => Array (
 135				'1' => $LANG->getLL('menu_about'),
 136				'2' => $LANG->getLL('menu_set_page_type'),
 137				'3' => $LANG->getLL('menu_insert_content_elements'),
 138			)
 139		);
 140		parent::menuConfig();
 141	}
 142
 143	/**
 144	 * Main function of the module. Write the content to $this->content
 145	 *
 146	 * @return	void
 147	 */
 148	function main()	{
 149		global $BE_USER,$LANG,$BACK_PATH,$TCA_DESCR,$TCA,$CLIENT,$TYPO3_CONF_VARS;
 150		$this->foldOut = isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['kb_tv_migrate']['foldOut'])?$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['kb_tv_migrate']['foldOut']:1;
 151		// Access check!
 152		// The page will show only if there is a valid page and if this page may be viewed by the user
 153		$this->pageinfo = t3lib_BEfunc::readPageAccess($this->id,$this->perms_clause);
 154		$access = is_array($this->pageinfo) ? 1 : 0;
 155
 156		if (($this->id && $access) || ($BE_USER->user['admin'] && !$this->id))	{
 157
 158				// Draw the header.
 159			$this->doc = t3lib_div::makeInstance('mediumDoc');
 160			$this->doc->backPath = $BACK_PATH;
 161			$this->doc->form='<form action="'.t3lib_div::linkThisScript(array('kb_tv_migrate' => '')).'" method="POST" target="_self" enctype="multipart/form-data">';
 162
 163				// JavaScript
 164			$this->doc->JScode = '
 165					###JS_REPLACE###
 166				<script language="javascript" type="text/javascript" src="../res/jsfunc.js"></script>'.chr(10);
 167			$this->doc->JScode .= $this->doc->getDynTabMenuJScode();
 168			$this->doc->postCode='
 169				<script language="javascript" type="text/javascript">
 170					var please_select = "'.$LANG->getLL('please_select').'";
 171					var select_ds_first = "'.$LANG->getLL('select_ds_first').'" 
 172					script_ended = 1;
 173					if (top.fsMod) top.fsMod.recentIds["web"] = '.intval($this->id).';
 174				</script>
 175			';
 176
 177			$headerSection = $this->doc->getHeader('pages',$this->pageinfo,$this->pageinfo['_thePath']).'<br>'.$LANG->sL('LLL:EXT:lang/locallang_core.php:labels.path').': '.t3lib_div::fixed_lgd_pre($this->pageinfo['_thePath'],50);
 178
 179			$this->content.=$this->doc->startPage($LANG->getLL('title'));
 180			$this->content.=$this->doc->header($LANG->getLL('title'));
 181			$this->content.=$this->doc->spacer(5);
 182			$this->content.=$this->doc->section('',$this->doc->funcMenu($headerSection,t3lib_BEfunc::getFuncMenu($this->id,'SET[function]',$this->MOD_SETTINGS['function'],$this->MOD_MENU['function'])));
 183			$this->content.=$this->doc->divider(5);
 184
 185
 186			// Render content:
 187			$this->moduleContent();
 188
 189
 190			// ShortCut
 191			if ($BE_USER->mayMakeShortcut())	{
 192				$this->content.=$this->doc->spacer(20).$this->doc->section('',$this->doc->makeShortcutIcon('id',implode(',',array_keys($this->MOD_MENU)),$this->MCONF['name']));
 193			}
 194
 195			$this->content.=$this->doc->spacer(10);
 196
 197			$this->content = str_replace('###JS_REPLACE###', $this->JSreplacement, $this->content);
 198		} else {
 199				// If no access or if ID == zero
 200
 201			$this->doc = t3lib_div::makeInstance('mediumDoc');
 202			$this->doc->backPath = $BACK_PATH;
 203
 204			$this->content.=$this->doc->startPage($LANG->getLL('title'));
 205			$this->content.=$this->doc->header($LANG->getLL('title'));
 206			$this->content.=$this->doc->spacer(5);
 207			$this->content.=$this->doc->spacer(10);
 208		}
 209	}
 210
 211	/**
 212	 * Prints out the module HTML
 213	 *
 214	 * @return	void
 215	 */
 216	function printContent()	{
 217
 218		$this->content.=$this->doc->endPage();
 219		echo $this->content;
 220	}
 221
 222	/**
 223	 * Generates the module content
 224	 *
 225	 * @return	void
 226	 */
 227	function moduleContent()	{
 228		global $LANG;
 229		switch((string)$this->MOD_SETTINGS['function'])	{
 230			case 1:
 231				$content = $LANG->getLL('about_text');
 232				$this->content.=$this->doc->section('About this extension',$content,0,1);
 233			break;
 234			case 2:
 235				$content= '';
 236				if (!intval($this->id))	{
 237					$content .= '<h1>Please select a page</h1><p>To start converting a site to Templa Voila please select the Root-Page of the site</p>';
 238				} else	{
 239					$combos = $this->getTemplateCombinations($this->id);
 240					$this->getDSTOCombinations($this->id, $combos);
 241					$similarPages = $this->pagesWithSameDSTO_possible($combos);
 242					$data = t3lib_div::_GP('kb_tv_migrate');
 243					if (is_array($data['dsto'])&&count($data['dsto']))	{
 244						$ok = $this->saveDSTOConfToPageheaders($similarPages, $data['dsto']);
 245						if ($ok)	{
 246							$content = '<h3>Save complete !!!</h3>
 247							<p>
 248								The data was saved sucessfully to the page headers. Have a look at your page headers
 249								and see if everything is set as expected.
 250							</p>'.chr(10);
 251						} else	{
 252							$content = '<h3>Error occured while saving</h3>
 253							<p>
 254								While saving the DS/TO configuration to the page headers there occured an error.<br />
 255								The specific error message is : <br />
 256								<br />
 257								<i>ERROR: '.$this->errorMsg.'</i>
 258								<br />
 259								<br />
 260								Please consult the Erros section of the manual on this specific error. If you don\'t find
 261								anything in there you can search the web or write a mail to the extension author.
 262							</p>'.chr(10);
 263						}
 264					} else	{
 265						$content .= $this->printDSTOForm($similarPages);
 266					}
 267				}
 268				$this->content.=$this->doc->section('Used Template Combinations',$content,0,1);
 269			break;
 270			case 3:
 271				$content='';
 272				if (!intval($this->id))	{
 273					$content .= '<h1>Please select a page</h1><p>To start converting a site to Templa Voila please select the Root-Page of the site</p>';
 274				} else	{
 275					$combos = $this->getTemplateCombinations($this->id);
 276					$this->getDSTOCombinations($this->id, $combos);
 277					$sameDSTO = $this->pagesWithSameDSTO_set($combos);
 278					$this->setUsedT3Columns($sameDSTO);
 279					if (!$sameDSTO&&strlen($this->errorMsg))	{
 280						$content = $this->error(2);
 281					} else {
 282						$data = t3lib_div::_GP('kb_tv_migrate');
 283						if (is_array($data['colmap'])&&count($data['colmap']))	{
 284							$ok = $this->saveColmapToDB($sameDSTO, $data['colmap']);
 285							if ($ok)	{
 286								$content = '<h3>Save complete !!!</h3>
 287								<p>
 288									The data was saved sucessfully to the tt_content table. Have a look at your Templa Voila
 289									pages and see if everything is inserted as expected.
 290								</p>'.chr(10);
 291							} else	{
 292								$content = $this->error(1);
 293							}
 294						} else	{
 295							$content .= $this->printColmapForm($sameDSTO);
 296						}
 297					}
 298				}
 299				$this->content.=$this->doc->section('Column Mapping',$content,0,1);
 300			break;
 301		}
 302	}
 303
 304
 305	/**
 306	 * Returns an error message
 307	 *
 308	 * @param	integer		The type of error
 309	 * @return	string		The error message
 310	 */
 311	function error($type)	{
 312		switch ($type)	{
 313			case 1:
 314				$content = '<h3>Error occurred while saving</h3>
 315								<p>
 316									While inserting the content elements into the Templa Voila columns there occurred an error.<br />
 317									The specific error message is : <br />
 318									<br />
 319									<i>ERROR: '.$this->errorMsg.'</i>
 320									<br />
 321									<br />
 322									Please consult the Erros section of the manual on this specific error. If you don\'t find
 323									anything in there you can search the web or write a mail to the extension author.
 324								</p>'.chr(10);
 325			break;
 326			case 2:
 327				$content = '<h3>Error occurred while checking DS/TO</h3>
 328								<p>
 329									While checking the validity of the DS/TO configuration some error occurred : <br />
 330									<br />
 331									<i>ERROR: '.$this->errorMsg.'</i>
 332									<br />
 333									<br />
 334									Please consult the Erros section of the manual on this specific error. If you don\'t find
 335									anything in there you can search the web or write a mail to the extension author.
 336								</p>'.chr(10);
 337			break;
 338			default:
 339				$content = '<h3>Unspecified Error occurred</h3>
 340								<p>
 341									An unspecified error occurred while performing some operation. This should not happen.
 342								</p>'.chr(10);
 343			break;
 344		}
 345		return $content;
 346	}
 347
 348	/*******************************************
 349	 *
 350	 * OUTPUT
 351	 *
 352	 * This methods perform operations on the data arrays
 353	 *
 354	 *******************************************/
 355
 356	/**
 357	 * Returns an the form for defining the column mapping
 358	 *
 359	 * @param	array		Data array
 360	 * @return	string		HTML
 361	 */
 362	function printColmapForm($similar)	{
 363		global $LANG;
 364		$content = '';
 365		$menuItems = array();
 366		$selectId = 0;
 367		$JS .= 'var selectVals = Array();'.chr(10);
 368		$JS .= 'var validTO = Array();'.chr(10);
 369		$JS .= 'var validTAB = Array();'.chr(10);
 370		$JS .= 'var validALL = 0;'.chr(10);
 371		$tab = 0;
 372		foreach ($similar as $main => $main_ar)	{
 373			foreach ($main_ar as $sub => $similar_ar)	{
 374				$JS .= 'validTO['.$tab.'] = Array();'.chr(10);
 375				$JS .= 'validTAB['.$tab.'] = 0;'.chr(10);
 376				$HTML = '';
 377				$HTML .= '<div class="bgColor4">'.chr(10);
 378				foreach ($similar_ar as $cnt => $data)	{
 379					$JS .= 'selectVals['.$selectId.'] = Array();'.chr(10);
 380					$JS .= 'validTO['.$tab.']['.$selectId.'] = 0;'.chr(10);
 381					$HTML .= '<h2>Pages with same TS/DO configuration:</h2>'.chr(10);
 382					$HTML .= '<ul>'.chr(10);
 383					foreach ($data['PAGES'] as $uid => $row)	{
 384						$HTML .= '<li>'.t3lib_BEfunc::getRecordTitle('pages', $row).' ('.$uid.')</li>'.chr(10);
 385					}
 386					$HTML .= '</ul>'.chr(10);
 387					$HTML .= '<p>Please select now which colum should get mapped to which TV column. You can just choose TV columns which are mapped in the TO selected for those pages. Content Elements get just inserted if they aren\'t in the TV column already. If more than one T3 column gets mapped to the same TV column then Content Elements from the lower (in this form) mapping will be above the elements of the higher (in this form) mapping.</p>'.chr(10);
 388					$HTML .= '<table cellspacing="8" cellpadding="0" border="0">'.chr(10);
 389					foreach ($data['T3_COLUMNS'] as $t3Col)	{
 390						$getLabel = $this->getItemLabel($GLOBALS['TCA']['tt_content']['columns']['colPos']['config']['items'], $t3Col);
 391						$HTML .= '<tr>
 392		<td align="right" style="font-weight: bold;">
 393			'.$LANG->getLL('t3col_label').' "'.$LANG->sL($getLabel).'" ==> '.$LANG->getLL('tvcol_label').' :
 394		</td>
 395		<td>
 396			<select id="select_'.$selectId.'_col" name="kb_tv_migrate[colmap]['.rawurlencode($main).']['.rawurlencode($sub).']['.$cnt.']['.$t3Col.']">
 397				<option value="">'.$LANG->getLL('no_target_column').'</option>'.chr(10);
 398						foreach ($data['TV_COLUMNS'] as $field => $fieldAr) {
 399							$HTML .= '<option value="'.$field.'">'.$fieldAr['tx_templavoila']['title'].'</option>'.chr(10);
 400						}
 401						$HTML .='</select>
 402		</td>
 403	</tr>';
 404					}
 405					$HTML .= '</table>'.chr(10);
 406					$selectId++;
 407				} // EOF: foreach ($similar_ar as $cnt => $data)	{
 408
 409				$HTML .= '</div>'.chr(10);
 410
 411				$mainTitle = ($main=='_DEFAULT')?'DEFAULT':$main;
 412				$subTitle = ($sub=='_DEFAULT')?'DEFAULT':$sub;
 413				$menuItem = array(
 414					'label' => $mainTitle.' / '.$subTitle,
 415					'description' => 'Pages with this MAIN / SUB Template',
 416					'linkTitle' => 'Pages with this MAIN / SUB Template',
 417					'content' => $HTML,
 418					'stateIcon' => -1,
 419				);
 420				$menuItems[] = $menuItem;
 421				$tab++;
 422			} // EOF: foreach ($main_ar as $sub => $similar_ar)	{
 423		} // EOF: foreach ($similar as $main => $main_ar)	{
 424		$content .= $this->doc->getDynTabMenu($menuItems, 'kb_tv_migrate', $this->foldOut?1:-1, $this->foldOut?true:false);
 425		$content .= '<input type="submit" name="submit" value="Save this Column-Mapping" id="submit" style="border: 1px solid #333333; padding: 4px 10px 4px 10px; margin: 20px 0px 0px 20px; font-weight: bold; font-size: 15px; width: 300px;" class="bgColor6" onClick="return confirm(\''.$LANG->getLL('confirm_mapping').'\')">';
 426		$this->JSreplacement = $this->doc->wrapScriptTags($JS);
 427		return $content;
 428	} // EOF: function printForm($similar)	{
 429
 430
 431	/**
 432	 * Returns an the form for defining DS/TO configuration
 433	 *
 434	 * @param	array		Data array
 435	 * @return	string		HTML
 436	 */
 437	function printDSTOForm($similar)	{
 438		global $LANG;
 439		$content = '';
 440		$menuItems = array();
 441		$selectId = 0;
 442		$JS .= 'var selectVals = Array();'.chr(10);
 443		$JS .= 'var validTO = Array();'.chr(10);
 444		$JS .= 'var validTAB = Array();'.chr(10);
 445		$JS .= 'var validALL = 0;'.chr(10);
 446		$tab = 0;
 447		foreach ($similar as $main => $main_ar)	{
 448			foreach ($main_ar as $sub => $similar_ar)	{
 449				$JS .= 'validTO['.$tab.'] = Array();'.chr(10);
 450				$JS .= 'validTAB['.$tab.'] = 0;'.chr(10);
 451				$HTML = '';
 452				$HTML .= '<div class="bgColor4">'.chr(10);
 453				foreach ($similar_ar as $cnt => $data)	{
 454					$JS .= 'selectVals['.$selectId.'] = Array();'.chr(10);
 455					$JS .= 'validTO['.$tab.']['.$selectId.'] = 0;'.chr(10);
 456					$HTML .= '<h2>Pages with same possible TS/DO configurations:</h2>'.chr(10);
 457					$HTML .= '<ul>'.chr(10);
 458					foreach ($data['PAGES'] as $uid => $row)	{
 459						$HTML .= '<li>'.t3lib_BEfunc::getRecordTitle('pages', $row).' ('.$uid.')</li>'.chr(10);
 460					}
 461					$HTML .= '</ul>'.chr(10);
 462					$HTML .= '<p>Please select now which DS/TO those pages should get assinged. If you wish to make finer grained decisions on which DS/TO to use please select it manually afterwards</p>'.chr(10);
 463					$HTML .= '<table cellspacing="8" cellpadding="0" border="0">
 464	<tr>
 465		<td align="right" style="font-weight: bold;">
 466			'.$LANG->getLL('ds_label').' :
 467		</td>
 468		<td>
 469			<select id="select_'.$selectId.'_ds" name="kb_tv_migrate[dsto]['.rawurlencode($main).']['.rawurlencode($sub).']['.$cnt.'][ds]" onChange="updateTOselect('.$selectId.'); updateValid('.$selectId.', '.$tab.'); return false;">
 470				<option value="0">'.$LANG->getLL('please_select').'</option>'.chr(10);
 471					foreach ($data['DSTO_OPTIONS'] as $ds => $to_ar) {
 472						$HTML .= '<option value="'.$ds.'">'.$this->getDSName($ds).'</option>'.chr(10);
 473						$JS .= 'selectVals['.$selectId.']['.$ds.'] = Array();'.chr(10);
 474						foreach ($to_ar as $to => $val)	{
 475							if (!intval($val)) continue;
 476							$JS .= 'selectVals['.$selectId.']['.$ds.']['.$to.'] = "'.$this->getTOName($to).'";'.chr(10);
 477						}
 478					} // EOF: foreach ($data['DSTO_OPTIONS'] as $ds => $to_ar) {
 479					$HTML .='</select>
 480		</td>
 481	</tr>
 482	<tr>
 483		<td align="right" style="font-weight: bold;">
 484			'.$LANG->getLL('to_label').' :
 485		</td>
 486		<td>
 487			<select id="select_'.$selectId.'_to" name="kb_tv_migrate[dsto]['.rawurlencode($main).']['.rawurlencode($sub).']['.$cnt.'][to]" onChange="updateValid('.$selectId.', '.$tab.')">
 488				<option value="0">'.$LANG->getLL('select_ds_first').'</option>
 489			</select>
 490		</td>
 491	</tr>
 492</table>'.chr(10);
 493					$selectId++;
 494				} // EOF: foreach ($similar_ar as $cnt => $data)	{
 495				$HTML .= '</div>'.chr(10);
 496				$mainTitle = ($main=='_DEFAULT')?'DEFAULT':$main;
 497				$subTitle = ($sub=='_DEFAULT')?'DEFAULT':$sub;
 498				$menuItem = array(
 499					'label' => $mainTitle.' / '.$subTitle,
 500					'description' => 'Pages with this MAIN / SUB Template',
 501					'linkTitle' => 'Pages with this MAIN / SUB Template',
 502					'content' => $HTML,
 503					'stateIcon' => 2,
 504				);
 505				$menuItems[] = $menuItem;
 506				$tab++;
 507			} // EOF: foreach ($main_ar as $sub => $similar_ar)	{
 508		} // EOF: foreach ($similar as $main => $main_ar)	{
 509		$content .= '<form action="'.t3lib_div::linkThisScript(array('kb_tv_migrate' => '')).'" method="POST" enctype="multipart/form-data" target="_self">'.chr(10);
 510		$content .= $this->doc->getDynTabMenu($menuItems, 'kb_tv_migrate', $this->foldOut?1:-1, $this->foldOut?true:false);
 511		$content .= '<input type="submit" name="submit" value="Save TO/DS Configuration to Pageheaders" id="submit" style="border: 1px solid #333333; padding: 4px 10px 4px 10px; margin: 20px 0px 0px 20px; font-weight: bold; font-size: 15px; width: 400px; display: none;" class="bgColor6">';
 512		$content .= '</form>'.chr(10);
 513		$this->JSreplacement = $this->doc->wrapScriptTags($JS);
 514		return $content;
 515	} // EOF: function printForm($similar)	{
 516
 517
 518	/*******************************************
 519	 *
 520	 * SAVE
 521	 *
 522	 * This methods store the submitted data in the database
 523	 *
 524	 *******************************************/
 525
 526
 527	/**
 528	 * Applies the column mapping to the page headers. Inserts references to the content elements of the T3 columns
 529	 *
 530	 * @param	array		Data array
 531	 * @param	array		Submitted data
 532	 * @return	bool		true on success, false on error
 533	 */
 534	function saveColmapToDB($similar, $data)	{
 535		if (!is_array($data))	{
 536			$this->errorMsg = 'Variable expected to be array but was from different type (11)';
 537			return false;
 538		}
 539		foreach ($data as $main => $main_ar)	{
 540			if (!is_array($main_ar))	{
 541				$this->errorMsg = 'Variable expected to be array but was from different type (12)';
 542				return false;
 543			}
 544			foreach ($main_ar as $sub => $data_ar)	{
 545				if (!is_array($data_ar))	{
 546					$this->errorMsg = 'Variable expected to be array but was from different type (13)';
 547					return false;
 548				}
 549				if (!is_array($similar[$main][$sub]))	{
 550					$this->errorMsg = 'Posted path not found in pagestructure array (14)';
 551					return false;
 552				}
 553				foreach ($data_ar as $cnt => $conf_ar)	{
 554					if (!is_array($conf_ar))	{
 555						$this->errorMsg = 'Variable expected to be array but was from different type (15)';
 556						return false;
 557					}
 558					if (!is_array($similar[$main][$sub][$cnt]))	{
 559						$this->errorMsg = 'Posted path not found in pagestructure array (16)';
 560						return false;
 561					}
 562					$t3Cols = implode(',', $similar[$main][$sub][$cnt]['T3_COLUMNS']);
 563					$tvCols = implode(',', array_keys($similar[$main][$sub][$cnt]['TV_COLUMNS']));
 564					foreach($conf_ar as $t3Col => $tvCol)	{
 565						$tvCol = trim($tvCol);
 566						if (!strlen($tvCol)) continue;
 567						$t3Col = intval($t3Col);
 568						if (!t3lib_div::inList($t3Cols, $t3Col))	{
 569							$this->errorMsg = 'Posted T3 column not valid for this section (17)';
 570							return false;
 571						}
 572						if (!t3lib_div::inList($tvCols, $tvCol))	{
 573							$this->errorMsg = 'Posted TV column not valid for this section (18)';
 574							return false;
 575						}
 576						if (strlen($tvCol))	{
 577							$similar[$main][$sub][$cnt]['MAPPING'][$t3Col] = $tvCol;
 578						}
 579					}
 580				} // EOF: foreach ($data_ar as $cnt => $conf_ar)	{
 581			} // EOF: foreach ($main_ar as $sub => $data_ar)	{
 582		} // EOF: foreach ($data as $main => $main_ar)	{
 583			// Insert references to the content elements into the TV XML records
 584		$xmlhandler = t3lib_div::makeInstance('tx_kbtvmigrate_xmlrelhndl');
 585		foreach ($similar as $main => $mainAr)	{
 586			foreach ($mainAr as $sub => $subAr)	{
 587				foreach ($subAr as $cnt => $dataAr)	{
 588					foreach ($dataAr['PAGES'] as $uid => $pageRow)	{
 589						if (is_array($dataAr['MAPPING'])&&count($dataAr['MAPPING']))	{
 590							$xml = t3lib_div::xml2array($pageRow['tx_templavoila_flex']);
 591							$xml = is_array($xml) ? $xml : array();
 592
 593							foreach ($dataAr['MAPPING'] as $t3Col => $tvCol)	{
 594								$records = t3lib_BEfunc::getRecordsByField('tt_content', 'pid', $pageRow['uid'], ' AND colPos='.$t3Col.' '.t3lib_BEfunc::BEenableFields('tt_content'), '', ' sorting DESC');
 595								if (is_array($records)&&count($records))	{
 596									$elements = $xml['data']['sDEF']['lDEF'][$tvCol]['vDEF'];
 597									$xmlTarget = 'pages:'.$pageRow['uid'].':sDEF:lDEF:'.$tvCol.':vDEF:0';
 598									foreach ($records as $contentRow)	{
 599											// Add if not already in list
 600										if (!t3lib_div::inList($elements, $contentRow['uid']))	{
 601											$xmlhandler->pasteRecord('ref', 'tt_content|'.$contentRow['uid'], $xmlTarget);
 602										}
 603									} // EOF: foreach ($records as $contentRow)	{
 604								} // EOF: if (is_array($records)&&count($records))	{
 605							} // EOF: foreach ($dataAr['MAPPING'] as $t3Col => $tvCol)	{
 606						} // EOF: if (is_array($dataAr['MAPPING'])&&count())	{
 607					} // EOF: foreach ($dataAr['PAGES'] as $uid => $pageRow)	{
 608				} // EOF: foreach ($subAr as $cnt => $dataAr)	{
 609			} // EOF: foreach ($mainAr as $sub => $subAr)	{
 610		} // EOF: foreach ($similar as $main => $mainAr)	{
 611		return true;
 612	} // EOF: function saveColmapToDB($similar, $data)	{
 613
 614
 615	/**
 616	 * Saves the new DS/TO configuration to the page headers
 617	 *
 618	 * @param	array		Data array
 619	 * @param	array		Submitted data
 620	 * @return	bool		true on success, false on error
 621	 */
 622	function saveDSTOConfToPageheaders($similar, $data)	{
 623		if (!is_array($data))	{
 624			$this->errorMsg = 'Variable expected to be array but was from different type (1)';
 625			return false;
 626		}
 627		$tree = $this->getPageTree($this->id);
 628		foreach ($data as $main => $main_ar)	{
 629			if (!is_array($main_ar))	{
 630				$this->errorMsg = 'Variable expected to be array but was from different type (2)';
 631				return false;
 632			}
 633			foreach ($main_ar as $sub => $data_ar)	{
 634				if (!is_array($data_ar))	{
 635					$this->errorMsg = 'Variable expected to be array but was from different type (3)';
 636					return false;
 637				}
 638				if (!is_array($similar[$main][$sub]))	{
 639					$this->errorMsg = 'Posted path not found in pagestructure array (4)';
 640					return false;
 641				}
 642				foreach ($data_ar as $cnt => $conf_ar)	{
 643					if (!is_array($conf_ar))	{
 644						$this->errorMsg = 'Variable expected to be array but was from different type (5)';
 645						return false;
 646					}
 647					if (!(isset($conf_ar['ds'])&&isset($conf_ar['to'])))	{
 648						$this->errorMsg = 'Expected Key in array not set (6)';
 649						return false;
 650					}
 651					if (!isset($similar[$main][$sub][$cnt]))	{
 652						$this->errorMsg = 'Combination index not set in pagestructure array (7)';
 653						return false;
 654					}
 655					if (!intval($similar[$main][$sub][$cnt]['DSTO_OPTIONS'][$conf_ar['ds']][$conf_ar['to']]))	{
 656						$this->errorMsg = 'Posted DS/TO combination not valid for this Main/Sub Template combination (8)';
 657						return false;
 658					}
 659					$pages = array();
 660					foreach ($similar[$main][$sub][$cnt]['PAGES'] as $uid => $row)	{
 661						$pages[] = $uid;
 662					}
 663					$pages = implode(',', $pages);
 664					$this->rec_setDSTOTree($tree, $pages, $conf_ar['ds'], $conf_ar['to']);
 665				} // EOF: foreach ($data_ar as $cnt => $conf_ar)	{
 666			} // EOF: foreach ($main_ar as $sub => $data_ar)	{
 667		} // EOF: foreach ($data as $main => $main_ar)	{
 668		$err = $this->rec_simplifyDSTOInTree($tree);
 669		if (strlen($err)) {
 670			$this->errorMsg = $err;
 671			return false;
 672		}
 673		$dataAr = array();
 674		$this->rec_setDataArByDSTO($tree, $dataAr);
 675		$tcemain = t3lib_div::makeInstance('t3lib_TCEmain');
 676		$tcemain->start($dataAr, array());
 677		$tcemain->process_datamap();
 678		return true;
 679	} // EOF: function saveDSTOConfToPageheaders($similar, $data)	{
 680
 681
 682	/*******************************************
 683	 *
 684	 * ARRAY MODIFICATION
 685	 *
 686	 * This methods perform operations on the data arrays
 687	 *
 688	 *******************************************/
 689
 690
 691	/**
 692	 * Sets the data array which get's passed to TCAmain by the values of the templavoila DS/TO configuration fields
 693	 * Gets called recursive
 694	 *
 695	 * @param	array		Tree part
 696	 * @param	array		TCAmain datamap array
 697	 * @return	void
 698	 */
 699	function rec_setDataArByDSTO($treePart, &$dataAr)	{
 700		$dataAr['pages'][$treePart['row']['uid']]['tx_templavoila_ds'] = $treePart['row']['tx_templavoila_ds'];
 701		$dataAr['pages'][$treePart['row']['uid']]['tx_templavoila_to'] = $treePart['row']['tx_templavoila_to'];
 702		$dataAr['pages'][$treePart['row']['uid']]['tx_templavoila_next_ds'] = $treePart['row']['tx_templavoila_next_ds'];
 703		$dataAr['pages'][$treePart['row']['uid']]['tx_templavoila_next_to'] = $treePart['row']['tx_templavoila_next_to'];
 704		if (is_array($treePart['childs'])&&count($treePart['childs']))	{
 705			foreach ($treePart['childs'] as $cnt => $data)	{
 706				$this->rec_setDataArByDSTO($treePart['childs'][$cnt], $dataAr);
 707			}
 708		}
 709	}
 710
 711
 712	/**
 713	 * Simplifies the DS/TO structure. In overall it checks how many of the sub-pages of a page are set to specific DS/TO combinations and finds the max set one. Then it sets the Sub-Page DS and TO on this page to the determined value and set all DS/TO settings of the subpages which match this combination to 0
 714	 * Gets called recursive
 715	 *
 716	 * @param	array		Tree part
 717	 * @return	void
 718	 */
 719	function rec_simplifyDSTOInTree(&$treePart)	{
 720		$childDSTO = array();
 721		if (is_array($treePart['childs'])&&count($treePart['childs']))	{
 722			foreach ($treePart['childs'] as $cnt => $data)	{
 723				$childDSTO[$data['row']['tx_templavoila_ds']][$data['row']['tx_templavoila_to']][] = $data['row']['uid'];
 724			}
 725		}
 726		$max = 0;
 727		$maxDS = 0;
 728		$maxTO = 0;
 729		if (is_array($childDSTO)&&count($childDSTO))	{
 730			foreach ($childDSTO as $ds => $ds_ar)	{
 731				foreach ($ds_ar as $to => $to_ar)	{
 732					if (($cnt = count($to_ar))>$max)	{
 733						$max = $cnt;
 734						$maxDS = $ds;
 735						$maxTO = $to;
 736					}
 737				}
 738			}
 739		}
 740		if ($max)	{
 741			if (!$maxDS||!$maxTO)	{
 742				return 'For some reason there is a max TS/DO number of childs in this row but has invalid maxDS or maxTO (9)';
 743			}
 744			$treePart['row']['tx_templavoila_next_ds'] = $maxDS;
 745			$treePart['row']['tx_templavoila_next_to'] = $maxTO;
 746			foreach ($treePart['childs'] as $cnt => $data)	{
 747				if (($data['row']['tx_templavoila_ds']==$maxDS)&&($data['row']['tx_templavoila_to']==$maxTO))	{
 748					$treePart['childs'][$cnt]['row']['tx_templavoila_ds'] = 0;
 749					$treePart['childs'][$cnt]['row']['tx_templavoila_to'] = 0;
 750				}
 751			}
 752		}
 753			// Call recursive
 754		if (is_array($treePart['childs'])&&count($treePart['childs']))	{
 755			foreach ($treePart['childs'] as $cnt => $data)	{
 756				$this->rec_simplifyDSTOInTree($treePart['childs'][$cnt]);
 757			}
 758		}
 759		return '';
 760	}
 761
 762	/**
 763	 * Sets DS/TO recursive in the tree on the given pages.
 764	 * Gets called recursive
 765	 *
 766	 * @param	array		Tree data
 767	 * @param	string		Set DS/TO to the given values on this pages
 768	 * @param	integer		UID of DS record
 769	 * @param	integer		UID of TO record
 770	 * @return	void
 771	 */
 772	function rec_setDSTOTree(&$treePart, $setPages, $ds, $to)	{
 773		if (t3lib_div::inList($setPages, $treePart['row']['uid']))	{
 774			$treePart['row']['tx_templavoila_ds'] = $ds;
 775			$treePart['row']['tx_templavoila_to'] = $to;
 776			$treePart['row']['tx_templavoila_next_ds'] = 0;
 777			$treePart['row']['tx_templavoila_next_to'] = 0;
 778		}
 779		if (is_array($treePart['childs'])&&count($treePart['childs']))	{
 780			foreach ($treePart['childs'] as $key => $child)	{
 781				$this->rec_setDSTOTree($treePart['childs'][$key], $setPages, $ds, $to);
 782			}
 783		}
 784	}
 785
 786	/**
 787	 * Sets the T3 Columns used in the by reference passed data array
 788	 * Extracts data from TSConfig
 789	 *
 790	 * @param	array		Template/DS/TO Combinations (passed by reference)
 791	 * @return	bool		true on success
 792	 */
 793	function setUsedT3Columns(&$similar)	{
 794		if (!is_array($similar)) return false;
 795		foreach ($similar as $main => $main_ar)	{
 796			if (!is_array($main_ar)) return false;
 797			foreach ($main_ar as $sub => $sub_ar)	{
 798				if (!is_array($sub_ar)) return false;
 799				foreach ($sub_ar as $cnt => $data_ar)	{
 800					if (!is_array($data_ar)) return false;
 801					$usedCols = array();
 802					foreach ($data_ar['PAGES'] as $uid => $row)	{
 803						$TSconfig = t3lib_BEfunc::getPagesTSconfig($uid);
 804						$colPos_list = $TSconfig['mod.']['SHARED.']['colPos_list'];
 805						if (!strlen($colPos_list)) $colPos_list = '1,0,2,3';
 806						$usedCols = array_merge($usedCols, t3lib_div::intExplode(',', $colPos_list));
 807						$usedCols = array_unique($usedCols);
 808					}
 809					sort($usedCols);
 810					$similar[$main][$sub][$cnt]['T3_COLUMNS'] = $usedCols;
 811				} // EOF: foreach ($sub_ar as $cnt => $data_ar)	{
 812			} // EOF: foreach ($main_ar as $sub => $sub_ar)	{
 813		} // EOF: foreach ($similar as $main => $main_ar)	{
 814		return true;
 815	}
 816
 817	/**
 818	 * Returns pages on which the same DS/TO combination is possible
 819	 *
 820	 * @param	array		Template Combinations
 821	 * @return	array		Template + DS/TO Combinations
 822	 */
 823	function pagesWithSameDSTO_possible($combos)	{
 824		$similar = array();
 825		foreach ($combos as $main => $main_ar)	{
 826			foreach ($main_ar as $sub => $pages_ar)	{
 827				foreach ($pages_ar as $uid => $row)	{
 828					$found = 0;
 829					if (is_array($similar[$main][$sub]))	{
 830						foreach ($similar[$main][$sub] as $cnt => $data)	{
 831							if (!$this->array_compare($row['_DSTO_OPTIONS'], $data['DSTO_OPTIONS']))	{
 832								$similar[$main][$sub][$cnt]['PAGES'][$row['uid']] = $row;
 833								$found = 1;
 834								break;
 835							}
 836						}
 837					}
 838					if (!$found)	{
 839						$similar[$main][$sub][] = array(
 840							'DSTO_OPTIONS' => $row['_DSTO_OPTIONS'],
 841							'PAGES' => array($row['uid'] => $row),
 842						);
 843					}
 844				}
 845			}
 846		}
 847		return $similar;
 848	}
 849
 850	/**
 851	 * Returns pages on which the same DS/TO combination is set
 852	 *
 853	 * @param	array		Template Combinations
 854	 * @return	array		Template + DS/TO Combinations
 855	 */
 856	function pagesWithSameDSTO_set(&$combos)	{
 857		$similar = array();
 858		foreach ($combos as $main => $main_ar)	{
 859			foreach ($main_ar as $sub => $pages_ar)	{
 860				foreach ($pages_ar as $uid => $row)	{
 861					$found = 0;
 862					if (is_array($similar[$main][$sub]))	{
 863						foreach ($similar[$main][$sub] as $cnt => $data)	{
 864							if (!$this->dsto_compare($row, $data['DSTO_DATA']))	{
 865								$similar[$main][$sub][$cnt]['PAGES'][$row['uid']] = $row;
 866								$found = 1;
 867								break;
 868							}
 869							if (strlen($this->errorMsg))	{
 870								return false;
 871							}
 872						}
 873					}
 874					if (!$found)	{
 875						$ds = $this->getDS($row);
 876						$to = $this->getTO($row);
 877						$toRec = t3lib_BEfunc::getRecord('tx_templavoila_tmplobj', $to);
 878						$dsRec = t3lib_BEfunc::getRecord('tx_templavoila_datastructure', $ds);
 879						$dsXML = t3lib_div::xml2array($dsRec['dataprot']);
 880						$mapping = unserialize($toRec['templatemapping']);
 881						$TVcols = array();
 882						foreach ($mapping['MappingInfo']['ROOT']['el'] as $field => $fieldAr)	{
 883							if ($dsXML['ROOT']['el'][$field]['tx_templavoila']['eType']=='ce')	 {
 884								$TVcols[$field] = $dsXML['ROOT']['el'][$field];
 885							}
 886						}
 887						$similar[$main][$sub][] = array(
 888							'DSTO_DATA' => array(
 889								'ds' => $ds,
 890								'to' => $to,
 891							),
 892							'TV_COLUMNS' => $TVcols,
 893							'PAGES' => array($row['uid'] => $row),
 894						);
 895					}
 896				}
 897			}
 898		}
 899		return $similar;
 900	}
 901
 902
 903	/**
 904	 * Compares if two DS/TO settings are the same.
 905	 * Gets called recursive.
 906	 *
 907	 * @param	array		Page row
 908	 * @param	array		Stored DS/TO settings
 909	 * @return	integer		0 if the settings are equal NOT 0 if different
 910	 */
 911	function dsto_compare($row, $dsto)	{
 912		$this_ds = $this->getDS($row);
 913		$this_to = $this->getTO($row);
 914		if (!($this_ds&&$this_to))	{
 915			$this->errorMsg = 'DS or TO not set for page "'.t3lib_BEfunc::getRecordTitle('pages', $row).'" ('.$row['uid'].') ! (10)';
 916			return -1;
 917		}
 918		if ($this_ds!=$dsto['ds']) return -1;
 919		if ($this_to!=$dsto['to']) return -1;
 920		return 0;
 921	}
 922
 923	/**
 924	 * Compares if two arrays are the same.
 925	 * Gets called recursive.
 926	 *
 927	 * @param	array		First array
 928	 * @param	array		Second array
 929	 * @return	integer		0 if the array are equal NOT 0 if different
 930	 */
 931	function array_compare($a, $b)	{
 932		if (is_array($a)&&is_array($b))	{
 933			foreach ($a as $key => $val)	{
 934				if (!isset($b[$key])) return -1;
 935				if (is_array($a[$key])&&is_array($b[$key]))	{
 936					if ($this->array_compare($a[$key], $b[$key]))	{
 937						return -1;
 938					}
 939				} else {
 940					if ($a[$key]!==$b[$key]) return -1;
 941				}
 942			}
 943		} else {
 944			return -1;
 945		}
 946		return 0;
 947	}
 948
 949	/*******************************************
 950	 *
 951	 * TO/DS METHODS
 952	 *
 953	 *******************************************/
 954
 955
 956	/**
 957	 * Returns the DS name of a given page row
 958	 *
 959	 * @param	array		Page row
 960	 * @return	string		Datastructure name
 961	 */
 962	function getDS($row)	{
 963		$this_ds = intval($row['tx_templavoila_ds']);
 964		if (!$this_ds)	{
 965			$parent = t3lib_BEfunc::getRecord('pages', $row['pid']);
 966			$this_ds = intval($parent['tx_templavoila_next_ds']);
 967		}
 968		return $this_ds;
 969	}
 970
 971	/**
 972	 * Returns the TO name of a given page row
 973	 *
 974	 * @param	array		Page row
 975	 * @return	string		Template Object name
 976	 */
 977	function getTO($row)	{
 978		$this_to= intval($row['tx_templavoila_to']);
 979		if (!$this_to)	{
 980			$parent = t3lib_BEfunc::getRecord('pages', $row['pid']);
 981			$this_to = intval($parent['tx_templavoila_next_to']);
 982		}
 983		return $this_to;
 984	}
 985
 986
 987	/**
 988	 * Returns the Title of a Datastructure
 989	 *
 990	 * @param	integer		Datastructure Id
 991	 * @return	string		Datastructure Name
 992	 */
 993	var $DScache = array();
 994	function getDSName($ds_id)	{
 995		if (!isset($DScache[$ds_id]))	{
 996			$DScache[$ds_id] = t3lib_BEfunc::getRecord('tx_templavoila_datastructure', $ds_id);
 997		}
 998		return t3lib_BEfunc::getRecordTitle('tx_templavoila_datastructure', $DScache[$ds_id]);
 999	}
1000
1001	/**
1002	 * Returns the Title of a TO
1003	 *
1004	 * @param	integer		Template Object Id
1005	 * @return	string		Template Object Name
1006	 */
1007	var $TOcache = array();
1008	function getTOName($to_id)	{
1009		if (!isset($TOcache[$to_id]))	{
1010			$TOcache[$to_id] = t3lib_BEfunc::getRecord('tx_templavoila_tmplobj', $to_id);
1011		}
1012		return t3lib_BEfunc::getRecordTitle('tx_templavoila_tmplobj', $TOcache[$to_id]);
1013	}
1014
1015
1016	/**
1017	 * Returns all DS/TO combinations
1018	 *
1019	 * @param	integer		Root Id
1020	 * @param	array		Template combinations
1021	 * @return	array		Template + DS/TO combinations
1022	 */
1023	function getDSTOCombinations($id, &$combos)	{
1024		$tceforms = t3lib_div::makeInstance('t3lib_TCEforms');
1025		t3lib_div::loadTCA('pages');
1026		$initfields_ds = $tceforms->initItemArray($GLOBALS['TCA']['pages']['columns']['tx_templavoila_ds']);
1027		$initfields_to = $tceforms->initItemArray($GLOBALS['TCA']['pages']['columns']['tx_templavoila_to']);
1028		$this->findingStorageFolderIds();
1029		$storage_folders = $this->getStorageFolders();
1030		foreach ($combos as $main => $sub_ar)	{
1031			foreach ($sub_ar as $sub => $pages_ar)	{
1032				foreach ($pages_ar as $uid => $row)	{
1033					$TSconfig = $tceforms->setTSconfig('pages',$row);
1034					$selItems_ds = $tceforms->addSelectOptionsToItemArray($initfields_ds, $GLOBALS['TCA']['pages']['columns']['tx_templavoila_ds'], $TSconfig, 'tx_templavoila_ds');
1035					foreach ($selItems_ds as $dsAr)	{
1036						if (!($ds = intval($dsAr[1]))) continue;
1037						$selItems_to = array();
1038						foreach ($storage_folders as $storage_folder)	{
1039							$TSconfigTmp = $TSconfig;
1040							$TSconfigTmp['_THIS_ROW']['tx_templavoila_ds'] = $ds;
1041							$TSconfigTmp['_STORAGE_PID'] = $storage_folder['uid'];
1042							$selItems_to = array_merge($selItems_to, $tceforms->addSelectOptionsToItemArray($initfields_to, $GLOBALS['TCA']['pages']['columns']['tx_templavoila_to'], $TSconfigTmp, 'tx_templavoila_to'));
1043						}
1044						foreach ($selItems_to as $toAr)	{
1045							if (!($to = intval($toAr[1]))) continue;
1046							$combos[$main][$sub][$uid]['_DSTO_OPTIONS'][$ds][$to] = 1;
1047						}
1048					} // EOF: foreach ($selItems_ds as $dsAr)	{
1049				} // EOF: foreach ($pages_ar as $uid => $row)	{
1050			} // EOF: foreach ($sub_ar as $sub => $pages_ar)	{
1051		} // EOF: foreach ($combos as $main => $sub_ar)	{
1052	} // EOF: function getDSTOCombinations($id, &$combos)	{
1053
1054
1055	/*******************************************
1056	 *
1057	 * TEMPLATE METHODS
1058	 *
1059	 *******************************************/
1060
1061
1062	/**
1063	 * Returns all template combinations
1064	 *
1065	 * @param	integer		Root Id
1066	 * @return	array		Template combinations
1067	 */
1068	function getTemplateCombinations($id)	{
1069		$tree = $this->getPageTree($id);
1070		$templateCombinations = array();
1071		$this->getTemplateCombination($templateCombinations, $tree['row']);
1072		$this->rec_getTemplateCombination($templateCombinations, $tree['childs']);
1073		return $templateCombinations;
1074	}
1075
1076
1077	/**
1078	 * Returns a single template combination
1079	 *
1080	 * @param	array		All combination (passed by reference)
1081	 * @param	array		Page row
1082	 * @return	void
1083	 */
1084	function getTemplateCombination(&$combos, $row)	{
1085		$main_tmpl = $row['tx_rlmptmplselector_main_tmpl'];
1086		if (!strlen($main_tmpl)) $main_tmpl = '_DEFAULT';
1087		if (!strcmp($main_tmpl, '0')) $main_tmpl = '_DEFAULT';
1088		$ca_tmpl = $row['tx_rlmptmplselector_ca_tmpl'];
1089		if (!strlen($ca_tmpl)) $ca_tmpl = '_DEFAULT';
1090		if (!strcmp($ca_tmpl, '0')) $ca_tmpl = '_DEFAULT';
1091		$combos[$main_tmpl][$ca_tmpl][$row['uid']] = $row;
1092	}
1093
1094	/**
1095	 * Sets first parameter to all possible template combinations.
1096	 * Gets called recursive.
1097	 *
1098	 * @param	array		All combination (passed by reference)
1099	 * @param	array		Actual part of tree
1100	 * @return	void
1101	 */
1102	function rec_getTemplateCombination(&$combos, $rows)	{
1103		foreach ($rows as $row)	{
1104			$this->getTemplateCombination($combos, $row['row']);
1105			$this->rec_getTemplateCombination($combos, $row['childs']);
1106		}
1107	}
1108
1109
1110
1111	/*******************************************
1112	 *
1113	 * Record methods
1114	 *
1115	 *******************************************/
1116
1117	/**
1118	 * Returns an page tree
1119	 *
1120	 * @param	integer		Root Id
1121	 * @return	array		Page tree
1122	 */
1123	function getPageTree($id)	{
1124		$row = t3lib_BEfunc::getRecord('pages', $id);
1125		if (is_array($row))	{
1126			$tmpChilds = t3lib_BEfunc::getRecordsByField('pages', 'pid', $row['uid'], ' AND doktype<200', '', 'sorting');
1127			$childs = array();
1128			if (is_array($tmpChilds) && count($tmpChilds))	{
1129				foreach ($tmpChilds as $key => $child) {
1130					if (!intval($child['uid'])) continue;
1131					$childs[] = $this->getPageTree($child['uid']);
1132				}
1133			}
1134			return array('row' => $row, 'childs' => $childs);
1135		} else {
1136			return false;
1137		}
1138	}
1139
1140	/****************************************
1141	 *
1142	 * STORAGE FOLDERS AND TEMPLATES
1143	 *
1144	 * This methods return the Storage folders and existing Templates
1145	 *
1146	 ****************************************/
1147
1148	/**
1149	 * Generates $this->storageFolders with available sysFolders linked to as storageFolders for the user
1150	 *
1151	 * @return	void		Modification in $this->storageFolders array
1152	 */
1153	function findingStorageFolderIds()	{
1154		global $TYPO3_DB;
1155
1156			// Init:
1157		$readPerms = $GLOBALS['BE_USER']->getPagePermsClause(1);
1158		$this->storageFolders=array();
1159
1160			// Looking up all references to a storage folder:
1161		$res = $TYPO3_DB->exec_SELECTquery (
1162			'uid,storage_pid',
1163			'pages',
1164			'storage_pid>0'.t3lib_BEfunc::deleteClause('pages')
1165		);
1166		while($row = $TYPO3_DB->sql_fetch_assoc($res))	{
1167			if ($GLOBALS['BE_USER']->isInWebMount($row['storage_pid'],$readPerms))	{
1168				$storageFolder = t3lib_BEfunc::getRecord('pages',$row['storage_pid'],'uid,title');
1169				if ($storageFolder['uid'])	{
1170					$this->storageFolders[$storageFolder['uid']] = $storageFolder['title'];
1171				}
1172			}
1173		}
1174
1175			// Compopsing select list:
1176		$sysFolderPIDs = array_keys($this->storageFolders);
1177		$sysFolderPIDs[]=0;
1178		$this->storageFolders_pidList = implode(',',$sysFolderPIDs);
1179	}
1180
1181
1182	/**
1183	 * Returns an array containing all storage folders
1184	 *
1185	 * @return	array		Storage folders
1186	 */
1187	function getStorageFolders()	{
1188		$folders = array();
1189		$ids = t3lib_div::trimExplode(',', $this->storageFolders_pidList, 1);
1190		foreach ($ids as $id)	{
1191			if (intval($id))	{
1192				$row = t3lib_BEfunc::getRecord('pages', $id);
1193				if (is_array($row))	{
1194					$folders[] = $row;
1195				}
1196			}
1197		}
1198		return $folders;
1199	}
1200
1201	/****************************************
1202	 *
1203	 * EXTRA METHODS
1204	 *
1205	 * Some methods we need
1206	 *
1207	 ****************************************/
1208
1209	/**
1210	 * Returns an label of a TCA 'group' items entry of a specific value
1211	 *
1212	 * @param	array		Items from TCA configuration
1213	 * @param	mixed		Value
1214	 * @return	string		Label
1215	 */
1216	function getItemLabel($items, $val)	{
1217		if (is_array($items)&&count($items))	{
1218			foreach ($items as $item)	{
1219				if ($item[1]==$val) return $item[0];
1220			}
1221		}
1222		return '[INVALID VALUE]';
1223	}
1224
1225}
1226
1227
1228
1229if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/kb_tv_migrate/mod1/index.php'])	{
1230	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/kb_tv_migrate/mod1/index.php']);
1231}
1232
1233
1234
1235
1236// Make instance:
1237$SOBE = t3lib_div::makeInstance('tx_kbtvmigrate_module1');
1238$SOBE->init();
1239
1240// Include files?
1241foreach($SOBE->include_once as $INC_FILE)	include_once($INC_FILE);
1242
1243$SOBE->main();
1244$SOBE->printContent();
1245
1246?>