You are here

module_builder.api.inc in Module Builder 6

API functions for the module_builder module

File

module_builder.api.inc
View source
<?php

/**
 * @file
 * API functions for the module_builder module
 */

/**
 * Gets all components
 *
 * Components are things like 'node', 'menu', 'comment'.  Basically,
 * a component is a hook (however, like the node component, it is not limnited
 * to one   Components are supplied with hook_module_builder.  It returns an array 
 * of arrays, which give information about the components.  The components each 
 * have a title, a machine(-readable name) a callback (which returns an FAPI array), 
 * and a submit function (which is set to a reasonable default).
 *
 * @return
 *   An array of all the components
 *
 * @see module_builder_module_builder for the example components array
 * @see module_builder_edit_form for an example form
 */
function module_builder_get_components() {
  include_once drupal_get_path('module', 'module_builder') . '/module_builder.components.inc';
  $return = array();
  foreach (module_implements('module_builder') as $module) {
    $function = $module . '_module_builder';
    $result = $function();
    foreach ($result as $item) {
      $return[$item['machine']] = $item + array(
        'title' => '',
        'machine' => '',
        'callback' => '',
        'default' => FALSE,
        'submit' => 'module_builder_default_submit',
        'submit_button' => TRUE,
        'export' => FALSE,
      );
    }
  }
  return $return;
}

/**
 * Checks if a module id is valid
 * 
 * @param $mid
 *   The module ID to be loaded
 * @return
 *   Either the module ID if found or FALSE if it wasn't found
 */
function module_builder_load($mid) {
  $result = db_fetch_object(db_query("SELECT * FROM {module_builder_basic} WHERE mid = %d", $mid));
  if ($result) {
    return $result;
  }
  else {
    return FALSE;
  }
}

/**
 * Builder form
 * 
 * @param $form_state
 *   The form state (from form.inc).  Passed into the callback.
 * @param $callback
 *   The callback function
 * @param $mid
 *   The module id
 * @param $type
 *   The name of the component (menu, node, comment)
 * @return
 *   An FAPI form array
 */
function module_builder_build(&$form_state, $module, $type) {
  $result = db_result(db_query("SELECT name FROM {module_builder_basic} WHERE mid = %d", $module->mid));
  drupal_set_title($result);
  $components = module_builder_get_components();
  $component = $components[$type];
  $result = unserialize(db_result(db_query("SELECT data FROM {module_builder_data} WHERE mid = %d AND type = '%s'", $module->mid, $type)));
  $values = $result !== FALSE ? $result : new stdClass();
  $form = $component['callback']($form_state, $values, $module);
  if ($component['submit_button']) {
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Submit'),
    );
  }
  $form['#submit'][] = $component['submit'];
  $form['mid'] = array(
    '#type' => 'value',
    '#value' => $module->mid,
  );
  $form['type'] = array(
    '#type' => 'value',
    '#value' => $type,
  );
  return $form;
}

/**
 * Module builder default submit
 */
function module_builder_default_submit($form, &$form_state) {
  $values = (object) $form_state['values'];
  unset($values->form_id, $values->submit, $values->op, $values->form_build_id, $values->form_token);
  $final = new stdClass();
  $final->mid = $values->mid;
  $final->type = $values->type;
  unset($values->mid, $values->type);
  $final->data = $values;
  if ((bool) db_result(db_query("SELECT mid FROM {module_builder_data} WHERE mid = %d AND type = '%s'", $final->mid, $final->type))) {
    drupal_write_record('module_builder_data', $final, array(
      'mid',
      'type',
    ));
  }
  else {
    drupal_write_record('module_builder_data', $final);
  }
  $form_state['rebuild'] = TRUE;
}

/**
 * The JavaScript callback.
 * 
 * Uses magic too complex to be explained here.
 */
function module_builder_js($param = '') {
  include_once drupal_get_path('module', 'module_builder') . '/module_builder.components.inc';
  $form_state = array(
    'storage' => NULL,
    'submitted' => FALSE,
  );
  $form_build_id = $_POST['form_build_id'];
  $form = form_get_cache($form_build_id, $form_state);

  // #parameters has $form_id, $form_state and then whatever was passed to
  // drupal_get_form.
  $args = $form['#parameters'];
  $form_id = array_shift($args);
  $form['#post'] = $_POST;
  $form['#redirect'] = FALSE;

  // This will set up $form_state['clicked_button'] and
  // $form_state['storage']['mlid'].
  drupal_process_form($form_id, $form, $form_state);

  // Recreate and re-cache the form.
  $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);

  // Pick up the parents of the pressed button.
  $array_parents = $form_state['clicked_button']['#array_parents'];

  // The last parent is the button itself, we need the wrapper instead.
  array_pop($array_parents);
  while ($array_parents) {
    $parent = array_shift($array_parents);
    $form = $form[$parent];
  }

  // Remove the button.
  unset($form['parent_submit']);

  // Render messages and selects.
  $output = theme('status_messages') . drupal_render($form);
  drupal_json(array(
    'status' => TRUE,
    'data' => $output,
  ));
}
function module_builder_add_js($name, $a2, $a3, $a4) {
  drupal_add_js(drupal_get_path('module', 'module_builder') . '/module_builder.js');
  switch ($name) {
    case 'copy':
      drupal_add_js(array(
        'moduleBuilderCopy' => array(
          array(
            'type' => $a2,
            'from' => $a3,
            'to' => $a4,
          ),
        ),
      ), 'setting');
  }
}
function module_builder_add_hook($hook, $module) {
  include_once './' . drupal_get_path('module', 'module_builder') . '/hooks/' . $hook . '.hook';
  return str_replace('{{{hook}}}', $module->machine, $hook);
}
function module_builder_archive_tar_enabled() {
  return (bool) file_exists('./' . drupal_get_path('module', 'module_builder') . '/archive_tar/archive_tar.php');
}
function module_builder_export_callback($module, $file) {
  $edit = unserialize(db_result(db_query("SELECT data FROM {module_builder_data} WHERE type = '%s' AND mid = %d", 'edit', $module->mid)));
  $module = (object) ((array) $module + (array) $edit + array(
    'description' => '',
    'machine' => '',
  ));
  if ($file == 'info') {
    header('Content-disposition: attachment');
    print _module_builder_export_info($module);
    exit;
  }
  elseif ($file == 'module') {
    header('Content-disposition: attachment');
    print _module_builder_export_module($module);
    exit;
  }
  elseif ($file == 'tar') {
  }
  else {
    drupal_not_found();
  }
}
function _module_builder_export_info($module) {
  $output = '';
  $info = array();
  $info['name'] = '"' . $module->name . '"';
  $info['description'] = '"' . $module->description . '"';
  $info['core'] = '6.x';
  foreach ($info as $key => $value) {
    $output .= $key . ' = ' . $value . "\n";
  }
  return $output;
}
function _module_builder_export_module($module) {
  $output = "<?php\n";
  $output .= "// \$" . "Id\$ \n\n";
  $output .= "/**\n";
  $output .= " * @fi" . "le " . $module->name . "\n";
  $output .= " * " . $module->description . "\n";
  $output .= " */\n\n";
  $components = module_builder_get_components();
  foreach ($components as $component_name => $component) {
    if ($component['export'] != FALSE) {
      $values = unserialize(db_result(db_query("SELECT data FROM {module_builder_data} WHERE type = '%s' AND mid = %d", $component_name, $module->mid)));
      if (!$values) {
        $values = new stdClass();
      }
      $output .= $component['export']($values, $module) . "\n";
    }
  }
  $output .= "\n";
  return $output;
}