Index: image_title.module
===================================================================
--- image_title.module (revision 418)
+++ image_title.module (working copy)
@@ -1,177 +1,360 @@
'radios',
- '#title' => t('Default image title'),
- '#default_value' => variable_get('image_title_'. $form['#node_type']->type, 0),
- '#options' => array(t('Disabled'), t('Enabled')),
- '#description' => t('Enable this to have image title for this content type.'),
- );
+function image_title_menu_router_form() {
+ $item = menu_get_item();
+
+ //load any existing file data
+ $image_title = db_fetch_array(db_query('SELECT image FROM {image_title_menu} WHERE path = "%s"', $item['path']));
+ $picture = theme('image_title', $image_title['image']);
+
+ $collapsed = TRUE;
+ if ($picture) {
+ $collapsed = FALSE;
}
- elseif (isset($form['type'])) {
- $type = $form['type']['#value'];
- }
- elseif (isset($form['orig_type'])) {
- $type = $form['orig_type']['#value'];
- }
- else {
- return;
- }
- // check node type and match settings
- if($form_id == $type .'_node_form' && user_access('add image title') && (variable_get('image_title_'. $type, 0) == 1) ){
-
- $form['image_title'] = array( '#type' => 'fieldset',
- '#title' => t('Title Image'),
- '#weight' => 1,
- '#collapsible' => TRUE,
- '#collapsed' => TRUE,
- );
- $form['image_title']['image_title_upload'] = array(
- '#type' => 'file',
- '#title' => t('Attach title image'),
- '#size' => 40,
- );
+ $form = array();
+ $form['menu_router_item'] = array(
+ '#type' => 'value',
+ '#value' => $item,
+ );
+ $form['#attributes']['enctype'] = 'multipart/form-data';
+
+ $form['image_title_upload'] = array(
+ '#type' => 'fieldset',
+ '#title' => t('Title image'),
+ '#collapsible' => TRUE,
+ '#collapsed' => $collapsed,
+ );
+ $form['image_title_upload']['image_title_upload'] = array(
+ '#type' => 'file',
+ '#title' => t('Attach title image'),
+ '#size' => 40,
+ );
- $node = $form['#node'];
-
- $picture = theme("image_title", $node);
- $form['image_title']['current_timage'] = array('#value' => $picture);
- $form['image_title']['current_timage_v'] = array('#type' => 'checkbox',
- '#default_value' => $node->image_title_status,
- '#title' => t('Show this image in place of title'),
- '#description' => t('Check this box to override title with image uploaded.'),
- ); // used as status
-
- $form['#attributes']['enctype'] = 'multipart/form-data';
-
+ if ($picture) {
+ //show the image
+ $form['image_title_upload']['current_title_image'] = array('#value' => $picture);
+ //set the delete flag
+ $form['image_title_upload']['image_title_delete'] = array(
+ '#type' => 'checkbox',
+ '#default_value'=> 0,
+ '#title' => t('Delete title image.'),
+ );
}
+ $form['image_title_upload']['submit'] = array(
+ '#value' => t('Save title image settings'),
+ '#type' => 'submit',
+ );
+
+ return $form;
}
+
/**
- * theming image field for title for node form ..
+ * Validation for title image upload form on pages that are not nodes
*/
-function theme_image_title($node) {
+function image_title_menu_router_form_validate($form, &$form_state) {
+ if(!empty($_FILES['files']['name']['image_title_upload'])) {
+ $fullpath = file_directory_path().'/'.variable_get('image_title_directory', 'img-title');
+ //file_check_directory() creates a form error automatically
+ file_check_directory($fullpath, 0, 'image_title_upload');
+ }
+}
- if ($node->image_title && file_exists($node->image_title)) {
- $picture = $node->image_title; // file_create_url($node->image_title);
+/**
+ * Submit function for title image upload form on hook_menu provided pages
+ */
+function image_title_menu_router_form_submit($form, &$form_state) {
+ //load any existing file data
+ $image_title = db_fetch_array(db_query('SELECT image FROM {image_title_menu} WHERE path = "%s"', $form_state['values']['menu_router_item']['path']));
+
+ //set delete flag according to form entry
+ $delete = $form_state['values']['image_title_delete'];
+ //if delete is requested, delete the unwanted image
+ if ($delete == 1) {
+ if (file_delete($image_title['image'])) {
+ drupal_set_message(t('Title image deleted.'));
}
+ //we don't want the database record either
+ db_query('DELETE FROM {image_title_menu} WHERE path = "%s"', $form_state['values']['menu_router_item']['path']);
+ }
+ //print_r($_FILES);
+ //now handle the upload
+ if(!empty($_FILES['files']['name']['image_title_upload'])) {
+ //set upload parameters
+ $fullpath = file_directory_path().'/'.variable_get('image_title_directory', 'img-title');
+ $validators = array(
+ 'file_validate_is_image' => array(),
+ );
+ $overwrite = 0;
+ if ($file = file_save_upload('image_title_upload', $validators, $fullpath, $overwrite)) {
+ file_set_status($file, FILE_STATUS_PERMANENT);
+ //we don't want the old image - delete it
+ file_delete($image_title['image']);
+ db_query('DELETE FROM {image_title_menu} WHERE path = "%s"', $form_state['values']['menu_router_item']['path']);
+ db_query('INSERT INTO {image_title_menu} (path, image) VALUES ("%s", "%s")', $form_state['values']['menu_router_item']['path'], $file->filepath);
+ drupal_set_message(t('Title image saved.'));
+ } else {
+ drupal_set_message(t('Failed to upload the title image.'), 'error');
+ }
+ }
+}
- if (isset($picture)) {
- $alt = t( $node->title );
- $picture = theme('image', $picture, $alt, $alt, '', FALSE);
-
- return "
$picture
";
+/**
+ * Implementation of hook_form_alter() to add title image field to node forms
+ */
+function image_title_form_alter(&$form, $form_state, $form_id) {
+ //check user is permitted to create image titles
+ if (user_access('create image titles')) {
+
+ //load an existing picture
+ $node = $form['#node'];
+ $picture = theme('image_title', $node->image_title);
+
+ $collapsed = TRUE;
+ if ($picture) {
+ $collapsed = FALSE;
}
+
+ // We're only modifying node forms, if the type field isn't set we don't need
+ // to bother; otherwise, store it for later retrieval.
+ if (isset($form['type'])) {
+ $type = $form['type']['#value'];
+ }
+ elseif (isset($form['orig_type'])) {
+ $type = $form['orig_type']['#value'];
+ }
+ else {
+ return;
+ }
+ // check node type and match settings
+ if($form_id == $type .'_node_form' ){
+ $form['image_title'] = array(
+ '#type' => 'fieldset',
+ '#title' => t('Title image'),
+ '#weight' => 40,
+ '#collapsible' => TRUE,
+ '#collapsed' => $collapsed,
+ );
+ $form['image_title']['image_title_upload'] = array(
+ '#type' => 'file',
+ '#title' => t('Attach title image'),
+ '#size' => 40,
+ );
+ if ($picture) {
+ //show the image
+ $form['image_title']['current_title_image'] = array('#value' => $picture);
+ // set the delete flag
+ $form['image_title']['image_title_delete'] = array(
+ '#type' => 'checkbox',
+ '#default_value'=> 0,
+ '#title' => t('Delete title image.'),
+ );
+ }
+ $form['#attributes']['enctype'] = 'multipart/form-data';
+ }
+ }
+}
+
+/**
+ * theming image field for title for node form ..
+ *
+ * @param $path
+ * a string representing the full Drupal path to the image
+ */
+function theme_image_title($path) {
+ if ($path && file_exists($path)) {
+ $picture = theme('image', $path, t('Title image preview.'), t('Title image preview.'), '', FALSE);
+ return "$picture
";
+ }
}
/**
- * nodeapi to save image for title
+ * Implementation of hook_theme()
*/
+function image_title_theme(){
+ return array(
+ 'image_title' => array(
+ 'arguments' => array('node' => NULL),
+ ),
+ );
+}
+
+/**
+ * Implementation of hook_nodeapi() to manage title images on nodes
+ * (save, update, delete, etc.)
+ */
function image_title_nodeapi(&$node, $op, $teaser, $page) {
- global $form_values;
-
- if( !user_access('add image title') || (variable_get('image_title_'. $node->type, 0) == 0) ) {
- return; // do nothing ...
- }
switch ($op) {
-
case 'validate':
-
- $form_values['image_title'] = $node->image_title;
- if ($file = file_check_upload('image_title_upload')) {
- if (!form_get_errors()) {
- $path = file_directory_path();
- if ($file1 = file_save_upload('image_title_upload', $path.'/img-title-'. $node->nid .''. $file->filename, 1)) {
- $form_values['image_title'] = $file1->filepath;
- }
- else {
- form_set_error('image_title', t("Failed to upload the image title; the %directory directory doesn't exist or is not writable.", array('%directory' => $path.'/img-title-'. $node->nid)));
- }
- }
+ if(!empty($_FILES['files']['name']['image_title_upload'])) {
+ $path = file_directory_path();
+ $fullpath = $path.'/'.variable_get('image_title_directory', 'img-title');
+ if (!file_check_directory($fullpath)) {
+ form_set_error('image_title_upload', t('Failed to upload the image title; the %directory directory doesn\'t exist or is not writable.', array('%directory' => $fullpath)));
+ }
}
-
break;
- case 'load':
-
- $image_title = db_fetch_array(db_query('SELECT image, status FROM {image_title} WHERE nid = %d', $node->nid));
-
- return array('image_title' => $image_title['image'], 'image_title_status' => $image_title['status'] );
-
- break;
-
+ case 'update':
case 'insert':
- db_query('INSERT INTO {image_title} (nid, image) VALUES (%d, "%s")', $node->nid, $form_values['image_title']);
-
- break;
-
- case 'update':
-
- if($form_values['current_timage_v']) {
- $status = $form_values['current_timage_v'];
- }else {
- $status = 0; // fine
+ //load any existing file data
+ $image_title = db_fetch_array(db_query('SELECT image FROM {image_title_node} WHERE nid = %d', $node->nid));
+
+ //set delete flag according to node form entry
+ $delete = 0;
+ if($node->image_title_delete) {
+ $delete = $node->image_title_delete;
}
- // insert update/database here ...
- if( $form_values['image_title'] != '' ) {
- db_query('DELETE FROM {image_title} WHERE nid = %d', $node->nid);
- db_query('INSERT INTO {image_title} (nid, image, status) VALUES (%d, "%s", %d)', $node->nid, $form_values['image_title'], $status);
- }else {
- db_query('update {image_title} set status = %d WHERE nid = %d', $status, $node->nid);
+ //if delete is requested, delete the unwanted image
+ if ($delete == 1) {
+ if (file_delete($image_title['image'])) {
+ drupal_set_message(t('Title image deleted.'));
+ }
+ //we don't want the database record either
+ db_query('DELETE FROM {image_title_node} WHERE nid = %d', $node->nid);
}
- break;
+ //now handle the upload
+ if(!empty($_FILES['files']['name']['image_title_upload'])) {
+ //set upload parameters
+ $fullpath = file_directory_path().'/'.variable_get('image_title_directory', 'img-title');
+ $validators = array(
+ 'file_validate_is_image' => array(),
+ );
+ $overwrite = 0;
+ if ($file = file_save_upload('image_title_upload', $validators, $fullpath, $overwrite)) {
+ file_set_status($file, FILE_STATUS_PERMANENT);
+ //we don't want the old image - delete it
+ file_delete($image_title['image']);
+ db_query('DELETE FROM {image_title_node} WHERE nid = %d', $node->nid);
+ db_query('INSERT INTO {image_title_node} (nid, image) VALUES (%d, "%s")', $node->nid, $file->filepath);
+ } else {
+ drupal_set_message(t('Failed to upload the title image.'), 'error');
+ }
+ }
+ break;
+ case 'load':
+ $image_title = db_fetch_array(db_query('SELECT image FROM {image_title_node} WHERE nid = %d', $node->nid));
+ return array('image_title' => $image_title['image']);
+ break;
+
case 'delete':
- db_query('DELETE FROM {image_title} WHERE nid = %d', $node->nid);
- break;
+ //clean up any orphan title images
+ $image_title = db_fetch_array(db_query('SELECT image FROM {image_title_node} WHERE nid = %d', $node->nid));
+ if (file_exists($image_title['image'])) {
+ file_delete($image_title['image']);
+ }
+ //delete the record
+ db_query('DELETE FROM {image_title_node} WHERE nid = %d', $node->nid);
+ break;
+ }
+}
- case 'view': // not confident here -- avail for detail page only ...
- if( $node->image_title_status == 1 ) {
+/**
+ * function extending the available page variables
+ *
+ * replaces the text title with the title image, where available, and
+ * offers the form for title image upload as a variable for theming
+ */
+function image_title_preprocess_page(&$variables) {
+ if (arg(0) === 'node' && is_numeric(arg(1))) {
+ $thisnode = node_load($variables['node']->nid);
+ if($thisnode->image_title){
+ $image = theme('image', $thisnode->image_title, $thisnode->title, $thisnode->title, '', FALSE);
+ $title = '' . $thisnode->title . '' . $image;
+ $variables['title'] = $title;
+ }
+ } else {
+ if (user_access('create image titles')) {
+ //form for title image upload optionally available in a variable
+ $variables['image_title_form'] = drupal_get_form('image_title_menu_router_form');
+ }
+ //load menu item page data
+ $item = menu_get_item();
+ //check for an image for this page
+ if ($image_title = db_fetch_array(db_query('SELECT image FROM {image_title_menu} WHERE path = "%s"', $item['path']))) {
+ $image = theme('image', $image_title['image'], $item['title'], $item['title'], '', FALSE);
+ $title = '' . $item['title'] . '' . $image;
+ $variables['title'] = $title;
+ //we set another variable here because some modules may override $title
+ //later on, so you can in turn override their values in your theme
+ //template.php file with an appropriate preprocess containing simply:
+ // $variables['title'] = $variables['image_title'];
+ $variables['image_title'] = $title;
+ }
+ }
+}
- if(arg(0) == 'node' && arg(1) != '' && is_numeric(arg(1)) ) {
- // add this image to title.. ?? on teaser too ? yes
- drupal_set_title(theme('image', $node->image_title, $node->title, $node->title, '', FALSE));
- }else {
- // you may use this var in .tpl for customizing
- // $node->title = theme('image', $node->image_title, $node->title, $node->title, '', FALSE);
- // above can't be done as there is some text restriction...
- $node->title = ''; // is it good ??
- $node->content['image_title'] = array(
- '#value' => l(theme('image', $node->image_title, $node->title, $node->title, '', FALSE), 'node/'.$node->nid,array(),'','',false,true),
- '#weight' => -1,
- );
- }
- }
- break;
+/**
+ * Implementation of hook_block.
+ *
+ * offers title image upload form as a block
+ */
+function image_title_block($op = 'list', $delta = 0) {
+ switch ($op) {
+ case 'list':
+ $block[0]['info'] = t('Title image upload');
+
+ return $block;
+ case 'view':
+ if (user_access('create image titles')) {
+ switch ($delta) {
+ case 0:
+ $images = image_get_latest();
+ $block['subject'] = t('Title image upload');
+ $block['content'] = drupal_get_form('image_title_menu_router_form');
+ break;
+ }
+ }
+ return $block;
}
}
-// view is still need to be updated .. add to ur template
-/* add this to ur theme page.tpl.php where title is printed
- image_title_status && $node->image_title != '' ) {
- echo theme('image', $node->image_title);
- }else {
- ?>
-
-
- */
\ No newline at end of file
+/**
+ * Implementation of hook_menu().
+ */
+function image_title_menu() {
+ //the admin settings page
+ $items['admin/settings/image-title'] = array(
+ 'title' => t('Image title'),
+ 'description' => t('Settings for Image title module.'),
+ 'page callback' => 'drupal_get_form',
+ 'page arguments' => array('image_title_admin_form'),
+ 'access arguments' => array('administer image titles'),
+ 'type' => MENU_NORMAL_ITEM,
+ 'file' => 'image_title.admin.inc',
+ );
+ return $items;
+}
+
+/**
+ * Implementation of hook_cron()
+ * cron tasks to tidy up images for deleted pages outside the scope of nodeapi
+ */
+function image_title_cron() {
+ $images = db_fetch_array(db_query('SELECT * FROM {image_title_menu}'));
+ foreach ($images as $image) {
+ if (!$menu_path = db_fetch_array(db_query('SELECT path FROM {menu_router} WHERE path = "%s"', $image['path']))) {
+ file_delete($image['image']);
+ db_query('DELETE FROM {image_title_menu} WHERE path = "%s"', $image['path']);
+ }
+ }
+}
+