PHP Classes

File: doc/examples/website/models/model.movie.php

Recommend this page to a friend!
  Classes of Kristo Vaher  >  Wave Framework  >  doc/examples/website/models/model.movie.php  >  Download  
File: doc/examples/website/models/model.movie.php
Role: Class source
Content type: text/plain
Description: Example Model
Class: Wave Framework
MVC framework for building Web sites and APIs
Author: By
Last change: It is now possible to return picture files and resource files as BASE64 encoded if you provide 'base64' parameter as a dynamic URL parameter. Added 'url-absolute' and 'url-relative' to the already existing 'request-url' variables that the default URL Controller returns to the View. Added 'output' return type that simply returns the whatever is stored in response arrays 'www-data' key. When using getState() method on a variable that does not exist, then it generates a notice in debugger log. Added an option to restrict URL's based on user agent being matched, this allows to create mobile specific redirects and other user agent specific features. Some State variable names have been changed to clean up the State namespace, this can break backwards compatibility. Chech the full list at update documentation. Updated jQuery to 1.9.0 release. Added isDeveloper() call and developer-ip and developer-user-agent configuration settings. Fixed a bug with Javascript API Wrapper parallel form requests.
Date: 8 years ago
Size: 5,411 bytes
 

Contents

Class file image Download
<?php

/**
 * Website Tutorial <http://www.waveframework.com>
 * Tutorial Movie Model
 *
 * It is recommended to extend View classes from WWW_Factory in order to
 * provide various useful functions and API access for the view.
 *
 * @package Factory
 * @author Kristo Vaher <kristo@waher.net>
 * @copyright Copyright (c) 2012, Kristo Vaher
 * @license Unrestricted
 * @tutorial /doc/pages/tutorial_website.htm
 * @since 1.0.0
 * @version 1.0.0
 */

class WWW_model_movie extends WWW_Factory {

   
/**
     * It is recommended to define all data variables here. Usually the
     * data variables have the same names as the column names of database
     * rows from a table.
     */
   
public $id=0;
    public
$title;
    public
$year;
   
   
/**
     * This function simply sets the title of the movie
     *
     * @param string $title movie title
     * @return boolean
     */
   
public function setTitle($title){
       
$this->title=$title;
        return
true;
    }
   
   
/**
     * This function simply sets the year of the movie
     *
     * @param string $year movie year
     * @return boolean
     */
   
public function setYear($year){
       
$this->year=$year;
        return
true;
    }
   
   
/**
     * This loads a movie based on its ID
     *
     * This example uses simple serialized database in filesystem, but it
     * could load data from MySQL or other databases
     *
     * @param string $id movie ID
     * @return array if success, boolean if fails
     */
   
public function loadMovie($id=0){
        if(
$id!=0){
           
// Database location
           
$dbLoc=$this->getState('directory-data').'movies.db';
           
// Making sure that current database exists
           
if(file_exists($dbLoc)){
               
$curDb=unserialize(file_get_contents($dbLoc));
            } else {
                return
false;
            }
           
// If this movie exists in the database, we assign its values to
            // current object.
           
if(isset($curDb[$id])){
               
$this->id=$id;
               
$this->title=$curDb[$id]['title'];
               
$this->year=$curDb[$id]['year'];
                return array(
                   
'id'=>$id,
                   
'title'=>$curDb[$id]['title'],
                   
'year'=>$curDb[$id]['year']
                    );
            } else {
                return
false;
            }
        } else {
            return
false;
        }
    }

   
/**
     * This loads all movies from database into returned value
     *
     * This example uses simple serialized database in filesystem, but it
     * could load data from MySQL or other databases
     *
     * @return array
     */
   
public function loadAllMovies(){
       
// Database location
       
$dbLoc=$this->getState('directory-data').'movies.db';
       
// Making sure that current database exists
       
if(file_exists($dbLoc)){
           
$curDb=unserialize(file_get_contents($dbLoc));
        } else {
           
// Since database does not exist (and thus movies don't exist) we return empty array instead of false
           
return array();
        }
       
// We store all movie data in a separate array that will be returned
       
$allMovies=array();
        foreach(
$curDb as $id=>$data){
           
$movie=array();
           
$movie['id']=$id;
           
$movie['title']=$data['title'];
           
$movie['year']=$data['year'];
           
$allMovies[]=$movie;
        }
        return
$allMovies;
    }

   
/**
     * This saves the current movie in database
     *
     * This example uses simple serialized database in filesystem, but it
     * could load data from MySQL or other databases
     *
     * @return integer for movie ID if success, false if fails
     */
   
public function saveMovie(){
       
// Making sure that title and year are both set
       
if($this->title!='' && $this->year!=''){
           
// Database location
           
$dbLoc=$this->getState('directory-data').'movies.db';
           
// If database file already exists, we simply load the database and unserialize its data to add a new movie to it
           
if(file_exists($dbLoc)){
               
$curDb=unserialize(file_get_contents($dbLoc));
               
// ID's are indexes in the stored array, so we seek the highest index in that array
               
$nextId=max(array_keys($curDb))+1;
            } else {
               
// Since database did not exist, an array is created for new database
               
$curDb=array();
               
$nextId=1;
            }
           
// Creating data node of current movie
           
$movie=array();
           
$movie['title']=$this->title;
           
$movie['year']=$this->year;
           
// Adding the new node into database array
           
$curDb[$nextId]=$movie;
           
// We overwrite the old database with the updated database with a new movie
           
if(file_put_contents($dbLoc,serialize($curDb))){
                return
$nextId;
            } else {
                return
false;
            }
        } else {
            return
false;
        }
    }

   
/**
     * This deletes movie with a specific ID from database
     *
     * This example uses simple serialized database in filesystem, but it
     * could load data from MySQL or other databases
     *
     * @param string $id movie ID
     * @return boolean
     */
   
public function deleteMovie($id=0){
       
// This function, if defined with an ID, deletes specific ID, otherwise it deletes currently active ID
       
if($id!=0){
           
$deleteId=$id;
        } else if(
$this->id!=0){
           
$deleteId=$this->id;
        } else {
           
// No ID was set in here nor in the current object
           
return false;
        }
       
// Database location
       
$dbLoc=$this->getState('directory-data').'movies.db';
       
// If database does not exist then we have no movies to delete
       
if(file_exists($dbLoc)){
           
$curDb=unserialize(file_get_contents($dbLoc));
        } else {
            return
false;
        }
       
// If such an ID exists in database, it is simply unset
       
if(isset($curDb[$deleteId])){
            unset(
$curDb[$deleteId]);
        } else {
            return
false;
        }
       
// We overwrite the old database with the updated database
       
if(file_put_contents($dbLoc,serialize($curDb))){
            return
true;
        } else {
            return
false;
        }
    }

}
   
?>

For more information send a message to info at phpclasses dot org.