rsstootalizer/RSSTootalizer/Base.pm

70 lines
1.7 KiB
Perl
Raw Normal View History

2017-04-21 14:56:24 +00:00
# vim: set foldmarker={,}:
use strict;
2017-04-23 18:51:48 +00:00
package RSSTootalizer::Base;
2017-04-21 14:56:24 +00:00
use JSON;
use Data::Dumper;
2017-04-23 18:51:48 +00:00
use RSSTootalizer::DB;
2017-04-21 14:56:24 +00:00
use Digest::MD5 qw(md5_hex);
# Class functions
sub all {
my $class = shift;
2017-04-23 18:51:48 +00:00
my $all = RSSTootalizer::DB->doSELECT("SELECT * FROM ".$class->dbTable." ORDER BY ".$class->orderBy);
2017-04-21 14:56:24 +00:00
my @retVal;
foreach my $object (@$all){
$object = $class->new($object);
push @retVal, $object;
}
return @retVal;
}
sub get_by {
my $class = shift;
my $key = shift;
my $value = shift;
2017-04-23 18:51:48 +00:00
my $retVal = RSSTootalizer::DB->doSELECT("SELECT * FROM ".$class->dbTable." WHERE ".$key." ".($value =~ /%/ ? "LIKE" : "=")." ?", $value);
2017-04-21 14:56:24 +00:00
$retVal = $$retVal[0];
return 0 unless defined($retVal);
return $class->new($retVal);
}
sub create {
my $class = shift;
my %data = @_;
2017-04-23 18:51:48 +00:00
RSSTootalizer::DB->doINSERThash($class->dbTable, %data);
my $data = RSSTootalizer::DB->doSELECT("SELECT * FROM ".$class->dbTable." WHERE ID = LAST_INSERT_ID()");
2017-04-21 14:56:24 +00:00
$data = $$data[0];
2017-04-22 18:49:49 +00:00
return $class->get_by("ID", $$data{ID});
2017-04-21 14:56:24 +00:00
}
sub new {
my $this = shift;
my $data = shift;
unless ($data){
&main::Error("Invalid data call", "new() called without data!");
}
my $self = { "data" => $data };
bless ($self, $this);
}
# Object methods
sub save {
my $self = shift;
if (exists($self->{"data"}->{"password"}) && "x".$self->{"data"}->{"password"} ne "x"){
2017-04-22 18:49:49 +00:00
$self->{"data"}->{"password"} = md5_hex($self->{"data"}->{"ID"}.$self->{"data"}->{"password"});
2017-04-21 14:56:24 +00:00
} else {
delete $self->{"data"}->{"password"};
}
2017-04-23 18:51:48 +00:00
return RSSTootalizer::DB->doUPDATEhash($self->dbTable, "ID = ".$self->{"data"}->{"ID"}, %{$self->{"data"}});
2017-04-22 18:49:49 +00:00
}
sub delete {
my $self = shift;
2017-04-23 18:51:48 +00:00
return RSSTootalizer::DB->doDELETE("DELETE FROM ".$self->dbTable." WHERE ID = ?", $self->{data}->{ID});
2017-04-21 14:56:24 +00:00
}
1;