/[drupal]/contributions/modules/audio/contrib/attach/audio_attach.module
ViewVC logotype

Diff of /contributions/modules/audio/contrib/attach/audio_attach.module

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

revision 1.8 by zirafa, Thu Jan 17 23:09:23 2008 UTC revision 1.9 by drewish, Sun May 25 17:21:27 2008 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: audio_attach.module,v 1.7 2007/08/03 02:41:13 drewish Exp $  // $Id: audio_attach.module,v 1.8 2008/01/17 23:09:23 zirafa Exp $
3    
4  /**  /**
5   * @file audio_attach.module   * @file audio_attach.module
# Line 10  define("AUDIO_ATTACH_PATH", drupal_get_p Line 10  define("AUDIO_ATTACH_PATH", drupal_get_p
10  /**  /**
11   * Implementation of hook_help().   * Implementation of hook_help().
12   */   */
13  function audio_attach_help($section) {  function audio_attach_help($section, $args) {
14    switch ($section) {    switch ($section) {
15      case 'admin/help#audio_attach':      case 'admin/help#audio_attach':
16        return t('<p>Allows audio files to be attached to any content type.  You can configure which content types by going to admin/content/types.</p>');        return t('<p>Allows audio files to be attached to any content type.  You can configure which content types by going to admin/content/types.</p>');
# Line 19  function audio_attach_help($section) { Line 19  function audio_attach_help($section) {
19  }  }
20    
21  /**  /**
22   *  Implementation of hook_menu   * Implementation of hook_menu
23   */   */
24  function audio_attach_menu($may_cache) {  function audio_attach_menu() {
25    $items = array();    $items = array();
26    
27    if ($may_cache) {    // For example: audio_attach/remove/314/1
28      // cached items    $items['audio_attach/%op/%node/%index'] = array(
29    }      'page callback' => '_audio_attach_action',
30    elseif (arg(0) == 'audio_attach' && is_numeric(arg(2))) {      'page arguments' => array(1, 2, 3),
31      $node = node_load(arg(2));      'access callback' => 'node_access',
32      $items[] = array(      'access arguments' => array('update', 2),
33        'path' => 'audio_attach',      'type' => MENU_CALLBACK,
34        'callback' => '_audio_attach_action',    );
35        'callback_arguments' => array(arg(1), arg(2), arg(3)),  
       'access' => node_access('update', $node),  
       'type' => MENU_CALLBACK,  
     );  
   }  
36    return $items;    return $items;
37  }  }
38    
39  /**  /**
40   *  Implementation of hook_perm   * Implementation of hook_perm
41   */   */
42  function audio_attach_perm() {  function audio_attach_perm() {
43    return array(    return array(
44      'attach any existing audio file'      'attach any existing audio file'
45      );    );
46  }  }
47    
48  /**  /**
49   * implementation of hook_form_alter()   * Implementation of hook_theme
50   */   */
51  function audio_attach_form_alter($form_id, &$form) {  function audio_attach_theme() {
52      return array(
53        'audio_attach_list' => array(
54          'arguments' => array('nids', 'teaser' => FALSE),
55        ),
56      );
57    }
58    
59    /**
60     * Implementation of hook_form_alter()
61     */
62    function audio_attach_form_alter(&$form, &$form_state, $form_id) {
63    switch ($form_id) {    switch ($form_id) {
64      // checkbox in the node's content type configuration page.      // checkbox in the node's content type configuration page.
65      case 'node_type_form':      case 'node_type_form':
66        if(isset($form['identity']['type'])) {        if (isset($form['identity']['type'])) {
67            $form['workflow']['audio_attach'] = array(            $form['workflow']['audio_attach'] = array(
68              '#type' => 'radios',              '#type' => 'radios',
69              '#title' => t('Attach audio files'),              '#title' => t('Attach audio files'),
# Line 68  function audio_attach_form_alter($form_i Line 74  function audio_attach_form_alter($form_i
74          }          }
75        break;        break;
76    
77        // FIXME: This is broken; I'm not sure what $form['type']['#value'] is
78        // expected to be, but it certainly doesn't always exist.
79      // if enabled adjust the form      // if enabled adjust the form
80      case $form['type']['#value'] . '_node_form':      //case $form['type']['#value'] .'_node_form':
81        if (variable_get('audio_attach_' . $form['type']['#value'], 0)) {      case 'FIXME_node_form':
82          if (variable_get('audio_attach_'. $form['type']['#value'], 0)) {
83          $node = $form['#node'];          $node = $form['#node'];
84          $form['#attributes'] = array("enctype" => "multipart/form-data");          $form['#attributes'] = array("enctype" => "multipart/form-data");
85          $form['audio_attach'] = array('#type' => 'fieldset', '#title' => t('Attached audio files'), '#collapsible' => TRUE);          $form['audio_attach'] = array('#type' => 'fieldset', '#title' => t('Attached audio files'), '#collapsible' => TRUE);
# Line 122  function audio_attach_form_alter($form_i Line 131  function audio_attach_form_alter($form_i
131  }  }
132    
133  /**  /**
134  * Implementation of hook_nodeapi().   * Implementation of hook_nodeapi().
135  */   */
136  function audio_attach_nodeapi(&$node, $op, $teaser, $page) {  function audio_attach_nodeapi(&$node, $op, $teaser, $page) {
137    global $user;    global $user;
138    // delete all references from other nodes if audio node is deleted 24/08/2006 sun    // delete all references from other nodes if audio node is deleted 24/08/2006 sun
139    if ($node->type == 'audio' && $op == 'delete'){    if ($node->type == 'audio' && $op == 'delete') {
140      db_query("DELETE FROM {audio_attach} WHERE aid = %d", $node->nid);      db_query("DELETE FROM {audio_attach} WHERE aid = %d", $node->nid);
141      return;      return;
142    }    }
143    if ($node->type == 'audio' || !variable_get("audio_attach_$node->type", 0)){    if ($node->type == 'audio' || !variable_get("audio_attach_$node->type", 0)) {
144      return;      return;
145    }    }
146    switch ($op) {    switch ($op) {
# Line 183  function audio_attach_nodeapi(&$node, $o Line 192  function audio_attach_nodeapi(&$node, $o
192  }  }
193    
194  /**  /**
195   *  Menu callback, perform an action on attachments.   * Menu callback, perform an action on attachments.
196   */   */
197  function _audio_attach_action($op, $nid, $index) {  function _audio_attach_action($op, $nid, $index) {
198    
199    switch($op) {    switch ($op) {
200      case 'remove':      case 'remove':
201        // remove the element index b from the array. rebuild the array, and save.        // remove the element index b from the array. rebuild the array, and save.
202        $children = audio_attach_get_children($nid);        $children = audio_attach_get_children($nid);
# Line 232  function _audio_attach_action($op, $nid, Line 241  function _audio_attach_action($op, $nid,
241  }  }
242    
243  /**  /**
244   *  Input a parent node id, and return an array of children id's ordered by their weight.   * Input a parent node id, and return an array of children id's ordered by their weight.
245   */   */
246  function audio_attach_get_children($nid) {  function audio_attach_get_children($nid) {
247    $children = array();    $children = array();
248    $result = db_query("SELECT aid, weight FROM {audio_attach} WHERE nid = %d ORDER BY weight ASC", $nid);    $result = db_query("SELECT aid, weight FROM {audio_attach} WHERE nid = %d ORDER BY weight ASC", $nid);
249    while($row = db_fetch_object($result)) {    while ($row = db_fetch_object($result)) {
250      $children[$row->weight] = $row->aid;      $children[$row->weight] = $row->aid;
251    }    }
252    return $children;    return $children;
253  }  }
254    
255  /**  /**
256   *  Add a new child to a parent node   * Add a new child to a parent node
257   */   */
258  function audio_attach_add_child($nid, $aid, $stack_bottom = TRUE) {  function audio_attach_add_child($nid, $aid, $stack_bottom = TRUE) {
259    $weight = audio_attach_get_next_weight($nid);    $weight = audio_attach_get_next_weight($nid);
# Line 252  function audio_attach_add_child($nid, $a Line 261  function audio_attach_add_child($nid, $a
261  }  }
262    
263  /**  /**
264   *  Delete a child node from a given parent node.  If no parent node is specified, it deletes the child from all parent nodes.  Returns true if successful.   * Delete a child node from a given parent node.  If no parent node is
265     * specified, it deletes the child from all parent nodes.  Returns true if
266     * successful.
267   */   */
268  function audio_attach_remove_child($aid, $nid, $weight) {  function audio_attach_remove_child($aid, $nid, $weight) {
269    $result = db_query("DELETE FROM {audio_attach} WHERE nid = %d AND aid = %d AND weight = %d", $nid, $aid, $weight);    $result = db_query("DELETE FROM {audio_attach} WHERE nid = %d AND aid = %d AND weight = %d", $nid, $aid, $weight);
# Line 260  function audio_attach_remove_child($aid, Line 271  function audio_attach_remove_child($aid,
271  }  }
272    
273  /**  /**
274   *  Delete a parent node. Returns true if successful.   * Delete a parent node. Returns true if successful.
275   */   */
276  function audio_attach_remove($nid) {  function audio_attach_remove($nid) {
277    $result = db_query("DELETE FROM {audio_attach} WHERE nid = %d", $nid);    $result = db_query("DELETE FROM {audio_attach} WHERE nid = %d", $nid);
# Line 268  function audio_attach_remove($nid) { Line 279  function audio_attach_remove($nid) {
279  }  }
280    
281  /**  /**
282   *   Get next weight in a given list.   * Get next weight in a given list.
283   **/   */
284  function audio_attach_get_next_weight($nid) {  function audio_attach_get_next_weight($nid) {
285    $max = db_result(db_query("SELECT MAX(weight) FROM {audio_attach} WHERE nid = %d LIMIT 1", $nid));    $max = db_result(db_query("SELECT MAX(weight) FROM {audio_attach} WHERE nid = %d", $nid));
286    return (is_null($max)) ? 0 : ++$max;    return (is_null($max)) ? 0 : ++$max;
287  }  }
288    
# Line 293  function _audio_attach_save_order($nid, Line 304  function _audio_attach_save_order($nid,
304  }  }
305    
306  /**  /**
307   *  Theme used if multiple files are attached   * Theme used if multiple files are attached
308   */   */
309  function theme_audio_attach_list($nids, $teaser = FALSE) {  function theme_audio_attach_list($nids, $teaser = FALSE) {
310    drupal_add_css(AUDIO_ATTACH_PATH .'/audio_attach.css');    drupal_add_css(AUDIO_ATTACH_PATH .'/audio_attach.css');
311    foreach ($nids as $aid) {    foreach ($nids as $aid) {
312      $audio = node_load($aid);      $audio = node_load($aid);
313      $audio = node_prepare($audio, $teaser);      $audio = node_prepare($audio, $teaser);
314      $title = $audio->status ? l($audio->title, 'node/' . $audio->nid, NULL, NULL, NULL, TRUE) : check_plain($audio->title);      $title = $audio->status ? l($audio->title, 'node/'. $audio->nid, array('html' => TRUE)) : check_plain($audio->title);
315      $items[] = '<div class="title">'. $title .'</div>'. theme('audio_teaser', $audio);      $items[] = '<div class="title">'. $title .'</div>'. theme('audio_teaser', $audio);
316    }    }
317    return theme('item_list', $items, null, 'ol', array('class' => 'audio-attach-list'));    return theme('item_list', $items, null, 'ol', array('class' => 'audio-attach-list'));
# Line 317  function audio_attach_get_audio_nodes() Line 328  function audio_attach_get_audio_nodes()
328    $sql .= !user_access("attach any existing audio file") ? "AND n.uid = $user->uid " : "";    $sql .= !user_access("attach any existing audio file") ? "AND n.uid = $user->uid " : "";
329    $sql .= "ORDER BY n.sticky DESC, n.title ASC";    $sql .= "ORDER BY n.sticky DESC, n.title ASC";
330    $result = db_query(db_rewrite_sql($sql));    $result = db_query(db_rewrite_sql($sql));
   if (db_num_rows($result) == 0) {  
       return $rows;  
   }  
331    while ($node = db_fetch_object($result)) {    while ($node = db_fetch_object($result)) {
332        $rows[$node->nid] = $node->title;        $rows[$node->nid] = $node->title;
333    }    }
# Line 327  function audio_attach_get_audio_nodes() Line 335  function audio_attach_get_audio_nodes()
335  }  }
336    
337  /**  /**
338   *  Return a list of currently attached audio files for a node.  Used in edit form.   * Return a list of currently attached audio files for a node.  Used in edit form.
339   * TODO: This is clunky. Needs to be leaner.   * TODO: This is clunky. Needs to be leaner.
340   */   */
341  function _audio_attach_current_list($nid) {  function _audio_attach_current_list($nid) {
# Line 337  function _audio_attach_current_list($nid Line 345  function _audio_attach_current_list($nid
345    $items = array();    $items = array();
346    $i = 0;    $i = 0;
347    if (is_array($node->audio_attach) && count($node->audio_attach) > 0) {    if (is_array($node->audio_attach) && count($node->audio_attach) > 0) {
348      foreach($node->audio_attach as $weight => $aid) {      foreach ($node->audio_attach as $weight => $aid) {
349        $audio = node_load($aid);        $audio = node_load($aid);
350        $drag = theme('image', AUDIO_ATTACH_PATH .'/images/drag_me.gif');        $drag = theme('image', AUDIO_ATTACH_PATH .'/images/drag_me.gif');
351        $link = l($audio->title, 'node/' . $audio->nid, NULL, NULL, NULL, TRUE);        $link = l($audio->title, 'node/'. $audio->nid, array('html' => TRUE));
352        $trashcan = l(theme('image', AUDIO_ATTACH_PATH .'/images/user-trash.png'), "audio_attach/remove/$node->nid/$i", array('class' => 'audio-attach-quick-link'), drupal_get_destination(), null, false, true);        $trashcan = l(theme('image', AUDIO_ATTACH_PATH .'/images/user-trash.png'),
353        $up = $i > 0 ? l(theme('image', AUDIO_ATTACH_PATH .'/images/go-up.png'), "audio_attach/up/$node->nid/$i", array('class' => 'audio-attach-quick-link'), drupal_get_destination(), null, false, true) : '' ;          "audio_attach/remove/$node->nid/$i", array(
354        $down = $i < count($node->audio_attach) - 1 ? l(theme('image', AUDIO_ATTACH_PATH .'/images/go-down.png'), "audio_attach/down/$node->nid/$i", array('class' => 'audio-attach-quick-link'), drupal_get_destination(), null, false, true) : '';            'attributes' => array('class' => 'audio-attach-quick-link'),
355              'query' => drupal_get_destination(),
356              'absolute' => true,
357            )
358          );
359          $up = $i > 0 ? l(theme('image', AUDIO_ATTACH_PATH .'/images/go-up.png'),
360            "audio_attach/up/$node->nid/$i", array(
361              'attributes' => array('class' => 'audio-attach-quick-link'),
362              'query' => drupal_get_destination(),
363              'absolute' => true,
364            )
365          ) : '' ;
366          $down = $i < count($node->audio_attach) - 1 ? l(theme('image', AUDIO_ATTACH_PATH .'/images/go-down.png'),
367            "audio_attach/down/$node->nid/$i", array(
368              'attributes' => array('class' => 'audio-attach-quick-link'),
369              'query' => drupal_get_destination(),
370              'absolute' => true,
371            )
372          ) : '';
373        $items[] = array($i + 1, $link, $down, $up, $trashcan);        $items[] = array($i + 1, $link, $down, $up, $trashcan);
374        $i++;        $i++;
375      }      }
# Line 353  function _audio_attach_current_list($nid Line 379  function _audio_attach_current_list($nid
379  }  }
380    
381  /**  /**
382   *  Ajax/Ahah callback to update the attached list table   * Ajax/Ahah callback to update the attached list table
383   */   */
384  function _audio_attach_ahah_update($nid) {  function _audio_attach_ahah_update($nid) {
385    print _audio_attach_current_list($nid);    print _audio_attach_current_list($nid);

Legend:
Removed from v.1.8  
changed lines
  Added in v.1.9

  ViewVC Help
Powered by ViewVC 1.1.3