[Fusionforge-commits] r12564 - in trunk/src/plugins/projectimport: common www

Olivier Berger olberger at fusionforge.org
Fri Mar 4 12:43:36 CET 2011


Author: olberger
Date: 2011-03-04 12:43:34 +0100 (Fri, 04 Mar 2011)
New Revision: 12564

Added:
   trunk/src/plugins/projectimport/www/usersimport.php
Modified:
   trunk/src/plugins/projectimport/common/ProjectImportPlugin.class.php
   trunk/src/plugins/projectimport/common/ProjectImporter.class.php
   trunk/src/plugins/projectimport/www/projectsimport.php
Log:
Add users import


Modified: trunk/src/plugins/projectimport/common/ProjectImportPlugin.class.php
===================================================================
--- trunk/src/plugins/projectimport/common/ProjectImportPlugin.class.php	2011-03-04 11:43:20 UTC (rev 12563)
+++ trunk/src/plugins/projectimport/common/ProjectImportPlugin.class.php	2011-03-04 11:43:34 UTC (rev 12564)
@@ -36,7 +36,9 @@
 		$this->hooks[] = "userisactivecheckboxpost" ; //
 		$this->hooks[] = "project_admin_plugins"; // to show up in the admin page fro group
 		*/
+		// The plugin has a link added to the Project administration part of site admin
 		$this->hooks[] = "site_admin_project_maintenance_hook";
+		$this->hooks[] = "site_admin_user_maintenance_hook";
 	}
 
 	function CallHook ($hookname, $params) {
@@ -158,7 +160,7 @@
 	}
 	
 	/**
-	 * Displays the link in the Project Admin part of the Site Admin ('site_admin_project_maintenance_hook' plugin_hook_by_reference() -style hook)
+	 * Displays the link in the Project Maintenance part of the Site Admin ('site_admin_project_maintenance_hook' plugin_hook_by_reference() -style hook)
 	 * @param array $params for concatenating return value in ['results']
 	 */
 	function site_admin_project_maintenance_hook (&$params) {
@@ -169,6 +171,18 @@
 		$params['result'] = $html;
 	}
 
+	/**
+	 * Displays the link in the User Maintenance part of the Site Admin ('site_admin_user_maintenance_hook' plugin_hook_by_reference() -style hook)
+	 * @param array $params for concatenating return value in ['results']
+	 */
+	function site_admin_user_maintenance_hook (&$params) {
+		$html = $params['result'];
+		$html .= '<li>'.
+			util_make_link ('/plugins/'.$this->name.'/usersimport.php',
+						     _("Import users"). ' [' . _('Project import plugin') . ']') .'</li>';
+		$params['result'] = $html;
+	}
+
 	
 }
 

Modified: trunk/src/plugins/projectimport/common/ProjectImporter.class.php
===================================================================
--- trunk/src/plugins/projectimport/common/ProjectImporter.class.php	2011-03-04 11:43:20 UTC (rev 12563)
+++ trunk/src/plugins/projectimport/common/ProjectImporter.class.php	2011-03-04 11:43:34 UTC (rev 12564)
@@ -35,6 +35,7 @@
 define('PLANETFORGE_NS', 'http://coclico-project.org/ontology/planetforge#');
 
 class ImportedProject {
+	protected $res;
 	
 	protected $full_name;
 	//protected $purpose;
@@ -156,7 +157,45 @@
 }
 
 class ImportedUser {
-	protected $initial_role;
+	protected $res;
+	
+	protected $unix_name;
+	protected $firstname;
+	protected $lastname;
+	protected $email;	
+	
+	//protected $initial_role;
+	function ImportedUser($res) {
+		$this->res = $res;
+		
+		$this->unix_name = $this->res->getPropValue('foaf:accountName');
+		$this->email = $this->res->getPropValue('sioc:email');
+
+	}
+	function init_owner_props($res) {
+		$name = $res->getPropValue('foaf:name');
+		print_r('Name: '.$name);
+		$first = '';
+		$last = '';
+		list($first, $last) = explode(' ', str_replace('/\s+/gi',' ',$name), 2);
+		print_r('first: '.$first);
+		print_r('last: '.$last);
+		$this->firstname = $first;
+		$this->lastname = $last;
+	}	
+	function getUnixName() {
+		return $this->unix_name;
+	}
+	function getEmail() {
+		return $this->email;
+	}
+	function getFirstname() {
+		return $this->firstname;
+	}
+	function getLastname() {
+		return $this->lastname;
+	}
+	
 }
 /**
  * TODO Enter description here ...
@@ -447,13 +486,18 @@
 			$dumpres = $this->project_dump();
 			
 			$this->users = array();
+			$this->user_objs = array();
 			
 			// parse the users
 			$users = $dumpres->getPropValues('forgeplucker:users');
 			foreach ($users as $user) {
 				//	      print_r($this->index[$user]);
 				$res = ProjectImporter::make_resource($user);
-				$accountName = $res->getPropValue('foaf:accountName');
+				
+				$user_obj = new ImportedUser($res);
+				$this->user_objs[$user] = $user_obj;
+				
+				$accountName = $user_obj->getUnixName();
 				$this->user_names[$user] = $accountName;
 				$this->users[$user] = $res;
 				//			print 'Found user : '. $accountName . "\n";
@@ -478,6 +522,9 @@
 					if (! $user->getPropValue('sioc:account_of')) {
 						$user->setProp('sioc:account_of', $person);
 					}
+					
+					$user_obj = & $this->user_objs[$account];
+					$user_obj->init_owner_props($res);
 				}
 			}
 			/*		foreach ($this->users as $user) {
@@ -488,6 +535,16 @@
 		}
 		return $this->users;
 	}
+	function get_user_objs() {
+		if (! $this->user_objs) {
+			
+			$this->get_users();
+			
+		}
+		return $this->user_objs;
+	}
+	
+	
 	function supportsTool($tool)
 	{
 		return in_array($tool, $this->providers);

Modified: trunk/src/plugins/projectimport/www/projectsimport.php
===================================================================
--- trunk/src/plugins/projectimport/www/projectsimport.php	2011-03-04 11:43:20 UTC (rev 12563)
+++ trunk/src/plugins/projectimport/www/projectsimport.php	2011-03-04 11:43:34 UTC (rev 12564)
@@ -19,6 +19,7 @@
 // it will stay there until approved by and an admin (no auto approval)
 // Nothing more done, like importing users/roles/data : will need to be approved first
 
+// TODO : ask for confirmation on projects to be created, instead of creating directly without confirmation
 
 require_once('../../../www/env.inc.php');
 require_once $gfwww.'include/pre.php';
@@ -60,7 +61,7 @@
 		global $feedback;
 		
 		$params= array();
-		$params['title']=_('Project importer');
+		$params['title']=_('Projects importer');
 		$params['toptab']='projectimport';
 		
 		site_admin_header($params);

Added: trunk/src/plugins/projectimport/www/usersimport.php
===================================================================
--- trunk/src/plugins/projectimport/www/usersimport.php	                        (rev 0)
+++ trunk/src/plugins/projectimport/www/usersimport.php	2011-03-04 11:43:34 UTC (rev 12564)
@@ -0,0 +1,243 @@
+<?php
+
+/**
+ * ProjectImport plugin for FusionForge 5.0.x
+ *
+ *
+ * This is the beginning of a project import pugin
+ * 
+ * Author : Olivier Berger <olivier.berger at it-sudparis.eu>
+ * 
+ * Copyright (c) Olivier Berger & Institut Télécom
+ * 
+ * Released under the GNU GPL v2 or later
+ * 
+ */
+
+// Import users from a JSON file (Site Admin tool)
+
+require_once('../../../www/env.inc.php');
+require_once $gfwww.'include/pre.php';
+require_once $gfwww.'admin/admin_utils.php';
+
+require_once $gfplugins.'projectimport/common/ProjectImporter.class.php';
+
+include_once('arc/ARC2.php');
+
+/**
+ * Manages the display of the page : HTML + forms
+ * 
+ * @author Olivier Berger
+ *
+ */
+class UsersImportPage {
+
+	protected $message;
+	
+	protected $importer;
+
+	protected $form_header_already_displayed;
+	
+	protected $html_generator;
+	
+	function UsersImportPage($HTML) {
+		$this->html_generator = $HTML;
+		$this->message = '';
+		$this->form_header_already_displayed = false;
+		
+		$this->importer = ProjectImporter::getInstance();
+	}
+	
+	/**
+	 * Display initial contents of the page
+	 * @param string $message
+	 */
+	function display_headers($message) {
+		global $feedback;
+		
+		$params= array();
+		$params['title']=_('Users importer');
+		$params['toptab']='projectimport';
+		
+		site_admin_header($params);
+		
+		$this->message .= $message;
+	}
+	
+	/**
+	 * Display the page
+	 */
+	function display_main() {
+		
+		global $feedback;
+		
+		// Do the work, first !
+		$html = $this->do_work();
+				
+		if($this->message) {
+			echo $this->message . '<br />';
+		}
+		html_feedback_top($feedback);
+		
+		echo $html;
+		
+		// If invoked initially (not on callback) or if more details needed
+		// display the last part of the form for JSON file upload
+		if (! $this->form_header_already_displayed) {
+			echo '<form enctype="multipart/form-data" action="'.getStringFromServer('PHP_SELF').'" method="post">';
+			$this->form_header_already_displayed = True;
+		}
+
+		// finally, display the file upload form
+		echo '<fieldset><legend>Please upload a file :</legend>
+		       <p><center>
+                          <input type="file" id="json" name="json" tabindex="2" size="30" />
+                       </center></p>
+                    </fieldset>
+                    <div style="text-align:center;">
+                      <input type="submit" name="submit" value="Submit" />
+                    </div>
+              </form>';
+
+		site_footer(array());
+	}
+	
+	/**
+	 * Initializes data structures from POSTed data coming from the form input
+	 */
+	function initialize_from_submitted_data() {
+		global $feedback;
+
+		$json = getUploadedFile('json');
+		$imported_file = $json['tmp_name'];
+		$json = fread(fopen($imported_file, 'r'), $json['size']);
+		if(! $json) {
+			$feedback = "Error : missing data";
+		}
+		else {
+
+			//			print_r($imported_file);
+			$this->importer->parse_OSLCCoreRDFJSON($json);
+
+			$debug = TRUE;
+			if($debug) {
+			 // Debug the loaded triples 
+			 $triples = $this->importer->parse_OSLCCoreRDFJSON($json);
+			 $ser = ARC2::getTurtleSerializer();
+
+			 if(count($triples)) {
+				$this->message .= '<pre>'. nl2br(htmlspecialchars($ser->toTurtle($triples))) . '</pre>';
+				}
+			}
+		}
+	}
+	
+	
+	/**
+	 * Does the main work
+	 * @return html string
+	 */
+	function do_work() {
+		global $feedback;
+
+		$html = '';
+
+		// If the posted JSON file indeed contains a project dump, an importer was created,
+		// and if it has data we can work
+		// If it indeed has valid data
+		if ($this->importer->has_project_dump()) {
+			$this->message .= "Here are the results from your upload :";
+				
+			// start HTML output
+			if (! $this->form_header_already_displayed) {
+				$this->form_header_already_displayed = true;
+				$html .= '<form enctype="multipart/form-data" action="'.getStringFromServer('PHP_SELF').'" method="post">';
+			}
+				
+			$imported_users = $this->importer->get_user_objs();
+				
+			if (count($imported_users)) {
+				$html .= $this->html_generator->boxTop(_("Users found in imported file"));
+
+				foreach($imported_users as $user => $user_obj) {
+
+					$unix_name = $user_obj->getUnixName();
+					$email = $user_obj->getEmail();
+
+					$firstname = $user_obj->getFirstname();
+					$lastname = $user_obj->getLastname();
+					
+					$theme_id=$this->html_generator->getThemeIdFromName(forge_get_config('default_theme'));
+					$password1 = substr(md5($GLOBALS['session_ser'] . time() . util_randbytes()), 0, 8);
+					$password2 = $password1;
+					$language_id = language_name_to_lang_id (choose_language_from_context ());
+					
+					$new_user = new GFUser();
+					$res = $new_user->create($unix_name,$firstname,$lastname,$password1,$password2,
+						$email,$mail_site,$mail_va,$language_id,$timezone,$jabber_address,$jabber_only,$theme_id,'',
+						$address,$address2,$phone,$fax,$title,$ccode,$send_mail);
+
+					if (!$res) {
+						$error_msg = $new_user->getErrorMessage();
+						if ($feedback) $feedback .= '<br />';
+						$feedback .= 'Import of "'. $unix_name . '": '. $error_msg;
+							
+						$html .= _('Failed to create user'). ': <pre>'. $unix_name .'</pre>';
+					}
+					else {
+						$html .= _('Created user'). ': <pre>'. $unix_name .'</pre>';
+					}
+
+					$html .= 'User :<br />';
+					$html .= ' account name : '. $unix_name .'<br />';
+					$html .= ' email : '. $email .'<br />';
+					$html .= ' firstname : '. $firstname .'<br />';
+					$html .= ' lastname : '. $lastname .'<br />';
+					$html .= '<br/>';
+				}
+
+				$html .= $this->html_generator->boxBottom();
+
+			}
+		}
+		else {
+				$feedback .= 'parsing problem <br />';
+		}
+		
+		return $html;
+	}
+	
+	
+}
+
+// The user should be forge admin
+session_require_global_perm ('forge_admin');
+
+global $group_id, $feedback;
+
+$this_page = new UsersImportPage($HTML);
+
+//print_r($_POST);
+
+$message = '';
+
+// when called back by post form we can initialize some elements provided by the user
+if (getStringFromRequest('submit')) {
+
+	$this_page->initialize_from_submitted_data();
+		
+}
+else {
+	$message .= "You can import a list of users from a JSON RDF document compatible with ForgePlucker's dump format.<br />";
+}
+
+$this_page->display_headers($message);
+
+$this_page->display_main();
+
+
+
+// Local Variables:
+// mode: php
+// c-file-style: "bsd"
+// End:




More information about the Fusionforge-commits mailing list