Merge pull request #21665 from civicrm/5.42
[civicrm-core.git] / CRM / Core / I18n / Form.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Core_I18n_Form extends CRM_Core_Form {
18
19 public function buildQuickForm() {
20 $config = CRM_Core_Config::singleton();
21 $tsLocale = CRM_Core_I18n::getLocale();
22 $this->_locales = array_keys($config->languageLimit);
23
24 // get the part of the database we want to edit and validate it
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);
28 $this->_structure = CRM_Core_I18n_SchemaStructure::columns();
29 if (!isset($this->_structure[$table][$field])) {
30 CRM_Core_Error::statusBounce("$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
37 $cols = [];
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
47 // get html type and attributes for this field
48 $widgets = CRM_Core_I18n_SchemaStructure::widgets();
49 $widget = $widgets[$table][$field];
50
51 // attributes
52 $attributes = ['class' => ''];
53 if (isset($widget['rows'])) {
54 $attributes['rows'] = $widget['rows'];
55 }
56 if (isset($widget['cols'])) {
57 $attributes['cols'] = $widget['cols'];
58 }
59 $required = !empty($widget['required']);
60
61 if ($widget['type'] == 'RichTextEditor') {
62 $widget['type'] = 'wysiwyg';
63 $attributes['class'] = 'collapsed';
64 }
65 elseif ($widget['type'] == 'Text') {
66 $attributes['class'] = 'huge';
67 }
68
69 $languages = CRM_Core_I18n::languages(TRUE);
70 foreach ($this->_locales as $locale) {
71 $attr = $attributes;
72 $name = "{$field}_{$locale}";
73 if ($locale == $tsLocale) {
74 $attr['class'] .= ' default-lang';
75 }
76 $this->add($widget['type'], $name, $languages[$locale], $attr, $required);
77
78 $this->_defaults[$name] = $dao->$locale;
79 }
80
81 $this->addDefaultButtons(ts('Save'), 'next', NULL);
82
83 $this->setTitle(ts('Languages'));
84
85 $this->assign('locales', $this->_locales);
86 $this->assign('field', $field);
87 }
88
89 /**
90 * This virtual function is used to set the default values of
91 * various form elements
92 *
93 * access public
94 *
95 * @return array
96 * reference to the array of default values
97 */
98 public function setDefaultValues() {
99 return $this->_defaults;
100 }
101
102 public function postProcess() {
103 $values = $this->exportValues();
104 $table = $values['table'];
105 $field = $values['field'];
106
107 // validate table and field
108 if (!isset($this->_structure[$table][$field])) {
109 CRM_Core_Error::statusBounce("$table.$field is not internationalized.");
110 }
111
112 $cols = [];
113 $params = [[$values['id'], 'Int']];
114 $i = 1;
115 foreach ($this->_locales as $locale) {
116 $cols[] = "{$field}_{$locale} = %$i";
117 $params[$i] = [$values["{$field}_{$locale}"], 'String'];
118 $i++;
119 }
120 $query = "UPDATE $table SET " . implode(', ', $cols) . " WHERE id = %0";
121 $dao = new CRM_Core_DAO();
122 $query = CRM_Core_DAO::composeQuery($query, $params, TRUE);
123 $dao->query($query, FALSE);
124 }
125
126 }