<?php

    include_once( PLOG_CLASS_PATH."class/action/admin/adminaction.class.php" );
    include_once( PLOG_CLASS_PATH."class/view/admin/adminplugintemplatedview.class.php" );
    include_once( PLOG_CLASS_PATH."plugins/currentlyaddictedto/class/dao/cat_item.class.php" );

    foreach (glob(PLOG_CLASS_PATH . "/plugins/currentlyaddictedto/class/dao/cat_*.php") as $filename) {
        include_once($filename);
    }

    class PluginCurrentlyaddictedtoAction extends AdminAction {
        
        function PluginCurrentlyaddictedtoAction( $actionInfo, $request ) {
            $this->AdminAction( $actionInfo, $request );
        }
        
        function perform() {
            // ok, I know, I KNOW! :D
            global $_POST, $_GET;

            //print_r($_POST);

            // load some configuration settings
            $blogSettings = $this->_blogInfo->getSettings();

            $classType = $this->_request->getValue('class_type');
            
            if(!$classType)
                die('No class type set!');

            //if(!$blogSettings->getValue("plugin_shoutbox_dbok") || $blogSettings->getValue( "plugin_shoutbox_version") != '0.12') {
            //    header('Location: admin.php?op=setupShoutbox');
            //    exit();
            //}

            if($this->_request->getValue('sub_op') == 'modifyCATItem') {
                eval("\$item = new $classType();");
                $item->fromForm($_POST);
                $item->init($_POST, $this->_blogInfo->getId(), $this->blogId, $blogSettings);
                $item->save();
            }
            else if($this->_request->getValue('sub_op') == 'expireCATItem') {
                eval("\$item = new $classType();");
                $item->fromForm($_POST);
                $item->init($_POST, $this->_blogInfo->getId(), $this->blogId, $blogSettings);
                $item->expire();
            }
            else if($this->_request->getValue('sub_op') == 'unexpire') {
                $itemFetcher = new CAT_ItemsFetcher($this->_blogInfo->getId(), $blogSettings);
                $itemsFetched = $itemFetcher->getItems($classType, false, $this->_request->getValue('id'));
                $item = $itemsFetched[0];
                $item->unexpire();
            }
            else if($this->_request->getValue('sub_op') == 'rate') {
                eval("\$item = new $classType();");
                $item->init($_GET, $this->_blogInfo->getId(), $this->blogId, $blogSettings);
                $item->setRate($this->_request->getValue('rate'));
            }
            else if($this->_request->getValue('sub_op') == 'additem') {
                eval("\$item = new $classType();");
                    
                $item->init(array('id'=>0), $this->_blogInfo->getId(), $this->blogId, $blogSettings);
                $addArray = array($item);
            }
            
            // Get the entries
            $itemFetcher = new CAT_ItemsFetcher($this->_blogInfo->getId(), $blogSettings);

            $itemsFetched = array_merge($addArray, $itemFetcher->getItems($classType, true));
            //print_r($itemsFetched);
            //$itemsFetched[0]->save();
            
            $expiredItems = $itemFetcher->getItems($classType, false);

            $fullQuery = "class_type=$classType&" .
                         "op=Currentlyaddictedto";
            
            // create a view and export the settings to the template
            $this->_view = new AdminPluginTemplatedView( $this->_blogInfo, "currentlyaddictedto", strtolower($classType), true );
            
            $this->_view->setValue("items", $itemsFetched);
            $this->_view->setValue("classType", $classType);
            $this->_view->setValue("expiredItems", $expiredItems);
            $this->_view->setValue("fullQuery", $fullQuery);

            $this->setCommonData();
            
            return true;
        }
}
?>