4 * Project importing script for site admin
6 * Copyright (c) 2011 Olivier Berger & Institut Telecom
8 * This program was developped in the frame of the COCLICO project
9 * (http://www.coclico-project.org/) with financial support of the Paris
12 * This file is part of FusionForge. FusionForge is free software;
13 * you can redistribute it and/or modify it under the terms of the
14 * GNU General Public License as published by the Free Software
15 * Foundation; either version 2 of the Licence, or (at your option)
18 * FusionForge is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 // Import projects from a JSON file (Site Admin tool)
29 // will just create the project as if submitted from register/index.php
30 // it will stay there until approved by and an admin (no auto approval)
31 // Nothing more done, like importing users/roles/data : will need to be approved first
33 // TODO : ask for confirmation on projects to be created, instead of creating directly without confirmation
35 require_once('../../../www/env.inc.php');
36 require_once 'OpenDocument.php';
38 require_once $gfwww.'include/pre.php';
39 require_once $gfwww.'admin/admin_utils.php';
41 require_once $gfplugins.'projectimport/common/ProjectImporter.class.php';
42 require_once $gfplugins.'projectimport/common/UploadedFiles.class.php';
44 include_once('arc/ARC2.php');
47 * Manages the display of the page : HTML + forms
49 * @author Olivier Berger
52 class ProjectsImportPage extends FileManagerPage {
56 protected $form_header_already_displayed;
58 function ProjectsImportPage($HTML) {
59 $this->form_header_already_displayed = false;
61 $this->importer = ProjectImporter::getInstance();
63 $storage = new SiteAdminFilesDirectory($HTML);
65 parent::FileManagerPage($HTML, $storage);
69 * Display initial contents of the page
70 * @param string $message
72 function display_headers($message) {
76 $params['title']=_('Project importer');
77 $params['toptab']='projectimport';
79 site_admin_header($params);
81 $this->message .= $message;
87 function display_main() {
91 // Do the work, first !
92 $html = $this->do_work();
95 echo $this->message . '<br />';
97 html_feedback_top($feedback);
101 // If invoked initially (not on callback) or if more details needed
102 // display the last part of the form for JSON file upload
103 if (! $this->form_header_already_displayed) {
104 echo '<form enctype="multipart/form-data" action="'.getStringFromServer('PHP_SELF').'" method="post">';
105 $this->form_header_already_displayed = True;
108 $preselected = False;
111 if ($this->posted_selecteddumpfile) {
112 $preselected = basename($this->posted_selecteddumpfile);
114 elseif ($this->posted_uploadedfile) {
115 $preselected = $this->posted_uploadedfile;
119 $selectiondialog = $this->storage->displayFileSelectionForm($preselected);
121 echo $selectiondialog;
123 // finally, display the file upload form
124 echo '<fieldset><legend>Please upload a file :</legend>
126 <input type="file" id="uploaded_file" name="uploaded_file" tabindex="2" size="30" />
129 <div style="text-align:center;">
130 <input type="submit" name="submit" value="Submit" />
134 site_footer(array());
138 * Initializes data structures from POSTed data coming from the form input
140 function initialize_from_submitted_data() {
143 $filechosen = $this->initialize_chosenfile_from_submitted();
146 $filepath = $this->posted_selecteddumpfile;
148 if ($this->storage->getMimeType($filepath) == 'application/x-planetforge-forge-export') {
149 $package = Opendocument::open($filepath);
152 $dumpfilenames = $package->getFileNamesByMediaType('application/x-forgeplucker-oslc-rdf+json');
153 if (count($dumpfilenames) == 1) {
154 $filename = $dumpfilenames[0];
156 $contents = $package->getFileContents($filename);
162 //print_r($filechosen);
163 $json = fread(fopen($filepath, 'r'), filesize($filepath));
166 $feedback = "Error : missing data";
170 // print_r($imported_file);
171 $this->importer->parse_OSLCCoreRDFJSON($json);
175 // Debug the loaded triples
176 $triples = $this->importer->parse_OSLCCoreRDFJSON($json);
177 $ser = ARC2::getTurtleSerializer();
179 if(count($triples)) {
180 $this->message .= '<pre>'. nl2br(htmlspecialchars($ser->toTurtle($triples))) . '</pre>';
186 if ((! $this->posted_selecteddumpfile) && (! $this->posted_uploadedfile)) {
187 $this->feedback(_('Please select an existing file to process, or upload a new one'));
195 * initialize_from_submitted_data() has already been called to intialize objects sent as POST vars
197 * @return html string
204 // If the posted JSON file indeed contains a project dump, an importer was created,
205 // and if it has data we can work
206 // If it indeed has valid data
207 if ($this->importer->has_project_dump()) {
208 $this->message .= "Here are the results from your upload :";
210 $projects = $this->importer->get_projects();
213 if (! $this->form_header_already_displayed) {
214 $this->form_header_already_displayed = true;
215 $html .= '<form enctype="multipart/form-data" action="'.getStringFromServer('PHP_SELF').'" method="post">';
217 // Then handle project(s)
219 if(count($projects)) {
221 // Display project's general description
222 $html .= '<table id="project-summary-and-devs" class="my-layout-table" summary="">';
224 // Display project attributes
225 foreach($projects as $project) {
227 $full_name = $project->getFullName();
228 $unix_name = $project->getUnixName();
229 $description = $project->getDescription();
230 $purpose = 'Imported from JSON file';
232 $is_public = $project->getIsPublic();
233 $send_mail = ! forge_get_config ('project_auto_approval') ;
234 $built_from_template = 0 ;
236 $group = new Group();
237 $u = session_get_user();
238 $res = $group->create(
248 $built_from_template);
251 $error_msg = $group->getErrorMessage();
252 if ($feedback) $feedback .= '<br />';
253 $feedback .= 'Import of "'. $unix_name . '": '. $error_msg;
257 <h2>'._('Failed to create project'). ': <pre>'. $unix_name .'</pre>
263 <h2>'._('Created project'). ': <pre>'. $unix_name .'</pre>
267 $html .= '<h3>'._('Project summary').'</h3>';
268 $html .= '<p><pre>'. $description .'</pre></p>';
270 $html .= '<p>full_name : '. $full_name .'</p>';
271 //$html .= '<p>purpose : '. $project->getPurpose() .'</p>';
272 $html .= '<p>is_public : '. $is_public .'</p>';
273 $html .= '</td></tr>';
279 $feedback .= 'Found no projects <br />';
288 // The user should be forge admin
289 session_require_global_perm ('forge_admin');
291 global $group_id, $feedback;
293 $this_page = new ProjectsImportPage($HTML);
299 // when called back by post form we can initialize some elements provided by the user
300 if (getStringFromRequest('submit')) {
302 $this_page->initialize_from_submitted_data();
306 $message .= "You can import a list of projects from a JSON RDF document compatible with ForgePlucker's dump format.<br />";
309 $this_page->display_headers($message);
311 $this_page->display_main();
317 // c-file-style: "bsd"