Advertisement
helgatheviki

WP Alchemy Upload/Remove Functions

Aug 9th, 2011
828
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.09 KB | None | 0 0
  1. /*-------------the metabox template --------------------------------*/
  2. /*
  3. * for my JS to work it is necessary that my markup is always the same
  4. * at least for div .slide_preview
  5. */
  6.  
  7. <div class="my_meta_control">
  8.  
  9.     <a href="#" class="deleteall dodelete-artworks button"><span class="icon delete"></span><?php _e('Remove All');?></a>
  10.  
  11.  
  12.     <?php while($mb->have_fields_and_multi('artworks')): ?>
  13.     <?php $mb->the_group_open(); ?>
  14.          
  15.     <a href="#" class="dodelete icon close" title="<?php _e('Remove');?>"><?php _e('Remove');?></a>
  16.  
  17.     <h3 class="slide handle"><?php _e('Artwork #')?><span class="count"><?php echo ($mb->get_the_index() +1 );?></span></h3>
  18.  
  19.       <div class="meta_inside clearfix">   
  20.      
  21.       <div class="slide_preview wide">
  22.         <div class="preview_wrap">
  23.        
  24.         <?php $mb->the_field('image_src'); ?>
  25.        
  26.         <img class="preview" src="<?php if($mb->get_the_value()){$mb->the_value();}else { echo WPALCHEMY . '/images/default_preview.png';}?>" alt="<?php $mb->the_name();?> Preview" />
  27.         </div>
  28.        
  29.         <input type="hidden" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>" class="image_src" />
  30.         <?php $mb->the_field('image_id'); ?>
  31.         <input type="hidden" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>" class="image_id" />
  32.         <?php $mb->the_field('image_alt'); ?>
  33.         <input type="hidden" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>" class="image_alt" />
  34.        
  35.         <?php if($mb->get_the_value('image_src') != "") {$icon = "change"; $button_text = __('Change Image'); $hide = '';} else { $icon = "upload"; $button_text = __('Upload Image'); $hide='hide';} ?>
  36.    
  37.         <button class="upload_image_button button" type="button"><span class="icon <?php echo $icon;?>"></span><?php echo $button_text;?></button>
  38.         <button class="delete_image_button button <?php echo $hide;?>" type="button"><span class="icon delete"></span><?php _e('Remove Image')?></button>
  39.        
  40.       </div>
  41.      
  42.        </div><!--.meta_inside-->
  43.  
  44.  
  45.     <?php $mb->the_group_close(); ?>
  46.     <?php endwhile; ?>
  47.    
  48.  
  49.     <p style="margin-bottom:15px; padding-top:5px;"><a href="#" class="docopy-artworks button"><span class="icon add"></span><?php _e('Add Artwork');?></a></p>
  50.    
  51.     <p class="meta-save"><button type="submit" class="button-primary" name="save"><?php _e('Update');?></button></p>
  52.  
  53.  
  54. </div><!-- .my_meta_control -->
  55.  
  56.  
  57. /*-------------ADD to Functions.php --------------------------------*/
  58.  
  59.  
  60. function my_admin_print_footer_scripts() {  ?>
  61.  
  62. <script type="text/javascript">
  63.  
  64. /* <![CDATA[ */
  65.  
  66.        
  67.             jQuery(document).ready(function($) {
  68.  
  69.                
  70.             /*
  71.              * Upload function
  72.              */
  73.              
  74.              
  75.             var form_src, form_alt, form_id, button, tbframe_interval;
  76.            
  77.  
  78.             $('.my_meta_control').delegate('.upload_image_button','click', function() {
  79.                 form_src = $(this).prevAll('input.image_src');
  80.                 form_alt = $(this).prevAll('input.image_alt');
  81.                 form_id = $(this).prevAll('input.image_id');
  82.                                
  83.                 button = $(this);
  84.                
  85.                 tbframe_interval = setInterval(function() {
  86.                     //change button text
  87.                     $('#TB_iframeContent').contents().find('.savesend .button').val('Use This Image');
  88.                     //remove url, alignment and size fields- auto set to null, none and full respectively
  89.                     $('#TB_iframeContent').contents().find('.url').hide().find('input').val('');
  90.                     $('#TB_iframeContent').contents().find('.align').hide().find('input:radio').filter('[value="none"]').attr('checked', true);
  91.                     $('#TB_iframeContent').contents().find('.image-size').hide().find('input:radio').filter('[value="full"]').attr('checked', true);
  92.                 }, 2000);
  93.                 tb_show('', 'media-upload.php?type=image&tab=library&TB_iframe=true');
  94.                 //tb_show('', 'media-upload.php?type=image&TB_iframe=true');
  95.                 return false;
  96.             });
  97.  
  98.             window.original_send_to_editor = window.send_to_editor;
  99.             window.send_to_editor = function(html){
  100.                 clearInterval(tbframe_interval);
  101.                 if (form_src) {
  102.                
  103.                     //if image links somewhere then the img node will be a child of the returned html
  104.                     if ( $(html).children().length > 0) {
  105.                         src = $('img',html).attr('src');
  106.                         imgclass = $('img',html).attr('class');
  107.                         alt = $('img',html).attr('alt');
  108.                         href = $('a',html).attr('href');
  109.                     } else { //img node IS the returned html
  110.                         src = $(html).attr('src');
  111.                         imgclass = $(html).attr('class');
  112.                         alt = $(html).attr('alt');
  113.                     }
  114.                    
  115.                     if(typeof imgclass != 'undefined') {
  116.                     var imageID = imgclass.match(/([0-9]+)/i);
  117.                         imageID = (imageID && imageID[1]) ? imageID[1] : '' ;
  118.                     }
  119.                        
  120.                     var url = src ? src : href ;
  121.                                
  122.                     form_src.val(url);
  123.                     form_alt.val(alt);
  124.                     form_id.val(imageID);
  125.                     form_src.prevAll('.preview_wrap').children('img').attr('src',url).fadeIn();
  126.                     button.html('<span class="icon change"></span><?php _e('Change Image');?>').next('.delete_image_button').fadeIn();
  127.                     tb_remove();
  128.                     form_src = ''; //reset form_src to null so original works
  129.                 } else {
  130.                     window.original_send_to_editor(html);
  131.                 }
  132.             };
  133.  
  134.            
  135.             /*
  136.              * Remove Function
  137.              */
  138.              
  139.        
  140.             $('.my_meta_control').delegate('.delete_image_button','click', function() {
  141.                 form_src = $(this).prevAll('input.image_src').val('');
  142.                 form_alt = $(this).prevAll('input.image_alt').val('');
  143.                 form_id = $(this).prevAll('input.image_id').val('');
  144.                
  145.                 var img = form_src.prevAll('.preview_wrap').children('img');
  146.                
  147.                 if( img.hasClass('photo')){
  148.                     img.attr('src','<?php echo WPALCHEMY; ?>/images/default_photo.png').fadeIn();
  149.                 } else {
  150.                     img.attr('src','<?php echo WPALCHEMY; ?>/images/default_preview.png').fadeIn();
  151.                 }
  152.                
  153.                 $(this).prev().html('<span class="icon upload"></span><?php _e('Upload Image');?>');
  154.                 $(this).fadeOut();
  155.                 return false;
  156.             });
  157.            
  158.             $('.slide_preview').each(function(){
  159.                 var src = $(this).find('.image_src').val();
  160.                 if(src) { $(this).find('.delete_image_button').show(); } else { $(this).find('.delete_image_button').hide(); }
  161.             });
  162.    
  163.            
  164.             }); //end doc ready
  165.  
  166.    
  167.            
  168.         /* ]]> */</script><?php
  169.     }
  170. //only load on pages and posts!
  171. add_action('admin_footer-post.php','my_admin_print_footer_scripts',99);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement