<?php

# PLUGIN PREVIEW BY TEXTPATTERN.INFO


/*
    adi_page_tab - Enhanced Page Tab

    Written by Adi Gilbert

    Released under the GNU General Public License

    Version history:
    0.5        - fixes for TXP 4.6
    0.4        - TXP 4.5-ified
            - TXP 4.5+ only
            - code tidy up
    0.3        - fix to ignore new page name if "copy page as" filled in and Save button clicked (or Enter key used)
    0.2        - fixed to cope with page create, delete & copy properly
            - "Update" button now has its own language string (thanks Pat64)
    0.1        - initial release

*/

global $adi_page_tab_debug;

if (@
txpinterface == 'admin') {

    
$adi_page_tab_debug 0;

    
// it's a modern world
    
if (!version_compare(txp_version,'4.5.0','>=')) return;

    
adi_page_tab_init();
}

function 
adi_page_tab_init() {
    
# it's a setup
    
global $event,$step,$prefs,$textarray,$adi_page_tab_debug,$adi_page_tab_message,$adi_page_tab_url;

    
// the new world order?
    
$adi_page_tab_txp460 = (version_compare(txp_version,'4.6-dev','>='));

    
// plugin options
    
$adi_page_tab_plugin_status fetch('status','txp_plugin','name','adi_page_tab',$adi_page_tab_debug);
    if (
$adi_page_tab_plugin_status) { // proper install - options under Plugins tab
        
add_privs('plugin_prefs.adi_page_tab','1,2,6');
        
register_callback('adi_page_tab_options','plugin_prefs.adi_page_tab');
    }
    else { 
// txpdev - options under Extensions tab
        
add_privs('adi_page_tab_options','1,2,6');
        
register_tab('extensions','adi_page_tab_options','adi_page_tab options');
        
register_callback('adi_page_tab_options','adi_page_tab_options');
    }

    
// Textpack
    
$adi_page_tab_url = array(
        
'textpack' => 'http://www.greatoceanmedia.com.au/files/adi_textpack.txt',
        
'textpack_download' => 'http://www.greatoceanmedia.com.au/textpack/download',
        
'textpack_feedback' => 'http://www.greatoceanmedia.com.au/textpack/?plugin=adi_page_tab',
    );
    if (
strpos($prefs['plugin_cache_dir'],'adi') !== FALSE// use Adi's local version
        
$adi_matrix_url['textpack'] = $prefs['plugin_cache_dir'].'/adi_textpack.txt';

# --- BEGIN PLUGIN TEXTPACK ---
    
$adi_page_tab_gtxt = array(
        
'adi_use_page_section' => 'Use <strong>{name}</strong> for section',
        
'adi_section_updated' => 'Section <strong>{name}</strong> updated',
        
'adi_section_update_fail' => 'Section <strong>{name}</strong> not updated',
        
'adi_textpack_fail' => 'Textpack installation failed',
        
'adi_textpack_feedback' => 'Textpack feedback',
        
'adi_textpack_online' => 'Textpack also available online',
        
'adi_update' => 'Update',
    );
# --- END PLUGIN TEXTPACK ---

    // update $textarray
    
$textarray += $adi_page_tab_gtxt;

    if (
$event == "page") { // go, go, go
        // style
        
register_callback('adi_page_tab_style','admin_side','head_end');
        
// some action
        
$adi_page_tab_message = array();
        
$assign gps('adi_page_tab_step');
        if (
$assign) { // someone has hit the section update button
            
$this_page gps('name'); // page name
            
if ($adi_page_tab_debug)
                echo 
'This page='.$this_page;
            
$new_section gps('adi_page_tab_new_section'); // section name
            
if (!empty($new_section)) { // update database
                
if ($adi_page_tab_debug)
                    echo 
', assigned to '.$new_section;
                
$res safe_update('txp_section','page="'.doSlash($this_page).'"','name="'.$new_section.'"',$adi_page_tab_debug);
                if (
$res) {
                    
update_lastmod();
                    
$adi_page_tab_message['text'] = gTxt('adi_section_updated',array('{name}' => $new_section));
                    
$adi_page_tab_message['status'] = 'success';
                }
                else {
                    
$adi_page_tab_message['text'] = gTxt('adi_section_update_fail',array('{name}' => $new_section));
                    
$adi_page_tab_message['status'] = 'error';
                }
            }
        }
        
// inject markup
        
if ($adi_page_tab_txp460)
            
register_callback('adi_page_tab_markup_46','admin_side','main_content_end'); // markup in the footer, shifted by jQuery
        
else
            
register_callback('adi_page_tab_sections','page','',1);    // inject markup directly into Pages tab

        // inject message markup into Pages tab
        
register_callback('adi_page_tab_messagepane','admin_side','footer');

        
// script
        
register_callback('adi_page_tab_script','admin_side','head_end');
    }
}

function 
adi_page_tab_style() {
// practical & stylish
    
echo
        
'<style type="text/css">
            #adi_page_tab_sections { margin-top:1em }
            #adi_page_tab_sections label { width:auto; float:left; margin:0 }
            #adi_page_tab_sections select { float:left; margin:0 0 0 0.5em }
            #adi_page_tab_sections input.smallerbox { margin:0 0 0 0.5em }
            #adi_page_tab_sections .adi_page_tab_disabled { color:gray } /* explicitly grey out disabled options (for IE6/7) - still no good for iOS Safari NOW REDUNDANT? */
            /* TXP 4.6 */
            p.txp-actions #adi_page_tab_sections label { float:none }
            p.txp-actions #adi_page_tab_sections select { float:none; margin:0.5em 0.5em 0.5em 0 }
            p.txp-actions #adi_page_tab_sections input.smallerbox { margin:0 }
            @media (max-width: 47em) {
                #adi_page_tab_sections label { float:none; display:block }
                #adi_page_tab_sections select { float:none; margin:0.5em 0 1em }
                #adi_page_tab_sections input.smallerbox { display:block; margin:0 }
            }
        </style>'
;
}

function 
adi_page_tab_script() {
// scripticle

    
echo <<<END_SCRIPT
<script type="text/javascript">
    $(function() {
        // adi_page_tab
        $("form#adi_page_tab").insertAfter($("form#page_form")); // shift page tag markup from footer
//         $("p.txp-actions").append($("form#adi_page_tab")); // shift page tag markup from footer
    });
</script>
END_SCRIPT;
}

function 
adi_page_tab_options($event,$step) {
// plugin options page
    
global $adi_page_tab_debug,$adi_page_tab_url,$textarray;

    
$message '';

    
// step-tastic
    
if ($step == 'textpack') {
        if (
function_exists('install_textpack')) {
            
$adi_textpack file_get_contents($adi_page_tab_url['textpack']);
            if (
$adi_textpack) {
                
$result install_textpack($adi_textpack);
                
$message gTxt('textpack_strings_installed', array('{count}' => $result));
                
$textarray load_lang(LANG); // load in new strings
            
}
            else
                
$message = array(gTxt('adi_textpack_fail'),E_ERROR);
        }
    }

    
// generate page
    
pagetop('adi_page_tab '.gTxt('plugin_prefs'),$message);

    
// options
    
echo tag(
        
tag('adi_page_tab '.gTxt('plugin_prefs'),'h2')
        
// textpack links
        
.graf(href(gTxt('install_textpack'),'?event='.$event.'&amp;step=textpack'))
        .
graf(href(gTxt('adi_textpack_online'),$adi_page_tab_url['textpack_download']))
        .
graf(href(gTxt('adi_textpack_feedback'),$adi_page_tab_url['textpack_feedback']))
        ,
'div'
        
,' style="text-align:center"'
    
);

    if (
$adi_page_tab_debug) {
        echo 
'<b>$adi_textpack ('.$adi_page_tab_url['textpack'].'):</b>';
        
$adi_textpack file_get_contents($adi_page_tab_url['textpack']);
        
dmp($adi_textpack);
    }

}

function 
adi_page_tab_section_popup($this_page) {
// get section information from txp_section
    
$rs safe_query("SELECT name,page FROM ".safe_pfx('txp_section')." ORDER BY name");
    
$out '';
    while (
$a nextRow($rs)) {
        
extract($a); // set 'name','title','parent' etc in $a
        
$disabled '';
        if (
$page == $this_page)
            
$disabled ' disabled="disabled" class="adi_page_tab_disabled"';
        
$out .= tag($name,'option',$disabled);
    }
    return 
tag('<option></option>'.$out,'select',' id="adi_page_tab_new_section" name="adi_page_tab_new_section"');
}

function 
adi_page_tab_sections() {
// the old-fashioned magic that gets adi_page_tab's section popup into the standard Page tab
    
ob_start('adi_page_tab_inject');
}

function 
adi_page_tab_inject($buffer) {
// another bit of magic that gets adi_page_tab's section popup into the standard Page tab
    
global $DB;

    if(!isset(
$DB)) $DB = new db;
    
$pattern '#id="page_form".*</form>#sU';
    
$insert 'adi_page_tab_markup';
    
$buffer preg_replace_callback($pattern$insert$buffer);
    return 
$buffer;
}

function 
adi_page_tab_markup($matches) {
// generate markup for adi_page_tab section list
    
global $step;

    
$this_page gps('name');
    
$newname gps('newname');
    
$copy gps('copy');
    
$savenew gps('savenew');

    if (
$step == "page_new"// don't output anything
        
return $matches[0];
    if (
$savenew || $copy// new page or page copy
        
$this_page $newname;
    if (
$step == 'page_delete'// default to default
        
$this_page 'default';
    if (empty(
$this_page)) // default visit to Page tab
        
$this_page 'default';

    return 
$matches[0]
        .
form(
            
tag(
                
tag(
                    
tag(gTxt('adi_use_page_section',array('{name}' => $this_page)),'label',' for="adi_page_tab_new_section"')
                    .
adi_page_tab_section_popup($this_page,FALSE)
                    .
fInput("submit""use",gTxt('adi_update'), "smallerbox")
                    .
hInput('adi_page_tab_step','update'// secret step
                    
.hInput('name',$this_page// provide page name back to Page tab
                    
.eInput("page")
                ,
'p')
            ,
'div'
            
,' id="adi_page_tab_sections"')
        );
}

function 
adi_page_tab_markup_46() {
// generate markup for adi_page_tab section list
    
global $step;

    
$this_page gps('name');
    
$newname gps('newname'); // set on page save (incl. rename) & duplicate
    
$savenew gps('savenew');

    if (
$step == "page_new"// don't output anything
        
return;
    if ((
$step == "page_save") || $savenew// page save/rename/copy or new
        
$this_page $newname;
    if (empty(
$this_page) || ($step == 'page_delete')) // use "default" if default visit to Page tab or page delete
        
$this_page 'default';

    echo 
form(
        
tag(
            
tag(
                
tag(gTxt('adi_use_page_section',array('{name}' => $this_page)),'label',' for="adi_page_tab_new_section"')
                .
adi_page_tab_section_popup($this_page,FALSE)
                .
fInput("submit""use",gTxt('adi_update'), "smallerbox")
                .
hInput('adi_page_tab_step','update'// secret step
                
.hInput('name',$this_page// provide page name back to Page tab
                
.eInput("page")
            ,
'p')
        ,
'div'
        
,' id="adi_page_tab_sections"')
        ,
'','','post','','','adi_page_tab'
    
);

}

function 
adi_page_tab_messagepane() {
// generate messagepane & message
    
global $adi_page_tab_message;

    
$adi_page_tab_txp450 = (version_compare(txp_version,'4.4.1','>'));

    if (
$adi_page_tab_message) {
        if (
$adi_page_tab_txp450) {
            
$html '<span id="message" class="messageflash '.$adi_page_tab_message['status'].'">'.$adi_page_tab_message['text'].' <a href="#close" class="close">&times;</a></span>';
            
$message_script = <<<END_SCRIPT
                <script type="text/javascript">
                <!--
                    $('#messagepane').append('
$html');
                    $("#messagepane").css({
                        left: ($(window).width() - $("#messagepane").outerWidth()) / 2
                    });
                    $(window).resize();
                // -->
                </script>
END_SCRIPT;
        }
        else { 
// pre-TXP 4.5.0
            
$html $adi_page_tab_message['text'];
            
$message_script = <<<END_SCRIPT
                <script type="text/javascript">
                <!--
                $('#messagepane').append('
$html');
END_SCRIPT;
            if (
$adi_page_tab_message['status'] != 'success') { // it's a warning or error, so flash it
                
$message_script .= <<<END_SCRIPT
                    $('#messagepane').fadeOut(800).fadeIn(800);
                    $('#messagepane').fadeOut(800).fadeIn(800);
END_SCRIPT;
            }
            
$message_script .= <<<END_SCRIPT
                // -->
                </script>
END_SCRIPT;
        }
        echo 
$message_script;
    }
}