You are here

function domain_taxonomy_form_alter in Domain Taxonomy 7

Same name and namespace in other branches
  1. 6 domain_taxonomy.module \domain_taxonomy_form_alter()
  2. 7.3 domain_taxonomy.module \domain_taxonomy_form_alter()

File

./domain_taxonomy.module, line 63

Code

function domain_taxonomy_form_alter(&$form, &$form_state, $form_id) {
  global $_domain, $user;

  // There are forms that we never want to alter, and they are passed here.
  $forms = module_invoke_all('domainignore');
  if (in_array($form_id, $forms)) {
    return;
  }

  // Apply to all node editing forms
  if (isset($form['#node']) && isset($form['#node_edit_form'])) {
    $form['#submit'][] = 'domain_taxonomy_form_node_submit';
    $parent_vid = variable_get('domain_inherit_type_voc_' . $form['#node']->type, 0);
    if ($parent_vid) {
      $vocs = taxonomy_get_vocabularies();
      if (user_access('set domain access')) {
        $form['domain']['domain_load_fromparent'] = array(
          '#type' => 'checkbox',
          '#prefix' => '<b>' . t('Actions on form submit') . ':</b>',
          '#title' => t('Load domain access options for node from parent term'),
          '#description' => t('This option overrides domain access options below (except <em>Source domain</em>). Parent vocabulary @v', array(
            '@v' => drupal_strtoupper($vocs[$parent_vid]->name),
          )),
          '#default_value' => TRUE,
          '#weight' => -3,
        );
      }
      else {
        $form['domain']['domain_load_fromparent'] = array(
          '#type' => 'value',
          '#value' => TRUE,
        );
      }
    }
  }

  // Apply to all term editing forms
  if (in_array($form['#id'], array(
    'taxonomy-form-term',
    'forum-form-forum',
    'forum-form-container',
  ))) {
    if (!isset($form['tid'])) {
      return;
    }
    $term = taxonomy_term_load($form['tid']['#value']);
    if (!isset($form['vid'])) {
      return;
    }
    $vocabulary = taxonomy_vocabulary_load($form['vid']['#value']);
    $disabled_vocs = _domain_taxonomy_get_disabled_vocabulares();
    if (in_array($vocabulary->vid, $disabled_vocs)) {
      return;
    }
    domain_taxonomy_load_term($term);
    $form['#submit'][] = 'domain_taxonomy_form_term_submit';

    // By default, the requesting domain is assigned.
    $default = array(
      $_domain['domain_id'],
    );

    // How is core content handled for this site?
    $behavior = variable_get('domain_taxonomy_behavior', DOMAIN_INSTALL_RULE);
    if ($_domain['domain_id'] == 0) {
      $default[] = -1;
    }

    // Some options will be passed as hidden values, we need to run some checks on those.
    if (isset($term->tid)) {
      $raw = $term->domains;
    }
    else {
      $raw = $default;
    }
    $options = array();
    foreach (domain_domains() as $data) {

      // Cannot pass zero in checkboxes.
      $data['domain_id'] == 0 ? $key = -1 : ($key = $data['domain_id']);

      // The domain must be valid.
      if ($data['valid'] || user_access('administer domains')) {
        $options[$key] = $data['sitename'];
        $source_options[$data['domain_id']] = $data['sitename'];
      }
    }

    // If the user is a site admin, show the form, otherwise pass it silently.
    if (user_access('set domain access')) {
      $form['domain'] = array(
        '#type' => 'fieldset',
        '#title' => t('Domain access options'),
        '#collapsible' => TRUE,
        '#collapsed' => FALSE,
        '#weight' => 0,
      );
      $dt_edit_id = 'domain-taxonomy-term-edit-' . (isset($term->tid) ? $term->tid : '');
      $form['domain']['domain_update_subterm'] = array(
        '#type' => 'checkbox',
        '#prefix' => '<strong>' . t('Actions on form submit') . ':</strong>',
        '#title' => t('Override all child terms'),
        '#description' => t('This option is not for saving: it only processes subterms when this form is submitted.'),
        '#default_value' => FALSE,
      );
      $form['domain']['domain_update_subnodes'] = array(
        '#type' => 'checkbox',
        '#title' => t('Override all child nodes'),
        '#description' => t('This option is not for saving: it only processes child nodes when this form is submitted.'),
        '#default_value' => FALSE,
      );
      $form['domain']['domain_load_fromparent'] = array(
        '#type' => 'checkbox',
        '#title' => t('Load domain access options for terms from parent term'),
        '#description' => t('This option overrides the domain access options below. It is not for saving: the inherit action is applied only when this form is submitted.'),
        '#default_value' => isset($term->tid) ? FALSE : TRUE,
        '#attributes' => array(
          'onclick' => 'jQuery(this).is(":checked") ? jQuery(\'#' . $dt_edit_id . '\').hide():jQuery(\'#' . $dt_edit_id . '\').show();',
        ),
      );
      $form['domain']['domain_site'] = array(
        '#type' => 'checkbox',
        '#prefix' => '<div id="' . $dt_edit_id . '" ' . (!isset($term->tid) ? 'style="display:none;"' : '') . '><strong>' . t('Publishing options') . ':</strong>',
        '#title' => t('Send to all affiliates'),
        '#required' => FALSE,
        '#description' => t('Select if this term can be shown to all affiliates. This setting will override the options below.'),
        '#default_value' => isset($term->tid) ? $term->domain_site : variable_get('domain_share_voc_' . $vocabulary->vid, $behavior),
      );
      $form['domain']['domains'] = array(
        '#type' => 'checkboxes',
        '#title' => t('Publish to'),
        '#options' => $options,
        '#required' => TRUE,
        '#description' => t('Select which affiliates can access this term.'),
        '#default_value' => isset($term->tid) ? $term->domains : $default,
      );
      $form['domain']['domain_source'] = array(
        '#type' => 'select',
        '#suffix' => '</div>',
        '#title' => t('Term source domain'),
        '#options' => $source_options,
        '#default_value' => isset($term->tid) ? $term->domain_source : $_domain['domain_id'],
        '#description' => t('This affiliate will be used to write links to this term from other affiliates, as needed.'),
      );
    }
    else {
      $form['domain']['domain_load_fromparent'] = array(
        '#type' => 'value',
        '#value' => $term->tid ? false : true,
      );
      if (user_access('view domain publishing')) {

        // hook_user() has not run, so get the domain data for this user.
        $user->domain_user = domain_get_user_domains($user);
        $action = variable_get('domain_options', 0);
        $user_domains = array();
        $default_options = array();
        $user_options = array();
        if (!empty($user->domain_user)) {
          foreach ($user->domain_user as $key => $value) {
            if (abs($value) > 0) {
              $user_domains[] = $value;
            }
          }
          $first_domain = current($user_domains);
          foreach ($options as $key => $value) {
            if (in_array($key, $user_domains)) {
              $user_options[$key] = $value;
            }
          }
        }

        // Raw data checks for published term.
        foreach ($raw as $key => $value) {
          if (in_array($value, $user_domains)) {
            $default_options[] = $value;
          }
          else {
            $raw_options[] = $value;
          }
        }

        // Act on the behavior desired by the site admin.
        switch ($action) {

          // 1 == go to the default domain.
          case 1:
            $root = domain_default();
            if ($root['domain_id'] != $_domain['domain_id']) {
              domain_goto($root);
            }
            break;

          // 2 == go to the user's assigned domain.
          case 2:
            $domain = domain_lookup($first_domain);

            // If the domain is invalid, go to the primary domain.
            if ($domain == -1 || $domain['valid'] == 0) {
              domain_goto(domain_default());
            }
            elseif ($domain['domain_id'] != $_domain['domain_id']) {
              domain_goto($domain);
            }
            break;

          // 3 == show checkboxes of available domains.
          case 3:

            // If the user has no available domains, then they cannot post.
            if (empty($user_options)) {
              $form = array();
              return drupal_access_denied();
            }
            $form['domain'] = array(
              '#type' => 'fieldset',
              '#title' => t('Affiliate publishing options'),
              '#collapsible' => TRUE,
              '#collapsed' => FALSE,
              '#weight' => $weight,
            );

            // We must preserve publishing options that the user cannot access, but only for
            // existing terms.
            if ($term->tid) {
              $raw = $raw_options;
            }
            else {
              $raw = array();
            }

            // If the raw options are being passed, then no input is technically required.
            empty($raw) ? $required = TRUE : ($required = FALSE);
            $form['domain']['domains'] = array(
              '#type' => 'checkboxes',
              '#title' => t('Publish to'),
              '#options' => $user_options,
              '#required' => $required,
              '#description' => t('Select which affiliates can access this term.'),
              '#default_value' => isset($term->tid) ? $term->domains : $default_options,
            );

            // Show the options that cannot be changed.
            $list = array();
            if ($term->domain_site) {
              $list[]['data'] = t('All affiliates');
            }
            if (!empty($raw)) {
              foreach ($raw as $did) {
                $did == -1 ? $id = 0 : ($id = $did);
                $raw_domains = domain_lookup($id);
                $list[]['data'] = $raw_domains['sitename'];
              }
            }
            if (!empty($list)) {
              $form['domain']['domains_notes'] = array(
                '#value' => '<label><strong>' . t('Publishing status') . ':</strong></label>' . theme('item_list', $list) . '<div class="description">' . t('This term has also been published to these affiliates.') . '</div>',
              );
            }
            break;
        }
      }

      // These form elements are hidden from non-privileged users, by design.
      $form['domain_site'] = array(
        '#type' => 'value',
        '#value' => isset($term->tid) ? $term->domain_site : variable_get('domain_share_voc_' . $vocabulary->vid, $behavior),
      );

      // Domains that have been assigned and cannot be changed.
      $form['domains_raw'] = array(
        '#type' => 'value',
        '#value' => $raw,
      );
    }
  }
}