Index: branches/RC/core/units/general/helpers/themes_helper.php =================================================================== diff -u -N -r11495 -r11646 --- branches/RC/core/units/general/helpers/themes_helper.php (.../themes_helper.php) (revision 11495) +++ branches/RC/core/units/general/helpers/themes_helper.php (.../themes_helper.php) (revision 11646) @@ -10,6 +10,13 @@ var $themesFolder = ''; /** + * List of theme names, found on system + * + * @var Array + */ + var $_themeNames = Array (); + + /** * Temporary array when all theme files from db are stored * * @var Array @@ -91,6 +98,7 @@ } } + $this->_themeNames[$theme_id] = $theme_name; $theme_path = $this->themesFolder.'/'.$theme_name; $this->FindThemeFiles('', $theme_path, $theme_id); // search from base theme directory @@ -140,24 +148,33 @@ elseif (substr($filename, -4) == '.tpl') { $file_path = $folder_path.'/'.$filename; + $meta_info = $this->_getTemplateMetaInfo(trim($file_path, '/'), $theme_id); $file_id = isset($this->themeFiles[$file_path]) ? $this->themeFiles[$file_path] : false; + $file_description = array_key_exists('desc', $meta_info) ? $meta_info['desc'] : ''; + if ($file_id) { // file was found in db & on hdd -> mark as existing - $sql = 'UPDATE '.TABLE_PREFIX.'ThemeFiles - SET FileFound = 1, FileType = '.$auto_structure.' - WHERE FileId = '.$file_id; - $this->Conn->Query($sql); + $fields_hash = Array ( + 'FileFound' => 1, + 'Description' => $file_description, + 'FileType' => $auto_structure, + 'FileMetaInfo' => serialize($meta_info), + ); + + $this->Conn->doUpdate($fields_hash, TABLE_PREFIX . 'ThemeFiles', 'FileId = ' . $file_id); } else { // file was found on hdd, but missing in db -> create new file record - $fields_hash = Array ( - 'ThemeId' => $theme_id, - 'FileName' => $filename, - 'FilePath' => $folder_path, - 'Description' => '', - 'FileType' => $auto_structure, // 1 - built-in, 0 - custom (not in use right now), 2 - skipped in structure - 'FileFound' => 1, - ); + $fields_hash = Array ( + 'ThemeId' => $theme_id, + 'FileName' => $filename, + 'FilePath' => $folder_path, + 'Description' => $file_description, + 'FileType' => $auto_structure, // 1 - built-in, 0 - custom (not in use right now), 2 - skipped in structure + 'FileMetaInfo' => serialize($meta_info), + 'FileFound' => 1, + ); + $this->Conn->doInsert($fields_hash, TABLE_PREFIX.'ThemeFiles'); $this->themeFiles[$file_path] = $this->Conn->getInsertID(); } @@ -168,6 +185,64 @@ } /** + * Returns template information (name, description, path) from it's header comment + * + * @param string $template + * @param int $theme_id + * @return Array + */ + function _getTemplateMetaInfo($template, $theme_id) + { + if (!defined('DBG_NPARSER_FORCE_COMPILE')) { + $this->Application->InitParser(); + define('DBG_NPARSER_FORCE_COMPILE', 1); + } + + $template = 'theme:' . $this->_themeNames[$theme_id] . '/' . $template; + $template_file = $this->Application->TemplatesCache->GetRealFilename($template); // ".tpl" was added before + + if (!file_exists($template_file)) { + // when template without info it's placed in top category + return Array (); + } + + $template_data = file_get_contents($template_file); + + if (substr($template_data, 0, 6) == '*/ + + $comment_end = strpos($template_data, '##-->'); + if ($comment_end === false) { + // badly formatted comment + return Array (); + } + + $comment = trim( substr($template_data, 6, $comment_end - 6) ); + if (preg_match_all('/<(NAME|DESC|SECTION)>(.*?)<\/(NAME|DESC|SECTION)>/is', $comment, $regs)) { + $ret = Array (); + foreach ($regs[1] as $param_order => $param_name) { + $ret[ strtolower($param_name) ] = trim($regs[2][$param_order]); + } + + if (array_key_exists('section', $ret) && $ret['section']) { + $category_path = explode('||', $ret['section']); + $category_path = array_map('trim', $category_path); + $ret['section'] = implode('||', $category_path); + } + + return $ret; + } + } + + return Array (); + } + + /** * Updates file system changes to database for all themes (including new ones) * */