{
    "type": "Ext.app.Controller",
    "reference": {
        "name": "items",
        "type": "array"
    },
    "codeClass": null,
    "userConfig": {
        "designer|userClassName": "GroupController"
    },
    "designerId": "03252d20-6089-4b14-b9f1-76585a66d848",
    "cn": [
        {
            "type": "controllerref",
            "reference": {
                "name": "items",
                "type": "array"
            },
            "codeClass": null,
            "userConfig": {
                "ref": "newGroupButton",
                "selector": "#newGroupButton"
            },
            "designerId": "2f5e3f00-0f75-4b14-9392-4f42f9c57ad6"
        },
        {
            "type": "controllerref",
            "reference": {
                "name": "items",
                "type": "array"
            },
            "codeClass": null,
            "userConfig": {
                "ref": "deleteGroupButton",
                "selector": "#deleteGroupButton"
            },
            "designerId": "8aee2edb-06e6-407b-ba5b-5237747ca304"
        },
        {
            "type": "controllerref",
            "reference": {
                "name": "items",
                "type": "array"
            },
            "codeClass": null,
            "userConfig": {
                "ref": "editRuleNewGroupButton",
                "selector": "#editRuleNewGroupButton"
            },
            "designerId": "113b3e5d-b4ae-4ce8-8776-7181077c773a"
        },
        {
            "type": "controllerref",
            "reference": {
                "name": "items",
                "type": "array"
            },
            "codeClass": null,
            "userConfig": {
                "ref": "editRuleGroup",
                "selector": "#editRuleGroup"
            },
            "designerId": "c519d601-f75f-46ff-8133-de456ca31b95"
        },
        {
            "type": "controllerref",
            "reference": {
                "name": "items",
                "type": "array"
            },
            "codeClass": null,
            "userConfig": {
                "autoCreate": true,
                "ref": "newGroupWindow",
                "selector": "#newGroupWindow",
                "xtype": "newGroupWindowAlias"
            },
            "designerId": "2e77a5f6-fea6-4a14-9db0-c09cd8a8cc97"
        },
        {
            "type": "controllerref",
            "reference": {
                "name": "items",
                "type": "array"
            },
            "codeClass": null,
            "userConfig": {
                "ref": "newGroupName",
                "selector": "#newGroupName"
            },
            "designerId": "3ecfacbe-9ce7-47af-81bd-9b8287eab1bc"
        },
        {
            "type": "controllerref",
            "reference": {
                "name": "items",
                "type": "array"
            },
            "codeClass": null,
            "userConfig": {
                "ref": "newGroupConfirmButton",
                "selector": "#newGroupConfirmButton"
            },
            "designerId": "3934e71c-c026-4272-9b7d-22f639aa7769"
        },
        {
            "type": "controllerref",
            "reference": {
                "name": "items",
                "type": "array"
            },
            "codeClass": null,
            "userConfig": {
                "ref": "newGroupCancelButton",
                "selector": "#newGroupCancelButton"
            },
            "designerId": "82fbb2d2-6865-41f3-b3b8-bad2876e94a1"
        },
        {
            "type": "controllerref",
            "reference": {
                "name": "items",
                "type": "array"
            },
            "codeClass": null,
            "userConfig": {
                "ref": "treegrid",
                "selector": "#treegrid"
            },
            "designerId": "726b32f3-cc79-405d-a92f-03f89c81ac7d"
        },
        {
            "type": "fixedfunction",
            "reference": {
                "name": "items",
                "type": "array"
            },
            "codeClass": null,
            "userConfig": {
                "designer|params": [
                    "application"
                ],
                "fn": "init",
                "implHandler": [
                    "//open window on new group button\r",
                    "this.control({\r",
                    "    '#newGroupButton': {\r",
                    "        click: this.openCreateNew\r",
                    "    },\r",
                    "\t'#editGroupButton': {\r",
                    "\t\tclick: function() {\r",
                    "\t\t\tthis.getNewGroupWindow().show();\r",
                    "\t\t\tthis.getNewGroupWindow().setTitle('Edit group');\r",
                    "\t\t\tthis.getNewGroupName().setValue(this.tmp_selected[0].data.name);\r",
                    "\t\t\tthis.getNewGroupName().focus();\r",
                    "\t\t\tthis.getNewGroupConfirmButton().hide();\r",
                    "\t\t\tthis.getEditGroupConfirmButton().show();\r",
                    "\t\t\tthis.tmp_method = \"EDIT\";\r",
                    "\t\t\t//console.log(\"Edit: \" + this.tmp_selected[0].data.name);\r",
                    "\t\t}\r",
                    "\t},\r",
                    "\t'#editGroupConfirmButton': {\r",
                    "\t\tclick: function() {\r",
                    "\t\t\tthis.editGroup();\r",
                    "\t\t}\r",
                    "\t},\r",
                    "    '#editRuleNewGroupButton': {\r",
                    "        click: this.openCreateNew\r",
                    "    },\r",
                    "\t'#treegrid': {\r",
                    "        selectionchange: function(model, selected, eOpts) {\r",
                    "\t\t\tthis.tmp_selected = selected;\r",
                    "        }\r",
                    "    },\r",
                    "//close window on cancel\r",
                    "    '#newGroupCancelButton': {\r",
                    "        click: function() {\r",
                    "            this.getNewGroupWindow().destroy();\r",
                    "        }\r",
                    "    },\r",
                    "//render confirm button active and inactive\r",
                    "    '#newGroupName': {\r",
                    "        change: function(component, newValue, oldValue) {\r",
                    "\t\t\tif(newValue.length > 0 && this.isGroupNew(newValue)) {\r",
                    "\t\t\t\tif(this.tmp_method === 'NEW') {\r",
                    "\t\t\t\t\tthis.getNewGroupConfirmButton().enable();\r",
                    "\t\t\t\t}\r",
                    "\t\t\t\telse if(this.tmp_method === 'EDIT') {\r",
                    "\t\t\t\t\tthis.getEditGroupConfirmButton().enable();\r",
                    "\t\t\t\t}\r",
                    "\t\t\t}\r",
                    "\t\t\telse {\r",
                    "\t\t\t\tthis.getNewGroupConfirmButton().disable();\r",
                    "\t\t\t\tthis.getEditGroupConfirmButton().disable();\r",
                    "\t\t\t}\r",
                    "        },\r",
                    "\t\tspecialkey: function(field, e) {\r",
                    "\t\t\tif (e.getKey() == e.ENTER) {\r",
                    "\t\t\t\tif (this.tmp_method === 'NEW') {\r",
                    "\t\t\t\t\tif(!this.getNewGroupConfirmButton().isDisabled())\r",
                    "\t\t\t\t\t\tthis.createGroup();\r",
                    "\t\t\t\t} else if (this.tmp_method === 'EDIT') {\r",
                    "\t\t\t\t\tif(!this.getEditGroupConfirmButton().isDisabled())\r",
                    "\t\t\t\t\t\tthis.editGroup();\r",
                    "\t\t\t\t}\r",
                    "\t\t\t}\r",
                    "\t\t}\r",
                    "    },\r",
                    "//confirm new response\r",
                    "    '#newGroupConfirmButton': {\r",
                    "        click: function() {\r",
                    "            this.createGroup();\r",
                    "        }\r",
                    "    },\r",
                    "//delete group button\r",
                    "    '#deleteGroupButton': {\r",
                    "        click: function() {\r",
                    "            this.deleteGroup();\r",
                    "        }\r",
                    "    },\r",
                    "\t//Activate/Deactivate group\r",
                    "\t'#activateGroupButton': {\r",
                    "\t\tclick: function(btn) {\r",
                    "\t\t\tbtn.disable();\r",
                    "\t\t\tthis.activateGroup();\r",
                    "\t\t}\r",
                    "\t},\r",
                    "\t'#deactivateGroupButton': {\r",
                    "\t\tclick: function(btn) {\r",
                    "\t\t\tbtn.disable();\r",
                    "\t\t\tthis.deactivateGroup();\r",
                    "\t\t}\r",
                    "\t}\r",
                    "});"
                ]
            },
            "designerId": "396e5cba-48c9-4239-817a-a14dd134f003"
        },
        {
            "type": "basicfunction",
            "reference": {
                "name": "items",
                "type": "array"
            },
            "codeClass": null,
            "userConfig": {
                "designer|params": [
                    "btn"
                ],
                "fn": "openCreateNew",
                "implHandler": [
                    "this.getNewGroupWindow().animateTarget = btn;\r",
                    "this.getNewGroupWindow().show();\r",
                    "this.tmp_method = \"NEW\";\r",
                    "\r",
                    "this.getNewGroupName().focus();\r",
                    ""
                ]
            },
            "designerId": "e80e3a60-0ca3-46eb-a1df-1ae5e1402254"
        },
        {
            "type": "basicfunction",
            "reference": {
                "name": "items",
                "type": "array"
            },
            "codeClass": null,
            "userConfig": {
                "fn": "createGroup",
                "implHandler": [
                    "var params = Ext.Object.fromQueryString(document.location.search);\r",
                    "params.func = 'NEW';\r",
                    "params.group = this.getNewGroupName().getValue();\r",
                    "params.ruletype = \"crit_role\";\r",
                    "\r",
                    "this.getNewGroupWindow().disable();\r",
                    "\r",
                    "Ext.Ajax.request({ //manual AJAX POST\r",
                    "\turl: '../../service/sod_get_possible_group.php',\r",
                    "\tmethod: 'POST',\r",
                    "\tparams: params,\r",
                    "\tjsonData: Ext.JSON.encode(params),\r",
                    "\tsuccess: function(response, object){\r",
                    "\t\tvar newId = response.responseText;\r",
                    "\r",
                    "\t\tthis.getTreegrid().getStore().load({ //reload tree works every time\r",
                    "\t\t\tcallback: function() {\r",
                    "\t\t\t\tvar newRecord = this.getTreegrid().getStore().getRootNode().findChild('id', newId);\r",
                    "\t\t\t\tnewRecord.expand();\r",
                    "\r",
                    "\t\t\t\tthis.getNewGroupWindow().destroy(); //close create dialog\r",
                    "\r",
                    "\t\t\t\tthis.getTreegrid().getSelectionModel().doSelect(newRecord);\r",
                    "\t\t\t},\r",
                    "\t\t\tscope: this\r",
                    "\t\t});\r",
                    "\r",
                    "\t\tExt.getStore('SelectGroupStore').load();\r",
                    "\r",
                    "\t\t//set new id as the choice in edit/create rule window\r",
                    "\t\tif(this.getEditRuleGroup() !== undefined && this.getEditRuleGroup() !== null) {\r",
                    "\t\t\tthis.getEditRuleGroup().setValue(newId);\r",
                    "\t\t}\r",
                    "\t},\r",
                    "\tfailure: function(response, object){\r",
                    "\t\tvar text = response.responseText;\r",
                    "\t\tExt.Error.raise({\r",
                    "\t\t\tmsg: 'Failed to get report parameters',\r",
                    "\t\t\toption: text,   // whatever was passed into the method\r",
                    "\t\t\t'object': object // other arbitrary info\r",
                    "\t\t});\r",
                    "\t},\r",
                    "\tscope: this\r",
                    "});"
                ]
            },
            "designerId": "0c94fa2f-a73b-4b4a-9418-09b8e9d401b0"
        },
        {
            "type": "basicfunction",
            "reference": {
                "name": "items",
                "type": "array"
            },
            "codeClass": null,
            "userConfig": {
                "fn": "deleteGroup",
                "implHandler": [
                    "var group = this.getTreegrid().getSelectionModel().selected.getAt(0);\r",
                    "\r",
                    "if(group.hasChildNodes()) {\r",
                    "\tExt.Msg.alert(CCS.Translator.getText('Error deleting group {1} is not empty!', [group.data.name]));\r",
                    "} else {\r",
                    "\tvar question = CCS.Translator.getText(\"Are you sure you want to delete {1}?\", [group.data.name]);\r",
                    "\tExt.MessageBox.confirm(CCS.Translator.getText('Delete group'), question, function(btn){\r",
                    "\t\tif(btn === 'yes'){\r",
                    "\t\t\tgroup.data.ruleId = \"Delete group\";\r",
                    "\t\t\tgroup.remove(false);\r",
                    "\t\t}\r",
                    "\t});\r",
                    "}\r",
                    ""
                ]
            },
            "designerId": "9dca4113-4c59-4c82-bb18-f0a3c50a7a52"
        },
        {
            "type": "basicfunction",
            "reference": {
                "name": "items",
                "type": "array"
            },
            "codeClass": null,
            "userConfig": {
                "fn": "activateGroup",
                "implHandler": [
                    "this.getTreegrid().setLoading(CCS.Translator.getText('Loading...'));\r",
                    "\r",
                    "var store = this.getTreegrid().getStore();\r",
                    "store.suspendAutoSync();\r",
                    "\r",
                    "var group  = this.getTreegrid().getSelectionModel().getSelection()[0];\r",
                    "\r",
                    "var nochanges = true;\r",
                    "group.eachChild(function(risk) {\r",
                    "\tif(risk.get('statusId') != 'Active') {\r",
                    "\t\trisk.set('ruleId', \"Update rule\");\r",
                    "\t\trisk.set('statusId', 'Active');\r",
                    "\t\tnochanges = false;\r",
                    "\t}\r",
                    "});\r",
                    "\r",
                    "var model = this.getTreegrid().getSelectionModel();\r",
                    "store.resumeAutoSync();\r",
                    "if(nochanges)\r",
                    "\tthis.getTreegrid().setLoading(false);\r",
                    "\r",
                    "store.sync({\r",
                    "\tcallback: function(){\r",
                    "\t\tthis.getTreegrid().setLoading(false);\r",
                    "\t\tstore.load({\r",
                    "\t\t\tcallback: function(records) {\r",
                    "\t\t\t\tExt.Array.each(records, function(rec){\r",
                    "\t\t\t\t\tif(rec.get('id') == group.get('id'))\r",
                    "\t\t\t\t\t\tmodel.doSelect(rec);\r",
                    "\t\t\t\t});\r",
                    "\t\t\t}\r",
                    "\t\t});\r",
                    "\t}, scope: this\r",
                    "});"
                ]
            },
            "designerId": "bcd3e9b1-6019-4a83-82a1-afe18bcd04a9"
        },
        {
            "type": "basicfunction",
            "reference": {
                "name": "items",
                "type": "array"
            },
            "codeClass": null,
            "userConfig": {
                "fn": "deactivateGroup",
                "implHandler": [
                    "this.getTreegrid().setLoading(CCS.Translator.getText('Loading...'));\r",
                    "\r",
                    "var store = this.getTreegrid().getStore();\r",
                    "store.suspendAutoSync();\r",
                    "\r",
                    "var group  = this.getTreegrid().getSelectionModel().getSelection()[0];\r",
                    "\r",
                    "var nochanges = true;\r",
                    "group.eachChild(function(risk) {\r",
                    "\tif(risk.get('statusId') != 'Inactive') {\r",
                    "\t\trisk.set('ruleId', \"Update rule\");\r",
                    "\t\trisk.set('statusId', 'Inactive');\r",
                    "\t\tnochanges = false;\r",
                    "\t}\r",
                    "});\r",
                    "\r",
                    "var model = this.getTreegrid().getSelectionModel();\r",
                    "store.resumeAutoSync();\r",
                    "if(nochanges)\r",
                    "\tthis.getTreegrid().setLoading(false);\r",
                    "\r",
                    "store.sync({\r",
                    "\tcallback: function(){\r",
                    "\t\tthis.getTreegrid().setLoading(false);\r",
                    "\t\tstore.load({\r",
                    "\t\t\tcallback: function(records) {\r",
                    "\t\t\t\tExt.Array.each(records, function(rec){\r",
                    "\t\t\t\t\tif(rec.get('id') == group.get('id'))\r",
                    "\t\t\t\t\t\tmodel.doSelect(rec);\r",
                    "\t\t\t\t});\r",
                    "\t\t\t}\r",
                    "\t\t});\r",
                    "\t}, scope: this\r",
                    "});"
                ]
            },
            "designerId": "e4d4d997-9513-40c4-8d20-88178d7facf8"
        },
        {
            "type": "controllerref",
            "reference": {
                "name": "items",
                "type": "array"
            },
            "codeClass": null,
            "userConfig": {
                "ref": "editGroupConfirmButton",
                "selector": "#editGroupConfirmButton"
            },
            "designerId": "02e38426-18c4-4a65-855e-29f31f604b8b"
        },
        {
            "type": "controllerref",
            "reference": {
                "name": "items",
                "type": "array"
            },
            "codeClass": null,
            "userConfig": {
                "ref": "editGroupButton",
                "selector": "#editGroupButton"
            },
            "designerId": "e8b5308e-422a-4fb4-80b7-8f0dd3739e09"
        },
        {
            "type": "basicfunction",
            "reference": {
                "name": "items",
                "type": "array"
            },
            "codeClass": null,
            "userConfig": {
                "fn": "editGroup",
                "implHandler": [
                    "var params = Ext.Object.fromQueryString(document.location.search);\r",
                    "params.func = 'EDIT';\r",
                    "params.group = this.getNewGroupName().getValue();\r",
                    "params.ruletype = \"sod_role2\";\r",
                    "//console.log(this.tmp_selected[0].data.id);\r",
                    "params.id = this.tmp_selected[0].data.id;\r",
                    "\r",
                    "this.getNewGroupWindow().disable();\r",
                    "\r",
                    "Ext.Ajax.request({ //manual AJAX POST\r",
                    "\turl: '../../service/sod_get_possible_group.php',\r",
                    "\tmethod: 'POST',\r",
                    "\tparams: params,\r",
                    "\tjsonData: Ext.JSON.encode(params),\r",
                    "\tsuccess: function(response, object){\r",
                    "\t\tvar newId = response.responseText;\r",
                    "\r",
                    "\t\tthis.getTreegrid().getStore().load({ //reload tree works every time\r",
                    "\t\t\tcallback: function() {\r",
                    "\t\t\t\tvar newRecord = this.getTreegrid().getStore().getRootNode().findChild('id', newId);\r",
                    "\t\t\t\tnewRecord.expand();\r",
                    "\r",
                    "\t\t\t\tthis.getNewGroupWindow().destroy(); //close create dialog\r",
                    "\r",
                    "\t\t\t\tthis.getTreegrid().getSelectionModel().doSelect(newRecord);\r",
                    "\t\t\t},\r",
                    "\t\t\tscope: this\r",
                    "\t\t});\r",
                    "\r",
                    "\t\tExt.getStore('SelectGroupStore').load();\r",
                    "\r",
                    "\t\t//set new id as the choice in edit/create rule window\r",
                    "\t\tif(this.getEditRuleGroup() !== undefined && this.getEditRuleGroup() !== null) {\r",
                    "\t\t\tthis.getEditRuleGroup().setValue(newId);\r",
                    "\t\t}\r",
                    "\t},\r",
                    "\tfailure: function(response, object){\r",
                    "\t\tvar text = response.responseText;\r",
                    "\t\tExt.Error.raise({\r",
                    "\t\t\tmsg: 'Failed to get report parameters',\r",
                    "\t\t\toption: text,   // whatever was passed into the method\r",
                    "\t\t\t'object': object // other arbitrary info\r",
                    "\t\t});\r",
                    "\t},\r",
                    "\tscope: this\r",
                    "});"
                ]
            },
            "designerId": "07a23c26-3c72-467c-aac1-d1de6f6d4b44"
        },
        {
            "type": "basicfunction",
            "reference": {
                "name": "items",
                "type": "array"
            },
            "codeClass": null,
            "userConfig": {
                "designer|params": [
                    "name"
                ],
                "fn": "isGroupNew",
                "implHandler": [
                    "var exists = false;\r",
                    "\r",
                    "var groups = this.getTreegrid().getRootNode().childNodes;\r",
                    "for(var key in groups) {\r",
                    "\tif(groups[key].get('name').toLowerCase() == name.toLowerCase())\r",
                    "\t\texists = true;\r",
                    "}\r",
                    "\r",
                    "return !exists;"
                ]
            },
            "designerId": "353f3840-b850-4b5d-9032-03e4d506b300"
        }
    ]
}