/** * 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 -->'; }