Location: PHPKode > projects > Write2Left > Base/Subs.php
<?php
/*
+------------------------------------------------------
| Write2Left
| (c) timdorr
| http://www.write2left.com
| hide@address.com
| See License.txt for license info
|------------------------------------------------------
| Script: Subs.php
| Description:
|   The main page for a subscription management
| Created Aug-4-2003
+------------------------------------------------------
*/

/* Class: Subs
 * Description:
 *   Driver for the subscription manager
 */
class Subs
{
	var $skin    = '';
	var $menu    = true;
	var $message = '';
	var $error   = '';
	
	function run()
	{
		global $W2L, $userinfo, $output, $db;
		
		// Do skin related stuff
		require( "./Skin/Subs.php" );
		$this->skin = new Skin_Subs();
		$output->page_title = "Manage Subscriptions";
		
		$output->add( $this->skin->body_top() );

		// Check for a method
		if( array_key_exists( 'M', $W2L->input ) )
		{
			if( $W2L->input['M'] == 'AddSub' )
				$this->do_add( $W2L->input['name'], 
							   $W2L->input['addy'],
							   $W2L->input['type'] );
			if( $W2L->input['M'] == 'EditSub' )
				$this->edit_sub( $W2L->input['sub_id'] );
			if( $W2L->input['M'] == 'DoEditSub' )
				$this->do_edit( $W2L->input['sub_id'], 
								$W2L->input['name'], 
								$W2L->input['addy'],
								$W2L->input['type'] );
			if( $W2L->input['M'] == 'DeleteSub' )
				$this->do_del( $W2L->input['sub_id'] );
			if( $W2L->input['M'] == 'SendMessage' )
				$this->send_message( $W2L->input['subject'], 
									 $W2L->input['message'] );
		}

		// Show whatever error we get, if any
		if( $this->error != "" )
			$output->add( "<div class=\"error\">\n" . $this->error . "\n</div><br />\n" );

		// Show whatever result we get, if any
		if( $this->message != "" )
			$output->add( "<div class=\"message\">\n" . $this->message . "\n</div><br />\n" );

		// Show the subscriber list
		$db->query( "SELECT * FROM w2l_subs
							 WHERE log_id = $userinfo->log_id" );
		$output->add( $this->skin->subs_head() );
		while( $sub = $db->fetch_array() )
			$output->add( $this->skin->sub_row( $sub['sub_id'], 
												$sub['name'] , 
												$sub['address'], 
												$sub['type'] ) );
		$output->add( $this->skin->subs_foot() );
		
		$output->add( $this->skin->send_form() );
		
		$output->add( $this->skin->body_bottom() );
	}
	
	//================
	// Adds a new subscription
	//================
	function do_add( $name, $addy, $type )
	{
		global $db, $userinfo;
		$db->query( "INSERT INTO w2l_subs VALUES
								 ( NULL,
								   $userinfo->log_id, 
								   '$name', 
								   '$addy',
								   '$type' )" );
								   
		$this->message = 'Subscription Added';
	}
	
	//================
	// Edits the subscription details
	//================
	function do_edit( $id, $name, $addy, $type )
	{
		global $db;
		$db->query( "UPDATE w2l_subs SET name='$name', 
										 address='$addy', 
										 type='$type'
									WHERE sub_id=$id" );
		
		$this->message = 'Subscription Edited';
	}
	
	//================
	// Prompts to delete a subscription from the database
	//================
	function del_user( $user_id )
	{
		global $db, $output;
		
		$user = $db->query_fetch( "SELECT name FROM w2l_users WHERE user_id = $user_id" );
		
		$output->add( $this->skin->del_confirm( $user['name'], $user_id ) );
	}
	
	//================
	// Removes a subscription.
	//================
	function do_del( $id )
	{
		global $db;
		$db->query( "DELETE FROM w2l_subs WHERE sub_id=$id" );
		
		$this->message = 'Subscription Removed';
	}
	
	//================
	// Show the edit subscription form
	//================
	function edit_sub( $id )
	{
		global $db, $output;
		
		$sub = $db->query_fetch( "SELECT * FROM w2l_subs WHERE sub_id=$id" );
		$output->add( $this->skin->sub_edit_form( $sub['sub_id'], 
												  $sub['name'], 
												  $sub['address'],
												  $sub['type'] ) );
	}
	
	//================
	// Send mailing
	//================
	function send_message( $subject, $message )
	{
		global $W2L, $db, $userinfo;
		
		
		if( trim( $subject ) == '' )
		{
			$this->error = 'No subject given. Please enter a subject.';
			return;
		}
		
		if( trim( $message ) == '' )
		{
			$this->error = 'No message was typed. Please enter a message.';
			return;
		}
		
		// Get the log options
		$options = $func->log_settings();
		
		// Get the subscribers and send to each
		$db->query( "SELECT address FROM w2l_subs WHERE log_id=$userinfo->log_id" );
		$count = 0;
		while( $sub = $db->fetch_array() )
		{
			$count++;
			
			// Do the mailing or bail out with an error...
			if( mail( $sub['address'], 
					  "$options[name] message: ".$subject, 
					  $message,
					  "From: $options[notify_from_mail]\r\n" .
					  "Reply-To: $options[notify_reply_mail]\r\n" .
					  "X-Mailer: Write2Left $W2L->version" ) == false)
			{
				$this->error = 'Unable to send notification email. Please check your mail settings with PHP';
				return;
			}
		}
		
		// Tell the result
		if( $count > 0 )
			$this->message .= "Notifications sent to $count subscribers<br />";
	}
}

$driver = new Subs();
?>
Return current item: Write2Left