<?  // PHPmyGallery - backend core - help system

/* File description:

This script displays the FAQ/Help pages.
Add &html to the URL when viewing the documentation to write the
current page to _conf/documentation/html/ for offline viewing.

### What this script does:
###
### Included from: adminmenu.index.php
### |
### | (Uses templates from faqhelp)
### +-  (Load selected Help-Page, Process Help Entries, Display Page)
### +-  (Writing the current page to documentation/html/) (add &html to URL)
###
*/ 
    
    // Fetch GET variable (page number)
    if(isset($_GET['page']))
        $pageNumber = $_GET['page'];
    else
        $pageNumber = '1';
    
    // if html variable is given in GET variables, the .html file
    // is written into the documentation/html directory
    if(isset($_GET['html']))
        $writeHtmlFile = true;
    else
        $writeHtmlFile = false;
    
    // Include Help file. If the page exists for the selected language,
    // it is loaded, else the default (English) is included.
    $helpfile = 'lang/faqhelp.'.$conf['lang'].'.page_'.$pageNumber.'.php';
    if(file_exists($helpfile)) {
        include($helpfile);
        
        if($writeHtmlFile) {
            $pktpl_vars['helpfileLang'] = $conf['lang'];
            $writeFilename = 'documentation/html/'.$conf['lang'].'.page_'.$pageNumber.'.html';
        }
    } // if
    else {
        $helpfile_default = 'lang/faqhelp.English.page_'.$pageNumber.'.php';
        include($helpfile_default);
        
        if($writeHtmlFile) {
            $pktpl_vars['helpfileLang'] = 'English';
            $writeFilename = 'documentation/html/English.page_'.$pageNumber.'.html';
        }
    } // else
    
    // Init chapter names
    $chapter_names = array('1'=>$l['manual_users'],
                           '2'=>$l['manual_developers'],
                           '3'=>$l['manual_general_stuff']);
    
    // Set OnLoad to initialise scrolling menu script
    $pktpl_vars['onLoad'] = 'onLoad="start();"';
    
    // Generate help texts and navigation
    $itemNumber     = 1;
    $headingNumbers = array(0,0,0);
    $pktpl_vars['helpHTMLMenu']     = '';
    $pktpl_vars['template_content'] = '';
    
    foreach($helpArray as $data) {
        $pktpl_vars['itemNumber'] = $itemNumber;
        $itemNumber++; // Increase itemNo by 1 (used for linking to headings)
        
        switch($data['_type_']) {
            case 'h1':      // Headings
            case 'h2':
            case 'h3':
                // Set ChapterNo according to heading type
                switch($data['_type_']) { 
                    case 'h1':
                        $headingNumbers[0]++;
                        $headingNumbers[1]=0;
                        $headingNumbers[2]=0;
                        $pktpl_vars['chapterNo'] = '<br>'.$headingNumbers[0].'.';
                        $pktpl_vars['h1br']      = '<br>';
                        break;
                    case 'h2':
                        $headingNumbers[1]++;
                        $headingNumbers[2]=0;
                        $pktpl_vars['chapterNo'] = $headingNumbers[0].'.'.$headingNumbers[1].'.';
                        $pktpl_vars['h1br']      = '';
                        break;
                    case 'h3':
                        $headingNumbers[2]++;
                        $pktpl_vars['chapterNo'] = $headingNumbers[0].'.'.$headingNumbers[1].'.'.$headingNumbers[2].'.';
                        $pktpl_vars['h1br']      = '';
                        break;
                } // switch
                
                $pktpl_vars['tag']        = $data['_type_'];
                $pktpl_vars['inner_html'] = $data['text'];
                $pktpl_vars['headertext'] = $pktpl_vars['chapterNo'].'&nbsp;'.$data['text'];
                
                // Seperate every chapter from the previous one, except the first one
                if ($data['_type_'] == 'h1' && $headingNumbers[0] != 1)
                    $pktpl_vars['template_content'] .= pktpl('adminmenu/faqhelp/tag_hr.html');
                
                $pktpl_vars['template_content'] .= pktpl('adminmenu/faqhelp/tag_hX.html');
                if($data['_type_'] != 'h3') // 3rd level headings are not added to slide menu
                    $pktpl_vars['helpHTMLMenu']     .= pktpl('adminmenu/faqhelp/jsSlideMenu_item.html');
                break;
            case 'img':     // Images
                $pktpl_vars['src'] = $data['src'];
                
                $pktpl_vars['template_content'] .= pktpl('adminmenu/faqhelp/tag_img.html');
                break;
            case 'code':
                $pktpl_vars['title']      = $data['title'];
                $pktpl_vars['inner_html'] = str_replace('<' ,'&lt;',$data['text']);
                $pktpl_vars['inner_html'] = str_replace("\n",'<br>',$pktpl_vars['inner_html']);
                $pktpl_vars['inner_html'] = str_replace("  ",'&nbsp;&nbsp;',$pktpl_vars['inner_html']);
                
                $pktpl_vars['template_content'] .= pktpl('adminmenu/faqhelp/tag_code.html');
                break;
            case 'chapterlink':
                $pktpl_vars['pageNo']   = $data['page'];
                $pktpl_vars['linkname'] = $chapter_names[$data['page']];
                
                $pktpl_vars['template_content'] .= pktpl('adminmenu/faqhelp/chapter_href.html');
                break;
            case 'weblink':
                $pktpl_vars['url']      = $data['url'];
                $pktpl_vars['linkname'] = $data['linkname'];
                
                $pktpl_vars['template_content'] .= pktpl('adminmenu/faqhelp/web_href.html');
                break;
            case '':        // Raw text
            case 'raw':
            case 'none':
                $pktpl_vars['template_content'] .= $data['text'];
                break;
        } // switch
    } // foreach
    $pktpl_vars['template_content'] .= pktpl('adminmenu/faqhelp/footer.html');
    $pktpl_vars['jsSlideMenu']       = pktpl('adminmenu/faqhelp/jsSlideMenu.html');
    
    
    // Save as html file into _conf/documentation/html if $_GET['html'] is set
    if($writeHtmlFile) {
        // Render page content with different navigation
        $pktpl_vars['template_admin_table'] = pktpl('adminmenu/faqhelp/faqhelpOtherNavi.html');
        
        // create HTML with pktpl
        $pktpl_vars['pathToBackend']           = '../../';
        $pktpl_vars['template_admin_metatags'] = pktpl('adminmenu/all_metatags.html');
        $HTMLhelp = pktpl('adminmenu/faqhelp/faqHTMLlayout.html');
        
        // write file
        $fd = fopen($writeFilename,'w+'); // open file for writing (replace existing file if any)
        fwrite($fd,$HTMLhelp); // write text
        fclose($fd); // close file
        
        // Render metatags again with {$pathToBackend} reset
        $pktpl_vars['pathToBackend'] = '';
        $pktpl_vars['template_admin_metatags'] = pktpl('adminmenu/all_metatags.html');
    } // if
    
    // Render page content
    $pktpl_vars['template_admin_table'] = pktpl('adminmenu/faqhelp/faqhelp.html');
    
?>