dev/translation#67 - Define "Translation" entity. Add during installation/upgrade.
[civicrm-core.git] / CRM / Core / BAO / Translation.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_BAO_Translation extends CRM_Core_DAO_Translation {
18
19 /**
20 * Get a list of valid statuses for translated-strings.
21 *
22 * @return string[]
23 */
24 public static function getStatuses($context = NULL) {
25 $options = [
26 ['id' => 1, 'name' => 'active', 'label' => ts('Active')],
27 ['id' => 2, 'name' => 'draft', 'label' => ts('Draft')],
28 ];
29 return self::formatPsuedoconstant($context, $options);
30 }
31
32 /**
33 * Get a list of tables with translatable strings.
34 *
35 * @return string[]
36 * Ex: ['civicrm_event' => 'civicrm_event']
37 */
38 public static function getEntityTables() {
39 if (!isset(Civi::$statics[__CLASS__]['allTables'])) {
40 $tables = array_keys(self::getTranslatedFields());
41 Civi::$statics[__CLASS__]['allTables'] = array_combine($tables, $tables);
42 }
43 return Civi::$statics[__CLASS__]['allTables'];
44 }
45
46 /**
47 * Get a list of fields with translatable strings.
48 *
49 * @return string[]
50 * Ex: ['title' => 'title', 'description' => 'description']
51 */
52 public static function getEntityFields() {
53 if (!isset(Civi::$statics[__CLASS__]['allFields'])) {
54 $allFields = [];
55 foreach (self::getTranslatedFields() as $columns) {
56 foreach ($columns as $column => $sqlExpr) {
57 $allFields[$column] = $column;
58 }
59 }
60 Civi::$statics[__CLASS__]['allFields'] = $allFields;
61 }
62 return Civi::$statics[__CLASS__]['allFields'];
63 }
64
65 /**
66 * Given a constant list of of id/name/label options, convert to the
67 * format required by pseudoconstant.
68 *
69 * @param string|NULL $context
70 * @param array $options
71 * List of options, each as a record of id+name+label.
72 * Ex: [['id' => 123, 'name' => 'foo_bar', 'label' => 'Foo Bar']]
73 *
74 * @return array|false
75 */
76 private static function formatPsuedoconstant($context, array $options) {
77 // https://docs.civicrm.org/dev/en/latest/framework/pseudoconstant/#context
78 $key = ($context === 'match') ? 'name' : 'id';
79 $value = ($context === 'validate') ? 'name' : 'label';
80 return array_combine(array_column($options, $key), array_column($options, $value));
81 }
82
83 /**
84 * @return array
85 * List of data fields to translate, organized by table and column.
86 * Omitted/unlisted fields are not translated. Any listed field may be translated.
87 * Values should be TRUE.
88 * Ex: $fields['civicrm_event']['summary'] = TRUE
89 */
90 public static function getTranslatedFields() {
91 $key = 'translatedFields';
92 $cache = Civi::cache('fields');
93 if (($r = $cache->get($key)) !== NULL) {
94 return $r;
95 }
96
97 $f = [];
98 \CRM_Utils_Hook::translateFields($f);
99
100 // Future: Assimilate defaults originating in XML (incl extension-entities)
101 // e.g. CRM_Core_I18n_SchemaStructure::columns() will grab core fields
102
103 $cache->set($key, $f);
104 return $f;
105 }
106
107 }