Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
I
intranet-csv-import
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
project-open
intranet-csv-import
Commits
fbfe42c1
Commit
fbfe42c1
authored
Mar 22, 2017
by
Frank Bergmann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
-- import relationships
parent
35638c9b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
230 additions
and
0 deletions
+230
-0
import-rels.tcl
www/import-rels.tcl
+230
-0
No files found.
www/import-rels.tcl
0 → 100755
View file @
fbfe42c1
# /packages/intranet-csv-import/www/import-rels.tcl
#
ad_page_contract
{
Starts the analysis process for the file imported
@author frank.bergmann@project-open.com
@param mapping_name: Should we store the current mapping in the DB for future use?
@param column: Name of the CSV column
@param map: Name of the ]po
[
object attribute
@param parser: Converter for CSV data type ->
]
po
[
data type
}
{
{
return_url
""
}
{
upload_file
""
}
{
import_filename
""
}
{
mapping_name
""
}
{
ns_write_p 1
}
column:array
map:array
parser:array
parser_args:array
}
# ---------------------------------------------------------------------
# Default & Security
# ---------------------------------------------------------------------
set
current_user_id
[
auth::require_login
]
set
page_title
[
lang::message::lookup
""
intranet-cvs-import.Upload_Objects
"Upload Objects"
]
set
context_bar
[
im_context_bar
""
$page
_title
]
set
admin_p
[
im_is_user_site_wide_or_intranet_admin
$current
_user_id
]
if
{
!$admin_p
}
{
ad_return_complaint 1
"Only administrators have the right to import objects"
ad_script_abort
}
# ---------------------------------------------------------------------
# Check and open the file
# ---------------------------------------------------------------------
if
{
!
[
file
readable
$import
_filename
]}
{
ad_return_complaint 1
"Unable to read the file '
$import
_filename'. <br>
Please check the file permissions or contact your system administrator.
\n
"
ad_script_abort
}
set
encoding
"utf-8"
if
{[
catch
{
set fl
[
open
$import
_filename
]
fconfigure
$fl
-encoding
$encoding
set lines_content
[
read
$fl
]
close
$fl
}
err
]}
{
ad_return_complaint 1
"Unable to open file
$import
_filename:<br><pre>
\n
$err
</pre>"
ad_script_abort
}
# Extract the header line from the file
set
lines
[
split
$lines
_content
"
\n
"
]
set
separator
[
im_csv_guess_separator
$lines
]
set
lines_len
[
llength
$lines
]
set
header
[
lindex
$lines
0
]
set
header_fields
[
im_csv_split
$header
$separator
]
set
header_len
[
llength
$header
_fields
]
set
values_list_of_lists
[
im_csv_get_values
$lines
_content
$separator
]
# ------------------------------------------------------------
# Get DynFields
# Determine the list of actually available fields.
set
mapped_vars
[
list
"''"
]
foreach
k
[
array
names map
]
{
lappend mapped_vars
"'
$map
(
$k
)'"
}
# ------------------------------------------------------------
# Render Result Header
if
{
$ns
_write_p
}
{
ad_return_top_of_page
"
[
im_header
]
[
im_navbar
]
"
ns_write
"<h1>Importing Relationships</h1>"
}
# ------------------------------------------------------------
set
cnt 1
foreach
csv_line_fields
$values
_list_of_lists
{
incr cnt
if
{
$ns
_write_p
}
{
ns_write
"</ul><hr><ul>
\n
"
}
if
{
$ns
_write_p
}
{
ns_write
"<li>Starting to parse line
$cnt
\n
"
}
# Preset values, defined by CSV sheet:
set object_name_one
""
set object_name_two
""
set object_id_one
""
set object_id_two
""
set rel_id
""
set percentage
""
# -------------------------------------------------------
# Extract variables from the CSV file
#
set var_name_list
[
list
]
for
{
set
j 0
}
{
$j
<
$header
_len
}
{
incr
j
}
{
set var_name
[
string
trim
[
lindex
$header
_fields
$j
]]
if
{
""
==
$var
_name
}
{
# No variable name - probably an empty column
continue
}
set var_name
[
string
tolower
$var
_name
]
set var_name
[
string
map -nocase
{
" "
"_"
"
\"
"
""
"'"
""
"/"
"_"
"-"
"_"
"
\[
"
"("
"
\{
"
"("
"
\}
"
")"
"
\]
"
")"
}
$var
_name
]
lappend var_name_list
$var
_name
ns_log notice
"import-rels: varname(
[
lindex
$header
_fields
$j
]
) =
$var
_name"
set var_value
[
string
trim
[
lindex
$csv
_line_fields
$j
]]
set var_value
[
string
map -nocase
{
"
\"
"
"'"
"
\[
"
"("
"
\{
"
"("
"
\}
"
")"
"
\]
"
")"
"
\$
"
""
"
\\
"
""
}
$var
_value
]
if
{
"NULL"
eq
$var
_value
}
{
set var_value
""
}
# replace unicode characters by non-accented characters
# Watch out! Does not work with Latin-1 characters
set var_name
[
im_mangle_unicode_accents
$var
_name
]
set cmd
"set
$var
_name
\"
$var
_value
\"
"
ns_log Notice
"import-rels: cmd=
$cmd
"
set result
[
eval
$cmd
]
}
# -------------------------------------------------------
# Transform the variables
set i 0
foreach varname
$var
_name_list
{
set p
$parser
(
$i
)
set p_args
$parser
_args
(
$i
)
set target_varname
$map
(
$i
)
ns_log Notice
"import-rels: Parser:
$varname
->
$target
_varname"
ns_log Notice
"import-rels: Parser: p_args:
$p
_args"
ns_log Notice
"import-rels: Parser: target_varname:
$target
_varname"
switch
$p
{
no_change
{
set
$target
_varname
[
set
$varname
]
ns_log Notice
"import-rels: Parser: no change: varname:
$varname
, val:
[
set
$varname
]
"
}
default
{
set proc_name
"im_csv_import_parser_
$p
"
if
{[
catch
{
set val
[
set
$varname
]
ns_log Notice
"import-rels: Parser: val:
$val
"
if
{
""
!=
$val
}
{
set result
[
$proc
_name -parser_args
$p
_args
$val
]
set res
[
lindex
$result
0
]
set err
[
lindex
$result
1
]
ns_log Notice
"import-rels: Parser: '
$p
-args
$p
_args
$val
' ->
$target
_varname=
$res
, err=
$err
"
if
{
""
!=
$err
}
{
if
{
$ns
_write_p
}
{
ns_write
"<li><font color=brown>Warning: Error parsing field='
$target
_varname' using parser '
$p
':<pre>
$err
</pre></font>
\n
"
}
}
set
$target
_varname
$res
}
}
err_msg
]}
{
if
{
$ns
_write_p
}
{
ns_write
"<li><font color=brown>Warning: Error parsing field='
$target
_varname' using parser '
$p
':<pre>
$err
_msg</pre></font>"
}
}
}
}
incr i
ns_log Notice
"import-rels: -------------------------------------------"
}
# -------------------------------------------------------
# Specific field transformations
if
{
"person"
==
$object
_type_one &&
"im_project"
==
$object
_type_two
}
{
if
{
""
eq
$object
_id_one ||
""
eq
$object
_id_two
}
{
if
{
$ns
_write_p
}
{
ns_write
"<li><font color='red'>Error: Record not imported - person or project/task not found</font>
\n
"
continue
}
}
if
{[
catch
{
set rel_id
[
db_string create_im_biz_object_member
"
select im_biz_object_member__new (
null,
'im_biz_object_member',
:object_id_two,
:object_id_one,
:role_id,
:percentage::numeric,
null,
'0.0.0.0'
);
"
]
ns_write
"<li><font color=green>Created relationship (id:
$rel
_id) btw. '
$object
_id_one' and '
$object
_id_two'</font>
\n
"
}
err_msg
]}
{
global errorInfo
if
{
$ns
_write_p
}
{
ns_write
"<li><font color=red>Error: Error creating relationship:<pre>
$error
Info</pre></font>
\n
"
}
continue
}
}
}
if
{
$ns
_write_p
}
{
ns_write
"</ul>
\n
"
ns_write
"<p>
\n
"
ns_write
"<A HREF=
$return
_url>Return to Project Page</A>
\n
"
}
# ------------------------------------------------------------
# Render Report Footer
if
{
$ns
_write_p
}
{
ns_write
[
im_footer
]
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment