You are here

function module_builder_page_input in Module Builder 6.2

Same name and namespace in other branches
  1. 5 module_builder.module \module_builder_page_input()
  2. 7.2 includes/module_builder.pages.inc \module_builder_page_input()
  3. 7 includes/module_builder.pages.inc \module_builder_page_input()
1 call to module_builder_page_input()
module_builder_page in includes/module_builder.pages.inc

File

includes/module_builder.pages.inc, line 60
Menu callback for main module builder page.

Code

function module_builder_page_input($form_state) {

  /*
  // built some default values.
  // these are either hardcoded or what the user already put into the form on a previous go round.
  $form_default_values = array(
    'module_root_name' => 'mymodule',
  );
  if (isset($form_state['storage']['input'])) {
    $form_default_values = array_merge($form_default_values, $form_state['storage']['input']);
  }

  #dsm($form_default_values);
  #dsm($form_state['storage']['input']);
  dsm($form_state);
  */

  // sanity check first time around

  #_module_builder_check_settings(); // moved up to main form builder

  // Get list of hooks from downloaded documentation, organized in fieldsets.
  // include the data processing file
  // TODO: only need the data loading.
  module_builder_include('process');
  $hook_groups = module_builder_get_hook_data();
  if (!is_array($hook_groups) || !count($hook_groups)) {
    form_set_error('hooks', t('No hooks were found. Please check the documentation path specified in the <a href="!settings">%administer >> %settings >> %modulebuilder</a> page.', array(
      '!settings' => url('admin/settings/module_builder'),
      '%administer' => 'Administer',
      '%settings' => 'Site configuration',
      '%modulebuilder' => "Module builder",
    )));
    return $form;
  }

  // Include CSS for formatting
  drupal_add_css(drupal_get_path('module', 'module_builder') . '/theme/module_builder.css');

  // Mark form as fresh to enable JS clearing of fields with sample text.
  if ($form_state['clicked_button']['#name'] != 'back') {
    $form['#attributes'] = array(
      'class' => 'fresh',
    );
  }

  // Module properties
  $form['module_root_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Machine-readable name'),
    '#description' => t('This string is used to name the module files and to prefix all of your functions. This must only contain letters, numbers, and underscores, and may not start with a number.'),
    '#required' => TRUE,
    '#default_value' => t('mymodule'),
    # $form_default_values['module_root_name'],
    '#repopulate' => TRUE,
  );
  $form['module_readable_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Name'),
    '#description' => t('Name of your module as it will appear on the module admin page.'),
    '#required' => TRUE,
    '#default_value' => t('My Module'),
    '#repopulate' => TRUE,
  );
  $form['module_short_description'] = array(
    '#type' => 'textfield',
    '#title' => t('Description'),
    '#description' => t('This text will appear in the module listing at <a href="!listing">%administer >> %build >> %modules</a>.', array(
      '!listing' => url('admin/build/modules'),
      '%administer' => 'Administer',
      '%build' => 'Site building',
      '%modules' => 'Modules',
    )),
    '#required' => TRUE,
    '#default_value' => t('Does awesome things. Makes tea. Washes up. Favours of a personal nature.'),
    '#repopulate' => TRUE,
  );
  $form['module_help_text'] = array(
    '#type' => 'textarea',
    '#title' => t('Help text'),
    '#description' => t('Help text (HTML) to appear in <a href="!help">%administer >> %help >> module_name</a> page.', array(
      '!help' => url('admin/help'),
      '%administer' => 'Administer',
      '%help' => 'Help',
    )),
    '#repopulate' => TRUE,
  );
  $form['module_dependencies'] = array(
    '#type' => 'textfield',
    '#title' => t('Dependencies'),
    '#description' => t('Space seperated list of other modules that your module requires.'),
    '#repopulate' => TRUE,
  );
  $form['module_package'] = array(
    '#type' => 'textfield',
    '#title' => t('Package'),
    '#description' => t('If your module comes with other modules or is meant to be used exclusively with other modules, enter the package name here. Suggested package names: Audio, Bot, CCK, Chat, E-Commerce, Event, Feed parser, Organic groups, Station, Video, Views and Voting.'),
    '#repopulate' => TRUE,
  );

  // Check for custom hook_groups file, else use default
  $path = drupal_get_path('module', 'module_builder');
  if (file_exists($path . MODULE_BUILDER_CUSTOM_HOOK_GROUPS_TEMPLATE_PATH)) {
    $template_file = file_get_contents($path . MODULE_BUILDER_CUSTOM_HOOK_GROUPS_TEMPLATE_PATH);
  }
  else {
    $template_file = file_get_contents($path . MODULE_BUILDER_HOOK_GROUPS_TEMPLATE_PATH);
  }
  $form['hook_groups'] = array(
    '#type' => 'fieldset',
    '#title' => t('Hook groupings'),
    '#description' => t('Selecting one or more of these features will automatically select appropriate hooks for you.'),
  );
  drupal_add_js($path . '/theme/module_builder.js');

  // Get list of hook presets from installed template.
  // Include generating component file.
  module_builder_include('process');
  $hook_presets = module_builder_parse_template($template_file);
  foreach ($hook_presets as $hook_preset_name => $hook_preset) {
    $hooks = explode("\n", $hook_preset['template']);
    $hook_array = array();
    foreach ($hooks as $hook) {
      $hook = trim($hook);
      if (!empty($hook)) {
        $hook_array[] = "'{$hook}'";
      }
    }
    $form['hook_groups']['groups-' . $hook_preset_name] = array(
      '#type' => 'checkbox',
      '#title' => $hook_preset_name,
      '#attributes' => array(
        'onclick' => 'check_hooks(this, [' . implode(', ', $hook_array) . '])',
      ),
    );
  }

  // Build hooks list
  $form['hooks'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Use the following specific hooks'),
  );
  foreach ($hook_groups as $hook_group => $hooks) {
    $form['hooks'][$hook_group] = array(
      '#type' => 'fieldset',
      '#title' => $hook_group . ' hooks',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#theme' => 'module_builder_hook_list',
    );
    foreach ($hooks as $hook) {
      $name = $hook['name'];
      $desc = $hook['description'];
      $form['hooks'][$hook_group][$name] = array(
        '#type' => 'checkbox',
        '#title' => preg_replace('/^hook_/', '', $name),
        '#description' => $desc,
        '#repopulate' => TRUE,
      );

      // Set some default hooks
      if ($name == 'hook_menu') {
        $form['hooks'][$hook_group][$name]['#default_value'] = 1;
      }
    }

    // Sort list alphabetically
    ksort($form['hooks'][$hook_group]);
  }
  $form['generate_module'] = array(
    '#type' => 'submit',
    '#name' => 'generate',
    '#value' => t('Generate'),
  );
  $form['#submit'] = array(
    'module_builder_page_input_submit',
  );

  #if ($form_state['rebuild']) { // fails as a test!?!?!
  if ($form_state['values']) {

    #dsm('rebuild');
    $form = _form_repopulate($form, $form_state);

    #dsm($form_state['storage']['input']);
  }
  return $form;
}