Page.Organization = Page.Organization || {}; Page.Organization.Edit = class extends _page { /** * Get class name. * @return {string} */ static getClassName() { return 'Page.Organization.Edit'; } /** * Get URI. * @return {string} */ static getURI() { return `/api/${Page.Meta.api_version}/page/organization/edit/index.php`; } /** * Initialize page. Called after open(). */ static init() { if (Page.getParameters().id) { this.openOrganization(Page.getParameters().id); } } /** * Open organization for view. * @param {int} id */ static openOrganization(id) { let me = this; Server.call( Page.Organization.Edit.getURI(), { func: 'apiGetOrganizationForm', id: id, error_return_code: 2 }, function (data) { if (data.return == 2) { Page.setParameter('id'); me.refresh(); } Page.setParameter('id', id); Page.getPageSegment().html(data.html); me.bindPage(); }, null, 'Loading organization...' ); } /** * Bind organization page actions. */ static bindPage() { Core.UI.Bind.get( Page.getPageSegment(), { buttons: { 'delete-organization': (data) => { new Core.Organization(data.id).delete( () => { Page.setParameter('id'); this.refresh(); } ); }, 'toggle-module': (data) => { new Modal.Module.Toggle({ id: data.id, organizationId: data.organization_id, onClose: () => Core.UI.TabMenu.get('organization', this).refresh() }); }, 'add-user': (data) => { this.openAddUserModal( data.organization_id, () => Core.UI.TabMenu.get('organization', this).refresh() ); }, 'apply-payment': (data) => { new Modal.Invoice.ApplyPayment({ id: data.id, onApply: () => Core.UI.TabMenu.get('organization', this).refresh() }); }, 'print-invoice': (data) => this.printInvoice(data.id), 'email-invoice': (data) => { this.emailInvoice( data.email, data.id ); }, 'issue-invoice': (data) => { }, 'edit-user': (data) => { new Modal.User.Edit({ id: data.id, onClose: () => Core.UI.TabMenu.get('organization', this).refresh() }); }, 'delete-user': (data) => { new Core.User(data.id) .delete( () => Core.UI.Table.get('users', this).deleteRow(data.id) ); } } } ); } /** * Bind tab. * @param {string} tabName * @param {jQuery} $tab */ static bindTab(tabName, $tab) { switch (tabName) { case 'modules': Core.UI.Table.get('modules', this); break; case 'users': let table = Core.UI.Table.get('users', this); break; case 'billing': Core.UI.Table.get('invoices', this); break; } } /** * Email invoice to user. * * @param {string} emailAddress * @param {string} id */ static emailInvoice(emailAddress, id) { Server.call( this.getURI(), { func: 'apiEmailInvoice', id: id, email: emailAddress }, function (data) { }, null, 'Sending email...' ); } /** * Open invoice in a new window for print. * @param {number} id */ static printInvoice(id) { Server.call( this.getURI(), { func: 'apiGetInvoiceForPrint', id: id }, function (data) { let w = window.open(); w.document.body.innerHTML = data.html; }, null, 'Loading...' ); } /** * Open add user modal. * @param {integer} organizationId * @param {function()} callback */ static openAddUserModal(organizationId, callback) { Server.call( this.getURI(), { func: 'apiGetAddUserModal', organization_id: organizationId }, function (data) { let m = new _modal(); let onCreate = () => { Core.User.create( m.form(null, 'get values'), () => { m.close(); if ($.isFunction(callback)) { callback(); } } ); return false; }; m.create( data.modal_id, data.modal, onCreate ); }, null, 'Loading add user modal...' ); } /** * Open modal to select organization. */ static openSelectOrganizationModal() { let me = this; Server.call( Page.Organization.Edit.getURI(), { func: 'apiGetSelectOrganizationModal' }, function (data) { let m = new _modal(); m.create(data.modal_id, data.modal); m.$modal.find('.ui.search').search({ apiSettings: { url: me.getURI() + '?func=apiGetOrganizations&criteria={query}' }, minCharacters: 2, onSelect: function (result) { m.close(); me.openOrganization(result.id); }, cache: false }); }, null, 'Loading select organization modal...' ); } /** * Open create organization modal. */ static openCreateOrganizationModal() { let me = this; Server.call( Page.Organization.Edit.getURI(), { func: 'apiGetCreateOrganizationModal' }, function (data) { let m = new _modal(); m.create( data.modal_id, data.modal, function () { m.close(); me.createOrganization(m.form(null, 'get values')); } ); }, null, 'Loading create organization modal...' ); } /** * Create organization. * @param {object} form */ static createOrganization(form) { let me = this; Server.call( Page.Organization.Edit.getURI(), { func: 'apiCreateOrganization', form: form }, function (data) { me.openOrganization(data.id); }, null, 'Creating organization...' ); } };