| 1 |
<?php |
<?php |
| 2 |
// $Id:$ |
// $Id: linkimagefield.module,v 1.2 2008/11/27 06:15:54 challakamal Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 25 |
// Add handlers for previewing new uploads. |
// Add handlers for previewing new uploads. |
| 26 |
foreach ($_SESSION['linkimagefield'] as $fieldname => $files) { |
foreach ($_SESSION['linkimagefield'] as $fieldname => $files) { |
| 27 |
if (is_array($files)) { |
if (is_array($files)) { |
| 28 |
foreach($files as $delta => $file) { |
foreach ($files as $delta => $file) { |
| 29 |
if ($file['preview']) { |
if ($file['preview']) { |
| 30 |
$items[] = array( |
$items[] = array( |
| 31 |
'path' => $file['preview'], |
'path' => $file['preview'], |
| 84 |
'fid' => array('type' => 'int', 'not null' => TRUE, 'default' => '0'), |
'fid' => array('type' => 'int', 'not null' => TRUE, 'default' => '0'), |
| 85 |
'title' => array('type' => 'varchar', length => 255, 'not null' => TRUE, 'default' => "''", 'sortable' => TRUE), |
'title' => array('type' => 'varchar', length => 255, 'not null' => TRUE, 'default' => "''", 'sortable' => TRUE), |
| 86 |
'alt' => array('type' => 'varchar', length => 255, 'not null' => TRUE, 'default' => "''", 'sortable' => TRUE), |
'alt' => array('type' => 'varchar', length => 255, 'not null' => TRUE, 'default' => "''", 'sortable' => TRUE), |
| 87 |
'url' => array('type' => 'varchar', length => 255, 'not null' => TRUE, 'default' => "''", 'sortable' => TRUE), |
'url' => array('type' => 'varchar', length => 255, 'not null' => TRUE, 'default' => "''", 'sortable' => TRUE), |
| 88 |
); |
); |
| 89 |
return $columns; |
return $columns; |
| 90 |
} |
} |
| 100 |
*/ |
*/ |
| 101 |
function linkimagefield_file_insert($node, &$file, $field) { |
function linkimagefield_file_insert($node, &$file, $field) { |
| 102 |
$fieldname = $field['field_name']; |
$fieldname = $field['field_name']; |
| 103 |
$filepath = file_create_path($field['widget']['image_path']) . '/' . $file['filename']; |
$filepath = file_create_path($field['widget']['image_path']) .'/'. $file['filename']; |
| 104 |
if (linkimagefield_check_directory($field['widget']['image_path']) && $file = file_save_upload((object)$file, $filepath)) { |
if (linkimagefield_check_directory($field['widget']['image_path']) && $file = file_save_upload((object)$file, $filepath)) { |
| 105 |
$file = (array)$file; |
$file = (array)$file; |
| 106 |
$file['fid'] = db_next_id('{files}_fid'); |
$file['fid'] = db_next_id('{files}_fid'); |
| 126 |
function linkimagefield_file_update($node, &$file, $field) { |
function linkimagefield_file_update($node, &$file, $field) { |
| 127 |
$file = (array)$file; |
$file = (array)$file; |
| 128 |
if ($file['flags']['delete'] == TRUE) { |
if ($file['flags']['delete'] == TRUE) { |
| 129 |
if(_linkimagefield_file_delete($file, $field['field_name'])) { |
if (_linkimagefield_file_delete($file, $field['field_name'])) { |
| 130 |
return array(); |
return array(); |
| 131 |
} |
} |
| 132 |
} |
} |
| 151 |
if (count($node_field)) { |
if (count($node_field)) { |
| 152 |
$values = array(); |
$values = array(); |
| 153 |
foreach ($node_field as $delta => $file) { |
foreach ($node_field as $delta => $file) { |
| 154 |
if (!empty($file)) { |
if (!empty($file)) { |
| 155 |
$file = _linkimagefield_file_load($file['fid']); |
$file = _linkimagefield_file_load($file['fid']); |
| 156 |
|
|
| 157 |
// In certain cases, content.module calls this function with |
// In certain cases, content.module calls this function with |
| 163 |
unset($node_field[$delta]); |
unset($node_field[$delta]); |
| 164 |
} |
} |
| 165 |
else { // otherwise, merge our info with CCK's, and all is fine. |
else { // otherwise, merge our info with CCK's, and all is fine. |
| 166 |
$node_field[$delta] = array_merge((array)$node_field[$delta], $file); |
$node_field[$delta] = array_merge((array)$node_field[$delta], $file); |
| 167 |
} |
} |
| 168 |
} |
} |
| 169 |
} |
} |
| 170 |
return array($fieldname => $node_field); |
return array($fieldname => $node_field); |
| 171 |
} |
} |
| 172 |
return array(); |
return array(); |
| 173 |
|
|
| 174 |
// called before content.module defaults. |
// called before content.module defaults. |
| 175 |
case 'insert': |
case 'insert': |
|
print_r($field); |
|
| 176 |
foreach ($node_field as $delta => $item) { |
foreach ($node_field as $delta => $item) { |
| 177 |
if($item[url]=="") $item[url]="node/".$node->nid; |
if ($item['url']=="") $item['url'] = "node/". $node->nid; |
| 178 |
$node_field[$delta] = linkimagefield_file_insert($node, $item, $field); |
$node_field[$delta] = linkimagefield_file_insert($node, $item, $field); |
| 179 |
// Remove non-existant images from items |
// Remove non-existant images from items |
| 180 |
if (empty($node_field[$delta])) { |
if (empty($node_field[$delta])) { |
| 181 |
unset($node_field[$delta]); |
unset($node_field[$delta]); |
| 182 |
} |
} |
| 183 |
} |
} |
| 184 |
linkimagefield_clear_field_session($fieldname); |
linkimagefield_clear_field_session($fieldname); |
| 185 |
break; |
break; |
| 187 |
// called before content.module defaults. |
// called before content.module defaults. |
| 188 |
case 'update': |
case 'update': |
| 189 |
foreach ($node_field as $delta => $item) { |
foreach ($node_field as $delta => $item) { |
| 190 |
if($item[url]=="") $item[url]="node/".$node->nid; |
if ($item['url'] == "") $item['url'] = "node/". $node->nid; |
| 191 |
// If we're dealing with a single value field, and we just received |
// If we're dealing with a single value field, and we just received |
| 192 |
// a new file item, we need to mark the existing (old) one for |
// a new file item, we need to mark the existing (old) one for |
| 193 |
// deletion. Otherwise, it will become orphaned. |
// deletion. Otherwise, it will become orphaned. |
| 194 |
/*if (!$field['multiple'] && (count($node_field) > 1) && ($delta === 0)) { |
|
| 195 |
$item['flags']['delete'] = TRUE; |
// Update each file item. |
|
}*/ |
|
|
// Update each file item. |
|
| 196 |
$node_field[$delta] = linkimagefield_file_update($node, $item, $field); |
$node_field[$delta] = linkimagefield_file_update($node, $item, $field); |
| 197 |
|
|
| 198 |
// If the file has been deleted, unset the file entry so that it's |
// If the file has been deleted, unset the file entry so that it's |
| 199 |
// actually deleted from the database, or at least set it to a |
// actually deleted from the database, or at least set it to a |
| 200 |
// default item if CCK won't delete it. |
// default item if CCK won't delete it. |
| 201 |
if (empty($node_field[$delta])) { |
if (empty($node_field[$delta])) { |
| 202 |
if ($field['multiple']) { |
if ($field['multiple']) { |
| 203 |
unset($node_field[$delta]); |
unset($node_field[$delta]); |
| 204 |
} |
} |
| 205 |
else { |
else { |
| 242 |
|
|
| 243 |
case 'form': |
case 'form': |
| 244 |
$form = array(); |
$form = array(); |
| 245 |
$form['max_resolution'] = array ( |
$form['max_resolution'] = array( |
| 246 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 247 |
'#title' => t('Maximum resolution for Images'), |
'#title' => t('Maximum resolution for Images'), |
| 248 |
'#default_value' => $widget['max_resolution'] ? $widget['max_resolution'] : 0, |
'#default_value' => $widget['max_resolution'] ? $widget['max_resolution'] : 0, |
| 277 |
break; |
break; |
| 278 |
|
|
| 279 |
case 'save': |
case 'save': |
| 280 |
return array('max_resolution', 'image_path', 'custom_url','custom_alt', 'custom_title', 'teaser_preset', 'body_preset'); |
return array('max_resolution', 'image_path', 'custom_url', 'custom_alt', 'custom_title', 'teaser_preset', 'body_preset'); |
| 281 |
} |
} |
| 282 |
} |
} |
| 283 |
|
|
| 290 |
* The form element containing the name of the directory to check. |
* The form element containing the name of the directory to check. |
| 291 |
*/ |
*/ |
| 292 |
function linkimagefield_form_check_directory($form_element) { |
function linkimagefield_form_check_directory($form_element) { |
| 293 |
if(!empty($form_element['#value'])) { |
if (!empty($form_element['#value'])) { |
| 294 |
linkimagefield_check_directory($form_element['#value'], $form_element); |
linkimagefield_check_directory($form_element['#value'], $form_element); |
| 295 |
} |
} |
| 296 |
return $form_element; |
return $form_element; |
| 306 |
* A form element to throw an error on if the directory is not writable |
* A form element to throw an error on if the directory is not writable |
| 307 |
*/ |
*/ |
| 308 |
function linkimagefield_check_directory($directory, $form_element = array()) { |
function linkimagefield_check_directory($directory, $form_element = array()) { |
| 309 |
foreach(explode('/', $directory) as $dir) { |
foreach (explode('/', $directory) as $dir) { |
| 310 |
$dirs[] = $dir; |
$dirs[] = $dir; |
| 311 |
$path = file_create_path(implode($dirs,'/')); |
$path = file_create_path(implode($dirs, '/')); |
| 312 |
file_check_directory($path, FILE_CREATE_DIRECTORY, $form_element['#parents'][0]); |
file_check_directory($path, FILE_CREATE_DIRECTORY, $form_element['#parents'][0]); |
| 313 |
} |
} |
| 314 |
return true; |
return TRUE; |
| 315 |
} |
} |
| 316 |
|
|
| 317 |
function _linkimagefield_scale_image($file, $resolution = 0) { |
function _linkimagefield_scale_image($file, $resolution = 0) { |
| 392 |
} |
} |
| 393 |
|
|
| 394 |
// Attach new files |
// Attach new files |
| 395 |
if ($file = file_check_upload($fieldname . '_upload')) { |
if ($file = file_check_upload($fieldname .'_upload')) { |
| 396 |
$file = (array)$file; |
$file = (array)$file; |
| 397 |
if (strpos($file['filemime'],'image') !== FALSE) { |
if (strpos($file['filemime'], 'image') !== FALSE) { |
| 398 |
$file = _linkimagefield_scale_image($file, $field['widget']['max_resolution']); |
$file = _linkimagefield_scale_image($file, $field['widget']['max_resolution']); |
| 399 |
|
|
| 400 |
// Create the filepath for the image preview |
// Create the filepath for the image preview |
| 403 |
if (strpos($filepath, file_directory_path()) !== FALSE) { |
if (strpos($filepath, file_directory_path()) !== FALSE) { |
| 404 |
$filepath = trim(substr($filepath, strlen(file_directory_path())), '\\/'); |
$filepath = trim(substr($filepath, strlen(file_directory_path())), '\\/'); |
| 405 |
} |
} |
| 406 |
$filepath = 'system/files/' . $filepath; |
$filepath = 'system/files/'. $filepath; |
| 407 |
} |
} |
| 408 |
|
|
| 409 |
$file['fid'] = 'upload'; |
$file['fid'] = 'upload'; |
| 426 |
|
|
| 427 |
// Load files from preview state. before committing actions. |
// Load files from preview state. before committing actions. |
| 428 |
if (is_array($_SESSION['linkimagefield'][$fieldname]) && count($_SESSION['linkimagefield'][$fieldname])) { |
if (is_array($_SESSION['linkimagefield'][$fieldname]) && count($_SESSION['linkimagefield'][$fieldname])) { |
| 429 |
foreach($_SESSION['linkimagefield'][$fieldname] as $delta => $file) { |
foreach ($_SESSION['linkimagefield'][$fieldname] as $delta => $file) { |
| 430 |
$node_field[] = $file; |
$node_field[] = $file; |
| 431 |
} |
} |
| 432 |
} |
} |
| 460 |
$form[$fieldname]['upload'] = array( |
$form[$fieldname]['upload'] = array( |
| 461 |
'#type' => 'button', |
'#type' => 'button', |
| 462 |
'#value' => t('Upload'), |
'#value' => t('Upload'), |
| 463 |
'#name' => 'cck_linkimagefield_'.$fieldname.'_op', |
'#name' => 'cck_linkimagefield_'. $fieldname .'_op', |
| 464 |
'#attributes' => array('id' => $fieldname.'-attach-button'), |
'#attributes' => array('id' => $fieldname .'-attach-button'), |
| 465 |
'#tree' => FALSE, |
'#tree' => FALSE, |
| 466 |
'#weight' => 2, |
'#weight' => 2, |
| 467 |
); |
); |
| 468 |
|
|
| 469 |
// Store the file data object to be carried on. |
// Store the file data object to be carried on. |
| 470 |
if (is_array($node_field) && count($node_field)) { |
if (is_array($node_field) && count($node_field)) { |
| 471 |
foreach($node_field as $delta => $file) { |
foreach ($node_field as $delta => $file) { |
| 472 |
if ($file['filepath'] && !$file['flags']['delete']) { |
if ($file['filepath'] && !$file['flags']['delete']) { |
| 473 |
$form[$fieldname][$delta] = array ( |
$form[$fieldname][$delta] = array( |
| 474 |
'#theme' => 'linkimagefield_edit_image_row', |
'#theme' => 'linkimagefield_edit_image_row', |
| 475 |
); |
); |
| 476 |
|
|
| 484 |
|
|
| 485 |
$form[$fieldname][$delta]['preview'] = array( |
$form[$fieldname][$delta]['preview'] = array( |
| 486 |
'#type' => 'markup', |
'#type' => 'markup', |
| 487 |
'#value' => theme('linkimagefield_image', $file, $file['alt'],$file['url'], $file['title'], array('width' => '150'), FALSE), |
'#value' => theme('linkimagefield_image', $file, $file['alt'], $file['url'], $file['title'], array('width' => '150'), FALSE), |
| 488 |
); |
); |
| 489 |
|
|
| 490 |
//drupal_set_message('linkimagefield['. $fieldname .'] '. $op .' node field: <pre>'. print_r($node_field, true) .'</pre>'); |
//drupal_set_message('linkimagefield['. $fieldname .'] '. $op .' node field: <pre>'. print_r($node_field, true) .'</pre>'); |
| 491 |
$form[$fieldname][$delta]['description'] = array( |
$form[$fieldname][$delta]['description'] = array( |
| 492 |
'#type' => 'markup', |
'#type' => 'markup', |
| 493 |
'#value' => '<strong>' . t('Filename: ') . '</strong>' . $file['filename'], |
'#value' => '<strong>'. t('Filename: ') .'</strong>'. $file['filename'], |
| 494 |
); |
); |
| 495 |
|
|
| 496 |
$form[$fieldname][$delta]['alt'] = array( |
$form[$fieldname][$delta]['alt'] = array( |
| 497 |
'#type' => 'hidden', |
'#type' => 'hidden', |
| 498 |
'#value' => $file['filename'], |
'#value' => $file['filename'], |
| 499 |
); |
); |
| 500 |
|
|
| 501 |
// overwrite with an input field if custom_alt is flagged; |
// overwrite with an input field if custom_alt is flagged; |
| 502 |
if ($field['widget']['custom_alt']) { |
if ($field['widget']['custom_alt']) { |
| 503 |
$form[$fieldname][$delta]['alt'] = array( |
$form[$fieldname][$delta]['alt'] = array( |
| 514 |
'#type' => 'hidden', |
'#type' => 'hidden', |
| 515 |
'#value' => $file['filename'], |
'#value' => $file['filename'], |
| 516 |
); |
); |
| 517 |
|
|
| 518 |
// overwrite with an input field if custom_title is flagged; |
// overwrite with an input field if custom_title is flagged; |
| 519 |
if ($field['widget']['custom_title']) { |
if ($field['widget']['custom_title']) { |
| 520 |
$form[$fieldname][$delta]['title'] = array( |
$form[$fieldname][$delta]['title'] = array( |
| 527 |
); |
); |
| 528 |
} |
} |
| 529 |
|
|
| 530 |
|
$form[$fieldname][$delta]['url'] = array( |
| 531 |
$form[$fieldname][$delta]['url'] = array( |
'#type' => 'textfield', |
| 532 |
'#type' => 'textfield', |
'#title' => t('URL'), |
| 533 |
'#title' => t('URL'), |
'#default_value' => $file['url'], |
| 534 |
'#default_value' => $file['url'], |
'#description' => t('URL. Leave blank to point to the respective node.'), |
| 535 |
'#description' => t('URL. Leave blank to point to the respective node.'), |
'#maxlength' => 255, |
| 536 |
'#maxlength' => 255, |
'#size' => 10, |
| 537 |
'#size' => 10, |
); |
|
); |
|
|
|
|
| 538 |
|
|
|
|
|
| 539 |
// Special handling for single value fields |
// Special handling for single value fields |
| 540 |
if (!$field['multiple']) { |
if (!$field['multiple']) { |
| 541 |
$form[$fieldname][$delta]['replace'] = array( |
$form[$fieldname][$delta]['replace'] = array( |
| 549 |
$form[$fieldname][$delta]['flags']['delete'] = array('#type' => 'value', '#value' => $file['flags']['delete']); |
$form[$fieldname][$delta]['flags']['delete'] = array('#type' => 'value', '#value' => $file['flags']['delete']); |
| 550 |
$form[$fieldname][$delta]['title'] = array('#type' => 'value', '#value' => $file['title']); |
$form[$fieldname][$delta]['title'] = array('#type' => 'value', '#value' => $file['title']); |
| 551 |
$form[$fieldname][$delta]['alt'] = array('#type' => 'value', '#value' => $file['alt']); |
$form[$fieldname][$delta]['alt'] = array('#type' => 'value', '#value' => $file['alt']); |
| 552 |
$form[$fieldname][$delta]['url'] = array('#type' => 'value', '#value' => $file['url']); |
$form[$fieldname][$delta]['url'] = array('#type' => 'value', '#value' => $file['url']); |
| 553 |
} |
} |
| 554 |
|
|
| 555 |
$form[$fieldname][$delta]['filename'] = array('#type' => 'value', '#value' => $file['filename']); |
$form[$fieldname][$delta]['filename'] = array('#type' => 'value', '#value' => $file['filename']); |
| 594 |
return ''; |
return ''; |
| 595 |
} |
} |
| 596 |
$file = _linkimagefield_file_load($item['fid']); |
$file = _linkimagefield_file_load($item['fid']); |
| 597 |
return theme('linkimagefield_image', $file, $item['alt'],$item['url'], $item['title']); |
return theme('linkimagefield_image', $file, $item['alt'], $item['url'], $item['title']); |
| 598 |
} |
} |
| 599 |
|
|
| 600 |
|
|
| 603 |
if (!empty($fid) && is_numeric($fid)) { |
if (!empty($fid) && is_numeric($fid)) { |
| 604 |
$result = db_query('SELECT * FROM {files} WHERE fid = %d', $fid); |
$result = db_query('SELECT * FROM {files} WHERE fid = %d', $fid); |
| 605 |
$file = db_fetch_array($result); |
$file = db_fetch_array($result); |
| 606 |
if ($file) { |
if ($file) { |
| 607 |
return $file; |
return $file; |
| 608 |
} |
} |
| 609 |
} |
} |
| 610 |
// return an empty array if nothing was found. |
// return an empty array if nothing was found. |
| 611 |
return array(); |
return array(); |
| 612 |
} |
} |
| 613 |
|
|
| 614 |
function theme_linkimagefield_view_image($file, $alt = '',$url = '', $title = '', $attributes = NULL, $getsize = TRUE) { |
function theme_linkimagefield_view_image($file, $alt = '', $url = '', $title = '', $attributes = NULL, $getsize = TRUE) { |
| 615 |
return theme('linkimagefield_image', $file, $alt,$url, $title, $attributes , $getsize); |
return theme('linkimagefield_image', $file, $alt, $url, $title, $attributes , $getsize); |
| 616 |
} |
} |
| 617 |
|
|
| 618 |
|
|
| 635 |
return $output; |
return $output; |
| 636 |
} |
} |
| 637 |
|
|
| 638 |
function theme_linkimagefield_image($file, $alt = '',$url_link = '', $title = '', $attributes = NULL, $getsize = TRUE) { |
function theme_linkimagefield_image($file, $alt = '', $url_link = '', $title = '', $attributes = NULL, $getsize = TRUE) { |
| 639 |
$file = (array)$file; |
$file = (array)$file; |
| 640 |
if (!$getsize || (is_file($file['filepath']) && (list($width, $height, $type, $image_attributes) = @getimagesize($file['filepath'])))) { |
if (!$getsize || (is_file($file['filepath']) && (list($width, $height, $type, $image_attributes) = @getimagesize($file['filepath'])))) { |
| 641 |
$attributes = drupal_attributes($attributes); |
$attributes = drupal_attributes($attributes); |
| 647 |
|
|
| 648 |
$url = file_create_url($path); |
$url = file_create_url($path); |
| 649 |
$url_link = url($url_link); |
$url_link = url($url_link); |
| 650 |
return '<a href="'.$url_link.'"><img src="'. check_url($url) .'" alt="'. |
return '<a href="'. $url_link .'"><img src="'. check_url($url) .'" alt="'. |
| 651 |
check_plain($alt) .'" title="'. check_plain($title) .'" '. $image_attributes . $attributes .' /></a>'; |
check_plain($alt) .'" title="'. check_plain($title) .'" '. $image_attributes . $attributes .' /></a>'; |
| 652 |
} |
} |
| 653 |
} |
} |