From f76da3335a2908e10de2688566950497d6f29e47 Mon Sep 17 00:00:00 2001 From: JT Smith Date: Wed, 7 Dec 2005 06:05:23 +0000 Subject: [PATCH] making the much needed user profile api --- lib/WebGUI/ProfileCategory.pm | 70 +++++++++++++--- lib/WebGUI/ProfileField.pm | 154 ++++++++++++++++++++++++++++++---- lib/WebGUI/SQL.pm | 30 +++++++ 3 files changed, 229 insertions(+), 25 deletions(-) diff --git a/lib/WebGUI/ProfileCategory.pm b/lib/WebGUI/ProfileCategory.pm index 749378f52..cf721e25c 100644 --- a/lib/WebGUI/ProfileCategory.pm +++ b/lib/WebGUI/ProfileCategory.pm @@ -16,8 +16,9 @@ package WebGUI::ProfileCategory; =cut use strict; -use WebGUI::Id; +use WebGUI::ProfileField; use WebGUI::Session; +use WebGUI::SQL; =head1 NAME @@ -38,6 +39,17 @@ These methods are available from this package: =cut +#------------------------------------------------------------------- +sub _reorderCategories { + my ($sth, $i, $id); + $sth = WebGUI::SQL->read("select profileCategoryId from userProfileCategory order by sequenceNumber"); + while (($id) = $sth->array) { + $i++; + WebGUI::SQL->write("update userProfileCategory set sequenceNumber='$i' where profileCategoryId=".quote($id)); + } + $sth->finish; +} + #------------------------------------------------------------------- =head2 create ( [ properties] ) @@ -53,9 +65,8 @@ A hash reference containing the properties of this field. See the set() method f sub create { my $class = shift; my $properties = shift; - my $id = WebGUI::Id::generate(); my ($sequenceNumber) = WebGUI::SQL->quickArray("select max(sequenceNumber) from userProfileCategory"); - WebGUI::SQL->write("insert into userProfileCategory (profileCategoryId,sequenceNumber) values (".quote($id).", ".($sequenceNumber+1).")"); + my $id = WebGUI::SQL->setRow("userProfileCategory","profileCategoryId",{profileCategoryId=>"new", sequenceNumber=>$sequenceNumber+1}); my $self = $class->new($id); $self->update($properties); return $self; @@ -74,7 +85,7 @@ sub delete { foreach my $field (@{$self->getFields}) { $field->delete; } - WebGUI::SQL->write("delete from userProfileCategory where profileCategoryId=".quote($self->getId)); + WebGUI::SQL->deleteRow("userProfileCategory","profileCategoryId",$self->getId); } #------------------------------------------------------------------- @@ -107,7 +118,12 @@ Returns an array reference of all WebGUI::ProfileCategory objects in order of se =cut sub getCategories { - + my $self = shift; + my @categories = (); + foreach my $id (WebGUI::SQL->buildArray("select profileCategoryId from userProfileCategory order by sequenceNumber")) { + push(@categories,WebGUI::ProfileCategory->new($id)); + } + return \@categories; } @@ -120,7 +136,12 @@ Returns an array reference of all WebGUI::ProfileField objects that are part of =cut sub getFields { - + my $self = shift; + my @fields = (); + foreach my $fieldName (WebGUI::SQL->buildArray("select fieldName from userProfileField where profileCategoryId=".quote($self->getId)." order by sequenceNumber")){ + push(@fields,WebGUI::ProfileField->new($fieldName)); + } + return \@fields; } #------------------------------------------------------------------- @@ -133,7 +154,7 @@ Returns the unique ID for this category. sub getId { my $self = shift; - return $self->{_id}; + return $self->get("profileCategoryId"); } #------------------------------------------------------------------- @@ -145,7 +166,15 @@ Moves this category down one position. =cut sub moveDown { - + my $self = shift; + my ($id, $thisSeq); + ($thisSeq) = WebGUI::SQL->quickArray("select sequenceNumber from userProfileCategory where profileCategoryId=".quote($self->getId)); + ($id) = WebGUI::SQL->quickArray("select profileCategoryId from userProfileCategory where sequenceNumber=$thisSeq+1"); + if ($id ne "") { + WebGUI::SQL->write("update userProfileCategory set sequenceNumber=sequenceNumber+1 where profileCategoryId=".quote($self->getId)); + WebGUI::SQL->write("update userProfileCategory set sequenceNumber=sequenceNumber-1 where profileCategoryId=".quote($id)); + _reorderCategories(); + } } #------------------------------------------------------------------- @@ -157,7 +186,15 @@ Moves this field up one position. =cut sub moveUp { - + my $self = shift; + my ($id, $thisSeq); + ($thisSeq) = WebGUI::SQL->quickArray("select sequenceNumber from userProfileCategory where profileCategoryId=".quote($self->getId)); + ($id) = WebGUI::SQL->quickArray("select profileCategoryId from userProfileCategory where sequenceNumber=$thisSeq-1"); + if ($id ne "") { + WebGUI::SQL->write("update userProfileCategory set sequenceNumber=sequenceNumber-1 where profileCategoryId=".quote($self->getId)); + WebGUI::SQL->write("update userProfileCategory set sequenceNumber=sequenceNumber+1 where profileCategoryId=".quote($id)); + _reorderCategories(); + } } #------------------------------------------------------------------- @@ -174,6 +211,10 @@ The unique id of this category. sub new { my $class = shift; + my $id = shift; + return undef unless ($id); + my $properties = WebGUI::SQL->getRow("userProfileCategory","profileCategoryId",$id); + bless {_properties=>$properties}, $class; } @@ -193,14 +234,21 @@ A perl structure that will return a scalar. Defaults to 'Undefined'. =head4 visible -A boolean indicating whether this field should be visible when a user views a user's profile. +A boolean indicating whether the fields in this category should be visible when a user views a user's profile. =head4 editable +A boolean indicating whether the user can edit the fields under this category. + =cut sub set { - + my $self = shift; + my $properties = shift; + $properties->{visible} = 0 unless ($properties->{visible} == 1); + $properties->{editable} = 0 unless ($properties->{editable} == 1); + $properties->{label} = 'Undefined' if ($properties->{label} =~ /^[\"\']*$/); + WebGUI::SQL->setRow("userProfileCategory","profileCategoryId",$properties); } diff --git a/lib/WebGUI/ProfileField.pm b/lib/WebGUI/ProfileField.pm index 33ab0d840..721243050 100644 --- a/lib/WebGUI/ProfileField.pm +++ b/lib/WebGUI/ProfileField.pm @@ -16,7 +16,11 @@ package WebGUI::ProfileField; =cut use strict; +use WebGUI::ProfileCategory; use WebGUI::Session; +use WebGUI::SQL; +use WebGUI::Form; +use WebGUI::FormProcessor; =head1 NAME @@ -38,8 +42,20 @@ These methods are available from this package: =cut #------------------------------------------------------------------- +sub _reorderFields { + my $category = shift; + my ($sth, $i, $id); + $sth = WebGUI::SQL->read("select fieldName from userProfileField where profileCategoryId=".quote($category)." order by sequenceNumber"); + while (($id) = $sth->array) { + $i++; + WebGUI::SQL->write("update userProfileField set sequenceNumber='$i' where fieldName=".quote($id)); + } + $sth->finish; +} -=head2 create ( fieldName [, properties] ) +#------------------------------------------------------------------- + +=head2 create ( fieldName [, properties, categoryId] ) Add a new field to the system. Returns a WebGUI::ProfileField object if created successfully, otherwise returns undef. @@ -51,10 +67,24 @@ The unique name of this field. A hash reference containing the properties of this field. See the set() method for details. +=head3 categoryId + +The unique id of the category to assign this field to. Defaults to "1" (misc). + =cut sub create { - + my $class = shift; + my $fieldName = shift; + my $properties = shift; + my $categoryId = shift; + my ($fieldName) = WebGUI::SQL->quickArray("select count(*) from userProfileField where fieldName=".quote($fieldName)); + return undef if ($fieldName); + my $id = WebGUI::SQL->setRow("userProfileField","fieldName",{fieldName=>"new"},undef,$fieldName); + my $self = $class->new($id); + $self->setCategory($categoryId || "1"); + $self->update($properties); + return $self; } #------------------------------------------------------------------- @@ -66,7 +96,9 @@ Deletes this field and all user data attached to it. =cut sub delete { - + my $self = shift; + WebGUI::SQL->write("delete from userProfileData where fieldName=".quote($self->getId)); + WebGUI::SQL->deleteRow("userProfileField","fieldName",$self->getId); } #------------------------------------------------------------------- @@ -124,10 +156,26 @@ Returns a WebGUI::ProfileCategory object for the category that this profile fiel =cut sub getCategory { - + my $self = shift; + return WebGUI::ProfileCategory->new($self->get("categoryId")); } +#------------------------------------------------------------------- + +=head2 getId () + +Returns the unique fieldName for this field. + +B This method is named getId for consistency amongst other packages even though technically profile fields have field names rather than ids. + +=cut + +sub getId { + my $self = shift; + return $self->get("fieldName"); +} + #------------------------------------------------------------------- =head2 moveDown () @@ -137,7 +185,15 @@ Moves this field down one position within it's category. =cut sub moveDown { - + my $self = shift; + my ($id, $thisSeq, $profileCategoryId); + ($thisSeq,$profileCategoryId) = WebGUI::SQL->quickArray("select sequenceNumber,profileCategoryId from userProfileField where fieldName=".quote($self->getId)); + ($id) = WebGUI::SQL->quickArray("select fieldName from userProfileField where profileCategoryId=".quote($profileCategoryId)." and sequenceNumber=$thisSeq+1"); + if ($id ne "") { + WebGUI::SQL->write("update userProfileField set sequenceNumber=sequenceNumber+1 where fieldName=".quote($self->getId)); + WebGUI::SQL->write("update userProfileField set sequenceNumber=sequenceNumber-1 where fieldName=".quote($id)); + _reorderFields($profileCategoryId); + } } #------------------------------------------------------------------- @@ -149,7 +205,15 @@ Moves this field up one position within it's category. =cut sub moveUp { - + my $self = shift; + my ($id, $thisSeq, $profileCategoryId); + ($thisSeq,$profileCategoryId) = WebGUI::SQL->quickArray("select sequenceNumber,profileCategoryId from userProfileField where fieldName=".quote($self->getId)); + ($id) = WebGUI::SQL->quickArray("select fieldName from userProfileField where profileCategoryId=".quote($profileCategoryId)." and sequenceNumber=$thisSeq-1"); + if ($id ne "") { + WebGUI::SQL->write("update userProfileField set sequenceNumber=sequenceNumber-1 where fieldName=".quote($self->getId)); + WebGUI::SQL->write("update userProfileField set sequenceNumber=sequenceNumber+1 where fieldName=".quote($id)); + _reorderFields($profileCategoryId); + } } @@ -166,9 +230,11 @@ The unique name of this field. =cut sub new { - my $class = shift; - my $fieldName = shift; - bless {_fieldName=>$fieldName}, $class; + my $class = shift; + my $id = shift; + return undef unless ($id); + my $properties = WebGUI::SQL->getRow("userProfileField","fieldName",$id); + bless {_properties=>$properties}, $class; } #------------------------------------------------------------------- @@ -184,7 +250,14 @@ The new name this field should take. =cut sub rename { - + my $self = shift; + my $newName = shift; + my ($fieldNameExists) = WebGUI::SQL->quickArray("select count(*) from userProfileField where fieldName=".quote($newName)); + return 0 if ($fieldNameExists); + WebGUI::SQL->write("update userProfileData set fieldName=".quote($newName)." where fieldName=".quote($self->getId)); + WebGUI::SQL->write("update userProfileField set fieldName=".quote($newName)." where fieldName=".quote($self->getId)); + $self->{_properties}{fieldName} = $newName; + return 1; } @@ -204,28 +277,81 @@ A perl structure that will return a scalar. Defaults to 'Undefined'. =head4 visible -A boolean indicating whether this field should be visible when a user views a user's profile. +A boolean indicating whether this field should be visible when a user views a user's profile. Defaults to 0. =head4 required +A boolean indicating whether the user must fill out this field in order to create/update his account. Defaults to 0. + =head4 protected +A boolean indicating whether this field may be deleted or not. Defaults to 0. + =head4 editable +A boolean indicating whether this field is editable by the user or not. Defaults to 0. + =head4 fieldType +A scalar indicating the type of field this will be when generated as a form element. Defaults to 'text'. + =head4 possibleValues +A scalar containing a hash reference declaration of possible values. Only used for list type fields. + =head4 defaultValues -=head4 categoryId +A scalar containing an array reference or scalar declaration of defaultly selected value(s). =cut -sub update { - +sub set { + my $self = shift; + my $properties = shift; + $properties->{visible} = 0 unless ($properties->{visible} == 1); + $properties->{editable} = 0 unless ($properties->{editable} == 1); + $properties->{protected} = 0 unless ($properties->{protected} == 1); + $properties->{required} = 0 unless ($properties->{required} == 1); + $properties->{label} = 'Undefined' if ($properties->{label} =~ /^[\"\']*$/); + $properties->{fieldType} = 'text' unless ($properties->{fieldType}); + if ($properties->{defaultValues} && $properties->{fieldType}=~/List$/) { + unless ($properties->{defaultValues} =~ /^\[/) { + $properties->{defaultValues} = "[".$properties->{defaultValues}; + } + unless ($properties->{defaultValues} =~ /\]$/) { + $properties->{defaultValues} .= "]"; + } + } + WebGUI::SQL->setRow("userProfileCategory","profileCategoryId",$properties); + foreach my $key (keys %{$properties}) { + $self->{_property}{$key} = $properties->{$key}; + } } +#------------------------------------------------------------------- + +=head2 setCategory ( id ) + +Assigns this field to a new category. + +=head3 id + +The unique ID of a category to assign this field to. + +=cut + +sub setCategory { + my $self = shift; + my $categoryId = shift; + return undef unless ($categoryId); + my $currentCategoryId = $self->get("profileCategoryId"); + my ($sequenceNumber) = WebGUI::SQL->quickArray("select max(sequenceNumber) from userProfileField where profileCategoryId=".quote($categoryId)); + WebGUI::SQL->setRow("userProfileField","fieldName",{fieldName=>$self->getId, profileCategoryId=>$categoryId, sequenceNumber=>$sequenceNumber+1}); + $self->{_property}{profileCategoryId} = $categoryId; + $self->{_property}{sequenceNumber} = $sequenceNumber+1; + _reorderFields($currentCategoryId) if ($currentCategoryId); + _reorderFields($categoryId); +} 1; diff --git a/lib/WebGUI/SQL.pm b/lib/WebGUI/SQL.pm index a284ee903..85e7c69c1 100644 --- a/lib/WebGUI/SQL.pm +++ b/lib/WebGUI/SQL.pm @@ -237,6 +237,36 @@ sub commit { } +#------------------------------------------------------------------- + +=head2 deleteRow ( table, key, keyValue [, dbh ] ) + +Deletes a row of data from the specified table. + +=head3 table + +The name of the table to delete the row of data from. + +=head3 key + +The name of the column to use as the key. Should be a primary or unique key in the table. + +=head3 keyValue + +The value to search for in the key column. + +=head3 dbh + +A database handler to use. Defaults to the WebGUI database handler. + +=cut + +sub deleteRow { + my ($self, $table, $key, $keyValue, $dbh) = @_; + WebGUI::SQL->write("delete from $table where ".$key."=".quote($keyValue), $dbh); +} + + #------------------------------------------------------------------- =head2 errorCode {