function pugpig_touch_all_editions()
{
    foreach (pugpig_get_editions() as $edition) {
        pugpig_touch_edition($edition->ID);
    }
}
}
$atom_mode = false;
if (isset($_GET["atom"]) && $_GET["atom"] == 'true') {
    $atom_mode = true;
}
$newsstand_mode = false;
if (isset($_GET["newsstand"]) && $_GET["newsstand"] == 'true') {
    $newsstand_mode = true;
}
if (isset($_GET["region"])) {
    $extra_comments[] = "Region Filter: " . $_GET["region"];
}
// This might be needed for caching in the future
// $cache_key = ($internal ? "INT_" : "EXT_") . ($atom_mode ? "ATOM_" : "PACK_") . ($newsstand_mode ? "NEWS" : "OPDS");
$extra_comments[] = "Status: {$status}. Number of editions to include: " . pugpig_get_num_editions();
$editions = pugpig_get_editions($status, pugpig_get_num_editions());
$edition_ids = array();
$modified = null;
foreach ($editions as $edition) {
    if (pugpig_should_keep_edition_in_feed($edition)) {
        $edition_ids[] = $edition->ID;
        $atom_timestamp = pugpig_get_page_modified($edition);
        if ($atom_mode) {
            $this_time = $atom_timestamp;
        } else {
            $package_timestamp = pugpig_get_edition_update_date(pugpig_get_edition($edition->ID), false);
            $this_time = max($package_timestamp, $atom_timestamp);
            // so cover changes etc. are picked up
        }
        if ($modified == NULL || $modified < $this_time) {
            $modified = $this_time;
function pugpig_push_notification_form()
{
    if (!empty($_POST['pugpig_push_message_type'])) {
        // push notification requested...
        $message_type = $_POST['pugpig_push_message_type'];
        $message_field = _pugpig_push_notification_get_message_field($message_type);
        $with_download = $_POST['pugpig_push_tab'] == 'download';
        $message = $_POST[$message_field];
        echo pugpig_send_push_notification($message, $with_download);
        $push_notification_menu = admin_url('admin.php?page=pugpig-push-notification');
        echo '<div><a href="' . $push_notification_menu . '">Return</a></div>';
        exit;
    }
    // display form...
    $last_edition_summary = '';
    $editions = pugpig_get_editions('publish', 1);
    if (count($editions) > 0) {
        $last_edition_summary = $editions[0]->post_excerpt;
    }
    $hidden_field_name = 'mt_submit_hidden';
    if (empty($_GET['tab'])) {
        $current_tab = 'download';
    } else {
        $current_tab = $_GET['tab'];
    }
    $is_tab_message = $current_tab == 'message';
    $is_tab_download = $current_tab == 'download';
    $default_message = get_option("pugpig_opt_urbanairship_message");
    $key = get_option("pugpig_opt_urbanairship_key");
    $secret = get_option("pugpig_opt_urbanairship_secret");
    $send_disabled = !pugpig_should_send_push();
    $key_unset = empty($key);
    $secret_unset = empty($secret);
    $is_disabled = $send_disabled | $key_unset | $secret_unset;
    $notifications_link = _pugpig_push_notification_get_settings_link();
    ?>

<div id="icon-edit" class="icon32 icon32-posts-pugpig_edition"><br /></div><h2>Pugpig Push Notification</h2>

<h2 class="nav-tab-wrapper">
  <a class='nav-tab<?php 
    if ($is_tab_download) {
        print ' nav-tab-active';
    }
    ?>
' href='?page=<?php 
    print $_GET['page'];
    ?>
&amp;tab=download' onclick='return check()'>Background Download Push</a>
  <a class='nav-tab<?php 
    if ($is_tab_message) {
        print ' nav-tab-active';
    }
    ?>
' href='?page=<?php 
    print $_GET['page'];
    ?>
&amp;tab=message'>Message Push</a>
</h2>

<?php 
    if ($is_tab_download) {
        ?>
<p>For Newsstand applications, use this form* to get the all the readers'** applications to perform a background download of the new content and optionally display a message when the download has completed.</p>
<p>It should only be used:
  <ul style="list-style:disc inside">
    <li>Once a new edition has been packaged, published and <strong>checked on suitable devices</strong></li>
    <li><strong>Once</strong> per edition</li>
    <li>Just <strong>after publishing</strong> an edition</li>
    <li><strong>Evenings</strong> are best as users tend to be plugged in and on wifi</li>
  </ul>
</p>
<?php 
    } else {
        ?>
  <p>Use this form* to send a message to all the readers**.</p>
<?php 
    }
    ?>

<form name="formpush" method="post" action="" onsubmit="return validateForm()">
  <input type="hidden" name="<?php 
    echo $hidden_field_name;
    ?>
" value="Y">
  <input type="hidden" name="pugpig_push_tab" value="<?php 
    print $current_tab;
    ?>
">
  <table class="form-table">

    <?php 
    if ($is_tab_download) {
        ?>
    <tr valign="top">
      <th scope="row"><label for="pugpig_push_message_type">No message</label></th>
      <td>
        <input type="radio" name="pugpig_push_message_type" value="none" />
        <input type="hidden" name="pugpig_push_message_none" value="" />
      </td>
    </tr>
    <?php 
    }
    ?>

    <tr valign="top">
      <th scope="row"><label for="pugpig_push_message_type">The last edition summary</label></th>
      <td>
        <input type="radio" name="pugpig_push_message_type" value="edition" <?php 
    if (empty($last_edition_summary)) {
        print 'disabled';
    }
    ?>
 />
        <input type="hidden" name="pugpig_push_message_edition" value="<?php 
    echo $last_edition_summary;
    ?>
" />
      </td>
      <td><?php 
    if (empty($last_edition_summary)) {
        ?>
        <em style="color:red;">last edition summary unset</em>
      <?php 
    } else {
        print $last_edition_summary;
    }
    ?>
      </td>
    </tr>

    <tr valign="top">
      <th scope="row"><label for="pugpig_push_message_type">The default notification message</label></th>
      <td>
        <input type="radio" name="pugpig_push_message_type" value="default" <?php 
    if (empty($default_message)) {
        print 'disabled';
    }
    ?>
/>
        <input type="hidden" name="pugpig_push_message_default" value="<?php 
    print $default_message;
    ?>
" />
      </td>
      <td><?php 
    if (empty($default_message)) {
        ?>
        <em style="color:red;">no message set</em> (<?php 
        print $notifications_link;
        ?>
)
      <?php 
    } else {
        print $default_message;
    }
    ?>
      </td>
    </tr>

    <tr valign="top">
      <th scope="row"><label for="pugpig_push_message_type">Your own message</label></th>
      <td><input type="radio" name="pugpig_push_message_type" value="custom" /></td>
      <td><input name="pugpig_push_message_custom" type="text" style="width:400px" /></td>
    </tr>

    <tr valign="top">
      <th scope="row"></th>
      <td></td>
      <td><input type="submit" value="<?php 
    print $is_tab_download ? 'Push update' : 'Send message';
    ?>
" <?php 
    if ($is_disabled) {
        print 'disabled';
    }
    ?>
/>
        <?php 
    if ($is_disabled) {
        print '<br/>';
        if ($send_disabled) {
            print '<em>Send push notifications</em> is not enabled.<br/>';
        } else {
            if ($key_unset) {
                print '<em>Urban Airship Key</em> is not set.<br/>';
            }
            if ($secret_unset) {
                print '<em>Urban Airship Application Master Secret</em> is not set.<br/>';
            }
        }
        print "See {$notifications_link}.";
    }
    ?>
        </td>
    </tr>
  </table>
</form>

<br/>
<small>* Please note that this form uses your Urban Airship account and you may be charged for notifications.<br/>
** Messages will only be received by readers who have push notifications enabled on their device.</small>

<script>
  function validateForm()
  {
    // check an option is selected
    var $checked = jQuery("input[type=radio]:checked");
    if ($checked.size()==0) {
      alert("Select a message to send.");

      return false;
    }

    // check that selected message is not empty
    var type='pugpig_push_message_' + $checked.first().val();
    if (type!='pugpig_push_message_none') {
      var message=jQuery("input[name='"+type+"']").first().val();
      if (message.length==0) {
        alert("No message text entered.");

        return false;
      }
    }

    return true;
  }
</script>
<?php 
    exit;
}