|
4 | 4 | * This script allows to add cloud file links to the document structure |
5 | 5 | * @package chamilo.document |
6 | 6 | */ |
7 | | -/** |
8 | | - * Code |
9 | | - */ |
10 | | - |
11 | | -// Including the global initialization file |
| 7 | + |
12 | 8 | 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'; |
17 | 9 |
|
18 | 10 | $fileLinkEnabled = api_get_configuration_value('enable_add_file_link'); |
19 | 11 |
|
20 | 12 | if (!$fileLinkEnabled) { |
21 | 13 | api_not_allowed(true); |
22 | 14 | } |
23 | | - |
24 | | -$course_info = api_get_course_info(); |
25 | | - |
26 | | -if (empty($course_info)) { |
27 | | - api_not_allowed(true); |
28 | | -} |
29 | 15 |
|
30 | | -if ($is_certificate_mode) { |
| 16 | +$courseInfo = api_get_course_info(); |
| 17 | + |
| 18 | +if (empty($courseInfo)) { |
31 | 19 | api_not_allowed(true); |
32 | 20 | } |
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); |
35 | 23 | if (empty($document_data)) { |
36 | | - $document_id = $parent_id = 0; |
| 24 | + $document_id = $parent_id = 0; |
37 | 25 | $path = '/'; |
38 | 26 | } else { |
39 | 27 | 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)); |
43 | 31 | } |
| 32 | + $dir = dirname($document_data['path']); |
44 | 33 | } |
45 | | - |
| 34 | + |
| 35 | +$is_certificate_mode = DocumentManager::is_certificate_mode($dir); |
| 36 | + |
| 37 | +if ($is_certificate_mode) { |
| 38 | + api_not_allowed(true); |
| 39 | +} |
| 40 | + |
46 | 41 | $is_allowed_to_edit = api_is_allowed_to_edit(null, true); |
47 | | - |
| 42 | + |
| 43 | +$groupIid = 0; |
48 | 44 | if (api_get_group_id()) { |
49 | 45 | // 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'; |
52 | 46 | // Get group info |
53 | 47 | $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 | + ); |
59 | 55 | } else { |
60 | 56 | api_not_allowed(true); |
61 | 57 | } |
62 | 58 | } elseif ($is_allowed_to_edit || DocumentManager::is_my_shared_folder(api_get_user_id(), $path, api_get_session_id())) { |
63 | 59 | // Admin for "regular" upload, no group documents. And check if is my shared folder |
64 | | - $to_group_id = 0; |
65 | | - $req_gid = ''; |
66 | 60 | } else { // No course admin and no group member... |
67 | 61 | api_not_allowed(true); |
68 | 62 | } |
69 | 63 |
|
70 | 64 | // Group docs can only be uploaded in the group directory |
71 | | -if ($to_group_id != 0 && $path == '/') { |
| 65 | +if ($groupIid != 0 && $path == '/') { |
72 | 66 | $path = $group_properties['directory'] . "/"; |
73 | 67 | } |
74 | | - |
| 68 | + |
75 | 69 | // 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 | + |
78 | 75 | // Interbreadcrumb for the current directory root path |
79 | 76 | if (empty($document_data['parents'])) { |
80 | 77 | // Hack in order to not add the document to the breadcrumb in case it is a link |
|
85 | 82 | foreach ($document_data['parents'] as $document_sub_data) { |
86 | 83 | // Hack in order to not add the document to the breadcrumb in case it is a link |
87 | 84 | 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 | + ); |
89 | 89 | } |
90 | 90 | } |
91 | 91 | } |
92 | | - |
93 | | -$this_section = SECTION_COURSES; |
94 | | - |
95 | | -$nameTools = get_lang('LinkAdd'); |
96 | 92 |
|
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; |
114 | 94 |
|
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'); |
121 | 96 | $action = api_get_self().'?'.api_get_cidreq().'&id='.$document_id; |
122 | | - |
| 97 | + |
123 | 98 | // URLs in whitelist |
124 | | -$urlWL = URLUtils::getFileHostingsWL(); |
| 99 | +$urlWL = DocumentManager::getFileHostingWhiteList(); |
| 100 | + |
125 | 101 | sort($urlWL); |
126 | 102 | $urlWLRegEx = '/(\/\/|\.)('.implode('|', $urlWL).')/i'; //Matches any of the whitelisted urls preceded by // or . |
127 | 103 | $urlWLText = "\n\t* ".implode("\n\t* ", $urlWL); |
128 | 104 | $urlWLHTML = "<ul><li>".implode("</li><li>", $urlWL)."</li></ul>"; |
129 | | - |
| 105 | + |
130 | 106 | $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); |
133 | 109 | $form->addElement('text', 'name', get_lang('LinkName'), array('id' => 'name_link')); |
134 | 110 | $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 | +); |
136 | 119 | $form->addButtonSend(get_lang('AddCloudLink'), 'submitDocument'); |
137 | | - |
| 120 | + |
138 | 121 | $form->addRule('name', get_lang('PleaseEnterCloudLinkName'), 'required', null, 'client'); |
139 | 122 | $form->addRule('name', get_lang('PleaseEnterCloudLinkName'), 'required', null, 'server'); |
140 | 123 | $form->addRule('url', get_lang('PleaseEnterURL'), 'required', null, 'client'); |
141 | 124 | $form->addRule('url', get_lang('PleaseEnterURL'), 'required', null, 'server'); |
142 | 125 | // Well formed url pattern (must have the protocol) |
143 | | -$urlRegEx = URLUtils::getWellformedUrlRegex(); |
| 126 | +$urlRegEx = DocumentManager::getWellFormedUrlRegex(); |
144 | 127 | $form->addRule('url', get_lang('NotValidURL'), 'regex', $urlRegEx, 'client'); |
145 | 128 | $form->addRule('url', get_lang('NotValidURL'), 'regex', $urlRegEx, 'server'); |
146 | 129 | $form->addRule('url', get_lang('NotValidDomain').$urlWLText, 'regex', $urlWLRegEx, 'client'); |
147 | 130 | $form->addRule('url', get_lang('NotValidDomain').$urlWLHTML, 'regex', $urlWLRegEx, 'server'); |
148 | | - |
| 131 | + |
149 | 132 | if ($form->validate()) { |
150 | 133 | 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']); |
152 | 135 | if ($doc_id) { |
153 | | - Display::display_confirmation_message(get_lang('CloudLinkAdded') . '<br />', false); |
| 136 | + Display::addFlash(Display::return_message(get_lang('CloudLinkAdded'), 'success', false)); |
154 | 137 | } 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)); |
157 | 140 | } else { |
158 | | - Display::display_error_message(get_lang('ErrorAddCloudLink'), false); |
| 141 | + Display::addFlash(Display::return_message(get_lang('ErrorAddCloudLink'), 'warning', false)); |
159 | 142 | } |
160 | 143 | } |
| 144 | + header('Location: document.php?'.api_get_cidreq()); |
| 145 | + exit; |
161 | 146 | } |
162 | 147 | } |
163 | 148 |
|
| 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 | + |
164 | 174 | // Add tooltip and correctly parse its inner HTML |
165 | 175 | echo '<script> |
166 | 176 | $(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 | + }); |
175 | 183 | }); |
176 | 184 | </script>'; |
177 | 185 |
|
178 | | -echo $form->return_form(); |
| 186 | +echo $form->returnForm(); |
179 | 187 | Display::display_footer(); |
0 commit comments