This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "FusionForge".

The branch, master has been updated
       via  a4bdf7672f3b93aaa5303fc503817b9996f1d7fe (commit)
      from  c149e004a7d30f5795e4dbff67416df9c99c1842 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
https://scm.fusionforge.org/anonscm/gitweb/?p=fusionforge/fusionforge.git;a=commitdiff;h=a4bdf7672f3b93aaa5303fc503817b9996f1d7fe

commit a4bdf7672f3b93aaa5303fc503817b9996f1d7fe
Author: Franck Villaume <[email protected]>
Date:   Sun Apr 23 20:49:44 2017 +0200

    forge cli: docgroupImport, initial code

diff --git a/src/bin/forge b/src/bin/forge
index 76f46d7..6f0acd3 100755
--- a/src/bin/forge
+++ b/src/bin/forge
@@ -37,6 +37,9 @@ class CliActions {
                                  "   pretty json formated string output\n".
                                  " - raw: (default)\n".
                                  "   standard PHP array print_r format\n";
+               $importFormatInfo = "The <file> must contain the exact 
information to import.\n".
+                                   "Best practice is to reuse the result of an 
export and modify it.\n".
+                                   "Use getZipDump function to get binary 
organization sample.\n";
                switch ($method) {
                        case 'help':
                                echo "Use: .../forge help ($methods)\n";
@@ -65,6 +68,11 @@ class CliActions {
                                echo "to dump a document from a specific 
project.\n";
                                echo $dumpFormatInfo;
                                break;
+                       case 'documentImport':
+                               echo "Use: .../forge documentImport <file> 
[json|raw]\n";
+                               echo "to import a document.\n";
+                               echo $dumpFormatInfo.$importFormatInfo;
+                               break;
                        case 'docgroupDump':
                                echo "Use: .../forge docgroupDump <docgroupid> 
[json|raw]\n";
                                echo "to dump a document group (folder) from a 
specific project.\n";
@@ -319,31 +327,32 @@ class CliActions {
        }
 
        function documentDump($docid, $format = 'json') {
-               $documentDump['id'] = $docid;
                $doc = document_get_object($docid);
                if ($doc && is_object($doc) && !$doc->isError()) {
                        $dvf = new DocumentVersionFactory($doc);
+                       $documentDump = (array)$doc;
+                       unset($documentDump['Group']);
                        $documentDump['versions'] = $dvf->getVersions();
                        $serialIDs = $dvf->getSerialIDs();
                        $drf = new DocumentReviewFactory($doc);
                        $documentDump['reviews'] = $drf->getReviews($serialIDs);
-                       $documentDump['document'] = (array)$doc;
-                       unset($documentDump['document']['Group']);
                        $documentDump['monitor'] = $doc->getMonitorIds();
                } else {
                        $documentDump['error'][] = _('Unable to get document 
id')._(': ').$docid;
                }
+               $documentDump['id'] = $docid;
                ksort($documentDump);
                $this->print_result($documentDump, $format);
                return $documentDump;
        }
 
        function documentImport($file, $format = 'json') {
+               global $importRefMapping;
                $stream = $this->loadFile($file, $format);
                if (!$stream) {
                        return false;
                }
-               $group_id = $stream['document']['data_array']['group_id'];
+               $group_id = 
$this->getMappingId($stream['data_array']['group_id'], 'group');
                $group = group_get_object($group_id);
                $importSerialIdRef = array();
                if ($group && is_object($group) && !$group->isError() && 
$group->usesDocman()) {
@@ -368,20 +377,22 @@ class CliActions {
                                        $filedatatype = $version['filetype'];
                                        $filedataname = '';
                                }
-                               $doc_group_id = 
$stream['document']['data_array']['doc_group'];
+
+                               $doc_group_id = 
$this->getMappingId($stream['data_array']['doc_group'], 'doc_group');
                                $doc_group = 
documentgroup_get_object_byid($doc_group_id);
                                if (!$doc_group || !is_object($doc_group) || 
$doc_group->isError()) {
                                        echo _('Invalid document group 
(folder)')._(': ').$doc_group_id."\n";
                                        db_rollback();
                                        return false;
                                }
-                               $importData['user'] = $version['created_by'];
+                               $created_by = 
$this->getMappingId($version['created_by'], 'user');
+                               $importData['user'] = $created_by;
                                $importData['nopermcheck'] = true;
                                $importData['nonotice'] = true;
                                $importData['time'] = $version['createdate'];
                                if ($firstversion == true) {
                                        $d = new Document($group);
-                                       if (!$d->create($version['filename'], 
$filedatatype, $filedataname, $doc_group_id, $version['title'], 
$version['description'], $stream['document']['data_array']['stateid'], 
$version['vcomment'], $importData)) {
+                                       if (!$d->create($version['filename'], 
$filedatatype, $filedataname, $doc_group_id, $version['title'], 
$version['description'], $stream['data_array']['stateid'], 
$version['vcomment'], $importData)) {
                                                echo $d->getErrorMessage()."\n";
                                                db_rollback();
                                                return false;
@@ -389,7 +400,7 @@ class CliActions {
                                                $firstversion = false;
                                        }
                                } else {
-                                       if (!$d->update($version['filename'], 
$filedatatype, $filedataname, $doc_group_id, $version['title'], 
$version['description'], $stream['document']['data_array']['stateid'], 0, 
$version['current_version'], 1, $importData, $version['vcomment'])) {
+                                       if (!$d->update($version['filename'], 
$filedatatype, $filedataname, $doc_group_id, $version['title'], 
$version['description'], $stream['data_array']['stateid'], 0, 
$version['current_version'], 1, $importData, $version['vcomment'])) {
                                                echo $d->getErrorMessage()."\n";
                                                db_rollback();
                                                return false;
@@ -404,13 +415,15 @@ class CliActions {
                                        $reviewmandatoryusers = array();
                                        $reviewoptionalusers = array();
                                        foreach($review['users'] as 
$reviewuser) {
+                                               $userid = 
$this->getMappingId($reviewuser['userid'], 'user');
                                                if ($reviewuser['typeid'] == 1) 
{
-                                                       $reviewmandatoryusers[] 
= $reviewuser['userid'];
+                                                       $reviewmandatoryusers[] 
= $userid;
                                                } elseif ($reviewuser['typeid'] 
== 2) {
-                                                       $reviewoptionalusers[] 
= $reviewuser['userid'];
+                                                       $reviewoptionalusers[] 
= $userid;
                                                }
                                        }
-                                       $importData['user'] = 
$review['created_by'];
+                                       $created_by = 
$this->getMappingId($review['created_by'], 'user');
+                                       $importData['user'] = $created_by;
                                        $importData['startdate'] = 
$review['startdate'];
                                        $importData['enddate'] = 
$review['enddate'];
                                        $importData['nonotice'] = true;
@@ -426,7 +439,7 @@ class CliActions {
                                                                db_rollback();
                                                                return false;
                                                        } else {
-                                                               if 
($comment['attachment']['storageref'] != null) {
+                                                               if 
(isset($comment['attachment']['storageref']) && 
$comment['attachment']['storageref'] != null) {
                                                                        if 
(!$nc->attachFile($comment['attachment']['data_array']['filename'], 
$comment['attachment']['data_array']['filetype'], 
$comment['attachment']['data_array']['createdate'], 
$comment['attachment']['storageref'])) {
                                                                                
echo $nc->getErrorMessage()."\n";
                                                                                
db_rollback();
@@ -439,13 +452,16 @@ class CliActions {
                                }
                        }
                        db_commit();
+                       
$importRefMapping['document'][$stream['data_array']['docid']] = $d->getID();
+                       echo _('Document injected')."\n";
+                       return true;
                } else {
                        echo _('Unable to get project to inject document')._(': 
').$group_id."\n";
+                       return false;
                }
        }
 
        function docgroupDump($docgroup_id, $format = 'json') {
-               $docgroupDump['id'] = $docgroup_id;
                $docgroup = documentgroup_get_object_byid($docgroup_id);
                if ($docgroup && is_object($docgroup) && !$docgroup->isError()) 
{
                        $dm = new DocumentManager($docgroup->getGroup());
@@ -464,23 +480,37 @@ class CliActions {
                } else {
                        $docgroupDump['error'][] = _('Unable to get docgroup 
id')._(': ').$docgroup_id;
                }
+               $docgroupDump['id'] = $docgroup_id;
                ksort($docgroupDump);
                $this->print_result($docgroupDump, $format);
                return $docgroupDump;
        }
 
-       function docgroupImport() {
+       function docgroupImport($file, $format = 'json') {
+               global $importRefMapping;
                $stream = $this->loadFile($file, $format);
                if (!$stream) {
                        return false;
                }
-               $group_id = $stream['document']['data_array']['group_id'];
+               $group_id = 
$this->getMappingId($stream['data_array']['group_id'], 'group');
                $group = group_get_object($group_id);
                $importSerialIdRef = array();
                if ($group && is_object($group) && !$group->isError() && 
$group->usesDocman()) {
-                       //TODO: do it!
+                       db_begin();
+                       $dg = new DocumentGroup($group);
+                       $parent_doc_group = 
$this->getMappingId($stream['data_array']['parent_doc_group'], 'doc_group');
+                       if (!$dg->create($stream['data_array']['groupname'], 
$parent_doc_group, $stream['data_array']['stateid'], 
$stream['data_array']['createdate'])) {
+                               echo $dg->getErrorMessage()."\n";
+                               db_rollback();
+                               return false;
+                       }
+                       db_commit();
+                       
$importRefMapping['doc_group'][$stream['data_array']['doc_group']] = 
$dg->getID();
+                       echo _('Document Group injected')."\n";
+                       return true;
                } else {
                        echo _('Unable to get project to inject document 
group')._(': ').$group_id."\n";
+                       return false;
                }
        }
 
@@ -605,6 +635,15 @@ class CliActions {
                unset($stream_ori);
                return $stream;
        }
+
+       function getMappingId($idtofind, $objecttofind) {
+               if (isset($importRefMapping[$objecttofind][$idtofind])) {
+                       return $importRefMapping[$objecttofind][$idtofind];
+               } else {
+                       //not found, return the idtofind.
+                       return $idtofind;
+               }
+       }
 }
 
 session_set_admin();

-----------------------------------------------------------------------

Summary of changes:
 src/bin/forge | 71 +++++++++++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 55 insertions(+), 16 deletions(-)


hooks/post-receive
-- 
FusionForge

_______________________________________________
Fusionforge-commits mailing list
[email protected]
http://lists.fusionforge.org/cgi-bin/mailman/listinfo/fusionforge-commits

Reply via email to