Skip to content

Commit

Permalink
Add simple tables for all pages
Browse files Browse the repository at this point in the history
* Basic table that will get the correct data to display straight from the database.
* More work needs to be done to tidy everything up and include additional features.
  • Loading branch information
Jack-Dane committed Oct 17, 2023
1 parent afe7132 commit 165e895
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 18 deletions.
18 changes: 17 additions & 1 deletion admin/api/schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ public function request($data)
abstract class OdooConnGetBaseSchema extends OdooConnBaseSchema
{

protected string $output_type = OBJECT;

public function __construct($output = OBJECT)
{
$this->output_type = $output;
}

protected function get_public_key()
{
return "id";
Expand Down Expand Up @@ -146,7 +153,16 @@ public function request($data)
$joined_query = $this->join_query($query);
$safe_query = $this->prepare_query($joined_query, $data, $argument_array);

return $wpdb->get_results($safe_query);
return $wpdb->get_results($safe_query, $this->output_type);
}

public function count_records()
{
global $wpdb;

$query = "SELECT COUNT(*) as 'count' FROM {$this->get_table_name()}";

return $wpdb->get_results($query)[0]->count;
}

}
Expand Down
1 change: 1 addition & 0 deletions admin/menu_items.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

include("pageHelpers/table_display.php");
include("pages/odoo_form.php");
include("pages/odoo_connection.php");
include("pages/odoo_form_mapping.php");
Expand Down
72 changes: 70 additions & 2 deletions admin/pageHelpers/table_display.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<?php

if (!class_exists('WP_List_Table')) {
require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php');
}

// register styles & scripts used
add_action("admin_enqueue_scripts", __NAMESPACE__ . "callback_for_setting_up_scripts");
add_action("admin_enqueue_scripts", __NAMESPACE__ . "odoo_conn_page_scripts_callback");

function callback_for_setting_up_scripts()
function odoo_conn_page_scripts_callback()
{
$root = esc_url_raw(rest_url());
$nonce = wp_create_nonce("wp_rest");
Expand All @@ -26,4 +30,68 @@ function callback_for_setting_up_scripts()
wp_enqueue_style("odoo-page-style", plugins_url("page_style.css", __FILE__));
}


class OdooConnCustomTableDisplay extends WP_List_Table
{

protected $table_backend;

private int $per_page = 10;

public function __construct($table_backend, $args = array())
{
parent::__construct($args);

$this->table_backend = $table_backend;
}

private function get_table_data()
{
$offset = ($this->get_pagenum() - 1) * $this->per_page;

$filter_data = [
"offset" => $offset,
"limit" => $this->per_page
];

return $this->table_backend->request($filter_data);
}

private function total_records() {
return $this->table_backend->count_records();
}

public function prepare_items()
{
$columns = $this->get_columns();
$hidden = array();
$sortable = array();
$primary = 'name';
$this->_column_headers = array($columns, $hidden, $sortable, $primary);

$table_data = $this->get_table_data();
$total_records = $this->total_records();

$this->set_pagination_args(array(
'total_items' => $total_records,
'per_page' => $this->per_page,
'total_pages' => ceil( $total_records / $this->per_page )
));

$this->items = $table_data;
}

public function column_default($item, $column_name)
{
return $item[$column_name];
}

public function column_cb($item)
{
return "<input type='checkbox' name='element[]' value='{$item['id']}' />";
}

}


?>
35 changes: 30 additions & 5 deletions admin/pages/odoo_connection.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
<?php

require_once(__DIR__ . "/../api/endpoints/odoo_connections.php");

use odoo_conn\admin\api\endpoints\OdooConnGetOdooConnection;

class OdooConnOdooConnectionListTable extends OdooConnCustomTableDisplay
{

function get_columns()
{
return array(
"cb" => '<input type="checkbox" />',
"name" => "Name",
"username" => "Username",
"url" => "URL",
"database_name" => "Database Name"
);
}

}

function odoo_conn_odoo_connection_page()
{
wp_register_script(
Expand All @@ -9,10 +29,10 @@ function odoo_conn_odoo_connection_page()
"root" => esc_url_raw(rest_url()), "nonce" => wp_create_nonce("wp_rest")
));
wp_enqueue_script("odoo-connection");

?>
<div class="wrap">
<h1>Odoo Connections</h1>

<a href="#" id="create-data" class="create-database-record button-primary" value="Create a new Connection">Create
a new Connection</a>
<form method="POST" onsubmit="return submitConnection();" id="form-data" class="submit-database"
Expand All @@ -24,12 +44,17 @@ function odoo_conn_odoo_connection_page()
<input type="text" name="database_name" id="database_name" placeholder="Database Name"/><br/>
<input type="Submit" name="submit" class="button-primary"/>
</form>

<table class="database-table"></table>
<div id="pageination-display"></div>

</div>
<?php

echo "<div class='wrap'>";
$odoo_connection = new OdooConnGetOdooConnection(ARRAY_A);
$table_display = new OdooConnOdooConnectionListTable($odoo_connection);

echo "<form method='post'>";
$table_display->prepare_items();
$table_display->display();
echo "</div>";
}

?>
35 changes: 32 additions & 3 deletions admin/pages/odoo_errors.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
<?php

require_once(__DIR__ . "/../api/endpoints/odoo_errors.php");

use odoo_conn\admin\api\endpoints\OdooConnGetOdooErrors;


class OdooConnOdooErrorsListTable extends OdooConnCustomTableDisplay
{

function get_columns()
{
return array(
"cb" => '<input type="checkbox" />',
"contact_7_id" => "Contact 7 Id",
"time_occurred" => "Time of Error",
"error_message" => "Error Message"
);
}

}


function odoo_conn_odoo_errors_page()
{
wp_register_script(
Expand All @@ -9,13 +30,21 @@ function odoo_conn_odoo_errors_page()
"root" => esc_url_raw(rest_url()), "nonce" => wp_create_nonce("wp_rest")
));
wp_enqueue_script("odoo-errors");

?>
<div class="wrap">
<h1>Odoo Submit Errors</h1>
<table class="database-table"></table>
<div id="pageination-display"></div>
<h1>Odoo Connections</h1>
</div>
<?php

echo "<div class='wrap'>";
$odoo_connection = new OdooConnGetOdooErrors(ARRAY_A);
$table_display = new OdooConnOdooErrorsListTable($odoo_connection);

echo "<form method='post'>";
$table_display->prepare_items();
$table_display->display();
echo "</div>";
}

?>
33 changes: 30 additions & 3 deletions admin/pages/odoo_form.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
<?php

require_once(__DIR__ . "/../api/endpoints/odoo_forms.php");

use odoo_conn\admin\api\endpoints\OdooConnGetOdooForm;

class OdooConnOdooFormListTable extends OdooConnCustomTableDisplay
{

function get_columns()
{
return array(
"cb" => '<input type="checkbox" />',
"odoo_connection_id" => "Odoo Connection ID",
"odoo_model" => "Odoo Model",
"name" => "Name",
"contact_7_id" => "Contact 7 ID"
);
}

}


function odoo_conn_odoo_form_page()
{
wp_register_script(
Expand All @@ -26,11 +47,17 @@ function odoo_conn_odoo_form_page()
<input type="Submit" name="submit" class="button-primary"/>
</form>

<table class="database-table"></table>
<div id="pageination-display"></div>

</div>
<?php

echo "<div class='wrap'>";
$odoo_connection = new OdooConnGetOdooForm(ARRAY_A);
$table_display = new OdooConnOdooFormListTable($odoo_connection);

echo "<form method='post'>";
$table_display->prepare_items();
$table_display->display();
echo "</div>";
}

?>
34 changes: 30 additions & 4 deletions admin/pages/odoo_form_mapping.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
<?php
require_once(__DIR__ . "/../pageHelpers/table_display.php");

require_once(__DIR__ . "/../api/endpoints/odoo_form_mappings.php");

use odoo_conn\admin\api\endpoints\OdooConnGetOdooFormMappings;


class OdooConnOdooFormMappingListTable extends OdooConnCustomTableDisplay
{

function get_columns()
{
return array(
"cb" => '<input type="checkbox" />',
"odoo_form_id" => "Odoo Form ID",
"cf7_field_name" => "CF7 Field Name",
"odoo_field_name" => "Odoo Field Name",
"constant_value" => "Constant Value"
);
}

}

function odoo_conn_odoo_form_mapping_page()
{
Expand Down Expand Up @@ -28,11 +48,17 @@ function odoo_conn_odoo_form_mapping_page()
<input type="Submit" name="submit" class="button-primary"/>
</form>

<table class="database-table"></table>
<div id="pageination-display"></div>

</div>
<?php

echo "<div class='wrap'>";
$odoo_connection = new OdooConnGetOdooFormMappings(ARRAY_A);
$table_display = new OdooConnOdooFormMappingListTable($odoo_connection);

echo "<form method='post'>";
$table_display->prepare_items();
$table_display->display();
echo "</div>";
}

?>

0 comments on commit 165e895

Please sign in to comment.