You are here

function img_assist_properties_form in Image Assist 5.3

Same name and namespace in other branches
  1. 5 img_assist.module \img_assist_properties_form()
  2. 5.2 img_assist.module \img_assist_properties_form()
  3. 6.2 img_assist.module \img_assist_properties_form()
  4. 6 img_assist.module \img_assist_properties_form()

Construct the image properties form.

Related topics

1 string reference to 'img_assist_properties_form'
img_assist_properties in ./img_assist.module
Load the image properties pane.

File

./img_assist.module, line 854
Image Assist module

Code

function img_assist_properties_form($node, $update) {
  require_once drupal_get_path('module', 'img_assist') . '/includes/img_assist.token.inc';
  $image_info = image_get_info(file_create_path($node->images[IMAGE_ORIGINAL]));
  $image_info['aspect_ratio'] = $image_info['height'] / $image_info['width'];

  // Select (or generate) a preview image.
  $img_assist_create_derivatives = variable_get('img_assist_create_derivatives', array());
  if (!empty($img_assist_create_derivatives['properties'])) {
    $properties_size['label'] = t('Properties');
    $properties_size['key'] = 'img_assist_properties';
    $properties_size['width'] = 200;
    $properties_size['height'] = 200;
  }
  else {
    $properties_size['key'] = IMAGE_THUMBNAIL;
  }
  $properties_image = img_assist_display($node, $properties_size);

  // Get actual image size.
  $properties_size = image_get_info(file_create_path($node->images[$properties_size['key']]));

  // Create an array of image derivative choices
  //
  // The name for each option is actually the size in pixels, not the derivative
  // name. This is necessary so that
  // - the Javascript that process this page and inserts code to your textarea
  //   or editor will know the size to make the image placeholder (in a WYSIWYG
  //   editor)
  // - the code that processes the img_assist filter tags can work with standard
  //   sizes and custom sizes in the same way.
  // The WYSIWYG placeholder, however, is the most important reason to keep the
  // img_assist tags this way. This way users can even resize images in the
  // editor, and if they aren't allow to create custom sizes the filter will
  // pick the existing derivative that is closest to the size of the WYSIWYG
  // placeholder. For users not familiar with pixel sizes or names like
  // 'thumbnail' and 'preview', this is a nice visual way to size images.
  // The size selection dropdown could even be hidden using the stylesheet,
  // making the insertion of images even more of a visual process. And for
  // administrators and those with the proper permissions, images don't have to
  // snap to the nearest standard size. You can create any arbitrary size you
  // choose.
  $max_size = explode('x', variable_get('img_assist_max_size', '640x640'));
  foreach (image_get_sizes(NULL, $image_info['aspect_ratio']) as $key => $size) {

    // Sizes are strings, may contain '' for 0; convert to integers.
    settype($size['width'], 'int');
    settype($size['height'], 'int');
    $added_to_derivatives = FALSE;
    if ($key == IMAGE_ORIGINAL) {
      if (user_access('use original size') && $image_info['width'] <= $max_size[0] && $image_info['height'] <= $max_size[1]) {
        $derivatives[$image_info['width'] . 'x' . $image_info['height']] = $size['label'];
        $added_to_derivatives = TRUE;
      }
    }
    elseif ($size['width'] <= $max_size[0] && $size['height'] <= $max_size[1]) {
      $derivatives[$size['width'] . 'x' . $size['height']] = $size['label'];
      $added_to_derivatives = TRUE;
    }
    if (!$added_to_derivatives) {

      // The thumbnail option will be shown even if it is larger than the
      // maximum size.
      if ($key == IMAGE_THUMBNAIL) {
        $derivatives[$size['width'] . 'x' . $size['height']] = $size['label'];
      }
    }
  }

  // Add a choice for 'other' if the user has the proper permission to create
  // custom sizes.
  if (!empty($img_assist_create_derivatives['custom_advanced']) && user_access('access advanced options')) {
    $derivatives['other'] = t('Other');
  }

  // Use 'preview' size by default.
  $default_size = image_get_info(file_create_path($node->images[IMAGE_PREVIEW]));
  $default_width = $default_size['width'];
  $default_height = $default_size['height'];

  // Calculate the aspect ratio to keep in a hidden field
  //
  // When 'other' is chosen, the custom size will always follow the aspect ratio.
  // It doesn't really matter what this value is here because the actual custom
  // image will be created when the  filter tag is processed. The size, of course,
  // is a bounding box. If it a user stretches an image placeholder out of
  // proportion in the WYSIWYG editor, the image will never be out of proportion
  // on the processed page.
  $aspect_ratio = $default_height > 0 ? round($default_width / $default_height, 6) : 1;

  // Create the form.
  $form[] = array(
    '#value' => '<div id="properties">',
  );
  $form[] = array(
    '#value' => '<table width="100%" border="0" cellspacing="0" cellpadding="0">',
  );
  $form[] = array(
    '#value' => '<tr><td valign="top" rowspan="3" id="preview">',
  );
  $form[] = array(
    '#value' => $properties_image,
  );
  $form[] = array(
    '#value' => '<span id="caption" style="width: ' . $properties_size['width'] . 'px;">' . check_plain($node->title) . '</span>',
  );

  // Image node properties fieldset.
  $form['properties'] = array(
    '#type' => 'fieldset',
    '#title' => t('Image properties'),
  );
  $form['properties'][] = array(
    '#value' => '<div class="field field-type-text"><div class="field-label">' . t('Size') . ': </div><div class="field-items"><div class="field-item">' . strtr('@widthx@height px', array(
      '@width' => $image_info['width'],
      '@height' => $image_info['height'],
    )) . '</div></div></div>',
  );
  $rendered = node_build_content($node);
  foreach (array_filter(variable_get('img_assist_display_properties', array())) as $field) {
    if (isset($node->content[$field])) {
      $form['properties'][] = array(
        '#value' => drupal_render($node->content[$field]),
      );
    }
  }
  $form[] = array(
    '#value' => '</td><td width="100%" colspan="2">',
  );
  $token_installed = module_exists('token');
  if (variable_get('img_assist_load_title', 1)) {
    $title = img_assist_sanitize($token_installed ? token_replace(variable_get('img_assist_title_pattern', '[title]'), 'node', $node) : $node->title);
  }
  if (variable_get('img_assist_load_description', 1)) {
    $description = img_assist_sanitize($token_installed ? token_replace(variable_get('img_assist_description_pattern', '[body]'), 'node', $node) : $node->body);
  }
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title (optional)'),
    '#default_value' => isset($title) ? $title : '',
    '#size' => 50,
    '#maxlength' => 255,
    '#description' => NULL,
    '#attributes' => array(
      'onblur' => 'parent.updateCaption()',
    ),
  );
  $form['desc'] = array(
    '#type' => 'textfield',
    '#title' => t('Description (optional)'),
    '#default_value' => isset($description) ? $description : '',
    '#size' => 50,
    '#maxlength' => 255,
    '#description' => NULL,
    '#attributes' => array(
      'onblur' => 'parent.updateCaption()',
    ),
  );

  // Size.
  $form[] = array(
    '#value' => '</td></tr><tr><td width="90%">',
  );
  $form[] = array(
    '#value' => '<div class="form-item" id="edit-size">',
  );
  $form[] = array(
    '#value' => '<label for="edit-size-label">' . t('Size: (max @maxsize)', array(
      '@maxsize' => variable_get('img_assist_max_size', '640x640'),
    )) . '</label>',
  );
  $form['size_label'] = array(
    '#type' => 'select',
    '#default_value' => variable_get('img_assist_default_label', '100x100'),
    '#options' => $derivatives,
    '#attributes' => array(
      'onchange' => 'parent.onChangeSizeLabel()',
    ),
  );
  $form[] = array(
    '#value' => '<div class="form-item" id="size-other">',
  );
  $form['width'] = array(
    '#type' => 'textfield',
    '#default_value' => $default_width,
    '#size' => 4,
    '#maxlength' => 4,
    '#attributes' => array(
      'onblur' => 'parent.onChangeWidth()',
    ),
  );
  $form[] = array(
    '#value' => ' x ',
  );
  $form['height'] = array(
    '#type' => 'textfield',
    '#default_value' => $default_height,
    '#size' => 4,
    '#maxlength' => 4,
    '#attributes' => array(
      'onblur' => 'parent.onChangeHeight()',
    ),
  );
  $form[] = array(
    '#value' => '</div></div>',
  );
  $form[] = array(
    '#value' => '</td><td>',
  );

  // Alignment.
  $form['align'] = array(
    '#type' => 'select',
    '#title' => t('Alignment'),
    '#default_value' => variable_get('img_assist_default_alignment', 'left'),
    '#options' => array(
      'left' => t('left'),
      'right' => t('right'),
      'none' => t('none'),
      'center' => t('center'),
    ),
    '#prefix' => '<div id="alignment">',
    '#suffix' => '</div>',
  );
  $form[] = array(
    '#value' => '</td></tr><tr><td colspan="2">',
  );

  // Link.
  if (user_access('access advanced options')) {
    $form[] = array(
      '#value' => '<div class="form-item" id="link-group">',
    );
    $form['link'] = array(
      '#type' => 'select',
      '#title' => t('Link'),
      '#default_value' => variable_get('img_assist_default_link_behavior', 'none'),
      '#options' => array(
        'none' => t('Not a link'),
        'node' => t('Link to image page'),
        'popup' => t('Open in popup window'),
        'url' => t('Go to URL'),
      ),
      '#attributes' => array(
        'onchange' => 'parent.onChangeLink()',
      ),
    );
    $form['url'] = array(
      '#type' => 'textfield',
      '#default_value' => variable_get('img_assist_default_link_url', 'http://'),
      '#size' => 25,
      '#maxlength' => 255,
      '#description' => NULL,
    );
    $form['link_options_visible'] = array(
      '#type' => 'hidden',
      '#value' => 1,
    );
    $form[] = array(
      '#value' => '</div>',
    );
  }
  else {
    $form['link'] = array(
      '#type' => 'hidden',
      '#value' => variable_get('img_assist_default_link_behavior', 'none'),
    );
    $form['url'] = array(
      '#type' => 'hidden',
      '#value' => variable_get('img_assist_default_link_url', 'http://'),
    );
    $form['link_options_visible'] = array(
      '#type' => 'hidden',
      '#value' => 0,
    );
  }

  // Default link url is needed for JS to indicate if an url has been entered.
  $form['default_url'] = array(
    '#type' => 'hidden',
    '#value' => variable_get('img_assist_default_link_url', 'http://'),
  );

  // Insert Mode (HTML or Filter Tag).
  if (user_access('access advanced options')) {
    $form[] = array(
      '#value' => '<div id="insertmode">',
    );
    $form['insertmode'] = array(
      '#type' => 'select',
      '#title' => t('Insert mode'),
      '#default_value' => variable_get('img_assist_default_insert_mode', 'filtertag'),
      '#options' => array(
        'filtertag' => t('Filter Tag'),
        'html' => t('HTML Code'),
      ),
    );
    $form[] = array(
      '#value' => '</div>',
    );
  }
  else {
    $form['insertmode'] = array(
      '#type' => 'hidden',
      '#value' => variable_get('img_assist_default_insert_mode', 'filtertag'),
    );
  }

  // Hidden Fields.
  $form['nid'] = array(
    '#type' => 'hidden',
    '#value' => $node->nid,
  );
  $form['update'] = array(
    '#type' => 'hidden',
    '#value' => $update,
  );
  $form['aspect'] = array(
    '#type' => 'hidden',
    '#value' => $aspect_ratio,
  );

  // Buttons.
  $form['buttons'] = array(
    '#prefix' => '<div id="buttons">',
    '#suffix' => '</div>',
  );
  $form['#attributes']['onsubmit'] = 'return parent.insertImage();';
  $form['buttons']['insert'] = array(
    '#type' => 'submit',
    '#value' => $update ? t('Update') : t('Insert'),
    '#attributes' => array(
      'style' => 'float: left;',
    ),
  );
  $form['buttons']['cancel'] = array(
    '#type' => 'button',
    '#value' => t('Cancel'),
    '#button_type' => 'button',
    '#attributes' => array(
      'onclick' => 'return parent.cancelAction();',
      'style' => 'float: right;',
    ),
  );
  $form[] = array(
    '#value' => '</td></tr></table></div>',
  );
  $form['#attributes']['name'] = 'img_assist';
  return $form;
}