/[drupal]/contributions/modules/linkimagefield/linkimagefield.module
ViewVC logotype

Diff of /contributions/modules/linkimagefield/linkimagefield.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.2, Thu Nov 27 06:15:54 2008 UTC revision 1.3, Wed Jul 1 02:17:28 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id:$  // $Id$
3    
4  /**  /**
5   * @file   * @file
6   * Defines an link image field type.   * Defines a link image field type.
  * linkimagefield uses content.module to store the fid, and the drupal files  
  * table to store the actual file data.  
7   *   *
8     * 6.x Port - Mainly wraps the ImageField CCK Type, adding a URL field
9     * The URL is stored (like the image alt and title tags) in the filefield's data element
10     *
11     * TODO:
12     * - for some reason the display formatter is not being set as the default
13     * -
14   */   */
15    
 function linkimagefield_default_item() {  
   return array(  
     'fid' => 0,  
     'title' => '',  
     'alt' => '',  
     'url' => '',  
   );  
 }  
   
   
 function linkimagefield_menu($maycache) {  
   $items = array();  
   if (!$maycache && $_SESSION['linkimagefield']) {  
     // Add handlers for previewing new uploads.  
     foreach ($_SESSION['linkimagefield'] as $fieldname => $files) {  
       if (is_array($files)) {  
         foreach($files as $delta => $file) {  
           if ($file['preview']) {  
             $items[] = array(  
               'path' => $file['preview'],  
               'callback' => '_linkimagefield_preview',  
               'access' => TRUE,  
               'type' => MENU_CALLBACK,  
             );  
           }  
         }  
       }  
     }  
   }  
   return $items;  
 }  
   
16  /**  /**
17   *  transfer a file that is in a 'preview' state.   * Implementation of hook_init().
18   *  @todo  multiple support   *
19     * Load required includes.
20   */   */
21  function _linkimagefield_preview() {  function linkimagefield_init() {
22    foreach ($_SESSION['linkimagefield'] as $fieldname => $files) {    // If Content, FileField, or ImageField is not available, immediately disable Link Image Field.
23      foreach ($files as $delta => $file) {    $disable = FALSE;
24        if ($file['preview'] == $_GET['q']) {    $message = '';
25          file_transfer($file['filepath'], array('Content-Type: '. mime_header_encode($file['filemime']),  
26                                             'Content-Length: '. $file['filesize']));    if (!module_exists('content')) {
27          exit();      $disable = TRUE;
28        }      $message = t('The <a href="http://drupal.org/project/cck">Content Creation Kit</a> needs to be installed for it to work properly.');
29      }    }
30    
31      if (!module_exists('filefield')) {
32        $disable = TRUE;
33        $message = (!empty($message) ? '<br />' : '') . t('The <a href="http://drupal.org/project/filefield">FileField module</a> needs to be installed for it to work properly.');
34      }
35    
36      if (!module_exists('imagefield')) {
37        $disable = TRUE;
38        $message = (!empty($message) ? '<br />' : '') . t('The <a href="http://drupal.org/project/imagefield">ImageField module</a> needs to be installed for it to work properly.');
39    }    }
 }  
40    
41  /**    if ($disable) {
42   * Implementation of hook_field_info().      module_disable(array('linkimagefield'));
43   */      drupal_set_message(t('The Link Image Field module has been disabled.<br />') . $message);
44  function linkimagefield_field_info() {      return;
45    return array(    }
46      'linkimage' => array('label' => 'Link Image'),  
47    );    // Load include code
48      module_load_include('inc', 'linkimagefield', 'linkimagefield_widget');
49  }  }
50    
51    
52  /**  /**
53   * Implementation of hook_field_settings().   * Implementation of hook_elements().
54   */   */
55  function linkimagefield_field_settings($op, $field) {  function linkimagefield_elements() {
56    switch ($op) {    $elements = array();
     case 'form':  
       $form = array();  
       return $form;  
     case 'validate':  
57    
58        break;    // An LinkImageField is really just a ImageField with a URL.
59      case 'save':    $imagefield_elements = imagefield_elements();
60        return array();    $elements['linkimagefield_widget'] = $imagefield_elements['imagefield_widget'];
61      case 'database columns':    $elements['linkimagefield_widget']['#process'][] = 'linkimagefield_widget_process';
62        $columns = array(    //$elements['linkimagefield_widget']['#element_validate'][] = 'linkimagefield_widget_validate';
         'fid' => array('type' => 'int', 'not null' => TRUE, 'default' => '0'),  
         'title' => array('type' => 'varchar', length => 255, 'not null' => TRUE, 'default' => "''", 'sortable' => TRUE),  
         'alt' => array('type' => 'varchar', length => 255, 'not null' => TRUE, 'default' => "''", 'sortable' => TRUE),  
         'url' => array('type' => 'varchar', length => 255, 'not null' => TRUE, 'default' => "''", 'sortable' => TRUE),  
       );  
       return $columns;  
   }  
 }  
63    
64  /**    // ImageField needs a separate value callback to save its alt, title and URL texts.
65   * insert a file into the database.    $elements['linkimagefield_widget']['#value_callback'] = 'linkimagefield_widget_value';
  * @param $node  
  *    node object file will be associated with.  
  * @param $file  
  *    file to be inserted, passed by reference since fid should be attached.  
  *  
  */  
 function linkimagefield_file_insert($node, &$file, $field) {  
   $fieldname = $field['field_name'];  
   $filepath = file_create_path($field['widget']['image_path']) . '/' . $file['filename'];  
   if (linkimagefield_check_directory($field['widget']['image_path']) && $file = file_save_upload((object)$file, $filepath)) {  
     $file = (array)$file;  
     $file['fid'] = db_next_id('{files}_fid');  
     db_query("INSERT into {files} (fid, nid, filename, filepath, filemime, filesize)  
              VALUES (%d, %d, '%s','%s','%s',%d)",  
             $file['fid'], $node->nid, $file['filename'], $file['filepath'], $file['filemime'], $file['filesize']);  
     return (array)$file;  
   }  
   else {  
     // Include file name in upload error.  
     form_set_error(NULL, t('Image upload was unsuccessful.'));  
     return FALSE;  
   }  
 }  
66    
67      return $elements;
68    }
69    
70  /**  function linkimagefield_theme() {
71   * update the file record if necessary    $theme =  array(
72   * @param $node      'linkimagefield_image' => array(
73   * @param $file        'arguments' => array('file' => NULL, 'alt' => '', 'title' => '', 'attributes' => NULL, 'getsize' => TRUE, 'url' => ''),
74   * @param $field      ),
75   */      // Theme a Link Image Field field item. It calls linkimagefied_image with the proper
76  function linkimagefield_file_update($node, &$file, $field) {      // item properties as arguments.
77    $file = (array)$file;      'linkimagefield_item' => array(
78    if ($file['flags']['delete'] == TRUE) {        'arguments' => array('item' => NULL),
79      if(_linkimagefield_file_delete($file, $field['field_name'])) {      ),
80        return array();      // linkimagefield_widget form element type theme function.
81      }      'linkimagefield_widget' => array(
82    }        'arguments' => array('element' => NULL),
83    if ($file['fid'] == 'upload') {        'file' => 'linkimagefield_widget.inc',
84      return linkimagefield_file_insert($node, $file, $field);      ),
85    }      // Use to generate a preview (admin view) of an linkimagefield item for use in
86    else {      // field item forms and filefield widgets. Invoked by filefield_widget_process.
87      // if fid is not numeric here we should complain.      'linkimagefield_widget_preview' => array(
88      // else we update the file table.        'arguments' => array('item' => NULL),
89        ),
90        // Theme function for the field item elements. allows you to place children
91        // within the context of the parent.
92        'linkimagefield_widget_item' => array(
93          'arguments' => array('element' => NULL),
94        ),
95        // Generates and img tag to the admin thumbnail of an LinkImageField upload.
96        'linkimagefield_admin_thumbnail' => array(
97          'arguments' => array('item' => NULL),
98        ),
99        'linkimagefield_formatter_linkimage' => array(
100          'arguments' => array('element' => NULL),
101          'file' => 'linkimagefield_formatter.inc',
102        ),
103      );
104    
105      $imagecache_presets = module_invoke('imagecache', 'presets');
106      foreach ($imagecache_presets as $preset) {
107        $theme['linkimagefield_formatter_'. $preset['presetname'] .'_linkimage'] = array(
108          'arguments' => array('element' => NULL),
109          'function' => 'theme_linkimagefield_imagecache',
110        );
111    }    }
112    return $file;  
113      return $theme;
114  }  }
115    
116  /**  /**
117   * Implementation of hook_field().   * Implementation of hook_file_download.
118     *
119     * @param string $filepath
120   */   */
121  function linkimagefield_field($op, $node, $field, &$node_field, $teaser, $page) {  function linkimagefield_file_download($filepath) {
122    $fieldname = $field['field_name'];    // Delegate to ImageField
123    switch ($op) {    imagefield_file_download($filepath);
     // called after content.module loads default data.  
     case 'load':  
        if (count($node_field)) {  
         $values = array();  
         foreach ($node_field as $delta => $file) {  
                   if (!empty($file)) {  
             $file = _linkimagefield_file_load($file['fid']);  
   
             // In certain cases, content.module calls this function with  
             // $items[$delta] set, even if the field has not yet been stored at  
             // all or has already been deleted. In that case, $file['fid'] == 0  
             // and file_load() returns an empty array. When that happens,  
             // unset() the delta so that subsequent hooks are not bothered.  
             if (empty($file)) {  
               unset($node_field[$delta]);  
             }  
             else { // otherwise, merge our info with CCK's, and all is fine.  
                   $node_field[$delta]  = array_merge((array)$node_field[$delta], $file);  
                         }  
           }  
                 }  
         return array($fieldname => $node_field);  
       }  
           return array();  
   
     // called before content.module defaults.  
     case 'insert':  
     print_r($field);  
       foreach ($node_field as  $delta => $item) {  
         if($item[url]=="") $item[url]="node/".$node->nid;  
         $node_field[$delta] = linkimagefield_file_insert($node, $item, $field);  
                 // Remove non-existant images from items  
         if (empty($node_field[$delta])) {  
           unset($node_field[$delta]);  
                 }  
       }  
       linkimagefield_clear_field_session($fieldname);  
       break;  
   
     // called before content.module defaults.  
     case 'update':  
       foreach ($node_field as $delta => $item) {  
         if($item[url]=="") $item[url]="node/".$node->nid;  
        // If we're dealing with a single value field, and we just received  
         // a new file item, we need to mark the existing (old) one for  
         // deletion.  Otherwise, it will become orphaned.  
         /*if (!$field['multiple'] && (count($node_field) > 1) && ($delta === 0)) {  
          $item['flags']['delete'] = TRUE;  
         }*/  
         // Update each file item.  
         $node_field[$delta] = linkimagefield_file_update($node, $item, $field);  
   
         // If the file has been deleted, unset the file entry so that it's  
         // actually deleted from the database, or at least set it to a  
         // default item if CCK won't delete it.  
         if (empty($node_field[$delta])) {  
          if ($field['multiple']) {  
             unset($node_field[$delta]);  
           }  
           else {  
             $node_field[$delta] = linkimagefield_default_item();  
           }  
         }  
       }  
       // Compact deltas.  
       $node_field = array_values($node_field);  
       linkimagefield_clear_field_session($fieldname);  
       break;  
   
     case 'delete':  
       foreach ($node_field as $delta => $item) {  
         _linkimagefield_file_delete($item, $field['field_name']);  
       }  
       break;  
   }  
124  }  }
125    
126  /**  /**
127   * Implementation of hook_widget_info().   * Implementation of CCK's hook_widget_info().
128     *
129     * @return array
130   */   */
131  function linkimagefield_widget_info() {  function linkimagefield_widget_info() {
132      $module_path = drupal_get_path('module', 'linkimagefield');
133    return array(    return array(
134      'linkimage' => array(      'linkimagefield_widget' => array(
135        'label' => 'Link Image',        'label' => t('Link Image'),
136        'field types' => array('linkimage'),        'field types' => array('filefield'),
137          'multiple values' => CONTENT_HANDLE_CORE,
138          'callbacks' => array('default value' => CONTENT_CALLBACK_CUSTOM),
139          'description' => t('An edit widget for image files that display as a link, including a preview of the image.'),
140      ),      ),
141    );    );
142  }  }
143    
144  /**  /**
145   * Implementation of hook_widget_settings().   * Implementation of CCK's hook_widget_settings().
146     *
147     * @param string $op
148     * @param array $widget
149     * @return array
150   */   */
151  function linkimagefield_widget_settings($op, $widget) {  function linkimagefield_widget_settings($op, $widget) {
152    switch ($op) {    switch ($op) {
     case 'callbacks':  
       return array('default value' => CONTENT_CALLBACK_CUSTOM);  
   
153      case 'form':      case 'form':
154        $form = array();        return linkimagefield_widget_settings_form($widget);
       $form['max_resolution'] = array (  
         '#type' => 'textfield',  
         '#title' => t('Maximum resolution for Images'),  
         '#default_value' => $widget['max_resolution'] ? $widget['max_resolution'] : 0,  
         '#size' => 15,  
         '#maxlength' => 10,  
         '#description' =>  
         t('The maximum allowed image size expressed as WIDTHxHEIGHT (e.g. 640x480). Set to 0 for no restriction.')  
       );  
       $form['image_path'] = array(  
         '#type' => 'textfield',  
         '#title' => t('Image path'),  
         '#default_value' => $widget['image_path'] ? $widget['image_path'] : '',  
         '#description' => t('Optional subdirectory within the "%dir" directory where images will be stored. Do not include trailing slash.', array('%dir' => variable_get('file_directory_path', 'files'))),  
         '#after_build' => array('linkimagefield_form_check_directory'),  
       );  
   
       $form['custom_alt'] = array(  
         '#type' => 'checkbox',  
         '#title' => t('Enable custom alternate text'),  
         '#default_value' =>  $widget['custom_alt'] ? $widget['custom_alt'] : 0,  
         '#description' => t('Enable custom alternate text for images. Filename will be used if not checked.'),  
       );  
       $form['custom_title'] = array(  
         '#type' => 'checkbox',  
         '#title' => t('Enable custom title text'),  
         '#default_value' =>  $widget['custom_title'] ? $widget['custom_title'] : 0,  
         '#description' => t('Enable custom title text for images. Filename will be used if not checked.'),  
       );  
       return $form;  
   
155      case 'validate':      case 'validate':
156        break;        return linkimagefield_widget_settings_validate($widget);
   
157      case 'save':      case 'save':
158        return array('max_resolution', 'image_path', 'custom_url','custom_alt', 'custom_title', 'teaser_preset', 'body_preset');        return linkimagefield_widget_settings_save($widget);
159    }    }
160  }  }
161    
162  /**  /**
163   * Wrapper function for linkimagefield_check_directory that accepts a form element   * Implementation of CCK's hook_widget
164   * to validate - if user specified one. Won't allow form submit unless the   *
165   * directory exists & is writable   * @param array $form
166   *   * @param array $form_state
167   * @param $form_element   * @param array $field
168   *   The form element containing the name of the directory to check.   * @param array $items
169   */   * @param integer $delta
170  function linkimagefield_form_check_directory($form_element) {   * @return array
171    if(!empty($form_element['#value'])) {   */
172      linkimagefield_check_directory($form_element['#value'], $form_element);  function linkimagefield_widget(&$form, &$form_state, $field, $items, $delta = 0) {
173      if (empty($items[$delta])) {
174        $items[$delta] = array('url' => '');
175    }    }
176    return $form_element;  
177  }    // Delegate to ImageField
178      $element = imagefield_widget($form, $form_state, $field, $items, $delta);
179    
180  /**    return $element;
  * Create the image directory relative to the 'files' dir recursively for every  
  * directory in the path.  
  *  
  * @param $directory  
  *   The directory path under files to check, such as 'photo/path/here'  
  * @param $form_element  
  *   A form element to throw an error on if the directory is not writable  
  */  
 function linkimagefield_check_directory($directory, $form_element = array()) {  
   foreach(explode('/', $directory) as $dir) {  
     $dirs[] = $dir;  
     $path = file_create_path(implode($dirs,'/'));  
     file_check_directory($path, FILE_CREATE_DIRECTORY, $form_element['#parents'][0]);  
   }  
   return true;  
 }  
   
 function _linkimagefield_scale_image($file, $resolution = 0) {  
   $info = image_get_info($file['filepath']);  
   if ($info) {  
     list($width, $height) = explode('x', $resolution);  
     if ($width && $height) {  
       $result = image_scale($file['filepath'], $file['filepath'], $width, $height);  
       if ($result) {  
         $file['filesize'] = filesize($file['filepath']);  
         drupal_set_message(t('The image was resized to fit within the maximum allowed resolution of %resolution pixels', array('%resolution' => $resolution)));  
       }  
     }  
   }  
   return $file;  
181  }  }
182    
183    /**
184  function linkimagefield_clear_session() {   * Implementation of hook_field_formatter_info
185    if (is_array($_SESSION['linkimagefield']) && count($_SESSION['linkimagefield'])) {   *
186      foreach (array_keys($_SESSION['linkimagefield']) as $fieldname) {   * @return array
187        linkimagefield_clear_field_session($fieldname);   */
188      }  function linkimagefield_field_formatter_info() {
189      unset($_SESSION['linkimagefield']);    $imagecache_presets = module_invoke('imagecache', 'presets');
190    }  
191  }    $formatters['linkimage'] = array(
192          'label' => t('Link Image'),
193  function linkimagefield_clear_field_session($fieldname) {        'field types' => array('image','filefield'),
194    if (is_array($_SESSION['linkimagefield'][$fieldname]) && count($_SESSION['linkimagefield'][$fieldname])) {        'description' => t('Displays image files as link to provided URL.'),
195      foreach ($_SESSION['linkimagefield'][$fieldname] as $delta => $file) {    );
196        if (is_file($file['filepath'])) {  
197          file_delete($file['filepath']);    foreach ($imagecache_presets as $preset) {
198        }      $formatters[$preset['presetname'] .'_linkimage'] = array(
199      }        'label' => t('@preset Link Image', array('@preset' => $preset['presetname'])),
200      unset($_SESSION['linkimagefield'][$fieldname]);        'field types' => array('image', 'filefield'),
201        );
202    }    }
203      return $formatters;
204  }  }
205    
206  function _linkimagefield_file_delete($file, $fieldname) {  /*
207    if (is_numeric($file['fid'])) {  function linkimagefield_field_formatter($field, $item, $formatter, $node) {
208      db_query('DELETE FROM {files} WHERE fid = %d', $file['fid']);    if (empty($item['fid']) && $field['use_default_image']) {
209    }      $item = $field['default_image'];
210    else {    }
211      unset($_SESSION['linkimagefield'][$fieldname][$file['sessionid']]);    // Views does not load the file for us, while CCK display fields does.
212      if (empty($item['filepath'])) {
213        $item = array_merge($item, field_file_load($item['fid']));
214      }
215      if (is_string($item['data'])) {
216        $item['data'] = unserialize($item['data']);
217      }
218      $alt = empty($item['data']['alt']) ? '' : $item['data']['alt'];
219      $title = empty($item['data']['title']) ? '' : $item['data']['title'];
220      $url = empty($item['data']['url']) ? '' : $item['data']['url'];
221      $parts =  explode('_', $formatter);
222      $style = array_pop($parts);
223      $presetname = implode('_', $parts);
224    
225      $class = "linkimagefield imagecache imagecache-$presetname imagecache-$style imagecache-$formatter";
226      if ($preset = imagecache_preset_by_name($presetname)) {
227        $item['filepath'] = $item['fid'] == 'upload' ? $item['preview'] : $item['filepath'];
228        $imagetag = theme('imagecache', $presetname, $item['filepath'], $alt, $title);
229        return l($imagetag, $url, array('attributes' => array('class' => $class), 'html' => TRUE));
230    }    }
231    return file_delete($file['filepath']);    return '<!-- linkimagefield formatter imagecache preset('. $presetname .') not found! -->';
232  }  }*/
233    
234  /**  /**
235   * Implementation of hook_widget().   * Implementation of hook_form_[form_id]_alter().
236     *
237     * Modify the add new field form to make "LinkImage" the default formatter.
238   */   */
239  function linkimagefield_widget($op, &$node, $field, &$node_field) {  function linkimagefield_form_content_field_overview_form_alter(&$form, &$form_state) {
240    $fieldname = $field['field_name'];    $form['#submit'][] = 'linkimagefield_form_content_field_overview_submit';
   $content_type = $field['type_name'];  
   switch ($op) {  
     case 'default value':  
       return array();  
   
     case 'prepare form values':  
       _linkimagefield_widget_prepare_form_values($node, $field, $node_field);  
       return;  
   
     case 'form':  
       $form = _linkimagefield_widget_form($node, $field, $node_field);  
       return $form;  
   
     case 'validate':  
       _linkimagefield_widget_form_validate($node, $field, $node_field);  
       return;  
   }  
241  }  }
242    
243  function _linkimagefield_widget_prepare_form_values($node, $field, &$node_field) {  /**
244    $fieldname = $field['field_name'];   * Submit handler to set a new field's formatter to "linkimage_urllink".
245    // clean up the session if we weren't posted.   */
246    if (!count($_POST)) {  function linkimagefield_form_content_field_overview_submit(&$form, &$form_state) {
247      linkimagefield_clear_session();    if (isset($form_state['fields_added']['_add_new_field']) && isset($form['#type_name'])) {
248    }      $new_field = $form_state['fields_added']['_add_new_field'];
249        $node_type = $form['#type_name'];
250    // Attach new files      $field = content_fields($new_field, $node_type);
251    if ($file = file_check_upload($fieldname . '_upload')) {      if ($field['widget']['module'] == 'linkimagefield') {
252      $file = (array)$file;        foreach ($field['display_settings'] as $display_type => $display_settings) {
253      if (strpos($file['filemime'],'image') !== FALSE) {          if ($field['display_settings'][$display_type]['format'] == 'default') {
254        $file = _linkimagefield_scale_image($file, $field['widget']['max_resolution']);            $field['display_settings'][$display_type]['format'] = 'linkimage_urllink';
   
       // Create the filepath for the image preview  
       $filepath = file_create_filename($file['filename'], file_create_path($field['widget']['image_path']));  
       if (variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC) ==  FILE_DOWNLOADS_PRIVATE) {  
         if (strpos($filepath, file_directory_path()) !== FALSE) {  
           $filepath = trim(substr($filepath, strlen(file_directory_path())), '\\/');  
         }  
         $filepath = 'system/files/' . $filepath;  
       }  
   
       $file['fid'] = 'upload';  
       $file['preview'] = $filepath;  
   
       // If a single field, mark any other images for deletion and delete files in session  
       if (!$field['multiple']) {  
         if (is_array($node_field)) {  
           foreach ($node_field as $delta => $session_file) {  
             $node_field[$delta]['flags']['delete'] = TRUE;  
           }  
         }  
         linkimagefield_clear_field_session($fieldname);  
       }  
       // Add the file to the session  
       $file_id = count($node_field) + count($_SESSION['linkimagefield'][$fieldname]);  
       $_SESSION['linkimagefield'][$fieldname][$file_id] = $file;  
     }  
   }  
   
   // Load files from preview state. before committing actions.  
   if (is_array($_SESSION['linkimagefield'][$fieldname]) && count($_SESSION['linkimagefield'][$fieldname])) {  
     foreach($_SESSION['linkimagefield'][$fieldname] as $delta => $file) {  
       $node_field[] = $file;  
     }  
   }  
 }  
   
 function _linkimagefield_widget_form($node, $field, &$node_field) {  
   
   $fieldname = $field['field_name'];  
   drupal_add_css(drupal_get_path('module', 'linkimagefield') .'/linkimagefield.css');  
   
   $form = array();  
   $form[$fieldname] = array(  
     '#type' => 'fieldset',  
     '#title' => t($field['widget']['label']),  
     '#weight' => $field['widget']['weight'],  
     '#collapsible' => TRUE,  
     '#collapsed' => FALSE,  
     '#tree' => TRUE,  
   );  
   
   // Seperate from tree becase of that silly things won't be  
   // displayed if they are a child of '#type' = form issue  
   $form[$fieldname][$fieldname .'_upload'] = array(  
     '#type'  => 'file',  
     '#title' => $delta == 0 ? t('Image') : NULL,  
     '#description' => $field['widget']['description'],  
     '#tree' => FALSE,  
     '#weight' => 1,  
   );  
   
   $form[$fieldname]['upload'] = array(  
     '#type' => 'button',  
     '#value' => t('Upload'),  
     '#name' => 'cck_linkimagefield_'.$fieldname.'_op',  
     '#attributes' => array('id' => $fieldname.'-attach-button'),  
     '#tree' => FALSE,  
     '#weight' => 2,  
   );  
   
   // Store the file data object to be carried on.  
   if (is_array($node_field) && count($node_field)) {  
     foreach($node_field as $delta => $file) {  
       if ($file['filepath'] && !$file['flags']['delete']) {  
         $form[$fieldname][$delta] = array (  
           '#theme' => 'linkimagefield_edit_image_row',  
         );  
   
         $form[$fieldname][$delta]['flags']['delete'] = array(  
           '#type' => 'checkbox',  
           '#title' => t('Delete'),  
           '#default_value' => 0,  
         );  
   
         $filename = $file['fid'] == 'upload' ? file_create_filename($file['filename'], file_create_path($field['widget']['image_path'])) : $file['filepath'];  
   
         $form[$fieldname][$delta]['preview'] = array(  
           '#type' => 'markup',  
           '#value' => theme('linkimagefield_image', $file, $file['alt'],$file['url'], $file['title'], array('width' => '150'), FALSE),  
         );  
   
       //drupal_set_message('linkimagefield['. $fieldname .'] '. $op .' node field: <pre>'. print_r($node_field, true) .'</pre>');  
         $form[$fieldname][$delta]['description'] = array(  
           '#type' => 'markup',  
           '#value' => '<strong>' . t('Filename: ') . '</strong>' . $file['filename'],  
         );  
   
         $form[$fieldname][$delta]['alt'] = array(  
           '#type' => 'hidden',  
           '#value' => $file['filename'],  
         );  
   
         // overwrite with an input field if custom_alt is flagged;  
         if ($field['widget']['custom_alt']) {  
           $form[$fieldname][$delta]['alt'] = array(  
             '#type' => 'textfield',  
             '#title' =>  t('Alternate text'),  
             '#default_value' => $file['alt'],  
             '#description' => t('Alternate text to be displayed if the image cannot be displayed.'),  
             '#maxlength' => 255,  
             '#size' => 10,  
           );  
         }  
   
         $form[$fieldname][$delta]['title'] = array(  
           '#type' => 'hidden',  
           '#value' => $file['filename'],  
         );  
   
         // overwrite with an input field if custom_title is flagged;  
         if ($field['widget']['custom_title']) {  
           $form[$fieldname][$delta]['title'] = array(  
             '#type' => 'textfield',  
             '#title' =>  t('Title'),  
             '#default_value' =>  $file['title'],  
             '#description' => t('Text to be displayed on mouse overs.'),  
             '#maxlength' => 255,  
             '#size' => 10,  
           );  
         }  
   
   
           $form[$fieldname][$delta]['url'] = array(  
             '#type' => 'textfield',  
             '#title' =>  t('URL'),  
             '#default_value' =>  $file['url'],  
             '#description' => t('URL. Leave blank to point to the respective node.'),  
             '#maxlength' => 255,  
             '#size' => 10,  
           );  
   
   
   
         // Special handling for single value fields  
         if (!$field['multiple']) {  
           $form[$fieldname][$delta]['replace'] = array(  
             '#type' => 'markup',  
             '#value' => t('If a new image is chosen, the current image will be replaced upon submitting the form.'),  
           );  
255          }          }
256        }        }
257        elseif ($file['filepath'] && $file['flags']['delete']) {        content_field_instance_update($field);
         // Hide all the form values if this item is marked for deletion  
         $form[$fieldname][$delta]['flags']['delete'] = array('#type' => 'value', '#value' => $file['flags']['delete']);  
         $form[$fieldname][$delta]['title'] = array('#type' => 'value', '#value' => $file['title']);  
         $form[$fieldname][$delta]['alt'] = array('#type' => 'value', '#value' => $file['alt']);  
         $form[$fieldname][$delta]['url'] = array('#type' => 'value', '#value' => $file['url']);  
       }  
   
       $form[$fieldname][$delta]['filename'] = array('#type' => 'value',  '#value' => $file['filename']);  
       $form[$fieldname][$delta]['filepath'] = array('#type' => 'value',  '#value' => $file['filepath']);  
       $form[$fieldname][$delta]['filemime'] = array('#type' => 'value',  '#value' => $file['filemime']);  
       $form[$fieldname][$delta]['filesize'] = array('#type' => 'value',  '#value' => $file['filesize']);  
       $form[$fieldname][$delta]['fid'] = array('#type' => 'value',  '#value' => $file['fid']);  
   
     }  
   }  
   return $form;  
 }  
   
 function _linkimagefield_widget_form_validate($node, $field, $node_field) {  
   $fieldname = $field['field_name'];  
   if ($field['required']) {  
     if (!count($node_field)) {  
       form_set_error($fieldname, $field['widget']['label'] .' is required.');  
258      }      }
259    }    }
260  }  }
261    
262  /**  /**
263   * Implementation of hook_field_formatter_info().   * Implementation of CCK's hook_default_value().
264   */   */
265  function linkimagefield_field_formatter_info() {  function linkimagefield_default_value(&$form, &$form_state, $field, $delta) {
266    $formatters = array(    return imagefield_default_value($form, $form_state, $field, $delta);
     'default' => array(  
       'label' => 'Default',  
       'field types' => array('linkimage'),  
     ),  
   );  
   return $formatters;  
 }  
   
 /**  
  * Implementation of hook_field_formatter().  
  *  
  */  
 function linkimagefield_field_formatter($field, $item, $formatter) {  
   if (empty($item['fid'])) {  
     return '';  
   }  
   $file = _linkimagefield_file_load($item['fid']);  
   return theme('linkimagefield_image', $file, $item['alt'],$item['url'], $item['title']);  
267  }  }
268    
269    function theme_linkimagefield_formatter($element) {
270  function _linkimagefield_file_load($fid = NULL) {    if (isset($element['#item']['nid']) && $node = node_load($element['#item']['nid'])) {
271    // Don't bother if we weren't passed and fid.      return linkimagefield_field_formatter($element['#field_name'], $element['#item'], $element['#formatter'], $node);
   if (!empty($fid) && is_numeric($fid)) {  
     $result = db_query('SELECT * FROM {files} WHERE fid = %d', $fid);  
     $file = db_fetch_array($result);  
             if ($file) {  
           return $file;  
     }  
272    }    }
   // return an empty array if nothing was found.  
   return array();  
273  }  }
274    
275  function theme_linkimagefield_view_image($file, $alt = '',$url = '', $title = '', $attributes = NULL, $getsize = TRUE) {  function theme_linkimagefield_image($file, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE, $url = '') {
276    return theme('linkimagefield_image', $file, $alt,$url, $title, $attributes , $getsize);    return '<a href="'. $url .'">'. theme('imagefield_image', $file, $alt, $title, $attributes, $getsize) .'</a>';
277  }  }
278    
279    function theme_linkimagefield_imagecache($element) {
280      $field = content_fields($element['#field_name']);
281      $item = $element['#item'];
282      $formatter = $element['#formatter'];
283    
284      if (empty($item['fid']) && $field['use_default_image']) {
285        $item = $field['default_image'];
286      }
287      // Views does not load the file for us, while CCK display fields does.
288      if (empty($item['filepath'])) {
289        $item = array_merge($item, field_file_load($item['fid']));
290      }
291      if (is_string($item['data'])) {
292        $item['data'] = unserialize($item['data']);
293      }
294      $alt = empty($item['data']['alt']) ? '' : $item['data']['alt'];
295      $title = empty($item['data']['title']) ? '' : $item['data']['title'];
296      $url = empty($item['data']['url']) ? '' : $item['data']['url'];
297      $parts =  explode('_', $formatter);
298      $style = array_pop($parts);
299      $presetname = implode('_', $parts);
300    
301      $class = "linkimagefield imagecache imagecache-$presetname imagecache-$style imagecache-$formatter";
302  function theme_linkimagefield_edit_image_row($element) {    if ($preset = imagecache_preset_by_name($presetname)) {
303    $output = '<div class="linkimagefield-edit-preview">'. drupal_render($element['preview']) .'</div>';      $item['filepath'] = $item['fid'] == 'upload' ? $item['preview'] : $item['filepath'];
304    $output .= '<div class="linkimagefield-edit-image-detail">';      $imagetag = theme('imagecache', $presetname, $item['filepath'], $alt, $title);
305    $output .= '<div class="linkimagefield-edit-image-flags">'. drupal_render($element['flags']) .'</div>';      return l($imagetag, $url, array('attributes' => array('class' => $class), 'html' => TRUE));
   $output .= '<div class="linkimagefield-edit-image-description">'. drupal_render($element['description']);  
   $output .= '</div>';  
   $output .= drupal_render($element['alt']);  
   $output .= drupal_render($element['title']);  
   $output .= drupal_render($element['url']);  
   $output .= '</div>';  
   //$output .= '<div class="linkimagefield-edit-image-fid">'. $element['fid']['#value'] .'</div>';  
   $output = '<div class="linkimagefield-edit-image-row clear-block">'. $output .'</div>';  
   if (isset($element['replace'])) {  
     $output .= '<div class="linkimagefield-edit-image-replace">'. drupal_render($element['replace']) .'</div>';  
306    }    }
307    return $output;    return '<!-- linkimagefield formatter imagecache preset('. $presetname .') not found! -->';
308    
309            /*if (isset($element['#item']['nid']) && $node = node_load($element['#item']['nid'])) {
310        $imagecache_html = module_invoke('imagecache', 'field_formatter', $element['#field_name'], $element['#item'], $element['#formatter'], $node);
311        //return '<a href="'. $element['#item']['data']['url'] .'">'. $imagecache_html .'</a>';
312        return l($imagecache_html, $element['#item']['data']['url'], array('attributes' => array('class' => $class), 'html' => TRUE));
313      }*/
314  }  }
315    
316  function theme_linkimagefield_image($file, $alt = '',$url_link = '', $title = '', $attributes = NULL, $getsize = TRUE) {  function theme_linkimagefield_item($item) {
317    $file = (array)$file;    return theme('linkimagefield_image', $item, $item['alt'], $item['title']);
   if (!$getsize || (is_file($file['filepath']) && (list($width, $height, $type, $image_attributes) = @getimagesize($file['filepath'])))) {  
     $attributes = drupal_attributes($attributes);  
   
     $path = $file['fid'] == 'upload' ? $file['preview'] : $file['filepath'];  
     $alt = empty($alt) ? $file['alt'] : $alt;  
     //$url_link;  
     $title = empty($title) ? $file['title'] : $title;  
   
     $url = file_create_url($path);  
     $url_link = url($url_link);  
     return '<a href="'.$url_link.'"><img src="'. check_url($url) .'" alt="'.  
         check_plain($alt) .'" title="'. check_plain($title) .'" '. $image_attributes . $attributes .' /></a>';  
   }  
318  }  }
319    
320  /**  function theme_linkimagefield_widget_item($element) {
321   * formats an array of images.    return theme('imagefield_widget_item', $element);
  * @param images  
  *    array of individually themed images  
  * @return  
  *    html string  
  */  
 function theme_linkimagefield_multiple($images) {  
   return implode("\n", $images);  
322  }  }
323    
324  /**  function theme_linkimagefield_widget_preview($item = NULL) {
325   * implementation of hook_filedownload    return '<div class="imagefield-preview">'. theme('imagefield_admin_thumbnail', $item) .'</div>';
326   * replicated from upload.module.  }
  *  
  * conditionally included since we're just replicating the  
  * work of upload.module for now.  
  */  
327    
328  if (!function_exists('upload_file_download')) {  function theme_linkimagefield_admin_thumbnail($item = NULL) {
329    function linkimagefield_file_download($file) {    return theme_imagefield_admin_thumbnail($item);
     $file = file_create_path($file);  
     $result = db_query("SELECT f.* FROM {files} f WHERE filepath = '%s'", $file);  
     if ($file = db_fetch_object($result)) {  
       if (user_access('view uploaded files')) {  
         $node = node_load($file->nid);  
         if (node_access('view', $node)) {  
           $name = mime_header_encode($file->filename);  
           $type = mime_header_encode($file->filemime);  
           // Serve images and text inline for the browser to display rather than download.  
           $disposition = ereg('^(text/|image/)', $file->filemime) ? 'inline' : 'attachment';  
           return array(  
             'Content-Type: '. $type .'; name='. $name,  
             'Content-Length: '. $file->filesize,  
             'Content-Disposition: '. $disposition .'; filename='. $name,  
             'Cache-Control: private'  
           );  
         }  
         else {  
           return -1;  
         }  
       }  
       else {  
         return -1;  
       }  
     }  
   }  
330  }  }

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.3

  ViewVC Help
Powered by ViewVC 1.1.2