/**
  * Saves the edits to the template
  *
  * @access	public
  * @param	array 		Block data
  * @param	array 		Block config
  * @return	bool		Saved
  */
 public function saveTemplateEdits($block, $config)
 {
     require_once IPSLib::getAppDir('ccs') . '/sources/blocks/feed/data_sources/' . $config['feed'] . '.php';
     $_className = "feed_" . $config['feed'];
     $_class = new $_className($this->registry);
     $_feedConfig = $_class->returnFeedInfo();
     //-----------------------------------------
     // Save the template
     //-----------------------------------------
     $templateHTML = $_POST['content'];
     $template = $this->DB->buildAndFetch(array('select' => '*', 'from' => 'ccs_template_blocks', 'where' => "tpb_name='{$_feedConfig['templateBit']}_{$block['block_id']}'"));
     if ($template['tpb_id']) {
         $this->DB->update('ccs_template_blocks', array('tpb_content' => $templateHTML), 'tpb_id=' . $template['tpb_id']);
     } else {
         $template = $this->DB->buildAndFetch(array('select' => '*', 'from' => 'ccs_template_blocks', 'where' => "tpb_name='{$_feedConfig['templateBit']}_{$config['content']}'"));
         if (!$template['tpb_id']) {
             $template = $this->DB->buildAndFetch(array('select' => '*', 'from' => 'ccs_template_blocks', 'where' => "tpb_name='{$_feedConfig['templateBit']}'"));
         }
         $this->DB->insert('ccs_template_blocks', array('tpb_name' => "{$_feedConfig['templateBit']}_{$block['block_id']}", 'tpb_content' => $templateHTML, 'tpb_params' => $template['tpb_params']));
         $template['tpb_id'] = $this->DB->getInsertId();
     }
     $cache = array('cache_type' => 'block', 'cache_type_id' => $template['tpb_id']);
     require_once IPS_KERNEL_PATH . 'classTemplateEngine.php';
     $engine = new classTemplate(IPS_ROOT_PATH . 'sources/template_plugins');
     $cache['cache_content'] = $engine->convertHtmlToPhp("{$_feedConfig['templateBit']}_{$block['block_id']}", $template['tpb_params'], $templateHTML, '', false, true);
     $hasIt = $this->DB->buildAndFetch(array('select' => 'cache_id', 'from' => 'ccs_template_cache', 'where' => "cache_type='block' AND cache_type_id={$template['tpb_id']}"));
     if ($hasIt['cache_id']) {
         $this->DB->update('ccs_template_cache', $cache, "cache_type='block' AND cache_type_id={$template['tpb_id']}");
     } else {
         $this->DB->insert('ccs_template_cache', $cache);
     }
     //-----------------------------------------
     // Recache the "skin" file
     //-----------------------------------------
     require_once IPSLib::getAppDir('ccs') . '/sources/pages.php';
     $_pagesClass = new pageBuilder($this->registry);
     $_pagesClass->recacheTemplateCache($engine);
     //-----------------------------------------
     // Recache block
     //-----------------------------------------
     if ($block['block_cache_ttl']) {
         $block['block_cache_output'] = $this->recacheBlock($block);
         $block['block_cache_last'] = time();
     }
     return true;
 }
 /**
  * Recache this block to the database based on content type and cache settings.
  *
  * @access	public
  * @param	array 				Block data
  * @param	bool				Return data instead of saving to database
  * @return	string				New cached content
  */
 public function recacheBlock($block, $return = false)
 {
     //-----------------------------------------
     // Save the template
     //-----------------------------------------
     $templateHTML = $this->_parseBlock($block);
     $template = $this->DB->buildAndFetch(array('select' => '*', 'from' => 'ccs_template_blocks', 'where' => "tpb_name='block__custom_{$block['block_id']}'"));
     if ($template['tpb_id']) {
         $this->DB->update('ccs_template_blocks', array('tpb_content' => $templateHTML), 'tpb_id=' . $template['tpb_id']);
     } else {
         $template = $this->DB->buildAndFetch(array('select' => '*', 'from' => 'ccs_template_blocks', 'where' => "tpb_name='block__custom'"));
         $this->DB->insert('ccs_template_blocks', array('tpb_name' => 'block__custom_' . $block['block_id'], 'tpb_content' => $templateHTML, 'tpb_params' => $template['tpb_params']));
         $template['tpb_id'] = $this->DB->getInsertId();
     }
     $cache = array('cache_type' => 'block', 'cache_type_id' => $template['tpb_id']);
     require_once IPS_KERNEL_PATH . 'classTemplateEngine.php';
     $engine = new classTemplate(IPS_ROOT_PATH . 'sources/template_plugins');
     $cache['cache_content'] = $engine->convertHtmlToPhp('block__custom_' . $block['block_id'], $template['tpb_params'], $templateHTML, '', false, true);
     $hasIt = $this->DB->buildAndFetch(array('select' => 'cache_id', 'from' => 'ccs_template_cache', 'where' => "cache_type='block' AND cache_type_id={$template['tpb_id']}"));
     //print_r($cache);exit;
     if ($hasIt['cache_id']) {
         $this->DB->update('ccs_template_cache', $cache, "cache_type='block' AND cache_type_id={$template['tpb_id']}");
     } else {
         $this->DB->insert('ccs_template_cache', $cache);
     }
     //-----------------------------------------
     // Recache the "skin" file
     //-----------------------------------------
     require_once IPSLib::getAppDir('ccs') . '/sources/pages.php';
     $_pagesClass = new pageBuilder($this->registry);
     $_pagesClass->recacheTemplateCache($engine);
     $_pagesClass->loadSkinFile();
     $func = 'block__custom_' . $block['block_id'];
     $content = $this->registry->output->getTemplate('ccs')->{$func}($block['block_name'], $templateHTML);
     if (!$return and $block['block_cache_ttl']) {
         $this->DB->update('ccs_blocks', array('block_cache_output' => $content, 'block_cache_last' => time()), 'block_id=' . intval($block['block_id']));
     }
     return $content;
 }
 /**
  * Save the edits to a template
  *
  * @access	protected
  * @param	string		[$type]		Saving of form (add|edit)
  * @return	void
  */
 protected function _save($type = 'add')
 {
     $id = 0;
     if ($type == 'edit') {
         $id = intval($this->request['template']);
         if (!$id) {
             $this->registry->output->showError($this->lang->words['ccs_no_template_id']);
         }
     }
     $save = array('template_name' => trim($this->request['template_name']), 'template_desc' => trim($this->request['template_desc']), 'template_key' => trim($this->request['template_key']), 'template_content' => str_replace('../', '../', trim($_POST['template_content'])), 'template_updated' => time(), 'template_category' => intval($this->request['template_category']));
     //-----------------------------------------
     // Make sure key is unique
     //-----------------------------------------
     if (!$save['template_key']) {
         $this->registry->output->showError($this->lang->words['template_key_missing']);
     }
     $check = $this->DB->buildAndFetch(array('select' => 'template_id', 'from' => 'ccs_page_templates', 'where' => "template_key='{$save['template_key']}' AND template_id<>{$id}"));
     if ($check['template_id']) {
         $this->registry->output->showError($this->lang->words['template_key_used']);
     }
     //-----------------------------------------
     // Save
     //-----------------------------------------
     if ($type == 'edit') {
         $this->DB->update('ccs_page_templates', $save, 'template_id=' . $id);
         $this->DB->update('ccs_pages', array('page_cache' => null), 'page_template_used=' . $id);
         $this->registry->output->global_message = $this->lang->words['template_edited'];
     } else {
         $this->DB->insert('ccs_page_templates', $save);
         $id = $this->DB->getInsertId();
         $this->registry->output->global_message = $this->lang->words['template_added'];
         $this->request['template'] = $id;
     }
     //-----------------------------------------
     // Recache the template
     //-----------------------------------------
     $cache = array('cache_type' => 'template', 'cache_type_id' => $id);
     require_once IPS_KERNEL_PATH . 'classTemplateEngine.php';
     $engine = new classTemplate(IPS_ROOT_PATH . 'sources/template_plugins');
     $cache['cache_content'] = $engine->convertHtmlToPhp($save['template_key'], '', $save['template_content'], '', false, true);
     $hasIt = $this->DB->buildAndFetch(array('select' => 'cache_id', 'from' => 'ccs_template_cache', 'where' => "cache_type='template' AND cache_type_id={$id}"));
     if ($hasIt['cache_id']) {
         $this->DB->update('ccs_template_cache', $cache, "cache_type='template' AND cache_type_id={$id}");
     } else {
         $this->DB->insert('ccs_template_cache', $cache);
     }
     //-----------------------------------------
     // Recache the "skin" file
     //-----------------------------------------
     require_once IPSLib::getAppDir('ccs') . '/sources/pages.php';
     $_pagesClass = new pageBuilder($this->registry);
     $_pagesClass->recacheTemplateCache($engine);
     //-----------------------------------------
     // Show form again?
     //-----------------------------------------
     if ($this->request['save_and_reload']) {
         $this->_form('edit');
         return;
     }
     //-----------------------------------------
     // Output
     //-----------------------------------------
     $this->registry->output->silentRedirectWithMessage($this->settings['base_url'] . '&module=templates&section=pages');
 }
 /**
  * Recache "skin" file
  *
  * @access	public
  * @param	object		Template engine (if already loaded)
  * @return	string		Skin file class
  */
 public function recacheTemplateCache($engine = null)
 {
     //-----------------------------------------
     // Make sure we got the engine
     //-----------------------------------------
     if (!$engine) {
         require_once IPS_KERNEL_PATH . 'classTemplateEngine.php';
         $engine = new classTemplate(IPS_ROOT_PATH . 'sources/template_plugins');
     }
     //-----------------------------------------
     // Recache the blocks
     //-----------------------------------------
     $this->DB->build(array('select' => '*', 'from' => 'ccs_template_blocks'));
     $outer = $this->DB->execute();
     while ($r = $this->DB->fetch($outer)) {
         $cache = array('cache_type' => 'block', 'cache_type_id' => $r['tpb_id']);
         $cache['cache_content'] = $engine->convertHtmlToPhp($r['tpb_name'], $r['tpb_params'], $r['tpb_content'], '', false, true);
         $hasIt = $this->DB->buildAndFetch(array('select' => 'cache_id', 'from' => 'ccs_template_cache', 'where' => "cache_type='block' AND cache_type_id={$r['tpb_id']}"));
         if ($hasIt['cache_id']) {
             $this->DB->update('ccs_template_cache', $cache, "cache_type='block' AND cache_type_id={$r['tpb_id']}");
         } else {
             $this->DB->insert('ccs_template_cache', $cache);
         }
     }
     //-----------------------------------------
     // Get templates
     //-----------------------------------------
     $templateBits = array();
     $this->DB->build(array('select' => '*', 'from' => 'ccs_template_cache', 'where' => "cache_type NOT IN('full','page')"));
     $this->DB->execute();
     while ($r = $this->DB->fetch()) {
         $templateBits[] = $r['cache_content'];
     }
     //-----------------------------------------
     // Now create the pseudo-code
     //-----------------------------------------
     $_fakeClass = "<" . "?php\n\n";
     $_fakeClass .= "class skin_ccs_1 {\n\n";
     $_fakeClass .= "\n\n}";
     $fullFile = $engine->convertCacheToEval($_fakeClass, "skin_ccs", 1);
     $fullFile = str_replace("\n\n}", implode("\n\n", $templateBits) . "\n\n}", $fullFile);
     $cache = array('cache_type' => 'full', 'cache_type_id' => 0, 'cache_content' => $fullFile);
     $hasIt = $this->DB->buildAndFetch(array('select' => 'cache_id', 'from' => 'ccs_template_cache', 'where' => "cache_type='full'"));
     if ($hasIt['cache_id']) {
         $this->DB->update('ccs_template_cache', $cache, "cache_type='full'");
     } else {
         $this->DB->insert('ccs_template_cache', $cache);
     }
     return $fullFile;
 }
 /**
  * Import skin templates for a plugin block
  *
  * @access	protected
  * @return	void
  */
 protected function _importBlock()
 {
     //-----------------------------------------
     // Developer reimporting templates?
     //-----------------------------------------
     if ($this->request['dev']) {
         $templates = array();
         $this->DB->build(array('select' => '*', 'from' => 'ccs_template_blocks'));
         $outer = $this->DB->execute();
         while ($r = $this->DB->fetch($outer)) {
             if (!preg_match("/_(\\d+)\$/", $r['tpb_name'])) {
                 $templates[$r['tpb_name']] = $r;
             }
         }
         $content = file_get_contents(IPSLib::getAppDir('ccs') . '/xml/block_templates.xml');
         require_once IPS_KERNEL_PATH . 'classXML.php';
         $xml = new classXML(IPS_DOC_CHAR_SET);
         $xml->loadXML($content);
         foreach ($xml->fetchElements('template') as $template) {
             $_template = $xml->fetchElementsFromRecord($template);
             if ($_template['tpb_name']) {
                 unset($_template['tpb_id']);
                 if (array_key_exists($_template['tpb_name'], $templates)) {
                     $this->DB->update("ccs_template_blocks", $_template, "tpb_id={$templates[$_template['tpb_name']]['tpb_id']}");
                 } else {
                     $this->DB->insert("ccs_template_blocks", $_template);
                 }
             }
         }
         $this->registry->output->global_message = $this->lang->words['block_import_devgood'];
         $this->registry->output->silentRedirectWithMessage($this->settings['base_url'] . '&module=blocks&section=blocks');
     }
     $content = $this->registry->getClass('adminFunctions')->importXml();
     //-----------------------------------------
     // Get xml class
     //-----------------------------------------
     require_once IPS_KERNEL_PATH . 'classXML.php';
     $xml = new classXML(IPS_DOC_CHAR_SET);
     $xml->loadXML($content);
     //-----------------------------------------
     // First, found out if this is just a plugin
     //-----------------------------------------
     $_fullBlock = false;
     $_block = array();
     $_blockId = 0;
     foreach ($xml->fetchElements('block') as $block) {
         $_block = $xml->fetchElementsFromRecord($block);
     }
     if (count($_block)) {
         $_fullBlock = true;
     }
     //-----------------------------------------
     // If full block, insert block first to get id
     //-----------------------------------------
     if ($_fullBlock) {
         unset($_block['block_id']);
         unset($_block['block_cache_last']);
         unset($_block['block_position']);
         unset($_block['block_category']);
         $check = $this->DB->buildAndFetch(array('select' => 'block_id', 'from' => 'ccs_blocks', 'where' => "block_key='{$_block['block_key']}'"));
         //-----------------------------------------
         // Instead of updating, just change key to prevent
         // overwriting someone's configured block
         //-----------------------------------------
         if ($check['block_id']) {
             $_block['block_key'] = $_block['block_key'] . md5(uniqid(microtime()));
         }
         $this->DB->insert('ccs_blocks', $_block);
         $_blockId = $this->DB->getInsertId();
     }
     //-----------------------------------------
     // Do the template regardless
     //-----------------------------------------
     $tpbId = 0;
     foreach ($xml->fetchElements('template') as $template) {
         $entry = $xml->fetchElementsFromRecord($template);
         if (!$entry['tpb_name']) {
             continue;
         }
         $templatebit = array('tpb_name' => $entry['tpb_name'], 'tpb_params' => $entry['tpb_params'], 'tpb_content' => $entry['tpb_content']);
         //-----------------------------------------
         // Fix name if full block
         //-----------------------------------------
         if ($_fullBlock) {
             $templatebit['tpb_name'] = preg_replace("/^(.+?)_(\\d+)\$/", "\\1_{$_blockId}", $templatebit['tpb_name']);
         }
         $check = $this->DB->buildAndFetch(array('select' => 'tpb_id', 'from' => 'ccs_template_blocks', 'where' => "tpb_name='{$entry['tpb_name']}'"));
         if ($check['tpb_id']) {
             $this->DB->update('ccs_template_blocks', $templatebit, 'tpb_id=' . $check['tpb_id']);
             $tpbId = $check['tpb_id'];
         } else {
             $this->DB->insert('ccs_template_blocks', $templatebit);
             $tpbId = $this->DB->getInsertId();
         }
     }
     //-----------------------------------------
     // Recache skin if full block
     //-----------------------------------------
     if ($_fullBlock and $tpbId) {
         $cache = array('cache_type' => 'block', 'cache_type_id' => $tpbId);
         require_once IPS_KERNEL_PATH . 'classTemplateEngine.php';
         $engine = new classTemplate(IPS_ROOT_PATH . 'sources/template_plugins');
         $cache['cache_content'] = $engine->convertHtmlToPhp("{$templatebit['tpb_name']}", $templatebit['tpb_params'], $templatebit['tpb_content'], '', false, true);
         $hasIt = $this->DB->buildAndFetch(array('select' => 'cache_id', 'from' => 'ccs_template_cache', 'where' => "cache_type='block' AND cache_type_id={$tpbId}"));
         if ($hasIt['cache_id']) {
             $this->DB->update('ccs_template_cache', $cache, "cache_type='block' AND cache_type_id={$tpbId}");
         } else {
             $this->DB->insert('ccs_template_cache', $cache);
         }
         //-----------------------------------------
         // Recache the "skin" file
         //-----------------------------------------
         require_once IPSLib::getAppDir('ccs') . '/sources/pages.php';
         $_pagesClass = new pageBuilder($this->registry);
         $_pagesClass->recacheTemplateCache($engine);
     }
     if ($_fullBlock) {
         $this->registry->output->global_message = $this->lang->words['block_full_import_good'];
     } else {
         $this->registry->output->global_message = $this->lang->words['block_import_good'];
     }
     $this->registry->output->silentRedirectWithMessage($this->settings['base_url'] . '&module=blocks&section=blocks');
 }