User login

Major change in CMT's internals: cmt_term_*_set functions become one cmt_term_attribute_set function

Old way:

/******************************************************************************
* The following _set functions save information to the cmt_term tables and
* subsequently to votingapi if appropriate.  These function names are based
* on the table names.
*
* They are currently written for clarity over efficiency.
*/

/**
* Saves a community-managed term's description and updates votingAPI.
*
* First it checks to make sure the description doesn't already exist for
* this term (as unlikely as that might be), and if it does, uses that
* content_id rather than inserting a new field.
*
* @param $tid
*   A CMT term ID.
* @param $description
*   A longtext description of a CMT term.
*
* @return
*  INSERT if a new description, UPDATE if the description was already
*  proposed for the term, and FALSE if a query failed.
*/
function cmt_term_description_set($tid, $description, $uid = NULL, $value = 1) {
  $result = db_query("SELECT content_id FROM {cmt_term_description} WHERE tid=%d AND description='%s'", $tid, $description);
  if (!$result) {
print 'There is no result';
    drupal_set_error("Select query to check for description failed.", 'error');
    // how do i do this right?  http://agaricdesign.com/throwing-errors-in-drupal-agarics-complete-guide  
    return FALSE;
  }
print 'there is a result';
  if ($cmt_term_description = db_fetch_object($result)) {
    // should I check to make sure nothing crazy has happened and there's somehow two rows?
    $content_id = $cmt_term_description->content_id;
    $return = 'UPDATE';
  }
  else {  // this is a new description
    $query = "INSERT INTO {cmt_term_description} (tid, description) VALUES (%d, '%s')";
    $result = db_query($query, $tid, $description);
    if (!$result) {
      drupal_set_error("Inserting new term description failed.");
      return FALSE;
    }
    $content_id = db_last_insert_id('cmt_term_description', 'content_id');
    $return = 'INSERT';
  }
  $content_type = 'cmt_term_description';
  // content_id has already been set, by select or db_last_insert_id
  cmt_vote($content_type, $content_id, $uid = NULL, $value = 1);
  // hell, we just have to hope everything downstream works!
  return $return;
}

New way:

/**
* Saves a community-managed term's attribute and updates votingAPI.
*
* First it checks to make sure the attribute doesn't already exist for
* this term (as unlikely as that might be), and if it does, uses that
* content_id rather than inserting a new row.
*
* @param $id
*   An array with a 'value' set to the CMT term ID or node ID, and
*   optionally a field name other than tid
* @param $attribute
*   An array with the attribute for that CMT term, including:
*     'value' which can be a numeric type, longtext
*     'field' the field name
*     'type' (optional) if not %d, define how db_query should take this
* @param $content_type
*   A string of the content_type, also the cmt_term_* table name
* @param $new
*   Optional parameter to assert a new insert, skip the SQL select
* @param $value
*   Optional vote value if more than 1
* @param $uid
*   Optional uid if not active user
*
* @return
*  INSERT if a new description, UPDATE if the description was already
*  proposed for the term, and FALSE if a query failed.
*/
function cmt_term_attribute_set($id, $attribute, $content_type, $new = FALSE, $value = 1, $uid = NULL) {
  // set defaults
  if (!$id['field'])  $id['field'] = 'tid';
  if (!$attribute['type'])  $attribute['type'] = '%d';
  $return = 'INSERT';  // if not overridden, insert new id-attribute pair
  if (!$new) {
    $result = db_query("SELECT content_id FROM {".$content_type."} WHERE ".$id['field']."=%d AND ".$attribute['field']."=".$attribute['type']."", $id['value'], $attribute['value']);
    if (!$result) {
print 'There is no result';
      drupal_set_error("Select query to check for description failed.", 'error');
      // how do i do this right?  http://agaricdesign.com/throwing-errors-in-drupal-agarics-complete-guide  
      return FALSE;
    }
print 'there is a result';
    if ($cmt_term_attribute = db_fetch_object($result)) {
      // should I check to make sure nothing crazy has happened and there's somehow two rows?
      $content_id = $cmt_term_attribute->content_id;
      // this is not a new attribute, we won't insert
      $return = 'UPDATE';
    }
  }
  if ($return = 'INSERT') {
    $result = db_query("INSERT INTO {".$content_type."} (".$id['field'].", ".$attribute['field'].") VALUES (%d, ".$attribute['type'].")", $id['value'], $attribute['value']);
    if (!$result) {
      drupal_set_error("Inserting new term description failed.");
      return FALSE;
    }
    $content_id = db_last_insert_id($content_type, 'content_id');
  }
  // content_id has been set by select or db_last_insert_id
  cmt_vote($content_type, $content_id);
  // downstream of cmt_vote will have to throw its own errors if anything goes wrong
  // this function can't take any more feedback!
  return $return;
}

Old way:

/******************************************************************************
* The following _set functions save information to the cmt_term tables and
* subsequently to votingapi if appropriate.  These function names are based
* on the table names.
*
* They are currently written for clarity over efficiency.
*/

/**
* Saves a community-managed term's description and updates votingAPI.
*
* First it checks to make sure the description doesn't already exist for
* this term (as unlikely as that might be), and if it does, uses that
* content_id rather than inserting a new field.
*
* @param $tid
*   A CMT term ID.
* @param $description
*   A longtext description of a CMT term.
*
* @return
*  INSERT if a new description, UPDATE if the description was already
*  proposed for the term, and FALSE if a query failed.
*/
function cmt_term_description_set($tid, $description, $uid = NULL, $value = 1) {
  $result = db_query("SELECT content_id FROM {cmt_term_description} WHERE tid=%d AND description='%s'", $tid, $description);
  if (!$result) {
print 'There is no result';
    drupal_set_error("Select query to check for description failed.", 'error');
    // how do i do this right?  http://agaricdesign.com/throwing-errors-in-drupal-agarics-complete-guide  
    return FALSE;
  }
print 'there is a result';
  if ($cmt_term_description = db_fetch_object($result)) {
    // should I check to make sure nothing crazy has happened and there's somehow two rows?
    $content_id = $cmt_term_description->content_id;
    $return = 'UPDATE';
  }
  else {  // this is a new description
    $query = "INSERT INTO {cmt_term_description} (tid, description) VALUES (%d, '%s')";
    $result = db_query($query, $tid, $description);
    if (!$result) {
      drupal_set_error("Inserting new term description failed.");
      return FALSE;
    }
    $content_id = db_last_insert_id('cmt_term_description', 'content_id');
    $return = 'INSERT';
  }
  $content_type = 'cmt_term_description';
  // content_id has already been set, by select or db_last_insert_id
  cmt_vote($content_type, $content_id, $uid = NULL, $value = 1);
  // hell, we just have to hope everything downstream works!
  return $return;
}

New way:

/**
* Saves a community-managed term's attribute and updates votingAPI.
*
* First it checks to make sure the attribute doesn't already exist for
* this term (as unlikely as that might be), and if it does, uses that
* content_id rather than inserting a new row.
*
* @param $id
*   An array with a 'value' set to the CMT term ID or node ID, and
*   optionally a field name other than tid
* @param $attribute
*   An array with the attribute for that CMT term, including:
*     'value' which can be a numeric type, longtext
*     'field' the field name
*     'type' (optional) if not %d, define how db_query should take this
* @param $content_type
*   A string of the content_type, also the cmt_term_* table name
* @param $new
*   Optional parameter to assert a new insert, skip the SQL select
* @param $value
*   Optional vote value if more than 1
* @param $uid
*   Optional uid if not active user
*
* @return
*  INSERT if a new description, UPDATE if the description was already
*  proposed for the term, and FALSE if a query failed.
*/
function cmt_term_attribute_set($id, $attribute, $content_type, $new = FALSE, $value = 1, $uid = NULL) {
  // set defaults
  if (!$id['field'])  $id['field'] = 'tid';
  if (!$attribute['type'])  $attribute['type'] = '%d';
  $return = 'INSERT';  // if not overridden, insert new id-attribute pair
  if (!$new) {
    $result = db_query("SELECT content_id FROM {".$content_type."} WHERE ".$id['field']."=%d AND ".$attribute['field']."=".$attribute['type']."", $id['value'], $attribute['value']);
    if (!$result) {
print 'There is no result';
      drupal_set_error("Select query to check for description failed.", 'error');
      // how do i do this right?  http://agaricdesign.com/throwing-errors-in-drupal-agarics-complete-guide  
      return FALSE;
    }
print 'there is a result';
    if ($cmt_term_attribute = db_fetch_object($result)) {
      // should I check to make sure nothing crazy has happened and there's somehow two rows?
      $content_id = $cmt_term_attribute->content_id;
      // this is not a new attribute, we won't insert
      $return = 'UPDATE';
    }
  }
  if ($return = 'INSERT') {
    $result = db_query("INSERT INTO {".$content_type."} (".$id['field'].", ".$attribute['field'].") VALUES (%d, ".$attribute['type'].")", $id['value'], $attribute['value']);
    if (!$result) {
      drupal_set_error("Inserting new term description failed.");
      return FALSE;
    }
    $content_id = db_last_insert_id($content_type, 'content_id');
  }
  // content_id has been set by select or db_last_insert_id
  cmt_vote($content_type, $content_id);
  // downstream of cmt_vote will have to throw its own errors if anything goes wrong
  // this function can't take any more feedback!
  return $return;
}

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.