/** * Hook that allows to dynamically extend the table definitions for e.g. custom * caches. The hook implementation may return table create strings that will be * respected by the extension manager during installation of an extension. * * @param string $extKey Extension key * @param array $extInfo Extension information array * @param string $fileContent Content of the current extension sql file * @param t3lib_install $instObj Instance of the installer * @param t3lib_install_Sql $instSqlObj Instance of the installer sql object * @param tx_em_Install $parent The calling parent object * @return string Either empty string or table create strings */ public function appendTableDefinitions($extKey, array $extInfo, $fileContent, t3lib_install $instObj, t3lib_install_Sql $instSqlObj, tx_em_Install $parent) { global $TCA; $extensionFlatSettings = Tx_Contexts_Api_Configuration::getExtensionFlatSettings(); if (!array_key_exists($extKey, $extensionFlatSettings)) { return ''; } $sql = ''; foreach ($extensionFlatSettings[$extKey] as $table => $settings) { $sql .= "\nCREATE TABLE {$table} (\n"; foreach ($settings as $setting) { $flatColumns = Tx_Contexts_Api_Configuration::getFlatColumns($table, $setting); $sql .= $flatColumns[0] . " tinytext NOT NULL,\n"; $sql .= $flatColumns[1] . " tinytext NOT NULL\n"; } $sql .= ');'; } return $sql; }
/** * Query a setting record and retrieve the value object * if one was found. * * @param string $table Database table name * @param string $setting Setting name * @param string $uid Record UID * @param array $arRow Database row for the given UID. * Useful for flat settings. * * @return Tx_Contexts_Context_Setting|null NULL when not enabled * and not disabled */ public final function getSetting($table, $setting, $uid, $arRow = null) { if ($arRow !== null) { //if it's a flat column, use the settings directly from the // database row instead of relying on the tx_contexts_settings // table $arFlatColumns = Tx_Contexts_Api_Configuration::getFlatColumns($table, $setting); if (isset($arRow[$arFlatColumns[0]]) && isset($arRow[$arFlatColumns[1]])) { return Tx_Contexts_Context_Setting::fromFlatData($this, $table, $setting, $arFlatColumns, $arRow); } } $settings = $this->getSettings($table, $uid, $arRow); return array_key_exists($setting, $settings) ? $settings[$setting] : null; }
/** * Tries to get if the setting is enabled by evaluating the flat columns * within the record * * @param string $table Table name * @param string $setting Setting name * @param array $row Record array * * @return null|boolean NULL when table has no flat settings or the record * doesn't contain the appropriate flat columns * boolean otherwise */ protected static function isSettingEnabledFlat($table, $setting, array $row) { $flatColumns = Tx_Contexts_Api_Configuration::getFlatColumns($table, $setting); if (!$flatColumns) { return null; } $rowValid = true; $flatColumnContents = array(); foreach ($flatColumns as $i => $flatColumn) { if (!array_key_exists($flatColumn, $row)) { t3lib_div::devLog('Missing flat field "' . $flatColumn . '"', 'tx_contexts', t3lib_div::SYSLOG_SEVERITY_WARNING, array('table' => $table, 'row' => $row)); $rowValid = false; } elseif ($row[$flatColumn] !== '') { $flatColumnContents[$i] = array_flip(explode(',', $row[$flatColumn])); } else { $flatColumnContents[$i] = array(); } } if (!$rowValid) { return null; } foreach (Tx_Contexts_Context_Container::get() as $context) { if (array_key_exists($context->getUid(), $flatColumnContents[0])) { return false; } } return true; }
/** * Add setting columns to the TCA. * * @param string $table Table to add settings to * @param array $settings Array of settings to register. * Key is the setting name, value its title * * @return void */ protected static function addToTcaColumns($table, array $settings) { global $TCA; t3lib_div::loadTCA($table); if (!isset($TCA[$table])) { return; } t3lib_div::loadTCA('tx_contexts_contexts'); if (!array_key_exists(self::RECORD_SETTINGS_COLUMN, $TCA[$table]['columns'])) { $recordSettingsConf = array("exclude" => 1, "label" => 'LLL:' . self::LANG_FILE . ':tabname', "config" => array("type" => "user", "size" => "30", "userFunc" => 'Tx_Contexts_Service_Tca->renderRecordSettingsField', 'settings' => $settings)); $arColumns = array(self::RECORD_SETTINGS_COLUMN => $recordSettingsConf); $arFlatColumns = Tx_Contexts_Api_Configuration::getFlatColumns($table); if (count($arFlatColumns)) { //add passthrough fields to keep settings when copying records foreach ($arFlatColumns as $arSetting) { foreach ($arSetting as $columnName) { $arColumns[$columnName] = array('config' => array('type' => 'passthrough')); } } } t3lib_extMgm::addTCAcolumns($table, $arColumns, 1); switch ($table) { case 'pages': t3lib_extMgm::addToAllTCAtypes($table, self::RECORD_SETTINGS_COLUMN, '1,4,5', 'after:fe_group'); t3lib_extMgm::addToAllTCAtypes($table, self::RECORD_SETTINGS_COLUMN, '254', 'after:hidden'); break; case 'tt_content': t3lib_extMgm::addToAllTCAtypes($table, self::RECORD_SETTINGS_COLUMN, '', 'after:fe_group'); break; } } else { $TCA[$table]['columns'][self::RECORD_SETTINGS_COLUMN]['config']['settings'] = array_merge($TCA[$table]['columns'][self::RECORD_SETTINGS_COLUMN]['config']['settings'], $settings); } $defaultSettingsColumn = 'default_settings_' . $table; if (!array_key_exists($defaultSettingsColumn, $TCA['tx_contexts_contexts']['columns'])) { $defaultSettingsConf = array("exclude" => 1, 'label' => $TCA[$table]['ctrl']['title'], 'config' => array('type' => 'user', 'size' => 30, 'userFunc' => 'Tx_Contexts_Service_Tca->renderDefaultSettingsField', 'table' => $table, 'settings' => $settings)); t3lib_extMgm::addTCAcolumns('tx_contexts_contexts', array($defaultSettingsColumn => $defaultSettingsConf), 1); t3lib_extMgm::addToAllTCAtypes('tx_contexts_contexts', $defaultSettingsColumn); } else { $TCA['tx_contexts_contexts']['columns'][$defaultSettingsColumn]['config']['settings'] = array_merge($TCA['tx_contexts_contexts']['columns'][$defaultSettingsColumn]['config']['settings'], $settings); } }
/** * Saves the settings which were configured to be flattened into theyr flat * columns on the table to allow quicker queries in enableField hook and to * save queries for already fetched rows * hook. * * @param string $table * @param int $uid * @param array $contextsAndSettings Array of settings. * Key is the context UID. * Value is an array of setting names * and their value, e.g. * tx_contexts_visibility => '', * menu_visibility => '0' * '' = undecided, 1 - on, 0 - off * @return void * @see Tx_Contexts_Service_Tsfe::enableFields() */ protected function saveFlatSettings($table, $uid, $contextsAndSettings) { $values = array(); $flatSettingColumns = Tx_Contexts_Api_Configuration::getFlatColumns($table); foreach ($flatSettingColumns as $setting => $flatColumns) { $values[$flatColumns[0]] = array(); $values[$flatColumns[1]] = array(); foreach ($contextsAndSettings as $contextId => $settings) { if ($settings[$setting] === '0' || $settings[$setting] === '1') { $values[$flatColumns[$settings[$setting]]][] = $contextId; } } } if (count($values)) { foreach ($values as $colname => &$val) { $val = implode(',', $val); } Tx_Contexts_Api_Configuration::getDb()->exec_UPDATEquery($table, 'uid=' . $uid, $values); } }
/** * Generates a SQL WHERE statement that filters out records * that may not be accessed with the current context settings * * @param string $table Database table name * @return string SQL filter string beginning with " AND " */ protected function getFilterSql($table) { global $TCA; $sql = ''; foreach (Tx_Contexts_Api_Configuration::getEnableSettings($table) as $setting) { $flatColumns = Tx_Contexts_Api_Configuration::getFlatColumns($table, $setting); if (!$flatColumns) { t3lib_div::devLog('Missing flat columns for setting "' . $setting . '"', 'tx_contexts', 2, array('table' => $table)); continue; } $enableChecks = array($flatColumns[1] . " = ''"); $disableChecks = array(); foreach (Tx_Contexts_Context_Container::get() as $context) { /* @var $context Tx_Contexts_Context_Abstract */ $enableChecks[] = $GLOBALS['TYPO3_DB']->listQuery($flatColumns[1], $context->getUid(), $table); $disableChecks[] = 'NOT ' . $GLOBALS['TYPO3_DB']->listQuery($flatColumns[0], $context->getUid(), $table); } $sql = ' AND (' . implode(' OR ', $enableChecks) . ')'; if (count($disableChecks)) { $sql .= ' AND (' . $flatColumns[0] . " = ''" . ' OR (' . implode(' AND ', $disableChecks) . ')' . ')'; } } return $sql; }