<?php
#-------------------------------------------------------------------------
# Module: RormContent
# Author: Robert Campbell <rob@techcom.dyndns.org>
#
#-------------------------------------------------------------------------
# CMS - CMS Made Simple is (c) 2005 by Ted Kulp (wishy@cmsmadesimple.org)
# This projects homepage is: http://www.cmsmadesimple.org
#
#-------------------------------------------------------------------------
#
# 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
# Or read it online: http:	//www.gnu.org/licenses/licenses.html#GPL
#
#-------------------------------------------------------------------------
#-------------------------------------------------------------------------

namespace HitCounter;

// a dumb class to allow me to maintain a list of strings
class StringList
{
    var $id;
    var $data;
    var $add;
    var $remove;
    var $name;
    var $size;
    var $maxlength;

    public function __construct( $id, $name )
    {
        $this->id = $id;
        $this->name = $name;
    }

    function setAdd( $add )
    {
        $this->add = $add;
    }

    function setData( $array )
    {
        $this->data = $array;
    }

    function setMaxlength( $maxlength )
    {
        $this->maxlength = $maxlength;
    }

    function setRemove( $remove )
    {
        $this->remove = $remove;
    }

    function setSize( $size )
    {
        $this->size = $size;
    }

    function display()
    {
        $str = "";
        if( count($this->data ) ) {
            foreach( $this->data as $elem )	{
                if( $elem == '' ) continue;
                $str.= $elem.'&nbsp;<input type="hidden" name="'.$this->id.$this->name.'_hidden_'.$elem.'" value="'.$elem.'" />';
                $str.='<input type="submit" name="'.$this->id.$this->name.'_remove_'.$elem.'" value="'.$this->remove.'" />';
                $str.="<br/>\n";
            }
        }
        $str .= '<input type="text" name="'.$this->id.$this->name.'_text" value="" size="'.$this->size.'" maxlength="'.$this->maxlength.'" />';
        $str .= '<input type="submit" name="'.$this->id.$this->name.'_add" value="'.$this->add.'" />';
        $str.="<br/>\n";
        return $str;
    }

    function getData()
    {
        return $this->data;
    }

    function handler( $params )
    {
        // setup the data
        $this->data = array();
        foreach( $params as $key => $value ) {
            if( preg_match( '/'.$this->name.'_hidden_/', $key ) ) $this->data[] = $value;
        }

        // handle an add
        if( isset( $params[$this->name.'_add'] ) ) $this->data[] = $params[$this->name.'_text'];

        foreach( $params as $key => $val ) {
            if( preg_match( '/'.$this->name.'_remove_/', $key ) ) {
                $itm = substr( $key, strlen($this->name.'_remove_') );
                // we're removing something
                unset($this->data[array_search($itm,$this->data)]);
            }
        }
    }
}

?>