File: import_docman.php

package info (click to toggle)
fusionforge 5.3.2%2B20141104-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 60,472 kB
  • sloc: php: 271,846; sql: 36,817; python: 14,575; perl: 6,406; sh: 5,980; xml: 4,294; pascal: 1,411; makefile: 911; cpp: 52; awk: 27
file content (154 lines) | stat: -rw-r--r-- 5,067 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php

require_once '../env.inc.php';
require_once $gfwww.'include/pre.php';
require_once $gfcommon.'docman/DocumentGroup.class.php';
require_once $gfcommon.'docman/DocumentGroupFactory.class.php';
require_once $gfcommon.'docman/Document.class.php';

class Docman {

	public $doc_group_ids = array();

	var $group;

	var $group_id;

	public $language_ids = array("Bulgarian" => 20, "Catalan" => 14, "Dutch" => 12, "English" => 1, "Esperanto" =>13, "French" =>7, "Greek" => 19, "German" =>6, "Hebrew" =>3,
					"Indonesian" =>21, "Italian" => 8, "Japanese" =>2, "Korean" => 22, "Latin" => 25, "Norwegian" =>9, "Polish" =>15, "Portuguese" =>18, "Pt. Brazilian" =>16, "Russian" => 17,
					"Smpl.Chinese" =>23, "Spanish" =>4, "Swedish" =>10, "Thai" =>5, "Trad.Chinese" =>11);

	public $docman_states = array("active"=>1, "deleted"=>2, "pending"=>3, "hidden"=>4, "private"=>5);

    function __construct($docman, $group_id) {
        $this->docman = $docman;
        $this->group_id = $group_id;
    	$group = group_get_object($group_id);
		if (!$group || !is_object($group)) {
			print "error retrieving group from id";
		} elseif ($group->isError()) {
			print "error";
		}
		$this->group = $group;
    }

    function checkDocGrpExistence($doc_group_name,$parent_doc_group_id){
    	if ($id = $this->doc_group_ids[$parent_doc_group_id][$doc_group_name]){
    		//this name already exists in the directory of id $parent_doc_group_id
    		return $id;
    	}
    	return false;
    }

    function addFile($params, $parent_dir_id, $status="active"){
    	//nothing for now
    	echo "Adding file:".$params["given_name"]." at directory:".$parent_dir_id;
    	echo "<br />";
    	$path = '/tmp/'.$params['url'];
		if (is_file($path)){
    		$doc = new Document($this->group);

    		$fn = $params["file_name"];
    		$ftitle = $params["given_name"];
			$fdata = file_get_contents($path);
			$fdocgrp = $parent_dir_id;
			$flanguage = $this->language_ids[$params["language"]];
			$fdesc = $params["description"];
			$finfo = new finfo(FILEINFO_MIME, "/usr/share/misc/magic"); // Retourne le type mime
			if (!$finfo) {
	   			echo "error opening fileinfo";
	    		exit();
			}
			$ftype = $finfo->file($path);

			$doc->create($fn,$ftype,$fdata,$fdocgrp,$ftitle,$flanguage,$fdesc);

			//now update state
			$fstate_id = $this->docman_states[$status];
			$doc->update($fn, $ftype, $fdata, $fdocgrp, $ftitle, $flanguage, $fdesc,$fstate_id);
		}
    }

    function addDirectory($dirName, $parent_dir_id){
    	if(!$parent_dir_id){
    		//root of the current type
    		echo "ROOT:".$dirName;
    		echo "<br />";
    		//is dir already existing?
    		if(!$dirid = $this->checkDocGrpExistence($dirName, $parent_dir_id)){
    			//dir does not exist : create
    			$doc_group = new DocumentGroup($this->group);
    			$doc_group->create($dirName);
    			$dirid = $doc_group->getID();
    			$this->doc_group_ids[$parent_dir_id][$dirName] = $dirid;
    		} else {
    			//dir exists : return its id
    		}

    	} else {
    		echo $parent_dir.":".$dirName;
    		echo "<br />";
    		//is dir already existing?
    		if(!$dirid = $this->checkDocGrpExistence($dirName, $parent_dir_id)){
    			//dir does not exist : create
    			$doc_group = new DocumentGroup($this->group);
    			$doc_group->create($dirName, $parent_dir_id);
    			$dirid = $doc_group->getID();
    			$this->doc_group_ids[$parent_dir_id][$dirName] = $dirid;
    		} else {
				//dir exists : return its id
    		}
    	}
    	return $dirid;
    }

    function getUncat(){
    	$gr = new DocumentGroupFactory($this->group);
		$dgroups = $gr->getDocumentGroups();
		foreach($dgroups as $dg){
			if($dg->getParentID()==0 && $dg->getName()=='Uncategorized Submissions'){
				return $dg->getID();
			}
		}
		return false;
    }

    function fill_type($content, $status = "", $parent_dir_id = ""){
//    	while (len($content) != 0){
//    		$c = array_pop($content);
		foreach($content as $k => $v){
    		if(array_key_exists("class", $v) && $v["class"] == "FILE"){
    			//$k is a file
    			$this->addFile($v, $parent_dir_id, $status);
    		} else {
    			//$k is a directory
    			if($k!='Uncategorized Submissions'){ //Uncategorized subs is a basic category which should not be duplicated, we need to get its id to add docs to it though
    				$dirid = $this->addDirectory($k, $parent_dir_id);
    			} else {
    				//get Uncategorized Submissions doc_group
    				$dirid = $this->getUncat();
    				if(!$dirid){
    					//error : no Uncategorized subs for this project for unknown reason
    					//create it
    					$dirid = $this->addDirectory($k, $parent_dir_id);
    				}
    			}

    			$this->fill_type($v,$status,$dirid);
    		}
    	}
    }

	function docman_fill(){
		$r1 = db_query_params ('DELETE FROM doc_data WHERE group_id=$1',
					   array ($this->group_id)) ;

		$r2 = db_query_params ('DELETE FROM doc_groups WHERE group_id=$1',
					   array ($this->group_id)) ;

		foreach($this->docman as $status => $content){
			$this->fill_type($content, $status);
		}
	}

}