syntax_highlighting
if ( empty( $settings ) || empty( $settings['codemirror'] ) ) {
return false;
}
wp_enqueue_script( 'code-editor' );
wp_enqueue_style( 'code-editor' );
wp_enqueue_script( 'csslint' );
wp_enqueue_script( 'htmlhint' );
wp_enqueue_script( 'jshint' );
wp_add_inline_script( 'code-editor', sprintf( 'jQuery.extend( wp.codeEditor.defaultSettings, %s );', wp_json_encode( $settings ) ) );
}
}
/**
* L'ajax per la richiesta dell'elenco dei valori unici per mostrarli nei filtri di ricerca
* $_REQUEST = array(5) {
* ["sql"]=> string(26) "SELECT * FROM `wpx_posts`"["rif"]=> string(21) "wpx_posts_post_author", ["column"]=> string(25) "`wpx_posts`.`post_author`", ["action"]=> string(19) "af_distinct_values", ["table"]=> string(9) "wpx_posts"
* }
* [{c=>il testo del campo distinct, p=>l'id se serve di filtrare per id oppure -1 n il numero di volte che compare},{}] | false if is not a select query
*
*/
public function af_distinct_values() {
global $wpdb;
ADFO_fn::require_init();
if (!isset($_REQUEST['column'])) {
wp_send_json(['error' => 'no_column_selected']);
die();
}
$table = (isset($_REQUEST['table'])) ? sanitize_text_field($_REQUEST['table']) : '';
$model = new ADFO_model( $table ); // è la tabella a cui appartiene il singolo campo!
if (isset($_REQUEST['sql'])) {
$req_sql =html_entity_decode( wp_kses_post( wp_unslash($_REQUEST['sql'])));
$model->prepare($req_sql);
if (!isset($_REQUEST['dbp_id'])) {
$model->removes_column_from_where_sql(sanitize_text_field($_REQUEST['column']));
}
}
if (isset($_REQUEST['filter_distinct'])) {
$result = $model->distinct(sanitize_text_field($_REQUEST['column']), sanitize_text_field($_REQUEST['filter_distinct']));
} else {
$result = $model->distinct(sanitize_text_field($_REQUEST['column']));
}
$error = "";
$count = 0;
if ($model->last_error != "" || !is_countable($result)) {
$error = __('Option not available for this query '.$model->last_error." ".$model->get_current_query(),'admin_form');
$result = [];
} else if ( count($result) >= 1000) {
//$error = __('The column has too many values to show.
You can use the field above to filter the results','admin_form');
$count = count ($result);
if (count($result) >= 5000) {
$count = "5000+";
}
$result = [];
} else {
$count = count ($result);
}
wp_send_json(['error' => $error, 'result' => $result, 'rif' => sanitize_text_field($_REQUEST['rif']), 'count'=>$count, 'filter_distinct'=>sanitize_text_field(@$_REQUEST['filter_distinct'] )]);
die();
}
/**
* L'ajax per la richiesta dell'elenco dei valori unici per mostrarli nei filtri di ricerca
* $_REQUEST = array(5) {
* ["sql"]=> string(26) "SELECT * FROM `wpx_posts`"["rif"]=> string(21) "wpx_posts_post_author", ["column"]=> string(25) "`wpx_posts`.`post_author`", ["action"]=> string(19) "af_distinct_values", ["table"]=> string(9) "wpx_posts"
* }
* [{c=>il testo del campo distinct, p=>l'id se serve di filtrare per id oppure -1 n il numero di volte che compare},{}] | false if is not a select query
*/
public function af_autocomplete_values() {
global $wpdb;
ADFO_fn::require_init();
if (!isset($_REQUEST['params'])) {
wp_send_json(['error' => 'no_params_selected']);
die();
}
$params = ADFO_fn::sanitize_text_recursive($_REQUEST['params']);
$result = [];
$error = "";
// TODO REQUEST TABLE E COLUMN devono trasformarsi in un generico attributes... perché esistono più tipi di distinct values (e in alcuni casi potrebbero non essere distinct (?!))
if (isset($params['type']) && $params['type'] == "post") {
$count = 2000;
$array_params = array(
'post_limits' => 100,
'orderby' => 'post_title',
'order' => 'ASC',
'post_type' => $params['post_types'],
'post_status' => 'publish'
);
if (isset($params['cats']) && is_countable(($params['cats']))) {
$array_params['category__in'] = $params['cats'];
}
if (isset($_REQUEST['filter_distinct'])) {
$array_params['search'] = sanitize_text_field($_REQUEST['filter_distinct'])."*";
}
$posts = query_posts($array_params);
foreach($posts as $rl) {
$result[] = ['c' => wp_trim_words(strip_tags($rl->post_title), 10), 'p'=>$rl->ID];
}
if (count($result) < 100) {
$count = count($result);
}
} else if (isset($params['type']) && $params['type'] == "user") {
$count = 2000;
$array_params = array('number'=>100);
if (isset($params['roles']) && is_countable(($params['roles']))) {
$array_params['role__in'] = $params['roles'];
}
if (isset($_REQUEST['filter_distinct'])) {
$array_params['search'] = sanitize_text_field($_REQUEST['filter_distinct'])."*";
}
$users = get_users( $array_params );
foreach($users as $rl) {
$result[] = ['c' => wp_trim_words(strip_tags($rl->user_login), 10), 'p'=>$rl->ID];
}
if (count($result) < 100) {
$count = count($result);
}
} else if (isset($params['type']) && $params['type'] == "lookup") {
/*
* NON VA BENE! devo passare id della lista e field del lookup
params[lookup_id]: dbt_id
params[lookup_sel_val]: field_key
*/
$count = -1;
$post = ADFO_functions_list::get_post_dbp(absint($params['lookup_id']));
$lookup_table = $lookup_sel_val = $lookup_sel_txt = $lookup_where = '';
foreach ($post->post_content['form'] as $form) {
if ($form['name'] == $params['lookup_sel_val']) {
$lookup_table = $form['lookup_id'];
$lookup_sel_val = $form['lookup_sel_val'];
$lookup_sel_txt = $form['lookup_sel_txt'];
$lookup_where = trim($form['lookup_where']);
}
}
if ($lookup_table != "") {
$table_model = new ADFO_model($lookup_table); // è la tabella a cui appartiene il singolo campo!
//TODO questo lo prendo da dbp_id se è presente altrimenti dalla query
if ($lookup_where != "") {
$sql = $table_model->get_current_query()." WHERE ".$lookup_where;
$table_model->prepare($sql);
}
$table_model->list_change_select('`'.esc_sql($lookup_sel_val).'` AS val, `'.esc_sql($lookup_sel_txt).'` AS txt');
if (isset($_REQUEST['filter_distinct']) && $_REQUEST['filter_distinct'] != "") {
$table_model->list_add_where([['op'=>'LIKE%','column'=>$lookup_sel_txt, 'value'=>sanitize_text_field($_REQUEST['filter_distinct'])],
['op'=>'NOT NULL','column'=>$lookup_sel_txt, 'value'=>'']]);
} else {
$table_model->list_add_where([['op'=>'NOT NULL','column'=>$lookup_sel_txt, 'value'=>'']]);
}
$table_model->list_add_limit(0, 100);
$table_model->list_add_order('`'.$lookup_sel_txt.'`', 'ASC');
//$error = "sql: | ".$table_model->get_current_query();
$items = $table_model->get_list();
$count = $table_model->get_count();
if ($table_model->last_error != "" || !is_countable($result)) {
$error = __('Query Error '.$table_model->last_error, 'admin_form');
} else {
array_shift($items);
foreach($items as $rl) {
$result[] = ['c' => wp_trim_words(strip_tags($rl->txt), 10), 'p'=>$rl->val];
}
$count = $table_model->get_count();
}
}
} else {
$model = new ADFO_model($params['table']); // è la tabella a cui appartiene il singolo campo!
//TODO questo lo prendo da dbp_id se è presente altrimenti dalla query
if (isset($_REQUEST['sql'])) {
$model->prepare(html_entity_decode(wp_kses_post( wp_unslash($_REQUEST['sql']))));
}
$result = $model->distinct($params['column'], sanitize_text_field($_REQUEST['filter_distinct']));
$error = "";
$count = 0;
if ($model->last_error != "" || !is_countable($result)) {
$error = __('Option not available for this query','admin_form');
$result = [];
} else if ( count($result) >= 1000) {
//$error = __('The column has too many values to show.
You can use the field above to filter the results','admin_form');
$count = count ($result);
if (count($result) >= 5000) {
$count = "5000+";
}
$result = [];
} else {
$count = count ($result);
}
}
wp_send_json([
'error' => $error,
'result' => $result,
'rif' => sanitize_text_field($_REQUEST['rif']),
'count'=>$count,
'filter_distinct' => sanitize_text_field(@$_REQUEST['filter_distinct']) ]);
die();
}
/**
* Aggiungo le voci di menu delle liste
* postmeta _dbp_admin_show
* ```json
* {"page_title":"dbp_174","menu_title":"connection","capability":"manage_options","slug":"dbp_174"}
* ```
*/
public function init_add_menu_page() {
global $wpdb;
$pages = $wpdb->get_results("SELECT * FROM ".$wpdb->postmeta ." WHERE meta_key = '_dbp_admin_show'");
if (is_countable($pages)) {
require_once(ADFO_DIR . "admin/class-af-admin-menu.php");
$db_admin = new ADFO_admin_list_menu();
foreach ($pages as $page) {
$page_data = maybe_unserialize(@$page->meta_value);
if (is_countable($page_data) && isset($page_data['show']) && $page_data['show'] == 1 && isset($page_data['status']) && $page_data['status'] != 'trash') {
add_menu_page($page_data['page_title'], $page_data['menu_title'], $page_data['capability'], $page_data['slug'], [$db_admin, 'controller'], $page_data['menu_icon'], $page_data['menu_position']);
}
}
}
}
/**
* Aggiungo gli script frontend
*/
public function frontend_enqueue_scripts() {
$file = plugin_dir_path( __FILE__ );
$dbp_css_ver = date("ymdGi", filemtime( plugin_dir_path($file) . 'frontend/admin-form.css' ));
$dbp_js_ver = date("ymdGi", filemtime( plugin_dir_path($file) . 'frontend/admin-form.js' ));
wp_register_style( 'dbp_frontend_css', plugins_url( 'frontend/admin-form.css', $file), false, $dbp_css_ver );
wp_enqueue_style( 'dbp_frontend_css' );
wp_register_script( 'dbp_frontend_js', plugins_url( 'frontend/admin-form.js', $file), false, $dbp_js_ver, true);
// wp_add_inline_script( 'mytheme-typekit', 'try{Typekit.load({ async: true });}catch(e){}' );
wp_add_inline_script( 'dbp_frontend_js', 'dbp_post = "'.esc_url( admin_url('admin-ajax.php')).'";', 'before' );
wp_enqueue_script( 'dbp_frontend_js' );
}
/**
* Restituisce una lista chiamata in ajax (per il frontend - da verificare che sia nel singolo giorno)
*/
public function get_list() {
ADFO_fn::require_init();
$result['div'] = sanitize_text_field($_REQUEST['dbp_div_id']);
if (isset($_REQUEST['dbp_prefix'])) {
$prefix = sanitize_text_field($_REQUEST['dbp_prefix']);
} else {
$prefix = "";
}
if (isset($_REQUEST['dbp_extra_attr'])) {
$dbp_extra_attr = ADFO_fn::sanitize_text_recursive($_REQUEST['dbp_extra_attr']);
$extra_attr = json_decode(base64_decode($dbp_extra_attr), true);
if (json_last_error() == JSON_ERROR_NONE) {
if (isset($extra_attr['request'])) {
foreach ($extra_attr['request'] as $key=>$val) {
$_REQUEST[$key] = $val;
}
pinacode::set_var('request', $extra_attr['request']);
}
//if (isset($extra_attr['params'])) {
// pinacode::set_var('params', $extra_attr['params']);
//}
if (isset($extra_attr['data'])) {
pinacode::set_var('data', $extra_attr['data']);
}
$result['html'] = ADFO::get_list(absint($_REQUEST['dbp_list_id']), true, $extra_attr['params'], $prefix);
} else {
$result['html'] = 'OPS an error occurred';
}
} else {
$result['html'] = ADFO::get_list(absint($_REQUEST['dbp_list_id']), true, [], $prefix);
}
wp_send_json($result);
die();
}
/**
* Carico i dettagli di una lista in detail
*
* @return void
*/
public function get_detail() {
ADFO_fn::require_init();
$dbp_ids = sanitize_text_field($_REQUEST['dbp_ids']);
$ids = ADFO_fn::ids_url_decode($dbp_ids);
$dbp_id = absint($_REQUEST['dbp_id']);
if ($dbp_id == 0 && !(is_array($ids) || is_numeric($ids))) return;
$result = ADFO::get_detail($dbp_id, $ids);
if ($result !== false ) {
$dbp_post = ADFO_functions_list::get_post_dbp($dbp_id);
if (isset($dbp_post->post_content['frontend_view']['detail_type']) && $dbp_post->post_content['frontend_view']['detail_type'] == "PHP") {
do_action('dbp_detail_'.$dbp_id, $result);
} else if (isset($dbp_post->post_content['frontend_view']['detail_type']) && $dbp_post->post_content['frontend_view']['detail_type'] == "CUSTOM") {
$detail_template = $dbp_post->post_content['frontend_view']['detail_template'];
PinaCode::set_var('data', $result) ;
$ris = PinaCode::execute_shortcode($detail_template);
if (is_array($ris) || is_object($ris)) {
echo wp_unslash(json_encode($ris));
} else {
echo $ris;
}
} else {
// stampo un detail di default
?>
$r) {
$set = null;
if (isset($dbp_post->post_content["list_setting"][$k])) {
$set = $dbp_post->post_content["list_setting"][$k];
$label = $set->title;
} else {
$label = $k;
}
?>
|
|
'', 'items' => ''];
if (!isset($_REQUEST['ids']) || !is_countable($_REQUEST['ids'])) {
$json_send['error'] = __('I have not found any results. Verify that the primary key of each selected table is always displayed in the MySQL SELECT statement.', 'admin_form');
wp_send_json($json_send);
die();
}
$table_model = $this->get_table_model_for_sidebar();
$table_model->remove_limit();
$table_items = $table_model->get_list();
//var_dump ($table_items);
if (is_countable($table_items) && count($table_items) == 2) {
$item = array_pop($table_items);
foreach ($item as &$val) {
$val = ADFO_fn::format_single_detail_value($val);
}
$json_send['items'] = [$item];
} else if (is_countable($table_items) && count($table_items) > 2 && count($table_items) < 200) {
// Sono più risultati quindi raggruppo i risultati per tabella e mostro solo i gruppi differenti.
$items = ADFO_fn::convert_table_items_to_group($table_items);
if (count($items) > 1) {
$json_send['error'] = __('The query responded with multiple lines. Verify that the primary key of each selected table is always displayed in the MySQL SELECT statement', 'admin_form');
}
$json_send['items'] = $items;
} else if (is_countable($table_items) && count($table_items) > 2 && count($table_items) >= 200) {
$json_send['error'] = __('I am sorry but I cannot show the requested details because I have found more than 200 results!. Verify that the primary key of each selected table is always displayed in the MySQL SELECT statement. Check that the tables have a unique auto increment primary key.', 'admin_form');
}else {
$json_send['error'] = __('Strange, I have not found any results. Verify that the primary key of each selected table is always displayed in the MySQL SELECT statement. Check that the tables have a unique auto increment primary key.', 'admin_form');
}
wp_send_json($json_send);
die();
}
/**
* Genera i parametri per la creazione della form (add o edit) nella sidebar
*/
public function af_edit_details_v2() {
ADFO_fn::require_init();
$json_send = ['error' => '', 'items' => ''];
if (isset($_REQUEST['div_id'])) {
$json_send['div_id'] = sanitize_text_field($_REQUEST['div_id']);
}
if (!isset($_REQUEST['dbp_id']) && !isset($_REQUEST['sql'])) {
$json_send['error'] = __('There was an unexpected problem, please try reloading the page.', 'admin_form');
wp_send_json($json_send);
die();
}
// aggiungo eventuali dbp_extra_attr (deve essere una funzione!)
if (isset($_REQUEST['dbp_extra_attr'])) {
$dbp_extra_attr = ADFO_fn::sanitize_text_recursive($_REQUEST['dbp_extra_attr']);
$extra_attr = json_decode(base64_decode($dbp_extra_attr), true);
if (json_last_error() == JSON_ERROR_NONE) {
if (isset($extra_attr['request'])) {
foreach ($extra_attr['request'] as $key=>$val) {
if (!isset($_REQUEST[$key])) {
$_REQUEST[$key] = $val;
}
}
}
if (isset($extra_attr['params'])) {
$params = (array)PinaCode::get_var('params');
$params = array_merge($extra_attr['params'], $params);
PinaCode::set_var('params', $params);
}
if (isset($extra_attr['data'])) {
pinacode::set_var('data', $extra_attr['data']);
}
}
}
$json_send['edit_ids'] = ADFO_fn::get_request('ids', 0);
$form = new ADFO_class_form($_REQUEST['dbp_id']);
$json_send['dbp_id'] = sanitize_text_field($_REQUEST['dbp_id']);
if (isset($_REQUEST['ids']) && is_countable($_REQUEST['ids'])) {
$ids = ADFO_fn::sanitize_absint_recursive($_REQUEST['ids']);
$items = $form->get_data($ids);
} else {
$items = [];
}
//var_dump ($_REQUEST['ids']);
list($settings, $table_options) = $form->get_form();
$json_send['items'] = $form->convert_items_to_groups($items, $settings, $table_options);
$json_send['params'] = $form->data_structures_to_array($settings);
$json_send['table_options'] = $form->data_structures_to_array($table_options);
//var_dump ($json_send['params']);
// DEFAULT RICALCOLATI
if (isset($_REQUEST['dbp_id'])) {
foreach ($json_send['params'] as $group_key => &$group_param) {
foreach ($group_param as $field_key => &$field_param) {
if (isset($field_param['default_value']) && $field_param['default_value'] != "") {
$field_param['default_value'] = PinaCode::execute_shortcode($field_param['default_value']);
}
// TODO: se sono lookup, post user o media gallery con default_value
// devo trovare gli altri dati per compilare il campo.
if ($field_param['form_type'] == 'LOOKUP') {
if (isset($field_param['default_value']) && isset($field_param['default_value']) && $field_param['default_value'] != "") {
foreach ($json_send['items'] as $key=>&$item) {
$is_new = (!isset($json_send['table_options'][$key][$group_key]['pri_value']) || absint($json_send['table_options'][$key][$group_key]['pri_value']) == 0);
if ($item[$group_key]->$field_key == "" && $is_new) {
$field_param['custom_value'] = $field_param['default_value'];
$item[$group_key]->$field_key = $field_param['default_value'];
$table_model2 = new ADFO_model($field_param['lookup_id']);
$table_model2->list_change_select('`'.esc_sql($field_param['lookup_sel_val']).'` AS val, `'.esc_sql($field_param['lookup_sel_txt']).'` AS txt');
$table_model2->list_add_where([['op'=>'=','column'=>$field_param['lookup_sel_val'], 'value'=>$field_param['default_value']]]);
$table_model2->list_add_limit(0,1);
$res = $table_model2->get_list();
if (is_array($res) && count($res) == 2) {
$row = array_pop($res);
$field_param['custom_value'] = $row->txt;
$item[$group_key]->$field_key = $row->val;
}
}
}
}
// risetto i parametri da passare al javascript che disegna la form passandogli l'id della lista e il nome del campo così quando invia l'ajax ricalcolo tabella field e where sono nel backend .
$field_param['lookup_id'] = absint($_REQUEST['dbp_id']);
$field_param['lookup_sel_val'] = $field_param['name'];
unset($field_param['lookup_sel_txt']);
unset($field_param['lookup_where']);
}
if ($field_param['form_type'] == 'USER' && isset($field_param['default_value']) && $field_param['default_value'] > 0) {
$user = get_user_by('id',absint($field_param['default_value']));
if (is_object($user) && isset($user->user_login)) {
foreach ($json_send['items'] as $key=>&$item) {
$is_new = (!isset($json_send['table_options'][$key][$group_key]['pri_value']) || absint($json_send['table_options'][$key][$group_key]['pri_value']) == 0);
if ($item[$group_key]->$field_key == "" && $is_new) {
$field_param['custom_value'] = $user->user_login;
$item[$group_key]->$field_key = absint($field_param['default_value']);
}
}
}
}
if ($field_param['form_type'] == 'POST' && isset($field_param['default_value']) && $field_param['default_value'] > 0) {
$post = get_post(absint($field_param['default_value']));
if (is_object($post) && isset($post->post_title)) {
foreach ($json_send['items'] as $key=>&$item) {
$is_new = (!isset($json_send['table_options'][$key][$group_key]['pri_value']) || absint($json_send['table_options'][$key][$group_key]['pri_value']) == 0);
if ($item[$group_key]->$field_key == "" && $is_new) {
$field_param['custom_value'] = $post->post_title;
$item[$group_key]->$field_key = absint($field_param['default_value']);
}
}
}
}
}
}
}
// FINE DEFAULT RICALCOLATI
$json_send['buttons'] = $form->get_btns_allow(); //['save'=>false,'delete'=>true];
wp_send_json($json_send);
die();
}
/**
* Salva un record
*/
public function af_save_details() {
global $wpdb;
ADFO_fn::require_init();
$json_result = ['reload'=>0,'msg'=>'','error'=>''];
if (isset($_REQUEST['div_id'])) {
$json_result['div_id'] = sanitize_text_field($_REQUEST['div_id']);
}
$queries_executed = [];
$query_to_execute = [];
$dbp_id = 0;
$form_dbp_id = false;
$request_edit_table = $_REQUEST['edit_table'];
// se è una lista ok, altrimenti solo gli amministratori possono salvare dati
if (isset($_REQUEST['dbp_global_list_id']) && absint($_REQUEST['dbp_global_list_id']) > 0) {
$dbp_id = absint($_REQUEST['dbp_global_list_id']);
$form_dbp_id = new ADFO_class_form($dbp_id);
} else {
if( !current_user_can('administrator') ) {
$json_result['result'] = 'nook';
$json_result['error'] = __('You do not have permission to access this content!', 'admin_form');
wp_send_json($json_result);
die();
}
}
foreach ($request_edit_table as $form_value) {
$alias_table = "";
foreach ($form_value as $table=>$rows) {
//print $table;
$primary_key = ADFO_fn::get_primary_key(sanitize_text_field($table));
$primary_field = $fields_names = [];
foreach ($rows as $key=>$row) {
if ($key == $primary_key) {
$primary_field = $row;
} else {
$fields_names[$key] = $row;
}
}
// ciclo per più query.
$exists = 0;
$primary_value = "";
// ciclo quante volte si ripete la chiave primaria per la tabella (ogni volta è una nuova riga)
foreach ($primary_field as $key => $pri) {
$sql = [];
$exists = 0;
$primary_value = $pri;
// l'alias della tabella sta in un campo nascosto e serve per definire i pinacode
if (isset($fields_names["_dbp_alias_table_"][$key])) {
$alias_table = $fields_names["_dbp_alias_table_"][$key];
}
if ($alias_table == "") {
$alias_table = $table;
}
//print "ALIAS TABLE:" . $alias_table;
$pri_name = ADFO_fn::clean_string($alias_table).'.'.$primary_key;
PinaCode::set_var($pri_name, $primary_value);
// preparo i campi da salvare
// Setto le variabili per i campi calcolati // DA TESTARE
foreach ($fields_names as $kn=>$fn) {
if ($kn != "_dbp_alias_table_") {
// ?
if (is_countable($fn[$key])) {
$fn[$key] = maybe_serialize($fn[$key]);
}
$fn[$key] = wp_kses_post(wp_unslash( $fn[$key] ));
//$sql[$kn] = $fn[$key];
PinaCode::set_var(ADFO_fn::clean_string($alias_table).".".$kn, $fn[$key]);
PinaCode::set_var("data.".$kn, $fn[$key]);
}
}
foreach ($fields_names as $kn=>$fn) {
if ($kn != "_dbp_alias_table_") {
$sql[$kn] = wp_kses_post(wp_unslash( $fn[$key] ));
}
}
// se primary key è un valore
if ($primary_value != "") {
$exists = $wpdb->get_var($wpdb->prepare('SELECT count(*) as tot FROM `'.ADFO_fn::sanitize_key($table).'` WHERE `'.ADFO_fn::sanitize_key($primary_key).'` = %s', absint($primary_value)));
if ($exists == 0) {
$sql[$primary_key] = $primary_value;
}
} else {
$sql[$primary_key] = $primary_value;
}
$setting = false;
$option = false;
if (is_a($form_dbp_id, 'admin_form\ADFO_class_form')) {
//TODO non è più sensata una sola funzione?
$setting = $form_dbp_id->find_setting_from_table_field($alias_table);
$option = $form_dbp_id->find_option_from_table_field($alias_table);
if ($option->module_type != 'EDIT' || $option->table_status == "CLOSE") continue;
}
if ($exists == 1) {
if (count($sql) > 0) {
$query_to_execute[] = ['action'=>'update', 'table'=>$table, 'sql_to_save'=>$sql, 'id'=> [$primary_key=>$primary_value], 'table_alias'=>$alias_table, 'pri_val'=>$primary_value, 'pri_name'=>$primary_key, 'setting' => $setting];
}
} else if ($exists == 0) {
$json_result['reload'] = 1;
if (isset($sql[$primary_key])) {
unset($sql[$primary_key]);
}
if (count($sql) > 0 && !(isset($sql['_dbp_leave_empty_']) && $sql['_dbp_leave_empty_'] == 1 )) {
$query_to_execute[] = ['action'=>'insert', 'table'=>$table, 'sql_to_save'=>$sql, 'table_alias'=>$alias_table, 'pri_val'=>$primary_value, 'pri_name'=>$primary_key, 'setting' => $setting];
}
} else {
// ha trovaro risultati doppi?
}
}
//die($pri);
}
}
$ris = ADFO_functions_list::execute_query_savedata($query_to_execute, $dbp_id, 'admin-form');
foreach ($ris as $r) {
if (!($r['result'] == true || ($r['result'] == false && $r['error'] == "" && $r['action']=="update"))) {
$json_result['error'] = ($r['error'] != "") ? $r['error'] : 'the data could not be saved';
ADFO_fn::set_cookie('error', $json_result['error']);
if (is_countable($queries_executed) && count($queries_executed) > 0) {
$json_result['msg'] =sprintf( __('%s record update','admin_form'), count($queries_executed));
ADFO_fn::set_cookie('msg', $json_result['msg']);
}
wp_send_json($json_result);
die();
} else {
$queries_executed[] = $r['query'];
}
}
if (is_countable($queries_executed) && count($queries_executed) > 0) {
$json_result['msg'] = __('Saved','admin_form');
ADFO_fn::set_cookie('msg', $json_result['msg']);
}
// preparo i dati da inviare per aggiornare la tabella nel frontend!
$dbp_global_list_id = ADFO_fn::get_request('dbp_global_list_id', 0, 'absint');
$table_model = $this->get_table_model_for_sidebar($dbp_global_list_id);
if ($table_model != false) {
// preparo i dati da inviare per aggiornare la tabella nel frontend!
$dbp_global_list_id = ADFO_fn::get_request('dbp_global_list_id', 0, 'absint');
if ($dbp_global_list_id > 0) {
$post = ADFO_functions_list::get_post_dbp($dbp_global_list_id);
ADFO_functions_list::add_lookups_column($table_model, $post);
}
$table_model->get_list();
if ($dbp_global_list_id > 0) {
$table_model->update_items_with_setting($post);
}
ADFO_fn::remove_hide_columns($table_model);
$table_items = $table_model->items;
if (count($table_model->items) == 2) {
$json_result['table_item_row'] = array_pop($table_items);
} else {
$json_result['reload'] = 1;
}
}
//$json_result['error'] = 'OPS ERRORE!!!';
wp_send_json($json_result);
die();
}
/**
* Imposta i cookie in una variabile statica e li rimuove dai cookie
*/
function init_get_msg_cookie() {
ADFO_fn::init_get_msg_cookie();
}
/**
* Raggruppo questo pezzettino di codice solo perché usato di continuo per preparare la query sull'edit, view, ecc..
*/
private function get_table_model_for_sidebar($dbp_id = 0) {
if ($dbp_id > 0) {
$post = ADFO_functions_list::get_post_dbp($dbp_id);
$table_model = new ADFO_model();
$table_model->prepare($post->post_content['sql']);
}
if (isset($_REQUEST['ids']) && is_countable($_REQUEST['ids'])) {
$filter = [];
$ids = ADFO_fn::sanitize_absint_recursive($_REQUEST['ids']);
foreach ($ids as $column => $id) {
$column = str_replace("`", "", $column );
$column = "`".str_replace(".", "`.`", $column )."`";
$filter[] = ['op' => "=", 'column' => $column, 'value' => $id];
}
$table_model->list_add_where($filter);
}
return $table_model;
}
/**
* Mette tutte le tabelle di wordpress in stato pubblicato
*/
function publish_wp_tables($status, $table) {
global $wpdb;
if (in_array($table, [$wpdb->posts, $wpdb->users, $wpdb->prefix.'usermeta', $wpdb->prefix.'terms' , $wpdb->prefix.'termmeta', $wpdb->prefix.'term_taxonomy', $wpdb->prefix.'term_relationships', $wpdb->postmeta, $wpdb->prefix.'options', $wpdb->prefix.'links', $wpdb->prefix.'comments', $wpdb->prefix.'commentmeta'])) {
$status = 'PUBLISH';
}
return $status;
}
/**
* Quando cliccano il popup per votare il plugin registro la scelta (già votato o non mi piace)
*/
function record_preference_vote() {
$vote = sanitize_text_field($_REQUEST['msg']);
$info = get_option('_af_activete_info');
$info['voted'] = $vote;
update_option('_af_activete_info', $info, false);
wp_send_json(['done']);
die();
}
/**
* Calcola quali record sta per eliminare a seconda della query e delle primary ID
*/
public function af_delete_confirm() {
ADFO_fn::require_init();
$json_send = [];
//$json_send = ['error' => '', 'items' => '', 'checkboxes'];
if (!isset($_REQUEST['ids']) || !is_countable($_REQUEST['ids'])) {
$json_send['error'] = __('I have not found any results. Verify that the primary key of each selected table is always displayed in the MySQL SELECT statement.', 'admin_form');
wp_send_json($json_send);
die();
}
if (isset($_REQUEST['dbp_id']) && $_REQUEST['dbp_id'] > 0) {
$ids = ADFO_fn::sanitize_intjson_recursive($_REQUEST['ids']);
$json_send = ADFO_fn::prepare_delete_rows($ids,'', absint($_REQUEST['dbp_id']));
} else if ($_REQUEST['sql'] != "") {
$ids = ADFO_fn::sanitize_absint_recursive($_REQUEST['ids']);
//TODO security nessun sql deve passare su request!
$json_send = ADFO_fn::prepare_delete_rows($ids, wp_kses_post( wp_unslash($_REQUEST['sql'])) );
} else {
$json_send['error'] = __('Something wrong', 'admin_form');
wp_send_json($json_send);
die();
}
unset($json_send['sql']);
wp_send_json($json_send);
die();
}
}
$ADFO_admin_loader = new ADFO_admin_loader();