Author Topic: Changing script over from procedural to OOP...  (Read 643 times)

0 Members and 1 Guest are viewing this topic.

Offline cgm225Topic starter

  • Enthusiast
    • View Profile
Changing script over from procedural to OOP...
« on: March 13, 2008, 12:25:49 PM »
Below is a procedural script I use at the core of my image/video gallery to gather all the information I need to display images/video. 

I want to make this script object oriented, but do not know where to start?

Could someone help me?

Also, any other feedback would be greatly appreciated!

Thank you all in advance!







Code: [Select]
<?php

    
//Enable full error reporting
    //error_reporting(E_ALL);

    /*//////General gallery includes, variables, etc.//////////////////////////////////////////////////////////// */

    
include_once "gallery.configurations.inc.php";
    include_once 
"gallery.functions.inc.php";

    
//Getting required variables
    
$id $_GET['id'];
    
$album $_GET['album'];   
    
$page $_GET['page'];
        if (!
$page) {$page 1;}
    
$image $_GET['image'];

    
/*////////////////////////////////////////////////////////////////////////////////////////
    //
    //  Database Name: Gallery
    //  Database Structure
    // 
    //  Albums Table: id  ||  full_title  ||  short_title  ||  dir  ||  timestamp
    //                 |
    //                 +----------------------------------------------------+
    //                                                                      |
    //  Images Table: id  ||  filename  ||  caption date  ||  location  || album  ||  timestamp
    //                                                                      |
    //                                                   +------------------+
    //                                                   |
    //  Videos Table: id  ||  filename  ||  title  ||  album  ||  timestamp
    //
    ////////////////////////////////////////////////////////////////////////////////////////*/

    //Connecting to MySQL gallery database
    
$mysql_server_connection_1 mysql_connect($mysql_server_1,$mysql_server_1_username,$mysql_server_1_password,new_link);
    
mysql_select_db($gallery_database$mysql_server_connection_1);

    
//Will create tables (as outlined above), if not already present
    
include_once "action_table_generation.inc.php";

    
//Gathering information and running actions for the pages with multiple photo and videos
    
if ($album != ""){

        
//If there is no album directory by that name, will send the user to an error page
        
if (($album != "") AND (!file_exists("$server_photo_album_location/$album/")) AND (!file_exists("$server_video_album_location/$album/"))) {
            
header("Location:http://www.example.com/home/error");}

        
//If the photo album is restricted by .htaccess, will also add authentication as well
        
if ((file_exists("$server_photo_album_location/$album/.htaccess")) OR (file_exists("$server_video_album_location/$album/.htaccess"))) {
            
general_permissions();}
    
        
/* Will determine the number of photos to display based on a default value or on a user selected
           value from a form at the bottom of the page main album page
        */

        
if (!$_POST["number_displayed"]) {
            if (!
$_SESSION['number_displayed']) {
                
$number_displayed 2;
                } else {
                
$number_displayed $_SESSION['number_displayed'];
                }
            } else {
                
$number_displayed $_POST["number_displayed"];
                
$_SESSION['number_displayed'] = $number_displayed;
            }
    
        
//Bringing in album information from MySQL gallery database, album table
        
$album_query "SELECT * FROM albums WHERE dir = '$album'";
        
$album_results mysql_fetch_array(mysql_query($album_query,$mysql_server_connection_1));
            
$album_id $album_results['id'];
            
$full_title $album_results['full_title'];
    
        
/*//////Adding any new images to the MySQL gallery database//////////////////////////////////////////// */
    
        
$number_of_image_files count(glob("$server_photo_album_location/$album/*.*"));
        
$number_of_mysql_image_entries mysql_num_rows(mysql_query("SELECT * FROM images WHERE album = '$album_id'",$mysql_server_connection_1));
        if (
$number_of_image_files != $number_of_mysql_image_entries) {
            include_once 
"action_add_images.inc.php";}
    
        
/*//////Retrieving the total number of photos and photo pages////////////////////////////////////////// */
    
        //Finding the total number of images for any particular album from MySQL gallery database, images table
        
$image_query "SELECT * FROM images WHERE album = $album_id";
        
$number_of_images mysql_num_rows(mysql_query($image_query,$mysql_server_connection_1));
    
        
//Calculating total number of photo pages
        
$number_of_photo_pages ceil($number_of_images $number_displayed);
    
        
/*//////Generating image thumbnails if not already done//////////////////////////////////////////////// */

        
$number_of_thumbnail_files count(glob("$server_photo_thumbnail_location/$album/*.*"));     
        if (
$number_of_thumbnail_files != ($number_of_images 2)) {
            include_once 
"action_thumbnail_generation.inc.php";}

        
/*//////Adding any new videos to the MySQL gallery database//////////////////////////////////////////// */
    
        
$number_of_video_files count(glob("$server_video_album_location/$album/*.*"));
        
$number_of_mysql_video_entries mysql_num_rows(mysql_query("SELECT * FROM videos WHERE album = '$album_id'",$mysql_server_connection_1));
        if (
$number_of_video_files != $number_of_mysql_video_entries) {
            include_once 
"action_add_videos.inc.php";}
    
        
/*//////Retrieving the total number of videos and video pages////////////////////////////////////////// */
    
        //Finding the total number of videos for any particular album from MySQL gallery database, videos table
        
$query "SELECT * FROM videos WHERE album = $album_id";
        
$number_of_videos mysql_num_rows(mysql_query($query,$mysql_server_connection_1));
    
        
//Calculating total number of video pages
        
$number_of_video_pages ceil($number_of_videos $number_displayed);
    
    }

    
//Gathering information and running actions for the pages with a single photo
    
if ($image != "") {

        
//Retrieving query of image for any one particular page
        
$query "SELECT * FROM images WHERE album = $album_id ORDER BY filename ASC LIMIT $image, 1";
        
$specific_image mysql_query($query,$mysql_server_connection_1);
    
        while(
$row mysql_fetch_array($specific_image)) {
            
$image_id $row['id'];
            
$filename $row['filename'];
            
$caption stripslashes($row['caption']);
            
$date stripslashes($row['date']);
            
$location stripslashes($row['location']);}

    }

?>


Offline keeB

  • Nick Stinemates
  • PHPFreaks Recommended
  • Devotee
  • *
  • Gender: Male
  • I'm no good at this
    • View Profile
    • A little dash of life..
Re: Changing script over from procedural to OOP...
« Reply #1 on: March 13, 2008, 04:13:43 PM »
Break everything in to a 'piece' of the puzzle.

Code: [Select]
<?php

class AlbumDao {
      private 
$dbconnection;

      public function 
getImageByName($dir) {
          
//find an image in a certain directory;
      
}

      public function 
getImageById($id) {
             
//select * from images where id = id
      
}
      
      public function 
initialize($albumName) {
            
// select * from album a 
            // inner join images i on i.album = a.id
            // inner join videos v on v.album = a.id
            // where album.short_tile = '$albumName'
      
}
      public function 
setDbConnection(DatabaseConnection $dbconnection) {$this->dbconnection $dbconnection;}
}


class 
Album {
    private 
$name;
    private 
$title;
    private 
$dir;

    private 
$dao;

    public function 
__construct($name) { 
          
$this->name $name
          
$this->init();
    }


    private 
init() {
          
$this->dao->initialize($this->name);
    }

    public function 
getImageList() {
        
// some helper functions which return a list of Image objects
    
}

    public function 
getVideoList() {
       
// some other helper functions
    
}

    public function 
setDao(AlbumDao $dao) {$this->dao $dao}

}

class 
Image {
     private 
$filename;
     private 
$caption;

     public function 
__construct() { }
     
     public function 
draw() {
          
// echo <img src ....
     
}
}
?>



I just wrote this object diagram as I went along, there could be some oversights, but this is how I would start.
« Last Edit: March 13, 2008, 04:14:48 PM by keeB »
Come visit my site to see my latest projects
http://nick.stinemates.org/wordpress/

Offline cgm225Topic starter

  • Enthusiast
    • View Profile
Re: Changing script over from procedural to OOP...
« Reply #2 on: March 13, 2008, 04:54:28 PM »
Thank you so much for your help! 

What does DAO stand for?

Offline cgm225Topic starter

  • Enthusiast
    • View Profile
Re: Changing script over from procedural to OOP...
« Reply #3 on: March 13, 2008, 04:56:10 PM »
Also, could you give me an example of how you would use the code you posted after all the classes had been included in a script?


Offline keeB

  • Nick Stinemates
  • PHPFreaks Recommended
  • Devotee
  • *
  • Gender: Male
  • I'm no good at this
    • View Profile
    • A little dash of life..
Re: Changing script over from procedural to OOP...
« Reply #4 on: March 13, 2008, 09:46:17 PM »
DAO = Data Access Object

http://en.wikipedia.org/wiki/Data_Access_Object

Here's a snippet of how I would display some family album.
Code: [Select]
<?php
$configuration 
Configurator::getInstance(); // reads the configuration file which contains database info, paths, etc
$db = new DatabaseFactory::getInstance($configuration->database->getType());

$album = new Album("Family Photos"); // who knows where you got the name from. Maybe a script which printed all listed albums.
$a->setDao($db); // make sure it's using our database

echo "Photo's in this album";

foreach(
$a->getImageList() as $images) {
      
$image->draw();
}

echo 
"Videos in this album";
foreach(
$a->getVideoList() as $video) {
      
$video->draw();
}

?>

Come visit my site to see my latest projects
http://nick.stinemates.org/wordpress/

Offline cgm225Topic starter

  • Enthusiast
    • View Profile
Re: Changing script over from procedural to OOP...
« Reply #5 on: March 14, 2008, 10:17:14 AM »
Thank you.. and I am sorry about all the follow-up questions..

but could you talk a little more about the initialize function and what it does/ how to code that?

Thank you again!

Offline keeB

  • Nick Stinemates
  • PHPFreaks Recommended
  • Devotee
  • *
  • Gender: Male
  • I'm no good at this
    • View Profile
    • A little dash of life..
Re: Changing script over from procedural to OOP...
« Reply #6 on: March 14, 2008, 03:57:29 PM »
Sure.

It initializes the AlbumList object with references to constructed Images and Videos. Think of what an album is. It's a collection of things with a given name. So, that's the way I have modeled it.

Depending on how abstract you want to make your app, AlbumDao.initialize() could return raw information (a ResultSet or something along those lines) from the database, or, it could [the more OOP way] return Image and Video objects which the AlbumList stores however it sees fit.
Come visit my site to see my latest projects
http://nick.stinemates.org/wordpress/

PHP Freaks Forums

« on: »

Tired of these ads? Purchase a supporter subscription to get rid of them.