Projet

Général

Profil

Demande #2841 » enclosure.module

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

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

/**
* @file
* Enables users to attach an enclosure tag to the RSS feed of a node.
*/

/**
* Implementation of hook_help().
*/
function enclosure_help($section) {
switch ($section) {
case 'admin/help#enclosure':
$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>';
$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>';
$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>';
return $output;
}
}


/**
* Implementation of hook_install().
*/
function enclosure_install() {
switch ($GLOBALS['db_type']) {
case 'mysql':
case 'mysqli':
db_query("CREATE TABLE {enclosure} (
nid int(10) unsigned NOT NULL default '0',
enc_url varchar(255) NOT NULL default '',
enc_length int(10) unsigned NOT NULL default '0',
enc_type varchar(30) NOT NULL default '',
PRIMARY KEY (nid)
)");
break;

case 'pgsql':
db_query("CREATE TABLE {enclosure} (
nid int NOT NULL default '0',
enc_url varchar(255) NOT NULL default '0',
enc_length int NOT NULL default '0',
enc_type varchar(30) NOT NULL default '',
PRIMARY KEY (nid)
)");
break;
}
}

/**
* Implementation of hook_uninstall().
*/
function enclosure_uninstall() {
db_query('DROP TABLE {enclosure}');
}



/**
* Implementation of hook_perm().
*/
function enclosure_perm() {
return array('modify enclosure');
}


/**
* Implementation of hook_form_alter().
*/
function enclosure_form_alter(&$form, &$form_state, $form_id) {
if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) {
$enc_url = $form['#node']->enc_url;
$enc_length = $form['#node']->enc_length;
$enc_type = $form['#node']->enc_type;
$form['enclosure'] = array(
'#type' => 'fieldset',
'#title' => t('RSS Enclosure'),
'#collapsible' => TRUE,
'#collapsed' => empty($enc_url),
'#access' => user_access('modify enclosure'),
'#weight' => 35,
);
$form['enclosure']['enc_url'] = array(
'#type' => 'textfield',
'#default_value' => $enc_url,
'#maxlength' => 250,
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#description' => t('Optionally specify an RSS enclosure URL for this node. This will only appear in an RSS feed.'),
);
$form['enclosure']['enc_length'] = array(
'#type' => 'textfield',
'#default_value' => $enc_length,
'#maxlength' => 8,
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#description' => t('Specify a file size. When in doubt, enter "0".'),
);
$form['enclosure']['enc_type'] = array(
'#type' => 'textfield',
'#default_value' => $enc_type,
'#maxlength' => 30,
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#description' => t('Specify a file type. When in doubt, enter "audio/mpeg".'),
);
}
}


/**
* Implementation of hook_nodeapi().
*/
function enclosure_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
if (user_access('modify enclosure')) {
switch ($op) {
case 'validate':
$node->enc_url = trim($node->enc_url);
$node->enc_length = trim($node->enc_length);
$node->enc_type = trim($node->enc_type);
if ($enc_url) {
if (!is_numeric($node->enc_length) || ($node->enc_length < 0)) {
form_set_error('enc_length', t('The enclosure length must be a number and greater than or equal to zero.'));
}
if (!$enc_type) {
form_set_error('enc_type', t('The enclosure type must be set.'));
}
}
break;
case 'load':
$data = db_fetch_object(db_query("SELECT * FROM {enclosure} WHERE nid = '%d'", $node->nid));
if ($data) {
$node->enc_url = $data->enc_url;
$node->enc_length = $data->enc_length;
$node->enc_type = $data->enc_type;
}
break;
case 'insert':
if ($node->enc_url) {
enclosure_set($node->nid, $node->enc_url, $node->enc_length, $node->enc_type);
}
break;
case 'update':
if ($node->enc_url) {
enclosure_set($node->nid, $node->enc_url, $node->enc_length, $node->enc_type);
} else {
enclosure_unset($node->nid);
}
break;

case 'delete':
enclosure_unset($node->nid);
break;

case 'rss item':
if ($node->enc_url) {
return array(
array(
'key' => 'enclosure',
'attributes' => array(
'url' => $node->enc_url,
'length' => $node->enc_length,
'type' => $node->enc_type
)
)
);
} else {
return array();
}
}
}
}

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

function enclosure_unset($nid) {
if (db_result(db_query("SELECT COUNT(nid) FROM {enclosure} WHERE nid = %d", $nid))) {
db_query("DELETE FROM {enclosure} WHERE nid = %d", $nid);
}
}

(1-1/3)