· 10 years ago · Feb 12, 2016, 09:24 AM
1<?php
2/**
3 * Plugin Name: Wuno Uploader
4 * Plugin URI: https://www.example.com
5 * Description: This plugin adds functionality for uploading CSV files to the database
6 * Version: 1.0.0
7 * Author: Me
8 * Author URI: https://www.example.com
9 */
10
11// create custom plugin settings menu
12add_action('admin_menu', 'wuno_plugin_create_menu');
13
14function wuno_plugin_create_menu() {
15
16 //create new top-level menu
17 add_menu_page('Wuno Plugin Settings', 'Wuno Installer', 'administrator', __FILE__, 'wuno_settings_page' , plugins_url('/images/icon.png', __FILE__) );
18
19 //call register settings function
20 add_action( 'admin_init', 'register_wuno_settings' );
21}
22
23function register_wuno_settings() {
24 //register our settings
25 register_setting( 'wuno-settings-group', 'file_to_install' );
26 register_setting( 'wuno-settings-group', 'some_other_option' );
27 register_setting( 'wuno-settings-group', 'option_etc' );
28}
29
30function productsExec() {
31 $hostname='localhost';
32 $username='usernmae';
33 $password='password';
34 $database='db';
35 $table_name = "myTable";
36 // path where your CSV file is located
37 define('CSV_PATH', '../wp-content/plugins/wuno-uploader/');
38 // Name of your CSV file
39 $csv_file = CSV_PATH . "inventory.csv";
40
41 try {
42 $dbh = new PDO("mysql:host=$hostname;dbname=$database",$username,$password);
43 $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
44 $sql = "DROP TABLE IF EXISTS " . $table_name;
45 $dbh->query($sql);
46
47 $sql = "CREATE TABLE IF NOT EXISTS " . $table_name . " (
48 id int(8) NOT NULL AUTO_INCREMENT,
49 wuno_product varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
50 wuno_description varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
51 wuno_alternates varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
52 wuno_onhand varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
53 wuno_condition varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
54 PRIMARY KEY (id)
55 ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
56 $dbh->query($sql);
57
58 file_put_contents( $csv_file, preg_replace( "@(rn),@", ',', file_get_contents( $csv_file ) ) );
59 if (($handle = fopen($csv_file, "r")) !== FALSE) {
60 fgetcsv($handle);
61 while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
62 $num = count($data);
63 for ($c=0; $c < $num; $c++) {
64 $col[$c] = $data[$c];
65 }
66
67 $col1 = $col[0];
68 $col2 = $col[1];
69 $col3 = $col[2];
70 $col4 = $col[3];
71 $col5 = $col[4];
72
73 // SQL Query to insert data into DataBase
74 $sql = "INSERT INTO " . $table_name . "(wuno_product, wuno_description, wuno_alternates, wuno_onhand, wuno_condition)
75 VALUES('".$col1."','".$col2."','".$col3."','".$col4."','".$col5."')";
76 $dbh->query( $sql );
77 }
78 fclose($handle);
79 }
80 if ($dbh->query($sql)) {
81 echo "<script type= 'text/javascript'>alert('New Record Inserted Successfully');</script>";
82 }
83 else{
84 echo "<script type= 'text/javascript'>alert('Data not successfully Inserted.');</script>";
85 }
86 $dbh = null;
87 }
88 catch(PDOException $e)
89 {
90 echo $e->getMessage();
91 }
92
93 }
94
95function wuno_settings_page() {
96 if (isset($_POST['wuno-inventory'])) {
97 productsExec();
98 }
99?>
100
101<h1>Wuno Inventory Updater</h1>
102
103<form method="POST">
104 <label for="wuno-inventory">Path To Inventory</label>
105 <input type="text" name="wuno-inventory" id="wuno-inventory" value="inventory.csv">
106 <input type="submit" value="Install" class="button button-primary button-large">
107</form>
108
109<?php
110 }
111?>