Skip to content

Commit

Permalink
Implemented - Pushing Contact Form 7 data into Odoo
Browse files Browse the repository at this point in the history
  • Loading branch information
coderscom committed Mar 4, 2017
1 parent 8f51cd0 commit d2ec527
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 16 deletions.
8 changes: 1 addition & 7 deletions admin/class-wp-odoo-form-integrator-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,6 @@ public function get_form_fields() {
public function get_odoo_models() {

$result = array();
/**
* TODO: Fetch name and key of all models dynamically from Odoo
*/
// $result['crm.lead'] = 'crm.lead';
// $result['res.partner'] = 'res.partner';
// echo json_encode($result);
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-wp-odoo-client.php';
$client = new Wp_Odoo_Client(get_option('wp_odoo_form_odoo_url'),
get_option('wp_odoo_form_odoo_database'),
Expand Down Expand Up @@ -420,7 +414,7 @@ public function display_plugin_integrated_forms_page() {
$this->js_object['str_notice_success_message'] = __( 'Odoo-Form Mapping is successfully deleted', 'wp-odoo-form-integrator' );
}

$rec_limit = 20;
$rec_limit = 50;
$sql = "SELECT count(id) as total FROM ". $cc_odoo_integrator_forms;
$result = $wpdb->get_row($sql);
$rec_count = $result->total;
Expand Down
9 changes: 7 additions & 2 deletions admin/js/wp-odoo-form-integrator-admin-add-new.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,17 +254,22 @@
$("#wp_odoo_form_add_new_mapping_table > tbody").html("");

$.each(odoo_fields, function(key, obj) {
// console.log(key+" : "+obj.string);
$('#wp_odoo_form_add_new_mapping_table tbody')
.append('<tr>' +
'<td class="row-title">' +
'<label for="tablecell">'+obj.string+'</label>' +
'</td>' +
'<td>' +
'<label for="tablecell"><code>'+key+'</code></label>' +
'</td>' +
'<td>' +
'<label for="tablecell"><code>'+obj.type+'</code></label>' +
'</td>' +
'<td>' +
'<select name="'+key+'" id="'+key+'" class="regular-text"></select>' +
'<label for="tablecell">'+((obj.required)?'<mark> required <mark>':'optional')+'</label>' +
'</td>' +
'<td>' +
'<select name="'+key+'" id="'+key+'" class="small-text"></select>' +
'</td>' +
'</tr>');
if (form_fields){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@
<table class="widefat" id="wp_odoo_form_add_new_mapping_table">
<thead>
<tr>
<th class="row-title"><?php esc_attr_e( 'Odoo Fields', 'wp-odoo-form-integrator' ); ?></th>
<th><?php esc_attr_e( 'Field Type', 'wp-odoo-form-integrator' ); ?></th>
<th><?php esc_attr_e( 'Form Fields', 'wp-odoo-form-integrator' ); ?></th>
<th width='35%' class="row-title"><?php esc_attr_e( 'Odoo Field', 'wp-odoo-form-integrator' ); ?></th>
<th width='20%'><?php esc_attr_e( 'Field Key', 'wp-odoo-form-integrator' ); ?></th>
<th width='15%'><?php esc_attr_e( 'Field Type', 'wp-odoo-form-integrator' ); ?></th>
<th width='10%'><?php esc_attr_e( 'Required', 'wp-odoo-form-integrator' ); ?></th>
<th width='20%'><?php esc_attr_e( 'Form Field', 'wp-odoo-form-integrator' ); ?></th>
</tr>
</thead>
<tbody>
Expand Down
31 changes: 30 additions & 1 deletion includes/class-wp-odoo-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public function get_fields($model){
$rclient = ripcord::client($this->url."/xmlrpc/2/object");
$ret = $rclient->execute_kw($this->database, $this->get_uid(),
$this->password, $model, 'fields_get',
array(), array('attributes' => array('string', 'help', 'type')));
array(), array('attributes' => array('string', 'required', 'help', 'type')));
if (!$ret[faultCode]){
$ret['status'] = true;
}else{
Expand All @@ -184,4 +184,33 @@ public function get_fields($model){
return $ret;
}

public function push_to_odoo($type, $id, $data){
$res = $this->get_uid();
if (is_integer($res)) {
$rclient = ripcord::client($this->url."/xmlrpc/2/object");
global $wpdb;
$cc_odoo_integrator_forms = $wpdb->prefix . 'cc_odoo_integrator_forms';
$cc_odoo_integrator_field_mapping = $wpdb->prefix . 'cc_odoo_integrator_field_mapping';
$sql = "SELECT A.id, A.odoo_model FROM " . $cc_odoo_integrator_forms . " as A " .
"WHERE A.form_type = '".$type."' AND A.form = ".$id;
$form_ids = $wpdb->get_results($sql);
foreach ( $form_ids as $form_row ) {
$sql = "SELECT A.odoo_field, A.field_type, A.form_field FROM ".
$cc_odoo_integrator_field_mapping . " A WHERE A.parent_id = ".$form_row->id;
$mappings = $wpdb->get_results($sql);
$fields = array();
foreach ( $mappings as $map_row ) {
if ($map_row->form_field!=='') {
$fields[$map_row->odoo_field] = $data[$map_row->form_field];
}
}
$data_fields = array();
array_push($data_fields, $fields);
$ret = $rclient->execute_kw($this->database, $this->get_uid(),
$this->password, $form_row->odoo_model, 'create',
$data_fields);
}
}
}

}
26 changes: 26 additions & 0 deletions includes/class-wp-odoo-form-integrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public function __construct() {
$this->set_locale();
$this->define_admin_hooks();
$this->define_public_hooks();
$this->define_form_hooks();

}

Expand Down Expand Up @@ -189,6 +190,31 @@ private function define_public_hooks() {

}

/**
* Register all of the hooks related to the public-facing functionality
* of the plugin.
*
* @since 1.0.0
* @access private
*/
private function define_form_hooks() {

global $wp_odoo_form_modules;
foreach ($wp_odoo_form_modules as $module) {
$object = new $module();
$this->loader->add_action( $object->get_action_tag(), $object, 'handle_callback' );
}
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-wp-odoo-client.php';
$client = new Wp_Odoo_Client(get_option('wp_odoo_form_odoo_url'),
get_option('wp_odoo_form_odoo_database'),
get_option('wp_odoo_form_odoo_username'),
get_option('wp_odoo_form_odoo_password'));
$this->loader->add_action( 'wp_odoo_form_integrator_push_to_odoo',
$client, 'push_to_odoo', 10, 3 );

}


/**
* Run the loader to execute all of the hooks with WordPress.
*
Expand Down
6 changes: 5 additions & 1 deletion modules/wp-odoo-form-integrator-abstract-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ abstract class Wp_Odoo_Form_Integrator_Abstract_Base {

abstract public function get_plugin_slug();

abstract public function get_action_tag();

abstract public function get_plugin_name();

abstract public function get_all_forms();

abstract public function get_form_fields($formID);
abstract public function get_form_fields($form_id);

abstract public function handle_callback($contact_form);

}
26 changes: 24 additions & 2 deletions modules/wp-odoo-form-integrator-form-contact-7.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ public function get_plugin_slug(){
return "wp-contact-form-7";
}

/**
* Register the stylesheets for the admin area.
*
* @since 1.0.0
*/
public function get_action_tag(){
return "wpcf7_mail_sent";
}

/**
* Register the stylesheets for the admin area.
*
Expand Down Expand Up @@ -63,8 +72,8 @@ public function get_all_forms(){
*
* @since 1.0.0
*/
public function get_form_fields($formID){
$args = get_post_meta( $formID, '_form', 'true' );
public function get_form_fields($form_id){
$args = get_post_meta( $form_id, '_form', 'true' );
preg_match_all( '/\[([^\]]*)\]/', $args, $matches );
if ( isset( $matches['1'] ) ) {
foreach ( $matches['1'] as $fields ) {
Expand All @@ -75,4 +84,17 @@ public function get_form_fields($formID){
return $result;
}

/**
* Register the stylesheets for the admin area.
*
* @since 1.0.0
*/
public function handle_callback($contact_form){
$submission = WPCF7_Submission::get_instance();
if ( $submission ) {
$form_id = $contact_form->id();
$posted_data = $submission->get_posted_data();
do_action( 'wp_odoo_form_integrator_push_to_odoo', __CLASS__, $form_id, $posted_data );
}
}
}

0 comments on commit d2ec527

Please sign in to comment.