/[drupal]/contributions/modules/lm_paypal/lm_paypal_paid_adverts.module
ViewVC logotype

Contents of /contributions/modules/lm_paypal/lm_paypal_paid_adverts.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.24 - (show annotations) (download) (as text)
Thu Jun 14 00:37:25 2007 UTC (2 years, 5 months ago) by leemcl
Branch: MAIN
CVS Tags: DRUPAL-5--1-0, DRUPAL-4-7--1-0, HEAD
Branch point for: DRUPAL-5, DRUPAL-4-7, DRUPAL-6--1
Changes since 1.23: +30 -15 lines
File MIME type: text/x-php
Only show paid adverts in my account to user who paid for them or admin
Corrected list to SELECT paid advert nodes only so pager produces valid pages
1 <?php
2
3 // $Id: lm_paypal_paid_adverts.module,v 1.23 2006/10/08 20:51:08 leemcl Exp $
4
5 /**
6 * @file
7 * Enables users to create pages only displayed when paid for with PayPal
8 */
9
10 define(LM_PAYPAL_PAID_ADVERTS, 'LM_PayPal_PaidAd');
11
12 /**
13 * Initialize global variables
14 */
15 function _lm_paypal_paid_adverts_ini() {
16 _lm_paypal_ini();
17 global $_lm_paypal_paid_adverts_type_subs;
18
19 static $inited = 0;
20
21 if ($inited) {
22 return;
23 }
24 $inited = 1;
25
26 $type_subs = variable_get('lm_paypal_paid_adverts_type_subs', null);
27 if (is_null($type_subs)) {
28 $_lm_paypal_paid_adverts_type_subs = array ();
29 }
30 else {
31 $_lm_paypal_paid_adverts_type_subs = unserialize($type_subs);
32 }
33 }
34
35
36 /**
37 * Implementation of hook_help().
38 */
39 function lm_paypal_paid_adverts_help($section) {
40 _lm_paypal_paid_adverts_ini();
41 global $_lm_paypal_welcome; // Welcome message
42 global $_lm_paypal_paid_adverts_type_subs;
43 global $_lm_paypal_drupal_major;
44
45 if ($_lm_paypal_drupal_major > 4) {
46 // Drupal 5
47 $c = '!'; // t() has changed and to use a link in it use '!'
48 $admin = l('LM PayPal Admin', 'admin/lm_paypal/settings');
49 $roles = l(t('user management > roles'), 'admin/user/roles');
50 $access = l(t('user management > access control'), 'admin/user/access');
51 $blocks = l(t('site building > blocks'), 'admin/build/block');
52 $subs = l(t('LM PayPal Subscriptions'), 'admin/lm_paypal/subscriptions');
53 $adminpa = l('LM PayPal Paid Adverts', 'admin/lm_paypal/paid_adverts_settings');
54 }
55 else {
56 // Drupal 4
57 $c = '%';
58 $admin = l('LM PayPal Admin', 'admin/settings/lm_paypal');
59 $roles = l(t('access control > roles'), 'admin/access/roles');
60 $access = l(t('access control'), 'admin/access');
61 $blocks = l(t('administer blocks'), 'admin/block');
62 $subs = l(t('LM PayPal Subscriptions'), 'admin/lm_paypal_subscriptions');
63 $adminpa = l('LM PayPal Paid Adverts', 'admin/settings/lm_paypal_paid_adverts');
64 }
65 $new_sub = l('LM PayPal New Subscriptions', 'admin/lm_paypal/subscriptions/new');
66 $help_pa = l(t('LM PayPal Paid Adverts Help'), 'admin/help/lm_paypal_paid_adverts');
67
68 // HIJACK! This will put at help message at the start of node/add/... pages
69 // for types associated with PayPal
70 if (strncmp($section, 'node/add/', 9) == 0) {
71 $type_subs = $_lm_paypal_paid_adverts_type_subs;
72
73 $type = substr($section, 9);
74 $ts = $type_subs[$type];
75 // Does this node type have a PayPal subscription associated with it
76 if (is_array($ts) && count($ts) > 0) {
77 // It does so print some help
78 $output .= '<center><b>' . t('Nodes of this type will not be published until paid for.<br>After Submitting this new node use the <em>PayPal Publish</em> tab to publish.<br>The status of paid items can be found under <em>my account</em>.') . '</b></center>';
79 return $output;
80 }
81 }
82
83 switch ($section) {
84 // admin/help/lm_paypal_paid_adverts
85 case 'admin/help#lm_paypal_paid_adverts':
86 $output = $_lm_paypal_welcome;
87
88 $output .= '<p>' . t('This module allow users to create Paid Advert pages, which are private by default. They can then make them publically viewable using a PayPal based subscription.') . '</p>';
89
90 $output .= '<p><b>'. t('Special Notes') . ':</b>';
91 $output .= '<ul>';
92 $output .= '<li><b>' . t('This module requires the lm_paypal and lm_paypal_subscription modules to be installed, enabled and configured.') . '</b></li>';
93 $output .= '</ul>';
94 $output .= '</p>';
95
96 $output .= '<h2>' . t('Initial Configuration') . '</h2>';
97 $output .= '<p>' . t('If you are new to this module you need to:');
98 $output .= '<ul>';
99 $output .= '<li>' . t("Update the site specific PayPal settings via ${c}admin. Normally you only need to provide your PayPal Business Email.", array("${c}admin" => $admin)) . '</li>';
100 $output .= '<li>' . t("Create one or more Node Subscriptions using ${c}new_sub .", array("${c}new_sub" => $new_sub)) . '</li>';
101 $output .= '<li>' . t("Associate these roles with node types ${c}adminpa .", array("${c}adminpa" => $adminpa)) . '</li>';
102 $output .= '</ul>';
103 $output .= '</p>';
104 $output .= '<p>' . t('With new and existing nodes of node types associated with Node Subscriptions the contents will be private to the author until paid for.') . '</p>';
105 $output .= '<p>' . t('Once a user creates a new node a tab will appear allowing them to pay for the node with one of the available Node Subscriptions.') . '</p>';
106 $output .= '<p>' . t('Users can view the status of all nodes they have created requiring a subscription under "my account".') . '</p>';
107 $output .= '<br>';
108 return $output;
109
110 // This is the brief description of the module displayed on the modules page
111 case 'admin/modules#description':
112 return t('Provides PayPal paid advert pages (requires lm_paypal and lm_paypal_subscriptions).');
113
114 // This appears at the start of the module admin page before the options
115 case 'admin/settings/lm_paypal_paid_adverts':
116 // This appears at the start of the paid_adverts admin page before the options
117 case 'admin/lm_paypal/paid_adverts':
118 $output = $_lm_paypal_welcome;
119
120 $output .= '<p>'. t("For detailed help please read ${c}help_pa", array("${c}help_pa" => $help_pa)) .'</p>';
121 return $output;
122 }
123 }
124
125 /**
126 * Implementation of hook_menu().
127 */
128 function lm_paypal_paid_adverts_menu($may_cache) {
129 _lm_paypal_paid_adverts_ini();
130 global $user;
131 global $_lm_paypal_paid_adverts_type_subs;
132 global $_lm_paypal_drupal_major;
133
134 $type_subs = $_lm_paypal_paid_adverts_type_subs;
135
136 $items = array();
137
138 if ($may_cache) {
139 if ($_lm_paypal_drupal_major > 4) {
140 // New to Drupal 5 - hook_settings gone so settings is a normal page
141 $items[] = array(
142 'path' => 'admin/lm_paypal/paid_adverts_settings',
143 'title' => t('LM PayPal Paid Advert Settings'),
144 'callback' => 'drupal_get_form',
145 'callback arguments' => array('lm_paypal_paid_adverts_settings_form'),
146 'access' => user_access('administer site configuration'),
147 'type' => MENU_NORMAL_ITEM,
148 'weight' => 2,
149 // New to Drupal 5 - every path has a description
150 'description' => t('PayPal Paid Advert interface configuration.'),
151 );
152 }
153 $items[] = array(
154 'path' => 'admin/lm_paypal/paid_adverts',
155 'title' => t('LM PayPal Show Paid Adverts'),
156 'callback' => 'lm_paypal_paid_adverts_list_nodes',
157 'access' => user_access('administer lm_paypal'),
158 'weight' => -2,
159 // New to Drupal 5 - every path has a description
160 'description' => t('Show all PayPal Paid Adverts.'),
161 );
162 }
163 else {
164 // Borrowed from book.module
165 // This appears as a tab on the edit page which is ideal as you shouldn't
166 // pay for an adverts until after it is created.
167 // This is called for every menu display so try and keep the costs down!
168 if (arg(0) == 'node' && is_numeric(arg(1))) {
169 $nid = arg(1);
170 $node = node_load($nid);
171 $has_sub = lm_paypal_paid_adverts_has_sub($node, $subid, $paidfor);
172 if ($has_sub) {
173 // Allow admin or the node owner to view the PayPal tab
174 if ($user->uid == 1 || $node->uid == $user->uid) {
175 if (!$paidfor) {
176 $title = t('PayPal Publish');
177 }
178 else {
179 $title = t('Published');
180 }
181 $items[] = array(
182 'path' => 'node/'. $nid .'/PayPal',
183 'title' => $title,
184 'callback' => 'lm_paypal_paid_adverts_payment',
185 'callback arguments' => array($nid),
186 'type' => MENU_LOCAL_TASK,
187 'weight' => 4,
188 );
189 }
190 }
191 }
192 }
193
194 return $items;
195 }
196
197 // Ugly magic to hide lm_paypal_settings from Drupal 5.0 as it spots its
198 // existance and refuses to access the real page.
199 if (strncmp(VERSION, '4', 1) == 0) {
200 /**
201 * Implementation of hook_settings()
202 * Note: hook_settings not used in Drupal 5.
203 */
204 function lm_paypal_paid_adverts_settings() {
205 return lm_paypal_paid_adverts_settings_form();
206 }
207 }
208
209 /**
210 * Implementation of hook_settings()
211 */
212 function lm_paypal_paid_adverts_settings_form() {
213 _lm_paypal_paid_adverts_ini();
214 global $_lm_paypal_paid_adverts_type_subs;
215 global $_lm_paypal_drupal_major;
216
217 if (!user_access('administer lm_paypal')) {
218 drupal_access_denied();
219 return;
220 }
221
222 $form['#base'] = 'lm_paypal_paid_adverts_settings_form';
223
224 $form ['header'] = array(
225 '#value' => '<h2>' . t('Associate node subscriptions with node types.') . '</h2>',
226 );
227
228 $form ['note'] = array(
229 '#value' => '<em>' . t('Once a subscription is associated with a node type when new nodes of that type are created they will be PRIVATE to the author until paid for.') . '</em>',
230 );
231
232 // Note: Drupal 5 pass 'names' to get a type,name array (Drupal 4 ignores it)
233 $node_types = node_get_types('names');
234 $node_subs = lm_paypal_subscription_node_subs();
235 if (count($node_subs) <= 0) {
236 $form ['none'] = array(
237 '#value' => t('No node subscriptions defined.'),
238 );
239 }
240 else {
241 $type_subs = $_lm_paypal_paid_adverts_type_subs;
242 foreach ($node_types as $node_type => $node_name) {
243 // Sadly when these form items are passed to submit they are no
244 // longer nested. So they are flattened and mixed with the other items.
245 // By sticking 'NT' at the start I can tell them apart.
246 $form ['lm_paypal_paid_adverts_node_subs']['NT'.$node_type] = array(
247 '#type' => 'checkboxes',
248 '#title' => $node_name,
249 '#options' => $node_subs,
250 '#default_value' => $type_subs[$node_type],
251 '#description' => t('Subscriptions, if any, available to publish nodes of this type.'),
252 );
253 }
254 }
255
256 if ($_lm_paypal_drupal_major > 4) {
257 // Borrowed from system_settings_form()
258 $form['buttons']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration') );
259 $form['buttons']['reset'] = array('#type' => 'submit', '#value' => t('Reset to defaults') );
260 }
261
262 return $form;
263 }
264
265 /**
266 * Saves the lm_paypal_paid_advert settings.
267 *
268 * @param $form_id
269 * The form_id that caused this _submit page to be called.
270 * @param $values
271 * An array containing the name,value pairs from the form
272 */
273 function lm_paypal_paid_adverts_settings_form_submit($form_id, $values) {
274 _lm_paypal_paid_adverts_ini();
275 global $_lm_paypal_paid_adverts_type_subs;
276
277 // Find all the type sub's by looking for any key that matches NTtype
278 foreach ($values as $key => $val) {
279 if (strncmp($key,"NT",2) != 0) {
280 // Not an NTtype skip it
281 continue;
282 }
283 $type = substr($key,2);
284 // Make sure it is an array and then
285 if (is_array($val) && count($val) > 0) {
286 // Copy over any set entries
287 $ts = array();
288 foreach ($val as $k => $v) {
289 if ($v != 0) {
290 $ts[$k] = $v;
291 }
292 }
293 // Skip empties
294 if (count($ts) > 0) {
295 $type_subs[$type] = $ts;
296 }
297 }
298 }
299 $_lm_paypal_paid_adverts_type_subs = $type_subs;
300 variable_set('lm_paypal_paid_adverts_type_subs', serialize($type_subs));
301 }
302
303 /**
304 * Pay for an unpublished advert
305 *
306 * @param $nid
307 * Node whose details should be displayed
308 */
309 function lm_paypal_paid_adverts_payment($nid) {
310 _lm_paypal_paid_adverts_ini();
311 global $_lm_paypal_paid_adverts_type_subs;
312 global $_lm_paypal_drupal_major;
313
314 $type_subs = $_lm_paypal_paid_adverts_type_subs;
315
316 $paidfor = lm_paypal_node_subscribed($nid, $subid);
317 if ($paidfor) {
318 $sub = lm_paypal_subscribe($subid, 23);
319 $c = ($_lm_paypal_drupal_major > 4 ? '!' : '%');
320 return t("This article has been paid for by the subscription: ${c}sub", array("${c}sub" => $sub));
321 }
322
323 $output .= '<p>' . t('Not yet paid for. To make this article viewable by others you need to pay with PayPal. Please take out <em>ONE</em> of the subscriptions below.') . '</p>';
324
325 $node = node_load($nid);
326 $ts = $type_subs[$node->type];
327 if (is_array($ts) && count($ts) > 0) {
328 foreach ($ts as $type => $sub) {
329 $output .= lm_paypal_subscribe($sub, 15, '', $nid);
330 }
331 }
332 else {
333 watchdog(
334 LM_PAYPAL_PAID_ADVERTS,
335 t('Attemping to pay for node %nid but no subscriptions available', array('%nid' => $nid)),
336 WATCHDOG_WARNING);
337 }
338
339 return $output;
340 }
341
342 /**
343 * Implementation of hook_user().
344 */
345 function lm_paypal_paid_adverts_user($op, &$edit, &$account, $category = NULL) {
346 _lm_paypal_paid_adverts_ini();
347 global $_lm_paypal_debug;
348
349 // In the "my account" view area show all paid_adverts
350 if ($op == 'view' && (user_access('administer lm_paypal') || $user->uid == $account->uid)) {
351 $output = lm_paypal_paid_adverts_list_nodes($account->uid,10);
352 $items [] = array(
353 'title' => '',
354 'value' => $output);
355 $ret_ads = array(t('Paypal Paid Adverts') => $items);
356
357 return $ret_ads;
358 }
359 }
360
361 /**
362 * Does the node require a subscription
363 *
364 * @param $node
365 * The node to check
366 * @param $subid
367 * If the node is subscription based and subscribed this is the subid
368 * @param $paidfor
369 * If the node is subscription based this is TRUE if the node has been paid for
370 * @return
371 * TRUE if the node does require a subscription
372 */
373 function lm_paypal_paid_adverts_has_sub($node, &$subid, &$paidfor)
374 {
375 _lm_paypal_paid_adverts_ini();
376 global $_lm_paypal_paid_adverts_type_subs;
377
378 $type_subs = $_lm_paypal_paid_adverts_type_subs;
379
380 $ts = $type_subs[$node->type];
381 // Does this node type have a PayPal subscription associated with it
382 if (!is_array($ts) || count($ts) <= 0) {
383 return FALSE;
384 }
385
386 $paidfor = lm_paypal_node_subscribed($node->nid, $subid);
387
388 return TRUE;
389 }
390
391 function lm_paypal_paid_adverts_list_nodes($uid = 0, $nodes_per_page = 50)
392 {
393 _lm_paypal_paid_adverts_ini();
394 global $_lm_paypal_paid_adverts_type_subs;
395
396 $type_subs = $_lm_paypal_paid_adverts_type_subs;
397
398 if (!is_numeric($uid) || intval($uid) != $uid) {
399 $uid = 0;
400 }
401
402 $header = array(
403 array('data' => t('User'), 'field' => 'u.name'),
404 array('data' => t('Node'), 'field' => 'n.nid', 'sort' => 'desc'),
405 array('data' => t('Title'), 'field' => 'n.title'),
406 array('data' => t('Type'), 'field' => 'n.type'),
407 array('data' => t('Published')),
408 );
409
410 // Create a list of known paid node types
411 $ts = array();
412 foreach ($type_subs as $k => $v) {
413 $ts[$k] = 1; // This will loose duplicates
414 }
415 // Turn it into a string to be used in an "IN" clause
416 $tstr = '(';
417 foreach ($ts as $t => $v) {
418 if ($nTs > 0) {
419 $tstr .= ',';
420 }
421 $tstr .= "'" . $t . "'";
422 $nTs ++;
423 }
424 $tstr .= ')';
425
426 if ($nTs > 0) {
427 if ($uid == 0) {
428 $sql = db_rewrite_sql("SELECT n.nid, u.name, n.title, n.type FROM {node} n INNER JOIN {users} u ON n.uid = u.uid WHERE n.status = 1 AND n.type IN $tstr");
429 }
430 else {
431 $sql = db_rewrite_sql("SELECT n.nid, u.name, n.title, n.type FROM {node} n INNER JOIN {users} u ON n.uid = u.uid WHERE n.uid = %d AND n.status = 1 AND n.type IN $tstr");
432 }
433
434 $tablesort = tablesort_sql($header);
435
436 $result = pager_query($sql . $tablesort, $nodes_per_page, 0, null, $uid);
437
438 // Note: Drupal 5 pass 'names' to get a type,name array (Drupal 4 ignores it)
439 $node_types = node_get_types('names');
440
441 while ($n = db_fetch_object($result)) {
442 $has_sub = lm_paypal_paid_adverts_has_sub($n, $subid, $paidfor);
443 if ($paidfor) {
444 $pub = t('published');
445 }
446 else {
447 $pub = l(t('not published'),"node/$n->nid/PayPal");
448 }
449 $rows[] = array('data' =>
450 array(
451 $n->name,
452 l(check_plain($n->nid), "node/$n->nid"),
453 check_plain($n->title),
454 check_plain($node_types[$n->type]),
455 $pub,
456 ),
457 );
458 }
459 }
460
461 if (!$rows) {
462 $rows[] = array(array('data' => t('No paid adverts.'), 'colspan' => 4));
463 }
464
465 $output .= theme('table', $header, $rows);
466 $output .= theme('pager', NULL, $nodes_per_page, 0);
467
468 return $output;
469 }
470
471 /**
472 * Implementation of nodeapi hook
473 * Prevent paid nodes having teasers that might be displayed for free!
474 */
475 function lm_paypal_paid_adverts_nodeapi(&$node, $op, $a3, $a4) {
476 _lm_paypal_paid_adverts_ini();
477 global $_lm_paypal_drupal_major;
478 global $user;
479
480 $title_len = 10;
481
482 if (strlen($node->title) > $title_len) {
483 $short_title = substr($node->title, 0, $title_len) . '...';
484 }
485 else {
486 $short_title = $node->title;
487 }
488
489 if ($_lm_paypal_drupal_major > 4) {
490 $overwrite = array();
491 // Drupal 5
492 switch ($op) {
493 case 'load':
494 if ($user->uid == 1) {
495 return;
496 }
497 $has_sub = lm_paypal_paid_adverts_has_sub($node, $subid, $paidfor);
498 if (!$has_sub) {
499 return;
500 }
501 if ($node->uid != $user->uid && !$paidfor) {
502 // Make sure nothing useful is visible
503 $overwrite['title'] = $short_title;
504 $overwrite['teaser'] = $overwrite['body'] = t('<em>Not yet published.</em>');
505 return $overwrite;
506 }
507 return;
508 }
509 }
510 else {
511 // Drupal 4
512 switch ($op) {
513 case 'view':
514 if ($user->uid == 1) {
515 return;
516 }
517 $has_sub = lm_paypal_paid_adverts_has_sub($node, $subid, $paidfor);
518 if (!$has_sub) {
519 return;
520 }
521 if ($node->uid != $user->uid && !$paidfor) {
522 // Make sure noting useful is visible
523 $node->title = $short_title;
524 $node->teaser = $node->body = t('<em>Not yet published.</em>');
525 }
526 break;
527
528 case 'submit':
529 $has_sub = lm_paypal_paid_adverts_has_sub($node, $subid, $paidfor);
530 if ($has_sub && !$paidfor) {
531 // A teaser might show unpaid content
532 $node->teaser = '';
533 }
534 break;
535 }
536 }
537 }
538
539 /**
540 * Implementation of hook_cron().
541 function lm_paypal_paid_adverts_cron() {
542 _lm_paypal_paid_adverts_ini();
543 global $_lm_paypal_debug;
544 global $_lm_paypal_paid_adverts_days;
545
546 if ($_lm_paypal_debug) {
547 watchdog(LM_PAYPAL_PAID_ADVERTS, 'cron');
548 }
549 }
550 */
551
552 /**
553 * Implementation of hook_form_alter().
554 */
555 function lm_paypal_paid_adverts_form_alter($form_id, $form) {
556 if ($form_id == 'system_modules' && !$_POST) {
557 // Make sure that both lm_paypal and lm_paypal_subscriptions are defined
558 $me = 'lm_paypal_paid_adverts';
559 $dep = array('lm_paypal','lm_paypal_subscriptions');
560 lm_paypal_system_module_validate($form,$me,$dep);
561 }
562 }
563 ?>

  ViewVC Help
Powered by ViewVC 1.1.2