Merge pull request #14578 from colemanw/i18n
[civicrm-core.git] / CRM / Core / I18n / Form.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Core_I18n_Form extends CRM_Core_Form {
518fa0ee 36
00be9182 37 public function buildQuickForm() {
6a488035 38 $config = CRM_Core_Config::singleton();
98466ff9 39 $tsLocale = CRM_Core_I18n::getLocale();
6a488035
TO
40 $this->_locales = array_keys($config->languageLimit);
41
42 // get the part of the database we want to edit and validate it
353ffa53
TO
43 $table = CRM_Utils_Request::retrieve('table', 'String', $this);
44 $field = CRM_Utils_Request::retrieve('field', 'String', $this);
45 $id = CRM_Utils_Request::retrieve('id', 'Int', $this);
6a488035
TO
46 $this->_structure = CRM_Core_I18n_SchemaStructure::columns();
47 if (!isset($this->_structure[$table][$field])) {
48 CRM_Core_Error::fatal("$table.$field is not internationalized.");
49 }
50
51 $this->addElement('hidden', 'table', $table);
52 $this->addElement('hidden', 'field', $field);
53 $this->addElement('hidden', 'id', $id);
54
be2fb01f 55 $cols = [];
6a488035
TO
56 foreach ($this->_locales as $locale) {
57 $cols[] = "{$field}_{$locale} {$locale}";
58 }
59 $query = 'SELECT ' . implode(', ', $cols) . " FROM $table WHERE id = $id";
60
61 $dao = new CRM_Core_DAO();
62 $dao->query($query, FALSE);
63 $dao->fetch();
64
313df5ee
SV
65 // get html type and attributes for this field
66 $widgets = CRM_Core_I18n_SchemaStructure::widgets();
67 $widget = $widgets[$table][$field];
6a488035 68
313df5ee 69 // attributes
be2fb01f 70 $attributes = ['class' => ''];
313df5ee
SV
71 if (isset($widget['rows'])) {
72 $attributes['rows'] = $widget['rows'];
73 }
74 if (isset($widget['cols'])) {
75 $attributes['cols'] = $widget['cols'];
76 }
313df5ee 77 $required = !empty($widget['required']);
132ab804 78
100a45ff
CW
79 if ($widget['type'] == 'RichTextEditor') {
80 $widget['type'] = 'wysiwyg';
071e5a4f
CW
81 $attributes['class'] = 'collapsed';
82 }
83 elseif ($widget['type'] == 'Text') {
84 $attributes['class'] = 'huge';
100a45ff
CW
85 }
86
6a488035
TO
87 $languages = CRM_Core_I18n::languages(TRUE);
88 foreach ($this->_locales as $locale) {
100a45ff 89 $attr = $attributes;
313df5ee
SV
90 $name = "{$field}_{$locale}";
91 if ($locale == $tsLocale) {
100a45ff 92 $attr['class'] .= ' default-lang';
313df5ee 93 }
100a45ff 94 $this->add($widget['type'], $name, $languages[$locale], $attr, $required);
313df5ee
SV
95
96 $this->_defaults[$name] = $dao->$locale;
6a488035
TO
97 }
98
d606fff7
CW
99 $this->addDefaultButtons(ts('Save'), 'next', NULL);
100
101 CRM_Utils_System::setTitle(ts('Languages'));
6a488035 102
6a488035
TO
103 $this->assign('locales', $this->_locales);
104 $this->assign('field', $field);
6a488035
TO
105 }
106
b5c2afd0
EM
107 /**
108 * This virtual function is used to set the default values of
109 * various form elements
110 *
111 * access public
112 *
a6c01b45
CW
113 * @return array
114 * reference to the array of default values
b5c2afd0 115 */
00be9182 116 public function setDefaultValues() {
6a488035
TO
117 return $this->_defaults;
118 }
119
00be9182 120 public function postProcess() {
6a488035 121 $values = $this->exportValues();
353ffa53
TO
122 $table = $values['table'];
123 $field = $values['field'];
6a488035
TO
124
125 // validate table and field
126 if (!isset($this->_structure[$table][$field])) {
127 CRM_Core_Error::fatal("$table.$field is not internationalized.");
128 }
129
be2fb01f
CW
130 $cols = [];
131 $params = [[$values['id'], 'Int']];
353ffa53 132 $i = 1;
6a488035
TO
133 foreach ($this->_locales as $locale) {
134 $cols[] = "{$field}_{$locale} = %$i";
be2fb01f 135 $params[$i] = [$values["{$field}_{$locale}"], 'String'];
6a488035
TO
136 $i++;
137 }
138 $query = "UPDATE $table SET " . implode(', ', $cols) . " WHERE id = %0";
353ffa53 139 $dao = new CRM_Core_DAO();
6a488035
TO
140 $query = CRM_Core_DAO::composeQuery($query, $params, TRUE);
141 $dao->query($query, FALSE);
6a488035 142 }
96025800 143
6a488035 144}