making the much needed user profile api
This commit is contained in:
parent
8442b362b7
commit
f76da3335a
3 changed files with 229 additions and 25 deletions
|
|
@ -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<NOTE:> 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;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue