You are here

function uc_weightquote_nodeapi in Ubercart 5

Same name and namespace in other branches
  1. 6.2 shipping/uc_weightquote/uc_weightquote.module \uc_weightquote_nodeapi()

Implementation of hook_nodeapi().

File

shipping/uc_weightquote/uc_weightquote.module, line 91
Shipping quote module that defines a shipping rate for each product based on weight.

Code

function uc_weightquote_nodeapi(&$node, $op) {
  if (in_array($node->type, module_invoke_all('product_types'))) {
    switch ($op) {
      case 'insert':
      case 'update':
        if (is_array($node->weightquote)) {
          if (!$node->revision) {
            db_query("DELETE FROM {uc_weightquote_products} WHERE vid = %d", $node->vid);
          }
          foreach ($node->weightquote as $mid => $rate) {
            if ($rate !== '') {
              db_query("INSERT INTO {uc_weightquote_products} (vid, nid, mid, rate) VALUES (%d, %d, %d, %f)", $node->vid, $node->nid, $mid, $rate);
            }
          }
        }
        break;
      case 'load':
        $return = array(
          'weightquote' => array(),
        );
        $result = db_query("SELECT mid, rate FROM {uc_weightquote_products} WHERE vid = %d", $node->vid);
        while ($rate = db_fetch_object($result)) {
          $return['weightquote'][$rate->mid] = $rate->rate;
        }
        $result = db_query("SELECT mid, unit_rate FROM {uc_weightquote_methods}");
        while ($method = db_fetch_object($result)) {
          $rate = $return['weightquote'][$method->mid];
          if ($rate === null || $rate < 0) {
            $return['weightquote'][$method->mid] = $method->unit_rate;
          }
        }
        return $return;
        break;
      case 'delete':
        db_query("DELETE FROM {uc_weightquote_products} WHERE nid = %d", $node->nid);
        break;
      case 'delete revision':
        db_query("DELETE FROM {uc_weightquote_products} WHERE vid = %d", $node->vid);
        break;
    }
  }
}