<?php
/*
NmnNewsletter is a library that provides newsletter service
management for websites running php and mysql.
Copyright (C) 2006 Ivan Preziosi from netmeans.net - Rome.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
For more informations or to join the development of the library contact
the author at: hide@address.com
*/
/**
* This class manages the Newsletter entities abstraction.
*
* @author Ivan Preziosi <hide@address.com>
* @version 1.0
* @since NmnNewsletter 1.0
* @package NmnNewsletter
*
*/
class Newsletter{
/**
* Returns all newsletters data in a array form
*
* @access static
*/
function getNewslettersArray(){
$dbConn = NewsletterDbConnector::connect();
$query = "SELECT id_".DB_NEWSLETTERS_TABLE.", titolo, data_creazione from ".DB_NEWSLETTERS_TABLE." ORDER BY data_creazione DESC";
//pagination/////////////////////////////////////////////////////////////////////////
$pagination = new MyPagina();
$pagination->sql = $query; // the (basic) sql statement (use the SQL whatever you like)
$result = $pagination->get_page_result(); // result set
$_SESSION["navigation"] = $pagination->return_formatted_links();
///////////////////////////////////////////////////////////////////////////////////////
NewsletterDbConnector::closeConnection($dbConn);
$returnArray = array();
while ($nlData = mysql_fetch_assoc($result)){
$nl = array();
$nl["id"] = $nlData["id_".DB_NEWSLETTERS_TABLE.""];
$nl["titolo"] = stripslashes($nlData["titolo"]);
$nl["data"] = $nlData["data_creazione"];
$returnArray[] = $nl;
}
return $returnArray;
}
/**
* Adds a newsletter to the database
*
* @param String $titolo the title of the newsletter
*
* @param String $html_body the html body of the newsletter
*
* @param String $text_body the text body of the newsletter
*
* @access static
*/
function addNewsletter($titolo, $html_body, $text_body){
if ($titolo != ''){
$todayDate = date('Y-m-d H:i:s');
$dbConn = NewsletterDbConnector::connect();
$query = "INSERT INTO ".DB_NEWSLETTERS_TABLE." (titolo, html_body, text_body) VALUES ('".mysql_escape_string($titolo)."','".mysql_escape_string($html_body)."','".mysql_escape_string($text_body)."')";
$result = mysql_query($query);
$nlId = mysql_insert_id();
NewsletterDbConnector::closeConnection($dbConn);
if ($result != 1){
return false;
}
return $nlId;
}
}
/**
* get a newsletter data from the db.
*
* @param Integer $idNewsletter the title of the newsletter
*
* @access static
*/
function getNewsletter($idNewsletter){
if(is_null($idNewsletter))$idNewsletter=0;
$dbConn = NewsletterDbConnector::connect();
$query = "SELECT * FROM ".DB_NEWSLETTERS_TABLE." WHERE id_".DB_NEWSLETTERS_TABLE." = '".mysql_escape_string($idNewsletter)."' LIMIT 1";
$result = mysql_query($query);
NewsletterDbConnector::closeConnection($dbConn);
$returnArray = array();
while ($nlData = mysql_fetch_assoc($result)){
$returnArray["id"] = $nlData["id_nl"];
$returnArray["titolo"] = stripslashes($nlData["titolo"]);
$returnArray["html_body"] = stripslashes($nlData["html_body"]);
$returnArray["text_body"] = stripslashes($nlData["text_body"]);
$returnArray["data"] = $nlData["data_creazione"];
}
return $returnArray;
}
/**
* edits a newsletter data from the db.
*
* @access static
*/
function editNewsletter($idNL, $titolo, $html_body, $text_body){
$dbConn = NewsletterDbConnector::connect();
$query = "UPDATE ".DB_NEWSLETTERS_TABLE." SET titolo = '".mysql_escape_string($titolo)."', html_body = '".mysql_escape_string($html_body)."', text_body = '".($text_body)."' WHERE id_".DB_NEWSLETTERS_TABLE." = '".($idNL)."'";
$result = @mysql_query($query);
NewsletterDbConnector::closeConnection($dbConn);
if ($result != 1){
echo $query;
return false;
}
return true;
}
/**
* deletes a newsletter data from the db.
*
* @access static
*/
function deleteNewsletter($idNewsletter){
if (!is_null($idNewsletter)){
$dbConn = NewsletterDbConnector::connect();
$query = "DELETE FROM ".DB_NEWSLETTERS_TABLE." WHERE id_".DB_NEWSLETTERS_TABLE." = '".mysql_escape_string($idNewsletter)."'";
$result = @mysql_query($query);
NewsletterDbConnector::closeConnection($dbConn);
if ($result != 1){
return false;
}
return true;
}
}
}
?>