Monthly Archive for September, 2005

List Field Functions

Previously I posted some LotusScript code to handle list fields in Lotus Notes/Domino. Here’s the JavaScript equivalent- because we wanted it to work on the web too. The form for adding an entry had this code…

function AddValue(field, v) {
// appends v to the list field f
	if (!opener.document.getElementById(field)) { return; }
	var newV = opener.document.getElementById(field).value;
 
	if (newV == "") {
		newV = v;
	} else {
		newV += "; " + v;
	}
	return newV;
} // end AddValue

The form to update/edit an entry had this code…

function UpdateValue(field, vindex, newvalue) {
// updates value at vindex with newvalue
 
	var newV = "";
	if (!opener.document.getElementById(field)) { return; }
	if (vindex < 0) { return; }
	var fSplit = opener.document.getElementById(field).value.split("; ")
	if (vindex > fSplit.length) { return; }
 
	for (j = 0; j < fSplit.length; j++) {
		if (j != vindex) {
 
			newV += fSplit[j] + "; ";
		} else {
			newV += newvalue + "; ";
		}
	}
 
	return newV.substring(0, newV.length - 2);
 
} // end UpdateValue

And the code to remove an entry went right on the form will all the list fields…

function RemoveValue(field, vindex) {
// removes the value at vindex
	var newV = "";
	if (!document.getElementById(field)) { return; }
	if (vindex < 0) { return; }
	var fSplit = document.getElementById(field).value.split("; ")
	if (vindex > fSplit.length) { return; }
 
	for (j = 0; j < fSplit.length; j++) {
 
		if (j != vindex) {
			newV += fSplit[j] + "; ";
		}
	}
 
	return newV.substring(0, newV.length - 2);
 
} // end RemoveValue

Here’s a bit of code to show you how I was using it. This will remove the selected entry from the third set of fields (Steps3, etc). An entry exists as a value in each of four fields.

var F = document.forms[0];
var selectedIndex = F.dispSteps3.selectedIndex;
 
if (selectedIndex < 0) {
	alert("Please select a task to remove");
	return false;
}
 
F.Steps3.value = RemoveValue("Steps3", selectedIndex);
F.WorkEffort3.value = RemoveValue("WorkEffort3", selectedIndex);
F.WorkEffortType3.value = RemoveValue("WorkEffortType3", selectedIndex);
F.Assignees3.value = RemoveValue("Assignees3", selectedIndex);
 
F.__Click.value = '$Refresh';
F.submit();

Design Element- Prohibit design refresh or replace

Occassionally we need our clients to upload a copy of their Notes database to us for support. Sometimes we get them and they’ve clicked this pesky “Prohibit design refresh or replace to modify” check box on the design elements. That prevents the changes we’ve made in the templates we send them from taking affect. If it’s just one or two design elements it’s no sweat to unclick that option. But one time I got a db with most of the design elements set that way, and I wasn’t going to go through it manually.

On the notes.net sandbox I found a Notes application that would let you pick another Notes database and it would tell you the names of all the design elements in the database. I did some research and found out about the almighty $Flags field, and what it’s contents meant, specifically the P flag. I copied the existing form in the database I got from notes.net and changed it to only display the design elements that have the P flag. That means it only shows you the design elements with the “Prohibit design refresh or replace to modify” option checked. Very cool. Then I made a copy of that and modified the code to uncheck that option. It not only tells you which design elements have the option checked,
but it removes them, too.

You can download the application here.

Automatic Thumbnails with PHP

For my illustration site, I wanted to display thumbnails. However, I didn’t want to manually create the thumbnails so I wrote PHP code to create them for me. The problem with that was that it was kinda slow. It was going through all of images, making a thumbnail of each, and displaying them- that was pretty slow. I modified it to write the thumbnail to a file, instead. Once that worked, I made the page check if there was a thumbnail file- if there isn’t, create one. It all sped right along then. Here’s the function to create a thumbnail file…

function makeThumb($img) {
 
# makes a thumbnail image file

    $image = ImageCreateFromJPEG("imagedir/$img");
    $ix = ImageSx($image);
    $iy = ImageSy($image);
 
    $tsize = min($ix, $iy) * 0.75;
  # first, extract the square we're going to use
    $ia = min($ix, $iy) - $tsize;
    $ib = $ia * 2;
 
    $tmp = imagecreatetruecolor($tsize, $tsize);
    imagecopyresampled($tmp,$image,0,0,$ia,$ia,$tsize,$tsize,$ib,$ib);
 
  # new resize that square to ?x?
    $t = imagecreatetruecolor(25, 25);
    imagecopyresampled($t,$tmp,0,0,0,0,25,25,$tsize,$tsize);
 
  # and write to a file
    $ifile = fopen("thumbnaildir/$img", "w");
    ImageJPEG($t, "thumbnaildir/$img");
 
    fclose($ifile);
 
  } # end of makeThumb

And here’s the code that uses it…

if (!file_exists("thumbnaildir/". $ximage)) {
  makeThumb($ximage);
}
 
# display thumbnail, since we know it's there now

Popup Select from View

I had a situation where each field had a button next to it that allowed the user to pick a value from a view. The value went into the field. Rather than repeat the code for the button a half dozen times, I wrote a generic function for it.

Function popup(field As String, view As String) As String
	On Error Goto Oops
 
	Dim w As New NotesUIWorkspace
	Dim db As NotesDatabase
 
	popup = ""
	Set db = w.CurrentDatabase.Database
 
 
	title$ = "Select a " & field
	prompt$ = "Please select a " & field
 
	f = w.PickListStrings(PICKLIST_CUSTOM, False, db.Server, db.FilePath, view, title$, prompt$, 1)
	If (Isempty(f)) Then Exit Function
 
	popup = Cstr(f(0))
 
	Exit Function
 
 
Oops:
	Msgbox Error$ & " on line #" & Cstr(Erl)
	Exit Function
 
End Function

And here’s how I used it.

Dim w As New NotesUIWorkspace
Call w.CurrentDocument.FieldSetText("CallerOrgCode", popup("Org Code", "COCNUMV"))

List Field Functions

Here’s the situation. We have sets of fields which represent items. For example, for each task on a form you have a task name, an amount of work effort, whether the work effort is hours or days, and a bunch of people who are assigned to this task. The way we do it here, we have four fields, each one accepts multiple values. So TaskName(3) + WorkEffort(3) + WorkEffortType(3) + Assignees(3) all make up the fourth (count from 0) task on this form. And so on.

Now, the hard part is adding, removing, and updating to these fields. You need to make sure it’s all done correctly because if you screw up one of the lists the whole thing is … well, screwed. The code to handle this appears over and over again throughout an application and we could just have one script library for it. So I wrote one.

Function AddValue(f As NotesItem, v As Variant) As Variant
On Error Goto ThatIsNotARealRobotMove
'v is the value to append to the list
AddValue = ""
 
' some error checking and whatnot
If (f Is Nothing) Then Exit Function
 
AddValue = Arrayappend(f.Values, v)
Exit Function
 
ThatIsNotARealRobotMove:
Msgbox "AddValue line #" &amp; Cstr(Erl) &amp; ": " &amp; Error$
Exit Function
 
End Function
 
Function UpdateValue(f As NotesItem, v As Integer, newv As Variant) As Variant
On Error Goto ThatIsNotARealRobotMove
'v is the INDEX of the value to be updated
'newv is the new value
 
UpdateValue = ""
 
' some error checking and whatnot
If (f Is Nothing) Then Exit Function
If (v &lt; 0) Then Exit Function
If (Cstr(newv) = "") Then Exit Function
fV = Ubound(f.Values)
If (v &gt; fV) Then Exit Function
 
Dim newf () As Variant
Redim newf (fV)
 
For i% = 0 To fV
  If (i% &lt;&gt; v) Then
    ewf(i%) = f.Values(i%)
  Else
    newf(i%) = newv
  End If
Next
 
UpdateValue = newf
 
Exit Function
 
ThatIsNotARealRobotMove:
Msgbox "UpdateValue line #" &amp; Cstr(Erl) &amp; ": " &amp; Error$
Exit Function
 
End Function
 
Function RemoveValue(f As NotesItem, v As Integer) As Variant
On Error Goto ThatIsNotARealRobotMove
'v is the INDEX of the value to be removed
 
RemoveValue = ""
 
' some error checking and whatnot
If (f Is Nothing) Then Exit Function
If (v &lt; 0) Then Exit Function
fV = Ubound(f.Values)
If (v &gt; fV) Then Exit Function
 
Dim newf () As Variant
Redim newf (fV - 1)
j% = 0
For i% = 0 To fV
  If (i% &lt;&gt; v) Then
    newf(j%) = f.Values(i%)
    j% = j% + 1
  End If
Next
 
RemoveValue = newf
 
Exit Function
 
ThatIsNotARealRobotMove:
Msgbox "RemoveValue line #" &amp; Cstr(Erl) &amp; ": " &amp; Error$
Exit Function
 
End Function
 
Function FindIndex(f As NotesItem, v As Variant) As Integer
' will return the index of v in f.Values
 
pos = Arraygetindex(f.Values, v)
If (Isnull(pos)) Then
  FindIndex = -1
Else
  FindIndex = Cint(pos)
End If
 
End Function

Here’s an example of how I used it.

' use a dialog box to gather information
d.Steps1 = AddValue(d.GetFirstItem("Steps1"), popupd.tmpTaskName(0))
d.WorkEffort1 = AddValue(d.GetFirstItem("WorkEffort1"), Cstr(popupd.tmpWE(0)))
d.WorkEffortType1 = AddValue(d.GetFirstItem("WorkEffortType1"), popupd.tmpWET(0))
d.Assignees1 = AddValue(d.GetFirstItem("Assignees1"), assignees$)
' refresh the document