Skip to content

Commit

Permalink
Merge pull request #39 from Jack-Dane/tidy
Browse files Browse the repository at this point in the history
Tidy
  • Loading branch information
Jack-Dane authored Jun 13, 2024
2 parents 99f1fd5 + 10fb6f5 commit d4aba87
Show file tree
Hide file tree
Showing 59 changed files with 1,901 additions and 1,946 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,13 @@

namespace odoo_conn\admin\api\endpoints;

use odoo_conn\admin\database_connection\OdooConnGetContact7Form;

class OdooConnGetContact7Form extends OdooConnGetExtendedSchema
{

public function __construct()
{
parent::__construct("wpcf7_contact_form");
}

protected function get_public_key()
{
return "ID";
}

protected function get_columns()
{
return "ID, post_title";
}

protected function get_table_name()
{
global $wpdb;

return $wpdb->posts;
}

protected function where_query()
{
return "post_type=%s";
}
}

function odoo_conn_get_contact_7_forms($data)
{
$get_contact_7_form = new OdooConnGetContact7Form();
$response = $get_contact_7_form->request($data);
return $response;
return $get_contact_7_form->request($data);
}

function odoo_conn_get_contact_7_forms_schema()
Expand Down
8 changes: 3 additions & 5 deletions admin/api/endpoints/main.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?php

include_once("odoo_connections.php");
include_once("odoo_forms.php");
include_once("odoo_form_mappings.php");
include_once("c7f_posts.php");
include_once("odoo_errors.php");
require_once 'odoo_connections.php';
require_once 'odoo_forms.php';
require_once 'cf7_posts.php';

?>
178 changes: 2 additions & 176 deletions admin/api/endpoints/odoo_connections.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,162 +2,9 @@

namespace odoo_conn\admin\api\endpoints;

use \odoo_conn\encryption\OdooConnEncryptionFileHandler;
use \odoo_conn\encryption\OdooConnEncryptionHandler;
use odoo_conn\odoo_connector\odoo_connector\OdooConnException;
use \odoo_conn\odoo_connector\odoo_connector\OdooConnOdooConnector;
use WP_Error;
use odoo_conn\admin\database_connection\OdooConnGetOdooConnection;


trait OdooConnOdooConnectionTableName
{

protected function get_table_name()
{
global $table_prefix;

return $table_prefix . "odoo_conn_connection";
}

}


trait OdooConnOdooConnectionColumns
{

protected function get_columns()
{
$columns = [
"id",
"name",
"username",
"url",
"database_name"
];

return implode(", ", $columns);
}

}


class OdooConnGetOdooConnection extends OdooConnGetBaseSchema
{

use OdooConnOdooConnectionTableName;
use OdooConnOdooConnectionColumns;
}

class OdooConnGetOdooConnectionSingle extends OdooConnGetExtendedSchema
{

use OdooConnOdooConnectionTableName;

public function request($data)
{
$connections = parent::request($data);
return !$connections ? null : $connections[0];
}

protected function where_query()
{
return "id=%d";
}
}


class OdooConnTestOdooConnection extends OdooConnGetOdooConnectionSingle
{
public function request($data)
{
$connection = parent::request($data);
if (!$connection) {
return new WP_Error(
"no_connection",
"No connection for that Id",
array("status" => 404)
);
}

return $connection;
}

public function test_connection($odoo_connector)
{
try {
$success = $odoo_connector->test_connection();
} catch (OdooConnException $e) {
return array(
"success" => false,
"error_string" => $e->getMessage(),
"error_code" => $e->getCode()
);
}
return array(
"success" => $success
);
}
}


class OdooConnPostOdooConnection extends OdooConnPostBaseSchema
{

use OdooConnOdooConnectionTableName;
use OdooConnOdooConnectionColumns;

public function __construct($encryption_handler)
{
$this->encryption_handler = $encryption_handler;
}

protected function parse_data($data)
{
$api_key = sanitize_text_field($data["api_key"]);
$encrypted_api_key = $this->encryption_handler->encrypt($api_key);

return array(
"name" => sanitize_text_field($data["name"]),
"username" => sanitize_text_field($data["username"]),
"api_key" => $encrypted_api_key,
"url" => sanitize_url($data["url"]),
"database_name" => sanitize_text_field($data["database_name"]),
);
}

protected function insert_data_types()
{
return array("%s", "%s", "%s", "%s", "%s");
}

}


class OdooConnPutOdooConnection extends OdooConnPutBaseSchema
{

use OdooConnOdooConnectionTableName;
use OdooConnOdooConnectionColumns;

protected function update_data($data)
{
return array(
"name" => sanitize_text_field($data["name"]),
"username" => sanitize_text_field($data["username"]),
"url" => sanitize_url($data["url"]),
"database_name" => sanitize_text_field($data["database_name"])
);
}

}


class OdooConnDeleteOdooConnection extends OdooConnDeleteBaseSchema
{

use OdooConnOdooConnectionTableName;
}

function odoo_conn_base_odoo_connections_schema()
{
return array(
Expand Down Expand Up @@ -194,28 +41,7 @@ function odoo_conn_base_odoo_connections_schema()
function odoo_conn_get_odoo_connections($data)
{
$get_odoo_connection = new OdooConnGetOdooConnection();
$response = $get_odoo_connection->request($data);
return $response;
}

function odoo_conn_test_odoo_connection($data)
{
$id = $data["id"];
$connection_tester = new OdooConnTestOdooConnection($id);
$connection = $connection_tester->request($data);

$encryption_file_handler = new OdooConnEncryptionFileHandler();
$encryption_handler = new OdooConnEncryptionHandler($encryption_file_handler);
$decrypted_api_key = $encryption_handler->decrypt($connection->api_key);

$odoo_connector = new OdooConnOdooConnector(
$connection->username,
$decrypted_api_key,
$connection->database_name,
$connection->url
);

return $connection_tester->test_connection($odoo_connector);
return $get_odoo_connection->request($data);
}

add_action("rest_api_init", function () {
Expand Down
126 changes: 1 addition & 125 deletions admin/api/endpoints/odoo_forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,132 +2,8 @@

namespace odoo_conn\admin\api\endpoints;

use odoo_conn\admin\database_connection\OdooConnGetOdooForm;

trait OdooConnOdooFormTableName
{

protected function get_table_name()
{
global $table_prefix;

return $table_prefix . "odoo_conn_form";
}

}


trait OdooConnOdooFormColumns
{

protected function get_columns()
{
global $wpdb, $table_prefix;

$columns = [
$table_prefix . "odoo_conn_form.id",
$table_prefix . "odoo_conn_form.odoo_connection_id",
$table_prefix . "odoo_conn_connection.name as 'odoo_connection_name'",
$table_prefix . "odoo_conn_form.odoo_model",
$table_prefix . "odoo_conn_form.name",
$table_prefix . "odoo_conn_form.contact_7_id as 'contact_7_id'",
$wpdb->posts . ".post_title as 'contact_7_title'"
];

return implode(", ", $columns);
}

}


class OdooConnGetOdooForm extends OdooConnGetBaseSchema
{

use OdooConnOdooFormTableName;
use OdooConnOdooFormColumns;

protected function foreign_keys()
{
global $wpdb, $table_prefix;

return [
"odoo_connection_id" => [
"table_name" => $table_prefix . "odoo_conn_connection",
"column_name" => "id"
],
"contact_7_id" => [
"table_name" => $wpdb->posts,
"column_name" => "ID"
]
];
}

}


class OdooConnGetOdooFormSingle extends OdooConnGetExtendedSchema
{

use OdooConnOdooFormTableName;

public function request($data)
{
$connections = parent::request($data);
return !$connections ? null : $connections[0];
}

protected function where_query()
{
return "id=%d";
}
}


class OdooConnPostOdooForm extends OdooConnPostBaseSchema
{

use OdooConnOdooFormTableName;

protected function parse_data($data)
{
return array(
"odoo_connection_id" => sanitize_text_field($data["odoo_connection_id"]),
"odoo_model" => sanitize_text_field($data["odoo_model"]),
"name" => sanitize_text_field($data["name"]),
"contact_7_id" => sanitize_text_field($data["contact_7_id"])
);
}

protected function insert_data_types()
{
return array("%d", "%s", "%s", "%d");
}

}


class OdooConnPutOdooForm extends OdooConnPutBaseSchema
{

use OdooConnOdooFormTableName;

protected function update_data($data)
{
return array(
"odoo_connection_id" => sanitize_text_field($data["odoo_connection_id"]),
"name" => sanitize_text_field($data["name"]),
"contact_7_id" => sanitize_text_field($data["contact_7_id"]),
"odoo_model" => sanitize_text_field($data["odoo_model"]),
);
}

}


class OdooConnDeleteOdooForm extends OdooConnDeleteBaseSchema
{

use OdooConnOdooFormTableName;
}

function odoo_conn_base_odoo_forms_schema()
{
Expand Down
Loading

0 comments on commit d4aba87

Please sign in to comment.