/**
 * Handle any warnings that have been invoked.
 */
function cpm_handle_warnings()
{
    global $cpm_config, $wpmu_version;
    // display informative messages to the use
    // TODO: remove separate arrays and tag messages based on an enum value
    foreach (array(array($cpm_config->messages, __("The operation you just performed returned the following:", 'comicpress-manager'), 'messages'), array($cpm_config->warnings, __("The following warnings were generated:", 'comicpress-manager'), 'warnings'), array($cpm_config->errors, __("The following problems were found in your configuration:", 'comicpress-manager'), 'errors')) as $info) {
        list($messages, $header, $style) = $info;
        if (count($messages) > 0) {
            if (count($messages) == 1) {
                $output = $messages[0];
            } else {
                ob_start();
                ?>

          <ul>
            <?php 
                foreach ($messages as $message) {
                    ?>
              <li><?php 
                    echo $message;
                    ?>
</li>
            <?php 
                }
                ?>
          </ul>

          <?php 
                $output = ob_get_clean();
            }
            if (strpos(PHP_OS, "WIN") !== false && $style == "warnings") {
                $output .= __("<p><strong>If your error is permissions-related, you may have to set some Windows-specific permissions on your filesystem.</strong> Consult your Webhost for more information.</p>", 'comicpress-manager');
            }
            ?>
        <div id="cpm-<?php 
            echo $style;
            ?>
"><?php 
            echo $output;
            ?>
</div>
      <?php 
        }
    }
    // errors are fatal.
    if (count($cpm_config->errors) > 0) {
        $current_theme_info = get_theme(get_current_theme());
        ?>
      <p><?php 
        _e("You must fix the problems above before you can proceed with managing your ComicPress installation.", 'comicpress-manager');
        ?>
</p>

      <?php 
        if ($cpm_config->show_config_editor) {
            ?>
        <p><strong><?php 
            _e("Details:", 'comicpress-manager');
            ?>
</strong></p>
        <ul>
          <li><strong><?php 
            _e("Current ComicPress theme folder:", 'comicpress-manager');
            ?>
</strong> <?php 
            echo $current_theme_info['Template Dir'];
            ?>
</li>
          <li><strong><?php 
            _e("Available categories:", 'comicpress-manager');
            ?>
</strong>
            <table id="categories-table">
              <tr>
                <th><?php 
            _e("Category Name", 'comicpress-manager');
            ?>
</th>
                <th><?php 
            _e("ID #", 'comicpress-manager');
            ?>
</th>
              </tr>
              <?php 
            foreach (get_all_category_ids() as $category_id) {
                $category = get_category($category_id);
                ?>
                <tr>
                  <td><?php 
                echo $category->category_nicename;
                ?>
</td>
                  <td align="center"><?php 
                echo $category->cat_ID;
                ?>
</td>
                </tr>
              <?php 
            }
            ?>
            </table>
          </li>
        </ul>
      <?php 
        }
        $update_automatically = true;
        $available_backup_files = array();
        $found_backup_files = glob(dirname($cpm_config->config_filepath) . '/comicpress-config.php.*');
        if ($found_backup_files === false) {
            $found_backup_files = array();
        }
        foreach ($found_backup_files as $file) {
            if (preg_match('#\\.([0-9]+)$#', $file, $matches) > 0) {
                list($all, $time) = $matches;
                $available_backup_files[] = $time;
            }
        }
        arsort($available_backup_files);
        //      if ($wpmu_version) {
        //        $cpm_config->show_config_editor = true;
        //      } else {
        if ($cpm_config->config_method == "comicpress-config.php") {
            if (!$cpm_config->can_write_config) {
                $update_automatically = false;
            }
        } else {
            if (count($available_backup_files) > 0) {
                if (!$cpm_config->can_write_config) {
                    $update_automatically = false;
                }
            } else {
                $update_automatically = false;
            }
        }
        if (!$update_automatically) {
            ?>
          <p>
            <?php 
            printf(__("<strong>You won't be able to update your comicpress-config.php or functions.php file directly through the ComicPress Manager interface.</strong> Check to make sure the permissions on %s and comicpress-config.php are set so that the Webserver can write to them.  Once you submit, you'll be given a block of code to paste into the comicpress-config.php file.", 'comicpress-manager'), $current_theme_info['Template Dir']);
            ?>
          </p>
        <?php 
        } else {
            if (count($available_backup_files) > 0) {
                ?>
            <p>
              <?php 
                _e("<strong>Some backup comicpress-config.php files were found in your theme directory.</strong>  You can choose to restore one of these backup files, or you can go ahead and create a new configuration below.", 'comicpress-manager');
                ?>
            </p>

            <form action="" method="post">
              <input type="hidden" name="action" value="restore-backup" />
              <strong><?php 
                _e("Restore from backup dated:", 'comicpress-manager');
                ?>
</strong>
                <select name="backup-file-time">
                  <?php 
                foreach ($available_backup_files as $time) {
                    ?>
                    <option value="<?php 
                    echo $time;
                    ?>
">
                      <?php 
                    echo date("r", $time);
                    ?>
                    </option>
                  <?php 
                }
                ?>
                </select>
              <input type="submit" class="button" value="<?php 
                _e("Restore", 'comicpress-manager');
                ?>
" />
            </form>
            <hr />
          <?php 
            }
        }
        //     }
        if ($cpm_config->show_config_editor) {
            echo cpm_manager_edit_config();
        }
        ?>

      <?php 
        if ($wpmu_version) {
            ?>
        <hr />

        <strong><?php 
            _e('Debug info', 'comicpress-manager');
            ?>
</strong> (<em><?php 
            _e("this data is sanitized to protect your server's configuration", 'comicpress-manager');
            ?>
</em>)

        <?php 
            echo cpm_show_debug_info(false);
        }
        return false;
    }
    return true;
}
/**
 * The config editor dialog.
 */
function cpm_manager_config()
{
    global $cpm_config;
    extract(cpm_normalize_storyline_structure());
    ob_start();
    ?>

  <h2 style="padding-right:0;"><?php 
    _e("Edit ComicPress Config", 'comicpress-manager');
    ?>
</h2>
  <?php 
    if (!$cpm_config->can_write_config) {
        ?>
    <p>
      <?php 
        _e("<strong>You won't be able to automatically update your configuration.</strong> After submitting, you will be shown the code to paste into comicpress-config.php. If you want to enable automatic updating, check the permissions of your theme folder and comicpress-config.php file.", 'comicpress-manager');
        ?>
    </p>
  <?php 
    }
    echo cpm_manager_edit_config();
    ?>

  <?php 
    if (get_option('comicpress-enable-storyline-support') == 1) {
        ?>
    <form action="" method="post">
      <input type="hidden" name="action" value="manage-subcomic" />
      <table class="form-table" cellspacing="0">
        <tr>
          <th scope="row">Manage a subcomic</th>
          <td>
            <select name="comic">
              <?php 
        $first = true;
        $path = CPM_DOCUMENT_ROOT . '/' . $cpm_config->properties['comic_folder'];
        foreach ($category_tree as $node) {
            $category = get_category(end(explode("/", $node)));
            $selected = get_option('comicpress-manager-manage-subcomic') == $category->term_id ? " selected" : "";
            if (is_dir($path . '/' . $category->slug) || $first) {
                ?>
                    <option value="<?php 
                echo $category->term_id;
                ?>
"<?php 
                echo $selected;
                ?>
><?php 
                echo $category->name . ($first ? " (default)" : "");
                ?>
</option>
                  <?php 
            }
            $first = false;
        }
        ?>
            </select>
            <input type="submit" value="Submit" />
          </td>
        </tr>
      </table>
    </form>
  <?php 
    }
    $activity_content = ob_get_clean();
    cpm_wrap_content(null, $activity_content);
}