Merge pull request #15263 from JMAConsulting/financial-67
[civicrm-core.git] / CRM / Core / I18n / Form.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 * $Id$
33 *
34 */
35 class CRM_Core_I18n_Form extends CRM_Core_Form {
36
37 public function buildQuickForm() {
38 $config = CRM_Core_Config::singleton();
39 $tsLocale = CRM_Core_I18n::getLocale();
40 $this->_locales = array_keys($config->languageLimit);
41
42 // get the part of the database we want to edit and validate it
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);
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
55 $cols = [];
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
65 // get html type and attributes for this field
66 $widgets = CRM_Core_I18n_SchemaStructure::widgets();
67 $widget = $widgets[$table][$field];
68
69 // attributes
70 $attributes = ['class' => ''];
71 if (isset($widget['rows'])) {
72 $attributes['rows'] = $widget['rows'];
73 }
74 if (isset($widget['cols'])) {
75 $attributes['cols'] = $widget['cols'];
76 }
77 $required = !empty($widget['required']);
78
79 if ($widget['type'] == 'RichTextEditor') {
80 $widget['type'] = 'wysiwyg';
81 $attributes['class'] = 'collapsed';
82 }
83 elseif ($widget['type'] == 'Text') {
84 $attributes['class'] = 'huge';
85 }
86
87 $languages = CRM_Core_I18n::languages(TRUE);
88 foreach ($this->_locales as $locale) {
89 $attr = $attributes;
90 $name = "{$field}_{$locale}";
91 if ($locale == $tsLocale) {
92 $attr['class'] .= ' default-lang';
93 }
94 $this->add($widget['type'], $name, $languages[$locale], $attr, $required);
95
96 $this->_defaults[$name] = $dao->$locale;
97 }
98
99 $this->addDefaultButtons(ts('Save'), 'next', NULL);
100
101 CRM_Utils_System::setTitle(ts('Languages'));
102
103 $this->assign('locales', $this->_locales);
104 $this->assign('field', $field);
105 }
106
107 /**
108 * This virtual function is used to set the default values of
109 * various form elements
110 *
111 * access public
112 *
113 * @return array
114 * reference to the array of default values
115 */
116 public function setDefaultValues() {
117 return $this->_defaults;
118 }
119
120 public function postProcess() {
121 $values = $this->exportValues();
122 $table = $values['table'];
123 $field = $values['field'];
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
130 $cols = [];
131 $params = [[$values['id'], 'Int']];
132 $i = 1;
133 foreach ($this->_locales as $locale) {
134 $cols[] = "{$field}_{$locale} = %$i";
135 $params[$i] = [$values["{$field}_{$locale}"], 'String'];
136 $i++;
137 }
138 $query = "UPDATE $table SET " . implode(', ', $cols) . " WHERE id = %0";
139 $dao = new CRM_Core_DAO();
140 $query = CRM_Core_DAO::composeQuery($query, $params, TRUE);
141 $dao->query($query, FALSE);
142 }
143
144 }