/[drupal]/contributions/modules/paypernode/paypernode.module
ViewVC logotype

Contents of /contributions/modules/paypernode/paypernode.module

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


Revision 1.1 - (show annotations) (download) (as text)
Sun Dec 10 19:04:20 2006 UTC (2 years, 11 months ago) by jareyero
Branch: MAIN
Branch point for: DRUPAL-4-7
File MIME type: text/x-php
New pay-per-node module with eCommerce integration.
1 <?php
2 // $Id: paypernode.module,v 1.21.2.1 2006/06/27 14:13:45 jose Exp $
3
4 /********************************************************************
5 * paypernode.module, Drupal 4.7 Module
6 *
7 * This module provides an eCommerce product linked to node types
8 * and the neccessary bindings and permission overrides to publish nodes based on the adquired products
9 *
10 * Developed by Jose A. Reyero, http://www.reyero.net
11 * for Exprimiendo Ideas S.L., http://www.exprimiendo.net
12 *
13 ********************************************************************/
14
15 /**
16 * Implementation of hook_help().
17 */
18 function paypernode_help($section = 'admin/help#paypernode') {
19 switch ($section) {
20 // Product functionality
21 case 'admin/modules#description':
22 return t('Pay-per-node system. Allows creation of a limited number of nodes. Provides e-commerce integration.');
23 case 'node/add/product#paypernode':
24 return t('A pay-per-node product allows the user to create a certain number of a given node type.');
25 }
26 // Node creation. Only when the user has no direct permission to create that type
27 if(preg_match("|^node/add#(\w+)|", $section, $matches)){
28 $type = $matches[1];
29 if(!node_access('create', $type) && $number = paypernode_user_can_create($type)) {
30 return t('You can create %number items of this type using the pay-per-node system.', array('%number' => $number));
31 }
32 } else {
33 // drupal_set_message("DEBUG: paypernode_help no match, section=$section");
34 }
35 }
36
37 /**
38 * Implementation of hook_menu().
39 */
40 function paypernode_menu($may_cache) {
41 $items = array();
42
43 if ($may_cache) {
44 $items[] = array('path' => "paypernode/add",
45 'type' => MENU_CALLBACK,
46 'callback' => 'paypernode_node_addd'
47 );
48 $items[] = array('path' => "admin/paypernode",
49 'title' => t('pay-per-node'),
50 'callback' => 'paypernode_admin',
51 'access' => user_access('administer pay-per-node system')
52 );
53 $items[] = array('path' => "admin/paypernode/view",
54 'title' => t('overview'),
55 'type' => MENU_DEFAULT_LOCAL_TASK,
56 'access' => user_access('administer pay-per-node system'),
57 'weight' => 0
58 );
59 $items[] = array('path' => "admin/paypernode/bulk",
60 'title' => t('bulk operations'),
61 'type' => MENU_LOCAL_TASK,
62 'access' => user_access('administer pay-per-node system'),
63 'weight' => 1
64 );
65
66
67 } else {
68 global $user;
69 // User balance. Only own balance and for administrators
70 if (arg(0) == 'user' && is_numeric(arg(1))) {
71 $items[] = array('path' => "user/". arg(1) ."/paypernode",
72 'title' => t('pay-per-node'),
73 'callback' => 'paypernode_user_page',
74 'type' => MENU_LOCAL_TASK,
75 'weight' => 2,
76 'access' => ($user->uid && $user->uid == arg(1)) || user_access('administer pay-per-node system'),
77 );
78 }
79 // Create 'node add' items for the pay-per-node system
80 // Hack visibility and callback for menu item or add new items, overriding existing node/add
81 global $_menu;
82 $nodetypes = node_get_types();
83 $allowed = paypernode_user_can_create();
84
85 foreach($allowed as $type => $credit){
86 if($credit && array_key_exists($type, $nodetypes) && !node_access('create', $type)){
87 $path = "node/add/$type";
88 $item = array('path' => "node/add/$type",
89 'title' => $nodetypes[$type]."($credit)",
90 'callback' => 'paypernode_node_add',
91 'access' => TRUE,
92 );
93 $nodeitems[] = paypernode_menu_override($item);
94 }
95 }
96 // Now override callback for node/add
97 if(isset($nodeitems)) {
98 $item = array('path' => "node/add", 'callback' => 'paypernode_node_add');
99 paypernode_menu_override($item, TRUE);
100 }
101 }
102
103 return $items;
104 }
105
106 /**
107 * Override existing menu items
108 *
109 * Only menu items of type MENU_CREATED_BY_ADMIN may be overridden using regular menu callbacks
110 * - but even for these ones, visibility is not fully updated.
111 * Refresh visibility for all these new menu items
112 * - Some parents may be 'visible if has children' so check the tree
113 */
114 function paypernode_menu_override($item, $check_visibility = FALSE){
115 global $_menu;
116 // Override callbacks
117 if (isset($item['callback'])) {
118 $_menu['callbacks'][$item['path']] = array('callback' => $item['callback']);
119 if (isset($item['callback arguments'])) {
120 $_menu['callbacks'][$item['path']]['callback arguments'] = $item['callback arguments'];
121 }
122 }
123 unset($item['callback']);
124 unset($item['callback arguments']);
125
126 if (isset($_menu['path index'][$item['path']]) && $mid = $_menu['path index'][$item['path']]) {
127 $item = array_merge($_menu['items'][$mid], $item);
128 $_menu['items'][$mid] = $item;
129
130 // Update visibility if the item should be displayed and currently is not
131 if ($check_visibility) { // && ($item['type'] & MENU_VISIBLE_IN_TREE) && _menu_item_is_accessible($mid) && !isset($_menu['visible'][$mid])) {
132 // DEBUG drupal_set_message("Menu system override, visible in tree ".$item['path']);
133 $pid = $item['pid'];
134 while ($pid && !isset($_menu['visible'][$pid])) {
135 $pid = $_menu['items'][$pid]['pid'];
136 }
137 // Rebuild visibility from latest visible ancestor
138 _menu_build_visible_tree($pid);
139 }
140 }
141 return $item;
142 }
143
144 /**
145 * function paypernode_node_add
146 * Menu callback for creating nodes, similar to node_add
147 */
148 function paypernode_node_add($type = NULL){
149 global $user;
150 global $paypernode;
151 $type = $type ? $type : arg(2);
152 $nodetypes = node_get_types();
153 // If a node type has been specified, validate its existence.
154 // Check for the type in node_get_types, the module may be disabled (?)
155 if (array_key_exists($type, $nodetypes) && node_access('create', $type)) {
156 // Go through normal node_add
157 return node_add($type);
158 } elseif (array_key_exists($type, $nodetypes) && paypernode_user_can_create($type) ) {
159 // Initialize settings:
160 $node = array('uid' => $user->uid, 'name' => $user->name, 'type' => $type);
161 // Mark this node to override form callback
162 $node['paypernode'] = TRUE;
163 $output = node_form($node);
164 drupal_set_title(t('Submit %name', array('%name' => node_get_name($node))));
165 }
166 else {
167 // If no (valid) node type has been provided, display a node type overview.
168 foreach (node_get_types() as $type => $name) {
169 if (module_invoke(node_get_base($type), 'access', 'create', $type) || paypernode_user_can_create($type)) {
170 $out = '<dt>'. l($name, "node/add/$type", array('title' => t('Add a new %s.', array('%s' => $name)))) .'</dt>';
171 $out .= '<dd>'. implode("\n", module_invoke_all('help', 'node/add#'. $type)) .'</dd>';
172 $item[$name] = $out;
173 }
174 }
175
176 if (isset($item)) {
177 uksort($item, 'strnatcasecmp');
178 $output = t('Choose the appropriate item from the list:') .'<dl>'. implode('', $item) .'</dl>';
179 }
180 else {
181 $output = t('You are not allowed to create content.');
182 }
183 }
184
185 return $output;
186 }
187
188 /**
189 * Implementation of hook_form_alter()
190 */
191 function paypernode_form_alter($form_id, &$form) {
192 //global $paypernode;
193 // Add options for node creation form
194 if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) {
195 $type = $form['type']['#value'];
196 //print "DEBUG: Create $type number $number";
197 if(isset($form['#node']->paypernode) && $number = paypernode_user_can_create($type)) {
198 //print "DEBUG: Create $type number $number";
199 $form['paypernode'] = array('#type' => 'fieldset', '#title' => t('Pay-per-node system'), '#weight' => -10, '#tree' => TRUE);
200 $form['paypernode']['text'] = array( '#value' => t('You can create %number nodes of type %type', array('%number' => $number, '%type' => node_get_name($type))));
201 //$form['paypernode']['create'] = array( '#type' => 'value', '#value' => 1);
202
203 //var_dump($form['#submit']);
204 // Unset previous callback for node_form_submit. We don't want to go through the standard submision process
205 if(isset($form['#submit']['node_form_submit'])) {
206 unset($form['#submit']['node_form_submit']);
207 }
208 // Add this module's callback. This is a re-worked submission process.
209 $form['#submit'] = array('paypernode_node_form_submit' => array()) + (array)$form['#submit'];
210 }
211 }
212 }
213 /**
214 * Form API callback for node creation
215 * We should get here only with pay-per-node node edited forms
216 * This function follows closely node_form_submit, but changing the access control
217 */
218 function paypernode_node_form_submit($form_id, $edit){
219 //drupal_set_message("DEBUG: paypernode_node_form_submit form_id=$form_id");
220 global $user;
221
222 // Fix up the node when required:
223 $node = node_submit($edit);
224 // Mark as pay-per-node node
225 $node->paypernode = TRUE;
226
227 // Prepare the node's body:
228 // TO-DO: Bypass this logic. We only get here for pay-per-node node creation
229 if ($node->nid) {
230 // Check whether the current user has the proper access rights to
231 // perform this operation:
232 if (node_access('update', $node)) {
233 node_save($node);
234 watchdog('content', t('%type: updated %title.', array('%type' => theme('placeholder', t($node->type)), '%title' => theme('placeholder', $node->title))), WATCHDOG_NOTICE, l(t('view'), 'node/'. $node->nid));
235 drupal_set_message(t('The %post was updated.', array ('%post' => node_get_name($node))));
236 }
237 }
238 else {
239 // Check whether the current user has the proper access rights to
240 // perform this operation:
241 if (paypernode_user_can_create($node->type)) {
242 node_save($node);
243 watchdog('content', t('%type: added %title.', array('%type' => theme('placeholder', t($node->type)), '%title' => theme('placeholder', $node->title))), WATCHDOG_NOTICE, l(t('view'), "node/$node->nid"));
244 drupal_set_message(t('Your %post was created.', array ('%post' => node_get_name($node))));
245 }
246 }
247 if ($node->nid) {
248 if (node_access('view', $node)) {
249 //drupal_set_message("DEBUG: node created $node->nid access OK");
250 return 'node/'. $node->nid;
251 }
252 else {
253 //drupal_set_message("DEBUG: node created $node->nid NO access");
254 return '';
255 }
256 }
257 // it is very unlikely we get here
258 return FALSE;
259 }
260
261 /**
262 * Implementation of hook_nodeapi()
263 */
264 function paypernode_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
265 // DEBUG:
266 /*
267 if($op == 'insert') {
268 print $op;
269 var_dump($node);
270 }
271 */
272 if($op == 'insert' && isset($node->paypernode)){
273 // Decrease user balance for this node type
274 // Get uid from node
275 paypernode_user_update($node->uid, $node->type, 0, 1);
276 $typename = node_get_name($node);
277 $left = paypernode_user_can_create($node->type, TRUE); // Last argument to refresh cache
278 // Log event and display additional user message.
279 watchdog('paypernode', t('Pay-per-node %typename node created. %number nodes left.', array('%number' => $left, '%typename' => $typename)));
280 drupal_set_message(t("The node has been charged into the pay-per-node system. You have %number nodes of type %typename left.", array('%number' => $left, '%typename' => $typename)));
281 }
282 }
283 /**
284 * User balance
285 */
286 function paypernode_user_page() {
287 if(user_access('administer pay-per-node system')){
288 return paypernode_user_admin(arg(1));
289 } else {
290 return paypernode_user_balance($_GLOBALS['user']->uid);
291 }
292
293 }
294 /**
295 * Display user balance, not editable
296 */
297 function paypernode_user_balance($uid){
298 drupal_set_title(t('Your pay per node balance'));
299 $balance = paypernode_user_get_balance($uid);
300 if(count($balance)) {
301 $nodetypes = node_get_types();
302 $header = array(t('Type'), t('Purchased'), t('Created'), t('Available'), '');
303 foreach($balance as $type => $data) {
304 $rows[] = array(
305 $nodetypes[$type],
306 $data->allowed,
307 $data->created,
308 $left = $data->allowed - $data->created,
309 $left ? l(t('create'), "node/add/$type") : '--'
310 );
311 }
312 $output = theme('box', t('Pay-per-node balance'), theme('table', $header, $rows));
313 } else {
314 $output = t('You have not purchased any content type');
315 }
316 return $output;
317 }
318
319 /**
320 * Admin interface for single user
321 */
322 function paypernode_user_admin($uid){
323 $account = user_load(array('uid' => $uid));
324 drupal_set_title(t('Pay-per-node balance for %user_name', array('%user_name' => $account->name)));
325 $balance = paypernode_user_get_balance($uid);
326 $nodetypes = node_get_types();
327
328 // Build table and form together
329 $thead = '<table>';
330 $header = array(t('Content type'), t('Purchased'), t('Created'), t('Available'), t('Last updated'));
331 $output .= ' <thead><tr>';
332 foreach ($header as $cell) {
333 $thead .= _theme_table_cell($cell, 1);
334 }
335 $thead .= " </tr></thead>\n";
336 $thead .= "<tbody>\n";
337
338 $form['uid'] = array('#type' => 'value', '#value' => $uid);
339 $form['table_head'] = array('#value' => $thead);
340 $trclass = 'odd'; // row type
341 foreach(node_get_types() as $type => $name){
342 $form[$type] = array(
343 '#value' => '', '#tree' => TRUE,
344 '#prefix' => "<tr class=\"$trclass\">", '#suffix' => '</tr>'
345 );
346 $allowed = isset($balance[$type]) ? $balance[$type]->allowed : 0;
347 $created = isset($balance[$type]) ? $balance[$type]->created : 0;
348
349 $form[$type]['name'] = array(
350 '#value' => $name,
351 '#prefix' => '<td>', '#suffix' => '</td>'
352 );
353 $form[$type]['allowed'] = array(
354 '#type' => 'textfield',
355 '#size' => 4,
356 '#default_value' => $allowed,
357 '#prefix' => '<td>', '#suffix' => '</td>'
358 );
359 $form[$type]['created'] = array(
360 '#type' => 'textfield',
361 '#size' => 4,
362 '#default_value' => $created,
363 '#prefix' => '<td>', '#suffix' => '</td>'
364 );
365 $form[$type]['available'] = array(
366 '#value' => $allowed - $created,
367 '#prefix' => '<td>', '#suffix' => '</td>'
368 );
369
370 $form[$type]['changed'] = array(
371 '#value' => isset($balance[$type]) ? format_date($balance[$type]->changed) : '--',
372 '#prefix' => '<td>', '#suffix' => '</td>'
373 );
374
375 $trclass = ($trclass == 'even') ? 'odd' : 'even';
376 //$rows[] = array($name, form_render($from[$type]['allowed']), form_render($from[$type]['created']), $allowed - $created);
377 }
378 $form['table_end'] = array('#value' => "</tbody>\n</table>\n");
379 $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
380
381 return drupal_get_form('paypernode_user_admin', $form);
382
383
384 return $output;
385 }
386
387 function paypernode_user_admin_submit($form_id, $form_values){
388 $uid = $form_values['uid'];
389 foreach(node_get_types() as $type => $name){
390 if(isset($form_values[$type]) && is_numeric($allowed = $form_values[$type]['allowed']) && is_numeric($created = $form_values[$type]['created'])) {
391 paypernode_user_update($uid, $type, $allowed, $created, 'set');
392 }
393 }
394 drupal_set_message(t('User account has been updated'));
395
396 }
397 /**
398 * Implementation of hook_perm().
399 */
400 function paypernode_perm() {
401 return array(
402 // Product permissions
403 'create pay-per-node products', 'edit own pay-per-node products',
404 // Administrative permissions
405 'administer pay-per-node system'
406 );
407 }
408
409 /**
410 * Implementation of hook_access().
411 */
412 function paypernode_access($op, $node) {
413 global $user;
414 if ($op == 'create') {
415 return user_access('create pay-per-node products');
416 }
417 if ($op == 'update' || $op == 'delete') {
418 if (user_access('edit own pay-per-node products') && ($user->uid == $node->uid)) {
419 return TRUE;
420 }
421 }
422 }
423
424 /**
425 * Implementation of hook_block().
426 *
427 * Displays the pay-per-node account balance for user.
428 */
429 function paypernode_block($op = 'list', $delta = 0) {
430 global $user;
431 if ($op == 'list') {
432 $block[0]['info'] = t('Pay-per-node balance');
433 return $block;
434 }
435 else if ($op == 'view' && $user->uid) {
436 $balance = paypernode_user_get_balance($user->uid);
437 if(count($balance)) {
438 $nodetypes = node_get_types();
439 $header = array(t('Type'), t('Allowed'));
440 foreach($balance as $type => $data) {
441 $rows[] = array($nodetypes[$type], $data->allowed - $data->created);
442 }
443 $block['content'] = theme('table', $header, $rows);
444 $block['content'] .= '<div class="more-link">'. l(t('my account'), "user/$user->uid/paypernode", array('title' => t('Full view of my pay-per-node account.'))) .'</div>';
445 $block['subject'] = t('Pay-per-node account');
446 return $block;
447 }
448 }
449 }
450 /**
451 * e-Commerce Product implementation
452 */
453 function paypernode_productapi(&$node, $op, $a3 = null, $a4 = null) {
454 // DEBUG
455 //drupal_set_message("DEBUG: paypernode_productapi op=$op");
456 //print "DEBUG: paypernode_productapi op=$op";
457
458 switch ($op) {
459
460 case 'fields':
461 return array('node_type' => $node->node_type);
462
463 case 'validate':
464
465 break;
466
467 case 'wizard_select':
468 return array('paypernode' => t('pay-per-node product'));
469
470 case 'attributes':
471 return array('in_stock');
472
473 case 'cart add item':
474 break;
475
476 case 'on payment completion':
477 if($node->node_type && $node->qty && $node->node_qtty) {
478 global $user;
479 $number = $node->qty * $node->node_qtty;
480 paypernode_user_update($user->uid, $node->node_type, $number);
481 drupal_set_message(t('You can create now %number nodes of type %node_type', array('%number' => $node->qty, '%node_type' => node_get_name($node->node_type))));
482 } else {
483 // Error condition
484 drupal_set_message(t("No new node types added. Please contact site administrator"));
485 }
486
487 break;
488
489 case 'form':
490 $form['node_qtty'] = array(
491 '#type' => 'textfield',
492 '#title' => t('Quantity'),
493 '#default_value' => isset($node->node_qtty) ? $node->node_qtty : 1,
494 '#description' => t("How many nodes the user will be allowed to create."),
495 );
496 $form['node_type'] = array(
497 '#type' => 'radios',
498 '#title' => t('Node type'),
499 '#default_value' => $node->node_type,
500 '#options' => node_get_types(),
501 '#description' => t("Node type the user will be allowed to create."),
502 );
503
504 return $form;
505
506 case 'load':
507 return db_fetch_object(db_query('SELECT * FROM {ec_product_paypernode} WHERE vid = %d', $node->vid));
508
509 case 'insert':
510 return db_query("INSERT INTO {ec_product_paypernode} (nid, vid, node_type, node_qtty) VALUES ('%d', '%d', '%s', '%d')", $node->nid, $node->vid, $node->node_type, $node->node_qtty);
511
512 case 'update':
513 // DEBUG
514 // var_dump($node);
515 // drupal_set_message("Update vid=$node->vid nid=$node->nid node_type=$node->node_type");
516 return db_query("UPDATE {ec_product_paypernode} SET node_type = '%s', node_qtty ='%d' WHERE vid = '%d'", $node->node_type, $node->node_qtty, $node->vid);
517
518 case 'delete':
519 // Note: cannot delete single revisions
520 //drupal_set_message("Delete vid=$node->vid nid=$node->nid node_type=$node->node_type");
521 return db_query('DELETE FROM {ec_product_paypernode} WHERE nid = %d', $node->nid);
522 }
523 }
524
525 /**
526 * Pay-per-node administrative interface
527 */
528 function paypernode_admin($op = NULL){
529 $output = '';
530 switch($op){
531 case 'bulk':
532 $output .= paypernode_admin_bulk();
533 break;
534 default:
535 $output .= paypernode_admin_overview();
536 }
537 return $output;
538 }
539
540 function paypernode_admin_overview() {
541 $output = '';
542
543 $query = "SELECT u.uid, u.name, SUM(p.allowed) AS allowed, SUM(p.created) AS created FROM {users} u ".
544 "INNER JOIN {ec_paypernode_user} p ON u.uid = p.uid GROUP BY u.uid, u.name ORDER BY u.uid";
545 $result = pager_query($query, 50, 0, NULL);
546
547 // Present results
548 if(db_num_rows($result)) {
549 $header = array(t('user'), t('total'), t('created'),t('left'), t('administer'));
550 while($user = db_fetch_object($result)) {
551 $rows[] = array(
552 theme('username', $user),
553 $user->allowed,
554 $user->created,
555 $user->allowed - $user->created,
556 l(t('edit'),"/user/$user->uid/paypernode")
557 );
558 }
559 $output .= theme('table', $header, $rows);
560 $output .= theme('pager', array(), 50);
561 } else {
562 $output .= t("No pay-per-node data for users");
563 }
564 return $output;
565 }
566 /**
567 * Bulk account administration
568 * Allows to add a number of nodes for a given role
569 */
570 function paypernode_admin_bulk(){
571 // $edit = isset($_POST['edit'] :
572 $form['role'] = array(
573 '#type' => 'radios',
574 '#title' => t('Role'),
575 '#default_value' => '',
576 '#options' => user_roles(1),
577 '#description' => t("User's role to perform the operation."),
578
579 );
580 $form['node_type'] = array(
581 '#type' => 'radios',
582 '#title' => t('Node type'),
583 '#default_value' => '',
584 '#options' => node_get_types(),
585 '#description' => t("Node type to perform the operation."),
586 );
587 $form['amount'] = array(
588 '#type' => 'textfield',
589 '#size' => 4, '#maxlength' => 4,
590 '#title' => t('Number of nodes'),
591 '#default_value' => 0,
592 '#description' => t("Number of nodes to add. This can be negative."),
593 );
594 $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
595 return drupal_get_form('paypernode_admin_bulk', $form);
596 }
597
598 function paypernode_admin_bulk_submit($form_id, $form_values){
599 $rid = $form_values['role'];
600 $node_type = $form_values['node_type'];
601 $amount = $form_values['amount'];
602
603 // Manage exception for "authenticated user", rid = 2, that is not in users_roles table
604 if($rid == 2) {
605 $sql = "SELECT u.uid, p.allowed, p.created FROM {users} u LEFT JOIN {ec_paypernode_user} p ON u.uid = p.uid AND p.node_type = '%s' WHERE u.uid > 0";
606 $params = array($node_type);
607 } else {
608 $sql = "SELECT u.uid, p.allowed, p.created FROM {users} u INNER JOIN {users_roles} r ON u.uid = r.uid LEFT JOIN {ec_paypernode_user} p ON u.uid = p.uid AND p.node_type = '%s' WHERE r.rid = '%d'";
609 $params = array($node_type, $rid);
610 }
611
612 // Update user accounts
613 $result = db_query($sql, $params);
614
615 $user_count = 0;
616 $node_count = 0;
617 while($user = db_fetch_object($result)){
618 drupal_set_message("DEBUG: uid=$user->uid, allowed=$user->allowed, created=$user->created<br/>");
619
620 if(is_numeric($user->allowed)) {
621 $total = $user->allowed + $amount;
622 // Check before update for negative balance and allowed >= created
623 if($total >= 0 && $total >= $user->created){
624 paypernode_user_update($user->uid, $node_type, $amount);
625 $user_count++;
626 $node_count += $amount;
627 } elseif($user->created < $user->allowed) {
628 // Just set balance to null, allowed = created
629 paypernode_user_update($user->uid, $node_type, $user->created, $user->created, 'set');
630 $user_count++;
631 $node_count += $user->created - $user->allowed;
632 } // else nothing to do for this user
633 } elseif($amount > 0) {
634 // User has no previous balance of that type. Do not allow negative balance
635 paypernode_user_update($user->uid, $node_type, $amount, 0, 'set');
636 $user_count++;
637 $node_count += $amount;
638 }
639 }
640 drupal_set_message(t('Updated %user_count user accounts. Added %node_count nodes.', array('%user_count' => $user_count, '%node_count' => $node_count)));
641 }
642 /**
643 * Node system and helper functions
644 */
645
646 /**
647 * Get pay-per-node user balance
648 * Balance is cached per user
649 * @param $refresh to reload user balance from db
650 */
651 function paypernode_user_get_balance($uid = NULL, $refresh = FALSE){
652 static $balance = array();
653 // User check
654 global $user;
655 $uid = user_access('administer pay-per-node system') ? ($uid ? $uid : $user->uid) : $user->uid;
656 if($refresh) {
657 unset($balance[$uid]);
658 }
659 // Retrieve data
660 if(!isset($balance[$uid])){
661 $balance[$uid] = array();
662 $result = db_query("SELECT * FROM {ec_paypernode_user} WHERE uid='%d'", $uid);
663 while($item = db_fetch_object($result)) {
664 $balance[$uid][$item->node_type] = $item;
665 }
666 }
667
668 return $balance[$uid];
669 }
670
671 /**
672 * Get number of nodes user can create
673 */
674 function paypernode_user_can_create($type = NULL, $refresh = FALSE){
675 $balance = paypernode_user_get_balance(NULL, $refresh);
676 $cancreate = array();
677 foreach($balance as $item){
678 $cancreate[$item->node_type] = $item->allowed - $item->created;
679 }
680 if($type){
681 return array_key_exists($type, $cancreate) ? $cancreate[$type] : 0;
682 } else {
683 return $cancreate;
684 }
685 }
686
687 /**
688 * Database layer
689 * Add/set a number of nodes for an user;
690 */
691 function paypernode_user_update($uid, $type, $allowed = 0, $created =0, $op = 'add'){
692 // Enclose number in parentheses for query. This will support either possitive or negative values
693 switch($op){
694 case 'add':
695 db_query("UPDATE {ec_paypernode_user} SET allowed = allowed + (%d), created = created + (%d), changed = '%d' WHERE uid = %d AND node_type = '%s'", $allowed, $created, time(), $uid, $type);
696 break;
697 case 'set':
698 db_query("UPDATE {ec_paypernode_user} SET allowed = '%d', created = '%d', changed = '%d' WHERE uid = %d AND node_type = '%s'", $allowed, $created, time(), $uid, $type);
699 break;
700 }
701 // If row doesn't exist, create
702 if(!db_affected_rows()){
703 db_query("INSERT INTO {ec_paypernode_user} (uid, node_type, allowed, created, changed) VALUES('%d', '%s', '%d', '%d', '%d')", $uid, $type, $allowed, $created, time());
704 }
705
706 }
707 ?>

  ViewVC Help
Powered by ViewVC 1.1.2