You are here

function asset_nodeapi in Asset 5

Same name and namespace in other branches
  1. 6 inc/asset.node.inc \asset_nodeapi()

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

File

./asset.module, line 292

Code

function asset_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'insert':
    case 'update':

      // 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'] == "value") {

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

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