You are here

function hosting_site_list_form in Hostmaster (Aegir) 6

Create a form for building a list of sites. @TODO Add ability to filter by additional fields

4 string references to 'hosting_site_list_form'
hosting_client_view in modules/hosting/client/hosting_client.module
Implementation of hook_view().
hosting_package_view in modules/hosting/package/hosting_package.module
Implementation of hook_view().
hosting_platform_view in modules/hosting/platform/hosting_platform.module
Implementation of hook_view().
hosting_sites in modules/hosting/site/hosting_site.module
Page handler for displaying list of hosted sites on front page

File

modules/hosting/site/hosting_site.module, line 493

Code

function hosting_site_list_form($form_state, $filter_by = NULL, $filter_value = NULL) {
  $step = isset($form_state['storage']['step']) ? $form_state['storage']['step'] : 1;

  // Step 1 - select sites
  if ($step == 1) {
    $args[] = 'site';
    $cond = '';
    if ($filter_by && $filter_value) {
      if ($filter_by == 'status') {
        $cond = ' AND s.' . $filter_by . ' & %d';
      }
      else {
        $cond = ' AND s.' . $filter_by . ' = %d';
      }
      $args[] = $filter_value;
    }
    $header = array(
      array(
        'data' => t('Site'),
        'field' => 'title',
      ),
      array(
        'data' => t('Profile'),
        'field' => 'profile',
      ),
      array(
        'data' => t('Language'),
        'field' => 'site_language',
      ),
      array(
        'data' => t('Created'),
        'field' => 'created',
        'sort' => 'desc',
      ),
    );
    $platforms = _hosting_get_platforms();
    if (sizeof($platforms) > 1) {
      $header[] = array(
        'data' => t('Platform'),
        'field' => 'platform',
      );
    }
    $sql = "SELECT n.nid, n.title, n.created, s.status as site_status, profile, s.language as site_language, platform, verified FROM {node} n left join {hosting_site} s ON n.vid=s.vid WHERE type='%s' AND s.status != -2 " . $cond;
    $sql .= tablesort_sql($header);

    // @TODO hide deleted sites
    $result = pager_query(db_rewrite_sql($sql), 25, 1, null, $args);
    $form['options'] = array(
      '#type' => 'fieldset',
      '#title' => t('Site tasks'),
      '#prefix' => '<div class="container-inline">',
      '#suffix' => '</div>',
    );
    $options = array();
    foreach (hosting_available_tasks('site') as $task => $array) {

      // At this stage we only want to handle simple tasks, the presense of a
      // specific task form means there are other options for this tasks.
      $func = 'hosting_task_' . $task . '_form';
      if (!function_exists($func) && user_access('create ' . $task . ' task')) {
        $options[$task] = $array['title'];
      }
    }
    $form['options']['task'] = array(
      '#type' => 'select',
      '#options' => $options,
      '#default_value' => 'backup',
    );
    $form['options']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Add to queue'),
      '#submit' => array(
        'hosting_site_list_form_submit',
      ),
    );
    $sites = array();
    while ($node = db_fetch_object($result)) {
      $sites[$node->nid] = '';
      $form['site'][$node->nid] = array(
        '#value' => l($node->title, 'node/' . $node->nid),
      );
      $form['profile'][$node->nid] = array(
        '#value' => $node->profile ? _hosting_node_link($node->profile) : t('n/a'),
      );
      $form['language'][$node->nid] = array(
        '#value' => $node->site_language ? _hosting_language_name($node->site_language) : t('n/a'),
      );
      $form['created'][$node->nid] = array(
        '#value' => t("@interval ago", array(
          '@interval' => format_interval(time() - $node->created, 1),
        )),
      );
      if (sizeof($platforms) > 1) {
        $form['platform'][$node->nid] = array(
          '#value' => $node->platform ? _hosting_node_link($node->platform) : t('n/a'),
        );
      }
      $form['site_class'][$node->nid] = array(
        '#value' => _hosting_site_list_class($node, $node->verified),
      );
    }
    $form['sites'] = array(
      '#type' => 'checkboxes',
      '#options' => $sites,
    );
    $form['pager'] = array(
      '#value' => theme('pager', NULL, 25, 1),
    );
    $form['#theme'] = 'hosting_site_list';
    $form['#action'] = url('hosting/sites');
    return $form;
  }
  elseif ($step == 2) {
    $task = $form_state['values']['task'];
    $tasks = hosting_available_tasks('site');
    $title = array(
      'passed' => t("The following sites will be processed"),
      'failed' => t("The following sites failed validation checks and will not be processed"),
    );
    foreach (array(
      'passed',
      'failed',
    ) as $type) {
      if (sizeof($form_state['storage'][$type])) {
        foreach ($form_state['storage'][$type] as $site_id => $url) {
          $row = array(
            'data' => array(
              array(
                'data' => l($url, 'node/' . $site_id),
                'class' => 'hosting-status',
              ),
            ),
            'class' => $type == 'passed' ? 'hosting-success' : 'hosting-error',
          );
          $rows[] = $row;
        }
      }
    }
    $header = array(
      t('Site'),
    );
    $form['sites_test'] = array(
      '#value' => theme('table', $header, $rows),
    );
    if (sizeof($form_state['storage']['failed']) && sizeof($form_state['storage']['passed'])) {
      drupal_set_message(t('The task @task is not able to be performed on all the sites selected, the sites below that failed will not be added to the queue.', array(
        '@task' => $task,
      )), 'warning');
    }
    elseif (sizeof($form_state['storage']['failed'])) {
      drupal_set_message(t('The task @task is not able to be performed on any of the selected sites.', array(
        '@task' => $task,
      )), 'error');
      $form['return_link'] = array(
        '#value' => l('Return to site listing', 'hosting/sites'),
      );
      return $form;
    }
    $form['help'] = array(
      '#value' => $tasks[$task]['description'],
    );
    $question = t('Are you sure you want to perform the "@task" task on each of the following sites?', array(
      '@task' => $tasks[$task]['title'],
    ));
    return confirm_form($form, $question, 'hosting/sites', '', $tasks[$task]['title']);
  }
}