4) { // Drupal 5 $c = '!'; // t() has changed and to use a link in it use '!' $admin = l('LM PayPal Admin', 'admin/lm_paypal/settings'); $roles = l(t('user management > roles'), 'admin/user/roles'); $access = l(t('user management > access control'), 'admin/user/access'); $blocks = l(t('site building > blocks'), 'admin/build/block'); $subs = l(t('LM PayPal Subscriptions'), 'admin/lm_paypal/subscriptions'); $adminpa = l('LM PayPal Paid Adverts', 'admin/lm_paypal/paid_adverts_settings'); } else { // Drupal 4 $c = '%'; $admin = l('LM PayPal Admin', 'admin/settings/lm_paypal'); $roles = l(t('access control > roles'), 'admin/access/roles'); $access = l(t('access control'), 'admin/access'); $blocks = l(t('administer blocks'), 'admin/block'); $subs = l(t('LM PayPal Subscriptions'), 'admin/lm_paypal_subscriptions'); $adminpa = l('LM PayPal Paid Adverts', 'admin/settings/lm_paypal_paid_adverts'); } $new_sub = l('LM PayPal New Subscriptions', 'admin/lm_paypal/subscriptions/new'); $help_pa = l(t('LM PayPal Paid Adverts Help'), 'admin/help/lm_paypal_paid_adverts'); // HIJACK! This will put at help message at the start of node/add/... pages // for types associated with PayPal if (strncmp($section, 'node/add/', 9) == 0) { $type_subs = $_lm_paypal_paid_adverts_type_subs; $type = substr($section, 9); $ts = $type_subs[$type]; // Does this node type have a PayPal subscription associated with it if (is_array($ts) && count($ts) > 0) { // It does so print some help $output .= '
' . t('Nodes of this type will not be published until paid for.
After Submitting this new node use the PayPal Publish tab to publish.
The status of paid items can be found under my account.') . '
'; return $output; } } switch ($section) { // admin/help/lm_paypal_paid_adverts case 'admin/help#lm_paypal_paid_adverts': $output = $_lm_paypal_welcome; $output .= '

' . 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.') . '

'; $output .= '

'. t('Special Notes') . ':'; $output .= '

'; $output .= '

'; $output .= '

' . t('Initial Configuration') . '

'; $output .= '

' . t('If you are new to this module you need to:'); $output .= '

'; $output .= '

'; $output .= '

' . t('With new and existing nodes of node types associated with Node Subscriptions the contents will be private to the author until paid for.') . '

'; $output .= '

' . 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.') . '

'; $output .= '

' . t('Users can view the status of all nodes they have created requiring a subscription under "my account".') . '

'; $output .= '
'; return $output; // This is the brief description of the module displayed on the modules page case 'admin/modules#description': return t('Provides PayPal paid advert pages (requires lm_paypal and lm_paypal_subscriptions).'); // This appears at the start of the module admin page before the options case 'admin/settings/lm_paypal_paid_adverts': // This appears at the start of the paid_adverts admin page before the options case 'admin/lm_paypal/paid_adverts': $output = $_lm_paypal_welcome; $output .= '

'. t("For detailed help please read ${c}help_pa", array("${c}help_pa" => $help_pa)) .'

'; return $output; } } /** * Implementation of hook_menu(). */ function lm_paypal_paid_adverts_menu($may_cache) { _lm_paypal_paid_adverts_ini(); global $user; global $_lm_paypal_paid_adverts_type_subs; global $_lm_paypal_drupal_major; $type_subs = $_lm_paypal_paid_adverts_type_subs; $items = array(); if ($may_cache) { if ($_lm_paypal_drupal_major > 4) { // New to Drupal 5 - hook_settings gone so settings is a normal page $items[] = array( 'path' => 'admin/lm_paypal/paid_adverts_settings', 'title' => t('LM PayPal Paid Advert Settings'), 'callback' => 'drupal_get_form', 'callback arguments' => array('lm_paypal_paid_adverts_settings_form'), 'access' => user_access('administer site configuration'), 'type' => MENU_NORMAL_ITEM, 'weight' => 2, // New to Drupal 5 - every path has a description 'description' => t('PayPal Paid Advert interface configuration.'), ); } $items[] = array( 'path' => 'admin/lm_paypal/paid_adverts', 'title' => t('LM PayPal Show Paid Adverts'), 'callback' => 'lm_paypal_paid_adverts_list_nodes', 'access' => user_access('administer lm_paypal'), 'weight' => -2, // New to Drupal 5 - every path has a description 'description' => t('Show all PayPal Paid Adverts.'), ); } else { // Borrowed from book.module // This appears as a tab on the edit page which is ideal as you shouldn't // pay for an adverts until after it is created. // This is called for every menu display so try and keep the costs down! if (arg(0) == 'node' && is_numeric(arg(1))) { $nid = arg(1); $node = node_load($nid); $has_sub = lm_paypal_paid_adverts_has_sub($node, $subid, $paidfor); if ($has_sub) { // Allow admin or the node owner to view the PayPal tab if ($user->uid == 1 || $node->uid == $user->uid) { if (!$paidfor) { $title = t('PayPal Publish'); } else { $title = t('Published'); } $items[] = array( 'path' => 'node/'. $nid .'/PayPal', 'title' => $title, 'callback' => 'lm_paypal_paid_adverts_payment', 'callback arguments' => array($nid), 'type' => MENU_LOCAL_TASK, 'weight' => 4, ); } } } } return $items; } // Ugly magic to hide lm_paypal_settings from Drupal 5.0 as it spots its // existance and refuses to access the real page. if (strncmp(VERSION, '4', 1) == 0) { /** * Implementation of hook_settings() * Note: hook_settings not used in Drupal 5. */ function lm_paypal_paid_adverts_settings() { return lm_paypal_paid_adverts_settings_form(); } } /** * Implementation of hook_settings() */ function lm_paypal_paid_adverts_settings_form() { _lm_paypal_paid_adverts_ini(); global $_lm_paypal_paid_adverts_type_subs; global $_lm_paypal_drupal_major; if (!user_access('administer lm_paypal')) { drupal_access_denied(); return; } $form['#base'] = 'lm_paypal_paid_adverts_settings_form'; $form ['header'] = array( '#value' => '

' . t('Associate node subscriptions with node types.') . '

', ); $form ['note'] = array( '#value' => '' . 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.') . '', ); // Note: Drupal 5 pass 'names' to get a type,name array (Drupal 4 ignores it) $node_types = node_get_types('names'); $node_subs = lm_paypal_subscription_node_subs(); if (count($node_subs) <= 0) { $form ['none'] = array( '#value' => t('No node subscriptions defined.'), ); } else { $type_subs = $_lm_paypal_paid_adverts_type_subs; foreach ($node_types as $node_type => $node_name) { // Sadly when these form items are passed to submit they are no // longer nested. So they are flattened and mixed with the other items. // By sticking 'NT' at the start I can tell them apart. $form ['lm_paypal_paid_adverts_node_subs']['NT'.$node_type] = array( '#type' => 'checkboxes', '#title' => $node_name, '#options' => $node_subs, '#default_value' => $type_subs[$node_type], '#description' => t('Subscriptions, if any, available to publish nodes of this type.'), ); } } if ($_lm_paypal_drupal_major > 4) { // Borrowed from system_settings_form() $form['buttons']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration') ); $form['buttons']['reset'] = array('#type' => 'submit', '#value' => t('Reset to defaults') ); } return $form; } /** * Saves the lm_paypal_paid_advert settings. * * @param $form_id * The form_id that caused this _submit page to be called. * @param $values * An array containing the name,value pairs from the form */ function lm_paypal_paid_adverts_settings_form_submit($form_id, $values) { _lm_paypal_paid_adverts_ini(); global $_lm_paypal_paid_adverts_type_subs; // Find all the type sub's by looking for any key that matches NTtype foreach ($values as $key => $val) { if (strncmp($key,"NT",2) != 0) { // Not an NTtype skip it continue; } $type = substr($key,2); // Make sure it is an array and then if (is_array($val) && count($val) > 0) { // Copy over any set entries $ts = array(); foreach ($val as $k => $v) { if ($v != 0) { $ts[$k] = $v; } } // Skip empties if (count($ts) > 0) { $type_subs[$type] = $ts; } } } $_lm_paypal_paid_adverts_type_subs = $type_subs; variable_set('lm_paypal_paid_adverts_type_subs', serialize($type_subs)); } /** * Pay for an unpublished advert * * @param $nid * Node whose details should be displayed */ function lm_paypal_paid_adverts_payment($nid) { _lm_paypal_paid_adverts_ini(); global $_lm_paypal_paid_adverts_type_subs; global $_lm_paypal_drupal_major; $type_subs = $_lm_paypal_paid_adverts_type_subs; $paidfor = lm_paypal_node_subscribed($nid, $subid); if ($paidfor) { $sub = lm_paypal_subscribe($subid, 23); $c = ($_lm_paypal_drupal_major > 4 ? '!' : '%'); return t("This article has been paid for by the subscription: ${c}sub", array("${c}sub" => $sub)); } $output .= '

' . t('Not yet paid for. To make this article viewable by others you need to pay with PayPal. Please take out ONE of the subscriptions below.') . '

'; $node = node_load($nid); $ts = $type_subs[$node->type]; if (is_array($ts) && count($ts) > 0) { foreach ($ts as $type => $sub) { $output .= lm_paypal_subscribe($sub, 15, '', $nid); } } else { watchdog( LM_PAYPAL_PAID_ADVERTS, t('Attemping to pay for node %nid but no subscriptions available', array('%nid' => $nid)), WATCHDOG_WARNING); } return $output; } /** * Implementation of hook_user(). */ function lm_paypal_paid_adverts_user($op, &$edit, &$account, $category = NULL) { _lm_paypal_paid_adverts_ini(); global $_lm_paypal_debug; // In the "my account" view area show all paid_adverts if ($op == 'view' && (user_access('administer lm_paypal') || $user->uid == $account->uid)) { $output = lm_paypal_paid_adverts_list_nodes($account->uid,10); $items [] = array( 'title' => '', 'value' => $output); $ret_ads = array(t('Paypal Paid Adverts') => $items); return $ret_ads; } } /** * Does the node require a subscription * * @param $node * The node to check * @param $subid * If the node is subscription based and subscribed this is the subid * @param $paidfor * If the node is subscription based this is TRUE if the node has been paid for * @return * TRUE if the node does require a subscription */ function lm_paypal_paid_adverts_has_sub($node, &$subid, &$paidfor) { _lm_paypal_paid_adverts_ini(); global $_lm_paypal_paid_adverts_type_subs; $type_subs = $_lm_paypal_paid_adverts_type_subs; $ts = $type_subs[$node->type]; // Does this node type have a PayPal subscription associated with it if (!is_array($ts) || count($ts) <= 0) { return FALSE; } $paidfor = lm_paypal_node_subscribed($node->nid, $subid); return TRUE; } function lm_paypal_paid_adverts_list_nodes($uid = 0, $nodes_per_page = 50) { _lm_paypal_paid_adverts_ini(); global $_lm_paypal_paid_adverts_type_subs; $type_subs = $_lm_paypal_paid_adverts_type_subs; if (!is_numeric($uid) || intval($uid) != $uid) { $uid = 0; } $header = array( array('data' => t('User'), 'field' => 'u.name'), array('data' => t('Node'), 'field' => 'n.nid', 'sort' => 'desc'), array('data' => t('Title'), 'field' => 'n.title'), array('data' => t('Type'), 'field' => 'n.type'), array('data' => t('Published')), ); // Create a list of known paid node types $ts = array(); foreach ($type_subs as $k => $v) { $ts[$k] = 1; // This will loose duplicates } // Turn it into a string to be used in an "IN" clause $tstr = '('; foreach ($ts as $t => $v) { if ($nTs > 0) { $tstr .= ','; } $tstr .= "'" . $t . "'"; $nTs ++; } $tstr .= ')'; if ($nTs > 0) { if ($uid == 0) { $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"); } else { $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"); } $tablesort = tablesort_sql($header); $result = pager_query($sql . $tablesort, $nodes_per_page, 0, null, $uid); // Note: Drupal 5 pass 'names' to get a type,name array (Drupal 4 ignores it) $node_types = node_get_types('names'); while ($n = db_fetch_object($result)) { $has_sub = lm_paypal_paid_adverts_has_sub($n, $subid, $paidfor); if ($paidfor) { $pub = t('published'); } else { $pub = l(t('not published'),"node/$n->nid/PayPal"); } $rows[] = array('data' => array( $n->name, l(check_plain($n->nid), "node/$n->nid"), check_plain($n->title), check_plain($node_types[$n->type]), $pub, ), ); } } if (!$rows) { $rows[] = array(array('data' => t('No paid adverts.'), 'colspan' => 4)); } $output .= theme('table', $header, $rows); $output .= theme('pager', NULL, $nodes_per_page, 0); return $output; } /** * Implementation of nodeapi hook * Prevent paid nodes having teasers that might be displayed for free! */ function lm_paypal_paid_adverts_nodeapi(&$node, $op, $a3, $a4) { _lm_paypal_paid_adverts_ini(); global $_lm_paypal_drupal_major; global $user; $title_len = 10; if (strlen($node->title) > $title_len) { $short_title = substr($node->title, 0, $title_len) . '...'; } else { $short_title = $node->title; } if ($_lm_paypal_drupal_major > 4) { $overwrite = array(); // Drupal 5 switch ($op) { case 'load': if ($user->uid == 1) { return; } $has_sub = lm_paypal_paid_adverts_has_sub($node, $subid, $paidfor); if (!$has_sub) { return; } if ($node->uid != $user->uid && !$paidfor) { // Make sure nothing useful is visible $overwrite['title'] = $short_title; $overwrite['teaser'] = $overwrite['body'] = t('Not yet published.'); return $overwrite; } return; } } else { // Drupal 4 switch ($op) { case 'view': if ($user->uid == 1) { return; } $has_sub = lm_paypal_paid_adverts_has_sub($node, $subid, $paidfor); if (!$has_sub) { return; } if ($node->uid != $user->uid && !$paidfor) { // Make sure noting useful is visible $node->title = $short_title; $node->teaser = $node->body = t('Not yet published.'); } break; case 'submit': $has_sub = lm_paypal_paid_adverts_has_sub($node, $subid, $paidfor); if ($has_sub && !$paidfor) { // A teaser might show unpaid content $node->teaser = ''; } break; } } } /** * Implementation of hook_cron(). function lm_paypal_paid_adverts_cron() { _lm_paypal_paid_adverts_ini(); global $_lm_paypal_debug; global $_lm_paypal_paid_adverts_days; if ($_lm_paypal_debug) { watchdog(LM_PAYPAL_PAID_ADVERTS, 'cron'); } } */ /** * Implementation of hook_form_alter(). */ function lm_paypal_paid_adverts_form_alter($form_id, $form) { if ($form_id == 'system_modules' && !$_POST) { // Make sure that both lm_paypal and lm_paypal_subscriptions are defined $me = 'lm_paypal_paid_adverts'; $dep = array('lm_paypal','lm_paypal_subscriptions'); lm_paypal_system_module_validate($form,$me,$dep); } } ?>