-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzurmo.php
308 lines (266 loc) · 9 KB
/
zurmo.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
require 'vendor/autoload.php';
/**
* Plugin Name: Gravity Forms Zurmo Add-On
* Description: Integrates Gravity Forms with Zurmo allowing form submissions to be automatically sent to your Zurmo account
* Version: 0.1.1
* Author: AXZM
* Author URI: http://www.axzm.com
*
* @package Gravityforms / Zurmo Addon
* @author Ross Edman / Tyler Ferguson <[email protected]>
* @license GPL-2.0+
* @link http://axzm.com
* @copyright 2013 AXZM
*
* ------------------------------------------------------------------------
* Copyright 2013 AXZM
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
use Zurmo\API as API;
use Helpers\ErrorHandler as Error;
class GFZurmo {
public static $name = "Gravity Forms Zurmo Add-On";
public static $path = "gravity-forms-zurmo/zurmo.php";
public static $url = "http://www.gravityforms.com";
public static $slug = "gravity-forms-zurmo";
public static $version = "0.1.0";
public static $min_gravityforms_version = "1.3.9";
/**
* Iniatilize!
*
* load the plugin and perform all checks to make sure the plugin can operate correctly
* Checks to make sure GravityForms is installed, loaded and adds sidebar navigation for admin
* area.
*/
public static function init()
{
global $pagenow;
if($pagenow === 'plugins.php')
{
add_action("admin_notices", array('GravityForms\Check', 'install'), 10);
}
if( GravityForms\Check::install(false, false) !== 1 )
{
add_action('after_plugin_row_' . self::$path, array('GravityForms\Check', 'plugin') );
return;
}
if(is_admin())
{
//creates a new Settings page on Gravity Forms' settings screen
if( GravityForms\Check::access("gravityforms_zurmo") )
{
RGForms::add_settings_page("Zurmo", array("GravityForms\Views", "settings"), "");
}
}
/**
* Action Form Option
*
* add checkbox into form settings
*/
add_action("gform_properties_settings", array('GravityForms\Views', 'checkbox'), 100);
/**
* Action Form Option
*
* add checkbox into form settings
*/
add_action("gform_properties_settings", array('GravityForms\Views', 'users'), 200);
/**
* Action Form Submission Hook
*
* is called when a gravity form is submitted
*/
add_action("gform_after_submission", array('GFZurmo', 'lead'), 10 ,2);
}
/**
* Create Side Menu Under Forms
*
* adds menu to `Forms` drop down so settings can be accessed
*/
public static function zurmo_page()
{
if(isset($_GET["view"]) && $_GET["view"] == "edit")
{
self::edit_page($_GET["id"]);
}
else
{
self::settings_page();
}
}
/**
* API
*
* load all meta fields for Zurmo install
*
* @var ZurmoAPI dependency injection, pass the whole object in
*/
public static function api( API\Connect $zurmo )
{
$api = false;
//global highrise settings
$settings = get_option("gf_zurmo_settings");
if( !empty($settings["url"]) && !empty($settings["password"]) && !empty($settings["username"]) )
{
$zurmo->setup($settings["url"], $settings["username"], $settings["password"]);
$api = $zurmo->login();
}
return $api;
}
/*
|------------------------------------
| New Lead Function
|------------------------------------
|
| Creates a new lead in the system. Requires a data array with possible fields:
| firstName
| lastName
| primaryEmail => Array(emailAddress, optOut)
| description
| mobilePhone
| officePhone
| department
| jobTitle
| companyName
| website
| source => array(value) - custom generated by client
| state => array(id)- this is not a geographical state like Texas, but a contact state which is defined by the installation of Zurmo. You must
| use the "contactStates" function to retrieve this info
|
|
*/
public function lead( $entry, $form )
{
//if($form['title'] == 'Demo Signup')
//{
$zurmo = new API\Connect();
$api = self::api($zurmo);
if($api)
{
//if the user is connected
if($api['token'])
{
//if the "Enable Zurmo" checkbox on the form settings page is checked
if( !empty( $form['enableZurmo'] ) )
{
$data = array();
foreach($form['fields'] as $field)
{
if($field['type'] == "name")
{
$data['firstName'] = $entry[$field['id'].'.3'];
$data['lastName'] = $entry[$field['id'].'.6'];
}
elseif($field['type'] == "email")
{
$data['primaryEmail']['emailAddress'] = $entry[$field['id']];
$data['primaryEmail']['optOut'] = 1;
}
elseif($field['type'] == "phone")
{
$data["mobilePhone"] = $entry[$field['id']];
}
elseif($field['type'] == "textarea")
{
$data['description'] = 'Message: '.$entry[$field['id']].' | '.$data['description'];
}
elseif($field['type'] == "time")
{
$data['description'] = $data['description'].' Time available: '.$entry[$field['id']];
}
elseif($field['label'] == "Company")
{
$data['companyName'] == $entry[$field['id']];
}
}
$data['state'] = array('id'=>3);
//get data here from form
/*
$data = array(
'firstName' => $entry["1.3"],
'lastName' => $entry["1.6"],
'primaryEmail' => array(
'emailAddress' => $entry["2"],
'optOut' => 1
),
'description' => $entry["5"].' Time available: '.$entry["6"],
'mobilePhone' => $entry["3"],
'source' => array('value' => $form['title']),
'state' => array('id' => 3),
);
*/
/*
|-------------------------------------
| Set headers
|-------------------------------------
*/
$headers = array(
'Accept: application/json',
'ZURMO_SESSION_ID: ' . $api['sessionId'],
'ZURMO_TOKEN: ' . $api['token'],
'ZURMO_API_REQUEST_TYPE: REST',
);
//global highrise settings
$settings = get_option("gf_zurmo_settings");
$url = $settings['url'];
/*
|-------------------------------------
| Make API call
|-------------------------------------
*/
$response = API\RestHelper::call($url.'/app/index.php/leads/contact/api/create/', 'POST', $headers, array('data' => $data));
//exit(var_dump($data));
$response = json_decode($response, true);
/*
|-------------------------
| Handle Response
|-------------------------
*/
if ($response['status'] == 'SUCCESS')
{
$contact = $response['data'];
$id = $contact['id'];
$data = array(
'owner' => array('id' => $form['enableUser'])
);
if(!empty($form['enableUser']))
{
$response = API\RestHelper::call($url.'/app/index.php/leads/contact/api/update/'.$id, 'PUT', $headers, array('data' => $data));
//exit(var_dump($response));
$response = json_decode($response, true);
}
//return $contact;
//Do something with contact data
}
else
{
// Error
$errors = $response['errors'];
//return $errors;
// Do something with errors, show them to user
}
}
}
}
}
//}
}
/**
* Link It Up
*
* hook into the Wordpress init
*/
add_action('init', array('GFZurmo', 'init'));