Index: class.ext_update.php =================================================================== --- class.ext_update.php (revision 34999) +++ class.ext_update.php (working copy) @@ -25,30 +25,23 @@ * Class for updating the db * * @author Rene Fritz + * @author Dmitry Dulepov * @package TYPO3-DAM * @subpackage tx_dam */ class ext_update { + protected $updateActionsReport = array(); + /** * Main function, returning the HTML content of the module * * @return string HTML */ function main() { - - if (!t3lib_div::_GP('do_update')) { - $onClick = "document.location.href='".t3lib_div::linkThisScript(array('do_update'=>1))."'; return false;"; - - return 'Do you want to perform the database update now? - -
- '; - } else { - - return $this->perform_update(); + $this->setupLanguageLabels(); + return $this->performUpdate(); } - } /** * Checks how many rows are found and returns true if there are any @@ -56,88 +49,116 @@ * @return boolean */ function access() { + return true; + } + /** + * Do the DB update + * + * @return string HTML + */ + protected function performUpdate() { - // Just do the upgrade without asking - $this->perform_update(); - return false; + $this->updateMediaToSysfolder(); + $this->renameFlexFormField(); + $this->renameMMSortingField(); + $this->localizeMedia(); + return $this->generateUpdateReport(); + } - //------------------------- - - $doit = false; - - $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('count(*)', 'pages', 'module='.$GLOBALS['TYPO3_DB']->fullQuoteStr('dam', 'pages').' AND doktype<254'); - $row = $GLOBALS['TYPO3_DB']->sql_fetch_row($res); - $doit = $row[0] ? true : $doit; - - $res = $GLOBALS['TYPO3_DB']->admin_get_fields('tt_content'); - - if (isset($res['tx_dam_flexform']) && !isset($res['ce_flexform'])) { - $doit = true; + /** + * Includes language labels for this module. + * + * @return void + */ + protected function setupLanguageLabels() { + $GLOBALS['LANG']->includeLLfile('EXT:dam/locallang_db.xml'); } - if (t3lib_div::compat_version('4.1')) { - $res = $GLOBALS['TYPO3_DB']->admin_get_fields('tx_dam_mm_ref'); - if (!isset($res['sorting_foreign'])) { - $doit = true; - } else { - $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('count(sorting_foreign)', 'tx_dam_mm_ref', 'sorting_foreign>0'); - if ($rows['0']['count(sorting_foreign)']==0) { - $doit = true; + /** + * Sets all "Media" pages to be sysfolders. + * + * @return void + */ + protected function updateMediaToSysfolder() { + $GLOBALS['TYPO3_DB']->exec_UPDATEquery('pages', + 'module=' . $GLOBALS['TYPO3_DB']->fullQuoteStr('dam', 'pages'), + array('doktype'=>'254')); + if ($GLOBALS['TYPO3_DB']->sql_affected_rows() > 0) { + $this->updateActionsReport[] = $GLOBALS['LANG']->getLL('tx_dam.update.media.sysfolder'); } } + + /** + * Renames tx_dam_flexform to ce_flexform. + * + * @return void + */ + protected function renameFlexFormField() { + $fieldList = $GLOBALS['TYPO3_DB']->admin_get_fields('tt_content'); + if (isset($fieldList['tx_dam_flexform']) && !isset($fieldList['ce_flexform'])) { + $GLOBALS['TYPO3_DB']->admin_query('ALTER TABLE tt_content CHANGE tx_dam_flexform ce_flexform mediumtext NOT NULL'); + $this->updateActionsReport[] = $GLOBALS['LANG']->getLL('tx_dam.update.flexform.fields'); } - return $doit; } - - /** - * Do the DB update + * Renames sorting field from foreign_sorting to sorting. * - * @return string HTML + * @return void */ - function perform_update() { - - $content = ''; - - $GLOBALS['TYPO3_DB']->exec_UPDATEquery('pages', 'module='.$GLOBALS['TYPO3_DB']->fullQuoteStr('dam', 'pages').'', array('doktype'=>'254')); - $content .= 'Updated Media folder to be a SysFolder
'; - - $res = $GLOBALS['TYPO3_DB']->admin_get_fields('tt_content'); - if (isset($res['tx_dam_flexform']) && !isset($res['ce_flexform'])) { - $GLOBALS['TYPO3_DB']->admin_query('ALTER TABLE tt_content CHANGE tx_dam_flexform ce_flexform mediumtext NOT NULL'); - $content .= 'Renamed field tt_content.tx_dam_flexform to ce_flexform
'; - } - - if (t3lib_div::compat_version('4.1')) { + protected function renameMMSortingField() { $existingTables=$GLOBALS['TYPO3_DB']->admin_get_tables(); if(isset($existingTables['tx_dam_mm_ref'])) { - $res = $GLOBALS['TYPO3_DB']->admin_get_fields('tx_dam_mm_ref'); - if (isset($res['sorting_foreign'])) { + $fieldList = $GLOBALS['TYPO3_DB']->admin_get_fields('tx_dam_mm_ref'); + if (isset($fieldList['sorting_foreign'])) { # for testing only: $GLOBALS['TYPO3_DB']->exec_UPDATEquery('tx_dam_mm_ref', '', array('sorting_foreign'=>'0')); - $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('count(sorting_foreign)', 'tx_dam_mm_ref', 'sorting_foreign>0'); - if ($rows['0']['count(sorting_foreign)']==0) { + list($row) = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('count(sorting_foreign)', 'tx_dam_mm_ref', 'sorting_foreign>0'); + if ($row['count(sorting_foreign)'] == 0) { $GLOBALS['TYPO3_DB']->admin_query('ALTER TABLE tx_dam_mm_ref DROP sorting_foreign'); - unset($res['sorting_foreign']); + unset($fieldList['sorting_foreign']); } } - if (!isset($res['sorting_foreign'])) { + if (!isset($fieldList['sorting_foreign'])) { $GLOBALS['TYPO3_DB']->admin_query('ALTER TABLE tx_dam_mm_ref CHANGE sorting sorting_foreign int(11) unsigned DEFAULT 0 NOT NULL'); $GLOBALS['TYPO3_DB']->admin_query('ALTER TABLE tx_dam_mm_ref ADD sorting int(11) unsigned DEFAULT 0 NOT NULL'); - $content .= 'Renamed field tx_dam_mm_ref.sorting to sorting_foreign
'; + $this->updateActionsReport[] = $GLOBALS['LANG']->getLL('tx_dam.update.mm.sorting'); } } } - return $content; + /** + * Localizes DAM records if necessary + * + * @return vlid + */ + protected function localizeMedia() { + t3lib_div::requireOnce(t3lib_extMgm::extPath('dam', 'lib/class.tx_dam_sysfolder.php')); + if (tx_dam_sysfolder::checkLocalization()) { + $this->updateActionsReport[] = $GLOBALS['LANG']->getLL('tx_dam.update.media.localization'); } + } - + /** + * Generates update report. + * + * @return string + */ + protected function generateUpdateReport() { + $result = ''; + if (count($this->updateActionsReport) == 0) { + $result = '

' . $GLOBALS['LANG']->getLL('tx_dam.update.up-to-date') . '

'; } + else { + $result = '

' . $GLOBALS['LANG']->getLL('tx_dam.update.update.list') . '

'; + $result = ''; + } + return $result; + } +} if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/dam/class.ext_update.php']) { Index: locallang_db.xml =================================================================== --- locallang_db.xml (revision 34999) +++ locallang_db.xml (working copy) @@ -217,6 +217,12 @@ + + + + + + Index: lib/class.tx_dam_sysfolder.php =================================================================== --- lib/class.tx_dam_sysfolder.php (revision 34999) +++ lib/class.tx_dam_sysfolder.php (working copy) @@ -69,30 +69,43 @@ if (!is_object($GLOBALS['TYPO3_DB'])) return false; - $damFolders = tx_dam_sysfolder::getAvailable(); - if (!count($damFolders)) { + $mediaPid = self::getMediaPid(); + if ($mediaPid == 0) { // creates a DAM folder on the fly tx_dam_sysfolder::create(); - $damFolders = tx_dam_sysfolder::getAvailable(); + $mediaPid = self::getMediaPid(); } - $df = current($damFolders); - return $df['uid']; + return $mediaPid; } + /** + * Obtains a pid for the "Media" sysfolder. + * + * @return integer + */ + static public function getMediaPid() { + $damPid = 0; + $damFolders = self::getAvailable(); + if (count($damFolders) > 0) { + $record = current($damFolders); + $damPid = $record['uid']; + } + return $damPid; + } + /** * Find the DAM folders and return an array of record arrays. * * @return array Array of rows of found DAM folders with fields: uid, pid, title. An empty array will be returned if no folder was found. */ function getAvailable() { - $rows=array(); - if ($damFolders = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('uid,pid,title,doktype', 'pages', 'module='.$GLOBALS['TYPO3_DB']->fullQuoteStr('dam', 'pages').' AND deleted=0', '', '', '', 'uid')) { - $rows = $damFolders; + return $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('uid,pid,title,doktype', + 'pages', + 'module=' . $GLOBALS['TYPO3_DB']->fullQuoteStr('dam', 'pages') . + self::enableFields('pages')); } - return $rows; - } /** @@ -123,11 +136,114 @@ $fields_values['module'] = 'dam'; $fields_values['crdate'] = time(); $fields_values['tstamp'] = time(); - return $GLOBALS['TYPO3_DB']->exec_INSERTquery('pages', $fields_values); + // TODO Dmitry: If we are in BE, it must be a TCEmain call, not a TYPO3_DB call! + $GLOBALS['TYPO3_DB']->exec_INSERTquery('pages', $fields_values); } + /** + * Checks that DAM is localized to all system languages. This function works + * only in BE mode due to constraints imposed by TCEmain. + * + * @return boolean true if changes were made + */ + static public function checkLocalization($mediaPid = 0) { + $result = false; + if (TYPO3_MODE == 'BE') { + if ($mediaPid == 0) { + $mediaPid = self::getMediaPid(); + } + $availableLanguages = self::getAvailableLanguages(); + if (count($availableLanguages) > 0) { + $existingLanguages = self::getExistingMedialanguages($mediaPid); + $newLanguages = array_diff($availableLanguages, $existingLanguages); + if (count($newLanguages) > 0) { + self::createMediaLocalizations($mediaPid, $newLanguages); + $result = true; + } + } + } + return $result; + } /** + * Obtains a list of all language id values that are available to the system. + * + * @return array + */ + static protected function getAvailableLanguages() { + $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('uid', 'sys_language', + '1=1' . self::enableFields('sys_language')); + $result = array(); + foreach ($rows as $row) { + $result[] = $row['uid']; + } + return $result; + } + + /** + * Obtains a list of language id values for the media pid. + * + * @param int $mediaPid + * @return array + */ + static protected function getExistingMediaLanguages($mediaPid) { + $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('sys_language_uid', + 'pages_language_overlay', 'pid=' . intval($mediaPid) . + self::enableFields('pages_language_overlay')); + $result = array(); + foreach ($rows as $row) { + $result[] = $row['sys_language_uid']; + } + return $result; + } + + /** + * Localizes media record to given languages. Note: the function does not + * check if the array of language contains anything. It will call TCEmain + * straight away. To prevent performance problems, make sure you call + * this function only when there is something for it to do! + * + * @param int $mediaPid + * @param array $newLanguages + * @return void + */ + static protected function createMediaLocalizations($mediaPid, array $newLanguages) { + $commandMap = array( + 'pages' => array( + ) + ); + foreach ($newLanguages as $languageId) { + $commandMap['pages'][$mediaPid] = array( + 'localize' => $languageId + ); + } + + t3lib_div::requireOnce(PATH_t3lib . 'class.t3lib_tcemain.php'); + $tceMain = t3lib_div::makeInstance('t3lib_TCEmain'); + /* @var t3lib_TCEmain $tceMain */ + $tceMain->start(null, $commandMap); + $tceMain->process_cmdmap(); + } + + /** + * Implements "enableFields" functionality regardless of the TYPO3 mode. + * + * @param string $table + * @return string + */ + static protected function enableFields($table) { + t3lib_div::loadTCA($table); + if (TYPO3_MODE == 'BE') { + t3lib_div::requireOnce(PATH_t3lib . 'class.t3lib_befunc.php'); + $result = t3lib_BEfunc::BEenableFields($table) . t3lib_BEfunc::deleteClause($table); + } + else { + $result = $GLOBALS['TSFE']->sys_page->enableFields($table); + } + return $result; + } + + /** * Move lost DAM records to the DAM sysfolder. * This is a maintance function. * Index: binding/tce/class.tx_dam_tce_process.php =================================================================== --- binding/tce/class.tx_dam_tce_process.php (revision 34999) +++ binding/tce/class.tx_dam_tce_process.php (working copy) @@ -116,7 +116,24 @@ } // todo: implement DB-Trigger - can't remember what this means } + + /** + * Ensures that "Media" sysfolder is properly localized. Note that the + * list of parameters for this function is shorter than the real callback + * uses. + * + * @param string $status + * @param string $table + */ + public function processDatamap_afterDatabaseOperations($status, $table) { + if ($table == 'sys_language') { + if (!class_exists('tx_dam_sysfolder', false)) { + t3lib_div::requireOnce(t3lib_extMgm::extPath('dam') . 'lib/class.tx_dam_sysfolder.php'); } + tx_dam_sysfolder::checkLocalization(); + } + } +} if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/dam/binding/tce/class.tx_dam_tce_process.php']) {