<?php
// Copyright 1999-2014. Parallels IP Holdings GmbH. All Rights Reserved.
?>

<form id="serviceRemovalForm" action="<?php echo $this->baseUrl('/wordpress/remove'); ?>" method="POST">
    <input type="hidden" id="id" name="id" value="" />
    <input type="hidden" id="service_removal_forgery_protection_token" name="forgery_protection_token" value="" />
</form>

<script type="text/javascript">
    //<![CDATA[
    Jsw.namespace('Jsw.WordpressPopupManager');
    Jsw.namespace('Jsw.Views.Wordpress');

    Jsw.Views.Wordpress.remove = function(id) {
        Jsw.messageBox.show({
            'type': Jsw.messageBox.TYPE_YESNO,
            'subtype': 'delete',
            'text': <?php echo !is_null($this->wordpressInstance->apsInstanceId) ? $this->jsLmsg('removeConfirmationTitle') : $this->jsLmsg('detachConfirmationTitle'); ?>,
            'description': <?php echo !is_null($this->wordpressInstance->apsInstanceId) ? $this->jsLmsg('removeConfirmationDescription') : $this->jsLmsg('detachConfirmationDescription'); ?>,
            'onYesClick': function() {
                var form = $('serviceRemovalForm');
                form.down('#service_removal_forgery_protection_token').value = $('forgery_protection_token').content;
                form.down('#id').value = id;
                form.submit();
            },
            'buttonTitles': {
                'yes':  <?php echo $this->jsLmsg('buttonConfirmationYes')?>,
                'no': <?php echo $this->jsLmsg('buttonConfirmationNo')?>
            }
        })
    };

    Jsw.onReady(function() {

        <?php if (!$this->wordpressInstance->isAlive()): ?>
            Jsw.addStatusMessage('error', '<?php echo Plesk_Base_Utils_String::safeForJs($this->wordpressInstance->getError()); ?>');
        <?php endif; ?>

        var generalItems = (function() {
            var items = [];

            items.push ({
                componentType: 'Jsw.form.DisplayField',
                fieldLabel: <?php echo $this->jsLmsg('fieldGeneralName'); ?>,
                value: <?php echo $this->jsEscape($this->wordpressInstance->name); ?>
            });

            items.push ({
                componentType: 'Jsw.form.DisplayField',
                fieldLabel: <?php echo $this->jsLmsg('fieldGeneralUrl'); ?>,
                value: '<a target="__blank" href="' + <?php echo $this->jsEscape($this->wordpressInstance->url); ?> + '">' + <?php echo $this->jsEscape($this->wordpressInstance->url); ?> + '</a>'
            });

            items.push ({
                componentType: 'Jsw.form.DisplayField',
                fieldLabel: <?php echo $this->jsLmsg('fieldGeneralInstallPath'); ?>,
                value: <?php echo $this->jsEscape($this->wordpressInstance->getInstallPath()); ?>
            });

            var manageIcon = '<i class="icon"><img src="' + Jsw.skinUrl + '/icons/16/plesk/manage.png" alt="manage" title=""></i> ';
            <?php if (!is_null($this->wordpressAdministratorLogin)): ?>
                var administratorUsername = <?php echo $this->jsEscape($this->wordpressAdministratorLogin); ?> + ' ';
            <?php else: ?>
                var administratorUsername = '';
            <?php endif; ?>

            items.push ({
                componentType: 'Jsw.form.DisplayField',
                fieldLabel: <?php echo $this->jsLmsg('fieldGeneralAdministrator'); ?>,
                value: administratorUsername +
                    '<a class="i-link" href="<?php echo $this->baseUrl('/wordpress/account/id/' . $this->wordpressInstance->id); ?>">' +
                        manageIcon +
                        '<span>' +
                            <?php echo $this->jsLmsg('fieldGeneralAdministratorManage'); ?> +
                        '</span>' +
                    '</a>'
            });

            items.push({
                componentType: 'Jsw.form.DisplayField',
                fieldLabel: <?php echo $this->jsLmsg('fieldGeneralAutoUpdates'); ?>,
                value: '<i class="icon"><img src="' + Jsw.skinUrl + '/icons/16/plesk/'
                    + <?php if ($this->autoUpdates): ?> 'on.png' <?php else: ?> 'subscription-status-suspended.png' <?php endif; ?> + '"></i> '
                    + <?php echo ($this->autoUpdates) ? $this->jsLmsg('autoUpdatesOn') : $this->jsLmsg('autoUpdatesOff'); ?>
                    <?php if ($this->isAutoUpdatesManagementAvailable): ?>
                    + ' <a href="' + Jsw.baseUrl + '/wordpress/switch-auto-updates'
                    + <?php if ($this->autoUpdates): ?> '/enable/0' <?php else: ?> '/enable/1' <?php endif; ?>
                    + '/id/' + <?php echo $this->jsEscape($this->wordpressInstance->id); ?>
                    + '">' + <?php echo ($this->autoUpdates) ? $this->jsLmsg('disableAutoUpdates') : $this->jsLmsg('enableAutoUpdates') ?>
                    + '</a>'
                    <?php endif; ?>
            });

            return items;
        }());

        var settingsItems = (function() {

            var items = [];
            <?php foreach ($this->settingsInfo as $setting): ?>
                items.push({
                componentType: 'Jsw.form.DisplayField',
                fieldLabel: <?php echo $this->jsEscape($setting['label']); ?>,
                value:
                    <?php if (is_array($setting['value'])): ?>
                        <?php
                            $value = '';
                            foreach ($setting['value'] as $val) {
                                if ('' !== $value) {
                                    $value .= '<br>';
                                }
                                $value .= $val;
                            }
                        ?>
                        <?php echo $this->jsEscape($value); ?>
                    <?php elseif ($setting['bool']): ?>
                        <?php echo $this->jsHtml($this->switchState('true' == $setting['value'])); ?>
                    <?php elseif (isset($setting['password']) && $setting['password']): ?>
                        '<span id="applicationAdminPasswordText" style="display:none;">' + <?php echo $this->jsEscape($setting['value']); ?> + '</span>' +
                        ' <a href="#" id="applicationAdminPasswordToggle" class="toggler">' +
                            <?php echo $this->jsLmsg('showPassword'); ?> +
                        '</a>'
                    <?php else: ?>
                        <?php echo $this->jsEscape($setting['value']); ?>
                    <?php endif; ?>
                });
            <?php endforeach; ?>

            return items;
        }());

        var tools = (function() {
            var items = [];

            var dashboardIcon = '<i class="icon"><img src="' + Jsw.skinUrl + '/icons/16/plesk/website.png" alt="dashboard" title=""></i>';
            <?php if (!is_null($this->dashboardUrl)): ?>
            items.push ({
                componentType: 'Jsw.form.DisplayField',
                value: '<a class="i-link" target="__blank" href="<?php echo $this->dashboardUrl; ?>">' + dashboardIcon + ' <span>' + <?php echo $this->jsLmsg('linkDashboard'); ?> + '</span></a>',
                singleRow: true
            });
            <?php else: ?>
            items.push ({
                componentType: 'Jsw.form.DisplayField',
                value: '<a class="i-link" href="<?php echo $this->baseUrl('/wordpress/account/id/' . $this->wordpressInstance->id); ?>">' + dashboardIcon + ' <span>' + <?php echo $this->jsLmsg('linkDashboard'); ?> + '</span></a>',
                singleRow: true
            });
            <?php endif; ?>

            var manageIcon = '<i class="icon"><img src="' + Jsw.skinUrl + '/icons/16/plesk/theme.png" alt="manage" title=""></i>';
            items.push ({
                componentType: 'Jsw.form.DisplayField',
                value: '<a class="i-link" href="#" id="linkManageThemes">' + manageIcon + ' <span>' + <?php echo $this->jsLmsg('linkManageThemes'); ?> + '</span></a>',
                singleRow: true
            });

            var manageIcon = '<i class="icon"><img src="' + Jsw.skinUrl + '/icons/16/plesk/components.png" alt="manage" title=""></i>';
            items.push ({
                componentType: 'Jsw.form.DisplayField',
                value: '<a class="i-link" href="#" id="linkManagePlugins">' + manageIcon + ' <span>' + <?php echo $this->jsLmsg('linkManagePlugins'); ?> + '</span></a>',
                singleRow: true
            });
            <?php if ($this->showSecureButton): ?>
            var manageIcon = '<i class="icon"><img src="' + Jsw.skinUrl + '/icons/16/plesk/security-scan.png" alt="manage" title=""></i>';
            items.push ({
                componentType: 'Jsw.form.DisplayField',
                value: '<a class="i-link" href="#" id="linkSecurityScan">' + manageIcon + ' <span>' + <?php echo $this->jsLmsg('linkSecurityScan'); ?> + '</span></a>',
                singleRow: true
            });
            <?php endif; ?>
            return items;
        }());

        var firstColumnItems = (function() {
            var firstColumnItems = [];

            firstColumnItems.push({
                componentType: 'Jsw.Panel',
                'title': <?php echo $this->jsLmsg('generalSectionTitle') ?>,
                cls: 'form-box',
                items: generalItems
            });

            <?php if ($this->settingsInfo): ?>
            firstColumnItems.push({
                componentType: 'Jsw.Panel',
                'title': <?php echo $this->jsLmsg('settingsSectionTitle') ?>,
                cls: 'form-box',
                items: settingsItems
            });
            <?php endif; ?>

            return firstColumnItems;
        }());

        var secondColumnItems = (function() {
            var secondColumnItems = [];

            secondColumnItems.push({
                componentType: 'Jsw.Panel',
                'title': <?php echo $this->jsLmsg('entryPointsSectionTitle') ?>,
                cls: 'form-box',
                items: tools
            });

            return secondColumnItems;
        }());

        new Jsw.Container({
            renderTo: 'main',
            items: [
                new Jsw.Hint({
                    hint: <?php echo $this->jsLmsg('pageHint'); ?>
                }),

                new Jsw.SmallTools({
                    operations: [
                        <?php if ($this->wordpressInstance->isAlive() && !is_null($this->wordpressInstance->apsInstanceId)): ?>
                        {
                            componentType: 'Jsw.SmallButton',
                            title: <?php echo $this->jsLmsg('settingsButton') ?>,
                            addCls: 'sb-app-configure',
                            href: '/wordpress/configure/id/' + '<?php echo $this->wordpressInstance->id; ?>'
                        },
                        <?php endif; ?>
                        {
                            componentType: 'Jsw.SmallButton',
                            title: <?php echo !is_null($this->wordpressInstance->apsInstanceId) ? $this->jsLmsg('removeButton') : $this->jsLmsg('detachButton'); ?>,
                            addCls: 'sb-app-delete',
                            handler: function(event) {
                                Jsw.Views.Wordpress.remove(
                                    <?php echo $this->jsEscape($this->wordpressInstance->id); ?>,
                                    <?php echo ($this->service['rootService'] ? 'true' : 'false') ?>
                                );
                            }
                        }, {
                            componentType: 'Jsw.SmallButton',
                            title: <?php echo $this->jsLmsg('clearCacheButton'); ?>,
                            addCls: 'sb-refresh',
                            href: '/wordpress/clear-cache/id/' + '<?php echo $this->wordpressInstance->id; ?>'
                        }
                    ]
                }),

                <?php if ($this->wordpressInstance->isAlive()): ?>
                new Jsw.layout.TwoColumns({
                    items: [
                        new Jsw.Container({
                            items: firstColumnItems
                        }),
                        new Jsw.Container({
                            items: secondColumnItems
                        })
                    ]
                })
                <?php else: ?>
                new Jsw.Panel({
                	cls: 'list-box',
	                'title': <?php echo $this->jsLmsg('generalSectionTitle') ?>,
	                items: [{
	                    componentType: 'Jsw.form.DisplayField',
	                    fieldLabel: <?php echo $this->jsLmsg('fieldGeneralInstallPath'); ?>,
	                    value: <?php echo $this->jsEscape($this->wordpressInstance->getInstallPath()); ?>,
	                }]
                })
                <?php endif; ?>

            ]
        });

        var buttonAdminPassword = $('applicationAdminPasswordToggle');
        if (buttonAdminPassword) {
            buttonAdminPassword.observe('click', function(event){
                Event.stop(event);
                var element = $('applicationAdminPasswordText');
                if (element.visible()) {
                    element.hide();
                    this.update(<?php echo $this->jsLmsg('showPassword') ?>);
                } else {
                    element.show();
                    this.update(<?php echo $this->jsLmsg('hidePassword') ?>);
                }

            });
        }

        var buttonManageThemes = $('linkManageThemes');
        if (buttonManageThemes) {
            buttonManageThemes.observe('click', function(event) {
                Event.stop(event);
                Jsw.WordpressPopupManager.manageThemes(
                    [<?php echo $this->wordpressInstance->id; ?>],
                    <?php echo $this->jsEscape($this->wordpressInstance->name); ?>,
                    <?php echo Zend_Json::encode(Zend_Registry::get('translate')->getSection('controllers.wordpress.manage-themes')); ?>,
                    <?php echo is_null($this->wordpressDashboardUrl) ? 'null' : $this->jsEscape($this->baseUrl($this->wordpressDashboardUrl)); ?>,
                    <?php echo $this->jsEscape($this->baseUrl('/wordpress/detail/id/' . $this->wordpressInstance->id)); ?>
                );
            });
        }
        var buttonManagePlugins = $('linkManagePlugins');
        if (buttonManagePlugins) {
            buttonManagePlugins.observe('click', function(event) {
                Event.stop(event);
                Jsw.WordpressPopupManager.managePlugins(
                    [<?php echo $this->wordpressInstance->id; ?>],
                    <?php echo $this->jsEscape($this->wordpressInstance->name); ?>,
                    <?php echo Zend_Json::encode(Zend_Registry::get('translate')->getSection('controllers.wordpress.manage-plugins')); ?>,
                    <?php echo is_null($this->wordpressDashboardUrl) ? 'null' : $this->jsEscape($this->baseUrl($this->wordpressDashboardUrl)); ?>,
                    <?php echo $this->jsEscape($this->baseUrl('/wordpress/detail/id/' . $this->wordpressInstance->id)); ?>
                );
            });
        }
        var buttonSecutiryScan = $('linkSecurityScan');
        if (buttonSecutiryScan) {
            buttonSecutiryScan.observe('click', function(event) {
                Event.stop(event);
                Jsw.WordpressPopupManager.securityScan(
                    [<?php echo $this->wordpressInstance->id; ?>],
                    <?php echo $this->jsEscape($this->wordpressInstance->name); ?>,
                    <?php echo Zend_Json::encode(Zend_Registry::get('translate')->getSection('components.wordpress.check')); ?>
                );
            });
        }

    });
    //]]>
</script>
