Issues (1798)

public/main/extrafield/translate.php (1 issue)

Labels
Severity
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
use Chamilo\CoreBundle\Entity\ExtraField;
0 ignored issues
show
This use statement conflicts with another class in this namespace, ExtraField. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use Chamilo\CoreBundle\Entity\Language;
7
use Chamilo\CoreBundle\Framework\Container;
8
use Gedmo\Translatable\Entity\Translation;
9
10
$cidReset = true;
11
12
require_once __DIR__.'/../inc/global.inc.php';
13
14
api_protect_admin_script();
15
16
$em = Database::getManager();
17
$request = Container::getRequest();
18
$extraFieldRepo = Container::getExtraFieldRepository();
19
$languageRepo = Container::getLanguageRepository();
20
21
$fieldId = $request->query->getInt('extra_field');
22
23
/** @var ExtraField|null $extraField */
24
$extraField = $extraFieldRepo->find($fieldId);
25
26
if (null === $extraField) {
27
    api_not_allowed(true);
28
}
29
30
$currentUrl = api_get_self().'?extra_field='.$fieldId;
31
$languages = $languageRepo->getAllAvailable(true)->getQuery()->getResult();
32
33
$form = new FormValidator('translate', 'POST', $currentUrl);
34
$form->addHidden('id', $fieldId);
35
36
$extraField->setLocale(Container::getParameter('locale'));
37
$em->refresh($extraField);
38
39
$form->addHeader($extraField->getDisplayText());
40
41
$translationsRepo = $em->getRepository(Translation::class);
42
$translations = $translationsRepo->findTranslations($extraField);
43
44
$defaults = [];
45
46
/** @var Language $language */
47
foreach ($languages as $language) {
48
    $iso = $language->getIsocode();
49
    $form->addText(
50
        'language['.$language->getId().']',
51
        $language->getOriginalName(),
52
        false
53
    );
54
    if (!empty($translations[$iso]['displayText'])) {
55
        $defaults['language['.$language->getId().']'] = $translations[$iso]['displayText'];
56
    }
57
}
58
59
$form->setDefaults($defaults);
60
$form->addButtonSave(get_lang('Save'));
61
62
$interbreadcrumb[] = ['url' => api_get_path(WEB_CODE_PATH).'admin', 'name' => get_lang('Administration')];
63
64
$type = \ExtraField::getExtraFieldTypeFromInt($extraField->getItemType());
65
66
$interbreadcrumb[] = [
67
    'url' => api_get_path(WEB_CODE_PATH).'admin/extra_fields.php?type='.$type,
68
    'name' => get_lang('Fields'),
69
];
70
71
$interbreadcrumb[] = [
72
    'url' => api_get_path(WEB_CODE_PATH).'admin/extra_fields.php?action=edit&type='.$type.'&id='.$fieldId,
73
    'name' => $extraField->getDisplayText(),
74
];
75
76
if ($form->validate()) {
77
    $values = $form->getSubmitValues();
78
    foreach ($languages as $language) {
79
        if (empty($values['language'][$language->getId()])) {
80
            continue;
81
        }
82
83
        $translationsRepo->translate(
84
            $extraField,
85
            'displayText',
86
            $language->getIsocode(),
87
            $values['language'][$language->getId()]
88
        );
89
    }
90
91
    $em->flush();
92
93
    Display::addFlash(Display::return_message(get_lang('Updated')));
94
    api_location($currentUrl);
95
}
96
97
$tpl = new Template(get_lang('Translations'));
98
$tpl->assign('form', $form->returnForm());
99
$template = $tpl->get_template('extrafield/translate.html.twig');
100
$content = $tpl->fetch($template);
101
$tpl->assign('content', $content);
102
$tpl->display_one_col_template();
103