From d5c2be1922dec286dd5610a95783ae835b209759 Mon Sep 17 00:00:00 2001 From: Sean Chen Date: Wed, 6 Feb 2013 13:18:26 -0500 Subject: [PATCH] get start the project management page: projects list is based on WP_List_Table Add new porject is working, we are using server side validation. --- wp-trac-client/admin-manager.php | 36 +++- wp-trac-client/admin-settings.php | 1 + wp-trac-client/admin-tags.php | 92 +++++++++ wp-trac-client/admin-widgets.php | 323 ++++++++++++++++++++++++++++++ 4 files changed, 451 insertions(+), 1 deletion(-) create mode 100644 wp-trac-client/admin-tags.php create mode 100644 wp-trac-client/admin-widgets.php diff --git a/wp-trac-client/admin-manager.php b/wp-trac-client/admin-manager.php index 52eb919..347b4e6 100644 --- a/wp-trac-client/admin-manager.php +++ b/wp-trac-client/admin-manager.php @@ -1,3 +1,37 @@ + +
-

WordPress Trac Client - Management

+ + + +$wptc_db_version

+

file: $file, basename: $filename

+

MY_PLUGIN_PATH: {$path}

+EOT; +} +?>
diff --git a/wp-trac-client/admin-settings.php b/wp-trac-client/admin-settings.php index cc39990..bc4bdd4 100644 --- a/wp-trac-client/admin-settings.php +++ b/wp-trac-client/admin-settings.php @@ -1,5 +1,6 @@ get_results($query, ARRAY_A); + return $projects; +} + +function wptc_add_project($name, $description) { + + global $wpdb; + + $success = $wpdb->insert( + WPTC_PROJECT, + array( + 'name' => $name, + 'description' => $description + ), + array( + '%s', + '%s' + ) + ); + + return $success; +} + +function wptc_get_project($name) { + + global $wpdb; + + $query = "select * from " . WPTC_PROJECT . + " where name = %s"; + $query = $wpdb->prepare($query, $name); + $project = $wpdb->get_row($query, ARRAY_A); + + return $project; +} diff --git a/wp-trac-client/admin-widgets.php b/wp-trac-client/admin-widgets.php new file mode 100644 index 0000000..7bb371f --- /dev/null +++ b/wp-trac-client/admin-widgets.php @@ -0,0 +1,323 @@ + 'project', + 'plural' => 'projects', + 'ajax' => false + )); + } + + /** + * define the columns here, + */ + function get_columns() { + // the values for each key will show as the + // title columns + // the key will be used in method name to + // customize the column value for each item/row. + $columns = array( + 'cb' => '', + 'name' => 'Name', + 'description' => 'Description' + // current milestones / versions + // components + ); + return $columns; + } + + /** + * cb is the checkbox column, it will be treated specially! + * this method customize the value of cb column for each + * item (each row). + */ + function column_cb($item) { + return sprintf( + '', + // for %1$s, using lable for singular, + // defined in the consturct method. + $this->_args['singular'], + // project id is the value. + $item['id'] + ); + } + + /** + * customize the value for name column. + */ + function column_name($item) { + + // Build row actions: Edit and Delete + $aTemp = '%s'; + $actions = array( + 'edit' => sprintf($aTemp, $_REQUEST['page'], + 'edit',$item['id'], 'Edit'), + 'delete' => sprintf($aTemp, $_REQUEST['page'], + 'delete',$item['id'], 'Delete'), + ); + + // Return the title contents + // (id:%2$s) + // /*$2%s*/ $item['ID'], + return sprintf('%1$s %2$s', + /*$1%s*/ $item['name'], + /*$2%s*/ $this->row_actions($actions) + ); + } + + /** + * here is for easy columns. + * column_name should be one the keys defined in + * method get_columns. + */ + function column_default($item, $column_name) { + + switch($column_name) { + case 'description': + return $item[$column_name]; + default: + // should not happen. + // in case it happens, print out details... + return print_r($item, true); + } + } + + /** + * set the sortable columns here. + */ + function get_sortable_columns() { + + $sortable_columns = array( + // true means it's already sorted + 'name' => array('title',false), + 'description' => array('director',false) + ); + + return $sortable_columns; + } + + /** + * set bulk actions for checkboxes. + */ + function get_bulk_actions() { + + $actions = array( + 'delete' => 'Delete' + ); + + return $actions; + } + + /** + * handle bulk action here. + */ + function process_bulk_action() { + + if ('delete' === $this->current_action()) { + wp_die('action place holder for now'); + } + } + + /** + * get ready the data here. + */ + function prepare_items() { + + //global $wpdb; + + // how many items per page. + $per_page = 5; + $columns = $this->get_columns(); + // no hidden for now. + $hidden = array(); + $sortable = $this->get_sortable_columns(); + + $this->_column_headers = array($columns, $hidden, + $sortable); + $this->process_bulk_action(); + + $data = wptc_get_projects(); + + // this is array sorting, + // we could query database directly + function usort_reorder($a,$b){ + $orderby = (!empty($_REQUEST['orderby'])) ? $_REQUEST['orderby'] : 'name'; //If no sort, default to name + $order = (!empty($_REQUEST['order'])) ? $_REQUEST['order'] : 'asc'; //If no order, default to asc + // Determine sort order + $result = strcmp($a[$orderby], $b[$orderby]); + // Send final sort direction to usort + return ($order==='asc') ? $result : -$result; + } + usort($data, 'usort_reorder'); + + // for pagination. + $current_page = $this->get_pagenum(); + $total_items = count($data); + $data = array_slice($data, + (($current_page - 1) * $per_page), + $per_page); + + // here is the data + $this->items = $data; + + // tracking pages. + $this->set_pagination_args(array( + 'total_items' => $total_items, + 'per_page' => $per_page, + 'total_pages' => ceil($total_items / $per_page) + )); + } +} + +/** + * load and analyzt the request context. + * the context will be array with + */ +function wptc_widget_pm_context() { + + $context = array(); + if (isset($_REQUEST['action'])) { + $context['action'] = $_REQUEST['action']; + } else { + $context['action'] = 'list'; + } + + return $context; +} + +/** + * get ready the list of projects. + */ +function wptc_widget_projects_list() { + + $projectListTable = new WPTC_Project_List_Table(); + $projectListTable->prepare_items(); + $page = $_REQUEST['page']; + + echo <<
+

Trac Projects

+EOT; + // new projcet form. + wptc_widget_new_project(); + + echo <<List of Projects + +
+ +EOT; + // show the list table. + $projectListTable->display(); + echo "
"; +} + +function wptc_widget_new_project() { + + $page = $_REQUEST['page']; + //$form_id = 'wptc_new_project_form'; + $form_id = 'wptcaddproject'; + + echo <<
+

Add New Project

+ +
+ + +EOT; + wp_nonce_field('add-new-project', + '_wpnonce_add-new-project' ); + echo << + + + + + + + + + + + + + + + +EOT; + submit_button('Add New Project', 'primary', + 'addproject', true, + array('id' => 'addproject')); + echo ""; +} + +/** + * facility to handle submit in project management page. + */ +function wptc_handle_pm_submit($context) { + + //if(isset($_POST['wptc_new_project_form_submit']) && + // $_POST['wptc_new_project_form_submit'] === 'Y') { + if(isset($_POST['wptcaddproject_submit']) && + $_POST['wptcaddproject_submit'] === 'Y') { + + wptc_handle_add_new_project(); + } +} + +/** + * handle add new project. + */ +function wptc_handle_add_new_project() { + + $name = trim($_POST['wptc_projectname']); + $desc = trim($_POST['wptc_projectdesc']); + + if($name === "" || $desc === "") { + echo <<

+ Both Name and Description + are required for a new project. +

+EOT; + } else if(count(wptc_get_project($name)) > 0) { + echo <<

+ Project $name already exist! +

+EOT; + } else { + + wptc_add_project($name, $desc); + + echo <<

+ Project Added! +

+EOT; + } +}