You are here

function kwresearch_form_node_form_alter in Keyword Research 7

File

./kwresearch.module, line 1212

Code

function kwresearch_form_node_form_alter(&$form, $form_state, $form_id) {

  //if (isset($form['type']['#value']) && ($form['type']['#value'] . '_node_form' == $form_id)) {
  if (1) {
    $node = $form['#node'];
    require_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'kwresearch') . "/includes/tax_report.inc";
    if (!empty($_GET['kwresearch_page_kid'])) {
      $kid0 = $_GET['kwresearch_page_kid'];
      $kw0 = kwresearch_get_keyword($kid0);
      if (isset($form['contentanalysis']) && isset($form['contentanalysis']['seo']) && isset($form['contentanalysis']['seo']['keyword']) && !$form['contentanalysis']['seo']['keyword']['#default_value']) {
        $form['contentanalysis']['seo']['keyword']['#default_value'] = $kw0;
      }
    }
    drupal_add_css(drupal_get_path('module', 'kwresearch') . '/kwresearch.css');
    drupal_add_js(drupal_get_path('module', 'kwresearch') . '/kwresearch.js');

    // drupal_add_js(drupal_get_path('module', 'kwresearch') . '/kwresearch.verticle-tabs.js');
    $report_vocabs = variable_get('kwresearch_report_vocabulary', array());
    $sync_vocab = variable_get('kwresearch_keyword_sync_vocabulary', '');
    $weight = 0;
    $img = base_path() . '/' . drupal_get_path('module', 'contentanalysis') . '/icons/refresh.png';
    $js = array(
      'kwresearch' => array(
        'tax_report_callback' => base_path() . 'kwresearch/keyword_tax_report_js',
        'tax_report_vocabs' => array(
          drupal_clean_css_identifier($sync_vocab),
        ),
        'base_path' => base_path(),
        'path_to_module' => base_path() . drupal_get_path('module', 'kwresearch'),
        'post_token' => drupal_get_token('kwresearch'),
      ),
    );
    foreach ($form as $id => $field) {

      // look for fields
      if (substr($id, 0, 6) == 'field_' && $field['#attributes']['class'][0] == 'field-type-taxonomy-term-reference') {
        $fi = field_info_field($id);

        // Get field language.
        $lang = $form[$id]['#language'];
        if ($fi['settings']['allowed_values'][0]['vocabulary'] == $sync_vocab) {
          $terms = array();
          $ts = explode(',', $field[$lang]['#default_value']);
          foreach ($ts as $term) {
            $t = trim(strtolower($term));
            $terms[$t] = $t;
          }
          $page_keywords = kwresearch_load_page_keywords_by_page($form['nid']['#value']);
          foreach ($page_keywords as $keyword_obj) {
            if (empty($terms[$keyword_obj->keyword])) {
              $form[$id][$lang]['#default_value'] .= ($form[$id][$lang]['#default_value'] ? ', ' : '') . $keyword_obj->keyword;
            }
          }
          if (empty($form[$id][$lang]['#default_value']) && !empty($kw0)) {
            $form[$id][$lang]['#default_value'] = $kw0;
          }
          $keywords = $form[$id][$lang]['#default_value'];

          // move field to additional settings group
          $form['kwresearch'] = array(
            '#type' => 'fieldset',
            '#title' => t('Keywords'),
            '#colapsible' => TRUE,
            '#collapsed' => FALSE,
            '#group' => 'additional_settings',
            '#weight' => -89,
          );
          $form['kwresearch'][$id] = $form[$id];
          $form['kwresearch'][$id]['#weight'] = -1;
          unset($form[$id]);
          $form['kwresearch']['kwresearch_sync_vocab'] = array(
            '#type' => 'value',
            '#value' => $id,
          );
          $form['kwresearch'][$id]['report'] = array(
            '#type' => 'item',
            '#title' => t('Keyword report'),
            '#markup' => kwresearch_keywords_tax_report($keywords),
            '#prefix' => '<div id="kwresearch-tax-report-' . drupal_clean_css_identifier($sync_vocab) . '" class="kwresearch-tax-report kwresearch-tax-report-' . drupal_clean_css_identifier($sync_vocab) . '">',
            '#suffix' => '</div>',
            '#weight' => 1,
          );
          $js['kwresearch']['sync_vocab_field'] = drupal_clean_css_identifier("{$id}-" . $lang);

          // Only support vertical tabs if there is a vertical tab element.
          foreach (element_children($form) as $key) {
            if (isset($form[$key]['#type']) && $form[$key]['#type'] == 'vertical_tabs') {
              $form['kwresearch']['#group'] = $key;
              $form['kwresearch']['#attached']['js']['vertical-tabs'] = drupal_get_path('module', 'kwresearch') . '/kwresearch.vertical-tabs.js';
              break;
            }
          }
        }

        /*
        $js = array(
          'kwresearch' => array(
            'tax_report_callback' => base_path() . 'kwresearch/keyword_tax_report_js',
            'tax_report_vocabs' => array(drupal_clean_css_identifier($sync_vocab)),
            'base_path' => base_path(),
            'path_to_module' => base_path() . drupal_get_path('module', 'kwresearch'),
            'sync_vocab_field' => drupal_clean_css_identifier("$id-" . $node->language),
          )
        );
        */
      }
    }
    drupal_add_js($js, array(
      'type' => 'setting',
      'scope' => JS_DEFAULT,
    ));
    return;

    //******************

    // TODO implement tax reports from D6 version
    if (isset($form['taxonomy']['tags']) && is_array($form['taxonomy']['tags'])) {
      foreach ($form['taxonomy']['tags'] as $vid => $tf) {
        $form['taxonomy']['tags'][$vid]['#weight'] = $weight++;
        if ($report_vocabs[$vid]) {
          $keywords = $tf['#default_value'];
          $form['taxonomy']['tags']['kwresearch-tax-report-' . $vid] = array(
            '#type' => 'item',
            '#title' => t('Keyword report'),
            '#markup' => kwresearch_keywords_tax_report($keywords),
            '#prefix' => '<div id="kwresearch-tax-report-' . $vid . '" class="kwresearch-tax-report kwresearch-tax-report-' . $vid . '">',
            '#suffix' => '</div>',
            '#weight' => $weight++,
          );
        }

        // check if any keywords have been added that is not in sync vocab
        if ($form['nid']['#value']) {
          if ($sync_vocab == $vid) {
            $terms = array();
            $ts = explode(',', $form['taxonomy']['tags'][$vid]['#default_value']);
            foreach ($ts as $term) {
              $t = trim($term);
              $terms[$t] = $t;
            }
            $page_keywords = kwresearch_load_page_keywords_by_page($form['nid']['#value']);
            foreach ($page_keywords as $keyword_obj) {
              if (!$terms[$keyword_obj->keyword]) {
                $form['taxonomy']['tags'][$vid]['#default_value'] .= ($form['taxonomy']['tags'][$vid]['#default_value'] ? ', ' : '') . $keyword_obj->keyword;
              }
            }
          }
        }
      }
    }
    drupal_add_js(array(
      'kwresearch' => array(
        'tax_report_callback' => base_path() . 'kwresearch/keyword_tax_report_js',
        'tax_report_vocabs' => $report_vocabs,
        'base_path' => base_path(),
        'path_to_module' => base_path() . drupal_get_path('module', 'kwresearch'),
        'post_token' => drupal_get_token('kwresearch'),
      ),
    ), array(
      'type' => 'setting',
      'scope' => JS_DEFAULT,
    ));
  }
}