public function buildQuickForm() { $config = CRM_Core_Config::singleton(); global $tsLocale; $this->_locales = array_keys($config->languageLimit); // get the part of the database we want to edit and validate it $table = CRM_Utils_Request::retrieve('table', 'String', $this); $field = CRM_Utils_Request::retrieve('field', 'String', $this); $id = CRM_Utils_Request::retrieve('id', 'Int', $this); $this->_structure = CRM_Core_I18n_SchemaStructure::columns(); if (!isset($this->_structure[$table][$field])) { CRM_Core_Error::fatal("{$table}.{$field} is not internationalized."); } $this->addElement('hidden', 'table', $table); $this->addElement('hidden', 'field', $field); $this->addElement('hidden', 'id', $id); $cols = array(); foreach ($this->_locales as $locale) { $cols[] = "{$field}_{$locale} {$locale}"; } $query = 'SELECT ' . implode(', ', $cols) . " FROM {$table} WHERE id = {$id}"; $dao = new CRM_Core_DAO(); $dao->query($query, FALSE); $dao->fetch(); // get html type and attributes for this field $widgets = CRM_Core_I18n_SchemaStructure::widgets(); $widget = $widgets[$table][$field]; // attributes $attributes = array('class' => ''); if (isset($widget['rows'])) { $attributes['rows'] = $widget['rows']; } if (isset($widget['cols'])) { $attributes['cols'] = $widget['cols']; } $required = !empty($widget['required']); if ($widget['type'] == 'RichTextEditor') { $widget['type'] = 'wysiwyg'; $attributes['class'] .= ' collapsed'; } $languages = CRM_Core_I18n::languages(TRUE); foreach ($this->_locales as $locale) { $attr = $attributes; $name = "{$field}_{$locale}"; if ($locale == $tsLocale) { $attr['class'] .= ' default-lang'; } $this->add($widget['type'], $name, $languages[$locale], $attr, $required); $this->_defaults[$name] = $dao->{$locale}; } $this->addDefaultButtons(ts('Save'), 'next', NULL); CRM_Utils_System::setTitle(ts('Languages')); $this->assign('locales', $this->_locales); $this->assign('field', $field); }
function buildQuickForm() { $config = CRM_Core_Config::singleton(); $this->_locales = array_keys($config->languageLimit); // get the part of the database we want to edit and validate it $table = CRM_Utils_Request::retrieve('table', 'String', $this); $field = CRM_Utils_Request::retrieve('field', 'String', $this); $id = CRM_Utils_Request::retrieve('id', 'Int', $this); $this->_structure = CRM_Core_I18n_SchemaStructure::columns(); if (!isset($this->_structure[$table][$field])) { CRM_Core_Error::fatal("{$table}.{$field} is not internationalized."); } $this->addElement('hidden', 'table', $table); $this->addElement('hidden', 'field', $field); $this->addElement('hidden', 'id', $id); $cols = array(); foreach ($this->_locales as $locale) { $cols[] = "{$field}_{$locale} {$locale}"; } $query = 'SELECT ' . implode(', ', $cols) . " FROM {$table} WHERE id = {$id}"; $dao = new CRM_Core_DAO(); $dao->query($query, FALSE); $dao->fetch(); // we want TEXTAREAs for long fields and INPUTs for short ones $this->_structure[$table][$field] == 'text' ? $type = 'textarea' : ($type = 'text'); $languages = CRM_Core_I18n::languages(TRUE); foreach ($this->_locales as $locale) { $this->addElement($type, "{$field}_{$locale}", $languages[$locale], array('cols' => 60, 'rows' => 3)); $this->_defaults["{$field}_{$locale}"] = $dao->{$locale}; } $this->addButtons(array(array('type' => 'next', 'name' => ts('Save'), 'isDefault' => TRUE))); global $tsLocale; $this->assign('tsLocale', $tsLocale); $this->assign('locales', $this->_locales); $this->assign('field', $field); $this->assign('context', CRM_Utils_Request::retrieve('context', 'String', $this)); }
static function createIndexes(&$tables, $createIndexPrefix = 'index', $substrLenghts = array()) { $queries = array(); require_once 'CRM/Core/DAO/Domain.php'; $domain = new CRM_Core_DAO_Domain(); $domain->find(TRUE); $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales); // if we're multilingual, cache the information on internationalised fields static $columns = NULL; if (!CRM_Utils_System::isNull($locales) and $columns === NULL) { $columns = CRM_Core_I18n_SchemaStructure::columns(); } foreach ($tables as $table => $fields) { $query = "SHOW INDEX FROM {$table}"; $dao = CRM_Core_DAO::executeQuery($query); $currentIndexes = array(); while ($dao->fetch()) { $currentIndexes[] = $dao->Key_name; } // now check for all fields if the index exists foreach ($fields as $field) { // handle indices over substrings, CRM-6245 // $lengthName is appended to index name, $lengthSize is the field size modifier $lengthName = isset($substrLenghts[$table][$field]) ? "_{$substrLenghts[$table][$field]}" : ''; $lengthSize = isset($substrLenghts[$table][$field]) ? "({$substrLenghts[$table][$field]})" : ''; $names = array("index_{$field}{$lengthName}", "FK_{$table}_{$field}{$lengthName}", "UI_{$field}{$lengthName}", "{$createIndexPrefix}_{$field}{$lengthName}"); // skip to the next $field if one of the above $names exists; handle multilingual for CRM-4126 foreach ($names as $name) { $regex = '/^' . preg_quote($name) . '(_[a-z][a-z]_[A-Z][A-Z])?$/'; if (preg_grep($regex, $currentIndexes)) { continue 2; } } // the index doesn't exist, so create it // if we're multilingual and the field is internationalised, do it for every locale if (!CRM_Utils_System::isNull($locales) and isset($columns[$table][$field])) { foreach ($locales as $locale) { $queries[] = "CREATE INDEX {$createIndexPrefix}_{$field}{$lengthName}_{$locale} ON {$table} ({$field}_{$locale}{$lengthSize})"; } } else { $queries[] = "CREATE INDEX {$createIndexPrefix}_{$field}{$lengthName} ON {$table} ({$field}{$lengthSize})"; } } } // run the queries without i18n-rewriting $dao = new CRM_Core_DAO(); foreach ($queries as $query) { $dao->query($query, FALSE); } }
static function createIndexes(&$tables, $createIndexPrefix = 'index') { $queries = array(); $domain = new CRM_Core_DAO_Domain(); $domain->find(true); $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales); // if we're multilingual, cache the information on internationalised fields static $columns = null; require_once 'CRM/Utils/System.php'; if (!CRM_Utils_System::isNull($locales) and $columns === null) { require_once 'CRM/Core/I18n/SchemaStructure.php'; $columns =& CRM_Core_I18n_SchemaStructure::columns(); } foreach ($tables as $table => $fields) { $query = "SHOW INDEX FROM {$table}"; $dao = CRM_Core_DAO::executeQuery($query); $currentIndexes = array(); while ($dao->fetch()) { $currentIndexes[] = $dao->Key_name; } // now check for all fields if the index exists foreach ($fields as $field) { $names = array("index_{$field}", "FK_{$table}_{$field}", "UI_{$field}", "{$createIndexPrefix}_{$field}"); // skip to the next $field if one of the above $names exists; handle multilingual for CRM-4126 foreach ($names as $name) { $regex = '/^' . preg_quote($name) . '(_[a-z][a-z]_[A-Z][A-Z])?$/'; if (preg_grep($regex, $currentIndexes)) { continue 2; } } // the index doesn't exist, so create it // if we're multilingual and the field is internationalised, do it for every locale if (!CRM_Utils_System::isNull($locales) and isset($columns[$table][$field])) { foreach ($locales as $locale) { $queries[] = "CREATE INDEX {$createIndexPrefix}_{$field}_{$locale} ON {$table} ({$field}_{$locale})"; } } else { $queries[] = "CREATE INDEX {$createIndexPrefix}_{$field} ON {$table} ({$field})"; } } } // run the queries without i18n-rewriting $dao = new CRM_Core_DAO(); foreach ($queries as $query) { $dao->query($query, false); } }
/** * Add a new locale to a multi-lang db, setting * its values to the current default locale. * * @param $locale string the new locale to add * @param $source string the locale to copy from * @return void */ function addLocale($locale, $source) { // get the current supported locales $domain =& new CRM_Core_DAO_Domain(); $domain->find(true); $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales); // break early if the locale is already supported if (in_array($locale, $locales)) { return; } $dao =& new CRM_Core_DAO(); // build the required SQL queries $columns =& CRM_Core_I18n_SchemaStructure::columns(); $indices =& CRM_Core_I18n_SchemaStructure::indices(); $queries = array(); foreach ($columns as $table => $hash) { // add new columns foreach ($hash as $column => $type) { $queries[] = "ALTER TABLE {$table} ADD {$column}_{$locale} {$type}"; $queries[] = "UPDATE {$table} SET {$column}_{$locale} = {$column}_{$source}"; } // add view $queries[] = self::createViewQuery($locale, $table, $dao); // add new indices $queries = array_merge($queries, self::createIndexQueries($locale, $table)); } // add triggers $queries = array_merge($queries, self::createTriggerQueries($locales, $locale)); // execute the queries without i18n rewriting foreach ($queries as $query) { $dao->query($query, false); } // update civicrm_domain.locales $locales[] = $locale; $domain->locales = implode(CRM_Core_DAO::VALUE_SEPARATOR, $locales); $domain->save(); }
/** * Add a new locale to a multi-lang db, setting * its values to the current default locale. * * @param $locale string the new locale to add * @param $source string the locale to copy from * * @return void */ static function addLocale($locale, $source) { // get the current supported locales $domain = new CRM_Core_DAO_Domain(); $domain->find(TRUE); $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales); // break early if the locale is already supported if (in_array($locale, $locales)) { return; } $dao = new CRM_Core_DAO(); // build the required SQL queries $columns = CRM_Core_I18n_SchemaStructure::columns(); $indices = CRM_Core_I18n_SchemaStructure::indices(); $queries = array(); foreach ($columns as $table => $hash) { // add new columns foreach ($hash as $column => $type) { // CRM-7854: skip existing columns if (CRM_Core_DAO::checkFieldExists($table, "{$column}_{$locale}", FALSE)) { continue; } $queries[] = "ALTER TABLE {$table} ADD {$column}_{$locale} {$type}"; $queries[] = "UPDATE {$table} SET {$column}_{$locale} = {$column}_{$source}"; } // add view $queries[] = self::createViewQuery($locale, $table, $dao); // add new indices $queries = array_merge($queries, array_values(self::createIndexQueries($locale, $table))); } // execute the queries without i18n rewriting foreach ($queries as $query) { $dao->query($query, FALSE); } // update civicrm_domain.locales $locales[] = $locale; $domain->locales = implode(CRM_Core_DAO::VALUE_SEPARATOR, $locales); $domain->save(); // invoke the meta trigger creation call CRM_Core_DAO::triggerRebuild(); }
/** * Create indexes. * * @param $tables * Tables to create index for in the format: * array('civicrm_entity_table' => 'entity_id') * OR * array('civicrm_entity_table' => array('entity_id', 'entity_table')) * The latter will create a combined index on the 2 keys (in order). * * Side note - when creating combined indexes the one with the most variation * goes first - so entity_table always goes after entity_id. * * It probably makes sense to consider more sophisticated options at some point * but at the moment this is only being as enhanced as fast as the test is. * * @todo add support for length & multilingual on combined keys. * * @param string $createIndexPrefix * @param array $substrLenghts */ public static function createIndexes($tables, $createIndexPrefix = 'index', $substrLenghts = array()) { $queries = array(); $domain = new CRM_Core_DAO_Domain(); $domain->find(TRUE); $locales = explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales); // if we're multilingual, cache the information on internationalised fields static $columns = NULL; if (!CRM_Utils_System::isNull($locales) and $columns === NULL) { $columns = CRM_Core_I18n_SchemaStructure::columns(); } foreach ($tables as $table => $fields) { $query = "SHOW INDEX FROM {$table}"; $dao = CRM_Core_DAO::executeQuery($query); $currentIndexes = array(); while ($dao->fetch()) { $currentIndexes[] = $dao->Key_name; } // now check for all fields if the index exists foreach ($fields as $field) { $fieldName = implode('_', (array) $field); if (is_array($field)) { // No support for these for combined indexes as yet - add a test when you // want to add that. $lengthName = ''; $lengthSize = ''; } else { // handle indices over substrings, CRM-6245 // $lengthName is appended to index name, $lengthSize is the field size modifier $lengthName = isset($substrLenghts[$table][$fieldName]) ? "_{$substrLenghts[$table][$fieldName]}" : ''; $lengthSize = isset($substrLenghts[$table][$fieldName]) ? "({$substrLenghts[$table][$fieldName]})" : ''; } $names = array("index_{$fieldName}{$lengthName}", "FK_{$table}_{$fieldName}{$lengthName}", "UI_{$fieldName}{$lengthName}", "{$createIndexPrefix}_{$fieldName}{$lengthName}"); // skip to the next $field if one of the above $names exists; handle multilingual for CRM-4126 foreach ($names as $name) { $regex = '/^' . preg_quote($name) . '(_[a-z][a-z]_[A-Z][A-Z])?$/'; if (preg_grep($regex, $currentIndexes)) { continue 2; } } // the index doesn't exist, so create it // if we're multilingual and the field is internationalised, do it for every locale // @todo remove is_array check & add multilingual support for combined indexes and add a test. // Note combined indexes currently using this function are on fields like // entity_id + entity_table which are not multilingual. if (!is_array($field) && !CRM_Utils_System::isNull($locales) and isset($columns[$table][$fieldName])) { foreach ($locales as $locale) { $queries[] = "CREATE INDEX {$createIndexPrefix}_{$fieldName}{$lengthName}_{$locale} ON {$table} ({$fieldName}_{$locale}{$lengthSize})"; } } else { $queries[] = "CREATE INDEX {$createIndexPrefix}_{$fieldName}{$lengthName} ON {$table} (" . implode(',', (array) $field) . "{$lengthSize})"; } } } // run the queries without i18n-rewriting $dao = new CRM_Core_DAO(); foreach ($queries as $query) { $dao->query($query, FALSE); } }