You are here

asset_content.inc in Asset 6

File

inc/modules/asset_content.inc
View source
<?php

/**
 * implementation of CCK's hook_content_is_empty
 *
 * Delegated to filefield.module.
 */
function asset_content_is_empty($item, $field) {
  return empty($item['fid']) || (int) $item['fid'] == 0;
}

/**
 * Implementation of cck hook_field_info().
 */
function asset_field_info() {
  return array(
    'asset' => array(
      'label' => 'Asset',
    ),
  );
}

/**
 * Implementation of cck hook_field_settings().
 */
function asset_field_settings($op, $field) {
  switch ($op) {
    case 'database columns':
      return array(
        'aid' => array(
          'type' => 'int',
          'unsigned' => true,
        ),
        'options' => array(
          'type' => 'text',
          'size' => 'big',
        ),
        'caption' => array(
          'type' => 'text',
          'size' => 'big',
        ),
        'copyright' => array(
          'type' => 'text',
          'size' => 'big',
        ),
      );
      break;
  }
}

/**
 * Implementation of cck hook_field_formatter_info()
 */
function asset_field_formatter_info() {
  return array(
    'default' => array(
      'label' => 'Default',
      'field types' => array(
        'asset',
      ),
      'multiple values' => CONTENT_HANDLE_CORE,
    ),
  );
}

/**
 * Implementation of FAPI hook_elements().
 *
 * Any FAPI callbacks needed for individual widgets can be declared here,
 * and the element will be passed to those callbacks for processing.
 *
 * Drupal will automatically theme the element using a theme with
 * the same name as the hook_elements key.
 *
 * Autocomplete_path is not used by text_widget but other widgets can use it
 * (see nodereference and userreference).
*/
function asset_elements() {
  $elements = array();
  $elements['textarea'] = array(
    '#input' => TRUE,
    '#columns' => array(
      'value',
    ),
    '#delta' => 0,
    '#process' => array(
      'asset_textarea' => array(),
    ),
  );
  return $elements;
}

/**
 * Declare information about a widget.
 */
function asset_widget_info() {
  return array(
    'asset' => array(
      'label' => t('Asset'),
      'field types' => array(
        'asset',
      ),
      'multiple values' => CONTENT_HANDLE_CORE,
      'callbacks' => array(
        'default value' => CONTENT_CALLBACK_NONE,
      ),
    ),
  );
}

/**
 * Handle the parameters for a widget.
 */
function asset_widget_settings($op, $widget) {
  switch ($op) {
    case 'callbacks':
      return array(
        'default value' => CONTENT_CALLBACK_NONE,
      );
  }
}

/**
 * Implementation of hook_widget().
 *
 * Attach a single form element to the form. It will be built out and
 * validated in the callback(s) listed in hook_elements. We build it
 * out in the callbacks rather than here in hook_widget so it can be
 * plugged into any module that can provide it with valid
 * $field information.
 *
 * Content module will set the weight, field name and delta values
 * for each form element. This is a change from earlier CCK versions
 * where the widget managed its own multiple values.
 *
 * If there are multiple values for this field, the content module will
 * call this function as many times as needed.
 *
 * @param $form
 *   the entire form array, $form['#node'] holds node information
 * @param $form_state
 *   the form_state, $form_state['values'] holds the form values.
 * @param $field
 *   the field array
 * @param $delta
 *   the order of this item in the array of subelements (0, 1, 2, etc)
 *
 * @return
 *   the form item for a single element for this field
 */
function asset_widget(&$form, &$form_state, $field, $items, $delta = 0) {
  foreach ($items as $key => $item) {
    $a = asset_load(array(
      'aid' => $item['aid'],
    ));
    $items[$key]['value'] = $a->filename;
  }
  $field_name = $field['field_name'];

  //	$form = array($field_name => array('#tree' => TRUE));
  $form[$field['field_name']] = array(
    '#tree' => TRUE,
  );

  // If the field has multiple values
  if ($field['multiple']) {

    // Currently the fieldset is disabled because the asset javascript doesn't like it > name collision
    drupal_add_js(drupal_get_path('module', 'asset') . '/misc/asset_more.js');
    $delta = 0;

    // Render link fields for all the entered values
    foreach ($items as $data) {
      if (is_array($data) && $data['aid']) {
        _asset_widget_form($form[$field['field_name']][$delta], $field, $data, $delta);
        $delta++;
      }
    }

    // Render one additional new asset field
    foreach (range($delta, $delta) as $delta) {
      _asset_widget_form($form[$field['field_name']][$delta], $field, array(), $delta);
    }

    // Create a wrapper for additional fields
    $id = str_replace('_', '-', $field['field_name']) . '-wrapper';
    $form[$field['field_name']]['wrapper'] = array(
      '#type' => 'markup',
      '#value' => '<div id="' . $id . '" class="clear-block"></div>',
    );

    // Add 'More' Javascript Callback
    $form[$field['field_name']]['more-url'] = array(
      '#type' => 'hidden',
      '#value' => url('asset/widget/js/' . $field['type_name'] . '/' . $field['field_name'], array(
        'absolute' => true,
      )),
      '#attributes' => array(
        'class' => 'more-links',
      ),
      '#id' => str_replace('_', '-', $field['field_name']) . '-more-url',
    );

    // Add Current Field Count
    $form[$field['field_name']]['count'] = array(
      '#type' => 'hidden',
      '#value' => $delta,
      '#id' => str_replace('_', '-', $field['field_name']) . '-count',
    );

    // Add More Button
    $form[$field['field_name']]['more'] = array(
      '#type' => 'button',
      '#value' => t('More Assets'),
      '#name' => 'more',
      '#id' => str_replace('_', '-', $field['field_name']) . '-more',
    );
  }
  else {
    module_invoke('i18n', 'language_rtl') ? $rtl = "right" : ($rtl = "left");

    // Show the one and only value field
    $form[$field_name][0]['value'] = array(
      '#type' => 'textfield',
      '#title' => t($field['widget']['label']),
      //   '#description' => t($field['widget']['description']),
      '#default_value' => $items[0]['options'],
    );

    // Set the aid (hidden)
    $form[$field_name][0]['aid'] = array(
      '#type' => 'hidden',
      '#default_value' => $items[0]['aid'],
    );

    // Set the preview
    if (!empty($items[0]['value']) || isset($_POST[$field_name][0]['aid']) && is_numeric($_POST[$field_name][0]['aid'])) {

      // When in preview mode, $items[0]['value'] is not set, so we need to get the $_POST data
      $aid = !empty($items[0]['value']) ? $items[0]['aid'] : filter_xss($_POST[$field_name][0]['aid']);
      $is_image = array(
        'jpg',
        'jpeg',
        'png',
        'gif',
        'tif',
        'tiff',
        'bmp',
      );
      $a = asset_load(array(
        'aid' => $aid,
      ));

      // Preview the image
      if (in_array($a->extension, $is_image)) {
        if (variable_get('asset_imagecache', 0)) {
          $presets = _imagecache_get_presets();
          $preset = $presets[variable_get('asset_imagecache', 0)];
          $preview = theme('imagecache', $preset, str_replace("//", "/", $a->filepath));
        }
        else {
          $asset = array(
            'aid' => $aid,
            'format' => 'image',
            'formatter' => 'asset',
            'width' => '120',
            'height' => '80',
          );
          $preview = asset_render_macro($asset);
        }
        $form[$field_name][0]['preview'] = array(
          '#prefix' => '<div id="preview_' . $field_name . '_0" style="float:' . $rtl . '">',
          '#suffix' => '</div>',
          '#value' => $preview,
        );
      }
      else {

        // Try to auto-detect the format
        $form[$field_name][0]['preview'] = array(
          '#prefix' => '<div id="preview_' . $field_name . '_0" style="float:' . $rtl . '">',
          '#suffix' => '</div>',
          '#value' => asset_preview($aid),
        );
      }
    }
    else {
      $form[$field_name][0]['preview'] = array(
        '#value' => '<div id="preview_' . $field_name . '_0" style="float:' . $rtl . '"></div>',
      );
    }

    // Set the caption
    $l_text = '<img src="' . base_path() . drupal_get_path('module', 'asset') . '/misc/lullacons/doc-option-add.png' . '" alt="' . t("Insert asset") . '" title="' . t("Insert asset") . '"/>';
    $l = l($l_text, 'asset/wizard/textfield', array(
      'html' => true,
      'query' => 'textfield=' . $field_name . '[0][value]&aidfield=' . $field_name . '[0][aid]&delta=0',
      'attributes' => array(
        'class' => 'asset-textfield-link',
        'id' => 'asset-link-' . $field_name . '-0-value',
        'onclick' => "window.open(this.href, 'asset_textfield_link', 'width=614,height=400,scrollbars=yes,status=yes,resizable=yes,toolbar=no,menubar=no'); return false;",
      ),
    ));
    $form[$field_name][0]['caption'] = array(
      '#type' => 'textfield',
      '#maxlength' => '256',
      '#default_value' => $items[0]['caption'],
      '#description' => 'Enter the caption for this asset',
      '#prefix' => '<div style="margin-' . $rtl . ':130px"><table style="width:400px;margin-top:-15px;border-collapse:inherit"><tr><td style="padding:0px;margin:0px;text-align:center;padding-top:16px;width:10px" valign="top">' . $l . '</td><td>',
      '#suffix' => '</td></tr></table></div>',
    );

    // Set the copyright
    $l_text = '<img src="' . base_path() . drupal_get_path('module', 'asset') . '/misc/lullacons/doc-option-remove.png' . '" alt="' . t("Delete asset") . '" title="' . t("Delete asset") . '"/>';
    $l = l($l_text, '', array(
      'html' => true,
      'attributes' => array(
        'onclick' => "\n\t\t\t\t\t\t\$('#edit-" . str_replace("_", "-", $field_name) . "-0-value').val('');\n\t\t\t\t\t\t\$('#edit-" . str_replace("_", "-", $field_name) . "-0-aid').val('');\n\t\t\t\t\t\t\$('#edit-" . str_replace("_", "-", $field_name) . "-0-caption').val('');\n\t\t\t\t\t\t\$('#edit-" . str_replace("_", "-", $field_name) . "-0-copyright').val('');\n\t\t\t\t\t\t\$('#preview_" . $field_name . "_0').html('');\n\t\t\t\t\t\treturn false;",
      ),
    ));
    $form[$field_name][0]['copyright'] = array(
      '#type' => 'textfield',
      '#maxlength' => '256',
      '#default_value' => $items[0]['copyright'],
      '#description' => 'Enter the copyright for this asset',
      '#prefix' => '<div style="margin-' . $rtl . ':130px"><table style="width:400px;margin-top:-30px;border-collapse:inherit"><tr><td style="padding:0px;margin:0px;text-align:center;padding-top:16px;width:10px" valign="top">' . $l . '</td><td>',
      '#suffix' => '</td></tr></table></div>',
    );
    $form[$field_name][0]['#prefix'] = '<div class="clear-block">';
    $form[$field_name][0]['#suffix'] = '</div>';
  }
  if (isset($field['widget']['description']) && !empty($field['widget']['description'])) {
    $form[$field_name][0]['description'] = array(
      '#value' => '<div class="description">' . $field['widget']['description'] . '</div>',
    );
  }
  return $form;
}

/**
 * Process an individual element.
 *
 * Build the form element. When creating a form using FAPI #process,
 * note that $element['#value'] is already set.
 *
 * The $fields array is in $form['#field_info'][$element['#field_name']].
 */

// $element, $edit, &$form_state, $form
function asset_process($element, $edit, &$form_state, $form) {
  $items =& $form['#field_info'][$element['#field_name']];
  foreach ($items as $delta => $item) {
    if ($field['multiple'] == 1 && (empty($item['aid']) || !is_numeric($item['aid']))) {
      unset($items[$delta]);
      continue;
    }
    if (!empty($item['aid']) && is_numeric($item['aid'])) {
      if (!empty($item['value'])) {
        $items[$delta]['options'] = $item['value'];
      }
      else {
        $a = asset_load(array(
          'aid' => $item['aid'],
        ));
        $items[$delta]['options'] = $a->filename;
      }
      $items[$delta]['aid'] = $item['aid'];
      $items[$delta]['caption'] = $item['caption'] ? $item['caption'] : '';
      $items[$delta]['copyright'] = $item['copyright'] ? $item['copyright'] : '';
    }
  }
  return $items;
}
function asset_widget_js($type_name, $field_name) {
  $field = content_fields($field_name, $type_name);
  $type = content_types($type);
  $delta = $_POST[$field_name]['count'];
  $form = array();
  $node_field = array();
  _asset_widget_form($form, $field, $node_field, $delta);

  // Assign parents matching the original form
  foreach (element_children($form) as $key) {
    $form[$key]['#parents'] = array(
      $field_name,
      $delta,
      $key,
    );
  }

  // Add names, ids, and other form properties
  foreach (module_implements('form_alter') as $module) {
    $function = $module . '_form_alter';
    $function('asset_widget_js', $form);
  }
  $form = form_builder('asset_widget_js', $form);
  $output = drupal_render($form);
  print drupal_to_js(array(
    'status' => TRUE,
    'data' => $output,
  ));
  exit;
}
function _asset_widget_form(&$form_item, $field, $node_field, $delta = 0) {

  /*
  	$rtl = module_invoke('i18n', 'language_rtl') ? "right" : "left";
  	$field_name = $field['field_name'];
  	$item_count = $delta + 1;
  	$form_item = array(
  		'#tree' => TRUE,
  	);
  	// Set the title
  	$form_item['value'] = array(
  		'#type' => 'textfield',
  		'#title' => t($field['widget']['label']) . " " . $item_count,
  		'#attributes' => array('disabled' => 'disabled'),
  		'#default_value' => $node_field['value'],
  	);
  	// Set the aid (hidden)
  	$form_item['aid'] = array(
  		'#type' => 'hidden',
  		'#default_value' => $node_field['aid'],
  	);
  	if (!empty($node_field['aid'])) {
  		$aid = $node_field['aid'];
  		// Set the preview
  		$is_image = array('jpg', 'jpeg', 'png', 'gif', 'tif', 'tiff', 'bmp');
  		$a = asset_load(array('aid' => $aid));
  		// Preview the image
  		if (in_array($a->extension, $is_image)) {
  			if(variable_get('asset_imagecache', 0)) {
  				$presets = _imagecache_get_presets();
  				$preset = $presets[variable_get('asset_imagecache', 0)];
  				$preview = theme('imagecache', $preset, str_replace("//","/", $a->filepath));
  			} else {
  				$asset = array(
  					'aid' => $aid,
  					'format' => 'image',
  					'formatter' => 'asset',
  					'width' => '120',
  					'height' => '80',
  				);
  				$preview = asset_render_macro($asset);
  			}
  			$form_item['preview'] = array(
  				'#prefix' => '<div id="preview_' . $field_name . '_' . $delta . '" style="float:' . $rtl . '">',
  				'#suffix' => '</div>',
  				'#value' => $preview,
  			);
  		} else {
  			// Try to auto-detect the format
  			if (asset_preview($aid)) {
  				$form_item['preview'] = array(
  					'#prefix' => '<div id="preview_' . $field_name . '_' . $delta . '" style="float:' . $rtl . '">',
  					'#suffix' => '</div>',
  					'#value' => asset_preview($aid),
  				);
  			} else {
  				// Display a placeholder for assets that don't have a preview: .mov, .doc, ...
  				$form_item['preview'] = array(
  					'#type' => 'markup',
  					'#value' => '<div id="preview_' . $field_name . '_' . $delta . '" style="float:' . $rtl . '"></div>',
  				);
  			}
  		}
  	} else {
  		$form_item['preview'] = array(
  			'#value' => '<div id="preview_' . $field_name . '_' . $delta . '" style="float:'.$rtl.'"></div>',
  		);
  	}
  	// Set the caption
  	$l_text = '<img src="' . base_path() . drupal_get_path('module', 'asset') . '/misc/lullacons/doc-option-add.png' . '" alt="' . t("Insert asset") . '" title="' . t("Insert asset") . '"/>';
  	$l = l($l_text, 'asset/wizard/textfield', array(
  		'html' => true,
  		'query' => 'textfield=' . $field_name . '[' . $delta . '][value]&aidfield=' . $field_name . '[' . $delta . '][aid]&delta=' . $delta,
  		'attributes' => array(
  			'class'=>'asset-textfield-link',
  			'id'=>'asset-link-' . $field_name . '-' . $delta . '-value',
  			'onclick'=>"window.open(this.href, 'asset_textfield_link', 'width=614,height=400,scrollbars=yes,status=yes,resizable=yes,toolbar=no,menubar=no'); return false",
  	)));
  	$form_item['caption'] = array(
  		'#type' => 'textfield',
  		'#maxlength' => '256',
  		'#default_value' => $node_field['caption'],
  		'#description' => 'Enter the caption for this asset',
  		'#prefix' => '<div style="margin-'.$rtl.':130px"><table style="width:400px;margin-top:-15px;border-collapse:inherit"><tr><td style="padding:0px;margin:0px;text-align:center;padding-top:16px;width:10px" valign="top">' . $l . '</td><td>',
  		'#suffix' => '</td></tr></table></div>',
  	);
  	// Set the copyright
  	$l_text = '<img src="' . base_path() . drupal_get_path('module', 'asset') . '/misc/lullacons/doc-option-remove.png' . '" alt="' . t("Delete asset") . '" title="' . t("Delete asset") . '"/>';
  	$l = l($l_text, '', array(
  		'html' => true,
  		'attributes' => array(
  			'onclick' => "
  			$('#edit-" . str_replace("_","-",$field_name) . "-" . $delta . "-value').val('');
  			$('#edit-" . str_replace("_","-",$field_name) . "-" . $delta . "-aid').val('');
  			$('#edit-" . str_replace("_","-",$field_name) . "-" . $delta . "-caption').val('');
  			$('#edit-" . str_replace("_","-",$field_name) . "-" . $delta . "-copyright').val('');
  			$('#preview_" . $field_name . "_" . $delta . "').html('');
  			return false;",
  	)));
  	$form_item['copyright'] = array(
  		'#type' => 'textfield',
  		'#maxlength' => '256',
  		'#default_value' => $node_field['copyright'],
  		'#description' => 'Enter the copyright for this asset',
  		'#prefix' => '<div style="margin-'.$rtl.':130px"><table style="width:400px;margin-top:-30px;border-collapse:inherit"><tr><td style="padding:0px;margin:0px;text-align:center;padding-top:16px;width:10px" valign="top">' . $l . '</td><td>',
  		'#suffix' => '</td></tr></table></div>',
  	);
  	if($delta==0 && isset($field['widget']['description']) && !empty($field['widget']['description'])) {
  		$form_item['description'] = array(
  			'#value' => '<div class="description">' . $field['widget']['description'] . '</div>',
  		);
  	}
  	$form_item['#prefix'] = '<div class="clear-block">';
  	$form_item['#suffix'] = '</div>';
  */
}

Functions

Namesort descending Description
asset_content_is_empty implementation of CCK's hook_content_is_empty
asset_elements Implementation of FAPI hook_elements().
asset_field_formatter_info Implementation of cck hook_field_formatter_info()
asset_field_info Implementation of cck hook_field_info().
asset_field_settings Implementation of cck hook_field_settings().
asset_process
asset_widget Implementation of hook_widget().
asset_widget_info Declare information about a widget.
asset_widget_js
asset_widget_settings Handle the parameters for a widget.
_asset_widget_form