Fix for nodequeuelock.module

  1. <?php
  2.  
  3. /*
  4.  * hook_nodequeue_add
  5.  */
  6. function nodequeuelock_nodequeue_add($sqid, $nid) {
  7.   nodequeuelock_enforce($sqid, $nid);
  8. }
  9.  
  10. /*
  11.  * hook_nodequeue_remove
  12.  */
  13. function nodequeuelock_nodequeue_remove($sqid, $nid) {
  14.   nodequeuelock_enforce($sqid, $nid);
  15. }
  16.  
  17. /*
  18.  * Ensure that everything is where it should be and stuff
  19.  */
  20. function nodequeuelock_enforce($sqid, $nid) {
  21.   $subqueue = nodequeue_load_subqueue($sqid);
  22.   $queue = nodequeue_load($subqueue->qid);
  23.   $nodes = nodequeuelock_load_queue($sqid);
  24.  
  25.   $sql = db_query("SELECT * FROM {nodequeue_lock} WHERE sqid = %d AND locked = 1",$sqid);
  26.   while($result = db_fetch_object($sql)) {
  27.     $locked[$result->nid] = $result->position;
  28.   }
  29.   foreach($locked as $nid => $position) {  
  30.     $nodes = nodequeuelock_load_queue($sqid);
  31.     // sometimes our node was removed    
  32.     if(!$nodes[$nid]) {
  33.       nodequeue_subqueue_add($queue, $subqueue, $nid);
  34.       $nodes = nodequeuelock_load_queue($sqid);
  35.     }
  36.     $suspect_nid = array_search($locked[$nid],$nodes);
  37.     // and sometimes its been misplaced
  38.     if($suspect_nid != $nid) {
  39.       $new_position = $nodes[$nid];
  40.       if(!$new_position) {
  41.         while($i < $queue->size) {
  42.           $i++;
  43.           $exists = array_search($i,$nodes);
  44.           if(!$exists) {
  45.             $new_postion = $i;
  46.           }
  47.          
  48.         }
  49.       }
  50.       db_query("UPDATE {nodequeue_nodes} SET position = %d WHERE nid = %d AND sqid = %d",$new_position, $suspect_nid, $sqid);
  51.      
  52.     }
  53.     db_query("UPDATE {nodequeue_nodes} SET position = %d WHERE nid = %d AND sqid = %d", $locked[$nid], $nid, $sqid);
  54.   }
  55. }
  56.  
  57. function nodequeuelock_form_alter(&$form, &$form_state, $form_id) {
  58.   switch($form_id) {
  59.     case 'nodequeue_arrange_subqueue_form':
  60.       $sql = db_query("SELECT * FROM {nodequeue_lock} WHERE sqid = %d",$form['#subqueue']['sqid']);
  61.       while($result = db_fetch_object($sql)) {
  62.         $locked[$result->nid][$result->position] = $result->locked;
  63.       }
  64.       $form['sqid'] = array(
  65.         '#type' => 'hidden',
  66.         '#value' => $form['#subqueue']['sqid']
  67.       );
  68.       foreach(element_children($form) as $key) {
  69.         if($form[$key]['#node']) {
  70.           $nid = $form[$key]['#node']['nid'];
  71.           $pos = $form[$key]['position']['#default_value'];
  72.           $form[$key]['title']['lock'] = array(
  73.             '#type' => 'checkbox',
  74.             '#title' => 'Lock',
  75.             '#default_value' => $locked[$nid][$pos]
  76.           );
  77.         }
  78.       }
  79.       $form['#submit'][] = 'nodequeuelock_subqueue_form_submit';
  80.       break;
  81.   }
  82. }
  83.  
  84. function nodequeuelock_subqueue_form_submit(&$form, &$form_state) {
  85.   foreach($form_state['values'] as $nid => $value) {
  86.     if(is_numeric($nid)) {
  87.       $update = null;
  88.       $result = db_fetch_object(db_query("SELECT * FROM {nodequeue_lock} WHERE sqid = %d AND nid = %d", $form_state['values']['sqid'], $nid));
  89.       if($result->sqid) {
  90.         $update = array('sqid', 'nid','position');
  91.       }
  92.       $record = array(
  93.         'sqid'  => $form_state['values']['sqid'],
  94.         'nid' => $nid,
  95.         'position' => $form_state['values'][$nid]['position'],
  96.         'locked' => $form_state['values'][$nid]['title']['lock'],
  97.         'expire' => time() + (3600 * 48)
  98.       );
  99.       drupal_write_record('nodequeue_lock', $record, $update);
  100.    
  101.     }
  102.   }
  103. }
  104.  
  105. function nodequeuelock_load_queue($sqid) {
  106.   $sql = db_query("
  107.    SELECT n.nid, nn.position FROM {node} n
  108.      INNER JOIN {nodequeue_nodes} nn ON n.nid = nn.nid
  109.    WHERE nn.sqid = %d  
  110.      ORDER BY nn.position DESC",
  111.     $sqid
  112.   );
  113.   while($result = db_fetch_object($sql)) {
  114.     $nodes[$result->nid] = $result->position;
  115.   }
  116.   return $nodes;
  117. }
  118.  
  119. function nodequeuelock_schema() {
  120.   $schema = array();
  121.   $schema['nodequeue_lock'] = array(
  122.     'description' => t('Indicates which nodes should remain locked in position'),
  123.     'fields' => array(
  124.       'sqid' => array(
  125.         'description' => t('Subqueue this node is in'),
  126.         'type' => 'int',
  127.         'unsigned' => TRUE,
  128.         'not null' => TRUE,
  129.       ),
  130.       'position' => array(
  131.         'description' => t('The position of the node in this subqueue.'),
  132.         'type' => 'int',
  133.         'unsigned' => TRUE,
  134.         'not null' => FALSE
  135.       ),
  136.       'nid' => array(
  137.         'description' => t('The position of the node in this subqueue.'),
  138.         'type' => 'int',
  139.         'unsigned' => TRUE,
  140.         'not null' => FALSE
  141.       ),
  142.       'locked' => array(
  143.         'description' => t('The position of the node in this subqueue.'),
  144.         'type' => 'int',
  145.         'unsigned' => TRUE,
  146.         'not null' => FALSE
  147.       ),
  148.       'expire' => array(
  149.         'description' => t(''),
  150.         'type' => 'int',
  151.         'unsigned' => TRUE,
  152.         'not null' => TRUE,
  153.         'default' => 0,
  154.       ),
  155.     ), // fields
  156.     'primary key' => array('sqid','nid')
  157.   ); // nodequeue_nodes
  158.   return $schema;
  159. }
  160.  
  161. function nodequeuelock_install() {
  162.   drupal_install_schema('nodequeuelock');
  163. }
  164.  
  165. function nodequeuelock_uninstall() {
  166.   drupal_uninstall_schema('nodequeuelock');
  167. }

Submit Fix

Any tags you'd like to associate with your code, delimitered by commas (example: Views, CCK, Module, etc).
Select the syntax highlighting mode to use.