Skip to content
Code-Schnipsel Gruppen Projekte
Manager.js 13,7 KiB
Newer Older
  • Learn to ignore specific revisions
  • 
    /**
     * Cron Manager
     *
    
     * @module package/quiqqer/cron/bin/Manager
    
     * @author www.pcsg.de (Henning Leutz)
    
     *
     * @require qui/QUI
     * @require qui/controls/desktop/Panel
     * @require qui/controls/windows/Confirm
     * @require qui/controls/buttons/Button
     * @require qui/controls/buttons/Seperator
     * @require controls/grid/Grid
     * @require Ajax
     * @require Locale
    
    define('package/quiqqer/cron/bin/Manager', [
    
    Henning Leutz's avatar
    Henning Leutz committed
        'qui/controls/desktop/Panel',
    
    Henning Leutz's avatar
    Henning Leutz committed
        'qui/controls/buttons/Button',
    
    Henning Leutz's avatar
    Henning Leutz committed
        'qui/controls/buttons/Seperator',
    
    Henning Leutz's avatar
    Henning Leutz committed
        'controls/grid/Grid',
    
    Henning Leutz's avatar
    Henning Leutz committed
    ],function(QUI, QUIPanel, QUIConfirm, QUIButton, QUIButtonSeperator, Grid, Ajax, Locale)
    
    Henning Leutz's avatar
    Henning Leutz committed
        "use strict";
    
    Henning Leutz's avatar
    Henning Leutz committed
        return new Class({
    
    
            Extends : QUIPanel,
    
            Type    : 'package/quiqqer/cron/bin/Manager',
    
    
            Binds : [
    
    Henning Leutz's avatar
    Henning Leutz committed
                '$onCreate',
                '$onResize'
    
    Henning Leutz's avatar
    Henning Leutz committed
            options : {
                title : 'Cron-Manager',
    
                icon  : 'icon-time'
    
            initialize : function(options)
            {
                this.parent( options );
    
                this.addEvents({
    
    Henning Leutz's avatar
    Henning Leutz committed
                    onCreate : this.$onCreate,
                    onResize : this.$onResize
                });
            },
    
            /**
             * Load the crons into the grid
             *
             * @return {self}
             */
            loadCrons : function()
            {
                var self = this;
    
                Ajax.get('package_quiqqer_cron_ajax_getList', function(result)
                {
    
                    if ( !self.$Grid ) {
                        return;
                    }
    
    
                    var execCron = function(Btn)
                    {
                        self.execCron(
                            Btn.getAttribute( 'cronId' )
                        );
                    };
    
                    var toggleCron = function(Btn)
                    {
                        self.toggleStatusOfCron(
                            Btn.getAttribute( 'cronId' )
                        );
                    };
    
                    for ( var i = 0, len = result.length; i < len; i++ )
    
                    {
                        result[ i ].status = {
                            title  : 'Cron aktivieren / deaktivieren',
    
                            icon   : result[ i ].active == 1 ? 'icon-ok' : 'icon-remove',
    
                            cronId : result[ i ].id,
    
    
                        result[ i ].play = {
                            name   : 'cron-play-button-'+ result[ i ].id,
                            title  : 'Cron ausführen',
                            icon   : 'icon-play',
                            cronId : result[ i ].id,
    
                    self.$Grid.setData({
                        data : result
                    });
    
    Henning Leutz's avatar
    Henning Leutz committed
    
                }, {
                    'package' : 'quiqqer/cron'
    
    Henning Leutz's avatar
    Henning Leutz committed
    
                return this;
    
            },
    
            /**
             * event : on Create
             */
            $onCreate : function()
            {
    
                var self = this;
    
    
    Henning Leutz's avatar
    Henning Leutz committed
                this.addButton(
                    new QUIButton({
    
    Henning Leutz's avatar
    Henning Leutz committed
                        text : 'Cron hinzufügen',
                        textimage : 'icon-plus',
                        events :
                        {
    
                            onClick : function() {
                                self.openAddCronWindow();
                            }
                        }
                    })
                );
    
                this.addButton( new QUIButtonSeperator() );
    
                this.addButton(
                    new QUIButton({
                        name : 'edit',
                        text : 'Markierten Cron editieren',
                        textimage : 'icon-pencil',
                        events :
                        {
                            onClick : function() {
                                self.editMarkedCron();
                            }
                        }
                    })
                );
    
    
                this.addButton(
                    new QUIButton({
    
                        text : 'Markierte(n) Cron(s) löschen',
    
                        textimage : 'icon-trash',
                        events :
                        {
                            onClick : function() {
                                self.deleteMarkedCrons();
    
    Henning Leutz's avatar
    Henning Leutz committed
                this.addButton( new QUIButtonSeperator() );
    
                this.addButton(
                    new QUIButton({
                        name : 'history',
                        text : 'Cron History',
                        textimage : 'icon-long-arrow-right',
                        events :
                        {
                            onClick : function() {
                                self.showHistory();
                            }
                        }
                    })
                );
    
    
                this.getButtons( 'edit' ).disable();
    
    Henning Leutz's avatar
    Henning Leutz committed
    
                    Container = new Element('div', {
                        'class' : 'box',
                        styles : {
                            width : '100%',
                            height : '100%'
                        }
                    }).inject( Content );
    
    
                this.$Grid = new Grid(Container, {
                    columnModel : [{
                        header    : 'Status',
    
                        dataIndex : 'status',
    
    Henning Leutz's avatar
    Henning Leutz committed
                        dataType  : 'button',
    
                    }, {
                        header    : '&nbsp;',
                        dataIndex : 'play',
                        dataType  : 'button',
    
                    }, {
                        header    : 'ID',
                        dataIndex : 'id',
                        dataType  : 'string',
                        width     : 50
    
    Henning Leutz's avatar
    Henning Leutz committed
                    }, {
                        header    : 'Cron-Name',
    
                        dataIndex : 'title',
    
    Henning Leutz's avatar
    Henning Leutz committed
                        dataType  : 'string',
                        width     : 150
                    }, {
                        header    : 'Min',
                        dataIndex : 'min',
                        dataType  : 'string',
                        width     : 50
                    }, {
                        header    : 'Std',
                        dataIndex : 'hour',
                        dataType  : 'string',
                        width     : 50
                    }, {
                        header    : 'Tag',
                        dataIndex : 'day',
                        dataType  : 'string',
                        width     : 50
                    }, {
                        header    : 'Monat',
                        dataIndex : 'month',
                        dataType  : 'string',
                        width     : 50
    
                    }, {
                        header    : 'Exec',
                        dataIndex : 'exec',
                        dataType  : 'string',
                        width     : 150
    
    Henning Leutz's avatar
    Henning Leutz committed
                    }, {
                        header    : 'Parameter',
                        dataIndex : 'params',
                        dataType  : 'string',
                        width     : 150
    
                    }, {
                        header    : 'Cron-Beschreibung',
                        dataIndex : 'desc',
                        dataType  : 'string',
                        width     : 200
    
                    }],
                    multipleSelection : true,
                    pagination : true
                });
    
                this.$Grid.addEvents({
                    onRefresh : function() {
                        self.loadCrons();
                    },
                    onClick : function()
                    {
    
                        var delButton  = self.getButtons( 'delete' ),
                            editButton = self.getButtons( 'edit' ),
                            selected   = self.$Grid.getSelectedIndices().length;
    
                        if ( selected == 1 )
                        {
                            editButton.enable();
                        } else
                        {
                            editButton.disable();
                        }
    
                        if ( selected )
    
                    },
    
                    onDblClick : function(data)
                    {
                        var rowData = self.$Grid.getDataByRow( data.row );
    
                        self.editCron( rowData.id );
    
    Henning Leutz's avatar
    Henning Leutz committed
                });
    
                this.loadCrons();
            },
    
            /**
             * event : on resize
             */
            $onResize : function()
            {
                if ( !this.$Grid ) {
                    return;
                }
    
    Henning Leutz's avatar
    Henning Leutz committed
                var Content = this.getContent(),
                    size    = Content.getSize();
    
    Henning Leutz's avatar
    Henning Leutz committed
                this.$Grid.setHeight( size.y - 40 );
                this.$Grid.setWidth( size.x - 40 );
    
            },
    
            /**
             * Open the delete marked cron windows and delete all marked crons
             *
             * @return {self}
             */
            deleteMarkedCrons : function()
            {
    
                if ( !this.$Grid ) {
                    return this;
                }
    
                var self = this,
                    data = this.$Grid.getSelectedData();
    
                if ( !data.length ) {
                    return this;
                }
    
                var ids = data.map(function(o) {
                    return o.id;
                });
    
                new QUIConfirm({
                    icon : 'icon-remove',
                    title : 'Cron Einträge löschen',
                    text : '<p>Möchten Sie folgende Cron Einträge wirklich löschen?</p>',
                    information : '<p>Zu löschende Cron-Einträge: <b>'+ ids.join(',') +'</b></p>' +
                                  '<p>Beachten Sie, die Cron-Einträge sind nicht wieder herstellbar</p>',
                    events :
                    {
                        onSubmit : function(Win)
                        {
                            Win.Loader.show();
    
                            Ajax.post('package_quiqqer_cron_ajax_delete', function()
                            {
                                Win.close();
                                self.loadCrons();
                            }, {
                                'package' : 'quiqqer/cron',
                                ids : JSON.encode( ids )
                            });
                        }
                    }
                }).open();
    
             * Edit a cron, opens the cron Edit-Window
             *
             * @param {String} cronId - ID of the Cron
             */
            editCron : function(cronId)
            {
                var self = this;
    
                require(['package/quiqqer/cron/bin/CronWindow'], function(Window)
                {
                     new Window({
                         cronId : cronId,
                         events :
                         {
                             onSubmit : function() {
                                 self.loadCrons();
                             }
                         }
                     }).open();
                });
    
                return this;
            },
    
            /**
             * Opens the Edit-Window for the marked cron
             */
            editMarkedCron : function()
            {
                if ( !this.$Grid ) {
                    return this;
                }
    
    
                var data = this.$Grid.getSelectedData();
    
    
                if ( !data.length ) {
                    return this;
                }
    
                this.editCron( data[ 0 ].id );
            },
    
            /**
             * Open the add Cron-Window
    
             *
             * @return {self}
             */
            openAddCronWindow : function()
            {
    
                require(['package/quiqqer/cron/bin/CronWindow'], function(Window)
    
                             onSubmit : function() {
                                 self.loadCrons();
                             }
                         }
                     }).open();
    
                });
    
                return this;
            },
    
            /**
             * Change the cron status
             * If the cron is active to deactive
             * If the cron is deactive to active
             *
    
             * @param {Number} cronId - ID of the Cron
    
             * @return {self}
             */
            toggleStatusOfCron : function(cronId)
            {
    
                Ajax.post('package_quiqqer_cron_ajax_cron_toggle', function()
    
                }, {
                    'package' : 'quiqqer/cron',
                    cronId    : cronId
                });
    
                return this;
    
             * @param {Number} cronId - ID of the Cron
    
             * @return {self}
             */
            execCron : function(cronId)
            {
                var i, len;
                var buttons = [];
    
                if ( this.$Grid ) {
                    buttons = QUI.Controls.get( 'cron-play-button-'+ cronId );
                }
    
                for ( i = 0, len = buttons.length; i < len; i++ ) {
                    buttons[ i ].setAttribute('icon', 'icon-refresh icon-spin');
                }
    
                Ajax.post('package_quiqqer_cron_ajax_cron_executeCron', function()
    
                {
                    for ( i = 0, len = buttons.length; i < len; i++ ) {
                        buttons[ i ].setAttribute('icon', 'icon-play');
                    }
    
                }, {
                    'package' : 'quiqqer/cron',
                    cronId    : cronId
                });
    
    Henning Leutz's avatar
    Henning Leutz committed
            },
    
            /**
             * Show the Cron-History Panel
             */
            showHistory : function()
            {
                var self = this;
    
                require(['package/quiqqer/cron/bin/History'], function(Panel) {
                    new Panel().inject( self.getParent() );
                });