Beispiel #1
0
function rsvpmaker_get_projected($template)
{
    /*
    echo "<pre>";
    print_r($template);
    echo "</pre>";
    */
    //backward compatability
    if (is_array($template["week"])) {
        $weeks = $template["week"];
        $dows = $template["dayofweek"];
    } else {
        $weeks[0] = $template["week"];
        $dows[0] = $template["dayofweek"];
    }
    /*
    echo "<pre>";
    print_r($template);
    echo "</pre>";
    */
    $cy = date("Y");
    $cm = date("m");
    if (!empty($template["stop"])) {
        $stopdate = strtotime($template["stop"] . ' 23:59:59');
    }
    foreach ($weeks as $week) {
        foreach ($dows as $dow) {
            //debug
            //echo "week: $week day: $dow <br />";
            if ($week == 6) {
                $stop = 26;
                for ($i = 0; $i <= $stop; $i++) {
                    $text = rsvpmaker_day($dow, 'strtotime') . " +" . $i . " week";
                    //echo $text.'<br />';
                    $ts = strtotime($text);
                    if (isset($stopdate) && $ts > $stopdate) {
                        //echo "stop break <br />";
                        break;
                    }
                    $projected[$ts] = $ts;
                    // add numeric value for 1 week
                }
            } else {
                //monthly
                $futuremonths = 12;
                for ($i = 0; $i < $futuremonths; $i++) {
                    $ts = mktime(0, 0, 0, $cm + $i, 1, $cy);
                    // first day of month
                    if (isset($stopdate) && $ts > $stopdate) {
                        //echo "stop break <br />";
                        break;
                    }
                    $firstdays[$ts] = $ts;
                    if ($week == 0) {
                        $projected[$ts] = $ts;
                    }
                }
                if ($week > 0) {
                    if ($week == 5) {
                        $wtext = 'Last';
                    } else {
                        $wtext = '+' . ($week - 1) . ' week';
                    }
                    foreach ($firstdays as $i => $firstday) {
                        $datetext = "{$wtext} " . rsvpmaker_day($dow, 'strtotime') . " " . date("F Y", $firstday);
                        $ts = strtotime($datetext);
                        if (isset($stopdate) && $ts > $stopdate) {
                            break;
                        }
                        $projected[$ts] = $ts;
                    }
                }
            }
        }
    }
    //order by timestamp
    ksort($projected);
    /*
    echo "<pre>";
    print_r($projected);
    echo "</pre>";
    */
    return $projected;
}
    function rsvp_template_checkboxes($t)
    {
        global $wpdb;
        global $current_user;
        $post = get_post($t);
        $template_editor = false;
        if (current_user_can('edit_others_rsvpmakers')) {
            $template_editor = true;
        } else {
            $eds = get_post_meta($t, '_additional_editors', false);
            $eds[] = $wpdb->get_var("SELECT post_author FROM {$wpdb->posts} WHERE ID = {$t}");
            $template_editor = in_array($current_user->ID, $eds);
        }
        $template = get_post_meta($t, '_sked', true);
        $hour = isset($template["hour"]) ? (int) $template["hour"] : 17;
        $minutes = isset($template["minutes"]) ? $template["minutes"] : '00';
        $terms = get_the_terms($t, 'rsvpmaker-type');
        if ($terms && !is_wp_error($terms)) {
            $rsvptypes = array();
            foreach ($terms as $term) {
                $rsvptypes[] = $term->term_id;
            }
        }
        $cy = date("Y");
        $cm = date("m");
        $cd = date("j");
        //backward compatability
        if (is_array($template["week"])) {
            $weeks = $template["week"];
            $dows = $template["dayofweek"];
        } else {
            $weeks[0] = $template["week"];
            $dows[0] = $template["dayofweek"];
        }
        if ($weeks[0] == 0) {
            $schedule = __('Schedule Varies', 'rsvpmaker');
        }
        foreach ($weeks as $week) {
            $schedule .= rsvpmaker_week($week) . ' ';
        }
        $schedule .= ' / ';
        foreach ($dows as $dow) {
            $schedule .= rsvpmaker_day($dow) . ' ';
        }
        printf('<p>%s:</p><h2>%s</h2><h3>%s</h3><blockquote><a href="%s">%s</a></blockquote>', __('Template', 'rsvpmaker'), $post->post_title, $schedule, admin_url('post.php?action=edit&post=' . $t), __('Edit Template', 'rsvpmaker'));
        if ($_GET["trashed"]) {
            $ids = (int) $_GET["ids"];
            $message = '<a href="' . esc_url(wp_nonce_url("edit.php?post_type=rsvpmaker&doaction=undo&action=untrash&ids={$ids}", "bulk-posts")) . '">' . __('Undo') . '</a>';
            echo '<div id="message" class="updated"><p>' . __('Moved to trash', 'rsvpmaker') . ' ' . $message . '</p></div>';
        }
        if (isset($_POST["update_from_template"])) {
            foreach ($_POST["update_from_template"] as $target_id) {
                if (!current_user_can('publish_rsvpmakers')) {
                    echo '<div class="updated">Error</div>';
                    break;
                }
                $sql = $wpdb->prepare("UPDATE {$wpdb->posts} SET post_title=%s, post_content=%s WHERE ID=%d", $post->post_title, $post->post_content, $target_id);
                $wpdb->query($sql);
                $dpart = explode(':', $template["duration"]);
                if (is_numeric($dpart[0])) {
                    $cddate = $wpdb->get_var("SELECT datetime from " . $wpdb->prefix . "rsvp_dates WHERE postID=" . $target_id);
                    $dtext = $cddate . ' +' . $dpart[0] . ' hours';
                    if ($dpart[1]) {
                        $dtext .= ' +' . $dpart[1] . ' minutes';
                    }
                    $dt = strtotime($dtext);
                    $duration = date('Y-m-d H:i:s', $dt);
                    //debug printf('<p>text: %s %s</p>',$dtext,$duration);
                } else {
                    $duration = $template["duration"];
                }
                $sql = "UPDATE " . $wpdb->prefix . "rsvp_dates SET duration = '{$duration}' WHERE postID=" . $target_id;
                //debug echo "<p>$sql</p>";
                $wpdb->query($sql);
                wp_set_object_terms($target_id, $rsvptypes, 'rsvpmaker-type', true);
                echo '<div class="updated">Updated: event #' . $target_id . ' <a href="post.php?action=edit&post=' . $target_id . '">Edit</a> / <a href="' . get_post_permalink($target_id) . '">View</a></div>';
            }
        }
        if (isset($_POST["recur_check"])) {
            $my_post['post_title'] = $post->post_title;
            $my_post['post_content'] = $post->post_content;
            $my_post['post_status'] = current_user_can('publish_rsvpmakers') ? 'publish' : 'draft';
            $my_post['post_author'] = $current_user->ID;
            $my_post['post_type'] = 'rsvpmaker';
            foreach ($_POST["recur_check"] as $index => $on) {
                $year = $_POST["recur_year"][$index];
                $cddate = $year . "-" . $_POST["recur_month"][$index] . "-" . $_POST["recur_day"][$index] . " " . $hour . ":" . $minutes . ":00";
                $y = (int) $_POST["recur_year"][$index];
                $m = (int) $_POST["recur_month"][$index];
                $d = (int) $_POST["recur_day"][$index];
                $date = $y . '-' . $m . '-' . $d;
                $dpart = explode(':', $template["duration"]);
                if (is_numeric($dpart[0])) {
                    $dtext = $cddate . ' +' . $dpart[0] . ' hours';
                    if ($dpart[1]) {
                        $dtext .= ' +' . $dpart[1] . ' minutes';
                    }
                    $dt = strtotime($dtext);
                    $duration = date('Y-m-d H:i:s', $dt);
                    //printf('<p>%s %s</p>',$dtext,$duration);
                } else {
                    $duration = $template["duration"];
                }
                //echo $duration."<br />";
                $my_post['post_name'] = sanitize_title($my_post['post_title'] . '-' . $date);
                $singular = __('Event', 'rsvpmaker');
                // Insert the post into the database
                if ($postID = wp_insert_post($my_post)) {
                    $sql = "INSERT INTO " . $wpdb->prefix . "rsvp_dates SET datetime='{$cddate}', duration='{$duration}', postID=" . $postID;
                    $wpdb->show_errors();
                    $return = $wpdb->query($sql);
                    if ($return == false) {
                        echo '<div class="updated">' . "Error: {$sql}.</div>\n";
                    } elseif ($my_post["post_status"] == 'publish') {
                        echo '<div class="updated">Posted: event for ' . $cddate . ' <a href="post.php?action=edit&post=' . $postID . '">Edit</a> / <a href="' . get_post_permalink($postID) . '">View</a></div>';
                    } else {
                        echo '<div class="updated">Draft for ' . $cddate . ' <a href="post.php?action=edit&post=' . $postID . '">Edit</a> / <a href="' . get_post_permalink($postID) . '">Preview</a></div>';
                    }
                    add_post_meta($postID, '_meet_recur', $t, true);
                    wp_set_object_terms($postID, $rsvptypes, 'rsvpmaker-type', true);
                    $results = $wpdb->get_results("SELECT * FROM {$wpdb->postmeta} WHERE meta_key LIKE '_rsvp%' AND post_id=" . $t);
                    if ($results) {
                        foreach ($results as $row) {
                            if ($row->meta_key == '_rsvp_reminder') {
                                continue;
                            }
                            $wpdb->query($wpdb->prepare("INSERT INTO {$wpdb->postmeta} SET meta_key=%s,meta_value=%s,post_id=%d", $row->meta_key, $row->meta_value, $postID));
                        }
                    }
                    //copy rsvp options
                }
            }
        }
        if (isset($_POST["nomeeting"])) {
            $my_post['post_title'] = __('No Meeting', 'rsvpmaker') . ': ' . $post->post_title;
            $my_post['post_content'] = $_POST["nomeeting_note"];
            $my_post['post_status'] = current_user_can('publish_rsvpmakers') ? 'publish' : 'draft';
            $my_post['post_author'] = $current_user->ID;
            $my_post['post_type'] = 'rsvpmaker';
            if (!strpos($_POST["nomeeting"], '-')) {
                //update vs new post
                $id = (int) $_POST["nomeeting"];
                $sql = $wpdb->prepare("UPDATE {$wpdb->posts} SET post_title=%s, post_content=%s WHERE ID=%d", $my_post['post_title'], $my_post['post_content'], $id);
                $wpdb->show_errors();
                $return = $wpdb->query($sql);
                if ($return == false) {
                    echo '<div class="updated">' . "Error: {$sql}.</div>\n";
                } else {
                    echo '<div class="updated">Updated: no meeting <a href="post.php?action=edit&post=' . $postID . '">Edit</a> / <a href="' . get_post_permalink($id) . '">View</a></div>';
                }
            } else {
                $cddate = $_POST["nomeeting"];
                $my_post['post_name'] = sanitize_title($my_post['post_title'] . '-' . $cddate);
                // Insert the post into the database
                if ($postID = wp_insert_post($my_post)) {
                    $sql = "INSERT INTO " . $wpdb->prefix . "rsvp_dates SET datetime='{$cddate}', duration='allday', postID=" . $postID;
                    $wpdb->show_errors();
                    $return = $wpdb->query($sql);
                    if ($return == false) {
                        echo '<div class="updated">' . "Error: {$sql}.</div>\n";
                    } else {
                        echo '<div class="updated">Posted: event for ' . $cddate . ' <a href="post.php?action=edit&post=' . $postID . '">Edit</a> / <a href="' . get_post_permalink($postID) . '">View</a></div>';
                    }
                    add_post_meta($postID, '_meet_recur', $t, true);
                }
            }
        }
        global $current_user;
        $sql = "SELECT {$wpdb->posts}.ID postID, {$wpdb->posts}.post_title, {$wpdb->posts}.post_author, " . $wpdb->prefix . "rsvp_dates.datetime, DATE_FORMAT(" . $wpdb->prefix . "rsvp_dates.datetime,'%Y%m') as month, " . $wpdb->prefix . "posts.ID as rsvp_id FROM " . $wpdb->prefix . "rsvp_dates JOIN `" . $wpdb->prefix . "postmeta` ON " . $wpdb->prefix . "rsvp_dates.postID = " . $wpdb->prefix . "postmeta.post_id JOIN " . $wpdb->prefix . "posts ON " . $wpdb->prefix . "posts.ID = " . $wpdb->prefix . "postmeta.post_id WHERE post_status='publish' AND `meta_key` = '_meet_recur' AND meta_value=" . $t . " and datetime > CURDATE() ORDER BY datetime";
        $wpdb->show_errors();
        $sched_result = $wpdb->get_results($sql);
        if ($sched_result) {
            foreach ($sched_result as $index => $sched) {
                $a = $index % 2 ? "" : "alternate";
                $thistime = strtotime($sched->datetime);
                $nomeeting .= sprintf('<option value="%s">%s (%s)</option>', $sched->postID, date('F j, Y', $thistime), __('Already Scheduled', 'rsvpmaker'));
                $donotproject[] = date('Y-m-j', $thistime);
                if (current_user_can("delete_post", $sched->postID)) {
                    $delete_text = __('Move to Trash');
                    $d = '<a class="submitdelete deletion" href="' . get_delete_post_link($sched->postID) . '">' . $delete_text . '</a>';
                } else {
                    $d = '-';
                }
                $edit = $sched->post_author == $current_user->ID || $template_editor ? sprintf('<a href="%s?post=%d&action=edit">' . __('Edit', 'rsvpmaker') . '</a>', admin_url("post.php"), $sched->postID) : '-';
                $editlist .= sprintf('<tr class="%s"><td><input type="checkbox" name="update_from_template[]" value="%s" /></td><td>%s</td><td>%s</td><td>%s</td><td><a href="%s">%s</a></td></tr>', $a, $sched->postID, $edit, $d, date('F d, Y', $thistime), get_post_permalink($sched->postID), $sched->post_title);
            }
        }
        $projected = rsvpmaker_get_projected($template);
        foreach ($projected as $i => $ts) {
            $today = date('d', $ts);
            $cm = date('n', $ts);
            $y = date('Y', $ts);
            $y2 = $y + 1;
            ob_start();
            if ($ts < time()) {
                continue;
            }
            // omit dates past
            if (is_array($donotproject) && in_array(date('Y-m-j', $ts), $donotproject)) {
                continue;
            }
            $nomeeting .= sprintf('<option value="%s">%s</option>', date('Y-m-d', $ts), date('F j, Y', $ts));
            ?>
<div style="font-family:Courier, monospace"><input name="recur_check[<?php 
            echo $i;
            ?>
]" type="checkbox" value="1">
<?php 
            _e('Month', 'rsvpmaker');
            ?>
: 
              <select name="recur_month[<?php 
            echo $i;
            ?>
]"> 
              <option value="<?php 
            echo $cm;
            ?>
"><?php 
            echo $cm;
            ?>
</option> 
              <option value="1">1</option> 
              <option value="2">2</option> 
              <option value="3">3</option> 
              <option value="4">4</option> 
              <option value="5">5</option> 
              <option value="6">6</option> 
              <option value="7">7</option> 
              <option value="8">8</option> 
              <option value="9">9</option> 
              <option value="10">10</option> 
              <option value="11">11</option> 
              <option value="12">12</option> 
              </select> 
            <?php 
            _e('Day', 'rsvpmaker');
            ?>
 
            <select name="recur_day[<?php 
            echo $i;
            ?>
]"> 
<?php 
            echo sprintf('<option value="%s">%s</option>', $today, $today);
            ?>
              <option value="">Not Set</option>
              <option value="1">1</option> 
              <option value="2">2</option> 
              <option value="3">3</option> 
              <option value="4">4</option> 
              <option value="5">5</option> 
              <option value="6">6</option> 
              <option value="7">7</option> 
              <option value="8">8</option> 
              <option value="9">9</option> 
              <option value="10">10</option> 
              <option value="11">11</option> 
              <option value="12">12</option> 
              <option value="13">13</option> 
              <option value="14">14</option> 
              <option value="15">15</option> 
              <option value="16">16</option> 
              <option value="17">17</option> 
              <option value="18">18</option> 
              <option value="19">19</option> 
              <option value="20">20</option> 
              <option value="21">21</option> 
              <option value="22">22</option> 
              <option value="23">23</option> 
              <option value="24">24</option> 
              <option value="25">25</option> 
              <option value="26">26</option> 
              <option value="27">27</option> 
              <option value="28">28</option> 
              <option value="29">29</option> 
              <option value="30">30</option> 
              <option value="31">31</option> 
            </select> 
            <?php 
            _e('Year', 'rsvpmaker');
            ?>
            <select name="recur_year[<?php 
            echo $i;
            ?>
]"> 
              <option value="<?php 
            echo $y;
            ?>
"><?php 
            echo $y;
            ?>
</option> 
              <option value="<?php 
            echo $y2;
            ?>
"><?php 
            echo $y2;
            ?>
</option> 
            </select>
</div>

<?php 
            $add_date_checkbox .= ob_get_clean();
            if (!isset($add_one)) {
                $add_one = str_replace('type="checkbox"', 'type="hidden"', $add_date_checkbox);
            }
        }
        // end for loop
        $checkallscript = "<script>\njQuery(function () {\n    jQuery('.checkall').on('click', function () {\n        jQuery(this).closest('fieldset').find(':checkbox').prop('checked', this.checked);\n    });\n});\n</script>\n";
        $action = admin_url('edit.php?post_type=rsvpmaker&page=rsvpmaker_template_list&t=' . $t);
        if ($editlist) {
            echo '<strong>' . __('Already Scheduled', 'rsvpmaker') . ':</strong><br /><br /><form method="post" action="' . $action . '">
<fieldset>
<table  class="wp-list-table widefat fixed posts" cellspacing="0">
<thead>
<tr><th class="manage-column column-cb check-column" scope="col" ><input type="checkbox" class="checkall" title="Check all"></th><th>' . __('Edit') . '</th><th>' . __('Move to Trash') . '<th>' . __('Date') . '</th><th>' . __('Title') . '</th></tr>
</thead>
<tbody>
' . $editlist . '
</tbody></table>
</fieldset>
<input type="submit" value="' . __('Update Checked', 'rsvpmaker') . '" /></form>' . '<p>' . __('Update function copies title and content of current template, replacing the existing content of checked posts.', 'rsvpmaker') . '</p>';
        }
        if (current_user_can('edit_rsvpmakers')) {
            printf('<div class="group_add_date"><br />
<form method="post" action="%s">
<strong>' . __('Add One', 'rsvpmaker') . ':</strong><br />
%s
<input type="hidden" name="template" value="%s" />
<br /><input type="submit" value="' . __('Add From Template', 'rsvpmaker') . '" />
</form>
<form method="post" action="%s">
<br /><strong>' . __('Projected Dates', 'rsvpmaker') . ':</strong>
<fieldset>
<div><input type="checkbox" class="checkall"> ' . __('Check all', 'rsvpmaker') . '</div>
%s
</fieldset>
<br /><input type="submit" value="' . __('Add From Template', 'rsvpmaker') . '" />
<input type="hidden" name="template" value="%s" />
</form>
</div><br />
%s', $action, $add_one, $t, $action, $add_date_checkbox, $t, $checkallscript);
        }
        if (current_user_can('edit_rsvpmakers')) {
            printf('<div class="group_add_date"><br />
<form method="post" action="%s">
<strong>%s:</strong><br />
%s: <select name="nomeeting">%s</select>
<br />%s:<br /><textarea name="nomeeting_note" cols="60" %s></textarea>
<input type="hidden" name="template" value="%s" />
<br /><input type="submit" value="%s" />
</form>
</div><br />
', $action, __('No Meeting', 'rsvpmaker'), __('Regularly Scheduled Date', 'rsvpmaker'), $nomeeting, __('Note (optional)', 'rsvpmaker'), 'style="max-width: 95%;"', $t, __('Submit', 'rsvpmaker'));
        }
    }