CRM-15375 - make the setup create metadata for I18n that can be used in the popup
[civicrm-core.git] / CRM / Core / I18n / Form.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
32 * $Id$
33 *
34 */
35 class CRM_Core_I18n_Form extends CRM_Core_Form {
36 public function buildQuickForm() {
37 $config = CRM_Core_Config::singleton();
38 global $tsLocale;
39 $this->_locales = array_keys($config->languageLimit);
40
41 // get the part of the database we want to edit and validate it
42 $table = CRM_Utils_Request::retrieve('table', 'String', $this);
43 $field = CRM_Utils_Request::retrieve('field', 'String', $this);
44 $id = CRM_Utils_Request::retrieve('id', 'Int', $this);
45 $this->_structure = CRM_Core_I18n_SchemaStructure::columns();
46 if (!isset($this->_structure[$table][$field])) {
47 CRM_Core_Error::fatal("$table.$field is not internationalized.");
48 }
49
50 $this->addElement('hidden', 'table', $table);
51 $this->addElement('hidden', 'field', $field);
52 $this->addElement('hidden', 'id', $id);
53
54 $cols = array();
55 foreach ($this->_locales as $locale) {
56 $cols[] = "{$field}_{$locale} {$locale}";
57 }
58 $query = 'SELECT ' . implode(', ', $cols) . " FROM $table WHERE id = $id";
59
60 $dao = new CRM_Core_DAO();
61 $dao->query($query, FALSE);
62 $dao->fetch();
63
64 // get html type and attributes for this field
65 $widgets = CRM_Core_I18n_SchemaStructure::widgets();
66 $widget = $widgets[$table][$field];
67
68 // attributes
69 $attributes = array('css' => '');
70 if (isset($widget['rows'])) {
71 $attributes['rows'] = $widget['rows'];
72 }
73 if (isset($widget['cols'])) {
74 $attributes['cols'] = $widget['cols'];
75 }
76 // FIXME: should have this
77 $required = !empty($widget['required']);
78
79 $languages = CRM_Core_I18n::languages(TRUE);
80 foreach ($this->_locales as $locale) {
81 $name = "{$field}_{$locale}";
82 if ($locale == $tsLocale) {
83 $attributes['class'] .= ' default-lang';
84 }
85 if ($widget['type'] == 'RichTextEditor') {
86 $attributes['click_wysiwyg'] = TRUE;
87 $this->addWysiwyg($name, $languages[$locale], $attributes, $required);
88 }
89 else {
90 $this->add($widget['type'], $name, $languages[$locale], $attributes, $required);
91 }
92
93 $this->_defaults[$name] = $dao->$locale;
94 }
95
96 $this->addDefaultButtons(ts('Save'), 'next', NULL);
97
98 CRM_Utils_System::setTitle(ts('Languages'));
99
100 $this->assign('locales', $this->_locales);
101 $this->assign('field', $field);
102 }
103
104 /**
105 * This virtual function is used to set the default values of
106 * various form elements
107 *
108 * access public
109 *
110 * @return array
111 * reference to the array of default values
112 */
113 public function setDefaultValues() {
114 return $this->_defaults;
115 }
116
117 public function postProcess() {
118 $values = $this->exportValues();
119 $table = $values['table'];
120 $field = $values['field'];
121
122 // validate table and field
123 if (!isset($this->_structure[$table][$field])) {
124 CRM_Core_Error::fatal("$table.$field is not internationalized.");
125 }
126
127 $cols = array();
128 $params = array(array($values['id'], 'Int'));
129 $i = 1;
130 foreach ($this->_locales as $locale) {
131 $cols[] = "{$field}_{$locale} = %$i";
132 $params[$i] = array($values["{$field}_{$locale}"], 'String');
133 $i++;
134 }
135 $query = "UPDATE $table SET " . implode(', ', $cols) . " WHERE id = %0";
136 $dao = new CRM_Core_DAO();
137 $query = CRM_Core_DAO::composeQuery($query, $params, TRUE);
138 $dao->query($query, FALSE);
139 }
140
141 }