Login   Register  
PHP Classes
elePHPant
Icontem

File: example.SiteMessage.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of John Wetsell  >  Site Message  >  example.SiteMessage.php  >  Download  
File: example.SiteMessage.php
Role: Example script
Content type: text/plain
Description: Example to show some basic use of the classes.
Class: Site Message
Exchange messages between site users using MySQL
Author: By
Last change:
Date: 4 years ago
Size: 8,882 bytes
 

Contents

Class file image Download
<html>
    <head>
        <title></title>
        <style>
            .info {
                border:1px solid red;
                background:#D6E6C6;
                color:black;
                font-weight:bolder;
                padding:3px;
            }
            .finish {
                border:1px solid green;
                background:#D6E6C6;
                color:black;
                font-weight:bolder;
                padding:3px;
            }            
        </style>
    </head>
    <body>
<?php
    
// http://webdev/siteMessage/example.SiteMessage.php
    
    // Read from Top to the bottom. otherways you may not 
    // understand this script. though it's quite simple,
    // read the comments!!
    // thanks.

    // INCLUDE NEEDED LIBs
    
include_once("class.SiteMessage.php");
    include_once(
"class.SiteMessageFolder.php");
    include_once(
"class.SiteMessageException.php");

    
// CREATE DATABASE AND TABLEs 
    
$config['db']['msg'] = array('hostname'=>'localhost','username'=>'alex','password'=>'acme213','name'=>'siteMessage');
    
    
    
$config['db']['msg']['hostname'] = 'nimda.globatmysql.com';
    
$config['db']['msg']['username'] = 'snr4rtest';
    
$config['db']['msg']['password'] = 'test0419';
    
$config['db']['msg']['name'] = 'snr4r_test';
    
    
/***
     * CREATE DATABASE THRU YOUR WEB HOST PROVIDERS CPANEL OR TOOL
     */
    
echo "<div>Please take the time to create the database with the proper user and password for this example to use.<br/><strong>Database:</strong>".$config['db']['msg']['name']."<br/><strong>Username:</strong>".$config['db']['msg']['username']."<br/><strong>Password:</strong>".$config['db']['msg']['password']."<br/></div>";
    
    
    
$db mysqli_connect($config['db']['msg']['hostname'], $config['db']['msg']['username'], $config['db']['msg']['password'],$config['db']['msg']['name']);
    if (
mysqli_connect_errno()) {
        
printf("Connect failed: %s\n"mysqli_connect_error());
        exit();
    }
    
/*
    if (mysqli_query($con,"CREATE DATABASE ".$config['db']['msg']['name']))
    {
       echo "Database created";
    } else {
       echo "Error creating database: " . mysqli_error($con);
       exit();
    }
    */
    
    
$error    mysqli_query($db,"
        CREATE TABLE siteMessage (
            sm_id            INTEGER        PRIMARY KEY NOT NULL AUTO_INCREMENT,
            sm_thread_id    VARCHAR(25)     NOT NULL,
            sm_sender_id    INTEGER     NOT NULL,
            sm_receiver_id  INTEGER     NOT NULL,
            sm_subject_data TEXT        NOT NULL,
            sm_message_data TEXT        NOT NULL,
            sm_message_date DATETIME NOT NULL,
            sm_message_read    DATETIME NULL,
            sm_folder_id    INTEGER     NOT NULL DEFAULT 0
        );    
    "
);    
    if (
$error == false) {
        echo 
"<pre class=\"info\">Error creating table siteMessage</pre>";
    }
    
$error    mysqli_query($db,"
        CREATE TABLE siteMessageFolders (
            sm_folder_id    INTEGER     PRIMARY KEY    NOT NULL AUTO_INCREMENT,
            sm_user_id        INTEGER        NOT NULL,        
            sm_folder_name    VARCHAR(50)    NOT NULL,
            sm_parent_id    INTEGER(10)    NULL,
            sm_folder_date    DATETIME    NOT NULL,
            UNIQUE (sm_user_id,sm_folder_name)
        );
    "
);
    if (
$error == false) {
        echo 
"<pre class=\"info\">Error creating table siteMessageFolders</pre>";
    } else {
        echo 
"<pre class=\"info\"> SUCCESS creating tables siteMessage and siteMessageFolders</pre>";
    }

    
//create a thread ID, you can add a prefix to uniqid such as a userID.
    
echo "Thread ID: ".uniqid('243')."<br/>";
    
/**
    * TRY TO OPEN FOLDER ID 1, IF IT DOESN't EXIST,
    * CREATE A NEW FOLDER(INBOX) FOR USERID 1
    *
    *         SiteMessageFolder::open()
    *         SiteMessageFolder::create()
    **/
    
$myFolder =    new SiteMessageFolder($db);
    try {
        
// OPEN
        
$myFolder->open(1);
        echo 
"<pre class=\"info\">-> opened Folder</pre>";
    } catch (
FolderNotAvailableException $e) {
        echo 
"<pre class=\"info\">Creating Folder</pre>";
        
// CREATE
        
$myFolder->create("INBOX","1");
        echo 
"<pre class=\"info\">-> created Folder</pre>";
    } catch (
MySqlException $e) {
        
// DIE BECAUSE OF SQL-ERROR
        
echo "<br/>SQL Error";
        die(
$e->getMessage());
    }
    
    
/**
    * SOME TESTs
    *
    *         SiteMessageFolder::getName()
    *        SiteMessageFolder::getDate()
    *         SiteMessageFolder::hasSubfolder()
    *        SiteMessageFolder::hasMessage()
    **/
    
echo "<pre>Foldername:     ".$myFolder->getName()."</pre>";
    echo 
"<pre>created:     ".date("H:i:s d.m.Y"strtotime($myFolder->getDate()))."</pre>";    
    echo 
"<pre>Subfolders:     ".$myFolder->hasSubfolder()."</pre>";
    echo 
"<pre>Messages:     ".$myFolder->hasMessage()."</pre>";
    
    
    
/**
    * SOME TESTs
    *
    *         SiteMessageFolder::setName()
    **/
    
try {
        switch(
$myFolder->getName()) {
            case 
"INBOX":    echo "<pre class=\"info\">-> renaming folder from INBOX to OUTBOX...</pre>";
                            
$myFolder->setName("OUTBOX"); 
                            break;
            case 
"OUTBOX":    echo "<pre class=\"info\">-> renaming folder from OUTBOX to INBOX...</pre>";
                            
$myFolder->setName("INBOX");
                            break;
        }
    } catch (
MySqlException $e) {
        die(
$e->getMessage());
    }
    
    
/**
    * SOME TESTs
    *
    *         SiteMessageFolder::getName()
    **/
    
echo "<pre>Foldername:     ".$myFolder->getName()."</pre>";
    
    
    
/**
    *    Create a Subfolder of Folder #1
    */
    
echo "<pre class=\"info\">-> creating subfolder</pre>";
    
$myFolder->create("SUB1",1,1);
    try {
        
$myFolder->open(1);
    } catch (
FolderNotAvailableException $e) {
        die(
"Error: ".$e->getMessage());
    }
        
    
/**
    * SOME TESTs
    *
    *         SiteMessageFolder::hasSubfolder()
    **/    
    
echo "<pre>Subfolders:     ".$myFolder->hasSubfolder()."</pre>";
    
$mySubFolder $myFolder->getSubfolder(1);
    
    echo 
"after subFolders Info";
    
/**
    * SOME TESTs ON SUBFOLDER
    *
    *         SiteMessageFolder::getName()
    *        SiteMessageFolder::getDate()
    *         SiteMessageFolder::hasSubfolder()
    *        SiteMessageFolder::hasMessage()
    **/
    
echo "<pre class=\"info\">-> showing subfolder-infos</pre>";
    echo 
"<pre>Foldername:     ".$mySubFolder->getName()."</pre>";
    echo 
"<pre>created:     ".date("H:i:s d.m.Y"strtotime($mySubFolder->getDate()))."</pre>";    
    echo 
"<pre>Subfolders:     ".$mySubFolder->hasSubfolder()."</pre>";
    echo 
"<pre>Messages:     ".$mySubFolder->hasMessage()."</pre>";
    
    
/**
    * SOME TESTs 
    *
    *         SiteMessage::create()
    *        SiteMessageFolder::hasMessage()
    **/
    
$myMessage    =    new SiteMessage($db);
    try {
        if(
$myMessage->create(1,1,"This is the subject","This is the text")) {
            echo 
"<pre class=\"info\">-> creating a message</pre>";
        }
    } catch (
MySqlException $e) {
        
// DIE BECAUSE OF SQL-ERROR
        
die($e->getMessage());        
    }        
    
    
/**
    * SOME TESTs
    *    Move the newly created message into INBOX-Folder (id:1)
    */
    
echo "<pre class=\"info\">-> moving message to folder INBOX.</pre>";
    
$myMessage->setFolder(1);
    
    
/**
    * SOME TESTs 
    *        SiteMessageFolder::hasMessage()
    **/    
    
echo "<pre>Messages:     ".$myFolder->hasMessage()."</pre>";
    
    
/**
    * SOME TESTs
    *
    *         SiteMessage::getSender()
    *        SiteMessage::getReceiver()
    *         SiteMessage::getSubject()
    *         SiteMessage::getText()
    *        SiteMessage::getDate()
    **/
    
echo "<pre class=\"info\">-> show Message-Informations</pre>";
    echo 
"<pre>Sender(id):    ".$myMessage->getSender()."</pre>";
    echo 
"<pre>Receiver(id):    ".$myMessage->getReceiver()."</pre>";
    echo 
"<pre>Folder(id):     ".$myMessage->getFolder()."</pre>";
    echo 
"<pre>Date:         ".date("H:i:s m/d/Y"strtotime($myMessage->getDate()))."</pre>";    
    echo 
"<pre>Subject:     ".$myMessage->getSubject()."</pre>";
    echo 
"<pre>Text:         ".$myMessage->getText()."</pre>";
    echo 
"<pre>Thread ID:        ".$myMessage->getThreadID()."</pre>";
    
    
/**
     * SOME TESTS
     *    Create a reply to the new message, threadID should be the same as the parent/original message.
     */
    
try {
        if(
$myMessage->create(1,1,"RE: This is the subject","The reply text is this!",$myMessage->getThreadID())) {
            echo 
"<pre class=\"info\">-> creating a reply</pre>";
            echo 
"<pre class=\"info\">-> show Message-Informations</pre>";
            echo 
"<pre>Sender(id):  ".$myMessage->getSender()."</pre>";
            echo 
"<pre>Receiver(id):    ".$myMessage->getReceiver()."</pre>";
            echo 
"<pre>Folder(id):  ".$myMessage->getFolder()."</pre>";
            echo 
"<pre>Date:        ".date("H:i:s m/d/Y"strtotime($myMessage->getDate()))."</pre>";  
            echo 
"<pre>Subject:     ".$myMessage->getSubject()."</pre>";
            echo 
"<pre>Text:        ".$myMessage->getText()."</pre>";
            echo 
"<pre>Thread ID:        ".$myMessage->getThreadID()."</pre>";
        }
    } catch (
MySqlException $e) {
        
// DIE BECAUSE OF SQL-ERROR
        
die($e->getMessage());      
    }
    
    
    
/**
    * SOME TESTs
    *    Deleting Folder #1
    *    comment out, to keep all messages, folders, etc.
    */
    /*echo "<pre class=\"info\">-> Deleting Folder (id:1) and all Subfolders/Messages</pre>";
    $myFolder->delete();
    
    echo "<pre class=\"finish\">That's it. All Messages and Folders have been deleted.
            <p>Be inspired </p> Have fun! <p>- Cornelius -</p></pre>";    
            */
?>

    </body>
</html>