You are here

function contentanalysis_get_aid in Content Analysis 6

Same name and namespace in other branches
  1. 8 contentanalysis.module \contentanalysis_get_aid()
  2. 7 contentanalysis.module \contentanalysis_get_aid()

Returns the aid for a given context

Parameters

$context:

$save: Boolean to save the aid if it does not already exist

Return value

aid

1 call to contentanalysis_get_aid()
contentanalysis_do_analysis in ./contentanalysis.module
Provides analysis on context passed in

File

./contentanalysis.module, line 1489

Code

function contentanalysis_get_aid($context, $save = 1) {

  // save data to db
  $aid = NULL;
  $where = '';
  if ($context['source'] == 'node-edit-form' && is_null($context['nid'])) {
    return $aid;
  }
  if ($context['aid'] > 0) {
    $where = 'aid = %d';
    $wherev = $context['aid'];
  }
  elseif ($context['nid'] > 0) {
    $where = 'nid = %d';
    $wherev = $context['nid'];
  }
  elseif (!is_null($context['path'])) {
    $where = 'path = "%s"';
    $wherev = $context['path'];
  }
  elseif (!is_null($context['url'])) {
    $where = 'url = "%s"';
    $wherev = $context['url'];
  }
  if ($where && $save) {

    // by pass db save if analyzing content direct submission
    $sql = '
      SELECT *
      FROM {contentanalysis}
      WHERE ' . $where . '
    ';
    $result = db_fetch_object(db_query($sql, $wherev));
    if ($result->aid) {
      $aid = $result->aid;
      $sql = '
        UPDATE {contentanalysis}
        SET last_analysis = %d,
        nid = %d,
        path = "%s",
        url = "%s"
        WHERE aid = %d
      ';
      db_query($sql, time(), $context['nid'], $context['path'], $context['url'], $context['aid']);
    }
    else {
      $sql = '
        INSERT INTO {contentanalysis}
        (aid, last_analysis, nid, path, url)
        VALUES
        (NULL,%d,%d,"%s","%s")
      ';
      db_query($sql, time(), $context['nid'], $context['path'], $context['url']);
      $aid = db_last_insert_id('contentanalysis', 'aid');
    }
  }
  return $aid;
}