$.fbuilder.controls[ 'datasource' ] = function(){ this.list = { 'database' : { title : 'Database', databaseData: { host: '', user: '', pass: '', database: '' }, queryData : { active: 'structure', query: '', value: '', text: '', table: '', where: '', orderby: '', limit: '' }, show : function( type ) // Type can be 'pair' or 'single', for 'pair' are shown options for text and value, for 'single' is shown only the option for value { var str = '
Database Connection
'; str += '
'; str += '
'; str += '
'; str += '
'; str += '
'; str += '
'; str += '
'; str += '
'; str += '
'; if( type == 'pair' ) { str += '
'; } str += '
'; str += '
'; str += '
'; str += '
'; str += '
'; str += '
'; str += '
'; str += '
'; str += '
'; str += '
'; return str; }, events : function() { $( '#sHost' ).bind( 'keyup change', { obj: this }, function( e ) { e.data.obj.databaseData.host = $.trim( $(this).val() ); $.fbuilder.reloadItems(); }); $( '#sUser' ).bind( 'keyup change', { obj: this }, function( e ) { e.data.obj.databaseData.user = $.trim( $(this).val() ); $.fbuilder.reloadItems(); }); $( '#sPass' ).bind( 'keyup change', { obj: this }, function( e ) { e.data.obj.databaseData.pass = $.trim( $(this).val() ); $.fbuilder.reloadItems(); }); $( '#sDatabase' ).bind( 'keyup change', { obj: this }, function( e ) { e.data.obj.databaseData.database = $.trim( $(this).val() ); $.fbuilder.reloadItems(); }); $( '[name="sQueryType"]' ).bind( "click", { obj: this }, function( e ) { $( '.queryType' ).hide(); $( '#databaseQueryData_'+e.target.value ).show(); e.data.obj.queryData.active = $.trim( $(this).val() ); }); $( '#sQueryValue' ).bind( 'keyup change', { obj: this }, function( e ) { e.data.obj.queryData.value = $.trim( $(this).val() ); $.fbuilder.reloadItems(); }); $( '#sQueryText' ).bind( 'keyup change', { obj: this }, function( e ) { e.data.obj.queryData.text = $.trim( $(this).val() ); $.fbuilder.reloadItems(); }); $( '#sQueryTable' ).bind( 'keyup change', { obj: this }, function( e ) { e.data.obj.queryData.table = $.trim( $(this).val() ); $.fbuilder.reloadItems(); }); $( '#sQueryWhere' ).bind( 'keyup change', { obj: this }, function( e ) { e.data.obj.queryData.where = $.trim( $(this).val() ); $.fbuilder.reloadItems(); }); $( '#sQueryOrderBy' ).bind( 'keyup change', { obj: this }, function( e ) { e.data.obj.queryData.orderby = $.trim( $(this).val() ); $.fbuilder.reloadItems(); }); $( '#sQueryLimit' ).bind( 'keyup change', { obj: this }, function( e ) { e.data.obj.queryData.limit = $.trim( $(this).val() ); $.fbuilder.reloadItems(); }); $( '#sCustomQuery' ).bind( 'keyup change', { obj: this }, function( e ) { e.data.obj.queryData.query = $.trim( $(this).val() ); $.fbuilder.reloadItems(); }); $( '#sTestConnection' ).bind( 'click', { obj: this }, function( e ) { var form_url = $( this ).parents( 'form' ).attr( 'action' ); $.ajax( { url : form_url, cache : false, data : $.extend( { cffaction: 'test_db_connection' }, e.data.obj.databaseData ), success : function( data ){ alert( data ); } } ); }); $( '#sTestQuery' ).bind( 'click', { obj: this }, function( e ) { var form_url = $( this ).parents( 'form' ).attr( 'action' ); $.ajax( { url : form_url, cache : false, data : $.extend( { cffaction: 'test_db_query' }, e.data.obj.databaseData, e.data.obj.queryData ), success : function( data ){ alert( data ); } } ); }); } }, 'csv' : { title : 'CSV', csvData : { text : 0, value : 0, file : '', fields : [], headline : false, delimiter : 'tabulator', character : ',', rows : [] }, show : function() { var str = '
CSV Import
', optionsTexts = '', optionsValues = ''; for( var index in this.csvData.fields ) { optionsTexts += ''; optionsValues += ''; } str += '
'; str += '
'; str += '
  
'; str += '
'; str += '
'; str += '
'; str += '
'; return str; }, events : function() { $( '#sCSVUseHeadline' ).bind( 'click', { obj: this}, function( e ) { e.data.obj.csvData.headline = $( this ).is( ':checked' ); $.fbuilder.reloadItems(); }); $( '#sCSVTexts' ).bind( 'change', { obj: this}, function( e ) { e.data.obj.csvData.text = $( this ).val(); $.fbuilder.reloadItems(); }); $( '#sCSVValues' ).bind( 'change', { obj: this}, function( e ) { e.data.obj.csvData.value = $( this ).val(); $.fbuilder.reloadItems(); }); $( '#sCSVImport' ).bind( 'click', { obj: this}, function( e ) { e.data.obj.csvData.fields = []; e.data.obj.csvData.rows = []; e.data.obj.csvData.text = 0; e.data.obj.csvData.value = 0; e.data.obj.csvData.file = ''; var obj = { config : { header : e.data.obj.csvData.headline, dynamicTyping : false, delimiter : ( ( e.data.obj.csvData.delimiter != 'tabulator' ) ? e.data.obj.csvData.character : '' ), preview : 0 }, complete : function( results, file, inputElem, event ) { function setFields( c ) { for ( var i = 0; i < c; i++ ) { e.data.obj.csvData.fields.push( 'Field_' + i ); } }; if( results.errors.length == 0 ) { if( typeof results.results.fields != 'undefined' ) { e.data.obj.csvData.fields = results.results.fields; e.data.obj.csvData.text = e.data.obj.csvData.value = results.results.fields[ 0 ]; } else if( typeof results.results.rows != 'undefined' ) { if( results.results.rows.length ) { setFields( results.results.rows[ 0 ].length ); } } else if( typeof results.results != 'undefined' ) { setFields( results.results[ 0 ].length ); } e.data.obj.csvData.text = e.data.obj.csvData.value = 0; e.data.obj.csvData.rows = ( typeof results.results.rows != 'undefined' ) ? results.results.rows : results.results; e.data.obj.csvData.file = $( '#sCSVLocation' ).val(); $.fbuilder.reloadItems(); $( '#datasourceSettings' ).html( e.data.obj.show() ); e.data.obj.events(); } else { alert( 'Error, checks the CSV file structure' ); } } }; $( '#sCSVLocation' ).parse( obj ); }); } }, 'posttype' : { title : 'Post Type', posttypeData:{ posttype : '', value : 'ID', text : 'post_title', last : '', id : '' }, loadPostTypes : function() { var me = this, e = $( '#sPostType' ), form_url = e.parents( 'form' ).attr( 'action' ); $.ajax( { dataType : 'json', url : form_url, cache : false, data : { cffaction: 'get_post_types' }, success : function( data ){ var opt = '', v, selected = ( me.posttypeData.posttype != '' ) ? me.posttypeData.posttype : Object.keys( data )[ 0 ]; for( var index in data ) { opt += ''; } e.html( opt ).change(); } } ); }, show : function( type ) // Type can be 'pair' or 'single', for 'pair' are shown options for text and value, for 'single' is shown only the option for value { var str = '
Select Post Type
', columns = [ 'ID', 'post_title', 'post_excerpt', 'post_content' ], optionsValues = '', optionsTexts = ''; for( var i in columns ) { optionsValues += ''; optionsTexts += ''; } str += '
'; str += '
'; if( type == 'pair' ) { this.posttypeData.id = ''; str += '
'; str += '
'; } else { str += '
'; } str += '
'; return str; }, events : function() { $( '#sPostType' ).bind( 'change', { obj: this }, function( e ) { e.data.obj.posttypeData.posttype = $( this ).val(); $.fbuilder.reloadItems(); }); $( '#sPostTypeText' ).bind( 'change', { obj: this }, function( e ) { e.data.obj.posttypeData.text = $( this ).val(); $.fbuilder.reloadItems(); }); $( '#sPostTypeValue' ).bind( 'change', { obj: this }, function( e ) { e.data.obj.posttypeData.value = $( this ).val(); $.fbuilder.reloadItems(); }); $( '#sPostTypeLast' ).bind( 'keyup change', { obj: this }, function( e ) { e.data.obj.posttypeData.last = $.trim( $( this ).val() ); $.fbuilder.reloadItems(); }); $( '#sPostId' ).bind( 'keyup change', { obj: this }, function( e ) { e.data.obj.posttypeData.id = $.trim( $( this ).val() ); $.fbuilder.reloadItems(); }); this.loadPostTypes(); } }, 'taxonomy' : { title : 'Taxonomy', taxonomyData:{ taxonomy : '', value : 'term_id', text : 'name', id : '', slug : '' }, loadTaxonomies : function() { var me = this, e = $( '#sTaxonomy' ), form_url = e.parents( 'form' ).attr( 'action' ); $.ajax( { dataType : 'json', url : form_url, cache : false, data : { cffaction: 'get_available_taxonomies' }, success : function( data ){ var opt = '', v, selected = ( me.taxonomyData.taxonomy != '' ) ? me.taxonomyData.taxonomy : Object.keys( data )[ 0 ]; for( var index in data ) { opt += ''; } e.html( opt ).change(); } } ); }, show : function( type ) // Type can be 'pair' or 'single', for 'pair' are shown options for text and value, for 'single' is shown only the option for value { var str = '
Select Taxonomy
', columns = [ 'term_id', 'name', 'slug' ], optionsValues = '', optionsTexts = ''; str += '
'; for( var i in columns ) { optionsValues += ''; optionsTexts += ''; } str += '
'; if( type == 'pair' ) { this.taxonomyData.id = ''; this.taxonomyData.slug = ''; str += '
'; } else { str += '
'; str += '
'; } str += '
'; return str; }, events : function() { $( '#sTaxonomy' ).bind( 'change', { obj: this }, function( e ) { e.data.obj.taxonomyData.taxonomy = $( this ).val(); $.fbuilder.reloadItems(); }); $( '#sTaxonomyText' ).bind( 'change', { obj: this }, function( e ) { e.data.obj.taxonomyData.text = $( this ).val(); $.fbuilder.reloadItems(); }); $( '#sTaxonomyValue' ).bind( 'change', { obj: this }, function( e ) { e.data.obj.taxonomyData.value = $( this ).val(); $.fbuilder.reloadItems(); }); $( '#sTermId' ).bind( 'keyup change', { obj: this }, function( e ) { e.data.obj.taxonomyData.id = $.trim( $( this ).val() ); $.fbuilder.reloadItems(); }); $( '#sTermSlug' ).bind( 'keyup change', { obj: this }, function( e ) { e.data.obj.taxonomyData.slug = $.trim( $( this ).val() ); $.fbuilder.reloadItems(); }); this.loadTaxonomies(); } }, 'user' : { title : 'User Data', userData : { logged : false, text : 'user_nicename', value : 'ID', id : '', login : '' }, show : function( type ) // Type can be 'pair' or 'single', for 'pair' are shown options for text and value, for 'single' is shown only the option for value { var str = '
Display for Users
', columns = [ 'ID', 'user_login', 'user_nicename', 'display_name', 'user_email' ], optionsValues = '', optionsTexts = ''; for( var i in columns ) { optionsValues += ''; optionsTexts += ''; } str += '
'; if( type == 'pair' ) { this.userData.logged = false; this.userData.id = ''; this.userData.login = ''; str += '
'; } else { this.userData.text = ''; str += '
'; str += '
'; str += '
'; } str += '
'; return str; }, events : function() { $( '#sUserValue' ).bind( 'change', { obj : this }, function( e ) { e.data.obj.userData.value = $(this).val(); $.fbuilder.reloadItems(); }); $( '#sUserText' ).bind( 'change', { obj : this }, function( e ) { e.data.obj.userData.text = $(this).val(); $.fbuilder.reloadItems(); }); $( '#sUserLogged' ).bind( 'click', { obj : this }, function( e ) { var isChecked = $(this).is( ':checked' ); e.data.obj.userData.logged = isChecked; $.fbuilder.reloadItems(); $( '#sUserId' ).attr( 'disabled', isChecked ); $( '#sUserLogin' ).attr( 'disabled', isChecked ); }); $( '#sUserId' ).bind( 'keyup change', { obj : this }, function( e ) { e.data.obj.userData.id = $.trim( $(this).val() ); $.fbuilder.reloadItems(); }); $( '#sUserLogin' ).bind( 'keyup change', { obj : this }, function( e ) { e.data.obj.userData.login = $.trim( $(this).val() ); $.fbuilder.reloadItems(); }); } } }; }; $.fbuilder.controls[ 'datasource' ].prototype = { isDataSource:true, active : '', editItemEventsDS : function() { for( var index in this.list ) { this.list[ index ].events(); } $( '#sDataSource' ).bind( 'change', { obj: this }, function( e ) { e.data.obj.active = $(this).val(); $.fbuilder.editItem( e.data.obj.index ); $.fbuilder.reloadItems(); }); }, showDataSource : function( list, type ) { if( this.active == '' ) { this.active = list[ 0 ]; } var str = '
' + this.list[ this.active ].show( type ) + '
'; return str; } };