--- watermark.module 2007-07-03 19:59:46.000000000 +0200 +++ watermark_new.module 2007-07-05 02:52:29.750000000 +0200 @@ -2,6 +2,7 @@ // $Id: watermark.module,v 1.5.2.4 2007/07/03 17:59:47 kbahey Exp $ // Author: Khalid Baheyeldin of http://2bits.com +// Rework and additional features by schnizZzla for BerlinerStrassen.com define('WATERMARK_ENABLE', 'watermark_enable'); define('WATERMARK_PATH', 'watermark_path'); @@ -45,6 +46,10 @@ function watermark_menu($may_cache) { return $items; } +function watermark_perm() { + return array('manage watermarks'); +} + function watermark_admin_settings() { if (!watermark_check_functions()) { return; @@ -227,49 +232,69 @@ function watermark_nodeapi(&$node, $op, return; } - $watermark = variable_get(WATERMARK_PATH, ''); - // $dir removed --> http://drupal.org/node/142178 - // see CHANGELOG.txt and TODO.txt - $location = variable_get(WATERMARK_LOCATION, 0); - - switch ($op) { - case 'validate': - // watermark on preview! - // check for new image upload - if ($node->new_file && is_array($node->images)) { - foreach ($node->images as $label => $filepath) { - if (variable_get(WATERMARK . $label, false)) { - if (_watermark_process($filepath, $watermark, $location)) { - drupal_set_message(t('Watermark applied to image: %file', array('%file' => $label))); - } - else { - drupal_set_message(t('Error adding watermark.'), 'error'); - watchdog('error', 'Error adding watermark'); - } - } + // We hook into node validation to be able to have a watermark on all uploads, also in previews! + if ($op == 'validate') { + // Was a new file uploaded? + $is_new_file = $node->new_file && is_array($node->images); + // Has this node already an image? + $has_image = !empty($node->images['_original']); + // for users with "manage watermarks" permission bypass automatic watermark process + if (user_access('manage watermarks')) { + if ($node->wm_apply) { + // do we need a watermark, is there any image? + if (!$has_image && !$is_new_file) { + return; } + _watermark_apply($node); } - break; + } + // normal user, automatic watermark process + else if ($is_new_file) { + _watermark_apply($node); + } + } +} - case 'insert': - case 'update': - // new image upload check, when the node is saved without preview - if ($node->new_file && is_array($node->images)) { - // We get the image from the database to guarantee it is the correct path, not temp files - $result = db_query('SELECT filename, filepath FROM {files} WHERE nid = %d', $node->nid); - while ($row = db_fetch_object($result)) { - if (variable_get(WATERMARK . $row->filename, false)) { - if (_watermark_process($row->filepath, $watermark, $location)) { - drupal_set_message(t('Watermark applied to image: %file', array( '%file' => $row->filename ) ) ); - } - else { - drupal_set_message(t('Error adding watermark.'), 'error'); - watchdog('error', 'Error adding watermark'); - } - } - } +function watermark_form_alter($form_id, &$form) { + // additional option to toggle watermark process when a user has "manage watermarks" permission + if ($form_id == 'image_node_form' && user_access('manage watermarks')) { + $apply_watermark = true; + + if (preg_match('/^\/node\/[0-9]+\/edit$/i', $form['#action'])) { + $apply_watermark = false; + } + else if ($form['#action'] != '/node/add/image') { + return; + } + + $form['watermark'] = array( + '#type' => 'fieldset', + '#title' => t('Watermark settings'), + '#collapsible' => true, + '#collapsed' => false, + ); + $form['watermark']['wm_apply'] = array( + '#title' => t('Apply watermark'), + '#type' => 'checkbox', + '#description' => t('Automatic watermark process bypass: Please decide yourself if a watermark needs to be applied. Be careful, do not apply watermarks twice!'), + '#default_value' => $apply_watermark, + ); + } +} + +function _watermark_apply($node) { + $watermark = variable_get(WATERMARK_PATH, ''); + $location = variable_get(WATERMARK_LOCATION, 0); + foreach ($node->images as $label => $filepath) { + if (variable_get(WATERMARK . $label, false)) { + if (_watermark_process($filepath, $watermark, $location)) { + drupal_set_message(t('Watermark applied to image: %file', array('%file' => $label))); + } + else { + drupal_set_message(t('Error adding watermark.'), 'error'); + watchdog('error', 'Error adding watermark'); } - break; + } } }