Index: nodeasblock.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/nodeasblock/nodeasblock.module,v
retrieving revision 1.3.2.4
diff -u -r1.3.2.4 nodeasblock.module
--- nodeasblock.module	25 Feb 2007 03:30:17 -0000	1.3.2.4
+++ nodeasblock.module	3 Apr 2007 15:44:59 -0000
@@ -10,60 +10,134 @@
  * Implementation of hook_perm().
  */
 function nodeasblock_perm() {
-  return array('create blocks for nodes');
+  return array('create blocks for nodes', 'administer blocks for nodes');
 }
 
 
 function nodeasblock_form_alter($form_id, &$form) {
+  global $theme;
+  init_theme(); // Just in case
   if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
-    $form['workflow']['nodeasblock'] = array(
-      '#type' => 'checkbox',
-      '#title' => t('Enable blocks from this node type'),
+    $form['workflow']['nodeasblockset'] = array (
+      '#type' => 'fieldset',
+      '#title' => t('Node as block settings'),
+      '#collapsible' => TRUE,
+    );
+    $form['workflow']['nodeasblockset']['nodeasblock'] = array(
+      '#type' => 'radios',
+      '#title' => t('Create blocks from this content type'),
       '#default_value' => variable_get('nodeasblock_'. $form['#node_type']->type, 1),
+      '#options' => array(0 => t('Never (Disabled)'), 1 => t('On a per node basis'), 2 => t('Always')),
       '#return_value' => 1,
       '#description' => t('Allow users to make these nodes visible as sidebar blocks.'),
     );
+    // Regions to select from default theme
+    $regions = system_region_list($theme);
+    $form['workflow']['nodeasblockset']['nodeasblock_region'] = array(
+      '#type' => 'select',
+      '#multiple' => TRUE,
+      '#title' => t('Select allowed regions for these blocks'),
+      '#options' => $regions,
+      '#default_value' => variable_get('nodeasblock_region_'.$form['#node_type']->type, array()),
+      '#description' => t('These are only the regions defined for the default theme'),
+    );
+    $form['workflow']['nodeasblockset']['nodeasblock_default_region'] = array(
+      '#type' => 'select',
+      '#title' => t('Default region'),
+      '#options' => $regions,
+      '#default_value' => variable_get('nodeasblock_default_region_'.$form['#node_type']->type, system_default_region($theme)),
+    );
+    $form['workflow']['nodeasblockset']['nodeasblock_default_weight'] = array(
+      '#type' => 'weight',
+      '#title' => t('Default weight'),
+      '#options' => $regions,
+      '#default_value' => variable_get('nodeasblock_default_weight_'.$form['#node_type']->type, 0),
+    );    
   }
   if (isset($form['type'])) {
-    global $theme;
     $node = $form['#node'];
     if ($form['type']['#value'] .'_node_form' == $form_id && variable_get("nodeasblock_$node->type", TRUE) && user_access('create blocks for nodes')) {
+      if ($node->nid) {
+        $block = db_fetch_array(db_query("SELECT * FROM {blocks} WHERE module = '%s' AND delta = '%s' AND theme = '%s'", 'nodeasblock', $node->nid, $theme));
+      }
+      if(!$block) {
+        $block = array(
+          'status' => 1, 
+          'roles' => array(),
+          'region' => variable_get('nodeasblock_default_region_'.$node->type, system_default_region($theme)),
+          'weight' => variable_get('nodeasblock_default_weight_'.$node->type, 0),
+          'custom' => 0, 'visibility' => 0, 'pages' => ''
+        );
+      }
       $form['nodeasblockset'] = array (
         '#type' => 'fieldset',
         '#title' => t('Provide a block'),
         '#collapsible' => true,
         '#collapsed' => !$node->nodeasblock,
+        '#tree' => TRUE
       );
-      $form['nodeasblockset']['nodeasblock'] = array (
-        '#type' => 'checkbox',
-        '#title' => t('Create a block for this node?'),
-        '#default_value' => $node->nodeasblock,
-        '#description' => t('Check this box to create a block for this node. The block will contain the teaser. To administer the appearance of this block, you will need to visit !block_admin.', array('!block_admin' => l('admin/build/block', 'admin/build/block'))),
-      );
-    
-      if (user_access('administer blocks')) {
-        if ($node->nid) {
-          $block = db_fetch_array(db_query("SELECT * FROM {blocks} WHERE module = '%s' AND delta = '%s' AND theme = '%s'", 'nodeasblock', $node->nid, $theme));
+      // Status, will default to 1
+      $form['nodeasblockset']['status'] = array('#type' => 'value', '#value' => $block['status']);
+      // If variable not set we'll assume 1 = on a per node basis
+      if (variable_get("nodeasblock_$node->type", 1) < 2) { 
+        $form['nodeasblockset']['nodeasblock'] = array (
+          '#type' => 'checkbox',
+          '#title' => t('Create a block for this node?'),
+          '#default_value' => $node->nodeasblock,
+          '#description' => t('Check this box to create a block for this node. The block will contain the teaser.'),
+        );
+      } else {
+        // Always create a block
+        $form['nodeasblockset']['nodeasblock'] = array('#type' => 'value', '#value' => 1);
+        $form['nodeasblockset']['#description'] = t('A block is always created for this content type');
+      }
+      // Advanced settings
+      if (user_access('administer blocks') || user_access('administer blocks for nodes')) {
+        // Fetch available list of regions
+        $theme_regions = system_region_list($theme);
+        $allowed_regions = array(BLOCK_REGION_NONE => '<'. t('none') .'>');
+        foreach(variable_get("nodeasblock_region_$node->type", array()) as $region) {
+          if(array_key_exists($region, $theme_regions)) {
+            $allowed_regions[$region] = $theme_regions[$region];
+          }
         }
-        $form['nodeasblockset']['nodeasblock_region'] = array (
+        $form['nodeasblockset']['region'] = array (
           '#type' => 'select',
-          '#options' => system_region_list($theme),
+          '#options' => $allowed_regions,
           '#title' => t('Region'),
-          '#default_value' => isset($block) ? $block['region'] : system_default_region($theme),
+          '#default_value' => $block['region'],
           '#description' => t('The region of the page this block should appear in.'),
           );
-        $form['nodeasblockset']['nodeasblock_weight'] = array (
+        $form['nodeasblockset']['weight'] = array (
           '#type' => 'weight',
           '#title' => t('Weight'),
-          '#default_value' => isset($block) ? $block['weight'] : 0,
+          '#default_value' => $block['weight'],
           '#description' => t('The weight of this block. Larger numbers move a block farther down in a region.')
         );
-        $form['nodeasblockset']['nodeasblock_status'] = array (
-           '#type' => 'checkbox',
-            '#title' => t('Visible'),
-            '#default_value' => isset($block) ? $block['status'] : true,
-            '#description' => t('Make this block visible.'),
-        );
+        $title = drupal_get_title(); // Save title and reset after
+        $block_form = block_admin_configure('nodeasblock', $node->nid);
+        drupal_set_title($title);
+        foreach( array('block_settings', 'page_vis_settings') as $category) {
+          $form['nodeasblockset'][$category] = $block_form[$category];
+          $form['nodeasblockset'][$category]['#collapsed'] = TRUE;
+          $form['nodeasblockset'][$category]['#parents'] = array('nodeasblockset');
+        }
+        
+        // Really advanced settings, only for real block administrators
+        if (user_access('administer blocks')) {
+          foreach( array('user_vis_settings', 'role_vis_settings') as $category) {
+            $form['nodeasblockset'][$category] = $block_form[$category];
+            $form['nodeasblockset'][$category]['#collapsed'] = TRUE;
+            $form['nodeasblockset'][$category]['#parents'] = array('nodeasblockset');
+           }
+        } else {
+          $form['nodeasblock']['custom'] = array('#type' => 'value', '#value' => $block['custom']);     
+        }
+      } else {
+        // Just pass values
+        foreach (array('region', 'weight', 'title', 'custom', 'roles', 'visibility', 'pages') as $field) {
+          $form['nodeasblockset'][$field] = array('#type' => 'value', '#value' => $block[$field]);
+        }      
       }
     }
   }
@@ -78,16 +152,26 @@
     case 'update':
     case 'insert':
       global $theme;
+      // Fetch block data
+      $block = $node->nodeasblockset;
       db_query("DELETE FROM {nodeasblock} WHERE nid = $node->nid");
-      if ($node->nodeasblock) {
+      if ($block['nodeasblock']) {
         db_query("INSERT INTO {nodeasblock} VALUES ($node->nid)");
-      }
-
-      // If we were passed enough information, set the block visibility, region, and weight as well.
-      if ($node->nodeasblock && isset($node->nodeasblock_region)) {
-        $blocks = _block_rehash();
-        db_query("UPDATE {blocks} SET status = %d, region = '%s', weight = %d WHERE module = '%s' AND delta = '%s'",
-          $node->nodeasblock_status, $node->nodeasblock_region, $node->nodeasblock_weight, 'nodeasblock', $node->nid);
+        // This will actually create the block
+        _block_rehash();        
+        $block['delta'] = $node->nid;
+        $block['module'] = 'nodeasblock';
+        // Update block data
+        db_query("UPDATE {blocks} SET status = %d, region = '%s', weight = %d, visibility = %d, pages = '%s', custom = %d, title = '%s' WHERE module = '%s' AND delta = '%s'", 
+          $block['status'], $block['region'], $block['weight'], $block['visibility'], trim($block['pages']), $block['custom'], $block['title'], $block['module'], $block['delta']);
+        if (is_array($block['roles'])) {
+        db_query("DELETE FROM {blocks_roles} WHERE module = '%s' AND delta = '%s'", $block['module'], $block['delta']);
+          foreach (array_filter($block['roles']) as $rid) {
+            db_query("INSERT INTO {blocks_roles} (rid, module, delta) VALUES (%d, '%s', '%s')", $rid, $block['module'], $block['delta']);
+          }
+        }
+        drupal_set_message(t('The block configuration has been saved.'));
+        cache_clear_all();
       }
       break;
     case 'delete':
@@ -120,20 +204,16 @@
     // variable_set('nodeblock_block_items', $edit['items']);
   }
   else if ($op == 'view') {
-    // Integration with i18n-translation
-    if (module_exists('translation')) {
-      if ($trans_nid = translation_node_nid($delta, i18n_get_lang())) {
-        $delta = $trans_nid;
-      }
-    }
-    
     // don't show on own node page
     if(arg(0) == 'node' and arg(1) == $delta) {
         return;
     }
-    
+    // Dont show nodes in other languages than current. Integration with i18n node languages. 
+    global $locale;
     if ($node = node_load($delta)) {
-      return theme('nodeasblock', $node);
+      if (empty($node->language) || $node->language == $locale) {
+        return theme('nodeasblock', $node);
+      }
     }
   }
 } 
@@ -141,8 +221,13 @@
 /*** Themeing ***/
 
 function theme_nodeasblock($node) {
+  
   // return an array with 'subject' and 'content
   $node = node_prepare($node, true);
+  
+  if (node_access('update', $node)) {
+		$node->teaser .= '<div class="edit-block">'. l('['.t('edit').']', 'node/'.$node->nid.'/edit', array("title"=>t("Edit"))). '</div>';
+  }
 
   $content = node_view($node, true, true, true);
   //$content = $node->teaser; // this might be better
