/**
  * 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;
 }
 /**
  * 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 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;
 }
 /**
  * Recache a block
  *
  * @access	protected
  * @return	void
  */
 protected function _recacheAllBlocks()
 {
     //-----------------------------------------
     // Get skin
     //-----------------------------------------
     require_once IPSLib::getAppDir('ccs') . '/sources/pages.php';
     $pageBuilder = new pageBuilder($this->registry);
     $pageBuilder->recacheTemplateCache();
     $pageBuilder->loadSkinFile();
     $this->DB->build(array('select' => '*', 'from' => 'ccs_blocks'));
     $outer = $this->DB->execute();
     while ($block = $this->DB->fetch($outer)) {
         if ($block['block_type'] and file_exists(IPSLib::getAppDir('ccs') . '/sources/blocks/' . $block['block_type'] . '/admin.php')) {
             require_once IPSLib::getAppDir('ccs') . '/sources/blocks/adminInterface.php';
             require_once IPSLib::getAppDir('ccs') . '/sources/blocks/' . $block['block_type'] . '/admin.php';
             $className = "adminBlockHelper_" . $block['block_type'];
             $extender = new $className($this->registry);
             $extender->recacheBlock($block);
         }
     }
     //-----------------------------------------
     // Clear page caches
     //-----------------------------------------
     $this->DB->update('ccs_pages', array('page_cache' => null));
     $this->registry->output->global_message = $this->lang->words['all_blocks_recached'];
     $this->registry->output->silentRedirectWithMessage($this->settings['base_url'] . '&module=blocks&section=blocks');
 }