You are here

function asset_nodeapi in Asset 6

Same name and namespace in other branches
  1. 5 asset.module \asset_nodeapi()

Implementation of hook_nodeapi() This is where we build the asset_node records.

File

inc/asset.node.inc, line 170

Code

function asset_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {

  //print_r($op);
  switch ($op) {
    case 'insert':
    case 'update':
      _content_widget_invoke($op, $node);

      // clear previous count
      db_query('DELETE FROM {asset_node} WHERE nid = %d', $node->nid);

      // only update if using a format that includes the asset filter
      $filters = filter_list_format($node->format);
      if (!$filters['asset/0']) {
        return;
      }
      $refs = array();
      foreach (asset_get_macros($node->body) as $macro) {
        $refs[$macro['aid']]++;
      }
      foreach ($refs as $aid => $ref) {
        db_query('INSERT INTO {asset_node} (aid, nid, refs) VALUES (%d, %d, %d)', $aid, $node->nid, $ref);
      }
      break;
    case 'load':
      $result = db_query('SELECT * FROM {asset_node} WHERE nid = %d AND refs > 0', $node->nid);
      while ($asset = db_fetch_object($result)) {
        $additions['assets'][] = asset_load($asset->aid);
      }
      return $additions;
    case 'view':
      foreach ($node as $fieldname => $value) {
        if (substr($fieldname, 0, 6) == "field_") {

          // For each cck field, check if it's an asset field
          $fields = content_fields($fieldname, $node->type);
          if ($fields['type'] == "asset") {

            // The asset computer name is "value"
            $assetcheck = 1;
            foreach (array_keys($node->{$fieldname}) as $key) {

              // Add the html code for the preview
              if (!empty($value[$key]['aid'])) {
                $value[$key]['value'] = asset_preview($value[$key]['aid']);
              }
            }
          }
        }
      }
      break;
  }
}