Esempio n. 1
0
 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);
     }
 }
Esempio n. 4
0
 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);
     }
 }
Esempio n. 5
0
 /**
  * Rewrite SQL query to use views to access tables with localized columns.
  *
  * @param $query string  the query for rewrite
  * @return string        the rewritten query
  */
 static function rewriteQuery($query)
 {
     static $tables = null;
     if ($tables === null) {
         $tables =& CRM_Core_I18n_SchemaStructure::tables();
     }
     global $dbLocale;
     foreach ($tables as $table) {
         $query = preg_replace("/([^'\"])({$table})([^_'\"])/", "\\1\\2{$dbLocale}\\3", $query);
     }
     // uncomment the below to rewrite the civicrm_value_* queries
     // $query = preg_replace("/(civicrm_value_[a-z0-9_]+_\d+)([^_])/", "\\1{$dbLocale}\\2", $query);
     return $query;
 }
Esempio n. 6
0
 static function schemaStructureTables($version = NULL, $force = FALSE)
 {
     static $_tables = NULL;
     if ($_tables === NULL || $force) {
         if ($version) {
             $latest = self::getLatestSchema($version);
             // FIXME: Doing require_once is a must here because a call like CRM_Core_I18n_SchemaStructure_4_1_0 makes
             // class loader look for file like - CRM/Core/I18n/SchemaStructure/4/1/0.php which is not what we want to be loaded
             require_once "CRM/Core/I18n/SchemaStructure_{$latest}.php";
             $class = "CRM_Core_I18n_SchemaStructure_{$latest}";
             $tables =& $class::tables();
         } else {
             $tables = CRM_Core_I18n_SchemaStructure::tables();
         }
         $_tables = $tables;
     }
     return $_tables;
 }
Esempio n. 7
0
 /**
  * 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);
     }
 }