[vhffs-dev] [1014] Fixed UTF-8 support on mailings, refactored SQL queries

[ Thread Index | Date Index | More vhffs.org/vhffs-dev Archives ]


Revision: 1014
Author:   gradator
Date:     2007-10-22 12:05:02 +0000 (Mon, 22 Oct 2007)

Log Message:
-----------
Fixed UTF-8 support on mailings, refactored SQL queries

Modified Paths:
--------------
    trunk/vhffs-api/src/Vhffs/Mailings.pm


Modified: trunk/vhffs-api/src/Vhffs/Mailings.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Mailings.pm	2007-10-21 22:36:38 UTC (rev 1013)
+++ trunk/vhffs-api/src/Vhffs/Mailings.pm	2007-10-22 12:05:02 UTC (rev 1014)
@@ -37,8 +37,11 @@
 
 package Vhffs::Mailings;
 
+use utf8;
+use strict;
 use Vhffs::Functions;
 use Vhffs::Constants;
+use Encode;
 
 sub add_mailing
 {
@@ -46,10 +49,10 @@
 	my $subject = shift;
 	my $message = shift;
 	
-	return -1 if ( ! defined $vhffs );
+	return -1 unless defined $vhffs;
 
-	$query = 'INSERT INTO vhffs_mailings (subject,message,date,state) VALUES( ? , ? , ? , ? )';
-	$request = $vhffs->{'db'}->prepare( $query );
+	my $query = 'INSERT INTO vhffs_mailings (subject,message,date,state) VALUES( ? , ? , ? , ? )';
+	my $request = $vhffs->{'db'}->prepare( $query );
 	$request->execute($subject, $message, time(), Vhffs::Constants::WAITING_FOR_CREATION) or return -2;
 
 	return 1;
@@ -60,17 +63,24 @@
 {
 	my $vhffs = shift;
 	my $state = shift;
+	my @params;
 
-    my $query;
-	my $request;
+	my $query = 'SELECT id_mailing,subject,message,date,state FROM vhffs_mailings ';
+	if( defined $state ) {
+		$query .= ' WHERE state=?';
+		push @params, $state;
+	}
 
-    $query = "SELECT * FROM vhffs_mailings ";
-    $query.= " WHERE state='".$state."'" if( defined $state );
+	my $request = $vhffs->{'db'}->prepare( $query );
+	$request->execute( @params ) or return undef;
+    
+	my $rows = $request->fetchall_hashref( 'id_mailing' );
+	foreach( keys(%{$rows}) ) {
+		$rows->{$_}->{'message'} = Encode::decode_utf8( $rows->{$_}->{'message'} );
+		$rows->{$_}->{'subject'} = Encode::decode_utf8( $rows->{$_}->{'subject'} );
+	}
 
-    $request = $vhffs->{'db'}->prepare( $query );
-    $request->execute or return undef ;
-    
-	return $request->fetchall_hashref('id_mailing');
+	return $rows;
 }
 
 
@@ -80,10 +90,11 @@
 	my $vhffs = shift;	
 	my $id = shift;
 	my $db = $vhffs->get_db_object;
-	$query = "DELETE FROM vhffs_mailings WHERE id_mailing='".$id."'";
-	$request = $db->prepare( $query );
-	$request->execute or return -2;
 
+	my $query = 'DELETE FROM vhffs_mailings WHERE id_mailing=?';
+	my $request = $db->prepare( $query );
+	$request->execute( $id ) or return -2;
+
 	return 1;
 }
 
@@ -94,13 +105,16 @@
 	my $id = shift;
 	my $db = $vhffs->get_db_object;
 
-	return undef if( ! defined $id );
+	return undef unless defined $id;
 	
-	$query = "SELECT * FROM vhffs_mailings WHERE id_mailing='".$id."'";
-	$request = $db->prepare( $query );
-	$request->execute or return -2;
+	my $query = 'SELECT id_mailing,subject,message,date,state FROM vhffs_mailings WHERE id_mailing=?';
+	my $request = $db->prepare( $query );
+	$request->execute( $id ) or return -2;
 
-	return $request->fetchrow_hashref() ;
+	my $row = $request->fetchrow_hashref();
+	$row->{'message'} = Encode::decode_utf8( $row->{'message'} );
+	$row->{'subject'} = Encode::decode_utf8( $row->{'subject'} );
+	return $row;
 }
 
 1;


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/