This repository has been archived by the owner on Feb 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ninja-forms-smartsheet.php
290 lines (236 loc) · 6.85 KB
/
ninja-forms-smartsheet.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( !defined('ABSPATH') )
define('ABSPATH', dirname(__FILE__) . DIRECTORY_SEPARATOR);
require ABSPATH . '..' . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR. 'autoload.php';
use SmartSheet\SmartSheet;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
define('SMARTSHEET_KEY', 'smartsheet');
define('FORM', 'form');
define('FIELDS', 'fields');
define('SMARTSHEET_DELIMITER', '_');
define('SMARTSHEET_ID', 'id');
/*
* Plugin Name: Ninja Forms - smartsheet
* Plugin URI: https://github.com/catenare/ninjaforms-smartsheet
* Description: Add a new sheet to Smartsheet when publishing a new form. Update the sheet with new data when form is submitted.
* Version: 3.0.0
* Author: Johan Martin
* Author URI: http://www.johan-martin.com
* Text Domain: ninja-forms-smartsheet
*
* Copyright 2017 Johan Martin.
*/
if ( version_compare( get_option( 'ninja_forms_version', '0.0.0' ), '3', '<' ) || get_option( 'ninja_forms_load_deprecated', false ) ) {
//include 'deprecated/ninja-forms-smartsheet.php';
} else {
/**
* Class NF_Smartsheet
*/
final class NF_Smartsheet {
const VERSION = '0.0.1';
const SLUG = 'smartsheet';
const NAME = 'smartsheet';
const AUTHOR = 'Johan Martin';
const PREFIX = 'NF_Smartsheet';
const SMARTSHEET_KEY = 'smartsheet';
/**
* @var NF_Smartsheet
* @since 3.0
*/
private static $instance;
/**
* Plugin Directory
*
* @since 3.0
* @var string $dir
*/
public static $dir = '';
/**
* Plugin URL
*
* @since 3.0
* @var string $url
*/
public static $url = '';
/**
* @var SmartSheet
*/
private $smartsheet;
private $smartsheet_field_array_by_label = [];
/**
* @var Logger
*/
protected $logger;
/**
* Main Plugin Instance
*
* Insures that only one instance of a plugin class exists in memory at any one
* time. Also prevents needing to define globals all over the place.
*
* @since 3.0
* @static
* @static var array $instance
* @return NF_Smartsheet Highlander Instance
*/
public static function instance() {
if ( ! isset( self::$instance ) && ! ( self::$instance instanceof NF_Smartsheet ) ) {
self::$instance = new NF_Smartsheet();
self::$dir = plugin_dir_path( __FILE__ );
self::$url = plugin_dir_url( __FILE__ );
/*
* Register our autoloader
*/
spl_autoload_register( array( self::$instance, 'autoloader' ) );
}
return self::$instance;
}
public function __construct() {
$url = Ninja_Forms()->get_setting( 'api' );
$token = Ninja_Forms()->get_setting( 'token' );
$this->smartsheet = new SmartSheet( $token, $url );
$this->logger = new Logger( SMARTSHEET_KEY );
$path = __DIR__ . DIRECTORY_SEPARATOR . 'log' . DIRECTORY_SEPARATOR . 'output.log';
$this->logger->pushHandler( new StreamHandler( $path, Logger::DEBUG ) );
// $this->logger->pushHandler(new BrowserConsoleHandler());
/*
* Required for all Extensions.
*/
add_action( 'admin_init', array( $this, 'setup_license' ) );
/*
* Optional. If your extension processes or alters form submission data on a per form basis...
*/
add_filter( 'ninja_forms_register_actions', array( $this, 'register_actions' ) );
add_filter( 'ninja_forms_plugin_settings', array( $this, 'plugin_settings' ) );
add_filter( 'ninja_forms_plugin_settings_groups', array( $this, 'plugin_settings_groups' ) );
add_filter( 'ninja_forms_update_setting_smartsheet', array( $this, 'save_smartsheet_settings' ) );
add_filter( 'ninja_forms_save_form', array( $this, 'form_publish' ) );
}
/**
* @param $actions
*
* @return mixed
*/
public function register_actions( $actions ) {
$actions['smartsheet'] = new NF_Smartsheet_Actions_Smartsheet( $this->smartsheet );
// includes/Actions/Smartsheet.php
return $actions;
}
/**
* @param $class_name
*/
public function autoloader( $class_name ) {
if ( class_exists( $class_name ) ) {
return;
}
if ( false === strpos( $class_name, self::PREFIX ) ) {
return;
}
$class_name = str_replace( self::PREFIX, '', $class_name );
$classes_dir = realpath( plugin_dir_path( __FILE__ ) ) . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR;
$class_file = str_replace( '_', DIRECTORY_SEPARATOR, $class_name ) . '.php';
if ( file_exists( $classes_dir . $class_file ) ) {
require_once $classes_dir . $class_file;
}
}
/**
* Template
*
* @param string $file_name
* @param array $data
*/
public static function template( $file_name = '', array $data = array() ) {
if ( ! $file_name ) {
return;
}
extract( $data );
include self::$dir . 'includes/Templates/' . $file_name;
}
/**
* Config
*
* @param $file_name
*
* @return mixed
*/
public static function config( $file_name ) {
return include self::$dir . 'includes/Config/' . $file_name . '.php';
}
/**
*
*/
public function setup_license() {
if ( ! class_exists( 'NF_Extension_Updater' ) ) {
return;
}
new NF_Extension_Updater( self::NAME, self::VERSION, self::AUTHOR, __FILE__, self::SLUG );
}
/**
* @param $settings
*
* @return mixed
*/
public function plugin_settings( $settings ) {
$settings['smartsheet'] = array(
'token' => array(
'id' => 'token',
'type' => 'textbox',
'width' => 'one-half',
'label' => __( 'Token', 'ninja-forms-smartsheet' ),
'desc' => __( 'Smartsheet Token Value', 'ninja-forms-smartsheet' )
),
'api' => array(
'id' => 'api',
'type' => 'textbox',
'width' => 'one-half',
'label' => __( 'Url', 'ninja-forms-smartsheet' ),
'desc' => __( 'Smartsheet Url Value', 'ninja-forms-smartsheet' )
)
);
return $settings;
}
/**
* @param $groups
*/
public function plugin_settings_groups( $groups ) {
$groups['smartsheet'] = array(
'id' => 'smartsheet',
'label' => __( 'Smartsheet Settings', 'ninja-forms-smartsheet' ),
);
}
/**
* @param $settings_value
*/
public function save_smartsheet_settings( $settings_value ) {
if ( strpos( $settings_value, '_' ) ) {
$parts = exploded( '_', $settings_value );
foreach ( $parts as $key => $value ) {
Ninja_Forms()->update_setting( 'smartsheet_part_' . $key, $value );
}
}
}
/**
* Create new smartsheet form
*
* @param $id
*/
public function form_publish( $id ) {
$smartsheet = new \NinjaForm\SmartSheet($this->smartsheet, $id);
if ( $smartsheet->getIsNewForm() && $smartsheet->getIsSmartsheetAction() ) {
$data = $smartsheet->getDataForSmartsheet();
$result = $smartsheet->saveToSmartsheet($data);
$smartsheet->updateColumnSettingWithSmartsheetId();
$smartsheet->updateSheetSmartsheetId();
}
}
}
/**
* @return NF_Smartsheet
*/
function NF_Smartsheet() {
return NF_Smartsheet::instance();
}
NF_Smartsheet();
}