Merge pull request #16420 from civicrm/5.22
[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 * $Id$
17 *
18 */
19 class CRM_Core_I18n_Form extends CRM_Core_Form {
20
21 public function buildQuickForm() {
22 $config = CRM_Core_Config::singleton();
23 $tsLocale = CRM_Core_I18n::getLocale();
24 $this->_locales = array_keys($config->languageLimit);
25
26 // get the part of the database we want to edit and validate it
27 $table = CRM_Utils_Request::retrieve('table', 'String', $this);
28 $field = CRM_Utils_Request::retrieve('field', 'String', $this);
29 $id = CRM_Utils_Request::retrieve('id', 'Int', $this);
30 $this->_structure = CRM_Core_I18n_SchemaStructure::columns();
31 if (!isset($this->_structure[$table][$field])) {
32 CRM_Core_Error::fatal("$table.$field is not internationalized.");
33 }
34
35 $this->addElement('hidden', 'table', $table);
36 $this->addElement('hidden', 'field', $field);
37 $this->addElement('hidden', 'id', $id);
38
39 $cols = [];
40 foreach ($this->_locales as $locale) {
41 $cols[] = "{$field}_{$locale} {$locale}";
42 }
43 $query = 'SELECT ' . implode(', ', $cols) . " FROM $table WHERE id = $id";
44
45 $dao = new CRM_Core_DAO();
46 $dao->query($query, FALSE);
47 $dao->fetch();
48
49 // get html type and attributes for this field
50 $widgets = CRM_Core_I18n_SchemaStructure::widgets();
51 $widget = $widgets[$table][$field];
52
53 // attributes
54 $attributes = ['class' => ''];
55 if (isset($widget['rows'])) {
56 $attributes['rows'] = $widget['rows'];
57 }
58 if (isset($widget['cols'])) {
59 $attributes['cols'] = $widget['cols'];
60 }
61 $required = !empty($widget['required']);
62
63 if ($widget['type'] == 'RichTextEditor') {
64 $widget['type'] = 'wysiwyg';
65 $attributes['class'] = 'collapsed';
66 }
67 elseif ($widget['type'] == 'Text') {
68 $attributes['class'] = 'huge';
69 }
70
71 $languages = CRM_Core_I18n::languages(TRUE);
72 foreach ($this->_locales as $locale) {
73 $attr = $attributes;
74 $name = "{$field}_{$locale}";
75 if ($locale == $tsLocale) {
76 $attr['class'] .= ' default-lang';
77 }
78 $this->add($widget['type'], $name, $languages[$locale], $attr, $required);
79
80 $this->_defaults[$name] = $dao->$locale;
81 }
82
83 $this->addDefaultButtons(ts('Save'), 'next', NULL);
84
85 CRM_Utils_System::setTitle(ts('Languages'));
86
87 $this->assign('locales', $this->_locales);
88 $this->assign('field', $field);
89 }
90
91 /**
92 * This virtual function is used to set the default values of
93 * various form elements
94 *
95 * access public
96 *
97 * @return array
98 * reference to the array of default values
99 */
100 public function setDefaultValues() {
101 return $this->_defaults;
102 }
103
104 public function postProcess() {
105 $values = $this->exportValues();
106 $table = $values['table'];
107 $field = $values['field'];
108
109 // validate table and field
110 if (!isset($this->_structure[$table][$field])) {
111 CRM_Core_Error::fatal("$table.$field is not internationalized.");
112 }
113
114 $cols = [];
115 $params = [[$values['id'], 'Int']];
116 $i = 1;
117 foreach ($this->_locales as $locale) {
118 $cols[] = "{$field}_{$locale} = %$i";
119 $params[$i] = [$values["{$field}_{$locale}"], 'String'];
120 $i++;
121 }
122 $query = "UPDATE $table SET " . implode(', ', $cols) . " WHERE id = %0";
123 $dao = new CRM_Core_DAO();
124 $query = CRM_Core_DAO::composeQuery($query, $params, TRUE);
125 $dao->query($query, FALSE);
126 }
127
128 }