<?php
/*
 * $RCSfile: GalleryPersistent.class,v $
 *
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2005 Bharat Mediratta
 *
 * 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., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
 */
/**
 * @version $Revision: 1.16 $ $Date: 2005/08/23 03:49:03 $
 * @package GalleryCore
 * @author Bharat Mediratta <bharat@menalto.com>
 */

/**
 * This member is unmodified
 */
define('MEMBER_UNMODIFIED', 0x00000000);

/**
 * This member is modified
 */
define('MEMBER_MODIFIED', 0x00000001);

/**
 * A class that contains information about the state of its members
 *
 * @package GalleryCore
 * @subpackage Classes
 */
class GalleryPersistent {

    /**
     * Keep track of which internal values are persistant and which ones aren't
     * so that we'll know when and what to save to the database.
     *
     * @var array $_persistentStatus
     * @access private
     */
    var $_persistentStatus;

    /*
     * ****************************************
     *                 Methods
     * ****************************************
     */

    /**
     * Constructor
     */
    function GalleryPersistent() {
	/*
	 * Initialize our persistent info tracker
	 */
	$this->_persistentStatus = new stdClass();
	$this->_persistentStatus->flags = 0;
	$this->_persistentStatus->modified = array();
	$this->_persistentStatus->originalValue = array();
    }

    /**
     * Return type info about this class's persistent members.
     *
     * This class as no persistent members, so return an empty array.
     * Subclasses will add data to the array.
     */
    function getPersistentMemberInfo() {
	return array();
    }

    /**
     * Get the data from this persistent object as an associative array
     *
     * @return array memberName => memberValue
     */
    function getMemberData() {
	return array('_className' => get_class($this));
    }

    /**
     * Have we modified any data in this class?
     *
     * @return bool true if modified, false if not.
     */
    function isModified() {
	return !empty($this->_persistentStatus->modified);
    }

    /**
     * Set the modified flag for a member
     *
     * @param string the name of the member
     * @param mixed the new field value
     */
    function setModifiedFlag($name, $newValue) {
	if (!array_key_exists($name, $this->_persistentStatus->originalValue)) {
	    $method = 'get' . $name;
	    $this->_persistentStatus->originalValue[$name] = $this->$method();
	}

	if ($this->_persistentStatus->originalValue[$name] === $newValue) {
	    unset($this->_persistentStatus->modified[$name]);
	} else {
	    $this->_persistentStatus->modified[$name] = MEMBER_MODIFIED;
	}
    }

    /**
     * Get the modification flag for a member
     */
    function getModifiedFlag($name) {
	if (empty($this->_persistentStatus->modified[$name])) {
	    return MEMBER_UNMODIFIED;
	} else {
	    return $this->_persistentStatus->modified[$name];
	}
    }

    /**
     * Get a list of all the members that have been modified
     *
     * @return array string key names
     */
    function getModifiedKeys() {
	return array_keys($this->_persistentStatus->modified);
    }

    /**
     * Reset all modification flags
     */
    function clearModifiedFlags() {
	$this->_persistentStatus->modified = array();
	$this->_persistentStatus->originalValue = array();
    }

    /**
     * Set a flag
     *
     * The GalleryStorage strategy uses this to flag objects for its own
     * internal purposes.
     *
     * @param string a bit flag to set
     */
    function setPersistentFlag($flag) {
	$this->_persistentStatus->flags |= $flag;
    }

    /**
     * Clear a flag
     *
     * The GalleryStorage strategy uses this to flag objects for its own
     * internal purposes.
     *
     * @param string a bit flag to clear
     */
    function clearPersistentFlag($flag) {
	$this->_persistentStatus->flags &= ~$flag;
    }

    /**
     * Test a flag
     *
     * The GalleryStorage strategy uses this to flag objects for its own
     * internal purposes.
     *
     * @param string a bit flag to test
     * @return bool true if the bit is on, false otherwise
     */
    function testPersistentFlag($flag) {
	return $this->_persistentStatus->flags & $flag;
    }

    /**
     * Truncate a string to the given size.
     *
     * @param string the input string
     * @param in the target max string size
     * @return the truncated string
     */
    function _truncateString($input, $size) {
	if ($input == '') {
	    return $input;
	}

	return GalleryUtilities::utf8Substring($input, 0, $size);
    }
}
?>
