<?php
/*
Plugin Name: Whoismanu PhotoQ
Version: 1.0.1b
Plugin URI: http://www.whoismanu.com/blog/2007/01/photoq-wordpress-plugin/
Description: Adds queue based photo-upload capability to Wordpress
Author: Manuel Flury
Author URI: http://www.whoismanu.com
*/

/*load text domain for localization*/
load_plugin_textdomain('PhotoQ');


// wimpq_add_admin_pages() is the sink function for the 'admin_menu' hook
function wimpq_add_admin_pages() {
    
// Add a new menu under Options:
    
add_options_page(__('PhotoQ Options','PhotoQ'), 'PhotoQ', 8, basename(__FILE__), 'wimpq_options_page');

    
// Add a new menu under Manage:
    
add_management_page(__('Manage PhotoQ', 'PhotoQ'), 'PhotoQ', 8, basename(__FILE__), 'wimpq_manage_page');

}


// wimpq_options_page() displays the page content for the Options submenu
function wimpq_options_page() {
    
    if (isset(
$_POST['info_update']) || isset($_POST['add_field']))
    {     
            
update_option('wimpq_qdir', $_POST['qdir']);
              
update_option('wimpq_imgdir', $_POST['imgdir']);
              
update_option('wimpq_thumb_constraint', $_POST['thumbsize']);
              switch (
$_POST['thumbsize']){
                  case
'height':
                      
update_option('wimpq_thumb_size', $_POST['thumb_height']);
                    break;
                  case
'width':
                      
update_option('wimpq_thumb_size', $_POST['thumb_width']);
                      break;
                  case
'side':
                      
update_option('wimpq_thumb_size', $_POST['thumb_side']);
                      break;
            }
            
            
update_option('wimpq_image_constraint', $_POST['imagesize']);
              switch (
$_POST['imagesize']){
                  case
'height':
                      
update_option('wimpq_image_size', $_POST['image_height']);
                    break;
                  case
'width':
                      
update_option('wimpq_image_size', $_POST['image_width']);
                      break;
                  case
'side':
                      
update_option('wimpq_image_size', $_POST['image_side']);
                      break;
            }
            
update_option('wimpq_freq', $_POST['cron_freq']);
            
$del_imgs = !isset($_POST["del_imgs"]) ? NULL : $_POST["del_imgs"];
            
update_option('wimpq_delimg', $del_imgs);
            
$noimgresize = !isset($_POST["no_img_resize"]) ? NULL : $_POST["no_img_resize"];
            
update_option('wimpq_no_img_resize', $noimgresize);
            
$add_posted = !isset($_POST["add_posted"]) ? NULL : $_POST["add_posted"];
            
update_option('wimpq_add_posted', $add_posted);
            
$delete_posted = !isset($_POST["delete_posted"]) ? NULL : $_POST["delete_posted"];
            
update_option('wimpq_delete_posted', $delete_posted);
            
$rename_posted = !isset($_POST["rename_posted"]) ? NULL : $_POST["rename_posted"];
            
update_option('wimpq_rename_posted', $rename_posted);
            
            
        
?>
        <div id="message" class="updated fade"><p><strong><?php _e('Options saved.') ?></strong></p></div>

        <?php
    
} ?>
    
    <?php if (isset($_POST['add_field'])) {  ?>
        <div class=wrap>
        <form method="post" action="options-general.php?page=whoismanu-photoq.php">
            <h2>PhotoQ Options</h2>
            
             <fieldset class="options" name="set1">
                <legend><?php _e('Add new Meta Field', 'PhotoQ') ?></legend>
                
                <table width="100%" cellspacing="2" cellpadding="5" class="editform">
                <tr valign="top">
                    <th width="33%" scope="row"><label for="qdir">
                        <?php _e('Field Name:', 'PhotoQ'); ?>
                        </label>
                    </th>
                    <td>
                        <input type="text" name="fieldname" size="50" maxlength="100"/>
                    </td>
                </tr>
                </table>
             </fieldset>
             <p class="submit">
                  <input type="submit" name="insert_field"
                      value="<?php _e('Add field', 'PhotoQ') ?> &raquo;" />
              </p>
          </form>
          </div>
        
    
    
    <?php } else { //we are not adding meta fields
    
        //we are inserting a field into the database
        
if(isset($_POST['insert_field'])){
            
wimpq_insert_field($_POST['fieldname']);
        }
        
        
//we are renaming a field
        
elseif(isset($_POST['rename_field'])){
            
wimpq_rename_field($_POST['field_id'], $_POST['field_name']);
        }
    
        
        
//we are deleting a field from the database
        
elseif ($_GET['action'] == 'delete') {
            
wimpq_remove_field( $_GET['entry']);
        }
    
    
    
?>
    <div class=wrap>
          <form method="post" action="options-general.php?page=whoismanu-photoq.php">
            <h2>PhotoQ Options</h2>
            
             <fieldset class="options" name="set1">
                <legend><?php _e('Directories', 'PhotoQ') ?></legend>
                
                <table width="100%" cellspacing="2" cellpadding="5" class="editform">
                <tr valign="top">
                    <th width="33%" scope="row"><label for="qdir">
                        <?php _e('Queue Directory:', 'PhotoQ'); ?>
                        </label>
                    </th>
                    <td>
                        <input type="text" name="qdir" size="50" maxlength="100"
                        value="<?php if (get_option('wimpq_qdir')) echo get_option('wimpq_qdir');?>" />
                        <br>Default is <code>/wp-content/qdir</code>
                    </td>
                </tr>
                <tr valign="top">
                    <th scope="row">
                        <label for="imgdir">
                        <?php _e('Image Directory:', 'PhotoQ'); ?>
                        </label>
                    </th>
                    <td>
                        <input type="text" name="imgdir" size="50" maxlength="100"
                        value="<?php if (get_option('wimpq_imgdir')) echo get_option('wimpq_imgdir');?>" />
                        <br>Default is <code>/wp-content</code>
                    </td>
                </tr>
                </table>
             </fieldset>
             <fieldset class="options" name="set2">
                <legend><?php _e('Meta Fields', 'PhotoQ') ?></legend>
                <table width="100%" cellspacing="2" cellpadding="5" class="editform">
                    <tr valign="top">
                        <th width="33%" scope="row">
                            <label for="del_imgs"><?php _e('Upon Add:', 'PhotoQ'); ?></label>
                        </th>
                        <td>
                            <input type="checkbox" name="add_posted" <?php if(get_option('wimpq_add_posted') == 'on'){echo 'checked="checked"';} ?> />
                            <?php _e('Add to already posted as well.', 'PhotoQ'); ?>
                        </td>
                    </tr>
                    <tr valign="top">
                        <th width="33%" scope="row">
                            <label for="del_imgs"><?php _e('Upon Delete:', 'PhotoQ'); ?></label>
                        </th>
                        <td>
                            <input type="checkbox" name="delete_posted" <?php if(get_option('wimpq_delete_posted') == 'on'){echo 'checked="checked"';} ?> />
                            <?php _e('Delete from already posted as well.', 'PhotoQ'); ?>
                        </td>
                    </tr>
                    <tr valign="top">
                        <th width="33%" scope="row">
                            <label for="del_imgs"><?php _e('Upon Rename:', 'PhotoQ'); ?></label>
                        </th>
                        <td>
                            <input type="checkbox" name="rename_posted" <?php if(get_option('wimpq_rename_posted') == 'on'){echo 'checked="checked"';} ?> />
                            <?php _e('Rename already posted as well.', 'PhotoQ'); ?>
                        </td>
                    </tr>
                    
                </table>
                
                <table width="500px" cellspacing="2" cellpadding="5" class="editform">

                <?php     
                        wimpq_list_metafields
('options');
                        
                
?>
                
                <tr valign="top">
                
                    <td >&nbsp;</td>
                    <td width="100px">&nbsp;</td>
                    
                    <td width="100px"><label for="qdir">
                        <div class="submit">
                      <input type="submit" name="add_field"
                      value="<?php _e('Add meta field', 'PhotoQ') ?> &raquo;" />
                  </div>
                        </label>
                    </td>
                </tr>
                </table>
             </fieldset>
             <fieldset class="options" name="set3">
                <legend><?php _e('Image sizes', 'PhotoQ') ?></legend>
                
                <table width="100%" cellspacing="2" cellpadding="5" class="editform">
                    <tr valign="top">
                        <td> <?php _e('Thumbnail size', 'PhotoQ') ?> </td>
                    </tr>
                    <tr valign="top">
                        <th width="33%" scope="row">
                            <label for="thumbsize_height">
                                <input type="radio" name="thumbsize" value="height" id="thumbsize_height" <?php if (get_option('wimpq_thumb_constraint') == 'height') echo "checked";?> />
                                <?php _e('Fixed height:', 'PhotoQ') ?>
                            </label>
                        </th>
                        <td>
                              <input type="text" name="thumb_height" size="4" value="<?php if (get_option('wimpq_thumb_size') && get_option('wimpq_thumb_constraint') == 'height') echo get_option('wimpq_thumb_size');?>" />px
                        </td>
                    </tr>
                    <tr valign="top">
                        <th scope="row">
                            <label for="thumbsize_width">
                                <input type="radio" name="thumbsize" value="width" id="thumbsize_width" <?php if (get_option('wimpq_thumb_constraint') == 'width') echo "checked";?> />
                                <?php _e('Fixed width:', 'PhotoQ') ?>
                            </label>
                        </th>
                        <td>
                              <input type="text" name="thumb_width" size="4" value="<?php if (get_option('wimpq_thumb_size') && get_option('wimpq_thumb_constraint') == 'width') echo get_option('wimpq_thumb_size');?>" />px
                        </td>
                    </tr>
                    <tr valign="top">
                        <th scope="row">
                            <label for="thumbsize_side">
                                <input type="radio" name="thumbsize" value="side" id="thumbsize_side" <?php if (get_option('wimpq_thumb_constraint') == 'side') echo "checked";?> />
                                <?php _e('Largest side:', 'PhotoQ') ?>
                            </label>
                        </th>
                        <td>
                              <input type="text" name="thumb_side" size="4" value="<?php if (get_option('wimpq_thumb_size') && get_option('wimpq_thumb_constraint') == 'side') echo get_option('wimpq_thumb_size');?>" />px
                
                        </td>
                    </tr>
                    
                </table>
        
                        
                <table width="100%" cellspacing="2" cellpadding="5" class="editform">
                    <tr valign="top">
                        <td> <?php _e('Image size', 'PhotoQ') ?> </td>
                    </tr>
                    <tr valign="top">
                        <th width="33%" scope="row">
                            <label for="imagesize_height">
                                <input type="radio" name="imagesize" value="height" id="imagesize_height" <?php if (get_option('wimpq_image_constraint') == 'height') echo "checked";?> />
                                <?php _e('Fixed height:', 'PhotoQ') ?>
                            </label>
                        </th>
                        <td>
                              <input type="text" name="image_height" size="4" value="<?php if (get_option('wimpq_image_size') && get_option('wimpq_image_constraint') == 'height') echo get_option('wimpq_image_size');?>" />px
                        </td>
                    </tr>
                    <tr valign="top">
                        <th scope="row">
                            <label for="imagesize_width">
                                <input type="radio" name="imagesize" value="width" id="imagesize_width" <?php if (get_option('wimpq_image_constraint') == 'width') echo "checked";?> />
                                <?php _e('Fixed width:', 'PhotoQ') ?>
                            </label>
                        </th>
                        <td>
                              <input type="text" name="image_width" size="4" value="<?php if (get_option('wimpq_image_size') && get_option('wimpq_image_constraint') == 'width') echo get_option('wimpq_image_size');?>" />px
                        </td>
                    </tr>
                    <tr valign="top">
                        <th scope="row">
                            <label for="imagesize_side">
                                <input type="radio" name="imagesize" value="side" id="imagesize_side" <?php if (get_option('wimpq_image_constraint') == 'side') echo "checked";?> />
                                <?php _e('Largest side:', 'PhotoQ') ?>
                            </label>
                        </th>
                        <td>
                              <input type="text" name="image_side" size="4" value="<?php if (get_option('wimpq_image_size') && get_option('wimpq_image_constraint') == 'side') echo get_option('wimpq_image_size');?>" />px
                        </td>
                    </tr>
                    <tr valign="top">
                        <th scope="row">
                            &nbsp;
                        </th>
                        <td>
                            <input type="checkbox" name="no_img_resize" <?php if(get_option('wimpq_no_img_resize') == 'on'){echo 'checked="checked"';} ?> />
                            <?php _e("Keep original image size, don't resize.", 'PhotoQ'); ?>
                        </td>
                    </tr>
                </table>    
             </fieldset>
             <fieldset class="options" name="set4">
                <legend><?php _e('Further Options', 'PhotoQ') ?></legend>
                <table width="100%" cellspacing="2" cellpadding="5" class="editform">
                    <tr valign="top">
                        <th width="33%" scope="row">
                            <label for="cron_freq"><?php _e('Cron Job Frequency:', 'PhotoQ'); ?></label>
                        </th>
                        <td>
                            <input type="text" name="cron_freq" size="3" maxlength="100" value="<?php if (get_option('wimpq_freq')) echo get_option('wimpq_freq');?>" />
                            <?php _e('hours', 'PhotoQ'); ?>
                        </td>
                    </tr>
                    <tr valign="top">
                        <th width="33%" scope="row">
                            <label for="del_imgs"><?php _e('Deleting Posts:', 'PhotoQ'); ?></label>
                        </th>
                        <td>
                            <input type="checkbox" name="del_imgs" <?php if(get_option('wimpq_delimg') == 'on'){echo 'checked="checked"';} ?> />
                            <?php _e('Delete image files from server when deleting post.', 'PhotoQ'); ?>
                        </td>
                    </tr>
                </table>
             </fieldset>
             
            <p class="submit">
                  <input type="submit" name="info_update"
                      value="<?php _e('Update options', 'PhotoQ') ?> &raquo;" />
              </p>
          </form>
     </div>
    
     <?php } //end if(edit)
}



// wimpq_manage_page() displays the page content for the Manage submenu
function wimpq_manage_page() {

    
    
    if (isset(
$_POST['add_entry']))
    {
        
/*a photo will be added*/
    
?>
        <div class=wrap>

             <form method="post" enctype="multipart/form-data" action="edit.php?page=whoismanu-photoq.php">
                <h2>Manage PhotoQ - Upload</h2>    
                <div id="poststuff">
                <fieldset id="categorydiv">
                  <legend><a href="http://wordpress.org/docs/reference/post/#category" title="<?php _e('Help on categories') ?>"><?php _e('Categories') ?></a></legend>
                  <div><?php wimpq_dropdown_categories(); ?></div>
                </fieldset>
                
                
                
                <p>
                  <label for="uploadedfile"><?php _e('File:') ?></label>
                  <br />
                <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo get_settings('fileupload_maxk') * 1024 ?>" />
                <input type="file" name="uploadedfile" size="35" class="uploadform" /></p>
                   <?php printf(__('You can upload files as long as they are no larger than %1$s <abbr title="Kilobytes">KB</abbr>. If you&#8217;re an admin you can configure these values under <a href="%2$s">options</a>.'), get_settings('fileupload_maxk'), 'options-misc.php') ?></p>
                <p>
                <label for="img_title"><?php _e('Title:') ?></label><br />
                <input type="text" name="img_title" size="30" class="uploadform" />
                </p>
                <p>
                <label for="img_descr"><?php _e('Description:') ?></label><br />
                <textarea style="font-size:small;" name="img_descr" cols="70" rows="10" class="uploadform"></textarea>
                </p>
                <p>
                <label for="img_slug"><?php _e('Post Slug:') ?></label><br />
                <input type="text" name="img_slug" size="30" class="uploadform" />
                </p>
                
                <?php wimpq_list_metafields('manage'); ?>
                
                <input type="hidden" name="action" value="save" />
                <div class="submit">
                      <input type="submit" name="submit_entry"
                      value="<?php _e('Upload Photo to Queue', 'PhotoQ') ?> &raquo;" />
                  </div>
                  </div>
              </form>
        </div>
    
        
    
    
    <?php
    
}else{
    
    
    
    if (isset(
$_POST['submit_entry'])) {
        
$status = wimpq_upload_photo($_POST['uploadedfile'], $_POST['img_title'], $_POST['img_descr'], $_POST['img_slug']);
    
    }
    
    if (isset(
$_POST['update_queue'])) {
        
wimpq_update_queue($_POST['img_name'], $_POST['img_title'], $_POST['img_descr'], $_POST['img_slug'], $_POST['img_position'], $_POST['img_old_position'], $_POST['q_length']);
    }
    
    if (
$_GET['action'] == 'delete') {
        
$status = wimpq_delete_photo( $_GET['entry'], $_GET['qlen'], $_GET['pos']);
    }
    
    if (isset(
$_POST['post_first'])) {
        
$status = wimpq_post_next();
    }
    
    
/*get the queue*/
    
$queue = wimpq_get_queue();
    
$q_length = sizeof($queue);
    
    
    if (
$status) {
        if(
$status["type"] == 'error'){
        
?>
            <div id="message" class="error"><p><strong><?php echo $status["message"]; ?></strong></p></div>
    <?php } else { ?>
            <div id="message" class="updated fade"><p><strong><?php echo $status["message"]; ?></strong></p></div>
    <?php }} ?>
    
    
    <div class=wrap>
             <form method="post" action="edit.php?page=whoismanu-photoq.php">
                <h2>Manage PhotoQ</h2>        
                <input type="hidden" name="q_length" value="<?php echo $q_length; ?>" />

                <?php if ($_GET['action'] == 'edit') {  ?>
                <div id="poststuff">
                <fieldset id="categorydiv">
                  <legend><a href="http://wordpress.org/docs/reference/post/#category" title="<?php _e('Help on categories') ?>"><?php _e('Categories') ?></a></legend>
                  <div><?php wimpq_dropdown_categories($queue[$_GET['entry']]->q_imgname, $default_post_cat); ?></div>
                </fieldset>
                
        
                
                <?php
                
                    wimpq_list_metafields
('edit_queue',$queue[$_GET['entry']]->q_imgname);
                
                }
                    if(
$queue){
                        echo
"Queue length: ".$q_length;
                        
                        
?>
                        <table width="100%" cellpadding="3" cellspacing="3">
                            <tr>
                        
                            <th scope="col">Position</th>
                            <th scope="col">Thumbnail</th>
                            <th scope="col">Name</th>
                            <th scope="col">Title</th>
                            <th scope="col">Description</th>
                            <!--<th scope="col">Place</th>
                            <th scope="col">Tags</th> -->
                            <th scope="col">Slug</th>
                            
                            
                            </tr>
                        <?php
                        
                        
for ($i = 0; $i < $q_length; $i++){
                            
//get the name of the thumbnail
                            
$thumb_path = wimpq_get_qdir() . basename(wimpq_get_thumbpath($queue[$i]->q_imgname));
                            
                            if (
$_GET['action'] == 'edit' && $_GET['entry'] == $i ) {  ?>
                                
                                <tr class='<?php if(($i+1)%2) echo "alternate"; ?>'>
                        
                                <th scope="row"><input type="text" name="img_position" size="2" value="<?php echo $queue[$i]->q_position; ?>" />
                                    <input type="hidden" name="img_old_position" value="<?php echo $queue[$i]->q_position; ?>" />
                                </th>
                        
                                        <td><?php $path = "../". wimpq_get_rel_url($thumb_path); ?>
                                            <img src='<?php echo $path; ?>' alt='<?php echo $queue[$i]->q_title; ?>' />
                                        </td>
                                        <td><?php echo $queue[$i]->q_imgname; ?><input type="hidden" name="img_name" value="<?php echo $queue[$i]->q_imgname; ?>" /></td>
                                        <td><input type="text" name="img_title" size="30" value="<?php echo $queue[$i]->q_title; ?>" /></td>
                                        <td><textarea style="font-size:small;" name="img_descr" cols="30" rows="3"><?php echo $queue[$i]->q_descr; ?> </textarea></td>
                                        <td><input type="text" name="img_slug" size="12" value="<?php echo $queue[$i]->q_slug; ?>" /></td>
                                        
                                        
                                </tr>
                                
                                
                        <?php        
                            
}else{
                        
?>

                                <tr class='<?php if(($i+1)%2) echo "alternate"; ?>'>
                        
                                <th scope="row"><?php echo $queue[$i]->q_position; ?></th>
                        
                                        <td><?php $path = "../".wimpq_get_rel_url($thumb_path); ?>
                                            <img src='<?php echo $path; ?>' alt='<?php echo $queue[$i]->q_title; ?>' /></td>
                                        <td><?php echo $queue[$i]->q_imgname; ?></td>
                                        <td><?php echo $queue[$i]->q_title; ?></td>
                                        <td><?php echo $queue[$i]->q_descr; ?></td>
                                        <!--<td><?php echo $queue[$i]->q_place; ?></td>
                                        <td><?php echo $queue[$i]->q_tags; ?></td>-->
                                        <td><?php echo $queue[$i]->q_slug; ?></td>
                                        <?php if ($_GET['action'] != 'edit'){ ?>
                                            <td><a href="edit.php?page=whoismanu-photoq.php&action=edit&entry=<?php echo $i; ?>" class="edit">Edit</a></td>
                                            <td><a href="edit.php?page=whoismanu-photoq.php&action=delete&entry=<?php echo $queue[$i]->q_imgname; ?>&qlen=<?php echo $q_length; ?>&pos=<?php echo $queue[$i]->q_position; ?>" class="delete" onClick="return confirm('Delete entry? Correpsonding image will also be deleted from server?');">Delete</a></td>
                                        <?php } ?>
                            </tr>
                        <?php }  }
                        
                        echo
"</table>";
                        
                    }else
                        echo
"Queue empty";
                
                if (
$_GET['action'] == 'edit') {
                
?>
                
                <div class="submit">
                      <input type="submit" name="update_queue"
                      value="<?php _e('Update queue', 'PhotoQ') ?> &raquo;" />
                  </div>
                  
                  <?php }else{?>
                  
                  <div class="submit">
                      <input type="submit" name="add_entry"
                      value="<?php _e('Add Photo to queue', 'PhotoQ') ?> &raquo;" />
                  </div>
                  
                  <div class="submit">
                      <input type="submit" name="post_first"
                      value="<?php _e('Post Top of queue', 'PhotoQ') ?> &raquo;"     onClick="return confirm(
        'Are you sure you want to publish the first entry of the queue?');" />
                  </div>
                  
                  <?php } if ($_GET['action'] == 'edit') {  ?>
                </div>
                
                
                <?php } ?>
              </form>
    </div>
    
    
    
    <?php }
}


/******************************************************/
/******************** options page ********************/
/******************************************************/

/***** functions to handle meta field stuff on options page *****/

/*display the list of currently used metafields*/
function wimpq_list_metafields($page, $q_entry = ''){
    
    global
$wpdb, $table_prefix;
    
$queue_table = $table_prefix."photoqfields";
    
    
$wpdb->hide_errors();

    
$results = $wpdb->get_results("
        SELECT
            *
        FROM
            $queue_table
        WHERE
            1
        ORDER BY q_field_name
        "
);
    
    
//create table at first call
    
if (strpos(mysql_error(), $table_prefix."photoqfields' doesn't exist")) wimpq_install();
    
    if(
$results){
        
$i = 0; //used to alternate styles
        
foreach ($results as $field_entry) {
            if(
$page == 'manage'){ //we are on the manage page
                
echo '<p><label for="'.$field_entry->q_field_name.'">'.$field_entry->q_field_name.':</label><br /><textarea style="font-size:small;" name="'.$field_entry->q_field_name.'" cols="30" rows="3"  class="uploadform"></textarea></p>';
            }elseif(
$page == 'edit_queue'){ //we are editing the queue
                //get the stored values
                
$queuemeta_table = $table_prefix."photoqmeta";
                
                
$field_value = $wpdb->get_var("SELECT
                        q_field_value
                    FROM
                        $queuemeta_table
                    WHERE
                        q_fk_imgname = '$q_entry' && q_fk_field_id = $field_entry->q_field_id"
);
                echo
'<p><label for="'.$field_entry->q_field_name.'">'.$field_entry->q_field_name.':</label><br /><textarea style="font-size:small;" name="'.$field_entry->q_field_name.'" cols="30" rows="3"  class="uploadform">'.$field_value.'</textarea></p>';
            }else{
//we are on the options page
                
echo '<tr valign="top" class=';
                if((
$i+1)%2) {echo '"alternate"';}
                echo
'>'