/* Copyright (c) 2020, 2023, The Unified Company. This code is part of Unify. This program is free software; you can redistribute it and/or modify it under the terms of the ESA Software Community License - Strong Copyleft LICENSE, as published by the ESA. See the ESA Software Community License - Strong Copyleft LICENSE, for more details. https://unifyjs.org */ import tools from '../../unify/tools.js'; import querySQL from '../../unify/querySQL.js'; import Console from '../console.js'; import database from '../database.js'; import deepclone from '../../unify/clonedeep.js'; export default class columnController{ prepareQuery() { } deleteByParentID( tableName, parent_id, storageCollection ) { var query = new querySQL(); query.type = "delete"; query.table = tableName; query.find( storageCollection.getLeft() + "_id", parent_id ); database.query( query ); } updateRow( row, clientValue ) { if( clientValue == "" ) { clientValue = false; } var validated = true; if( object.validate ) { validated = object.validate( row, client, eventName ); } if( clientValue == true && validated ) { var row_id = row.id; var query = new querySQL(); query.type = "insert"; query.table = tableName; query.setColumn( storageCollection.getLeft() + "_id", parent_id ); query.setColumn( storageCollection.getRight() + "_id", row_id ); database.query( query ); } } updateRenderCollection( object ) { var clientObject = deepclone( object ); var storageCollection = object.storageCollection; var leftTableName = storageCollection.parentName; var rightTableName = storageCollection.parent.propertyName; var tableName = leftTableName + "_" + rightTableName; var parent_id = object.parent.id; this.deleteByParentID( tableName, parent_id, storageCollection ); object.get( client ); for( var c = 0; c < object.rows.length; c++ ) { var row = object.rows[c]; var clientValue = clientObject.rows[c].value; this.updateRow( row, clientValue ); } } findParentTable( object ) { if( object.type == "table" ) { return object; } else { return this.findParentTable( object.parent ); } } updateColumn( object ) { var table = this.findParentTable( object ); var query = new querySQL(); query.type = "update"; //console.log( table ); query.addObject( table ); database.query( query ); //this.objectManager.updateClient( object, client, eventName ); } public update( object, client, eventName ) { if( !object.isAllowed( client.user, "WRITE" ) ) { object.deserialize(); return false; } if( object.type == "renderCollection" ) { // For storageCollection selects and check buttons this.updateRenderCollection( object ); } else { this.updateColumn( object ); } return object; } }