Location: PHPKode > projects > NmnNewsletter > NmnNewsletter1.0.8/includes/NewsletterLog.class.php
<?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
    */
class NewsletterLog extends NewsletterDbConnector{
	
	function getLogsArray($idNewsletter){
		if(is_null($idNewsletter))$idNewsletter=0;
		$dbConn = NewsletterDbConnector::connect();
		$query = "SELECT n.id_".DB_LOGS_TABLE.", n.id_nl, n.id_user, n.stato, n.data_invio, n.data_lettura, nl.titolo, nl.data_creazione, u.email, u.deleted FROM ".DB_LOGS_TABLE." n LEFT OUTER JOIN ".DB_NEWSLETTERS_TABLE." nl ON nl.id_".DB_NEWSLETTERS_TABLE." = n.id_nl LEFT OUTER JOIN ".DB_USERS_TABLE." u ON u.id_".DB_USERS_TABLE." = n.id_user WHERE n.id_nl = ".mysql_escape_string($idNewsletter);
		//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 ($nl = mysql_fetch_assoc($result)){
			$myNlLog = array();
			$myNlLog["id_".DB_LOGS_TABLE.""] = $nl["id_".DB_LOGS_TABLE.""];
			$myNlLog["id_newsletter"] = $nl["id_nl"];
			$myNlLog["titolo_newsletter"] = $nl["titolo"];
			$myNlLog["data_creazione_nl"] = $nl["data_creazione"];
			$myNlLog["id_".DB_USERS_TABLE.""] = $nl["id_user"];
			$myNlLog["email"] = $nl["email"];
			$myNlLog["stato"] = $nl["stato"];
			$myNlLog["data_invio"] = $nl["data_invio"];
			$myNlLog["data_lettura"] = $nl["data_lettura"];
			$myNlLog["deleted"] = $nl["deleted"];
			$returnArray[] = $myNlLog;
		}
		return $returnArray;
	}

	function getSentNewsletters($idNewsletter){
		if(is_null($idNewsletter))$idNewsletter=0;
		if ($idNewsletter){
			$dbConn = NewsletterDbConnector::connect();
			$query = "SELECT sum(n.stato) as 'sent', (count(n.stato)) as 'unsent' FROM ".DB_LOGS_TABLE." n WHERE n.id_nl = '".$idNewsletter."' LIMIT 1";
			$result = mysql_query($query);
			NewsletterDbConnector::closeConnection($dbConn);
			$sentData = mysql_fetch_assoc($result);
			return $sentData;
		}
	}

	function markLogAsViewed($idNewsletter, $idUser){
		if(is_null($idNewsletter))$idNewsletter=0;
		if(is_null($idUser))$idUser=0;
		$dbConn = NewsletterDbConnector::connect();
		$query = "UPDATE ".DB_LOGS_TABLE." SET data_lettura = CURRENT_TIMESTAMP() WHERE id_nl = '".mysql_escape_string($idNewsletter)."' AND id_user = '".mysql_escape_string($idUser)."'";
		$result = @mysql_query($query);
		NewsletterDbConnector::closeConnection($dbConn);
		if ($result != 1){
			return false;
		}
		return true;
	}

	function markLogAsSent($idLog){
		if(is_null($idLog))$idLog=0;
		if ($idLog != ''){
			$todayDate = date('Y-m-d H:i:s');
			$dbConn = NewsletterDbConnector::connect();
			$query = "UPDATE ".DB_LOGS_TABLE." SET stato = '1', data_invio = '".mysql_escape_string($todayDate)."' WHERE id_".DB_LOGS_TABLE." = '".mysql_escape_string($idLog)."'";
			$result = @mysql_query($query);
			NewsletterDbConnector::closeConnection($dbConn);
			if ($result != 1){
				return false;
			}
			return true;
		}
	}

	function createLogs($id_newsletter){
		if(is_null($idNewsletter))$idNewsletter=0;
		if ($id_newsletter){
			$users = NewsletterUsers::getUserArray(true,false);
			$dbConn = NewsletterDbConnector::connect();
			foreach ($users as $idUser => $email){
				$query = "Select * from ".DB_LOGS_TABLE." where id_nl = '".mysql_escape_string($id_newsletter)."' AND id_user = '".mysql_escape_string($idUser)."'";
				$result = mysql_query($query);

				if (mysql_num_rows($result) < 1){
					$query = "INSERT INTO ".DB_LOGS_TABLE." (id_nl,id_user,stato) VALUES ('".mysql_escape_string($id_newsletter)."','".mysql_escape_string($idUser)."','0')";
					$result = mysql_query($query);

					if (!$result){
						echo 'failed to create log';
					}
				}

			}
		}
	}

	



}



?>
Return current item: NmnNewsletter