Location: PHPKode > projects > Club video list website > animelists/showclass.php
<?php
/*
Animelists website
Copyright (C) 2002 Jeff Wartes

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program 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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

-------------------------------------------------

This file is not a stand-alone page, it provides no HTML whatsoever.

Defines the data associated with an entry in the list, and how
to handle that data.
This is done with two classes:
   Show: A single entry in the list
   ShowList: The entire list of shows

$Id: showclass.php,v 1.3 2002/10/20 19:20:59 pyreforge Exp $
*/

if (!isset($COMMONINCLUDED)){
    include("common.php");
}
$SHOWINCLUDED = true;

class Show {
    //File format: <title>|<key>|<username>|<DVD|VHS|VCD|LaserDisc>|<Dubbed|Subtitles|Both>|<Movie|Complete|Partial|<episodes owned>|<comments>|<(who voted for this)*>
    //note: number of eps can be other than an integer at the moment because of the partial series box
    //this could effect sorting.

    //showINDEX variables:
    //NOTE: The numbering order here MUST match the order in the Show->outputDataLine() function!
    var $TITLEINDEX = 0;
    var $KEYINDEX = 1;
    var $OWNERINDEX = 2;
    var $FORMATINDEX = 3;
    var $LANGUAGEINDEX = 4;
    var $COLLECTIONINDEX = 5;
    var $OWNEDINDEX = 6;
    var $COMMENTSINDEX = 7;
    var $VOTESINDEX = 8;
    var $YEARINDEX = 9;
    var $TIMEINDEX = 10;

    var $title = "";
    var $key = "";
    var $owner = "";
    var $format = "";
    var $language = "";
    var $collection = "";
    var $owned = "";
    var $comments = "&nbsp;";
    var $votes = "";         //stored here as an array of user/owner names, file is comma-delinated names
    var $year = "&nbsp;";
    var $time = "&nbsp;";

    var $delete = false; //prevents output functions from returning anything if set to true

    //Constructor. Stores the show's information in the object using a single string that
    //uses the $sep character to separate data types.
    function Show($line="", $sep=SEP){
        $line = trim($line);  //remove leading and trailing whitespace
        $fileData = explode($sep, $line);

        //used in the "if" tests below to prevent array subscript errors due to
        //improper $line parameter construction
        $fileDataSize = count($fileData);

        if($line){
            if ($this->TITLEINDEX < $fileDataSize) $this->title = $fileData[$this->TITLEINDEX];
            if ($this->KEYINDEX < $fileDataSize) $this->key = $fileData[$this->KEYINDEX];
            if ($this->OWNERINDEX < $fileDataSize) $this->owner = $fileData[$this->OWNERINDEX];
            if ($this->FORMATINDEX < $fileDataSize) $this->format = $fileData[$this->FORMATINDEX];
            if ($this->LANGUAGEINDEX < $fileDataSize) $this->language = $fileData[$this->LANGUAGEINDEX];
            if ($this->COLLECTIONINDEX < $fileDataSize) $this->collection = $fileData[$this->COLLECTIONINDEX];
            if ($this->OWNEDINDEX < $fileDataSize) $this->owned = $fileData[$this->OWNEDINDEX];
            if ($this->COMMENTSINDEX < $fileDataSize) $this->comments = $fileData[$this->COMMENTSINDEX];
            if ($this->VOTESINDEX < $fileDataSize) $this->votes = $this->explodeVotes($fileData[$this->VOTESINDEX]);
            if ($this->YEARINDEX < $fileDataSize) $this->year = $fileData[$this->YEARINDEX];
            if ($this->TIMEINDEX < $fileDataSize) $this->time = $fileData[$this->TIMEINDEX];
        }
        //else creates a Show with empty data
    }

    function outputDataLine($lineend=NEWLINE){
        //NOTE: This order MUST match the numbering order of the showINDEX variables!
        if (!$this->delete){
            return $this->title . SEP . $this->key . SEP . $this->owner . SEP . $this->format . SEP . $this->language . SEP . $this->collection . SEP . $this->owned . SEP . $this->comments . SEP . $this->implodeVotes() . SEP . $this->year . SEP . $this->time . $lineend;
        }
        return "";
    }

    function explodeVotes($votestr){
        $voters = explode(",", $votestr);
        //filter out blank entries
        foreach ($voters as $aVoter){
            if (trim($aVoter)){
                $votes[] = $aVoter;
            }
        }
        if (isset($votes)){
            return $votes;
        }
        else return "";
    }
    function implodeVotes(){  //assumed imploding $this->votes
        if (gettype($this->votes) == "array"){
            return Implode(',', $this->votes);
        }
        return "";
    }

    function countVotes(){
        if (gettype($this->votes) == "array"){
            return count($this->votes);
        }
        return "0";
    }
    function voterExists($user){
        if (gettype($this->votes) == "array"){
            foreach ($this->votes as $voter){
                if (strcasecmp(trim($voter), $user) == 0){
                    return true;
                }
            }
        }
        return false;
    }
    function addVoter($user){
        $this->votes[] = $user;
    }
    function removeVoter($user){
        //can't (don't know if possible) delete an array entry and preserve indexing
        //create a new array and overwrite
        $replacement = "";
        foreach ($this->votes as $aVote){
            if (strcasecmp(trim($aVote), $user) != 0){
                $replacement[] = $aVote;
            }
        }
        $this->votes = $replacement;
    }
    function compareByOwner($name){
        //this should use the same compare rules used in the User method compareByName()
        return (strcasecmp($this->owner,$name) == 0);
    }
}

function compareShows($first, $second){
    return strcasecmp($first->title, $second->title);
}

class ShowList {
    var $shows = array();

    function ShowList($filename){
        $fileLines = file($filename); //TODO, insert error checking code here

        //for each array index (a line in the file), explode the contents using the $sep
        foreach ($fileLines as $line){
            $line = Ltrim(rtrim($line));
            //remove comment lines and blank lines
            if ($line && !ereg("^;", $line)){
                $this->shows[] = new Show($line);
            }
        }
    }
    function addShow($newShow){
        $this->shows[] = $newShow;
    }

    //overwrites any old contents in the $filename.
    //NOTE: Pushing  a datafile thru this class deletes any comments and blank lines
    function outputListToFile($filename){
        $fp = fopen($filename, "w");
        if (!$fp) return false;
        fwrite($fp, $this->outputList());
        fclose($fp);
        return true;
    }

    function outputList($type=NEWLINE){
        $output = "";
        usort($this->shows, compareShows);   //TEMPORARY FIX: causes the list to be semi-sorted whenever the list is changed
        foreach($this->shows as $aShow){
            $output .= $aShow->outputDataLine();
            if ($type != NEWLINE){
                $output .= $type;
            }
        }
        return $output;
    }

    //returns a copy of the Show with the matching key
    function getByKey($key){
        foreach ($this->shows as $aShow){
            if ($key == $aShow->key){
                return $aShow;
            }
        }
        return false;
    }
    function replaceByKey($obj){
       for ($i = 0; $i < count($this->shows); $i++){
            if ($this->shows[$i]->key == $obj->key){
                record("Changing show entry: " . $this->shows[$i]->outputDataLine(" ") . " to: " . $obj->outputDataLine(" "));
                $this->shows[$i] = $obj;
                return true;
            }
        }
        return false;
    }
    function deleteByKey($key){
        for ($i = 0; $i < count($this->shows); $i++){
            if ($this->shows[$i]->key == $key){
                record("Deleting Show entry with key: " . $key);
                $this->shows[$i]->delete = true;
                return true;
            }
        }
        return false;
    }
    function deleteByOwner($name){
        record("Deleting all Show entries of user: " . $name);
        $entryfound = false;
        for ($i = 0; $i < count($this->shows); $i++){
            if ($this->shows[$i]->compareByOwner($name)){
                record("-->User deletion, removing show: \"" . $this->shows[$i]->title . "\" / " . $this->shows[$i]->key);
                $this->shows[$i]->delete = true;
                $entryfound = true;
            }
        }

        return $entryfound;
    }
}

?>
Return current item: Club video list website