Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | class CRM_Core_I18n_Form extends CRM_Core_Form { | |
518fa0ee | 18 | |
00be9182 | 19 | public function buildQuickForm() { |
6a488035 | 20 | $config = CRM_Core_Config::singleton(); |
98466ff9 | 21 | $tsLocale = CRM_Core_I18n::getLocale(); |
6a488035 TO |
22 | $this->_locales = array_keys($config->languageLimit); |
23 | ||
24 | // get the part of the database we want to edit and validate it | |
353ffa53 TO |
25 | $table = CRM_Utils_Request::retrieve('table', 'String', $this); |
26 | $field = CRM_Utils_Request::retrieve('field', 'String', $this); | |
27 | $id = CRM_Utils_Request::retrieve('id', 'Int', $this); | |
6a488035 TO |
28 | $this->_structure = CRM_Core_I18n_SchemaStructure::columns(); |
29 | if (!isset($this->_structure[$table][$field])) { | |
30 | CRM_Core_Error::fatal("$table.$field is not internationalized."); | |
31 | } | |
32 | ||
33 | $this->addElement('hidden', 'table', $table); | |
34 | $this->addElement('hidden', 'field', $field); | |
35 | $this->addElement('hidden', 'id', $id); | |
36 | ||
be2fb01f | 37 | $cols = []; |
6a488035 TO |
38 | foreach ($this->_locales as $locale) { |
39 | $cols[] = "{$field}_{$locale} {$locale}"; | |
40 | } | |
41 | $query = 'SELECT ' . implode(', ', $cols) . " FROM $table WHERE id = $id"; | |
42 | ||
43 | $dao = new CRM_Core_DAO(); | |
44 | $dao->query($query, FALSE); | |
45 | $dao->fetch(); | |
46 | ||
313df5ee SV |
47 | // get html type and attributes for this field |
48 | $widgets = CRM_Core_I18n_SchemaStructure::widgets(); | |
49 | $widget = $widgets[$table][$field]; | |
6a488035 | 50 | |
313df5ee | 51 | // attributes |
be2fb01f | 52 | $attributes = ['class' => '']; |
313df5ee SV |
53 | if (isset($widget['rows'])) { |
54 | $attributes['rows'] = $widget['rows']; | |
55 | } | |
56 | if (isset($widget['cols'])) { | |
57 | $attributes['cols'] = $widget['cols']; | |
58 | } | |
313df5ee | 59 | $required = !empty($widget['required']); |
132ab804 | 60 | |
100a45ff CW |
61 | if ($widget['type'] == 'RichTextEditor') { |
62 | $widget['type'] = 'wysiwyg'; | |
071e5a4f CW |
63 | $attributes['class'] = 'collapsed'; |
64 | } | |
65 | elseif ($widget['type'] == 'Text') { | |
66 | $attributes['class'] = 'huge'; | |
100a45ff CW |
67 | } |
68 | ||
6a488035 TO |
69 | $languages = CRM_Core_I18n::languages(TRUE); |
70 | foreach ($this->_locales as $locale) { | |
100a45ff | 71 | $attr = $attributes; |
313df5ee SV |
72 | $name = "{$field}_{$locale}"; |
73 | if ($locale == $tsLocale) { | |
100a45ff | 74 | $attr['class'] .= ' default-lang'; |
313df5ee | 75 | } |
100a45ff | 76 | $this->add($widget['type'], $name, $languages[$locale], $attr, $required); |
313df5ee SV |
77 | |
78 | $this->_defaults[$name] = $dao->$locale; | |
6a488035 TO |
79 | } |
80 | ||
d606fff7 CW |
81 | $this->addDefaultButtons(ts('Save'), 'next', NULL); |
82 | ||
83 | CRM_Utils_System::setTitle(ts('Languages')); | |
6a488035 | 84 | |
6a488035 TO |
85 | $this->assign('locales', $this->_locales); |
86 | $this->assign('field', $field); | |
6a488035 TO |
87 | } |
88 | ||
b5c2afd0 EM |
89 | /** |
90 | * This virtual function is used to set the default values of | |
91 | * various form elements | |
92 | * | |
93 | * access public | |
94 | * | |
a6c01b45 CW |
95 | * @return array |
96 | * reference to the array of default values | |
b5c2afd0 | 97 | */ |
00be9182 | 98 | public function setDefaultValues() { |
6a488035 TO |
99 | return $this->_defaults; |
100 | } | |
101 | ||
00be9182 | 102 | public function postProcess() { |
6a488035 | 103 | $values = $this->exportValues(); |
353ffa53 TO |
104 | $table = $values['table']; |
105 | $field = $values['field']; | |
6a488035 TO |
106 | |
107 | // validate table and field | |
108 | if (!isset($this->_structure[$table][$field])) { | |
109 | CRM_Core_Error::fatal("$table.$field is not internationalized."); | |
110 | } | |
111 | ||
be2fb01f CW |
112 | $cols = []; |
113 | $params = [[$values['id'], 'Int']]; | |
353ffa53 | 114 | $i = 1; |
6a488035 TO |
115 | foreach ($this->_locales as $locale) { |
116 | $cols[] = "{$field}_{$locale} = %$i"; | |
be2fb01f | 117 | $params[$i] = [$values["{$field}_{$locale}"], 'String']; |
6a488035 TO |
118 | $i++; |
119 | } | |
120 | $query = "UPDATE $table SET " . implode(', ', $cols) . " WHERE id = %0"; | |
353ffa53 | 121 | $dao = new CRM_Core_DAO(); |
6a488035 TO |
122 | $query = CRM_Core_DAO::composeQuery($query, $params, TRUE); |
123 | $dao->query($query, FALSE); | |
6a488035 | 124 | } |
96025800 | 125 | |
6a488035 | 126 | } |