[vhffs-dev] [1994] reworked Vhffs::Object, maybe everything works |
[ Thread Index |
Date Index
| More vhffs.org/vhffs-dev Archives
]
Revision: 1994
Author: gradator
Date: 2012-02-03 00:26:42 +0100 (Fri, 03 Feb 2012)
Log Message:
-----------
reworked Vhffs::Object, maybe everything works
Modified Paths:
--------------
trunk/vhffs-api/src/Vhffs/Object.pm
Modified: trunk/vhffs-api/src/Vhffs/Object.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Object.pm 2012-02-02 22:43:35 UTC (rev 1993)
+++ trunk/vhffs-api/src/Vhffs/Object.pm 2012-02-02 23:26:42 UTC (rev 1994)
@@ -42,7 +42,7 @@
You can have all the history of an object with a method, get the status
of a service, ...
-This type stored informations about state, history, owner group/user.
+This type store information about state, history, owner group/user.
=head1 METHODS
@@ -59,90 +59,32 @@
use Locale::gettext;
use Encode;
-sub new
-{
- my ( $this , $main , $id , $owner_uid , $owner_gid ) = @_;
-
- my $self = {};
-
- my $class = ref($this) || $this;
- bless($self , $class );
-
- return undef if ( ! defined $main );
-
- $self->{'main'} = $main;
- $self->{'db'} = $main->get_db_object;
- $self->{'owner_uid'} = $owner_uid;
- $self->{'owner_gid'} = $owner_gid;
- $self->{'description'} = '';
-
- if ( defined $id )
- {
- $self->{'object_id'} = $id;
- }
- else
- {
- $self->{'object_id'} = 0;
- }
-
- return $self;
-}
-
sub _new {
- my ($class, $main, $oid, $owner_uid, $owner_gid, $date_creation, $description, $refuse_reason, $state, $type) = @_;
-
- my $self = {};
+ my ($class, $main, $oid, $owner_uid, $owner_gid, $date_creation, $description, $refuse_reason, $state, $type) = @_;
- bless($self, $class);
+ my $self = {};
- return undef unless(defined $main);
+ bless($self, $class);
- $self->{main} = $main;
- $self->{db} = $main->get_db_object;
- $self->{object_id} = $oid;
- $self->{owner_uid} = $owner_uid;
- $self->{owner_gid} = $owner_gid;
- $self->{date_creation} = $date_creation;
- $self->{description} = $description;
- $self->{refuse_reason} = $refuse_reason;
- $self->{state} = $state;
- $self->{type} = $type;
-
- return $self;
-}
+ return undef unless(defined $main);
-sub get_main
-{
- my $self = shift;
- return $self->{'main'};
-}
+ $self->{main} = $main;
+ $self->{db} = $main->get_db_object;
+ $self->{object_id} = $oid;
+ $self->{owner_uid} = $owner_uid;
+ $self->{owner_gid} = $owner_gid;
+ $self->{date_creation} = $date_creation;
+ $self->{description} = $description;
+ $self->{refuse_reason} = $refuse_reason;
+ $self->{state} = $state;
+ $self->{type} = $type;
-sub get_db_object {
- my $self = shift;
- return $self->{main}->get_db_object;
+ return $self;
}
-sub delete
-{
- my $self = shift;
-
- my $query;
- my $request;
-
-
- # Foreign key constraints are in 'ON DELETE CASCADE' mode
- # we don't have to bother with foreign tables deletion.
- $query = 'DELETE FROM vhffs_object WHERE object_id=?';
- $request = $self->{'db'}->prepare($query);
- $request->execute( $self->{'object_id'} ) or return -1;
-
- return 1;
-}
-
-
=head2 create
- my $object = Vhffs::Object::create($main, [$owner_uid, $owner_gid, $description, $state, $type])
+my $object = Vhffs::Object::create($main, [$owner_uid, $owner_gid, $description, $state, $type])
Create (in database) and return a new object.
@@ -163,42 +105,151 @@
=back
=cut
-
sub create {
- my ($vhffs, $owner_uid, $owner_gid, $description, $state, $type) = @_;
- $description = '' unless defined $description;
- $description =~ s/\r\n/\n/g;
- $state = ($vhffs->get_config->get_moderation ? Vhffs::Constants::WAITING_FOR_VALIDATION : Vhffs::Constants::WAITING_FOR_CREATION) unless defined $state;
- my $sth = $vhffs->get_db_object->prepare('INSERT INTO vhffs_object(owner_uid, owner_gid, date_creation, state, description, type) VALUES ( ?, ?, ?, ?, ?, ?)');
- $sth->execute($owner_uid, $owner_gid, time(), $state, $description, $type) or return undef;
- my $oid = $vhffs->get_db_object->last_insert_id(undef, undef, 'vhffs_object', undef);
+ my ($vhffs, $owner_uid, $owner_gid, $description, $state, $type) = @_;
+ $description = '' unless defined $description;
+ $description =~ s/\r\n/\n/g;
+ $state = ($vhffs->get_config->get_moderation ? Vhffs::Constants::WAITING_FOR_VALIDATION : Vhffs::Constants::WAITING_FOR_CREATION) unless defined $state;
+ my $sth = $vhffs->get_db_object->prepare('INSERT INTO vhffs_object(owner_uid, owner_gid, date_creation, state, description, type) VALUES ( ?, ?, ?, ?, ?, ?)');
+ $sth->execute($owner_uid, $owner_gid, time(), $state, $description, $type) or return undef;
+ my $oid = $vhffs->get_db_object->last_insert_id(undef, undef, 'vhffs_object', undef);
- my $res = get_by_oid($vhffs, $oid);
+ my $res = get_by_oid($vhffs, $oid);
- $res->add_history('Object created');
- return $res;
+ $res->add_history('Object created');
+ return $res;
}
-sub get_owner_uid
-{
+=pod
+
+=head2 get_by_oid
+
+my $obj = Vhffs::Object::get_by_oid($main, $oid);
+
+Fetches an object using its object ID. Returned object is
+fully functional.
+
+=cut
+sub get_by_oid {
+ my ($vhffs, $oid) = @_;
+ return undef unless ( defined $oid && $oid =~ /^\d+$/ );
+
+ my $query = 'SELECT owner_uid, owner_gid, date_creation, description, refuse_reason, state, type FROM vhffs_object WHERE object_id =?';
+ my $sth = $vhffs->get_db_object->prepare( $query );
+ my $rows = $sth->execute( $oid );
+
+ return undef unless $rows == 1;
+
+ my @result = $sth->fetchrow_array();
+
+ my $object = _new Vhffs::Object($vhffs, $oid, @result);
+
+ return $object;
+}
+
+=pod
+
+=head2 get_main
+
+This method returns the Vhffs::Main object contained in this object.
+This method can be useful if you have an object and you need a Vhffs::Main instance.
+
+=cut
+sub get_main {
my $self = shift;
+ return $self->{'main'};
+}
+
+sub get_db_object {
+ my $self = shift;
+ return $self->{main}->get_db_object;
+}
+
+=pod
+
+=head2 delete
+
+Delete the object. This method is called from inherited class.
+Note that it destroy the object-part (history, ...), but not the inherited class.
+Returns negative value if fails, positive if success.
+
+=cut
+sub delete {
+ my $self = shift;
+
+ my $query;
+ my $request;
+
+ # Foreign key constraints are in 'ON DELETE CASCADE' mode
+ # we don't have to bother with foreign tables deletion.
+ $query = 'DELETE FROM vhffs_object WHERE object_id=?';
+ $request = $self->{'db'}->prepare($query);
+ $request->execute( $self->{'object_id'} ) or return -1;
+
+ return 1;
+}
+
+=pod
+
+=head2 set_owner_uid
+
+Change the uid of the owner of this object.
+
+=cut
+sub set_owner_uid {
+ my ( $self , $value ) = @_;
+ $self->{'owner_uid'} = $value;
+}
+
+=pod
+
+=head2 get_owner_uid
+
+Returns the uid that owns this Object.
+
+=cut
+sub get_owner_uid {
+ my $self = shift;
return $self->{'owner_uid'};
}
-sub get_owner_gid
-{
+=pod
+
+=head2 set_owner_gid
+
+Change the gid of the group of this object.
+
+=cut
+sub set_owner_gid {
+ my ( $self , $value ) = @_;
+ $self->{'owner_gid'} = $value;
+}
+
+=pod
+
+=head2 get_owner_gid
+
+Returns the gid that owns this Object.
+
+=cut
+sub get_owner_gid {
my $self = shift;
return $self->{'owner_gid'};
}
-sub get_type
-{
- my $self = shift;
- return $self->{type};
+sub get_type {
+ my $self = shift;
+ return $self->{type};
}
-sub commit
-{
+=pod
+
+=head2 commit
+
+Apply all changes that were made on this object. Returns negative value if failed, positive if success.
+
+=cut
+sub commit {
my $self = shift;
my $request;
@@ -207,39 +258,50 @@
$result->execute( $self->{'state'} , $self->{'description'} , $self->{'refuse_reason'}, $self->{'owner_uid'} , $self->{'owner_gid'} , $self->{'object_id'} );
}
-sub get_date
-{
+=pod
+
+=head2 get_date
+
+Returns the date of the creation for this object.
+
+=cut
+sub get_date {
my $self = shift;
return $self->{'date_creation'};
}
-sub get_oid
-{
+=pod
+
+=head2 get_oid
+
+Returns the object_id of this Object.
+
+=cut
+sub get_oid {
my $self = shift;
return $self->{'object_id'};
}
-sub get_description
-{
+=pod
+
+=head2 get_description
+
+Returns the description of this object.
+
+=cut
+sub get_description {
my $self = shift;
return $self->{'description'};
}
-sub set_owner_uid
-{
- my ( $self , $value ) = @_;
- $self->{'owner_uid'} = $value;
-}
+=pod
-sub set_owner_gid
-{
- my ( $self , $value ) = @_;
- $self->{'owner_gid'} = $value;
-}
+=head2 get_status
+Get the status of this object. The status are given in the Vhffs::Constants class.
-sub get_status
-{
+=cut
+sub get_status {
my $self = shift;
return $self->{'state'};
}
@@ -256,8 +318,14 @@
return '????';
}
-sub set_status
-{
+=pod
+
+=head2 set_status
+
+Change the status of an object. The status are available as constants in Vhffs::Constants class. When you change the status of an object, a message is added in its history.
+
+=cut
+sub set_status {
my ($self , $value);
$self = shift;
$value = shift;
@@ -295,33 +363,27 @@
}
+=pod
-sub set_default_state
-{
- my $self = shift;
- my $vhffs = $self->{'main'};
+=head2 set_description( $string )
- if( $vhffs->get_config->get_moderation == 1 )
- {
- $self->set_status( Vhffs::Constants::WAITING_FOR_VALIDATION );
- }
- else
- {
- $self->set_status( Vhffs::Constants::WAITING_FOR_CREATION );
- }
- return( 1 );
-}
+Change the description of an object. As all accessors, you need to call commit() method after.
-sub set_description
-{
+=cut
+sub set_description {
my ($self , $value) = @_;
$value =~ s/\r\n/\n/g;
$self->{'description'} = $value ;
}
+=pod
-sub add_history
-{
+=head2 add_history( $string )
+
+Add a message in the object's history. Don't forget to set current user through $main->set_current_user before.
+
+=cut
+sub add_history {
use Vhffs::Main;
my $self = shift;
my $message = shift;
@@ -334,20 +396,41 @@
return $self->{'db'}->last_insert_id(undef, undef, 'vhffs_history', undef);;
}
-# Returns a hashref wich contains all the history
-sub get_history
+=pod
+
+=head2 get_history( )
+
+Returns an hashref that represent the history of this object. This object has as key the date of each history message. For exemple :
+
+$history = $object->get_history();
+
+if( defined $history )
+
{
+
+ foreach $key ( keys %{$history} )
+
+ {
+
+ print "At date " . $key . " message: " . $history->{$key}{'message'};
+
+ }
+
+}
+
+=cut
+sub get_history {
use Vhffs::Functions;
my $self = shift;
- my $dbh = $self->get_db_object;
- my $sql = 'SELECT history_id, message, date, source.username as source FROM vhffs_history h LEFT JOIN vhffs_users source ON source.uid = h.source_uid WHERE h.object_id = ? ORDER BY date DESC';
- return $dbh->selectall_arrayref($sql, {Slice => {}}, $self->{object_id});
+ my $dbh = $self->get_db_object;
+ my $sql = 'SELECT history_id, message, date, source.username as source FROM vhffs_history h LEFT JOIN vhffs_users source ON source.uid = h.source_uid WHERE h.object_id = ? ORDER BY date DESC';
+ return $dbh->selectall_arrayref($sql, {Slice => {}}, $self->{object_id});
}
sub set_refuse_reason {
- my ($self , $value) = @_;
+ my ($self , $value) = @_;
$self->{'refuse_reason'} = $value;
}
@@ -359,8 +442,7 @@
# Accept this object
-sub moderate_accept
-{
+sub moderate_accept {
my $self = shift;
my $comments = shift;
@@ -404,7 +486,7 @@
);
}
my $subject = sprintf(
- gettext('Your request for a %s (%s) on %s was accepted'),
+ gettext('Your request for a %s (%s) on %s was accepted'),
Vhffs::Functions::type_string_from_type_id( $self->get_type ),
$self->get_label,
$vhffs->get_config->get_host_name
@@ -422,8 +504,7 @@
# Refuse this object
-sub moderate_refuse
-{
+sub moderate_refuse {
my $self = shift;
my $reason = shift;
@@ -472,8 +553,7 @@
# Delete this object with a notice mail
-sub delete_withmail
-{
+sub delete_withmail {
my $self = shift;
my $vhffs = $self->{main};
@@ -514,8 +594,7 @@
}
-sub resubmit_for_moderation
-{
+sub resubmit_for_moderation {
my $self = shift;
my $description = shift;
@@ -527,13 +606,17 @@
return $self->commit;
}
+=pod
-#Get all objects of the database.
-#If $name is undefined, the functions returns ALL objects.
-#Else, it returns objects like the name. It searchs in oid, user and description
-#The first arg is a valid instance of Vhffs::Main
-sub getall
-{
+=head2 getall( Vhffs::Main , $name )
+
+The getall is very important and defined in every service. In Vhffs::Object,
+it returns all object if $name is not defined (undef). Return all objects that matches with $name if $name is defined.
+
+If $name is undefined, the functions returns ALL objects.
+
+=cut
+sub getall {
my $vhffs = shift;
my $name = shift;
my $state = shift;
@@ -592,7 +675,7 @@
=head2 get_group
- my $group = $object->get_group;
+my $group = $object->get_group;
Returns a Vhffs::Group object of the group of the Vhffs::Object object.
@@ -609,7 +692,7 @@
=head2 get_owner
- my $owner = $object->get_owner;
+my $owner = $object->get_owner;
Returns a Vhffs::User object of the owner of the Vhffs::Object object.
@@ -622,53 +705,23 @@
return $self->{'user'};
}
-=pod
-
-=head2 get_by_oid
-
- my $obj = Vhffs::Object::get_by_oid($main, $oid);
-
-Fetches an object using its object ID. Returned object is
-fully functional.
-
-=cut
-
-sub get_by_oid
-{
- my ($vhffs, $oid) = @_;
- return undef unless ( defined $oid && $oid =~ /^\d+$/ );
-
- my $query = 'SELECT owner_uid, owner_gid, date_creation, description, refuse_reason, state, type FROM vhffs_object WHERE object_id =?';
- my $sth = $vhffs->get_db_object->prepare( $query );
- my $rows = $sth->execute( $oid );
-
- return undef unless $rows == 1;
-
- my @result = $sth->fetchrow_array();
-
- my $object = _new Vhffs::Object($vhffs, $oid, @result);
-
- return $object;
-}
-
-
=head2 fill_object
- my $svc = Vhffs::Service::XXX::fill_object($obj);
+my $svc = Vhffs::Service::XXX::fill_object($obj);
This method should be overloaded in every subclasses.
Its goal is to transform a given object into a more
specialized subclass.
=cut
sub fill_object {
- my ($class, $obj) = @_;
- warn "Unimplemented fill_object method\n";
- return $obj;
+ my ($class, $obj) = @_;
+ warn 'Unimplemented fill_object method'."\n";
+ return $obj;
}
=head2 _fill_object
- $svc = $class->SUPER::_fill_object($obj, $sql);
+my $svc = $class->SUPER::_fill_object($obj, $sql);
Convenience method to implement fill_object in subclasses.
@@ -681,115 +734,20 @@
=cut
sub _fill_object {
- my ($class, $obj, $sql) = @_;
- my $dbh = $obj->get_db_object();
- my $res = $dbh->selectrow_hashref($sql, undef, $obj->get_oid);
- return $obj unless(defined $res);
- foreach(keys %$res) {
- $obj->{$_} = $res->{$_};
- }
- return bless($obj, $class);
+ my ($class, $obj, $sql) = @_;
+ my $dbh = $obj->get_db_object();
+ my $res = $dbh->selectrow_hashref($sql, undef, $obj->get_oid);
+ return $obj unless(defined $res);
+ foreach(keys %$res) {
+ $obj->{$_} = $res->{$_};
+ }
+ return bless($obj, $class);
}
1;
-
+
__END__
-=head2 get_main( )
-
-This method returns the Vhffs::Main object contained in this object.
-This method can be useful if you have an object and you need a Vhffs::Main instance.
-
-=head2 delete( )
-
-Delete the object. This method is called from inherited class.
-Note that it destroy the object-part (history, ...), but not the inherited class.
-Returns negative value if fails, positive if success.
-
-=head2 commit( )
-
-Apply all changes that were made on this object. Returns negative value if failed, positive if success.
-
-=head2 fetch( )
-
-Get informations for this object from the database. Typically, you create an instance of Vhffs::Object with a valid object-id and when you call fetch, it takes all informations about this object in the database according to the object-id.
-
-=head2 fetch_object( )
-Same as fetch()
-
-=head2 get_owner_uid( )
-
-Returns the uid that own this Object.
-
-=head2 modify( $field , $value )
-
-Modify a propertiy of this object. For example, modify( "description" , "my_description") will change the description of this Object. If you want update those modified informations on the database, you need to call commit() to update informations in the database. Note that you never had to change object-id of an object.
-
-=head2 get_date( )
-
-Returns the date of the creation for this object.
-
-=head2 get_oid( )
-
-Returns the object_id of this Object.
-
-=head2 get_description( )
-
-Returns the description of this object.
-
-=head2 set_owner( )
-
-Change the uid of the owner of this object.
-
-=head2 get_status( )
-
-Get the status of this object. The status are given in the Vhffs::Constants class.
-
-=head2 set_status( )
-
-Change the status of an object. The status are available as constants in Vhffs::Constants class. When you change the status of an object, a message is added in its history.
-
-=head2 set_description( $string )
-
-Change the description of an object. As all accessors, you need to call commit() method after.
-
-=head2 add_history( $string )
-
-Add a message in the object's history.
-
-=head2 get_history( )
-
-Returns an hashref that represent the history of this object. This object has as key the date of each history message. For exemple :
-
-$history = $object->get_history();
-
-if( defined $history )
-
-{
-
- foreach $key ( keys %{$history} )
-
- {
-
- print "At date " . $key . " message: " . $history->{$key}{'message'};
-
- }
-
-}
-
-=head2 get_group( )
-
-Returns the Vhffs::Group instance that own this object. Returns undef is fails.
-
-=head2 get_owner( )
-
-Returns the Vhffs::User instance that own this object. Returns undef is fails.
-
-=head2 getall( Vhffs::Main , $name )
-
-The getall is very important and defined in every service. In Vhffs::Object,
-it returns all object if $name is not defined (undef). Return all objects that matches with $name if $name is defined.
-
=head1 SEE ALSO
Vhffs::Constants Vhffs::User Vhffs::Group