summaryrefslogtreecommitdiffstats
path: root/wizards/source/template/Correspondence.xba
blob: 01da7f3d83e7a5dbf982a396c3ebd91ad4fb447f (plain)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
<!--
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
-->
<script:module xmlns:script="http://openoffice.org/2000/script" script:name="Correspondence" script:language="StarBasic">Option Explicit

Public msgNoTextmark$, msgError$
Public sAddressbook$
Public Table
Public sCompany$, sFirstName$, sLastName$, sStreet$, sPostalCode$, sCity$, sState$, sInitials$, sPosition$
Public DialogExited
Public oDocument, oText, oBookMarks, oBookMark, oBookMarkCursor, oBookText as Object
Public bTemplate, bDBFields as Boolean

Sub Main
	bTemplate = true
    BasicLibraries.LoadLibrary(&quot;Tools&quot;)
	TemplateDialog = LoadDialog(&quot;Template&quot;, &quot;TemplateDialog&quot;)
	DialogModel = TemplateDialog.Model
	DialogModel.Step = 2
	DialogModel.Optmerge.State = True
	LoadLanguageCorrespondence()	
	TemplateDialog.Execute
	TemplateDialog.Dispose()
End Sub


Sub Placeholder
	bTemplate = false
	BasicLibraries.LoadLibrary(&quot;Tools&quot;)
	LoadLanguageCorrespondence()
	bDBFields = false
	OK()
End Sub


Sub Database
	bTemplate = false
	BasicLibraries.LoadLibrary(&quot;Tools&quot;)
	LoadLanguageCorrespondence()
	bDBFields = true
	OK()
End Sub


Function LoadLanguageCorrespondence() as Boolean
	If InitResources(&quot;&apos;Template&apos;&quot;) Then
		msgNoTextmark$ = GetResText(&quot;CorrespondenceDialog_0&quot;) &amp; Chr(13) &amp; Chr(10) &amp; GetResText(&quot;CorrespondenceNoTextmark_1&quot;)
		msgError$ = GetResText(&quot;CorrespondenceMsgError&quot;)
		If bTemplate Then
			DialogModel.Title = GetResText(&quot;CorrespondenceDialog_3&quot;)
			DialogModel.CmdCancel.Label = GetResText(&quot;STYLES_2&quot;)
			DialogModel.CmdCorrGoOn.Label = GetResText(&quot;STYLES_3&quot;)
			DialogModel.OptSingle.Label = GetResText(&quot;CorrespondenceDialog_1&quot;)
			DialogModel.Optmerge.Label = GetResText(&quot;CorrespondenceDialog_2&quot;)
			DialogModel.FrmLetter.Label = GetResText(&quot;CorrespondenceDialog_0&quot;)
		End If
		LoadLanguageCorrespondence() = True
	Else
		msgbox(&quot;Warning: Resource could not be loaded!&quot;)
	End If
End Function


Function GetFieldName(oFieldKnot as Object, GeneralFieldName as String)
	If oFieldKnot.HasByName(GeneralFieldName) Then
    GetFieldName = oFieldKnot.GetByName(GeneralFieldName).AssignedFieldName
	Else
		GetFieldName = &quot;&quot;
	End If
End Function


Sub OK
Dim ParaBreak
Dim sDocLang as String
Dim oSearchDesc as Object
Dim oFoundAll as Object
Dim oFound as Object
Dim sFoundContent as String
Dim sFoundString as String
Dim sDBField as String
Dim i as Integer
Dim oDBAccess as Object
Dim oAddressDialog as Object
Dim oAddressPilot as Object
Dim oFields as Object
Dim oDocSettings as Object
Dim oContext as Object
Dim bDBvalid as Boolean
	&apos;On Local Error Goto GENERALERROR
	
	If bTemplate Then
		bDBFields = DialogModel.Optmerge.State              &apos;database or placeholder
		TemplateDialog.EndExecute()
		DialogExited = TRUE
	End If
	
	If bDBFields Then
		oDBAccess = GetRegistryKeyContent(&quot;org.openoffice.Office.DataAccess/AddressBook/&quot;)
		sAddressbook = oDBAccess.DataSourceName

		bDBvalid = false
		oContext = createUnoService( &quot;com.sun.star.sdb.DatabaseContext&quot; )		

		If (not isNull(oContext)) Then 
			&apos;Is the previously assigned address data source still valid?
			bDBvalid = oContext.hasByName(sAddressbook)
		end if
				
		If (bDBvalid = false) Then			
			oAddressPilot = createUnoService(&quot;com.sun.star.ui.dialogs.AddressBookSourcePilot&quot;)
			oAddressPilot.execute
			
			oDBAccess = GetRegistryKeyContent(&quot;org.openoffice.Office.DataAccess/AddressBook/&quot;)
			sAddressbook = oDBAccess.DataSourceName
			If sAddressbook = &quot;&quot; Then
				MsgBox(GetResText(&quot;CorrespondenceNoTextmark_1&quot;))
				Exit Sub
			End If
		End If
		oFields = oDBAccess.GetByName(&quot;Fields&quot;)
		Table = oDBAccess.GetByName(&quot;Command&quot;)
	End If

	ParaBreak = com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK
  	oDocument = ThisComponent
	If bDBFields Then
		&apos;set the address db as current db at the document
    	oDocSettings = oDocument.createInstance(&quot;com.sun.star.document.Settings&quot;)
		oDocSettings.CurrentDatabaseDataSource = sAddressbook
		oDocSettings.CurrentDatabaseCommand = Table
		oDocSettings.CurrentDatabaseCommandType = 0
	End If
	oBookmarks = oDocument.Bookmarks
	oText = oDocument.Text

	oSearchDesc = oDocument.createsearchDescriptor()
	oSearchDesc.SearchRegularExpression = True
	oSearchDesc.SearchWords = True
	oSearchDesc.SearchString  = &quot;&lt;[^&gt;]+&gt;&quot;
	oFoundall = oDocument.FindAll(oSearchDesc)

	&apos;Loop over the foundings
  	For i = oFoundAll.Count -1 To 0 Step -1
		oFound = oFoundAll.GetByIndex(i)
		sFoundString = oFound.String
		&apos;Extract the string inside the brackets
		sFoundContent = FindPartString(sFoundString,&quot;&lt;&quot;,&quot;&gt;&quot;,1)
		sFoundContent = LTrim(sFoundContent)
		&apos; Define the Cursor and place it on the founding
		oBookmarkCursor = oFound.Text.CreateTextCursorbyRange(oFound)
		oBookText = oFound.Text
		If bDBFields Then
			sDBField = GetFieldname(oFields, sFoundContent)
			If sDBField &lt;&gt; &quot;&quot; Then
				InsertDBField(sAddressbook, Table, sDBField)
			Else
				InsertPlaceholder(sFoundContent)
			End If
		Else
			InsertPlaceholder(sFoundContent)
		End If
	Next i
	If bDBFields Then
		&apos;Open the DB beamer with the right DB
		Dim oDisp as Object
		Dim oTransformer
		Dim aURL as new com.sun.star.util.URL
		aURL.complete = &quot;.component:DB/DataSourceBrowser&quot;
		oTransformer = createUnoService(&quot;com.sun.star.util.URLTransformer&quot;)
		oTransformer.parseStrict(aURL)
		oDisp = oDocument.getCurrentController.getFrame.queryDispatch(aURL, &quot;_beamer&quot;, com.sun.star.frame.FrameSearchFlag.CHILDREN + com.sun.star.frame.FrameSearchFlag.CREATE)
		Dim aArgs(3) as new com.sun.star.beans.PropertyValue
		aArgs(1).Name = &quot;DataSourceName&quot;
		aArgs(1).Value = sAddressbook
		aArgs(2).Name = &quot;CommandType&quot;
		aArgs(2).Value = com.sun.star.sdb.CommandType.TABLE
		aArgs(3).Name = &quot;Command&quot;
		aArgs(3).Value = Table
		oDisp.dispatch(aURL, aArgs())
	End If
	
	GENERALERROR:
	If Err &lt;&gt; 0 Then
		Msgbox(msgError$,16, GetProductName())
		Resume LETSGO
	End If
	LETSGO:

End Sub


Sub InsertDBField(sDBName as String, sTableName as String, sColName as String)
Dim oFieldMaster, oField as Object
	If sColname &lt;&gt; &quot;&quot; Then
		oFieldMaster = oDocument.createInstance(&quot;com.sun.star.text.FieldMaster.Database&quot;)
		oField = oDocument.createInstance(&quot;com.sun.star.text.TextField.Database&quot;)
		oFieldMaster.DataBaseName = sDBName
		oFieldMaster.DataBaseName = sDBName
		oFieldMaster.DataTableName = sTableName
		oFieldMaster.DataColumnName = sColName
		oField.AttachTextfieldmaster (oFieldMaster)
		oBookText.InsertTextContent(oBookMarkCursor, oField, True)
		oField.Content = &quot;&lt;&quot; &amp; sColName &amp; &quot;&gt;&quot;
	End If
End Sub


Sub InsertPlaceholder(sColName as String)
Dim oFieldMaster as Object
Dim bCorrectField as Boolean
	If sColname &lt;&gt; &quot;&quot; Then
		bCorrectField = True
		oFieldMaster = oDocument.createInstance(&quot;com.sun.star.text.TextField.JumpEdit&quot;)
		Select Case sColName
			Case &quot;Company&quot;
				oFieldMaster.PlaceHolder = getResText(&quot;CorrespondenceFields_1&quot;)
			Case &quot;Department&quot;
				oFieldMaster.PlaceHolder = getResText(&quot;CorrespondenceFields_2&quot;)
			Case &quot;FirstName&quot;
				oFieldMaster.PlaceHolder = getResText(&quot;CorrespondenceFields_3&quot;)
			Case &quot;LastName&quot;
				oFieldMaster.PlaceHolder = getResText(&quot;CorrespondenceFields_4&quot;)
			Case &quot;Street&quot;
				oFieldMaster.PlaceHolder = getResText(&quot;CorrespondenceFields_5&quot;)
			Case &quot;Country&quot;
				oFieldMaster.PlaceHolder = getResText(&quot;CorrespondenceFields_6&quot;)
			Case &quot;Zip&quot;
				oFieldMaster.PlaceHolder = getResText(&quot;CorrespondenceFields_7&quot;)
			Case &quot;City&quot;
				oFieldMaster.PlaceHolder = getResText(&quot;CorrespondenceFields_8&quot;)
			Case &quot;Title&quot;
				oFieldMaster.PlaceHolder = getResText(&quot;CorrespondenceFields_9&quot;)
			Case &quot;Position&quot;
				oFieldMaster.PlaceHolder = getResText(&quot;CorrespondenceFields_10&quot;)
			Case &quot;AddrForm&quot;
				oFieldMaster.PlaceHolder = getResText(&quot;CorrespondenceFields_11&quot;)
			Case &quot;Code&quot;
				oFieldMaster.PlaceHolder = getResText(&quot;CorrespondenceFields_12&quot;)
			Case &quot;AddrFormMail&quot;
				oFieldMaster.PlaceHolder = getResText(&quot;CorrespondenceFields_13&quot;)
			Case &quot;PhonePriv&quot;
				oFieldMaster.PlaceHolder = getResText(&quot;CorrespondenceFields_14&quot;)
			Case &quot;PhoneComp&quot;
				oFieldMaster.PlaceHolder = getResText(&quot;CorrespondenceFields_15&quot;)
			Case &quot;Fax&quot;
				oFieldMaster.PlaceHolder = getResText(&quot;CorrespondenceFields_16&quot;)
			Case &quot;EMail&quot;
				oFieldMaster.PlaceHolder = getResText(&quot;CorrespondenceFields_17&quot;)
			Case &quot;URL&quot;
				oFieldMaster.PlaceHolder = getResText(&quot;CorrespondenceFields_18&quot;)
			Case &quot;Note&quot;
				oFieldMaster.PlaceHolder = getResText(&quot;CorrespondenceFields_19&quot;)
			Case &quot;Altfield1&quot;
				oFieldMaster.PlaceHolder = getResText(&quot;CorrespondenceFields_20&quot;)
			Case &quot;Altfield2&quot;
				oFieldMaster.PlaceHolder = getResText(&quot;CorrespondenceFields_21&quot;)
			Case &quot;Altfield3&quot;
				oFieldMaster.PlaceHolder = getResText(&quot;CorrespondenceFields_22&quot;)
			Case &quot;Altfield4&quot;
				oFieldMaster.PlaceHolder = getResText(&quot;CorrespondenceFields_23&quot;)
			Case &quot;Id&quot;
				oFieldMaster.PlaceHolder = getResText(&quot;CorrespondenceFields_24&quot;)
			Case &quot;State&quot;
				oFieldMaster.PlaceHolder = getResText(&quot;CorrespondenceFields_25&quot;)
			Case &quot;PhoneOffice&quot;
				oFieldMaster.PlaceHolder = getResText(&quot;CorrespondenceFields_26&quot;)
			Case &quot;Pager&quot;
				oFieldMaster.PlaceHolder = getResText(&quot;CorrespondenceFields_27&quot;)
			Case &quot;PhoneCell&quot;
				oFieldMaster.PlaceHolder = getResText(&quot;CorrespondenceFields_28&quot;)
			Case &quot;PhoneOther&quot;
				oFieldMaster.PlaceHolder = getResText(&quot;CorrespondenceFields_29&quot;)
			Case &quot;CalendarURL&quot;
				oFieldMaster.PlaceHolder = getResText(&quot;CorrespondenceFields_30&quot;)
			Case &quot;InviteParticipant&quot;
				oFieldMaster.PlaceHolder = getResText(&quot;CorrespondenceFields_31&quot;)
			Case Else
				bCorrectField = False
		End Select
		If bCorrectField Then
			oFieldMaster.Hint = getResText(&quot;CorrespondenceFields_0&quot;)
			oBookText.InsertTextContent(oBookMarkCursor, oFieldMaster, True)
		End If
	End If
End Sub
</script:module>