Index: fancy_slide.module
===================================================================
--- fancy_slide.module	(revision 29510)
+++ fancy_slide.module	(working copy)
@@ -33,9 +33,16 @@
  */
 function fancy_slide_autocomplete_nid($string) {
   $matches = array();
+  $types = variable_get('fancy_slide_node_type', 0);
+  $select = "SELECT nid, title FROM {node} WHERE LOWER(title) LIKE LOWER('%%%s%%')";
+  $args = array($string);
+  if ($types) {
+    $select .= ' AND type in (' . db_placeholders($types, 'varchar') . ')';
+    $args = array_merge($args, $types);
+  }
   $result = db_query_range(
-    "SELECT nid, title FROM {node} WHERE LOWER(title) LIKE LOWER('%%%s%%')",
-    $string,
+    $select,
+    $args,
     0, 10
   );
   
@@ -95,6 +102,89 @@
   drupal_goto('admin/content/fancy-slide/view');
 }
 
+/*
+ * The form for global settings
+ *
+ */
+function fancy_slide_settings_form($form_state) {
+  $form = array();
+  $max_height = variable_get('fancy_slide_max_height', null);
+  $form['max_height'] = array(
+    '#type'          => 'textfield',
+    '#title'         => t('Maximum height'),
+    '#description'   => t('The maximum height in pixels of a slideshow'),
+    '#maxlength'     => 4,
+    '#default_value' => $max_height,
+  );
+  $max_width = variable_get('fancy_slide_max_width', null);
+  $form['max_width'] = array(
+    '#type'          => 'textfield',
+    '#title'         => t('Maximum width'),
+    '#description'   => t('The maximum width in pixels of a slideshow'),
+    '#maxlength'     => 4,
+    '#default_value' => $max_width,
+  );
+  $node_type = variable_get('fancy_slide_node_type', array());
+  foreach (node_get_types() as $type) {
+    $types[$type->type] = $type->name;
+  }
+  $form['node_type'] = array(
+    '#type'          => 'checkboxes',
+    '#title'         => t('Node Types'),
+    '#description'   => t('The types of nodes that can be used for slideshows. If none are checked, all will be available.'),
+    '#options'       => $types,
+    '#default_value' => $node_type,
+  );
+  $field = variable_get('fancy_slide_field', 'none');
+  $fields = fancy_slide_options_fields();
+  $fields['none'] = 'N/A';
+  $form['node_field'] = array(
+    '#type'           => 'select',
+    '#title'          => t('Content field'),
+    '#description'    => t('The field that contains the image. Bear in mind that this is a list of all fields, and not all fields will be on each node type, so you must make sure that you have chosen a field that appears on the type of node you\'re using, otherwise nothing will show up in the slideshow. If you specify a value here, it will override the same option on the main slideshow page.'),
+    '#options'        => $fields,
+    '#default_value'  => $field
+  );
+  $orientation = variable_get('fancy_slide_orientation', -1);
+  $form['orientation'] = array(
+    '#type'          => 'radios',
+    '#title'         => t('Fixed Orientation'),
+    '#description'   => t('If orientation is set here, it cannot be set on the edit page'),
+    '#options'       => array('-1' => t('N/A'), '0' => t('Horizontal'), '1' => t('Vertical')),
+    '#default_value' => $orientation,
+  );
+  $form['submit'] = array(
+    '#type'   => 'submit',
+    '#value'  => t('Save'),
+  );
+  return $form;
+}
+
+/* 
+ * Submit function for the settings form
+ */
+function fancy_slide_settings_form_submit($form_id, &$form_state) {
+  if ($form_state['values']['max_height'] && intval($form_state['values']['max_height'])) {
+    variable_set('fancy_slide_max_height', $form_state['values']['max_height']);
+  }
+  if ($form_state['values']['max_width'] && intval($form_state['values']['max_width'])) {
+    variable_set('fancy_slide_max_width', $form_state['values']['max_width']);
+  }
+  if ($form_state['values']['node_type']) {
+    foreach($form_state['values']['node_type'] as $name => $type) {
+      if ($type)
+        $types[] = $type;
+    }
+    variable_set('fancy_slide_node_type', $types);
+  }
+  if ($form_state['values']['orientation'] != null) {
+    variable_set('fancy_slide_orientation', $form_state['values']['orientation']);
+  }
+  if ($form_state['values']['node_field'] != null) {
+    variable_set('fancy_slide_field', $form_state['values']['node_field']);
+  }
+}
+
 /**
  * The form for adding or editing a slideshow.
  * 
@@ -125,22 +215,28 @@
     '#maxlength'      => 128,
     '#default_value'  => $slideshow['name'],
   );
+  $max_height = variable_get('fancy_slide_max_height', 2048);
+  $max_width = variable_get('fancy_slide_max_width', 2048);
   $form['dimensions'] = array(
     '#type'           => 'textfield',
     '#title'          => t('Dimensions'),
-    '#description'    => t('Enter a width and height in pixels, such as 860x352. Width must be between 64 and 2048. Height must be between 27 and 2048.'),
+    '#description'    => t('Enter a width and height in pixels, such as 860x352. Width must be between 64 and !width. Height must be between 27 and !height.', array('!width' => $max_width, '!height' => $max_height)),
     '#maxlength'      => 9,
     '#default_value'  => $slideshow['width'] ? $slideshow['width'] . 'x' . $slideshow['height'] : '',
   );
-  $form['type'] = array(
-    '#type'           => 'radios',
-    '#title'          => t('Type of slideshow'),
-    '#description'    => t('Choose the type of slideshow. This will determine where the images are drawn from.'),
-    '#required'       => TRUE,
-    '#options'        => fancy_slide_options_types(),
-    '#default_value'  => $slideshow['type'],
-  );
-  $form['nid'] = array(
+  // Don't bother showing this if there is only one option
+  $types = fancy_slide_options_types();
+  if (count($types) > 2) {
+    $form['type'] = array(
+      '#type'           => 'radios',
+      '#title'          => t('Type of slideshow'),
+      '#description'    => t('Choose the type of slideshow. This will determine where the images are drawn from.'),
+      '#required'       => TRUE,
+      '#options'        => $types,
+      '#default_value'  => $slideshow['type'],
+    );
+  }
+  $form['node'] = array(
     '#type'               => 'textfield',
     '#title'              => t('Node'),
     '#description'        => t('The node that contains all the images for the slideshow. Begin typing the node\'s title to autocomplete it.'),
@@ -167,13 +263,17 @@
     ),
     '#default_value'  => $slideshow['animation'] ? $slideshow['animation'] : 'fade',
   );
-  $form['slidefield'] = array(
-    '#type'           => 'select',
-    '#title'          => t('Slideshow field'),
-    '#description'    => t('The field that contains the image. Bear in mind that this is a list of all fields, and not all fields will be on each node type, so you must make sure that you have chosen a field that appears on the type of node you\'re using, otherwise nothing will show up in the slideshow. If you are using a single node type slideshow, this field is expected to contain multiple images. Otherwise, if the field contains multiple images, only the first will be used.'),
-    '#options'        => fancy_slide_options_fields(),
-    '#default_value'  => $slideshow['field'],
-  );
+  // Only show this if it's not overriden
+  $field = variable_get('fancy_slide_field', 'none');
+  if ($field == 'none') {
+    $form['slidefield'] = array(
+      '#type'           => 'select',
+      '#title'          => t('Slideshow field'),
+      '#description'    => t('The field that contains the image. Bear in mind that this is a list of all fields, and not all fields will be on each node type, so you must make sure that you have chosen a field that appears on the type of node you\'re using, otherwise nothing will show up in the slideshow. If you are using a single node type slideshow, this field is expected to contain multiple images. Otherwise, if the field contains multiple images, only the first will be used.'),
+      '#options'        => fancy_slide_options_fields(),
+      '#default_value'  => $slideshow['field'],
+    );
+  }
   $form['delay'] = array(
     '#type'           => 'textfield',
     '#title'          => t('Delay between slides'),
@@ -208,14 +308,17 @@
     '#default_value'  => $slideshow['continuous'],
   );
   
-  $form['vertical'] = array(
-    '#type'           => 'radios',
-    '#title'          => t('Slideshow orientation'),
-    '#description'    => t('If horizontal, the slides will animate horizontally as though they are side by side. If vertical, the slides will animate vertically as though they are on top of one another.'),
-    '#required'       => TRUE,
-    '#options'        => array('0' => t('Horizontal'), '1' => t('Vertical')),
-    '#default_value'  => $slideshow['vertical'],
-  );
+  $orientation = variable_get('fancy_slide_orientation', -1);
+  if ($orientation == -1) {
+    $form['vertical'] = array(
+      '#type'           => 'radios',
+      '#title'          => t('Slideshow orientation'),
+      '#description'    => t('If horizontal, the slides will animate horizontally as though they are side by side. If vertical, the slides will animate vertically as though they are on top of one another.'),
+      '#required'       => TRUE,
+      '#options'        => array('0' => t('Horizontal'), '1' => t('Vertical')),
+      '#default_value'  => $slideshow['vertical'],
+    );
+  }
   $form['show_controls'] = array(
     '#type'           => 'checkbox',
     '#title'          => t('Show controls'),
@@ -242,20 +345,32 @@
 function fancy_slide_edit_form_validate($form_id, &$form_state) {
   $form_state['values']['slideshow_name'] = check_plain($form_state['values']['slideshow_name']);
   
-  if (!preg_match('/[0-9]{1,4}x[0-9]{1,4}/', $form_state['values']['dimensions'])) {
+  $match = preg_match('/([0-9]{1,4})x([0-9]{1,4})/', $form_state['values']['dimensions'], $matches);
+
+  if (!$match) {
     form_set_error('dimensions', t('Dimensions must be in the form 640x480 (numbers, followed by the x character, followed by more numbers.'));
   }
+  $width = intval($matches[1]);
+  $height = intval($matches[2]);
+  $max_width = variable_get('fancy_slide_max_width', 2048);
+  $max_height = variable_get('fancy_slide_max_height', 2048);
+  if ($width < 64 || $width > $max_width) {
+    form_set_error('dimensions', t('Width must be between 64 and !width', array('!width' => $max_width)));
+  }
+  if ($height < 64 || $height > $max_height) {
+    form_set_error('dimensions', t('Height must be between 64 and !height', array('!height' => $max_height)));
+  }
   
   $showtype = $form_state['values']['type'];
   
   // If the slideshow is a single type, make sure that the node exists.
   if ($showtype == FANCY_SLIDE_TYPE_SINGLE) {
     $nid = db_result(db_query(
-      "SELECT nid FROM {node} WHERE title = '%s'", $form_state['values']['nid']
+      "SELECT nid FROM {node} WHERE title = '%s'", $form_state['values']['node']
     ));
     
     if (!$nid) {
-      form_set_error('nid', t('The node %node was not found.', array('%node', $form_state['values']['nid'])));
+      form_set_error('nid', t('The node %node was not found.', array('%node', $form_state['values']['node'])));
     }
   }
   // If the slideshow is a nodequeue type, make sure a nodequeue is selected and
@@ -284,8 +399,8 @@
   }
   
   // Make sure that the selected field actually exists.
-  if (!in_array($form_state['values']['slidefield'], array_keys(content_fields()))) {
-    form_set_error('slidefield', t('The field %field does not exist.', array('%title', $form_state['values']['slidefield'])));
+  if (isset($form_state['values']['slidefield']) && !in_array($form_state['values']['slidefield'], array_keys(content_fields()))) {
+    form_set_error('slidefield', t('The field %field does not exist.', array('%field', $form_state['values']['slidefield'])));
   }
   
   // Make sure the slideshow delay is numeric and greater than 0.
@@ -317,19 +432,35 @@
   
   $slide['name'] = $form_state['values']['slideshow_name'];
   list($slide['width'], $slide['height']) = explode('x', $form_state['values']['dimensions']);
-  $slide['type'] = $form_state['values']['type'];
+  if (isset($form_state['values']['type'])) {
+    $slide['type'] = $form_state['values']['type'];
+  }
+  else {
+    $types = fancy_slide_options_types();
+    $slide['type'] = $types[0];
+  }
   $slide['animation'] = $form_state['values']['animation'];
   $slide['nodequeue'] = $form_state['values']['nodequeue'];
-  $slide['field'] = $form_state['values']['slidefield'];
+  if (isset($form_state['values']['slidefield'])) {
+    $slide['field'] = $form_state['values']['slidefield'];
+  }
+  else {
+    $slide['field'] = variable_get('fancy_slide_field', 'none');
+  }
   $slide['delay'] = $form_state['values']['delay'];
   $slide['preset'] = $form_state['values']['preset'];
-  $slide['vertical'] = $form_state['values']['vertical'];
+  if (isset($form_state['values']['vertical'])) {
+    $slide['vertical'] = $form_state['values']['vertical'];
+  }
+  else {
+    $slide['vertical'] = variable_get('fancy_slide_orientation', 1);
+  }
   $slide['continuous'] = $form_state['values']['continuous'];
   $slide['transition_speed'] = $form_state['values']['transition_speed'];
   $slide['show_controls'] = $form_state['values']['show_controls'];
   $slide['slide_controls'] = $form_state['values']['slide_controls'];
   
-  if ($title = $form_state['values']['nid']) {
+  if ($title = $form_state['values']['node']) {
     $slide['nid'] = db_result(db_query_range(
       "SELECT nid FROM {node} WHERE title = '%s'", $title, 0, 1
     ));
@@ -483,6 +614,14 @@
     'page arguments'    => array('fancy_slide_edit_form'),
     'access arguments'  => array('administer fancy_slide'),
   );
+  $items['admin/content/fancy-slide/admin'] = array(
+    'title'             => 'Settings',
+    'description'       => 'Fancy Slide Settings',
+    'type'              => MENU_LOCAL_TASK,
+    'page callback'     => 'drupal_get_form',
+    'page arguments'    => array('fancy_slide_settings_form'),
+    'access arguments'  => array('administer fancy_slide settings'),
+  );
   $items['admin/content/fancy-slide/edit/%'] = array(
     'title'             => 'Edit',
     'description'       => 'Edit a Fancy Slide slideshow',
@@ -587,6 +726,7 @@
 function fancy_slide_perm() {
   return array(
     'administer fancy_slide',
+    'administer fancy_slide settings',
   );
 }
 
