Projet

Général

Profil

Demande #2841 » enclosure.module

Romain H., 02/04/2018 14:21

 
1
<?php
2
// $Id: enclosure.module,v 1.0 2007/04/13 20:48:35 Exp $
3

    
4
/**
5
 * @file
6
 * Enables users to attach an enclosure tag to the RSS feed of a node.
7
 */
8

    
9
/**
10
 * Implementation of hook_help().
11
 */
12
function enclosure_help($section) {
13
  switch ($section) {
14
    case 'admin/help#enclosure':
15
      $output = '<p>'. t('The enclosure module allows users to specify an RSS enclosure tag for any node.  The tag will appear any time RSS is generated from a particular node.') .'</p>';
16
      $output .= '<p>'. t('Users with the add RSS enclosure permission can upload attachments. You can choose which post types can take RSS enclosure tags on the content types settings page.') .'</p>';
17
      $output .= '<p>'. t('For more information please contact Armando Leon, the author of this module, at <a href="@upload">armando(dot)leon(at)gmail(dot)com</a>.', array('@upload' => 'mailto:armando(dot)leon(at)gmail(dot)com')) .'</p>';
18
      return $output;
19
  }
20
}
21

    
22

    
23
/**
24
 * Implementation of hook_install().
25
 */
26
function enclosure_install() {
27
  switch ($GLOBALS['db_type']) {
28
    case 'mysql':
29
    case 'mysqli':
30
      db_query("CREATE TABLE {enclosure} (
31
                  nid int(10) unsigned NOT NULL default '0',
32
                  enc_url varchar(255) NOT NULL default '',
33
                  enc_length int(10) unsigned NOT NULL default '0',
34
                  enc_type varchar(30) NOT NULL default '',
35
                  PRIMARY KEY (nid)
36
                )");
37
      break;
38

    
39
    case 'pgsql':
40
      db_query("CREATE TABLE {enclosure} (
41
                  nid int NOT NULL default '0',
42
                  enc_url varchar(255) NOT NULL default '0',
43
                  enc_length int NOT NULL default '0',
44
                  enc_type varchar(30) NOT NULL default '',
45
                  PRIMARY KEY (nid)
46
                )");
47
      break;
48
  }
49
}
50

    
51
/**
52
 * Implementation of hook_uninstall().
53
 */
54
function enclosure_uninstall() {
55
  db_query('DROP TABLE {enclosure}');
56
}
57

    
58

    
59

    
60
/**
61
 * Implementation of hook_perm().
62
 */
63
function enclosure_perm() {
64
  return array('modify enclosure');
65
}
66

    
67

    
68
/**
69
 * Implementation of hook_form_alter().
70
 */
71
function enclosure_form_alter(&$form, &$form_state, $form_id) {
72
  if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) {
73
    $enc_url = $form['#node']->enc_url;
74
    $enc_length = $form['#node']->enc_length;
75
    $enc_type = $form['#node']->enc_type;
76
    
77
    $form['enclosure'] = array(
78
      '#type' => 'fieldset',
79
      '#title' => t('RSS Enclosure'),
80
      '#collapsible' => TRUE,
81
      '#collapsed' => empty($enc_url),
82
      '#access' => user_access('modify enclosure'),
83
      '#weight' => 35,
84
    );
85
    $form['enclosure']['enc_url'] = array(
86
      '#type' => 'textfield',
87
      '#default_value' => $enc_url,
88
      '#maxlength' => 250,
89
      '#collapsible' => TRUE,
90
      '#collapsed' => TRUE,
91
      '#description' => t('Optionally specify an RSS enclosure URL for this node.  This will only appear in an RSS feed.'),
92
    );
93
    $form['enclosure']['enc_length'] = array(
94
      '#type' => 'textfield',
95
      '#default_value' => $enc_length,
96
      '#maxlength' => 8,
97
      '#collapsible' => TRUE,
98
      '#collapsed' => TRUE,
99
      '#description' => t('Specify a file size.  When in doubt, enter "0".'),
100
    );
101
    $form['enclosure']['enc_type'] = array(
102
      '#type' => 'textfield',
103
      '#default_value' => $enc_type,
104
      '#maxlength' => 30,
105
      '#collapsible' => TRUE,
106
      '#collapsed' => TRUE,
107
      '#description' => t('Specify a file type.  When in doubt, enter "audio/mpeg".'),
108
    );
109
  }
110
}
111

    
112

    
113
/**
114
 * Implementation of hook_nodeapi().
115
 */
116
function enclosure_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
117
  if (user_access('modify enclosure')) {
118
    switch ($op) {
119
      case 'validate':
120
        $node->enc_url = trim($node->enc_url);
121
        $node->enc_length = trim($node->enc_length);
122
        $node->enc_type = trim($node->enc_type);
123
        if ($enc_url) {
124
          if (!is_numeric($node->enc_length) || ($node->enc_length < 0)) {
125
            form_set_error('enc_length', t('The enclosure length must be a number and greater than or equal to zero.'));
126
          }
127
          if (!$enc_type) {
128
            form_set_error('enc_type', t('The enclosure type must be set.'));
129
          }
130
        }
131
        break;
132
      
133
      case 'load':
134
        $data = db_fetch_object(db_query("SELECT * FROM {enclosure} WHERE nid = '%d'", $node->nid));
135
        if ($data) {
136
          $node->enc_url = $data->enc_url;
137
          $node->enc_length = $data->enc_length;
138
          $node->enc_type = $data->enc_type;
139
        }
140
        break;
141
      
142
      case 'insert':
143
        if ($node->enc_url) {
144
          enclosure_set($node->nid, $node->enc_url, $node->enc_length, $node->enc_type);
145
        }
146
        break;
147
    
148
      case 'update':
149
        if ($node->enc_url) {
150
          enclosure_set($node->nid, $node->enc_url, $node->enc_length, $node->enc_type);
151
        } else {
152
          enclosure_unset($node->nid);
153
        }
154
        break;
155

    
156
      case 'delete':
157
        enclosure_unset($node->nid);
158
        break;
159

    
160
      case 'rss item':
161
        if ($node->enc_url) {
162
            return array(
163
              array(
164
                'key' => 'enclosure',
165
                'attributes' => array(
166
                'url' => $node->enc_url,
167
                'length' => $node->enc_length,
168
                'type' => $node->enc_type
169
                )
170
              )
171
            );
172
         } else {
173
         return array();
174
         }
175
    }
176
  }
177
}
178

    
179
function enclosure_set($nid, $enc_url, $enc_length, $enc_type) {
180
  if (db_result(db_query("SELECT COUNT(nid) FROM {enclosure} WHERE nid = %d", $nid))) {
181
    db_query("UPDATE {enclosure} SET enc_url = '%s', enc_length = '%d', enc_type = '%s' WHERE nid = %d", $enc_url, $enc_length, $enc_type, $nid);
182
  } else {
183
    db_query("INSERT INTO {enclosure} (nid, enc_url, enc_length, enc_type) VALUES (%d, '%s', %d, '%s')", $nid, $enc_url, $enc_length, $enc_type);
184
  }
185
}
186

    
187
function enclosure_unset($nid) {
188
  if (db_result(db_query("SELECT COUNT(nid) FROM {enclosure} WHERE nid = %d", $nid))) {
189
    db_query("DELETE FROM {enclosure} WHERE nid = %d", $nid);
190
  }
191
}
192

    
(1-1/3)