You are here

function kwresearch_toggle_site_keyword_js in Keyword Research 7

Same name and namespace in other branches
  1. 6 kwresearch.module \kwresearch_toggle_site_keyword_js()

AJAX handler to enable site keyword priority to be set

1 string reference to 'kwresearch_toggle_site_keyword_js'
kwresearch_menu in ./kwresearch.module
Implements hook_menu()

File

./kwresearch.module, line 890

Code

function kwresearch_toggle_site_keyword_js() {
  global $user;
  $output = array(
    'data' => array(),
  );

  // check if valid request containing Drupal generated token
  if (empty($_POST['token']) || !drupal_valid_token($_POST['token'], 'kwresearch')) {
    drupal_access_denied();
    drupal_exit();
  }
  $keyword = check_plain($_POST['kwresearch_keyword']);
  $priority = check_plain($_POST['priority']);
  $keyword_obj = new stdClass();
  $keyword = strtolower($keyword);
  $keyword_obj->priority = $priority;
  $kid = kwresearch_save_site_keyword($keyword, NULL, $keyword_obj);
  $keyword_obj = kwresearch_load_site_keyword($kid);
  if ($user->uid != $keyword_obj->uid) {

    // TODO Convert "user_load" to "user_load_multiple" if "$keyword_obj->uid" is other than a uid.
    // To return a single user object, wrap "user_load_multiple" with "array_shift" or equivalent.
    // Example: array_shift(user_load_multiple(array(), $keyword_obj->uid))
    $theuser = user_load($keyword_obj->uid);
  }
  else {
    $theuser = $user;
  }
  $site_priority_options = kwresearch_get_priority_options();
  $output['data'] = array(
    'kid' => (int) $keyword_obj->kid,
    'keyword' => $keyword,
    'priority' => (int) $keyword_obj->priority,
    'priority_out' => $keyword_obj->priority >= 0 ? $site_priority_options[$keyword_obj->priority] : '-',
    'value' => (double) $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($output);
}