Skip to content

Commit

Permalink
get ready the manage project page: list milestones and versions,
Browse files Browse the repository at this point in the history
add new milestone / version
delete milestone /version
  • Loading branch information
seanchen committed Feb 9, 2013
1 parent ae17011 commit 3d2ee3a
Show file tree
Hide file tree
Showing 3 changed files with 414 additions and 18 deletions.
64 changes: 57 additions & 7 deletions wp-trac-client/admin-tags.php
Expand Up @@ -27,11 +27,11 @@ function wptc_create_tables($force=false) {
name varchar(100) not null,
description varchar(512) not null default '',
PRIMARY KEY (id),
KEY name (name)
UNIQUE KEY name (name)
);";
wptc_logging("create project table: " . $sql);
dbDelta($sql);

// the project metadata table.
// the type will be one of [milestone, version]
$sql = "CREATE TABLE " . WPTC_PROJECT_METADATA . " (
Expand All @@ -42,9 +42,9 @@ function wptc_create_tables($force=false) {
description varchar(512) not null default '',
due_date datetime not null default '0000-00-00 00:00:00',
PRIMARY KEY (id),
KEY id (id)
UNIQUE KEY name (name)
);";
wptc_logging("crate project metadata table: ". $sql);
wptc_logging("create project metadata table: ". $sql);
dbDelta($sql);
}

Expand Down Expand Up @@ -83,19 +83,31 @@ function wptc_get_project($name) {

global $wpdb;

// query project.
$query = "select * from " . WPTC_PROJECT .
" where name = %s";
$query = $wpdb->prepare($query, $name);
$project = $wpdb->get_row($query, ARRAY_A);
if(count($project) < 1) {
// not such project exist.
return $project;
}

// query all metadats
$query = "select * from " . WPTC_PROJECT_METADATA .
" where project_id = %d order by due_date DESC";
$query = $wpdb->prepare($query, $project['id']);
$meta = $wpdb->get_results($query, ARRAY_A);
$project['meta'] = $meta;

return $project;
}

function wptc_remove_project($name) {
function wptc_remove_byname($table_name, $name) {

global $wpdb;
$query = "delete from " . WPTC_PROJECT .

$query = "delete from " . $table_name .
" where name = %s";
$query = $wpdb->prepare($query, $name);
// if error, false is return.
Expand All @@ -104,3 +116,41 @@ function wptc_remove_project($name) {

return $rows;
}

function wptc_update_mandv($project_name, $type, $name,
$description, $duedate) {

global $wpdb;
$project = wptc_get_project($project_name);

$success = $wpdb->insert(
WPTC_PROJECT_METADATA,
array(
'name' => $name,
'description' => $description,
'type' => $type,
'due_date' => $duedate->format('Y-m-d H:i:s'),
'project_id' => $project['id']
),
array(
'%s',
'%s',
'%s',
'%s',
'%d'
)
);

return $success;
}

function wptc_get_mandv($name) {

global $wpdb;
$query = "select * from " . WPTC_PROJECT_METADATA .
" where name = %s";
$query = $wpdb->prepare($query, $name);
$mandv = $wpdb->get_row($query, ARRAY_A);

return $mandv;
}
179 changes: 170 additions & 9 deletions wp-trac-client/admin-widgets.php
Expand Up @@ -104,10 +104,19 @@ function wptc_handle_pm_submit($context) {
$_POST['wptcaddproject_submit'] === 'Y') {

wptc_handle_add_new_project();
} else if(isset($_REQUEST['action']) &&
$_REQUEST['action'] === 'deleteproject') {
//
wptc_handle_delete_project();
} else if(isset($_POST['wptc_mandv_form_submit']) &&
$_POST['wptc_mandv_form_submit'] === 'Y') {
wptc_handle_mandv_formsubmit();
} else if(isset($_REQUEST['action'])) {
switch($_REQUEST['action']) {
case 'deleteproject':
//
wptc_handle_delete("project");
break;
case 'deletemandv':
wptc_handle_delete("mandv");
break;
}
}
}

Expand All @@ -119,6 +128,7 @@ function wptc_handle_add_new_project() {
$name = trim($_POST['wptc_projectname']);
$desc = trim($_POST['wptc_projectdesc']);

// Server site validation.
if($name === "" || $desc === "") {
echo <<<EOT
<div class="error"><p>
Expand All @@ -145,15 +155,28 @@ function wptc_handle_add_new_project() {
}

/**
* delete projects.
* delete a project for mandv
*/
function wptc_handle_delete_project() {
function wptc_handle_delete($type) {

$name = $_REQUEST['project'];
wptc_remove_project($name);
$name = $_REQUEST[$type];
switch($type) {
case 'project':
$table_name = WPTC_PROJECT;
$label = 'Project';
break;
case 'mandv':
$table_name = WPTC_PROJECT_METADATA;
$label = 'Milestone / Version';
break;
default:
// do nothing.
return;
}
wptc_remove_byname($table_name, $name);
echo <<<EOT
<div class="updated"><p><strong>
Project <em><b>$name</b></em> Removed!
{$label} <em><b>{$name}</b></em> Removed!
</strong></p></div>
EOT;
}
Expand All @@ -164,9 +187,147 @@ function wptc_handle_delete_project() {
function wptc_widget_manage_project() {

$name = $_REQUEST['project'];
$milestoneList = new WPTC_Milestone_List_Table($name);
$milestoneList->prepare_items();
$page = $_REQUEST['page'];

echo <<<EOT
<div id="icon-edit-pages" class="icon32"><br/></div>
<h2>Manage Project <b>$name</b></h2>
EOT;
// milestone edit form.
wptc_widget_mandv_form($name);

echo <<<EOT
<h3>List of Milestones</h3>
<form id="projects-filter" method="get">
<input type="hidden" name="page" value="{$page}"/>
EOT;
// show the list table.
$milestoneList->display();
echo "</form>";
}

/**
* milestone and version list for a project.
*/
function wptc_widget_mandv_form($project) {

$page = $_REQUEST['page'];
$form_id = 'wptc_mandv_form';

echo <<<EOT
<h3>Add New Milestone / Version</h3>
<form id="{$form_id}" method="post" name="{$form_id}"
action=""
class="validate">
<input type="hidden" name="page" value="{$page}"/>
<input type="hidden" name="wptc_projectname" value="{$project}"/>
<input type="hidden" name="{$form_id}_submit" value="Y"/>
EOT;
wp_nonce_field('create-edit-mandv',
'_wpnonce_create_edit_mandv');
echo <<<EOT
<table class="form-table"><tbody>
<tr>
<th scope="row">
<label for="wptc_mandvtype">Type: </label>
</th>
<td>
<select id="wptc_mandvtype" name="wptc_mandvtype">
<option value="version" selected>Version</option>
<option value="milestone">Milestone</option>
</select>
</td>
</tr>
<tr>
<th scope="row">
<label for="wptc_mandvname">Name:
<span class="description">(required)</span>
</label>
</th>
<td><input type="text" id="wptc_mandvname"
name="wptc_mandvname"
value="" size="58"/>
</td>
</tr>
<tr>
<th scope="row">
<label for="wptc_mandvdesc">Description:
<span class="description">(required)</span>
</label>
</th>
<td><input type="text" id="wptc_mandvdesc"
name="wptc_mandvdesc"
value="" size="88"/>
</td>
</tr>
<tr>
<th scope="row">
<label for="wptc_mandvduedate">Due Date:
<span class="description">(required)</span>
</label>
</th>
<td><input type="text" id="wptc_mandvduedate"
name="wptc_mandvduedate"/>
</td>
</tr>
</tbody></table>
EOT;

wp_enqueue_script('jquery-ui-datepicker');
wp_enqueue_style('jquery-ui');
submit_button('Add New Milestone / Version', 'primary',
'mandvsubmit', true,
array('id' => 'mandvsubmit'));
echo <<<EOT
<script>
jQuery(function() {
jQuery("#wptc_mandvduedate").datepicker();
});
</script>
</form>
EOT;
}

/**
* handle form submit for milestone and version
*/
function wptc_handle_mandv_formsubmit() {

$projectName = trim($_POST['wptc_projectname']);
$type = trim($_POST['wptc_mandvtype']);
$name = trim($_POST['wptc_mandvname']);
$desc = trim($_POST['wptc_mandvdesc']);
// due date with format mm/dd/yyyy from date picker
$due = trim($_POST['wptc_mandvduedate']);

if($name === "" || $desc === "" || $due === "") {
echo <<<EOT
<div class="error"><p>
Fields <em><b>Name</b></em>, <em><b>Description</b></em>
and <em><b>Type</b></em>
are <em><b>required</b></em> for a new {$type}.
</p></div>
EOT;
} else if(count(wptc_get_mandv($name)) > 0){
echo <<<EOT
<div class="error"><p>
{$type} <em><b>$name</b></em> already exist!
</p></div>
EOT;
} else {
// adding time to the end of the date
$duedate = DateTime::createFromFormat('m/d/Y H:i:s',
$due . ' 17:00:00');
$success = wptc_update_mandv($projectName, $type, $name,
$desc, $duedate);
echo <<<EOT
<div class="updated"><p><strong>
{$type} <b>$name</b> Added!
</strong></p></div>
EOT;
}
}

0 comments on commit 3d2ee3a

Please sign in to comment.