Skip to content

Commit 958f1f5

Browse files
committed
Fixes after merge cloud file links #2229
1 parent 1b73435 commit 958f1f5

File tree

5 files changed

+231
-235
lines changed

5 files changed

+231
-235
lines changed

main/document/add_link.php

Lines changed: 96 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -4,77 +4,74 @@
44
* This script allows to add cloud file links to the document structure
55
* @package chamilo.document
66
*/
7-
/**
8-
* Code
9-
*/
10-
11-
// Including the global initialization file
7+
128
require_once __DIR__.'/../inc/global.inc.php';
13-
14-
// Including additional libraries
15-
require_once __DIR__.'/../inc/lib/document.lib.php';
16-
require_once __DIR__.'/../inc/lib/urlUtils.lib.php';
179

1810
$fileLinkEnabled = api_get_configuration_value('enable_add_file_link');
1911

2012
if (!$fileLinkEnabled) {
2113
api_not_allowed(true);
2214
}
23-
24-
$course_info = api_get_course_info();
25-
26-
if (empty($course_info)) {
27-
api_not_allowed(true);
28-
}
2915

30-
if ($is_certificate_mode) {
16+
$courseInfo = api_get_course_info();
17+
18+
if (empty($courseInfo)) {
3119
api_not_allowed(true);
3220
}
33-
34-
$document_data = DocumentManager::get_document_data_by_id($_REQUEST['id'], api_get_course_id(), true);
21+
$dir = '/';
22+
$document_data = DocumentManager::get_document_data_by_id($_REQUEST['id'], api_get_course_id(), true);
3523
if (empty($document_data)) {
36-
$document_id = $parent_id = 0;
24+
$document_id = $parent_id = 0;
3725
$path = '/';
3826
} else {
3927
if ($document_data['filetype'] == 'folder') {
40-
$document_id = $document_data['id'];
41-
$path = $document_data['path'].'/';
42-
$parent_id = DocumentManager::get_document_id(api_get_course_info(), dirname($path));
28+
$document_id = $document_data['id'];
29+
$path = $document_data['path'].'/';
30+
$parent_id = DocumentManager::get_document_id(api_get_course_info(), dirname($path));
4331
}
32+
$dir = dirname($document_data['path']);
4433
}
45-
34+
35+
$is_certificate_mode = DocumentManager::is_certificate_mode($dir);
36+
37+
if ($is_certificate_mode) {
38+
api_not_allowed(true);
39+
}
40+
4641
$is_allowed_to_edit = api_is_allowed_to_edit(null, true);
47-
42+
43+
$groupIid = 0;
4844
if (api_get_group_id()) {
4945
// If the group id is set, check if the user has the right to be here
50-
// Needed for group related stuff
51-
require_once api_get_path(LIBRARY_PATH).'groupmanager.lib.php';
5246
// Get group info
5347
$group_properties = GroupManager::get_group_properties(api_get_group_id());
54-
55-
if ($is_allowed_to_edit || GroupManager::is_user_in_group($_user['user_id'], api_get_group_id())) { // Only courseadmin or group members allowed
56-
$to_group_id = api_get_group_id();
57-
$req_gid = '&gidReq='.api_get_group_id();
58-
$interbreadcrumb[] = array('url' => '../group/group_space.php?gidReq='.api_get_group_id(), 'name' => get_lang('GroupSpace'));
48+
if ($is_allowed_to_edit || GroupManager::is_user_in_group($_user['user_id'], api_get_group_id())) {
49+
// Only courseadmin or group members allowed
50+
$groupIid = $group_properties['iid'];
51+
$interbreadcrumb[] = array(
52+
'url' => '../group/group_space.php?'.api_get_cidreq(),
53+
'name' => get_lang('GroupSpace'),
54+
);
5955
} else {
6056
api_not_allowed(true);
6157
}
6258
} elseif ($is_allowed_to_edit || DocumentManager::is_my_shared_folder(api_get_user_id(), $path, api_get_session_id())) {
6359
// Admin for "regular" upload, no group documents. And check if is my shared folder
64-
$to_group_id = 0;
65-
$req_gid = '';
6660
} else { // No course admin and no group member...
6761
api_not_allowed(true);
6862
}
6963

7064
// Group docs can only be uploaded in the group directory
71-
if ($to_group_id != 0 && $path == '/') {
65+
if ($groupIid != 0 && $path == '/') {
7266
$path = $group_properties['directory'] . "/";
7367
}
74-
68+
7569
// Breadcrumbs
76-
$interbreadcrumb[] = array('url' => './document.php?id='.$document_id.$req_gid, 'name'=> get_lang('Documents'));
77-
70+
$interbreadcrumb[] = array(
71+
'url' => './document.php?id='.$document_id.'&'.api_get_cidreq(),
72+
'name' => get_lang('Documents'),
73+
);
74+
7875
// Interbreadcrumb for the current directory root path
7976
if (empty($document_data['parents'])) {
8077
// Hack in order to not add the document to the breadcrumb in case it is a link
@@ -85,95 +82,106 @@
8582
foreach ($document_data['parents'] as $document_sub_data) {
8683
// Hack in order to not add the document to the breadcrumb in case it is a link
8784
if ($document_data['filetype'] != 'link') {
88-
$interbreadcrumb[] = array('url' => $document_sub_data['document_url'], 'name' => $document_sub_data['title']);
85+
$interbreadcrumb[] = array(
86+
'url' => $document_sub_data['document_url'],
87+
'name' => $document_sub_data['title'],
88+
);
8989
}
9090
}
9191
}
92-
93-
$this_section = SECTION_COURSES;
94-
95-
$nameTools = get_lang('LinkAdd');
9692

97-
// Display the header
98-
Display::display_header($nameTools, 'Doc');
99-
100-
/* Here we do all the work */
101-
102-
// Actions
103-
echo '<div class="actions">';
104-
// Link back to the documents overview
105-
echo '<a href="document.php?id='.$document_id.'">'.Display::return_icon('back.png', get_lang('BackTo').' '.get_lang('DocumentsOverview'), '', ICON_SIZE_MEDIUM).'</a>';
106-
echo '</div>';
107-
108-
// Form to select directory
109-
$folders = DocumentManager::get_all_document_folders(
110-
$_course,
111-
$groupIid,
112-
$is_allowed_to_edit
113-
);
93+
$this_section = SECTION_COURSES;
11494

115-
echo DocumentManager::build_directory_selector(
116-
$folders,
117-
$document_id,
118-
(isset($group_properties['directory']) ? $group_properties['directory'] : array())
119-
);
120-
95+
$nameTools = get_lang('LinkAdd');
12196
$action = api_get_self().'?'.api_get_cidreq().'&id='.$document_id;
122-
97+
12398
// URLs in whitelist
124-
$urlWL = URLUtils::getFileHostingsWL();
99+
$urlWL = DocumentManager::getFileHostingWhiteList();
100+
125101
sort($urlWL);
126102
$urlWLRegEx = '/(\/\/|\.)('.implode('|', $urlWL).')/i'; //Matches any of the whitelisted urls preceded by // or .
127103
$urlWLText = "\n\t* ".implode("\n\t* ", $urlWL);
128104
$urlWLHTML = "<ul><li>".implode("</li><li>", $urlWL)."</li></ul>";
129-
105+
130106
$form = new FormValidator('upload', 'POST', $action, '', array('enctype' => 'multipart/form-data'));
131-
$form->addElement('hidden', 'linkid', $document_id);
132-
$form->addElement('hidden', 'curdirpath', $path);
107+
$form->addHidden('linkid', $document_id);
108+
$form->addHidden('curdirpath', $path);
133109
$form->addElement('text', 'name', get_lang('LinkName'), array('id' => 'name_link'));
134110
$form->addElement('text', 'url', get_lang('Url'), array('id' => 'url_link'));
135-
$form->addElement('static', 'info', '', '<span class="text-primary" data-toggle="tooltip" title="'.$urlWLHTML.'">'.get_lang('ValidDomainList').' <span class="glyphicon glyphicon-question-sign"></span></span>');
111+
$form->addElement(
112+
'static',
113+
'info',
114+
'',
115+
'<span class="text-primary" data-toggle="tooltip" title="'.$urlWLHTML.'">'.get_lang(
116+
'ValidDomainList'
117+
).' <span class="glyphicon glyphicon-question-sign"></span></span>'
118+
);
136119
$form->addButtonSend(get_lang('AddCloudLink'), 'submitDocument');
137-
120+
138121
$form->addRule('name', get_lang('PleaseEnterCloudLinkName'), 'required', null, 'client');
139122
$form->addRule('name', get_lang('PleaseEnterCloudLinkName'), 'required', null, 'server');
140123
$form->addRule('url', get_lang('PleaseEnterURL'), 'required', null, 'client');
141124
$form->addRule('url', get_lang('PleaseEnterURL'), 'required', null, 'server');
142125
// Well formed url pattern (must have the protocol)
143-
$urlRegEx = URLUtils::getWellformedUrlRegex();
126+
$urlRegEx = DocumentManager::getWellFormedUrlRegex();
144127
$form->addRule('url', get_lang('NotValidURL'), 'regex', $urlRegEx, 'client');
145128
$form->addRule('url', get_lang('NotValidURL'), 'regex', $urlRegEx, 'server');
146129
$form->addRule('url', get_lang('NotValidDomain').$urlWLText, 'regex', $urlWLRegEx, 'client');
147130
$form->addRule('url', get_lang('NotValidDomain').$urlWLHTML, 'regex', $urlWLRegEx, 'server');
148-
131+
149132
if ($form->validate()) {
150133
if (isset($_REQUEST['linkid'])) {
151-
$doc_id = DocumentManager::addCloudLink($course_info, $path, $_REQUEST['url'], $_REQUEST['name']);
134+
$doc_id = DocumentManager::addCloudLink($courseInfo, $path, $_REQUEST['url'], $_REQUEST['name']);
152135
if ($doc_id) {
153-
Display::display_confirmation_message(get_lang('CloudLinkAdded') . '<br />', false);
136+
Display::addFlash(Display::return_message(get_lang('CloudLinkAdded'), 'success', false));
154137
} else {
155-
if (DocumentManager::cloudLinkExists($course_info, $path, $_REQUEST['url'])) {
156-
Display::display_error_message(get_lang('UrlAlreadyExists'), false);
138+
if (DocumentManager::cloudLinkExists($courseInfo, $path, $_REQUEST['url'])) {
139+
Display::addFlash(Display::return_message(get_lang('UrlAlreadyExists'), 'warning', false));
157140
} else {
158-
Display::display_error_message(get_lang('ErrorAddCloudLink'), false);
141+
Display::addFlash(Display::return_message(get_lang('ErrorAddCloudLink'), 'warning', false));
159142
}
160143
}
144+
header('Location: document.php?'.api_get_cidreq());
145+
exit;
161146
}
162147
}
163148

149+
150+
// Display the header
151+
Display::display_header($nameTools, 'Doc');
152+
153+
// Actions
154+
echo '<div class="actions">';
155+
// Link back to the documents overview
156+
echo '<a href="document.php?id='.$document_id.'&'.api_get_cidreq().'">'.
157+
Display::return_icon('back.png', get_lang('BackTo').' '.get_lang('DocumentsOverview'), '', ICON_SIZE_MEDIUM).
158+
'</a>';
159+
echo '</div>';
160+
161+
// Form to select directory
162+
$folders = DocumentManager::get_all_document_folders(
163+
$_course,
164+
$groupIid,
165+
$is_allowed_to_edit
166+
);
167+
168+
echo DocumentManager::build_directory_selector(
169+
$folders,
170+
$document_id,
171+
isset($group_properties['directory']) ? $group_properties['directory'] : array()
172+
);
173+
164174
// Add tooltip and correctly parse its inner HTML
165175
echo '<script>
166176
$(document).ready(function() {
167-
$("[data-toggle=\'tooltip\']").tooltip(
168-
{
169-
content:
170-
function() {
171-
return $(this).attr("title");
172-
}
173-
}
174-
);
177+
$("[data-toggle=\'tooltip\']").tooltip({
178+
content:
179+
function() {
180+
return $(this).attr("title");
181+
}
182+
});
175183
});
176184
</script>';
177185

178-
echo $form->return_form();
186+
echo $form->returnForm();
179187
Display::display_footer();

main/document/document.php

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -262,37 +262,35 @@
262262
$_GET['deleteid'],
263263
$groupIid
264264
);
265+
if ($deleteDocument) {
266+
$certificateId = isset($_GET['delete_certificate_id']) ? $_GET['delete_certificate_id'] : null;
267+
DocumentManager::remove_attach_certificate(
268+
api_get_course_id(),
269+
$certificateId
270+
);
271+
Display::addFlash(
272+
Display::return_message(
273+
get_lang('DocDeleted').': '.$documentInfo['title'],
274+
'success'
275+
)
276+
);
277+
} else {
278+
Display::addFlash(Display::return_message(get_lang('DocDeleteError'), 'warning'));
279+
}
265280
} else {
266281
// Cloud Links
267-
$deleteDocument = DocumentManager::deleteCloudLink($_course, $_GET['deleteid']);
268-
}
269-
270-
if ($deleteDocument) {
271-
$certificateId = isset($_GET['delete_certificate_id']) ? $_GET['delete_certificate_id'] : null;
272-
DocumentManager::remove_attach_certificate(
273-
api_get_course_id(),
274-
$certificateId
275-
);
276-
if ($documentInfo['filetype'] != 'link') {
282+
$deleteDocument = DocumentManager::deleteCloudLink($courseInfo, $_GET['deleteid']);
283+
if ($deleteDocument) {
277284
Display::addFlash(Display::return_message(
278-
get_lang('DocDeleted').': '.$documentInfo['title'],
285+
get_lang('CloudLinkDeleted').': '.$documentInfo['title'],
279286
'success'
280287
));
281288
} else {
282-
if ($documentInfo['filetype'] != 'link') {
283-
Display::addFlash(Display::return_message(
284-
get_lang('CloudLinkDeleted').': '.$data['title'],
285-
'success'
286-
));
287-
} else {
288-
Display::addFlash(Display::return_message(
289-
get_lang('CloudLinkDeleteError').': '.$data['title'],
290-
'error'
291-
));
292-
}
289+
Display::addFlash(Display::return_message(
290+
get_lang('CloudLinkDeleteError').': '.$documentInfo['title'],
291+
'error'
292+
));
293293
}
294-
} else {
295-
Display::addFlash(Display::return_message(get_lang('DocDeleteError'), 'warning'));
296294
}
297295
} else {
298296
Display::addFlash(Display::return_message(get_lang('FileNotFound'), 'warning'));
@@ -1067,7 +1065,7 @@ function convertModal (id, format) {
10671065
if (!DocumentManager::cloudLinkExists($_course, $moveTo, $document_to_move['comment'])) {
10681066
$doc_id = $moveFile;
10691067
DocumentManager::updateDBInfoCloudLink($document_to_move['path'], $moveTo.'/', $doc_id);
1070-
1068+
10711069
//update database item property
10721070
api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'FileMoved', api_get_user_id(), $to_group_id, null, null, null, $session_id);
10731071
Display::addFlash(

0 commit comments

Comments
 (0)