Slooze PHP Cross Reference Image Galleries

Source: /src/slooze_ct_csv.php - 380 lines - 12917 bytes - Summary - Text - Print

   1  <?php  /* -*- Mode: C++; indent-tabs-mode: nil; c-basic-indent: 2 -*- */
   2  /*

   3   * Slooze PHP Web Photo Album

   4   * Copyright (c) 2000 Slooze developers (see AUTHORS file)

   5   * $Id: slooze_ct_csv.php,v 1.16 2006/06/28 05:37:34 mdkendall Exp $

   6   *

   7   * Container (Ct) class holding the photo data. Exactly where the data is

   8   * stored depends on which Ct class you use. In this implementation, the data

   9   * is held in CSV files reached via an encapsulating class.

  10   *

  11   * Function naming convention:

  12   * getBars()          SELECT * FROM Bars

  13   * getBarsInFoo($foo) SELECT * FROM Bars WHERE (Foo = $foo)

  14   * getBar($barID)     SELECT * FROM Bars WHERE (BarID = $barID) BarID is primary key

  15   *

  16   * getBars...() returns $bar[rows][fields]

  17   * getBar()     returns $bar[fields]

  18   *

  19   * addBar($bar)       INSERT INTO Bars VALUES $bar

  20   * updateBar($bar)    UPDATE Bars SET * = $bar WHERE (BarID = $bar[BarID])

  21   * deleteBar($barID)  DELETE FROM Bars WHERE (BarID = $barID)

  22   *

  23   */
  24  
  25  class SloozeCtCsv {
  26  
  27    /* Redefine this parameter by deriving your own class */

  28    var $csvClass = "Csv";  /* type of csv file data store */
  29    var $csv;               /* csv file data store */
  30    var $errString;         /* if an error occurs, set this to the error */
  31  
  32    /* public: constructor */

  33    function SloozeCtCsv() {
  34      $name = $this->csvClass;
  35      $this->csv = new $name;
  36    }
  37  
  38    /* public: resetError() makes errString empty so errors don't carry over */

  39    function resetError() {
  40      $this->errString = "";
  41    }
  42  
  43    /* public: getError() returns errString */

  44    function getError() {
  45      return $this->errString;
  46    }
  47  
  48    /* public: getTopics() returns all the topics.

  49     * Logically: SELECT * FROM Topics */
  50    function getTopics() {
  51      return( $this->csv->read( 'topics' ) );
  52    }
  53  
  54    /* public: getTopicsInParentTopic() returns all the topics with the

  55     * given parent TopicID. Slash / is a magic TopicID meaning root.

  56     * Logically: SELECT * FROM Topics WHERE ( (ParentTopicID = $parentTopicID)

  57     * and (TopicID != '/') ) */
  58    function getTopicsInParentTopic( $parentTopicID = "/" ) {
  59      $allTopics = $this->getTopics();
  60      $topics = array();
  61      reset($allTopics);
  62      while( list($key, $topic) = each($allTopics) ) {
  63        if( ($topic['ParentTopicID'] == $parentTopicID) &&
  64            ($topic['TopicID'] != "/") ) {
  65          $topics[] = $topic;
  66        }
  67      }
  68      return( $topics );
  69    }
  70  
  71    /* public: getTopic() returns a single topic.

  72     * Logically: SELECT * FROM Topics WHERE (TopicID = $topicID) */
  73    function getTopic( $topicID ) {
  74      $allTopics = $this->getTopics();
  75      reset($allTopics);
  76      while( list($key, $topic) = each($allTopics) ) {
  77        if( $topic['TopicID'] == $topicID ) {
  78          return( $topic );
  79        }
  80      }
  81      return FALSE;
  82    }
  83  
  84    /* public: addTopic() adds one single topic */

  85    function addTopic( $topic ) {
  86      if (is_array($topic) && ($topic['TopicID'] != "") && !$this->getTopic($topic['TopicID'])) {
  87        if (is_array($topics = $this->csv->read('topics'))) {
  88          $topics[] = $topic;
  89          return($this->csv->write('topics', $topics));
  90        }
  91        return FALSE; /* read failed */
  92      }
  93      return FALSE; /* null topic or TopicID, or TopicID already exists */
  94    }
  95  
  96    /* public: deleteTopic() deletes one single topic */

  97    function deleteTopic( $topicID ) {
  98      if (is_array($topics = $this->csv->read('topics'))) {
  99        $newTopics = array();
 100        reset($topics);
 101        while( list($key, $topic) = each($topics)) {
 102          if($topic['TopicID'] != $topicID) {
 103            $newTopics[] = $topic;
 104          }
 105        }
 106        return($this->csv->write('topics', $newTopics));
 107      }
 108      return FALSE; /* failed */
 109    }
 110  
 111    /* public: updateTopic() updates one single topic */

 112    function updateTopic( $topic ) {
 113      if ($this->deleteTopic($topic['TopicID'])) {
 114        return ($this->addTopic($topic));
 115      }
 116      return FALSE; /* delete failed */
 117    }
 118  
 119    /* public: getRolls() returns all the rolls.

 120     * Logically: SELECT * FROM Rolls */
 121    function getRolls() {
 122      return( $this->csv->read( 'rolls' ) );
 123    }
 124  
 125    /* public: addRoll() adds one single roll */

 126    function addRoll( $roll ) {
 127      if (is_array($roll) && $roll['RollID'] != "") {
 128        if (is_array($rolls = $this->csv->read('rolls'))) {
 129          $rolls[] = $roll;
 130          return($this->csv->write('rolls', $rolls));
 131        }
 132        return FALSE; /* read failed */
 133      }
 134      return FALSE; /* null roll or RollID */
 135    }
 136  
 137    /* public: deleteRoll() deletes one single roll */

 138    function deleteRoll( $rollID ) {
 139      if (is_array($rolls = $this->csv->read('rolls'))) {
 140        $newRolls = array();
 141        reset($rolls);
 142        while( list($key, $roll) = each($rolls)) {
 143          if($roll['RollID'] != $rollID) {
 144            $newRolls[] = $roll;
 145          }
 146        }
 147        return($this->csv->write('rolls', $newRolls));
 148      }
 149      return FALSE; /* failed */
 150    }
 151  
 152    /* public: updateRoll() updates one single roll */

 153    function updateRoll( $roll ) {
 154      if ($this->deleteRoll($roll['RollID'])) {
 155        return ($this->addRoll($roll));
 156      }
 157      return FALSE; /* delete failed */
 158    }
 159  
 160    /* public: getPictures() returns all the pictures.

 161     * Logically: SELECT * FROM Pictures */
 162    function getPictures() {
 163      return( $this->csv->read( 'pictures' ) );
 164    }
 165  
 166    /* public: getPicturesInRoll() returns all the Pictures in a given Roll.

 167     * Logically: SELECT * FROM Pictures WHERE (RollID = $rollID) */
 168    function getPicturesInRoll( $rollID ) {
 169      $allPictures = $this->getPictures();
 170      $pictures = array();
 171      reset($allPictures);
 172      while( list($key, $picture) = each($allPictures) ) {
 173        if( $picture['RollID'] == $rollID ) {
 174          $pictures[] = $picture;
 175        }
 176      }
 177      return( $pictures );
 178    }
 179  
 180    /* getPicturesInTopic() returns all the Pictures in a given Topic.

 181     * Logically: SELECT * FROM Pictures WHERE (ParentTopicID = $topicID) */
 182    function getPicturesInTopic( $topicID ) {
 183      $allPictures = $this->getPictures();
 184      $pictures = array();
 185      reset($allPictures);
 186      while( list($key, $picture) = each($allPictures) ) {
 187        if( $picture['ParentTopicID'] == $topicID ) {
 188          $pictures[] = $picture;
 189        }
 190      }
 191      return( $pictures );
 192    }
 193  
 194    /* getPicturesInSearch() returns all the Pictures where the given

 195     * search term occurs in the Description, or in an associated comment.

 196     * Logically: SELECT * FROM Pictures WHERE (Description LIKE $search)

 197     * plus SELECT Pictures.* FROM Pictures LEFT JOIN Comments

 198     *      ON (Pictures.FrameID = Comments.FrameID AND Pictures.RollID = Comments.RollID)

 199     *      WHERE (Comments.Comment LIKE $search) */
 200    function getPicturesInSearch( $search ) {
 201      $pictures = array();
 202      if( $search != "") {
 203        /* search description */

 204        $allPictures = $this->getPictures();
 205        reset($allPictures);
 206        while( list($key, $picture) = each($allPictures) ) {
 207          if( stristr($picture['Description'], $search) ) {
 208            $pictures[] = $picture;
 209          }
 210        }
 211        /* search comments */

 212        $allComments = $this->getComments();
 213        reset($allComments);
 214        while( list($key, $comment) = each($allComments) ) {
 215          if( stristr($comment['Comment'], $search) ) {
 216            $pictures[] = $this->getPicture($comment['RollID'], $comment['FrameID']);
 217          }
 218        }
 219        /* eliminate duplicates */

 220        $pictures = $this->uniquePictures($pictures);
 221      }
 222      return( $pictures );
 223    }
 224  
 225    /* getPicture() returns a single picture.

 226     * Logically: SELECT * FROM Pictures WHERE (RollID = $rollID && FrameID = $frameID) */
 227    function getPicture( $rollID, $frameID ) {
 228      $allPictures = $this->getPictures();
 229      reset($allPictures);
 230      while( list($key, $picture) = each($allPictures) ) {
 231        if( $picture['RollID'] == $rollID && $picture['FrameID'] == $frameID) {
 232          return( $picture );
 233        }
 234      }
 235      return FALSE;
 236    }
 237  
 238    /* public: addPicture() adds one single picture */

 239    function addPicture( $picture ) {
 240      if (is_array($pictures = $this->csv->read('pictures'))) {
 241        $pictures[] = $picture;
 242        return($this->csv->write('pictures', $pictures));
 243      }
 244      return FALSE; /* read failed */
 245    }
 246  
 247    /* public: deletePicture() deletes one single picture */

 248    function deletePicture( $rollID, $frameID ) {
 249      if (is_array($pictures = $this->csv->read('pictures'))) {
 250        $newPictures = array();
 251        reset($pictures);
 252        while( list($key, $picture) = each($pictures)) {
 253          if(!(($picture['RollID'] == $rollID) &&
 254               ($picture['FrameID'] == $frameID))){
 255            $newPictures[] = $picture;
 256          }
 257        }
 258        return($this->csv->write('pictures', $newPictures));
 259      }
 260      return FALSE; /* read failed */
 261    }
 262  
 263    /* public: updatePicture() updates one single picture */

 264    function updatePicture( $picture ) {
 265      if( $allPictures = $this->getPictures() ) {
 266        reset($allPictures);
 267        while( list($currentkey, $currentpicture) = each($allPictures) ) {
 268          if( $currentpicture['RollID'] == $picture['RollID'] &&
 269              $currentpicture['FrameID'] == $picture['FrameID']) {
 270            while( list($key, $val) = each($picture) ) {
 271              $allPictures[$currentkey][$key] = $picture[$key];
 272            }
 273          }
 274        }
 275        return($this->csv->write('pictures', $allPictures));
 276      } else {
 277        return FALSE; /* getPictures failed */
 278      }
 279    }
 280  
 281    /* public: incrementPictureViews() add one to Views field of picture */

 282    function incrementPictureViews( $rollID, $frameID, $picture ) {
 283      $picture['Views'] += 1;
 284      $this->updatePicture($picture);
 285      return TRUE;
 286    }
 287  
 288    /* public: getComments() returns all the comments.

 289     * Logically: SELECT * FROM Comments */
 290    function getComments() {
 291      return( $this->csv->read( 'comments' ) );
 292    }
 293  
 294    /* getCommentsInPicture() returns all the Comments for the given picture.

 295     * Logically: SELECT * FROM Comments WHERE (RollID = $rollID && FrameID = $frameID) */
 296    function getCommentsInPicture( $rollID, $frameID ) {
 297      $comments = array();
 298      $allComments = $this->getComments();
 299      reset($allComments);
 300      while( list($key, $comment) = each($allComments) ) {
 301        if( $comment['RollID'] == $rollID && $comment['FrameID'] == $frameID) {
 302          $comments[] = $comment;
 303        }
 304      }
 305      return( $comments );
 306    }
 307  
 308    /* public: addComment() adds one single comment */

 309    function addComment( $comment ) {
 310      if (is_array($comments = $this->csv->read('comments'))) {
 311        $comments[] = $comment;
 312        return($this->csv->write('comments', $comments));
 313      }
 314      return FALSE; /* read failed */
 315    }
 316  
 317    /* public: deleteComment() deletes one single comment */

 318    function deleteComment( $commentID ) {
 319      if (is_array($comments = $this->csv->read('comments'))) {
 320        $newComments = array();
 321        reset($comments);
 322        while( list($key, $comment) = each($comments)) {
 323          if($comment['CommentID'] != $commentID) {
 324            $newComments[] = $comment;
 325          }
 326        }
 327        return($this->csv->write('comments', $newComments));
 328      }
 329      return FALSE; /* failed */
 330    }
 331  
 332    /* public: addRating() adds a rating */

 333    function addRating( $rating ) {
 334      if (is_array($ratings = $this->csv->read('ratings'))) {
 335        $ratings[] = $rating;
 336        $this->csv->write('ratings', $ratings);
 337        /* calculate the new rating for the picture */

 338        reset($ratings);
 339        $count = 0; $total = 0;
 340        while( list(,$oldRating) = each($ratings) ) {
 341          if ( $oldRating['RollID'] == $rating['RollID'] &&
 342               $oldRating['FrameID'] == $rating['FrameID'] ) {
 343            $count += 1;
 344            $total += $oldRating['Rating'];
 345          }
 346        }
 347        $score = ($count>0) ? $total/$count : 0;
 348        /* update the picture */

 349        $picture = $this->getPicture($rating['RollID'], $rating['FrameID']);
 350        $picture['Rating'] = $score;
 351        $this->updatePicture($picture);
 352      }
 353      return FALSE; /* read failed */
 354    }
 355  
 356    /* public: checkDataVersion() makes sure our database/csv has the right structure,

 357       and maybe fixes it if it doesn't */
 358    function checkDataVersion() {
 359    }
 360  
 361    /* private: uniquePictures() eliminates duplicates from a set of pictures */

 362    function uniquePictures($pictures) {
 363      if (function_exists('in_array')) {
 364        $newPictures = array();
 365        $frames = array();
 366        while( list(, $picture) = each($pictures) ) {
 367          if (!in_array($picture['RollID'].'/'.$picture['FrameID'], $frames)) {
 368            $frames[] = $picture['RollID'].'/'.$picture['FrameID'];
 369            $newPictures[] = $picture;
 370          }
 371        }
 372        return $newPictures;
 373      } else {
 374        /* fallback if PHP3 and no in_array function */

 375        return $pictures;
 376      }
 377    }
 378  
 379  } /* end of class SloozeCtCsv */
 380  ?>

title

Description

title

Description

title

Description

title

title

Body