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

Diff of /contributions/modules/premium/premium.module

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

revision 1.12, Sun Aug 3 03:22:51 2008 UTC revision 1.13, Sun Aug 3 04:05:25 2008 UTC
# Line 1  Line 1 
1  <?php /* $Id: premium.module,v 1.11 2008/07/30 03:16:23 vauxia Exp $ */  <?php /* $Id: premium.module,v 1.12 2008/08/03 03:22:51 vauxia Exp $ */
2  /**  /**
3   * @file Restrict access to the full body of premium content   * @file Restrict access to the full body of premium content
4   */   */
5    
6    /**
7     * Implementation of hook_menu()
8     */
9  function premium_menu($may_cache) {  function premium_menu($may_cache) {
10    $items = array();    $items = array();
11    if ($may_cache) {    if ($may_cache) {
# Line 36  function premium_cron() { Line 39  function premium_cron() {
39   */   */
40  function premium_nodeapi(&$node, $op, $teaser) {  function premium_nodeapi(&$node, $op, $teaser) {
41    $node->premium = _premium_node($node);    $node->premium = _premium_node($node);
42      $node->premium_access = _premium_access($node, $teaser);
43    
44    switch ($op) {    switch ($op) {
45        case 'load':
46          return array(
47            'premium' => $node->premium,
48            'premium_access' => $node->premium_access);
49    
50      case 'insert':      case 'insert':
51      case 'delete':      case 'delete':
# Line 45  function premium_nodeapi(&$node, $op, $t Line 53  function premium_nodeapi(&$node, $op, $t
53        _premium_set_premium($node, $node->premium);        _premium_set_premium($node, $node->premium);
54        return;        return;
55    
     case 'load':  
       $ts = time();  
       return array('premium' => (int) db_result(db_query(  
           "SELECT 1 FROM {premium}  WHERE nid = %d  
           AND ( start_ts = 0 and end_ts > %d)  
           OR ( start_ts < %d AND end_ts = 0)  
           OR ( start_ts = 0 AND end_ts = 0)", $node->nid, $ts, $ts)));  
   
56      case 'view':      case 'view':
57        $node->premium_access = true;        if(!$node->premium_access) {
58            $node->content['body']['#value'] = theme('premium_body', $node);
       global $user;  
       if (!$node->premium || user_access('access premium content')) {  
         return;                  // not premium content or user has privileges  
       }  
       if ($teaser) {  
         return;                  // not viewing the body  
59        }        }
60        foreach (module_implements('premium_access') as $name) {        return;
         $function = $name .'_premium_access';  
         if ($function($user, $node)) {  
           return;                // access granted explicitly  
         }  
       }  
   
       $node->premium_access = false;  
       $node->content['body']['#value'] = theme('premium_body', $node);  
61    }    }
62    return;    return;
63  }  }
64    
65  /**  /**
66   * implementation of hook_node_operations().   * Implementation of hook_node_operations().
67   */   */
68  function premium_node_operations() {  function premium_node_operations() {
69    $operations = array(    $operations = array(
# Line 96  function premium_node_operations() { Line 82  function premium_node_operations() {
82  }  }
83    
84  /**  /**
85     * Callback for hook_node_operations()
86     */
87    function _premium_node_operations_premium($nids, $premium = 0) {
88      foreach($nids as $nid) {
89        $node = node_load($nid);
90        _premium_set_premium($node, $premium);
91      }
92    }
93    
94    /**
95   * Implementation of hook_form_alter()   * Implementation of hook_form_alter()
96   *   *
97   * Add the Premium checkbox to the node editing options and default settings   * Add the Premium checkbox to the node editing options and default settings
# Line 202  function premium_settings() { Line 198  function premium_settings() {
198  }  }
199    
200  /**  /**
201   * Save premium-ness as set on admin/settings/premium to each type   * Save premium status as set on admin/settings/premium to each type
202   */   */
203  function premium_settings_save($form_id, $form_values) {  function premium_settings_save($form_id, $form_values) {
204    $count = $form_values['premium_time_count'];    $count = $form_values['premium_time_count'];
# Line 271  function _premium_node($node) { Line 267  function _premium_node($node) {
267      return in_array('premium', variable_get("node_options_{$node}", array()));      return in_array('premium', variable_get("node_options_{$node}", array()));
268    }    }
269    
270    // Already has a value    // Already has a value.
271    if (isset($node->premium)) return $node->premium;    if (isset($node->premium)) return $node->premium;
272    
273    // Use default settings for this node type    if ($node->nid) {
274        // Attempt to find the value from the premium table.
275        return (int) db_result(db_query("SELECT 1 FROM {premium}  WHERE nid = %d
276          AND ( start_ts = 0 and end_ts > %d)
277          OR ( start_ts < %d AND end_ts = 0)
278          OR ( start_ts = 0 AND end_ts = 0)", $node->nid, time(), time()));
279      }
280    
281      // Use default settings for this node type.
282    return in_array('premium', variable_get("node_options_{$node->type}", array()));    return in_array('premium', variable_get("node_options_{$node->type}", array()));
283  }  }
284    
285  function _premium_node_operations_premium($nids, $premium = 0) {  /**
286    foreach($nids as $nid) {   * Establish premium visibility settings for a node
287      $node = node_load($nid);   */
288      _premium_set_premium($node, $premium);  function _premium_access($node, $teaser) {
289      if (isset($node->premium_access)) return $node->premium_access;
290    
291      // Not viewing the body, or it's not premium, or user has privileges.
292      if ($teaser || !$node->premium || user_access('access premium content')) {
293        return TRUE;
294      }
295    
296      // Access is granted explicitly.
297      foreach (module_implements('premium_access') as $name) {
298        $function = $name .'_premium_access';
299        if (is_bool($access = $function($user, $node))) {
300          return $access;
301        }
302    }    }
 }  
303    
304      // Nobody said we could access the node.
305      return FALSE;
306    }
307    
308    /**
309     * Update the premium table with appropriate premium values for a node.
310     */
311  function _premium_set_premium($node, $premium = FALSE) {  function _premium_set_premium($node, $premium = FALSE) {
312    db_query('DELETE FROM {premium} WHERE nid = %d', $node->nid);    db_query('DELETE FROM {premium} WHERE nid = %d', $node->nid);
313    if ($premium) {    if ($premium) {

Legend:
Removed from v.1.12  
changed lines
  Added in v.1.13

  ViewVC Help
Powered by ViewVC 1.1.2