Skip to content

Commit 8634302

Browse files
committed
Merge branch 'master' of github.com:chamilo/chamilo-lms
2 parents 501deba + 1eb92b0 commit 8634302

File tree

4 files changed

+153
-143
lines changed

4 files changed

+153
-143
lines changed

public/main/admin/access_urls.php

Lines changed: 102 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
$cidReset = true;
1717
require_once __DIR__.'/../inc/global.inc.php';
1818
$this_section = SECTION_PLATFORM_ADMIN;
19-
20-
//api_protect_admin_script();
2119
api_protect_global_admin_script();
2220

2321
$httpRequest = HttpRequest::createFromGlobals();
@@ -36,12 +34,11 @@
3634

3735
switch ($httpRequest->query->get('action')) {
3836
case 'delete_url':
39-
$result = UrlManager::delete($url_id);
40-
if ($result) {
41-
echo Display::return_message(get_lang('URL deleted.'), 'normal');
42-
} else {
43-
echo Display::return_message(get_lang('Cannot delete this URL.'), 'error');
44-
}
37+
$ok = UrlManager::delete($url_id);
38+
echo Display::return_message(
39+
$ok ? get_lang('URL deleted.') : get_lang('Cannot delete this URL.'),
40+
$ok ? 'normal' : 'error'
41+
);
4542

4643
break;
4744
case 'lock':
@@ -55,22 +52,19 @@
5552

5653
break;
5754
case 'register':
58-
// we are going to register the admin
59-
if (api_is_platform_admin()) {
60-
if (-1 != $current_access_url_id) {
61-
$url_str = '';
62-
foreach ($url_list as $my_url) {
63-
if (!in_array($my_url['id'], $my_user_url_list)) {
64-
UrlManager::add_user_to_url(api_get_user_id(), $my_url['id']);
65-
$url_str .= $my_url['url'].' <br />';
66-
}
55+
if (api_is_platform_admin() && -1 != $current_access_url_id) {
56+
$url_str = '';
57+
foreach ($url_list as $u) {
58+
if (!in_array($u['id'], $my_user_url_list)) {
59+
UrlManager::add_user_to_url(api_get_user_id(), $u['id']);
60+
$url_str .= $u['url'] . '<br />';
6761
}
68-
echo Display::return_message(
69-
get_lang('Admin user assigned to this URL').': '.$url_str.'<br />',
70-
'normal',
71-
false
72-
);
7362
}
63+
echo Display::return_message(
64+
get_lang('Admin user assigned to this URL') . ': ' . $url_str,
65+
'normal',
66+
false
67+
);
7468
}
7569

7670
break;
@@ -81,15 +75,14 @@
8175

8276
// Checking if the admin is registered in all sites
8377
$url_string = '';
84-
$my_user_url_list = api_get_access_url_from_user(api_get_user_id());
85-
foreach ($url_list as $my_url) {
86-
if (!in_array($my_url['id'], $my_user_url_list)) {
87-
$url_string .= $my_url['url'].' <br />';
78+
foreach ($url_list as $u) {
79+
if (!in_array($u['id'], $my_user_url_list)) {
80+
$url_string .= $u['url'] . '<br />';
8881
}
8982
}
9083
if (!empty($url_string)) {
9184
echo Display::return_message(
92-
get_lang('Admin user should be registered here').'<br />'.$url_string,
85+
get_lang('Admin user should be registered here') . '<br />' . $url_string,
9386
'warning',
9487
false
9588
);
@@ -98,28 +91,59 @@
9891
// checking the current installation
9992
if (-1 == $current_access_url_id) {
10093
echo Display::return_message(
101-
get_lang('URL not configured yet, please add this URL :').': '.api_get_path(WEB_PATH),
94+
get_lang('URL not configured yet, please add this URL :') . ' ' . api_get_path(WEB_PATH),
10295
'warning'
10396
);
10497
} elseif (api_is_platform_admin()) {
105-
$quant = UrlManager::relation_url_user_exist(
106-
api_get_user_id(),
107-
$current_access_url_id
108-
);
98+
$quant = UrlManager::relation_url_user_exist(api_get_user_id(), $current_access_url_id);
10999
if (0 == $quant) {
110100
echo Display::return_message(
111-
'<a href="'.api_get_self().'?action=register&sec_token='.$parameters['sec_token'].'">'.
112-
get_lang('Click here to register the admin into all sites').'</a>',
101+
'<a href="' . api_get_self() . '?action=register&sec_token=' . $parameters['sec_token'] . '">' .
102+
get_lang('Click here to register the admin into all sites') .
103+
'</a>',
113104
'warning',
114105
false
115106
);
116107
}
117108
}
118109

110+
// 1) Find the default URL (ID = 1)
111+
$defaultUrl = 'http://localhost/';
112+
foreach ($url_list as $u) {
113+
if ((string)$u['id'] === '1') {
114+
$defaultUrl = trim($u['url']);
115+
break;
116+
}
117+
}
118+
119+
// 2) Tooltip message (in English, per spec)
120+
$tooltip = 'Adding new URLs requires you to first set the first URL to a value different than localhost.';
121+
$isLocalhost = ($defaultUrl === 'http://localhost/');
122+
123+
// 3) Decide link href and base attributes
124+
$attributes = ['id' => 'add-url-button'];
125+
if ($isLocalhost) {
126+
// Block the link and apply a “disabled” style
127+
$attributes['class'] = 'ch-disabled';
128+
$linkHref = '#';
129+
} else {
130+
$linkHref = api_get_path(WEB_CODE_PATH) . 'admin/access_url_edit.php';
131+
}
132+
133+
// 4) Build the “Add URL” action
119134
$actions = Display::url(
120-
Display::getMdiIcon('web-plus', 'ch-tool-icon', null, ICON_SIZE_MEDIUM, get_lang('Add URL')),
121-
api_get_path(WEB_CODE_PATH).'admin/access_url_edit.php'
135+
Display::getMdiIcon(
136+
'web-plus',
137+
'ch-tool-icon',
138+
null,
139+
ICON_SIZE_MEDIUM,
140+
get_lang('Add URL')
141+
),
142+
$linkHref,
143+
$attributes
122144
);
145+
146+
// 5) Append the other “Manage” actions as before
123147
if (api_get_multiple_access_url()) {
124148
$actions .= Display::url(
125149
Display::getMdiIcon('account', 'ch-tool-icon', null, ICON_SIZE_MEDIUM, get_lang('Manage users')),
@@ -143,51 +167,59 @@
143167
);
144168
}
145169

146-
echo Display::toolbarAction('urls', [$actions]);
147-
148-
$data = UrlManager::get_url_data();
149-
$urls = [];
150-
foreach ($data as $row) {
151-
// Title
152-
$url = Display::url($row['url'], $row['url'], ['target' => '_blank']);
153-
$description = $row['description'];
154-
$createdAt = api_get_local_time($row['tms']);
155-
156-
//Status
157-
$active = $row['active'];
158-
$action = 'unlock';
159-
$image = StateIcon::INACTIVE;
160-
if ('1' == $active) {
161-
$action = 'lock';
162-
$image = StateIcon::ACTIVE;
163-
}
164-
// you cannot lock the default
165-
if ('1' == $row['id']) {
166-
$status = Display::getMdiIcon($image, 'ch-tool-icon', null, ICON_SIZE_SMALL, get_lang(ucfirst($action)));
170+
// 6) If still localhost, show the tooltip inline next to the button
171+
$toolbarItems = [$actions];
172+
if ($isLocalhost) {
173+
$toolbarItems[] = '<span style="
174+
margin-left: 8px;
175+
font-size: 0.9em;
176+
color: #666;
177+
">'.$tooltip.'</span>';
178+
}
179+
180+
// 7) Render the toolbar
181+
echo Display::toolbarAction('urls', $toolbarItems);
182+
183+
$rows = [];
184+
foreach ($url_list as $u) {
185+
$link = Display::url($u['url'], $u['url'], ['target' => '_blank']);
186+
$desc = $u['description'];
187+
$ts = api_get_local_time($u['tms']);
188+
$active = ($u['active'] === '1');
189+
190+
$iconAction = $active ? 'lock' : 'unlock';
191+
$stateIcon = $active ? StateIcon::ACTIVE : StateIcon::INACTIVE;
192+
193+
if ((string)$u['id'] === '1') {
194+
$status = Display::getMdiIcon($stateIcon, 'ch-tool-icon', null, ICON_SIZE_SMALL, get_lang(ucfirst($iconAction)));
167195
} else {
168-
$status = '<a href="access_urls.php?action='.$action.'&amp;url_id='.$row['id'].'">'.
169-
Display::getMdiIcon($image, 'ch-tool-icon', null, ICON_SIZE_SMALL, get_lang(ucfirst($action))).'</a>';
196+
$status = '<a href="access_urls.php?action=' . $iconAction . '&url_id=' . $u['id'] . '">' .
197+
Display::getMdiIcon($stateIcon, 'ch-tool-icon', null, ICON_SIZE_SMALL, get_lang(ucfirst($iconAction))) .
198+
'</a>';
170199
}
171-
// Actions
172-
$url_id = $row['id'];
173-
$actions = Display::url(
200+
201+
$rowActions = Display::url(
174202
Display::getMdiIcon(ActionIcon::EDIT, 'ch-tool-icon', null, ICON_SIZE_SMALL, get_lang('Edit')),
175-
"access_url_edit.php?url_id=$url_id"
203+
"access_url_edit.php?url_id={$u['id']}"
176204
);
177-
if ('1' != $url_id) {
178-
$actions .= '<a href="access_urls.php?action=delete_url&amp;url_id='.$url_id.'" onclick="javascript:if(!confirm('."'".addslashes(api_htmlentities(get_lang('Please confirm your choice'), ENT_QUOTES))."'".')) return false;">'.
179-
Display::getMdiIcon('delete', 'ch-tool-icon', null, ICON_SIZE_SMALL, get_lang('Delete')).'</a>';
205+
206+
if ((string)$u['id'] !== '1') {
207+
$rowActions .= '<a href="access_urls.php?action=delete_url&url_id=' . $u['id'] . '" ' .
208+
'onclick="return confirm(\'' . addslashes(get_lang('Please confirm your choice')) . '\');">' .
209+
Display::getMdiIcon('delete', 'ch-tool-icon', null, ICON_SIZE_SMALL, get_lang('Delete')) .
210+
'</a>';
180211
}
181-
$urls[] = [$url, $description, $status, $createdAt, $actions];
212+
213+
$rows[] = [$link, $desc, $status, $ts, $rowActions];
182214
}
183215

184-
$table = new SortableTableFromArrayConfig($urls, 2, 50, 'urls');
216+
$table = new SortableTableFromArrayConfig($rows, 2, 50, 'urls');
185217
$table->set_additional_parameters($parameters);
186218
$table->set_header(0, 'URL');
187219
$table->set_header(1, get_lang('Description'));
188-
$table->set_header(2, get_lang('active'));
220+
$table->set_header(2, get_lang('Active'));
189221
$table->set_header(3, get_lang('Created at'));
190222
$table->set_header(4, get_lang('Edit'), false);
191223
$table->display();
192224

193-
Display :: display_footer();
225+
Display::display_footer();

src/CoreBundle/Controller/ExceptionController.php

Lines changed: 43 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace Chamilo\CoreBundle\Controller;
88

9-
use Chamilo\CoreBundle\Helpers\AccessUrlHelper;
9+
use Chamilo\CoreBundle\Repository\Node\AccessUrlRepository;
1010
use Exception;
1111
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1212
use Symfony\Component\ErrorHandler\Exception\FlattenException;
@@ -18,37 +18,33 @@
1818

1919
class ExceptionController extends AbstractController
2020
{
21+
public function __construct(
22+
private UrlGeneratorInterface $urlGenerator,
23+
private AccessUrlRepository $accessUrlRepository
24+
) {}
25+
2126
public function show(Exception $exception): Response
2227
{
2328
if ('dev' === (string) $this->getParameter('app_env')) {
2429
throw new HttpException($exception->getCode(), $exception->getMessage());
2530
}
2631

27-
$showException = true;
28-
// $name = $showException ? 'exception' : 'error';
29-
$name = 'exception';
30-
$code = $exception->getCode();
31-
$format = 'html';
32-
$loader = $this->container->get('twig')->getLoader();
33-
34-
$templateToLoad = \sprintf('@ChamiloCore/Exception/%s.html.twig', 'exception_full');
32+
$name = 'exception';
33+
$code = $exception->getCode();
34+
$format = 'html';
35+
$loader = $this->container->get('twig')->getLoader();
36+
$templateToLoad = sprintf('@ChamiloCore/Exception/%s.html.twig', 'exception_full');
3537

36-
// when not in debug, try to find a template for the specific HTTP status code and format
37-
$template = \sprintf('@ChamiloCore/Exception/%s%s.%s.twig', $name, $code, $format);
38-
if ($loader->exists($template)) {
39-
$templateToLoad = $template;
38+
$candidate = sprintf('@ChamiloCore/Exception/%s%s.%s.twig', $name, $code, $format);
39+
if ($loader->exists($candidate)) {
40+
$templateToLoad = $candidate;
4041
}
4142

42-
// try to find a template for the given format
43-
$template = \sprintf('@ChamiloCore/Exception/%s.%s.twig', $name, $format);
44-
if ($loader->exists($template)) {
45-
$templateToLoad = $template;
43+
$candidate = sprintf('@ChamiloCore/Exception/%s.%s.twig', $name, $format);
44+
if ($loader->exists($candidate)) {
45+
$templateToLoad = $candidate;
4646
}
4747

48-
// default to a generic HTML exception
49-
// $request->setRequestFormat('html');
50-
// $template = sprintf('@ChamiloCore/Exception/%s.html.twig', $showException ? 'exception_full' : $name);
51-
5248
return $this->render($templateToLoad, [
5349
'exception' => $exception,
5450
]);
@@ -63,62 +59,51 @@ public function error(Request $request): Response
6359

6460
$exception->setMessage($message);
6561

66-
$showException = true;
67-
// $name = $showException ? 'exception' : 'error';
68-
$name = 'exception';
69-
$code = $exception->getCode();
70-
$format = 'html';
71-
$loader = $this->container->get('twig')->getLoader();
62+
$name = 'exception';
63+
$code = $exception->getCode();
64+
$format = 'html';
65+
$loader = $this->container->get('twig')->getLoader();
66+
$templateToLoad = sprintf('@ChamiloCore/Exception/%s.html.twig', 'exception_full');
7267

73-
$templateToLoad = \sprintf('@ChamiloCore/Exception/%s.html.twig', 'exception_full');
74-
75-
// when not in debug, try to find a template for the specific HTTP status code and format
76-
// if (!$showException) {
77-
$template = \sprintf('@ChamiloCore/Exception/%s%s.%s.twig', $name, $code, $format);
78-
if ($loader->exists($template)) {
79-
$templateToLoad = $template;
68+
$candidate = sprintf('@ChamiloCore/Exception/%s%s.%s.twig', $name, $code, $format);
69+
if ($loader->exists($candidate)) {
70+
$templateToLoad = $candidate;
8071
}
81-
// }
8272

83-
// try to find a template for the given format
84-
$template = \sprintf('@ChamiloCore/Exception/%s.%s.twig', $name, $format);
85-
if ($loader->exists($template)) {
86-
$templateToLoad = $template;
73+
$candidate = sprintf('@ChamiloCore/Exception/%s.%s.twig', $name, $format);
74+
if ($loader->exists($candidate)) {
75+
$templateToLoad = $candidate;
8776
}
8877

89-
// default to a generic HTML exception
90-
// $request->setRequestFormat('html');
91-
9278
return $this->render($templateToLoad, [
9379
'exception' => $exception,
9480
]);
9581
}
9682

9783
#[Route(path: '/error/undefined-url', name: 'undefined_url_error')]
98-
public function undefinedUrlError(
99-
Request $request,
100-
UrlGeneratorInterface $urlGenerator,
101-
AccessUrlHelper $accessUrlHelper
102-
): Response {
84+
public function undefinedUrlError(Request $request): Response
85+
{
10386
$host = $request->getHost();
10487

105-
$accessUrl = $accessUrlHelper->getFirstAccessUrl();
88+
$accessUrl = $this->accessUrlRepository->find(1);
10689
$themeHost = rtrim($accessUrl?->getUrl() ?? '', '/');
10790
$themeName = 'chamilo';
10891

109-
$cssUrl = $themeHost.$urlGenerator->generate('theme_asset', [
110-
'name' => $themeName,
111-
'path' => 'colors.css',
112-
], UrlGeneratorInterface::ABSOLUTE_PATH);
92+
$cssUrl = $themeHost
93+
. $this->urlGenerator->generate('theme_asset', [
94+
'name' => $themeName,
95+
'path' => 'colors.css',
96+
], UrlGeneratorInterface::ABSOLUTE_PATH);
11397

114-
$logoUrl = $themeHost.$urlGenerator->generate('theme_asset', [
115-
'name' => $themeName,
116-
'path' => 'images/header-logo.svg',
117-
], UrlGeneratorInterface::ABSOLUTE_PATH);
98+
$logoUrl = $themeHost
99+
. $this->urlGenerator->generate('theme_asset', [
100+
'name' => $themeName,
101+
'path' => 'images/header-logo.svg',
102+
], UrlGeneratorInterface::ABSOLUTE_PATH);
118103

119104
return $this->render('@ChamiloCore/Exception/undefined_url.html.twig', [
120-
'host' => $host,
121-
'cssUrl' => $cssUrl,
105+
'host' => $host,
106+
'cssUrl' => $cssUrl,
122107
'logoUrl' => $logoUrl,
123108
]);
124109
}

0 commit comments

Comments
 (0)