This repository has been archived by the owner on Dec 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chargify_sites.module
302 lines (263 loc) · 8.63 KB
/
chargify_sites.module
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
<?php
include_once('chargify_sites.features.inc');
/**
* Implementation of hook_init().
*/
function chargify_sites_init() {
drupal_add_css(drupal_get_path('module', 'chargify_sites') .'/chargify_sites.css');
}
/**
* Implementation of hook_menu().
*/
function chargify_sites_menu() {
$items = array();
$items['admin/settings/chargify-sites'] = array(
'title' => 'Chargify Sites',
'description' => 'Configure settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('chargify_sites_settings_form'),
'access arguments' => array('administer chargify sites'),
'file' => 'chargify_sites.pages.inc',
);
$items['new-site/%chargify_sites_product'] = array(
'title' => 'New Subscription',
'title callback' => 'chargify_sites_product_title',
'title arguments' => array(1),
'page callback' => 'chargify_sites_new_site_page',
'page arguments' => array(1),
'access arguments' => array('create site content'),
'file' => 'chargify_sites.pages.inc',
);
$items['new-site/%node/login'] = array(
'title' => 'Please Login or Register',
'description' => 'Please Login or Register in order to complete your purchase.',
'page callback' => 'chargify_sites_login_page',
'page arguments' => array(1),
'access callback' => 'chargify_sites_payment_page_access',
'access arguments' => array(1),
'file' => 'chargify_sites.pages.inc',
);
$items['new-site/%node/time-to-pay'] = array(
'title' => 'Payment',
'title callback' => 'chargify_sites_payment_page_title',
'title arguments' => array(1),
'page callback' => 'chargify_sites_payment_page',
'page arguments' => array(1),
'access callback' => 'chargify_sites_payment_page_access',
'access arguments' => array(1),
'file' => 'chargify_sites.pages.inc',
);
return $items;
}
/**
* Loads up a product by handle from Chargify.
*/
function chargify_sites_product_load($handle) {
$product = chargify_api_product_get($handle, 'handle');
return is_object($product) ? $product : FALSE;
}
/**
* Return a product title.
*/
function chargify_sites_product_title($product) {
return t($product->getName());
}
/**
* Alter the payment page title.
*/
function chargify_sites_payment_page_title($node) {
return t('Time to pay for !title', array('!title' => $node->title));
}
/**
* Provide access for the payment page.
*/
function chargify_sites_payment_page_access($node, $account = NULL) {
global $user;
$account = is_null($account) ? $user : $account;
return ($node->uid == $account->uid);
}
/**
* Implementation of hook_perm().
*/
function chargify_sites_perm() {
return array('administer chargify sites');
}
/**
* Implementation of hook_nodeapi().
*/
function chargify_sites_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
if ($node->type == 'site') {
switch ($op) {
case 'insert':
$_REQUEST['destination'] = 'new-site/'. $node->nid .'/login';
if (isset($node->subscription['product_handle']) && $node->subscription['product_handle']) {
$_REQUEST['destination'] .= '?product_handle='. urlencode($node->subscription['product_handle']);
}
case 'update':
if (isset($node->subscription->subscription_id)) {
chargify_sites_node_save($node);
}
break;
case 'load':
return chargify_sites_node_load($node);
case 'view':
$output = isset($node->subscription) ? theme('chargify_subscription_state', $node->subscription->state) : theme('chargify_sites_payment_link', $node);
$weight = content_extra_field_weight($node->type, 'chargify_sites');
$node->content['chargify_sites'] = array(
'#weight' => !empty($weight) ? $weight : -10,
'#value' => $output,
);
$teaser = $a3;
$page = $a4;
if (!$teaser && $page) {
drupal_set_breadcrumb(array(l(t('My sites'), 'my-sites')));
}
break;
case 'delete':
chargify_sites_node_delete($node);
break;
}
}
}
/**
* Implementation of hook_theme().
*/
function chargify_sites_theme() {
$items = array();
$items['chargify_sites_product_page'] = array(
'arguments' => array('product' => NULL),
'template' => 'chargify-sites-product-page',
);
$items['chargify_sites_tos'] = array(
'arguments' => array('url' => NULL),
);
$items['chargify_sites_payment_link'] = array(
'arguments' => array('nid' => NULL),
);
return $items;
}
/**
* Implementation of hook_strongarm().
*/
function chargify_sites_strongarm() {
return array(
'ant_site' => 1,
'ant_pattern_site' => '[field_domain-url]',
'user_email_verification' => FALSE,
'pathauto_node_site_pattern' => 'my-sites/[title-raw]',
);
}
/**
* Implementation of hook_content_extra_fields().
*/
function chargify_sites_content_extra_fields() {
$extras['chargify_sites'] = array(
'label' => t('Subscription Status'),
'description' => t('Chargify subscription status fieldset.'),
'weight' => -10,
);
return $extras;
}
/**
* Implementation of hook_form_alter().
*/
function chargify_sites_form_site_node_form_alter(&$form, $form_state) {
if ($product = menu_get_object('chargify_sites_product')) {
$form['subscription'] = array('#tree' => TRUE);
$form['subscription']['product_handle'] = array(
'#type' => 'hidden',
'#value' => $product->getHandle(),
);
$form['#product'] = $product;
}
else {
drupal_set_message(t("Whoopsy, there was a problem accessing that product. Try contacting whoever sent you here. :("), 'error');
drupal_goto('<front>');
}
$form['buttons']['preview']['#access'] = FALSE;
$form['buttons']['submit']['#value'] = t('Continue');
if ($tos_path = variable_get('chargify_sites_tos', '')) {
$form['buttons']['submit']['#suffix'] = theme('chargify_sites_tos', $tos_path);
}
}
/**
* Set some theme settings. Called by _webgear_feature_enable().
*/
function chargify_sites_theme_settings() {
return array(
'toggle_node_info_site' => 0,
);
}
/**
* Implementation of hook_preprocess_chargify_sites_product_page().
*/
function chargify_sites_preprocess_chargify_sites_product_page(&$vars) {
drupal_add_js(drupal_get_path('module', 'chargify_sites') .'/fancyForm.js');
global $user;
$product = $vars['product'];
module_load_include('inc', 'node', 'node.pages');
$node_type = 'site';
$node = array('uid' => $user->uid, 'name' => (isset($user->name) ? $user->name : ''), 'type' => $node_type, 'language' => '');
$vars['form_output'] = drupal_get_form($node_type .'_node_form', $node);
$vars['price'] = theme('chargify_product_price', $product);
// Convert line breaks into p tags.
$text = _filter_autop($product->description);
$vars['description'] = t($text);
}
/**
* Theme function for displaying the TOS text.
*/
function theme_chargify_sites_tos($url) {
$output = "<div class='chargify-sites-tos'>\n";
$output .= t('By submitting this form, you are agreeing to the <a href="!url" target="_blank">Terms of Service</a>.', array('!url' => url($url)));
$output .= "</div>\n";
return $output;
}
/**
* Theme function for displaying the link to pay for a node.
*/
function theme_chargify_sites_payment_link($node) {
if (chargify_sites_payment_page_access($node)) {
$options = array(
'attributes' => array(
'title' => chargify_sites_payment_page_title($node),
'class' => 'chargify-sites-payment-link',
),
);
return l(t('Time to Pay!'), "new-site/{$node->nid}/time-to-pay", $options);
}
}
/**
* Helper function to save a node.
*/
function chargify_sites_node_save(&$node) {
$record = array('nid' => $node->nid, 'subscription_id' => $node->subscription->subscription_id);
// Check to see if we're updating.
if (db_result(db_query("SELECT COUNT(*) FROM {chargify_sites} WHERE nid = %d", $node->nid)) > 0) {
drupal_write_record('chargify_sites', $record, array('subscription_id'));
}
// Otherwise, write a new row.
else {
drupal_write_record('chargify_sites', $record);
}
}
/**
* Helper function to process loading a node.
*/
function chargify_sites_node_load(&$node) {
$ret = array();
$sql = "SELECT * FROM {chargify_subscriptions} sub INNER JOIN {chargify_sites} site ON sub.subscription_id = site.subscription_id WHERE site.nid = %d";
if ($subscription = db_fetch_object(db_query($sql, $node->nid))) {
$ret['subscription'] = $subscription;
}
return $ret;
}
/**
* Helper function to save a node.
*/
function chargify_sites_node_delete(&$node) {
if (isset($node->subscription->subscription_id)) {
chargify_subscription_delete_record($node->subscription->subscription_id);
db_query("DELETE FROM {chargify_sites} WHERE nid = %d", $node->nid);
}
}