Example #1
0
function cp_edit_ad_images($ad_id)
{
    global $post;
    $args = array('post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $ad_id, 'order' => 'ASC', 'orderby' => 'ID');
    // get all the images associated to this ad
    $images = get_posts($args);
    // print_r($images); // for debugging
    ?>
	
	<script type="text/javascript">
	//<![CDATA[	
	jQuery(document).ready(function() {
		/* upload printable coupon image */
		jQuery('input#upload_image_button').click(function() {
			tb_show('', 'media-upload.php?post_id=<?php 
    echo $post->ID;
    ?>
&amp;type=image&amp;TB_iframe=true');
			return false;
		});

		/* send the uploaded image url to the field */
		window.send_to_editor = function(html) {
			var s = jQuery('img',html).attr('class'); // get the class with the image id
			var imageID = parseInt(/wp-image-(\d+)/.exec(s)[1], 10); // now grab the image id from the wp-image class
			var imgurl = jQuery('img',html).attr('src'); // get the image url
			var imgoutput = '<a href="' + imgurl + '" target="_blank"><img src="' + imgurl + '" /></a>'; //get the html to output for the image preview
			
			jQuery('#cp_print_url').val(imgurl); // return the image url to the field
			jQuery('input[name=new_ad_image_id]').val(imageID); // return the image url to the field			
			jQuery('#cp_print_url').siblings('.upload_image_preview').slideDown().html(imgoutput); // display the uploaded image thumbnail
			tb_remove();
		}

	});	
	//]]>
	</script>

	<table class="form-table ad-meta-table">
	
	<?php 
    // make sure we have images associated to the ad
    if ($images) {
        $i = 1;
        foreach ($images as $image) {
            // go get the width and height fields since they are stored in meta data
            $meta = wp_get_attachment_metadata($image->ID);
            if (is_array($meta) && array_key_exists('width', $meta) && array_key_exists('height', $meta)) {
                $media_dims = "<span id='media-dims-" . $image->ID . "'>{$meta['width']}&nbsp;&times;&nbsp;{$meta['height']}</span> ";
            }
            ?>
		
		
		<tr>
			<th style="width:20%"><label><?php 
            _e('Image', 'appthemes');
            ?>
 <?php 
            echo $i;
            ?>
:</label></th>

			<td>
				<div class="thumb-wrap-edit">
					<?php 
            echo cp_get_attachment_link($image->ID);
            ?>
				</div>

				<div class="image-meta">
					<input class="checkbox" type="checkbox" name="image[]" value="<?php 
            echo $image->ID;
            ?>
">&nbsp;<?php 
            _e('Delete Image', 'appthemes');
            ?>
<br />
					<strong><?php 
            _e('Upload Date:', 'appthemes');
            ?>
</strong> <?php 
            echo mysql2date(get_option('date_format'), $image->post_date);
            ?>
<br />
					<strong><?php 
            _e('File Info:', 'appthemes');
            ?>
</strong> <?php 
            echo $media_dims;
            ?>
 (<?php 
            echo $image->post_mime_type;
            ?>
)<br />
				</div>
				
				<div class="clr"></div>

				<?php 
            $alt = get_post_meta($image->ID, '_wp_attachment_image_alt', true);
            // get the alt text and print out the field
            ?>
				
				<p class="alt-text">
					<div class="labelwrapper">
						<label><?php 
            _e('Alt Text:', 'appthemes');
            ?>
</label>
					</div>
					<input type="text" class="text" name="attachments[<?php 
            echo $image->ID;
            ?>
][image_alt]" id="image_alt" value="<?php 
            if (count($alt)) {
                echo esc_attr(stripslashes($alt));
            }
            ?>
" />
				</p>
				
			</td>
			
		</tr>	

		<?php 
            $i++;
        }
    }
    ?>
	
		<tr>
			<th style="width:20%"><label><?php 
    _e('New Image', 'appthemes');
    ?>
:</label></th>

			<td>
				<input style="display:none;" type="text" readonly name="cp_print_url" id="cp_print_url" class="upload_image_url text" value="" />
				<input id="upload_image_button" class="upload_button button" rel="cp_print_url" type="button" value="<?php 
    _e('Add Image', 'appthemes');
    ?>
" />							
				<br />
				<div class="upload_image_preview"></div>
				<input type="text" class="hide" id="imageid" name="new_ad_image_id" value="" />	
			</td>
			
		</tr>
	
	</table>
	
	<?php 
}
    function cp_get_ad_images($ad_id)
    {
        $args = array('post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $ad_id, 'order' => 'ASC', 'orderby' => 'ID', 'no_found_rows' => true);
        // get all the images associated to this ad
        $images = get_posts($args);
        // print_r($images); // for debugging
        // get the total number of images already on this ad
        // we need it to figure out how many upload fields to show
        $imagecount = count($images);
        // make sure we have images associated to the ad
        if ($images) {
            $i = 1;
            $media_dims = '';
            foreach ($images as $image) {
                // go get the width and height fields since they are stored in meta data
                $meta = wp_get_attachment_metadata($image->ID);
                if (is_array($meta) && array_key_exists('width', $meta) && array_key_exists('height', $meta)) {
                    $media_dims = "<span id='media-dims-" . $image->ID . "'>{$meta['width']}&nbsp;&times;&nbsp;{$meta['height']}</span> ";
                }
                ?>
				<li class="images">
					<div class="labelwrapper">
						<label><?php 
                _e('Image', APP_TD);
                ?>
 <?php 
                echo $i;
                ?>
:</label>
					</div>

					<div class="thumb-wrap-edit">
						<?php 
                echo cp_get_attachment_link($image->ID);
                ?>
					</div>

					<div class="image-meta">
						<p class="image-delete"><input class="checkbox" type="checkbox" name="image[]" value="<?php 
                echo $image->ID;
                ?>
">&nbsp;<?php 
                _e('Delete Image', APP_TD);
                ?>
</p>
						<p class="image-meta"><strong><?php 
                _e('Upload Date:', APP_TD);
                ?>
</strong> <?php 
                echo appthemes_display_date($image->post_date, 'date');
                ?>
</p>
						<p class="image-meta"><strong><?php 
                _e('File Info:', APP_TD);
                ?>
</strong> <?php 
                echo $media_dims;
                ?>
 <?php 
                echo $image->post_mime_type;
                ?>
</p>
					</div>

					<div class="clr"></div>

				<?php 
                // get the alt text and print out the field
                $alt = get_post_meta($image->ID, '_wp_attachment_image_alt', true);
                ?>
					<p class="alt-text">
						<div class="labelwrapper">
							<label><?php 
                _e('Alt Text:', APP_TD);
                ?>
</label>
						</div>
						<input type="text" class="text" name="attachments[<?php 
                echo $image->ID;
                ?>
][image_alt]" id="image_alt" value="<?php 
                if (count($alt)) {
                    echo esc_attr(stripslashes($alt));
                }
                ?>
" />
					</p>

					<div class="clr"></div>
				</li>
			<?php 
                $i++;
            }
        }
        // returns a count of array keys so we know how many images currently
        // are being used with this ad. this value is needed for cp_ad_edit_image_input_fields()
        return $imagecount;
    }