<?php
// $Id: kwresearch.module,v 1.1.2.16 2010/10/01 16:12:37 tomdude48 Exp $

/**
 * @file
 */

define('KWANALYSIS_CACHE_TIMEOUT', 1728000); // time in secs between when module should check for new data.

require_once './' . drupal_get_path('module', 'kwresearch') . "/includes/stats.inc";
require_once './' . drupal_get_path('module', 'kwresearch') . "/includes/contentanalysis.inc";
 
/**
 * Implementation of hook_menu().
 */
function kwresearch_menu() {
  $items = array();
  $items['admin/content/kwresearch'] = array(
    'title' => t('Keyword research'),
    'page callback' => 'kwresearch_keywords_list_page',
    'access callback' => 'user_access',
    'access arguments' => array('kwresearch admin site keywords'),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'includes/site_report.inc',
  );
  $items['admin/content/kwresearch/keyword_list'] = array(
    'title' => t('Keyword lists'),
    'page callback' => 'kwresearch_keywords_list_page',
    'access callback' => 'user_access',
    'access arguments' => array('kwresearch admin site keywords'),    
    'file' => 'includes/site_report.inc',
    'weight' => -8,
    'type' => MENU_LOCAL_TASK,
  );
  $items['admin/content/kwresearch/keyword_list/all'] = array(
    'title' => t('All keywords'),
    'page callback' => 'kwresearch_keywords_list_page',
    'access callback' => 'user_access',
    'access arguments' => array('kwresearch admin site keywords'),
    'file' => 'includes/site_report.inc',
    'weight' => -10,
    'type' => MENU_DEFAULT_LOCAL_TASK,
  );
  $items['admin/content/kwresearch/keyword_list/site'] = array(
    'title' => t('Site keywords'),
    'page callback' => 'kwresearch_keywords_list_page',
    'page arguments' => array('site'),
    'access callback' => 'user_access',
    'access arguments' => array('kwresearch admin site keywords'),
    'file' => 'includes/site_report.inc',
    'weight' => -8,
    'type' => MENU_LOCAL_TASK,
  );
  $items['admin/content/kwresearch/keyword_list/page'] = array(
    'title' => t('Page keywords'),
    'page callback' => 'kwresearch_keywords_list_page',
    'page arguments' => array('page'),
    'access callback' => 'user_access',
    'access arguments' => array('kwresearch admin site keywords'),
    'file' => 'includes/site_report.inc',
    'weight' => -6,
    'type' => MENU_LOCAL_TASK,
  );
  $items['admin/content/kwresearch/keyword_report'] = array(
    'title' => t('Keyword stats'),
    'page callback' => 'kwresearch_keyword_stats_report_page',
    'access callback' => 'user_access',
    'access arguments' => array('access content'),
    'type' => MENU_LOCAL_TASK,
    'weight' => -6,
    'file' => 'includes/stats_report.inc',
  );
  $items['admin/content/kwresearch/keyword_list/edit'] = array(
    'title' => t('Add keyword'),
    'page callback' => 'kwresearch_keywords_list_edit',
    'access callback' => 'user_access',
    'access arguments' => array('kwresearch admin site keywords'),
    'type' => MENU_LOCAL_TASK,
    'file' => 'kwresearch.admin.inc',
  );
  $items['admin/content/kwresearch/keyword_list/import'] = array(
    'title' => t('Import  keywords'),
    'page callback' => 'kwresearch_site_keywords_import',
    'access callback' => 'user_access',
    'access arguments' => array('kwresearch admin site keywords'),
    'type' => MENU_LOCAL_TASK,
    'file' => 'kwresearch.admin.inc',
  );
  $items['admin/content/kwresearch/keyword_pages'] = array(
    'title' => t('Page keyword list'),
    'page callback' => 'kwresearch_keyword_pages_page',
    'access callback' => 'user_access',
    'access arguments' => array('kwresearch admin page keywords'),
    'type' => MENU_CALLBACK,
    'file' => 'kwresearch.admin.inc',
  );
  $items['admin/content/kwresearch/page_keywords'] = array(
    'title' => t('Page keyword list'),
    'page callback' => 'kwresearch_page_keywords_page',
    'access callback' => 'user_access',
    'access arguments' => array('kwresearch admin page keywords'),
    'type' => MENU_CALLBACK,
    'file' => 'includes/page_report.inc',
  );
  $items['node/%node/kwresearch'] = array(
    'title' => 'Keywords',
    'page callback' => 'kwresearch_page_keywords_page',
    'page arguments' => array(1, 'node'),
    'access callback' => 'user_access',
    'access arguments' => array('kwresearch admin page keywords'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 50,
    'file' => 'includes/page_report.inc',
  );
  $items['admin/content/kwresearch/page_keywords_edit'] = array(
    'title' => t('Add page keyword'),
    'page callback' => 'kwresearch_page_keywords_edit',
    'access callback' => 'user_access',
    'access arguments' => array('kwresearch admin page keywords'),
    'type' => MENU_CALLBACK,
    'file' => 'kwresearch.admin.inc',
  );
  $items['admin/content/kwresearch/clear_keywords'] = array(
    'title' => t('Clear Keyword Reserach keywords'),
    'page callback' => 'kwresearch_clear_keywords',
    'access callback' => 'user_access',
    'access arguments' => array('kwresearch admin page keywords'),
    'type' => MENU_CALLBACK,
    'file' => 'kwresearch.admin.inc',
  );
  $items['admin/settings/kwresearch'] = array(
    'title' => t('Keyword research'),
    'description' => 'Analize keywords for search optimization.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('kwresearch_admin_settings'),
    'access callback' => 'user_access',
    'access arguments' => array('administer page titles'),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'kwresearch.admin.inc',
  );
  $items['kwresearch/keyword_tax_report_js'] = array(
    'title' => '',
    'page callback' => 'kwresearch_keywords_tax_report_js',
    'access callback' => 'user_access',
    'access arguments' => array('perform content analysis'),
    'type' => MENU_CALLBACK,
    'file' => 'includes/tax_report.inc',
  );
  $items['kwresearch/analyze_js'] = array(
    'title' => '',
    'page callback' => 'kwresearch_analyze_js',
    'access callback' => 'user_access',
    'access arguments' => array('perform content analysis'),
    'type' => MENU_CALLBACK,
  );
  $items['kwresearch/toggle_site_keyword_js'] = array(
    'title' => '',
    'page callback' => 'kwresearch_toggle_site_keyword_js',
    'access callback' => 'user_access',
    'access arguments' => array('kwresearch admin site keywords'),
    'type' => MENU_CALLBACK,
  );
  $items['kwresearch/toggle_page_keyword_js'] = array(
    'title' => '',
    'page callback' => 'kwresearch_toggle_page_keyword_js',
    'access callback' => 'user_access',
    'access arguments' => array('kwresearch admin page keywords'),
    'type' => MENU_CALLBACK,
  );
  $items['kwresearch/delete_site_keyword_js'] = array(
    'title' => '',
    'page callback' => 'kwresearch_delete_site_keyword_js',
    'access callback' => 'user_access',
    'access arguments' => array('kwresearch admin site keywords'),
    'type' => MENU_CALLBACK,
  );
  
  return $items;
}

/**
 * Implementation of hook_perm()
 */
function kwresearch_perm() {
  return array('kwresearch admin site keywords', 'kwresearch admin page keywords');
}

/**
 * Saves site keyword user settings data to database
 * 
 * @param str $keyword
 * @param int $kid
 * @param keyword stdClass $keyword_obj - use to set priority, value or uid. 
 * @param boolean $update_stats - set to true if you want to refresh the stats for the keyword
 */
function kwresearch_save_site_keyword($keyword, $kid = NULL, $keyword_obj = NULL, $update_stats = FALSE) {
  $keyword = strtolower($keyword);
  $key = ($kid) ? $kid : $keyword;
  $keyword_obj0 = kwresearch_load_site_keyword($key);
  if (!$kid && $keyword_obj0->kid) {
    $kid = $keyword_obj0->kid;
  }
  if (!$keyword_obj->uid) {
    $keyword_obj->uid = ($keyword_obj0->uid)? $keyword_obj0->uid : $GLOBALS['user']->uid; // set logged in user as default
  } 
  if (is_null($keyword_obj->priority)) {
    $keyword_obj->priority = ($keyword_obj0->priority)? $keyword_obj0->priority : -1; // set default to standard
  }
  if (!$keyword_obj->value) {
    $keyword_obj->value = ($keyword_obj0->value)? $keyword_obj0->value : -1; // set to default value to 0
  }
  if (!$kid) {
    $sql = '
      INSERT INTO {kwresearch_keyword}
      (uid, keyword, word_count, priority, value)
      VALUES (%d, "%s", %d, %d, %d)      
    ';
    db_query($sql, $keyword_obj->uid, $keyword, str_word_count($keyword), $keyword_obj->priority, $keyword_obj->value);
    $kid = db_last_insert_id('kwresearch_keywords', 'kid');
  }
  else {
    $sql = '
      UPDATE {kwresearch_keyword}
      SET 
        priority = %d,
        value = %f,
        uid = %d
      WHERE kid = %d
    ';
    db_query($sql, $keyword_obj->priority, $keyword_obj->value, $keyword_obj->uid, $kid);  
  }
  
  // update keyword stats if set
  if ($update_stats) {    
    $kw_stats = kwresearch_get_keyword_stats_data($keyword, $msgs);
    if ($msgs) {
      foreach ($msgs AS $msg) {
        drupal_set_message($msg['value'], $msg['status']);
      }
    }
  }
  
  return $kid;  
}

/**
 * Deletes site keyword from db
 * @param int|string $key if int, treated as a kid. If str treated as a keyword phrase
 */
function kwresearch_delete_site_keyword($key) {
  if (is_numeric($key)) {
    $kid = $key;
  }
  else {
    $keyword_obj = kwresearch_load_site_keyword($key);
    if ($keyword_obj->kid) {
      $kid = $keyword_obj->kid;
    }
    else {
      return FALSE;
    }  
  }
  $sql = '
    DELETE FROM {kwresearch_keyword}
      WHERE kid = %d
  ';
  db_query($sql, $kid);  
  return TRUE;
}

/**
 * Deletes all site keyword db entries if not set as a site keyword or used as a page keyword
 */
function kwresearch_clear_site_keywords() {
  $sql = '
    DELETE FROM {kwresearch_keyword}
    WHERE page_count = 0
      AND (priority IS NULL || priority = 0) 
  ';
  db_query($sql);
  drupal_set_message(t('Unused keywords have been cleared.'));
  return t('OK');
}

/**
 * Loads site keyword object from databased
 * @param int|string $key if int, treated as a kid. If str treated as a keyword phrase
 */
function kwresearch_load_site_keyword($key) {
  if ($key > 0) {  // load by kid
    $sql = '
      SELECT * 
      FROM {kwresearch_keyword}
      WHERE kid = %d
    ';
    $keyword_obj = db_fetch_object(db_query($sql, $key));       
  }
  else { // load by keyword
    $sql = '
      SELECT * 
      FROM {kwresearch_keyword}
      WHERE keyword = "%s"
    ';
    $keyword_obj = db_fetch_object(db_query($sql, $key));      
  }
  return $keyword_obj;
}

/**
 * Returns the kid for a given keyworld
 * @param unknown_type $keyword
 */
function kwresearch_get_kid($keyword) {
  $keyword_obj = kwresearch_load_site_keyword($keyword);
  return $keyword_obj->kid;
}

/**
 * Returns the keyword phrase for a given kid
 * @param $kid
 */
function kwresearch_get_keyword($kid) {
  $keyword_obj = kwresearch_load_site_keyword($kid);
  return $keyword_obj->keyword;
}

/**
 * Saves a page keyword. Links a site keyword to a node/page
 * 
 * @param int|str $pid - If int, treated as a nid. If string treated as a path
 * @param str $keyword
 * @param int $kid 
 * @param stdClass $page_keyword_obj - object containing parameters
 * @param bool $update_stats - if true, will generate and save keyword stats data
 * @param bool $save_term - if true, will also call save_site_keyword
 */
function kwresearch_save_page_keyword($pid, $keyword, $kid = NULL, $page_keyword_obj = NULL, $update_stats = FALSE, $save_term = FALSE) {
//kwresearch_save_site_keyword($keyword, $kid = NULL, $keyword_obj = NULL, $update_stats = FALSE)
  if (!is_object($page_keyword_obj)) {
    $page_keyword_obj = new stdClass();
  }
  $keyword = strtolower($keyword);
  $key = ($kid) ? $kid : $keyword;
  $keyword_obj0 = kwresearch_load_site_keyword($key);

  // if site keyword does not exist, create it
  if (!$keyword_obj0->kid) {
    $kid = kwresearch_save_site_keyword($keyword);
    $keyword_obj0 = kwresearch_load_site_keyword($kid);    
  }

  $kid = $keyword_obj0->kid;
  
  $page_keyword_obj0 = kwresearch_load_page_keyword($pid, $kid);  

  if (!$page_keyword_obj->uid) {
    $page_keyword_obj->uid = ($page_keyword_obj0->uid)? $page_keyword_obj0->uid : $GLOBALS['user']->uid; // set logged in user as default
  } 
  if (!is_numeric($page_keyword_obj->priority)) {
    $page_keyword_obj->priority = ($page_keyword_obj0->priority)? $page_keyword_obj0->priority : -1; // set default to standard
  }

  // page keyword does not exists, create it
  if (!$page_keyword_obj0->kid) {
    if ($pid > 0) { // insert as node
      $sql = '
        INSERT INTO {kwresearch_page_keyword}
        (kid, nid, priority, uid)
        VALUES (%d, %d,  %d, %d)
      ';   
      db_query($sql, $kid, $pid, $page_keyword_obj->priority, $page_keyword_obj->uid);    
    } 
    else {
      $sql = '
        INSERT INTO {kwresearch_page_keyword}
        (kid, path, priority, uid)
        VALUES (%d, "%s", %d, %d)
      '; 
      db_query($sql, $kid, $pid, $page_keyword_obj->priority, $page_keyword_obj->uid);  
    }
  }
  else {
    // if priority > 0 update table, otherwise delete record
    $where = ($pid > 0)? 'nid=%d' : 'path="%s"';
    if ($page_keyword_obj->priority) {      
      $sql = '
        UPDATE {kwresearch_page_keyword}
        SET priority = %d
        WHERE kid = %d
          AND ' . $where . '
      ';
      db_query($sql, $page_keyword_obj->priority, $kid, $pid);
    }
    else {
      kwresearch_delete_page_keyword($kid, $pid);
    }
  } 
  kwresearch_update_keyword_page_counts($kid); 
  return $kid;
}

/**
 * Deletes site keyword from db
 * @param int|string $key if int, treated as a kid. If str treated as a keyword phrase
 */
function kwresearch_delete_page_keyword($key, $pid) {
  if (is_numeric($key)) {
    $kid = $key;
  }
  else {
    $keyword_obj = kwresearch_load_page_keyword($key);
    if ($keyword_obj->kid) {
      $kid = $keyword_obj->kid;
    }
    else {
      return FALSE;
    }  
  }
  $where = ($pid > 0)? 'nid=%d' : 'path="%s"';
  $sql = "
    DELETE FROM {kwresearch_page_keyword}
    WHERE 
      kid = %d
      AND $where
  ";
  db_query($sql, $kid, $pid); 
  return TRUE;
}

/**
 * Deletes all the page keyword for a given page
 * @param int|str $pid - If int, treated as a nid. If string treated as a path
 */
function kwresearch_delete_page_keywords_by_page($pid) {
  $page_keywords = kwresearch_load_page_keywords_by_page($pid);
  
  $where = ($pid > 0)? 'nid=%d' : 'path="%s"';
  $sql = "
    DELETE FROM {kwresearch_page_keyword}
    WHERE 
      $where
  ";
  db_query($sql, $pid);
  
  foreach ($page_keywords AS $page_keyword) {
    kwresearch_update_keyword_page_counts($page_keyword->kid);
  }
}

/**
 * Loads page keyword object from database
 * 
 * @param int|str $pid if int, treated as a node id. If str treated as a path.
 * @param int|str $key if int, treated as a kid. If str treated as a keyword phrase
 */
function kwresearch_load_page_keyword($pid, $key) {
  $site_keyword_obj = kwresearch_load_site_keyword($key);
  $kid = $site_keyword_obj->kid; 
  
  $where = ($pid > 0)? 'nid=%d' : 'path="%s"';
  
  $sql = "
    SELECT * 
    FROM {kwresearch_page_keyword}
    WHERE kid = %d
      AND $where
  ";

  $page_keyword_obj = db_fetch_object(db_query($sql, $kid, $pid));       

  $page_keyword_obj->site = $site_keyword_obj;
  return $page_keyword_obj;
}

/**
 * Loads an array of page keyword objects of all pages associated with a specific keyword
 * @param int|str $key Int treated as kid, str as keyword phrase
 */
function kwresearch_load_page_keywords_by_keyword($key) {
  if (!is_numeric($key) || $key <= 0) {
    $keyword_obj = kwresearch_load_site_keyword($key);
    $kid = $keyword_obj->kid;  
  }
  else {
    $kid = $key;
  }
  $sql = '
    SELECT * 
    FROM {kwreserach_page_keyword}
    WHERE kid = %d
  ';
  $result = db_query($sql, $kid);
  $page_keywords = array();
  while ($row = db_fetch_object($result)) {
    $page_keywords[] = $row;
  }
  return $page_keywords;
}

/**
 * Loads an array of page keyword objects associated with a specific page
 * @param int|str $key Int treated as kid, str as keyword phrase
 */
function kwresearch_load_page_keywords_by_page($pid) {
  
  $where = ($pid > 0)? 'nid=%d' : 'path="%s"';
  
  $sql = "
    SELECT pk.*, k.keyword, k.priority AS site_priority, k.value AS value 
    FROM {kwresearch_page_keyword} pk
    JOIN {kwresearch_keyword} k ON pk.kid = k.kid
    WHERE $where
  ";
  $result = db_query($sql, $pid);  
  $page_keywords = array();
  while ($row = db_fetch_object($result)) {
    $page_keywords[] = $row;
  }
  return $page_keywords;
}

/**
 * Updates the page_count field in site keyword table
 * @param unknown_type $kid
 */
function kwresearch_update_keyword_page_counts($kid) {
  $sql = '
    SELECT COUNT(*)
    FROM {kwresearch_page_keyword}
    WHERE kid = %d
  ';
  $count = db_result(db_query($sql, $kid));
  $sql = '
    UPDATE {kwresearch_keyword}
    SET page_count = %d
    WHERE kid = %d
  ';
  db_query($sql, $count, $kid);
  return $count;
}

/**
 * returns array of priority options
 */
function kwresearch_get_priority_options() {
  return array(
    0 => t('None'),
    10 => t('Very low'),
    30 => t('Low'),
    50 => t('Standard'),
    70 => t('High'),
    90 => t('Top'),
  );
}



/**
 * AJAX handler to enable site keyword priority to be set
 */
function kwresearch_toggle_site_keyword_js() {
  GLOBAL $user;
  
  $keyword = $_POST['kwresearch_keyword'];
  $priority = $_POST['priority'];
    
  $keyword_obj = new stdClass();
  $keyword = strtolower($_POST['kwresearch_keyword']);
  $keyword_obj->priority = $_POST['priority'];
  
  $kid = kwresearch_save_site_keyword($keyword, NULL, $keyword_obj);
  
  $keyword_obj = kwresearch_load_site_keyword($kid);

  if ($user->uid != $keyword_obj->uid) {
    $theuser = user_load($keyword_obj->uid);
  } 
  else {
    $theuser = $user;  
  }
  
  $site_priority_options = kwresearch_get_priority_options();
  $output = array();
  $output['data'] = array(
    'kid' => (int)$keyword_obj->kid,
    'keyword' => $keyword,
    'priority' => (int)$keyword_obj->priority,
    'priority_out' => ($keyword_obj->priority) ? $site_priority_options[$keyword_obj->priority] : '-',
    'value' => (float)$keyword_obj->value,
    'value_out' => ($keyword_obj->value >= 0) ? t('$') . number_format($keyword_obj->value, 2) : '-',
    'user_out' => ($theuser->uid) ? l($theuser->name, 'user/' . $theuser->uid) : '-',
  );
  
  drupal_json($output);  
}

/**
 * AJAX handler to delete a site keyword from db 
 */
function kwresearch_delete_site_keyword_js() {
  
  $keyword = $_POST['kwresearch_keyword'];
  $kid = $_POST['kid'];
  
  $deleted = kwresearch_delete_site_keyword($kid);
    
  $site_priority_options = kwresearch_get_priority_options();
  $output = array();
  $output['data'] = array(
    'kid' => (int)$kid,
    'keyword' => $keyword,
    'deleted' => (boolean)$deleted,
  );
  
  drupal_json($output);  
}

/**
 * AJAX handler to create, delete and set priorities for page keywords
 */
function kwresearch_toggle_page_keyword_js() {
  
  $keyword = strtolower($_POST['kwresearch_keyword']);
  $nid = $_POST['nid'];
  $path = $_POST['path'];
  $pid = (!$nid) ? $path : $nid;

  $page_keyword_obj = new stdClass();
  $page_keyword_obj->priority = $_POST['priority'];

  kwresearch_save_page_keyword($pid, $keyword, NULL, $page_keyword_obj);
  
  $output = array();
  $output['data'] = array(
    'keyword' => $keyword,
    'priority' => (int)$page_keyword_obj->priority,
  );
  
  drupal_json($output);  
}

/**
 * AJAX handler to generate keyword stats report for content analysis display
 */
function kwresearch_analyze_js() {
  require_once './' . drupal_get_path('module', 'kwresearch') . "/includes/stats_report.inc";
  
  $analysis['inputs'] = array(
    'keyword' => $_POST['kwresearch_keyword'],
    'kwresearch_include_misspellings' => $_POST['kwresearch_include_misspellings'],
    'kwresearch_include_plurals' => $_POST['kwresearch_include_plurals'],
    'kwresearch_adult_filter' => $_POST['kwresearch_adult_filter'],
    'nid' => $_POST['kwresearch_nid']
  );
  
  if ($analysis['inputs']['nid'] == -1) {
    $analysis['inputs']['nid'] = NULL;
  }
  
  $params = array(
    'wordtracker' => array(
      'type' => 'all',
      'include_misspellings' => $analysis['inputs']['kwresearch_include_misspellings'],
      'include_plurals' => $analysis['inputs']['kwresearch_include_plurals'],
      'kwresearch_adult_filter' => $analysis['inputs']['kwresearch_adult_filter'],
    ),
    'pid' => $analysis['inputs']['nid'],
  );

  $analysis['analysis'] = kwresearch_get_keyword_stats_data($analysis['inputs']['keyword'], $msgs, $params);
  if (!empty($msgs)) {
    $analysis['messages'] = $msgs;
  }
  $output = array();
  $output['report']['data'] = $analysis;
  $output['report']['output'] = theme_keyword_stats_report($analysis);
  
  drupal_json($output);    
}

/**
 * Implementation of hook_nodeapi().
 * @param $node
 * @param $op
 * @param $a3
 * @param $a4
 */
function kwresearch_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  if ($op == 'insert' || $op == 'update') {
    static $vocab_filtered = FALSE;
    
    if (!$kw_vid = variable_get('kwresearch_keyword_sync_vocabulary', 0)) {
      return;
    }
    $processed = array();
    $page_keywords = kwresearch_load_page_keywords_by_page($node->nid); 
    foreach ($page_keywords AS $page_keyword) {
        $processed[$page_keyword->kid] = 0;
    }
    
    $terms_str = $node->taxonomy['tags'][$kw_vid];
    $terms = explode(',', $terms_str);
    $node_terms_str = '';
    $needs_filtering = 1;
    foreach ($terms AS $term) {      
      $page_priority = $site_priority = $site_value = FALSE;
      list($term, $args) = explode('[', $term);
      if (substr($args, -1) == ']') {
        $args = substr($args, 0, -1);
        list($page_priority, $site_priority, $site_value) = explode(';', $args);
      }
      $term = trim($term);
      if (!$page_keyword_obj) {
        $page_keyword_obj = new stdClass();
        
        if ($page_priority) {
          $page_keyword_obj->priority = $page_priority;
          $needs_filtering = 1;
        }
        else {
          $page_keyword_obj->priority = 50;
        }
      }
      $kid = kwresearch_save_page_keyword($node->nid, $term, NULL, $page_keyword_obj);
      $processed[$kid] = 1;
      $node_terms_str .= (($node_terms_str) ? ',':'') . $term;
    }
    // save filtered taxonomy
    if (!$vocab_filtered && $needs_filtering) {
      $vocab_filtered = TRUE;
      $node->taxonomy['tags'][$kw_vid] = $node_terms_str;
      node_save($node);
      
    }
    // delete keywords not submitted
    //$page_keywords = kwresearch_load_page_keywords_by_page($node->nid);
    $page_keyword_obj = new stdClass();
    $page_keyword_obj->priority = 0;
    foreach ($processed AS $kid => $v) {
      if (!$v) {
        kwresearch_delete_page_keyword($kid, $node->nid);
      }
    }
  }
  elseif ($op == 'delete') {
    kwresearch_delete_page_keywords_by_page($node->nid);
  }
}

/**
 * Implementation of hook_form_alter().
 *
 * Add Content Optimizer field set to node edit forms.
 * Add Content Analysis enabled field to content type admin form
 */
function kwresearch_form_alter(&$form, $form_state, $form_id) {
  if (isset($form['type']['#value']) && ($form['type']['#value'] .'_node_form' == $form_id)) {
//dsm(variable_get('kwresearch_report_vocabulary', ''));
    require_once './' . drupal_get_path('module', 'kwresearch') . "/includes/tax_report.inc";
    
    drupal_add_css(drupal_get_path('module', 'kwresearch') . '/kwresearch.css'); 
    drupal_add_js(drupal_get_path('module', 'kwresearch') . '/kwresearch.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';
    if(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'),    
            '#value' => 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($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'),
    )), 
  'setting');
  }
}

/**
 * Formats a message element
 * @param $message
 *   Text of the message
 * @param $type
 *   Status of the message. Values: ['status'|'complete'|'warning','error']
 * @return
 *   Message formated associative array
 */
function kwresearch_format_message($message, $type='status') {
  return array(
    'value' => $message,
    'status' => $type,
  );
}

/**
 * returns data options for reports
 * report: stats|site|page|tax
 */
function kwresearch_get_report_data_options($report = 'stats', $th = FALSE) {
  if($report == 'stats') {
    $options = array(
      'site_priority' => ($th)?t('Site priority'):t('Keyword\'s priority for the site'),
      'site_value' => ($th)?t('Site value'):t('Keyword\'s value for the site'),
      //'page_priority' => ($th)?t('Page priority'):t('Keyword\'s priority for the page'),
      'total_dailyest' => ($th)?t('Total daily'):t('Total daily estimated searches'),
      'competition' => ($th)?t('Competition'):t('Rating of competition for keyword'),
      'bid' => ($th)?t('Bid'):t('Recommended bid price for keyword'),
      'google_dailyest' => ($th)?t('Google daily'):t('Google daily estimated searches'),
      'yahoo_dailyest' => ($th)?t('Yahoo! daily'):t('Yahoo! daily estimated searches'),
      'bing_dailyest' => ($th)?t('Bing daily'):t('Bing daily estimated searches'),
    ); 
  }
  else if($report == 'site') {
    $options = array(
      'daily_volume' => ($th)?t('Total daily'):t('Total daily estimated searches'),
      'competition' => ($th)?t('Competition'):t('Rating of competition for keyword'),
      'bid' => ($th)?t('Bid'):t('Recommended bid price for keyword'),
      'google_dailyest' => ($th)?t('Google daily'):t('Google daily estimated searches'),
      'yahoo_dailyest' => ($th)?t('Yahoo! daily'):t('Yahoo! daily estimated searches'),
      'bing_dailyest' => ($th)?t('Bing daily'):t('Bing daily estimated searches'),
    );    
  }
  // make api calls
  $sources = module_invoke_all('kwresearch_sources');
  if (is_array($sources)) {
    foreach ($sources AS $aid => $source) {
      if(is_array($source[$report . '_report_columns'])) {
        foreach ($source[$report . '_report_columns'] AS $k => $v) {
          $options[$k] = $v;
        }
      }
    }  
  }
  return $options; 
}

function kwresearch_format_report_data_value($values, $keyword='', $type = 'term') {
  switch ($type) {
    case 'term':
      $output = '<span class="kwresearch_keyword">' . $values['term'] . '</div>';
      break;
    //case 'wordtracker_count':
    //  $output = ($values['wordtracker_count']=='NA') ? 'NA': number_format($values['wordtracker_count']);
    //  break;
    
    case 'site_priority':
      $output = '-';
      if ($values['priority'] > 0) {
        $options = kwresearch_get_priority_options();
        $output = $options[$values['priority']];
      }
      break;
    case 'site_value':
      $output = ($values['value'] > 0) ? t('$') . number_format($values['value'], 2) : '-';
      $type = 'value';
      break;
    case 'page_priority':
      $output = '-';
      if ($values['page_priority'] > 0) {
        $options = kwresearch_get_priority_options();
        $output = $options[$values['page_priority']];
      }
      break;
    case 'daily_volume':
    case 'total_dailyest':
      $coefs = kwresearch_get_report_coefs();
      if (is_null($values['_searches']) || ($values['_searches'] < 0)) {
        $output = 'NA';
      }
      else {   
        $output = '<div class="stats-indicator">';
        $output .= '  <div class="searches-indicator-bar" style="width: ' . (100 * $values['_searches'] / $values['_meta']['searches_max']) . '%;" title="' . number_format($values['_competition']) . '">';
        $output .= '    <div class="stats-indicator-text">' . number_format($values['_searches']) . '</div>';
        $output .= '  </div>';
        $output .= '</div>';
      }
      break;
    case 'competition':
      if (is_null($values['_competition']) || ($values['_competition'] < 0)) {
        $output = 'NA';
      }
      else {
        $output = '<div class="stats-indicator">';
        $output .= '  <div class="competition-indicator-bar" style="width: ' . $values['_competition'] . '%;" title="' . number_format($values['_competition']) . '">';
        $output .= '    <div class="stats-indicator-text">' . number_format($values['_competition']) . '%</div>';
        $output .= '  </div>';
        $output .= '</div>';
      }
      break;
    case 'bid':
      $output = ($values['_bid'] > 0) ? t('$') . number_format($values['_bid'], 2) : '-';
      if (is_null($values['_bid']) || ($values['_bid'] < 0)) {
        $output = 'NA';
      }
      elseif ($values['_bid'] == 0) {
        $output = '-';
      }
      else {   
        $output = '<div class="stats-indicator">';
        $output .= '  <div class="bid-indicator-bar" style="width: ' . (100 * $values['_bid'] / $values['_meta']['bid_max']) . '%;" title="' . number_format($values['_competition']) . '">';
        $output .= '    <div class="stats-indicator-text">' . t('$') . number_format($values['_bid'], 2) . '</div>';
        $output .= '  </div>';
        $output .= '</div>';
      }
      break;
      break;
    case 'google_dailyest':
      $coefs = kwresearch_get_report_coefs();
      $v = ($values['_searches']==0) ? 'NA': number_format($values['_searches'] * $coefs['google_search_share']);
      $output = l($v, 'http://www.google.com/search?btnG=Google+Search&q=' . $keyword, array('attributes' => array('target' => $type)));
      break;
    case 'yahoo_dailyest':
      $coefs = kwresearch_get_report_coefs();
      $v = ($values['_searches']==0) ? 'NA': number_format($values['_searches'] * $coefs['yahoo_search_share']);
      $output = l($v, 'http://search.yahoo.com/search?fr=ytff-&ei=UTF-8&rs=all&p=' . $keyword, array('attributes' => array('target' => $type)));
      break;
    case 'bing_dailyest':
      $coefs = kwresearch_get_report_coefs();
      $v = ($values['_searches']==0) ? 'NA': number_format($values['_searches'] * $coefs['bing_search_share']);
      $output = l($v, 'http://www.bing.com/search?q=' . $keyword, array('attributes' => array('target' => $type)));
      break;
    default:
      return FALSE;
  } 
  $output = array(
    'data' => $output,
    'class' => $type
  ); 
  return $output;
}

function kwresearch_get_report_coefs() {
  return array(
    'google_search_share' => .709,
    'yahoo_search_share' => .2035,
    'bing_search_share' => .0885
  ); 
}

function kwresearch_count_sort($a, $b) {
  $a_count = (is_array($a) && isset($a['_searches'])) ? $a['_searches'] : 0;
  $b_count = (is_array($b) && isset($b['_searches'])) ? $b['_searches'] : 0;
  if ($a_count == $b_count) {
    return 0;
  }
  return ($a_count > $b_count) ? -1 : 1;
}