/**
 * The config editor dialog.
 */
function cpm_manager_cpm_config()
{
    global $cpm_config, $wpmu_version;
    include realpath(dirname(__FILE__)) . '/../cpm_configuration_options.php';
    $is_table = false;
    ob_start();
    ?>

  <h2 style="padding-right:0;"><?php 
    _e("Edit ComicPress Manager Config", 'comicpress-manager');
    ?>
</h2>

  <form action="" method="post" id="config-editor">
    <input type="hidden" name="action" value="update-cpm-config" />
      <?php 
    foreach ($configuration_options as $option) {
        $ok = true;
        if ($wpmu_version) {
            $ok = !isset($option['strip-wpmu']);
        }
        if ($option['type'] == "categories") {
            $ok = count($category_checkboxes = cpm_generate_additional_categories_checkboxes($option['id'], explode(",", $result))) > 0;
        }
        if ($ok) {
            ?>
          <?php 
            if (is_string($option)) {
                ?>
            <?php 
                if ($is_table) {
                    ?>
              </table></div>
              <?php 
                    $is_table = false;
                }
                ?>
            <h3><?php 
                echo $option;
                ?>
</h3>
          <?php 
            } else {
                if (!$is_table) {
                    ?>
              <div style="overflow: hidden"><table class="form-table">
              <?php 
                    $is_table = true;
                }
                ?>
            <tr>
              <th scope="row"><?php 
                echo $option['name'];
                ?>
</th>
              <td>
                <?php 
                $result = cpm_option($option['id']);
                switch ($option['type']) {
                    case "checkbox":
                        $ok = true;
                        if (isset($option['imagemagick-only'])) {
                            $ok = $cpm_config->get_scale_method() == CPM_SCALE_IMAGEMAGICK;
                        }
                        if ($ok) {
                            ?>
                        <input type="checkbox" id="<?php 
                            echo $option['id'];
                            ?>
" name="<?php 
                            echo $option['id'];
                            ?>
" value="yes" <?php 
                            echo $result == 1 ? " checked" : "";
                            ?>
 />
                      <?php 
                        }
                        break;
                    case "text":
                        ?>
                      <input type="text" size="<?php 
                        echo isset($option['size']) ? $option['size'] : 10;
                        ?>
" name="<?php 
                        echo $option['id'];
                        ?>
" value="<?php 
                        echo $result;
                        ?>
" />
                      <?php 
                        break;
                    case "textarea":
                        ?>
                      <textarea name="<?php 
                        echo $option['id'];
                        ?>
" rows="4" cols="30"><?php 
                        echo $result;
                        ?>
</textarea>
                      <?php 
                        break;
                    case "dropdown":
                        $dropdown_parts = array();
                        foreach (explode("|", $option['options']) as $dropdown_option) {
                            $parts = explode(":", $dropdown_option);
                            $key = array_shift($parts);
                            $dropdown_parts[$key] = implode(":", $parts);
                        }
                        ?>
                      <select name="<?php 
                        echo $option['id'];
                        ?>
">
                        <?php 
                        foreach ($dropdown_parts as $key => $value) {
                            ?>
                          <option value="<?php 
                            echo $key;
                            ?>
" <?php 
                            echo $result == $key ? " selected" : "";
                            ?>
><?php 
                            echo $value;
                            ?>
</option>
                        <?php 
                        }
                        ?>
                      </select>
                      <?php 
                        break;
                    case "categories":
                        echo implode("\n", $category_checkboxes);
                        break;
                }
                ?>
                <em><label for="<?php 
                echo $option['id'];
                ?>
">(<?php 
                echo $option['message'];
                ?>
)<label></em>
              </td>
            </tr>
          <?php 
            }
            ?>
        <?php 
        }
        ?>
      <?php 
    }
    ?>
      <tr>
        <td colspan="2" align="center">
          <input class="button" type="submit" value="Change Configuration" />
        </td>
      </tr>
    </table></div>
  </form>

  <div id="first-run-holder">
    <p><strong>Re-run the &quot;First Run&quot; action? This will attempt to create the default comic folders on your site.</strong></p>

    <form action="<?php 
    echo $target_page;
    ?>
" method="post">
      <input type="hidden" name="action" value="do-first-run" />
      <input class="button" type="submit" value="Yes, try and make my comic directories" />
    </form>
  </div>

  <?php 
    $activity_content = ob_get_clean();
    cpm_wrap_content(null, $activity_content);
}
/**
 * Show the Post Editor.
 * @param integer $width The width in pixels of the text editor widget.
 */
function cpm_post_editor($width = 435, $is_import = false)
{
    global $cpm_config;
    $form_titles_and_fields = array();
    if (is_null(get_category($cpm_config->properties['comiccat']))) {
        ?>
    <p><strong>You don't have a comics category defined!</strong> Go to the
    <a href="?page=<?php 
        echo plugin_basename(__FILE__);
        ?>
-config">ComicPress Config</a> screen and choose a category.
    <?php 
        return;
    }
    extract(cpm_normalize_storyline_structure());
    if (cpm_get_subcomic_directory() !== false) {
        $post_categories = array(get_option('comicpress-manager-manage-subcomic'));
    } else {
        if (cpm_option('cpm-default-comic-category-is-last-storyline') == 1) {
            $post_categories = array(end(explode("/", end($category_tree))));
        } else {
            $post_categories = array($cpm_config->properties['comiccat']);
        }
    }
    ob_start();
    cpm_display_storyline_checkboxes($category_tree, $post_categories);
    $storyline_editor = ob_get_clean();
    $form_titles_and_fields[] = array(__("Storyline:", 'comicpress-manager'), $storyline_editor);
    // see if there are additional categories that can be set besides the comic and blog categories
    if (count($category_checkboxes = cpm_generate_additional_categories_checkboxes()) > 0) {
        $form_titles_and_fields[] = array(__("Additional Categories:", 'comicpress-manager'), implode("\n", $category_checkboxes));
    }
    $form_titles_and_fields[] = array(__("Time to post:", 'comicpress-manager'), "<input type=\"text\" name=\"time\" value=\"" . cpm_option('cpm-default-post-time') . "\" size=\"10\" />" . __(" <em>(must be in the format <strong>HH:MM am/pm</strong> or <strong>now</strong>)</em>", 'comicpress-manager'));
    $form_titles_and_fields[] = array('<label for="publish">' . __("Publish post:", 'comicpress-manager') . '</label>', '<input id="publish" type="checkbox" name="publish" value="yes" checked />' . __(" <label for=\"publish\"><em>(set the status of this post to <strong>published</strong> instead of <strong>draft</strong>)</em></label>", "comicpress-manager"));
    if (!$is_import) {
        $form_titles_and_fields[] = array('<label for="duplicate-check">' . __("Check for duplicate posts:", 'comicpress-manager') . '</label>', '<input id="duplicate-check" type="checkbox" name="duplicate_check" value="yes" checked />' . __(" <label for=\"duplicate-check\"><em>(if you've set up ComicPress to use multiple posts on the same day, you'll need to disable this option to allow ComicPress Manager to make multiple posts)</em></label>", "comicpress-manager"));
    } else {
        $form_titles_and_fields[] = '<input type="hidden" name="duplicate_check" value="yes" />';
    }
    if ($cpm_config->get_scale_method() != CPM_SCALE_NONE) {
        $thumbnail_writes = cpm_get_thumbnails_to_generate();
        $thumb_write_holder = ' (<em>';
        if (count($thumbnail_writes) > 0) {
            $thumb_write_holder .= sprintf("If enabled, you'll be writing thumbnails to: %s", implode(", ", $thumbnail_writes));
        } else {
            $thumb_write_holder .= __("You won't be generating any thumbnails.", 'comicpress-manager');
        }
        $thumb_write_holder .= "</em>)";
        if (count($thumbnail_writes) > 0) {
            $form_titles_and_fields[] = array('<label for="thumbnails">' . __("Generate thumbnails:", 'comicpress-manager') . '</label>', '<input onclick="hide_show_div_on_checkbox(\'thumbnail-write-holder\', this, true)" type="checkbox" name="thumbnails" id="thumbnails" value="yes" checked />' . '<label for="thumbnails">' . $thumb_write_holder . "</label>");
        }
    }
    $form_titles_and_fields[] = array(__("Title For All Posts:", 'comicpress-manager'), '<input type="text" name="override-title-to-use" value="' . cpm_option('cpm-default-override-title') . '" />' . __(" <em>(the title to use for all posts)</em>", 'comicpress-manager'));
    $form_titles_and_fields[] = array(__("&lt;img title&gt;/Hovertext For All Posts:", 'comicpress-manager'), '<input type="text" name="hovertext-to-use" value="' . cpm_option('cpm-default-hovertext') . '" />' . __(" <em>(the hovertext to use for all posts)</em>", 'comicpress-manager'));
    $form_titles_and_fields[] = array(__("Transcript For All Posts:", 'comicpress-manager'), '<textarea name="transcript-to-use" rows="5" cols="30"></textarea>' . __(" <em>(the transcript to use for all posts)</em>", 'comicpress-manager'));
    if (!is_plugin_active('what-did-they-say/what-did-they-say.php')) {
        $form_titles_and_fields[] = '<em>' . __('Want even better control over your transcripts? Try <a href="http://wordpress.org/extend/plugins/what-did-they-say/" target="wdts">What Did They Say?!?</a>', 'comicpress-manager') . '</em>';
    }
    $form_titles_and_fields[] = array(__("Upload Date Format:", 'comicpress-manager'), '<input type="text" id="upload-date-format" name="upload-date-format" />' . __(" <em>(if the files you are uploading have a different date format, specify it here. ex: <strong>Ymd</strong> for a file named <strong>20080101-my-new-years-day.jpg</strong>)</em>", 'comicpress-manager'));
    $form_titles_and_fields[] = array(__("Tags:", 'comicpress-manager'), '<input type="text" id="tags" name="tags" value="' . cpm_option('cpm-default-post-tags') . '" />' . __(" <em>(any tags to add to the post, separated by commas. any tags you've used before will be listed below.)</em>", 'comicpress-manager'));
    $all_tags_links = array();
    foreach (get_tags() as $tag) {
        $all_tags_links[] = "<a href=\"#\" class=\"tag\">{$tag->name}</a>";
    }
    sort($all_tags_links);
    if (count($all_tags_links) > 0) {
        $form_titles_and_fields[] = array(__("Quick Tags (click to add):", 'comicpress-manager'), implode("\n", $all_tags_links));
    }
    ?>
<table class="form-table"><?php 
    foreach ($form_titles_and_fields as $form_title_and_field) {
        if (is_array($form_title_and_field)) {
            list($title, $field, $id) = $form_title_and_field;
            ?>
      <tr<?php 
            echo !empty($id) ? " id=\"{$id}\"" : "";
            ?>
>
        <th scope="row" valign="top"><?php 
            echo $title;
            ?>
</th>
        <td valign="top"><?php 
            echo $field;
            ?>
</td>
      </tr>
    <?php 
        } else {
            ?>
      <tr><td colspan="2"><?php 
            echo $form_title_and_field;
            ?>
</td></tr>
    <?php 
        }
        ?>
  <?php 
    }
    ?>
  </table>

  <?php 
    cpm_show_post_body_template($width);
}
/**
 * Write Comic Post is the QuomicPress panel on the Dashboard..
 */
function cpm_manager_write_comic($form_target, $show_header = true)
{
    global $cpm_config;
    $ok_to_generate_thumbs = false;
    $thumbnails_to_generate = array();
    $cpm_config->need_calendars = true;
    $thumbnails_to_generate = cpm_get_thumbnails_to_generate();
    $go_live_time_string = cpm_option('cpm-default-post-time') == "now" ? __("<strong>now</strong>", 'comicpress-manager') : sprintf(__("at <strong>%s</strong>", 'comicpress-manager'), cpm_option('cpm-default-post-time'));
    cpm_write_global_styles_scripts();
    ?>
  <div class="wrap">
    <?php 
    cpm_handle_warnings();
    ?>
    <?php 
    if (count($cpm_config->errors) == 0) {
        ?>
      <?php 
        if ($show_header) {
            ?>
        <h2><?php 
            _e("Write Comic Post", 'comicpress-manager');
            ?>
</h2>
      <?php 
        }
        ?>

      <?php 
        if (count($cpm_config->comic_files) == 0 && cpm_get_subcomic_directory() === false) {
            ?>
        <div style="border: solid #daa 1px; background-color: #ffe7e7; padding: 5px">
          <strong>It looks like this is a new ComicPress install.</strong> You should test to make
          sure uploading works correctly by visiting <a href="admin.php?page=<?php 
            echo plugin_basename(realpath(dirname(__FILE__) . '/../comicpress_manager_admin.php'));
            ?>
">ComicPress -> Upload</a>.
        </div>
      <?php 
        }
        ?>

      <p>
        <?php 
        printf(__("<strong>Upload a single comic file</strong> and immediately start editing the associated published post. Your post will be going live %s on the provided date and will be posted in the <strong>%s</strong> category.", 'comicpress-manager'), $go_live_time_string, generate_comic_categories_options('category'));
        ?>

        <?php 
        if (cpm_get_subcomic_directory() !== false) {
            printf(__("Comic files will be uploaded to the <strong>%s</strong> comic subdirectory.", 'comicpress-manager'), get_cat_name(get_option('comicpress-manager-manage-subcomic')));
        }
        ?>

        <?php 
        if (!empty($thumbnails_to_generate)) {
            $thumbnail_strings = array();
            foreach ($thumbnails_to_generate as $type) {
                $thumbnail_strings[] = sprintf(__("<strong>%s</strong> thumbnails that are <strong>%spx</strong> wide", 'comicpress-manager'), $type, $cpm_config->properties["{$type}_comic_width"]);
            }
            ?>
          <?php 
            printf(__("You'll be generating: %s.", 'comicpress-manager'), implode(", ", $thumbnail_strings));
            ?>
        <?php 
        }
        ?>
      </p>

      <form action="?page=<?php 
        echo $form_target;
        ?>
" method="post" enctype="multipart/form-data">
        <input type="hidden" name="MAX_FILE_SIZE" value="<?php 
        echo cpm_short_size_string_to_bytes(ini_get('upload_max_filesize'));
        ?>
" />
        <input type="hidden" name="action" value="write-comic-post" />
        <input type="hidden" name="upload-destination" value="comic" />
        <input type="hidden" name="thumbnails" value="yes" />
        <input type="hidden" name="new_post" value="yes" />
        <?php 
        echo generate_comic_categories_options('in-comic-category[]', false);
        ?>
        <input type="hidden" name="time" value="<?php 
        echo cpm_option('cpm-default-post-time');
        ?>
" />

        <table class="form-table">
          <tr>
            <th scope="row"><?php 
        _e('File:', 'comicpress-manager');
        ?>
</th>
            <td><input type="file" name="upload" /></td>
          </tr>
          <?php 
        if (count($category_checkboxes = cpm_generate_additional_categories_checkboxes()) > 0) {
            ?>
            <tr>
              <th scope="row"><?php 
            _e("Additional Categories:", 'comicpress-manager');
            ?>
</th>
              <td><?php 
            echo implode("\n", $category_checkboxes);
            ?>
</td>
            </tr>
          <?php 
        }
        ?>
          <tr>
            <th scope="row"><?php 
        _e("Post date (leave blank if already in filename):", 'comicpress-manager');
        ?>
</th>
            <td>
              <div class="curtime"><input type="text" id="override-date" name="override-date" /></div>
            </td>
          </tr>
          <tr>
            <td colspan="2"><input type="submit" class="button" value="Upload Comic File and Edit Post" /></td>
          </tr>
        </table>
      </form>
      <script type="text/javascript">
        Calendar.setup({
          inputField: "override-date",
          ifFormat: "%Y-%m-%d",
          button: "override-date"
        });
      </script>
    <?php 
    }
    ?>
  </div>

  <?php 
}