<?php
/**
 * Joomla! 1.5 component NS Pro
 *
 * @version $Id: nspro.php 2009-04-28 01:47:12 svn $
 * @author Christopher Mavros
 * @package Joomla
 * @subpackage NS Pro
 * @license GNU/GPL
 *
 * Pro Version of Newsletter Subscriber.
Provides:
Keep subscriptions in database
Unsubscription Page
Optional Confirmation on subscribe/unsubscribe
Subscription Manager
Send Newsletters
Newsletter Manager
Subscription Statistics
Unique Emails (does not allow duplicates)
 *
 * This component file was created using the Joomla Component Creator by Not Web Design
 * http://www.notwebdesign.com/joomla_component_creator/
 *
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

// Import Joomla! libraries
jimport('joomla.application.component.model');

class NsproModelListEdit extends JModel {
    function __construct()
    {
        parent::__construct();

        $array = JRequest::getVar('cid',  0, '', 'array');
        $this->setId((int)$array[0]);
    }

    /**
     * Method to set the NsLists identifier
     *
     * @access  public
     * @param   int NsLists identifier
     * @return  void
     */
    function setId($id)
    {
        // Set id and wipe data
        $this->_id      = $id;
        $this->_data    = null;
    }

    function getList()
    {
        $db =& JFactory::getDBO();
        // Load the data
        if (empty( $this->_data )) {
            $query = ' SELECT * FROM #__nspro_lists '.
                    '  WHERE id = '.$this->_id;
            $db->setQuery( $query );
            $this->_data = $db->loadObject();
        }
        if (!$this->_data) {
            $this->_data = new stdClass();
            $this->_data->id = 0;
            $this->_data->lName  = null;
            $this->_data->published = false;
            $this->_data->notes = null;
        }
        return $this->_data;
    }

    function getListById($id)
    {
        $db =& JFactory::getDBO();

        $query = ' SELECT * FROM #__nspro_lists '.
                 '  WHERE id = '.$id;
        $db->setQuery( $query );
        $ldata = $db->loadObject();
        return $ldata;
    }

    function getLists($limit, $limitstart) {
        $db =& JFactory::getDBO();

        $query = 'SELECT * FROM #__nspro_lists';

        $listss = $this->_getList( $query , $limitstart, $limit);

        return $listss;
    }

    function getListCount() {
        $db =& JFactory::getDBO();

        $query = 'SELECT COUNT(*) FROM #__nspro_lists';

        $db->setQuery( $query );
        $lCount = $db->loadResult();

        return $lCount;
    }

    /**
     * Method to store a record
     *
     * @access  public
     * @return  boolean True on success
     */
    function store()
    {
        $db =& JFactory::getDBO();
        $row =& $this->getTable('nslists');
        $data = JRequest::get( 'post' );

        // Bind the form fields to the nsnewsletters table
        if (!$row->bind($data)) {
            $this->setError($db->getErrorMsg());
            return false;
        }

        // Make sure the nsnewsletters records are valid
        if (!$row->check()) {
            $this->setError($db->getErrorMsg());
            return false;
        }

        // Store the nsnewsletters table to the database
        if (!$row->store()) {
            $this->setError( $row->getErrorMsg() );
            return false;
        }

        return true;
    }

    /**
     * Method to delete record(s)
     *
     * @access  public
     * @return  boolean True on success
     */
    function delete()
    {
        $cids = JRequest::getVar( 'cid', array(0), 'post', 'array' );

        $row =& $this->getTable('nslists');

        if (count( $cids ))
        {
            foreach($cids as $cid) {
                if (!$row->delete( $cid )) {
                    $this->setError( $row->getErrorMsg() );
                    return false;
                }
            }
        }
        return true;
    }
}
?>