コード例 #1
0
ファイル: admin.php プロジェクト: sabdev1/ljcdevsab
 /**
  * Add a Sheet Page
  */
 function modify_sheet_page()
 {
     if (!current_user_can('manage_options') && !current_user_can('manage_signup_sheets')) {
         wp_die(__('You do not have sufficient permissions to access this page.'));
     }
     // Set mode vars
     $edit = empty($_GET['sheet_id']) ? false : true;
     $add = $edit ? false : true;
     $submitted = isset($_POST['mode']) && $_POST['mode'] == 'submitted';
     $err = 0;
     // Process form if submitted
     if ($submitted) {
         try {
             // Sheet
             if ($add) {
                 $result = $this->data->add_sheet($_POST);
             } else {
                 if ($edit) {
                     $result = $this->data->update_sheet($_POST, $_GET['sheet_id']);
                 }
             }
             $sheet_id = $add ? $result : $_GET['sheet_id'];
             echo '<div class="updated"><p><strong>' . __('Sheet saved.', 'dls-sus-menu') . '</strong></p></div>';
             // Categories
             $sheet_categories = $this->data->get_categories_by_sheet($sheet_id);
             $curr_categories = array();
             foreach ($sheet_categories as $sheet_category) {
                 $curr_categories[] = $sheet_category->category_id;
             }
             $input_categories = isset($_POST['categories']) ? $_POST['categories'] : array();
             $categories_to_add = array_diff((array) $input_categories, (array) $curr_categories);
             $categories_to_delete = array_diff((array) $curr_categories, (array) $input_categories);
             foreach ($categories_to_add as $category_id) {
                 if (!empty($category_id)) {
                     $add_sheet_category_result = $this->data->add_sheet_category($sheet_id, $category_id);
                     if (is_wp_error($add_sheet_category_result)) {
                         throw new DLS_SUS_Data_Exception($add_sheet_category_result->get_error_message());
                     }
                 }
             }
             foreach ($categories_to_delete as $category_id) {
                 foreach ($sheet_categories as $sheet_category) {
                     if ($sheet_category->category_id == $category_id) {
                         $this->data->delete_sheet_category($sheet_category->id);
                     }
                 }
             }
             // Tasks
             $tasks = $this->data->get_tasks($_GET['sheet_id']);
             $tasks_to_delete = array();
             $tasks_to_update = array();
             $keys_to_process = array();
             foreach ($_POST['task_title'] as $key => $value) {
                 $keys_to_process[] = $key;
             }
             // Queue for removal: tasks where the fields were emptied out
             for ($i = 0; $i < count($_POST['task_id']); $i++) {
                 if (empty($_POST['task_title'][$i])) {
                     if (!empty($_POST['task_id'][$i])) {
                         $tasks_to_delete[] = $_POST['task_id'][$i];
                     }
                     continue;
                 } else {
                     $tasks_to_update[] = $_POST['task_id'][$i];
                     $signup_count = count($this->data->get_signups($_POST['task_id'][$i]));
                     if ($signup_count > $_POST['task_qty'][$i]) {
                         $err++;
                         if (!empty($err)) {
                             echo '<div class="error"><p><strong>' . __('The number of spots for task "' . $_POST['task_title'] . '" cannot be set below ' . $signup_count . ' because it currently has ' . $signup_count . ' ' . ($signup_count > 1 ? 'people' : 'person') . ' signed up.  Please clear some spots first before updating this task.') . '</strong></p></div>';
                         }
                     }
                 }
             }
             // Queue for removal: tasks that are no longer in the list
             foreach ($tasks as $task) {
                 if (!in_array($task->id, $_POST['task_id'])) {
                     $tasks_to_delete[] = $task->id;
                     $signup_count = count($this->data->get_signups($task->id));
                     if ($signup_count > 0) {
                         $err++;
                         if (!empty($err)) {
                             echo '<div class="error"><p><strong>' . __('The task "' . $task->title . '" cannot be removed because it has ' . $signup_count . ' ' . ($signup_count > 1 ? 'people' : 'person') . ' signed up.  Please clear all spots first before removing this task.') . '</strong></p></div>';
                         }
                     }
                 }
             }
             if (empty($err)) {
                 $i = 0;
                 foreach ($keys_to_process as $key) {
                     if (empty($_POST['task_title'][$key])) {
                         continue;
                     }
                     foreach ($this->data->tables['task']['allowed_fields'] as $field => $nothing) {
                         if (!isset($_POST['task_' . $field])) {
                             continue;
                         }
                         $task_data['task_' . $field] = $_POST['task_' . $field][$key];
                         $task_data['task_position'] = $i;
                     }
                     $sheet = new DLS_SUS_Sheet($sheet_id);
                     if (!empty($sheet->custom_fields['task'])) {
                         foreach ($sheet->custom_fields['task'] as $field) {
                             $slug = str_replace('-', '_', $field['slug']);
                             $task_data['task_' . $slug] = $_POST['task_' . $slug][$key];
                         }
                         reset($sheet->custom_fields['task']);
                     }
                     $task_data['task_sheet_id'] = $sheet_id;
                     if (empty($_POST['task_id'][$key])) {
                         if (($result = $this->data->add_task($task_data, $sheet_id)) === false) {
                             $err++;
                         }
                     } else {
                         if (($result = $this->data->update_task($task_data, $_POST['task_id'][$key])) === false) {
                             $err++;
                         }
                     }
                     $i++;
                 }
                 if (!empty($err)) {
                     echo '<div class="error"><p><strong>' . __('Error saving ' . $err . ' task' . ($err > 1 ? 's' : '') . '.', 'dls-sus-menu') . '</strong></p></div>';
                 }
                 // Delete unused tasks
                 foreach ($tasks_to_delete as $task_id) {
                     if ($this->data->delete_task($task_id) === false) {
                         echo '<div class="error"><p><strong>' . __('Error removing a task.', 'dls-sus-menu') . '</strong></p></div>';
                     }
                 }
             }
         } catch (DLS_SUS_Data_Exception $e) {
             $err++;
             echo '<div class="error"><p><strong>' . __($e->getMessage()) . '</strong></p></div>';
         }
     }
     // Set field values for form
     $fields = isset($_POST) && !$add ? $this->data->stripslashes_full($_POST) : null;
     if ($edit && empty($err)) {
         $sheet_fields = array();
         $task_fields = array();
         $custom_task_fields = array();
         // Pull from DB instead
         $sheet = new DLS_SUS_Sheet($_GET['sheet_id']);
         if ($sheet->is_valid()) {
             $sheet_fields = array();
             foreach ($sheet->get_data() as $k => $v) {
                 $sheet_fields['sheet_' . $k] = $v;
             }
         }
         if ($tasks = $this->data->get_tasks($_GET['sheet_id'])) {
             $task_fields = array();
             foreach ($tasks as $task) {
                 $task_fields['task_id'][] = $task->id;
                 $task_fields['task_title'][] = $task->title;
                 $task_fields['task_date'][] = $task->date;
                 $task_fields['task_qty'][] = $task->qty;
                 // Custom fields
                 if (!empty($task->fields)) {
                     foreach ($task->fields as $slug => $value) {
                         $custom_task_fields['task_' . $slug][] = $value;
                     }
                     reset($task->fields);
                 }
             }
             reset($tasks);
         }
         $fields = array_merge((array) $sheet_fields, (array) $task_fields, (array) $custom_task_fields);
     }
     // Display Form
     echo '<div class="wrap dls_sus">';
     echo '<div id="icon-dls-sus" class="icon32"><br /></div>';
     echo '<h2>' . ($add ? 'Add' : 'Edit') . ' Sign-up Sheet</h2>';
     echo '<div id="poststuff">';
     echo '<div id="post-body" class="metabox-holder columns-1">';
     echo '<div id="post-body-content">';
     $this->display_sheet_form($fields, $sheet);
     echo '</div><!-- #post-body-content -->';
     echo '</div><!-- #post-body -->';
     echo '</div><!-- #poststuff -->';
     echo '</div><!-- .wrap -->';
 }
コード例 #2
0
ファイル: sign-up-sheets.php プロジェクト: sabdev1/ljcdevsab
 /**
  * Output the volunteer signup form
  *
  * @param array @atts attributes from shortcode call
  * @return string
  */
 function display_sheet($atts)
 {
     extract(shortcode_atts(array('id' => false, 'list_title' => 'Current Sign-up Sheets', 'category_id' => false, 'list_title_is_category' => 'false'), $atts));
     $return = null;
     $force_one_sheet = false;
     $show_backlink = false;
     if (!empty($_GET['sheet_id'])) {
         $id = $_GET['sheet_id'];
     }
     // ID overrides shortcode id if defined
     if (!empty($_GET['sheet_id']) || !empty($_GET['task_id'])) {
         $force_one_sheet = true;
         $show_backlink = true;
     }
     if (!empty($_GET['task_id'])) {
         $task = $this->data->get_task($_GET['task_id']);
         $id = $task->sheet_id;
         // TODO: get sheet id from task id
     }
     $this->shortcode_count++;
     if ($id === false && $force_one_sheet === false) {
         // Display all active
         if ($category_id !== false && $list_title_is_category === 'true') {
             $category = $this->data->get_category($category_id);
             $list_title = $category->title;
         }
         $return = '<h2>' . $list_title . '</h2>';
         if ($category_id === false) {
             $sheets = $this->data->get_sheets(false, true);
         } else {
             $sheets = $this->data->get_sheets(false, true, $category_id);
         }
         $sheets = array_reverse($sheets);
         if (empty($sheets)) {
             $return .= '<p>No sheets available at this time.</p>';
         } else {
             $return .= '
             <table class="dls-sus-sheets" cellspacing="0">
                 <thead>
                     <tr>
                         <th class="column-title">Title</th>
                         <th class="column-date">Date</th>
                         <th class="column-open_spots">Open Spots</th>
                         <th class="column-view_link">&nbsp;</th>
                     </tr>
                 </thead>
                 <tbody>
                 ';
             foreach ($sheets as $sheet) {
                 $open_spots = $this->data->get_sheet_total_spots($sheet->id) - $this->data->get_sheet_signup_count($sheet->id);
                 if ($sheet->end_date == '0000-00-00') {
                     $display_date = 'N/A';
                 } else {
                     $display_date = $sheet->start_date == $sheet->end_date ? null : date(get_option('date_format'), strtotime($sheet->start_date)) . ' - ';
                     $display_date .= date(get_option('date_format'), strtotime($sheet->end_date));
                 }
                 $display_date = $return .= '
                         <tr' . ($open_spots === 0 ? ' class="filled"' : '') . '>
                             <td class="column-title"><a href="' . $this->request_uri . 'sheet_id=' . $sheet->id . '">' . $sheet->title . '</a></td>
                             <td class="column-date">' . $display_date . '</td>
                             <td class="column-open_spots">' . $open_spots . '</td>
                             <td class="column-view_link">' . ($open_spots > 0 ? '<a href="' . $this->request_uri . 'sheet_id=' . $sheet->id . '">View &amp; sign-up &raquo;</a>' : '&#10004; Filled') . '</td>
                         </tr>
                     ';
             }
             $return .= '
                 </tbody>
             </table>
         ';
         }
     } else {
         // Display Individual Sheet
         if ($force_one_sheet && $this->shortcode_count > 1) {
             return null;
         }
         // Do not process multiple short codes on one page
         $sheet = new DLS_SUS_Sheet($id);
         if (!$sheet->is_valid() || !empty($sheet->trash)) {
             $return .= '<p>' . __('Sign-up sheet not found.', $this->plugin_prefix) . '</p>';
             return $return;
         } else {
             if ($show_backlink) {
                 $return .= '<p class="dls-sus-backlink"><a href="' . remove_query_arg(array('sheet_id', 'task_id'), $_SERVER['REQUEST_URI']) . '">' . __('&laquo; View all', 'dls-sus') . '</a></p>';
             }
             $return .= '
             <div class="dls-sus-sheet">
                 <h2>' . $sheet->title . '</h2>
         ';
             $submitted = isset($_POST['mode']) && $_POST['mode'] == 'submitted';
             $err = 0;
             $success = false;
             // Process Sign-up Form
             if ($submitted) {
                 // reCAPTCHA
                 $recaptcha_resp = null;
                 $recaptcha_error = null;
                 # was there a reCAPTCHA response?
                 if (isset($_POST["recaptcha_response_field"])) {
                     $resp = $this->recaptcha->recaptcha_check_answer($this->private_key, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);
                     if ($resp->is_valid) {
                         // Valid
                     } else {
                         # set the error code so that we can display it
                         $this->recaptcha_error = $resp->error;
                     }
                 }
                 // Error Handling
                 if (!empty($sheet->custom_fields['signup'])) {
                     $custom_fields_err = 0;
                     foreach ($sheet->custom_fields['signup'] as $field) {
                         $slug = str_replace('-', '_', $field['slug']);
                         $required = isset($field['required']) && $field['required'] === 'true' ? true : false;
                         if (!$required) {
                             continue;
                         }
                         if (!isset($_POST['signup_' . $slug]) || is_string($_POST['signup_' . $slug]) && trim($_POST['signup_' . $slug]) == '' || is_array($_POST['signup_' . $slug]) && empty($_POST['signup_' . $slug])) {
                             $test = trim($_POST['signup_' . $slug]);
                             $custom_fields_err++;
                         }
                     }
                 }
                 if (empty($_POST['signup_firstname']) || empty($_POST['signup_lastname']) || empty($_POST['signup_email']) || $this->data->phone_required($sheet) && $this->data->show_phone($sheet) && empty($_POST['signup_phone']) || $this->data->address_required($sheet) && $this->data->show_address($sheet) && (empty($_POST['signup_address']) || empty($_POST['signup_city']) || empty($_POST['signup_state']) || empty($_POST['signup_zip'])) || get_option('dls_sus_recaptcha') !== 'true' && empty($_POST['spam_check']) || get_option('dls_sus_recaptcha') === 'true' && empty($_POST["recaptcha_response_field"]) || !empty($custom_fields_err)) {
                     $err++;
                     $return .= '<p class="dls-sus error">' . __('Please complete all required fields.', 'dls-sus') . '</p>';
                 } elseif (get_option('dls_sus_recaptcha') !== 'true' && (empty($_POST['spam_check']) || !empty($_POST['spam_check']) && trim($_POST['spam_check']) != '8')) {
                     $err++;
                     $return .= '<p class="dls-sus error">' . sprintf(__('Oh dear, 7 + 1 does not equal %s. Please try again.', 'dls-sus'), esc_attr($_POST['spam_check'])) . '</p>';
                 } elseif (!isset($_POST['double_signup']) && (get_option('dls_sus_recaptcha') === 'true' && $_POST["recaptcha_response_field"])) {
                     $recaptcha_resp = $this->recaptcha->recaptcha_check_answer($this->private_key, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);
                     if (!$resp->is_valid) {
                         $recaptcha_error = $recaptcha_resp->error;
                         $err++;
                         $return .= '<p class="dls-sus error">' . __('Captcha not correct. Please try again.', 'dls-sus') . '</p>';
                     }
                 }
                 // Add Signup
                 if (!$err) {
                     try {
                         // Check if already signed up for task by email address
                         if (empty($_POST['double_signup']) && $this->data->isEmailOnTask($_POST['signup_email'], $_GET['task_id'])) {
                             $return .= '
                             <p class="dls-sus alert">
                                 ' . __('You have already signed up for this task.  Do you want to sign up again?', 'dls-sus') . '
                                 <form method="post" action="' . $this->data->get_current_url(true) . '">
                                     ';
                             $prefix = 'signup_';
                             foreach ($_POST as $key => $value) {
                                 if (is_array($value)) {
                                     foreach ($value as $v) {
                                         $return .= '<input type="hidden" name="' . esc_attr($key) . '[]" value="' . esc_attr($v) . '" />' . "\n";
                                     }
                                 } else {
                                     $return .= '<input type="hidden" name="' . esc_attr($key) . '" value="' . esc_attr($value) . '" />' . "\n";
                                 }
                             }
                             $return .= '
                                     <input type="hidden" name="double_signup" value="1" />
                                     <input type="hidden" name="mode" value="submitted" />
                                     <input type="submit" name="Submit" class="button-primary" value="Yes, sign me up" />
                                     <a href="' . $_SERVER['REQUEST_URI'] . '">No, thanks</a>
                                 </form>
                             </p>
                         ';
                         } else {
                             $signup_id = $this->data->add_signup($_POST, $_GET['task_id']);
                             if (isset($_POST['dls_sus_remember']) && $_POST['dls_sus_remember'] === 'true') {
                                 $this->data->remember_signup($signup_id);
                             }
                             $success = true;
                             $return .= '<p class="dls-sus updated">' . __('You have been signed up!', 'dls-sus') . '</p>';
                             $this->mail->send_mail($_POST['signup_email'], $_GET['task_id'], $signup_id, 'signup');
                         }
                     } catch (DLS_SUS_Data_Exception $e) {
                         $err++;
                         $return .= '<p class="dls-sus error">' . __($e->getMessage(), 'dls-sus') . '</p>';
                     }
                 }
             }
             // Display Sign-up Form
             if (!$submitted || $err) {
                 if (isset($_GET['task_id'])) {
                     $return .= $this->display_signup_form($_GET['task_id']);
                     return $return;
                 }
             }
             // Sheet Details
             if (!$submitted || $success || $err) {
                 $return .= '
                 ' . ($sheet->date && $sheet->date != '0000-00-00' ? '<p>Date: ' . date(get_option('date_format'), strtotime($sheet->date)) . '</p>' : '') . '
                 <div class="dls-sus-sheet-details">' . nl2br($sheet->details) . '</div>
                 <h3>Sign up below...</h3>
             ';
                 // Tasks
                 $return .= $sheet->get_tasks_table(array('show_clear' => false, 'show_signup_link' => true));
             }
             $return .= '</div><!-- .dls-sus-sheet -->';
         }
     }
     return $return;
 }