You are here

function brilliant_gallery_checklist_save in Brilliant Gallery 7

Same name and namespace in other branches
  1. 5.4 brilliant_gallery.module \brilliant_gallery_checklist_save()
  2. 5.3 brilliant_gallery.module \brilliant_gallery_checklist_save()
  3. 6.4 brilliant_gallery.module \brilliant_gallery_checklist_save()
  4. 6 brilliant_gallery.module \brilliant_gallery_checklist_save()
  5. 6.2 brilliant_gallery.module \brilliant_gallery_checklist_save()
  6. 6.3 brilliant_gallery.module \brilliant_gallery_checklist_save()
  7. 7.2 OLD_brilliant_gallery.module \brilliant_gallery_checklist_save()

Function to save/update the state of a checkbox when toggled

1 string reference to 'brilliant_gallery_checklist_save'
brilliant_gallery_menu in ./brilliant_gallery_menu.inc
@todo Please document this function.

File

./brilliant_gallery.module, line 143

Code

function brilliant_gallery_checklist_save($nid = '', $qid = '', $state = '', $token = '') {
  global $user;
  $GLOBALS['devel_shutdown'] = FALSE;

  // Prevent possible damage from loading path bgchecklist/save directly.
  if (drupal_valid_token($token, 'render_brilliant_gallery_manage')) {

    // Token valid.
  }
  else {
    watchdog('Brilliant Gal', 'Invalid token submitted from bgchecklist/save', NULL, WATCHDOG_ALERT);
    return;
  }

  // First try to check whether we got here by an AJAX request. See https://drupal.stackexchange.com/questions/54296/how-can-i-detect-ajax-request-inside-drupal/54317#54317
  if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {

    // AJAX request.
  }
  else {
    watchdog('Brilliant Gal', 'Not an AJAX request from bgchecklist/save', NULL, WATCHDOG_ALERT);
    return;
  }

  // Verify that the submitted values are of expected type and range.
  if ($nid == 9999999 && $qid != '' && ($state == 0 || $state == 1)) {

    // Seems like a valid request.
    $qid = check_plain($qid);
  }
  else {
    watchdog('Brilliant Gal', 'Invalid request from bgchecklist/save', NULL, WATCHDOG_ALERT);
    return;
  }
  if (preg_match("/^user-/", $qid) == 1) {
    $uid = $user->uid;
  }
  else {
    $uid = 0;
  }

  // TODO Please convert this statement to the D7 database API syntax.
  $existing = db_query("select count(state) from {brilliant_gallery_checklist} " . "where nid=:nid and user=:uid and qid=:qid", array(
    ':nid' => $nid,
    ':uid' => $uid,
    ':qid' => $qid,
  ))
    ->fetchField();
  if ($existing == 0) {

    // TODO Please convert this statement to the D7 database API syntax.
    db_query("insert into {brilliant_gallery_checklist} (nid,user,qid,state) " . "values (:nid,:uid,:qid,:state)", array(
      ':nid' => $nid,
      ':uid' => $uid,
      ':qid' => $qid,
      ':state' => $state,
    ));
  }
  else {

    // TODO Please convert this statement to the D7 database API syntax.
    $current = db_query("select state from {brilliant_gallery_checklist} " . " where nid=:nid and user=:uid and qid=:qid", array(
      ':nid' => $nid,
      ':uid' => $uid,
      ':qid' => $qid,
    ))
      ->fetchField();
    if ($current != $state) {

      // TODO Please convert this statement to the D7 database API syntax.
      db_query("update {brilliant_gallery_checklist} " . "set state=:state where nid=:nid and user=:uid and qid=:qid", array(
        ':state' => $state,
        ':nid' => $nid,
        ':uid' => $uid,
        ':qid' => $qid,
      ));
    }
  }
  print drupal_json_encode("1");
  exit;
}