<?php

class Service_ThemeBlocksAd extends XcBase {

      var $_kernel;
      var $_curPageCache = null;

        function Service_ThemeBlocksAd($kernelLink) {
           $this->_localname = 'ThemeBlocksAd';
           $this->XcBase($kernelLink);
           $this->_kernel = $this->srvMainKernel();
           //$this->advOnto = $this->_kernel->srvFactory("AdvOntology");

        }

        function onInstall($params) {
           $this->_db->Query("CREATE TABLE `".$this->_db->config["table_prefix"]."themeblocksad` (
  `id` int(11) NOT NULL auto_increment,
  `blockname` varchar(50) NOT NULL default '',
  `priority` int(2) NOT NULL default 1,
  `blocktext` text NOT NULL,
  PRIMARY KEY  (`id`),
  INDEX (`blockname`)
) ENGINE=MyISAM");
           return true;
        }


        function onUninstall($params) {
           $this->_db->Query("DROP TABLE `".$this->_db->config["table_prefix"]."themeblocksad`");
           return true;
        }

        function getBlock($blockname) {
           $mythis = $this->_db;
           $res1 = $mythis->LoadSingle('SELECT * FROM '.$mythis->config["table_prefix"]."themeblocksad WHERE (blockname='".quote($blockname)."') AND priority='2'");
           if($res1) {

           } else {
               $res = $mythis->LoadAll('SELECT * FROM '.$mythis->config["table_prefix"]."themeblocksad WHERE (blockname='".quote($blockname)."') AND priority='1'");
               $res1 = $res[rand(0, count($res)-1)];
           }
           return $res1["blocktext"];
        }


}


?>
