$.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 += '
Username:
';
str += '
Password:
';
str += '
Database:
';
str += '';
str += '';
str += '
Query Structure Custom Query
';
str += '
';
str += '
Column for values:
';
if( type == 'pair' )
{
str += '
Column for texts:
';
}
str += '
Table name:
';
str += '
Condition:
';
str += '
Order by:
';
str += '
Limit:
';
str += '
';
str += '
';
str += '
Type the query:
';
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 += '
Select CSV file:
';
str += '
Use headline:
';
str += '
Delimiter: Tabulator Character
';
str += '';
str += '
Select column for texts:
';
str += '
Select column for values:
';
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 = '
';
}
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 = '