<?php
// $Id: profilify.module,v 1.1 2007/12/30 02:34:10 cwgordon7 Exp $

/**
 * Implementation of hook_menu().
 */
function profilify_menu() {
  $items = array();
  $items['admin/build/profilify'] = array(
    'title' => t('Profilify'),
	'description' => t('Profilify this install.'),
	'page callback' => 'drupal_get_form',
	'page arguments' => array('profilify_generate'),
	'type' => MENU_DEFAULT_LOCAL_TASK,
  );
  $items['admin/build/profilify/view'] = array(
    'title' => t('View'),
	'description' => t('View the profilified version of this install.'),
	'page callback' => 'drupal_get_form',
	'page arguments' => array('profilify_view'),
	'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Menu callback - set the settings for profile generation.
 */
function profilify_generate() {
  $form = array();
  $on_off_settings = array('variables', 'modules', 'permissions', 'blocks', 'menus', 'content_types');
  $available = array('modules', 'variables');
  if (variable_get('profilify_generate_name', '') != '') {
    drupal_set_message(t('You may view your generated profile !url.', array('!url' => l(t('here'), 'admin/build/profilify/view'))));
  }
  $form['profilify_generate_name'] = array(
    '#type' => 'textfield',
	'#title' => t('Name'),
	'#description' => t('The name of the install profile, human-readable.'),
	'#default_value' => '',
	'#required' => TRUE,
  );
  $form['profilify_generate_internal'] = array(
    '#type' => 'textfield',
	'#title' => t('Internal name'),
	'#description' => t('The name of the install profile, machine-readable.'),
	'#default_value' => '',
	'#required' => TRUE,
  );
  $form['profilify_generate_description'] = array(
    '#type' => 'textarea',
	'#title' => t('Description'),
	'#description' => t('A description of the install profile, human-readable.'),
	'#default_value' => '',
	'#suffix' => t('Disabled checkboxes represent placeholders for future features.'),
	'#required' => TRUE,
  );
  foreach ($on_off_settings as $setting) {
    $form['profilify_generate_'. $setting] = array(
	  '#type' => 'checkbox',
	  '#title' => t(ucfirst($setting)),
	  '#description' => t('Should the current settings for @var be included in the profile?', array('@var' => $setting)),
	  '#default_value' => FALSE,
	  '#disabled' => !in_array($setting, $available),
	);
  }
  return system_settings_form($form);
}

/**
 * Menu callback. Displays the generated profile.
 */
function profilify_view() {
  $form = array();
  if (variable_get('profilify_generate_name', '') != '') {
    $form['install_profile_text'] = array(
      '#type' => 'textarea',
	  '#title' => t('Your profile\'s text'),
	  '#description' => t('Your profile\'s text. Copy-and-paste this into '. variable_get('profilify_generate_internal', '') .'.profile'),
	  '#default_value' => profilify_get_profile(),
	  '#prefix' => t('WARNING: This is online an outline for an install profile. This may contain irrelevant information, or may need some extra information not included.'),
	  '#disabled' => FALSE,
	);
  }
  else {
    drupal_set_message(t('There is no profile currently available.'));
  }
  return $form;
}

/**
 * Helper function for profilify_view. Returns the actual text.
 */
function profilify_get_profile() {
  $on_off_settings = array('variables', 'modules', 'permissions', 'blocks', 'menus', 'content_types');
  foreach ($on_off_settings as $setting) {
    $$setting = variable_get('profilify_generate_'. $setting, FALSE);
  }
  $name = variable_get('profilify_generate_name', '');
  $internal = variable_get('profilify_generate_internal', '');
  $description = variable_get('profilify_generate_description', '');
  $text = profilify_create($name, $internal, $description, $variables, $modules, $permissions, $blocks, $menus);
  return $text;
}

/**
 * Helper function for profility_create. Returns an executable set of variable_set() functions.
 */
function profilify_get_vars() {
  global $conf;
  $return = '';
  foreach ($conf as $name => $value) {
    // Profilify variables aren't included.
    if (strpos($name, 'profilify') === FALSE) {
	  // TODO: Don't include site-specific variables such as last cron run, install profile, etc.
      $return .= "\n  variable_set('". $name ."', ". var_export($value, TRUE) .");";
	}
  }
  return $return;
}

/**
 * Helper function for profilify_get_profile. Returns the bulk of the text.
 */
function profilify_create($name, $internal, $description, $variables, $modules, $permissions, $blocks, $menus) {
  $modulestring = '';
  if ($modules) {
    $module_list = module_list();
	if (count($module_list) > 0) {
	  $modulestring = '\'';
	  // Unset required core modules.
	  unset($module_list['block']);
	  unset($module_list['filter']);
	  unset($module_list['node']);
	  unset($module_list['system']);
	  unset($module_list['user']);
	  // Unset update status because the user decides whether or not to enable it in the install process.
	  unset($module_list['update']);
	  // Unset profilify.
	  unset($module_list['profilify']);
	  $modulestring .= implode('\', \'', $module_list);
	  $modulestring .= '\'';
	}
  }
  $varlist = '';
  if ($variables) {
    $varlist = profilify_get_vars();
  }
  $text = '<?php
// $Id: profilify.module,v 1.1 2007/12/30 02:34:10 cwgordon7 Exp $

/**
 * Return an array of the modules to be enabled when this profile is installed.
 *
 * @return
 *   An array of modules to enable.
 */
function '. $internal .'_profile_modules() {
  return array('. $modulestring .');
}

/**
 * Return a description of the profile for the initial installation screen.
 *
 * @return
 *   An array with keys \'name\' and \'description\' describing this profile,
 *   and optional \'language\' to override the language selection for
 *   language-specific profiles.
 */
function '. $internal .'_profile_details() {
  return array(
    \'name\' => \''. $name .'\',
    \'description\' => \''. $description .'\'
  );
}

/**
 * Return a list of tasks that this profile supports.
 *
 * @return
 *   A keyed array of tasks the profile will perform during
 *   the final stage. The keys of the array will be used internally,
 *   while the values will be displayed to the user in the installer
 *   task list.
 */
function '. $internal .'_profile_task_list() {
}

/**
 * Perform any final installation tasks for this profile.
 *
 * The installer goes through the profile-select -> locale-select
 * -> requirements -> database -> profile-install-batch
 * -> locale-initial-batch -> configure -> locale-remaining-batch
 * -> finished -> done tasks, in this order, if you don\'t implement
 * this function in your profile.
 *
 * If this function is implemented, you can have any number of
 * custom tasks to perform after \'configure\', implementing a state
 * machine here to walk the user through those tasks. First time,
 * this function gets called with $task set to \'profile\', and you
 * can advance to further tasks by setting $task to your tasks\'
 * identifiers, used as array keys in the hook_profile_task_list()
 * above. You must avoid the reserved tasks listed in
 * install_reserved_tasks(). If you implement your custom tasks,
 * this function will get called in every HTTP request (for form
 * processing, printing your information screens and so on) until
 * you advance to the \'profile-finished\' task, with which you
 * hand control back to the installer. Each custom page you
 * return needs to provide a way to continue, such as a form
 * submission or a link. You should also set custom page titles.
 *
 * You should define the list of custom tasks you implement by
 * returning an array of them in hook_profile_task_list(), as these
 * show up in the list of tasks on the installer user interface.
 *
 * Remember that the user will be able to reload the pages multiple
 * times, so you might want to use variable_set() and variable_get()
 * to remember your data and control further processing, if $task
 * is insufficient. Should a profile want to display a form here,
 * it can; the form should set \'#redirect\' to FALSE, and rely on
 * an action in the submit handler, such as variable_set(), to
 * detect submission and proceed to further tasks. See the configuration
 * form handling code in install_tasks() for an example.
 *
 * Important: Any temporary variables should be removed using
 * variable_del() before advancing to the \'profile-finished\' phase.
 *
 * @param $task
 *   The current $task of the install system. When hook_profile_tasks()
 *   is first called, this is \'profile\'.
 * @param $url
 *   Complete URL to be used for a link or form action on a custom page,
 *   if providing any, to allow the user to proceed with the installation.
 *
 * @return
 *   An optional HTML string to display to the user. Only used if you
 *   modify the $task, otherwise discarded.
 */
function '. $internal .'_profile_tasks(&$task, $url) {

  // Insert default user-defined node types into the database. For a complete
  // list of available node type attributes, refer to the node type API
  // documentation at: http://api.drupal.org/api/HEAD/function/hook_node_info.
  $types = array(
    array(
      \'type\' => \'page\',
      \'name\' => st(\'Page\'),
      \'module\' => \'node\',
      \'description\' => st("A <em>page</em>, similar in form to a <em>story</em>, is a simple method for creating and displaying information that rarely changes, such as an \"About us\" section of a website. By default, a <em>page</em> entry does not allow visitor comments and is not featured on the site\'s initial home page."),
      \'custom\' => TRUE,
      \'modified\' => TRUE,
      \'locked\' => FALSE,
      \'help\' => \'\',
      \'min_word_count\' => \'\',
    ),
    array(
      \'type\' => \'story\',
      \'name\' => st(\'Story\'),
      \'module\' => \'node\',
      \'description\' => st("A <em>story</em>, similar in form to a <em>page</em>, is ideal for creating and displaying content that informs or engages website visitors. Press releases, site announcements, and informal blog-like entries may all be created with a <em>story</em> entry. By default, a <em>story</em> entry is automatically featured on the site\'s initial home page, and provides the ability to post comments."),
      \'custom\' => TRUE,
      \'modified\' => TRUE,
      \'locked\' => FALSE,
      \'help\' => \'\',
      \'min_word_count\' => \'\',
    ),
  );

  foreach ($types as $type) {
    $type = (object) _node_type_set_defaults($type);
    node_type_save($type);
  }

  // Default page to not be promoted and have comments disabled.
  variable_set(\'node_options_page\', array(\'status\'));
  variable_set(\'comment_page\', COMMENT_NODE_DISABLED);
  
  '. $varlist .'

  // Don\'t display date and author information for page nodes by default.
  $theme_settings = variable_get(\'theme_settings\', array());
  $theme_settings[\'toggle_node_info_page\'] = FALSE;
  variable_set(\'theme_settings\', $theme_settings);

  // Update the menu router information.
  menu_rebuild();
}

/**
 * Implementation of hook_form_alter().
 *
 * Allows the profile to alter the site-configuration form. This is
 * called through custom invocation, so $form_state is not populated.
 */
function '. $internal .'_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == \'install_configure\') {
    // Set default for site name field.
    $form[\'site_information\'][\'site_name\'][\'#default_value\'] = $_SERVER[\'SERVER_NAME\'];
  }
}';
  return $text;
}
