Commit b0f50daf authored by John Punzalan's avatar John Punzalan

update Entity and news

parent e7b2fc6d
......@@ -226,6 +226,9 @@ class Esi_Management_Admin {
$plugin_admin_region = new Esi_Management_Region( $this->plugin_name, $this->version );
add_submenu_page( $this->plugin_name, __( 'Region', 'esi' ), __( 'Region', 'esi' ), 'manage_options', 'esi-management-region', array( $plugin_admin_region, 'plugin_page' ) );
$plugin_admin_ability= new Esi_Management_Ability( $this->plugin_name, $this->version );
add_submenu_page( $this->plugin_name, __( 'Ability', 'esi' ), __( 'Ability', 'esi' ), 'manage_options', 'esi-management-ability', array( $plugin_admin_ability, 'plugin_page' ) );
}
/**
......
<?php
/**
* Handle the form submissions
*
* @package Package
* @subpackage Sub Package
*/
class Esi_Management_Ability_Form {
/**
* Hook 'em all
*/
public function __construct() {
add_action( 'admin_init', array( $this, 'handle_form' ) );
}
/**
* Handle the new ability and edit form
*
* @return void
*/
public function handle_form($is_ajax = false) {
if ( !isset( $_POST['submit_ability'] ) && !$is_ajax ) {
return;
}
if ( ! wp_verify_nonce( $_POST['_wpnonce'], '' ) ) {
die( __( 'Are you cheating?', 'esi' ) );
}
if ( ! current_user_can( 'read' ) ) {
wp_die( __( 'Permission Denied!', 'esi' ) );
}
$errors = array();
$page_url = admin_url( 'admin.php?page=esi-management-ability' );
$field_id = isset( $_POST['field_id'] ) ? intval( $_POST['field_id'] ) : 0;
$name = isset( $_POST['name'] ) ? sanitize_text_field( $_POST['name'] ) : '';
$description = isset( $_POST['description'] ) ? sanitize_text_field( $_POST['description'] ) : '';
$parent = isset( $_POST['parent'] ) ? sanitize_text_field( $_POST['parent'] ) : '';
// some basic validation
if ( ! $name ) {
$errors[] = __( 'Error: Name is required', 'esi' );
}
if ( ! $description ) {
$errors[] = __( 'Error: Description is required', 'esi' );
}
// bail out if error found
if ( $errors ) {
$first_error = reset( $errors );
$redirect_to = add_query_arg( array( 'error' => $first_error ), $page_url );
wp_safe_redirect( $redirect_to );
exit;
}
$fields = array(
'name' => $name,
'description' => $description,
'parent' => $parent,
);
// New or edit?
if ( ! $field_id ) {
$insert_id = esi_insert_ability( $fields );
$fields['id'] = $insert_id;
} else {
$fields['id'] = $field_id;
$insert_id = esi_insert_ability( $fields );
}
if($is_ajax){
return $fields;
}
if ( is_wp_error( $insert_id ) ) {
$redirect_to = add_query_arg( array( 'message' => 'error' ), $page_url );
} else {
$redirect_to = add_query_arg( array( 'message' => 'success' ), $page_url );
}
wp_safe_redirect( $redirect_to );
exit;
}
}
new Esi_Management_Ability_Form();
<?php
/**
* The admin-specific functionality of the plugin.
*
* @link http://www.exapartners.com/
* @since 1.0.0
*
* @package Esi_Management
* @subpackage Esi_Management/admin
*/
/**
* The admin-specific functionality of the plugin.
*
* Defines the plugin name, version, and two examples hooks for how to
* enqueue the admin-specific stylesheet and JavaScript.
*
* @package Esi_Management
* @subpackage Esi_Management/admin
* @author Kremer Nicolas <nicolas.kremer@exapartners.com>
*/
if ( ! class_exists ( 'WP_List_Table' ) ) {
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
}
/**
* List table class
*/
class Esi_Management_Ability_List extends \WP_List_Table {
function __construct() {
parent::__construct( array(
'singular' => 'ability',
'plural' => 'ability',
'ajax' => false
) );
}
function get_table_classes() {
return array( 'widefat', 'fixed', 'striped', $this->_args['plural'] );
}
/**
* Message to show if no designation found
*
* @return void
*/
function no_items() {
_e( 'No ability found', 'esi' );
}
/**
* Default column values if no callback found
*
* @param object $item
* @param string $column_name
*
* @return string
*/
function column_default( $item, $column_name ) {
switch ( $column_name ) {
case 'name':
return $item->name;
case 'description':
return $item->description;
default:
return isset( $item->$column_name ) ? $item->$column_name : '';
}
}
/**
* Get the column names
*
* @return array
*/
function get_columns() {
$columns = array(
'cb' => '<input type="checkbox" />',
'name' => __( 'Name', 'esi' ),
'description' => __( 'Description', 'esi' ),
);
return $columns;
}
/**
* Render the designation name column
*
* @param object $item
*
* @return string
*/
function column_name( $item ) {
$actions = array();
$actions['edit'] = sprintf( '<a href="%s" data-id="%d" name="%s">%s</a>', admin_url( 'admin.php?page=esi-management-ability&action=edit&id=' . $item->id ), $item->id, __( 'Edit this item', 'esi' ), __( 'Edit', 'esi' ) );
$actions['delete'] = sprintf( '<a href="%s" class="submitdelete" data-id="%d" name="%s">%s</a>', admin_url( 'admin.php?page=esi-management-ability&action=delete&id=' . $item->id ), $item->id, __( 'Delete this item', 'esi' ), __( 'Delete', 'esi' ) );
return sprintf( '<a href="%1$s"><strong>%2$s</strong></a> %3$s', admin_url( 'admin.php?page=esi-management-ability&action=view&id=' . $item->id ), $item->name, $this->row_actions( $actions ) );
}
/**
* Get sortable columns
*
* @return array
*/
function get_sortable_columns() {
$sortable_columns = array(
'name' => array( 'name', true ),
);
return $sortable_columns;
}
/**
* Set the bulk actions
*
* @return array
*/
function get_bulk_actions() {
$actions = array(
'trash' => __( 'Move to Trash', 'esi' ),
);
return $actions;
}
/**
* Render the checkbox column
*
* @param object $item
*
* @return string
*/
function column_cb( $item ) {
return sprintf(
'<input type="checkbox" name="ability_id[]" value="%d" />', $item->id
);
}
/**
* Set the views
*
* @return array
*/
public function get_views_() {
$status_links = array();
$base_link = admin_url( 'admin.php?page=sample-page' );
foreach ($this->counts as $key => $value) {
$class = ( $key == $this->page_status ) ? 'current' : 'status-' . $key;
$status_links[ $key ] = sprintf( '<a href="%s" class="%s">%s <span class="count">(%s)</span></a>', add_query_arg( array( 'status' => $key ), $base_link ), $class, $value['label'], $value['count'] );
}
return $status_links;
}
/**
* Prepare the class items
*
* @return void
*/
function prepare_items() {
$columns = $this->get_columns();
$hidden = array( );
$sortable = $this->get_sortable_columns();
$this->_column_headers = array( $columns, $hidden, $sortable );
$per_page = 20;
$current_page = $this->get_pagenum();
$offset = ( $current_page -1 ) * $per_page;
$this->page_status = isset( $_GET['status'] ) ? sanitize_text_field( $_GET['status'] ) : '2';
// only ncessary because we have sample data
$args = array(
'offset' => $offset,
'number' => $per_page,
);
if ( isset( $_REQUEST['orderby'] ) && isset( $_REQUEST['order'] ) ) {
$args['orderby'] = $_REQUEST['orderby'];
$args['order'] = $_REQUEST['order'] ;
}
$this->items = esi_get_all_ability( $args );
$this->set_pagination_args( array(
'total_items' => esi_get_ability_count(),
'per_page' => $per_page
) );
}
}
<?php
/**
* Admin Menu
*/
class Esi_Management_Ability {
/**
* The ID of this plugin.
*
* @since 1.0.0
* @access private
* @var string $plugin_name The ID of this plugin.
*/
private $plugin_name;
/**
* The version of this plugin.
*
* @since 1.0.0
* @access private
* @var string $version The current version of this plugin.
*/
private $version;
/**
* Initialize the class and set its properties.
*
* @since 1.0.0
* @param string $plugin_name The name of this plugin.
* @param string $version The version of this plugin.
*/
public function __construct( $plugin_name, $version ) {
$this->plugin_name = $plugin_name;
$this->version = $version;
// add_action( 'admin_menu_ability', array( $this, 'admin_menu' ) );
}
// /**
// * Add menu items
// *
// * @return void
// */
// public function admin_menu() {
//
// /** Top Menu **/
// add_submenu_page( 'esi-management', __( 'ability', 'esi' ), __( 'ability', 'esi' ), 'manage_ability', 'esi-management-ability', array( $this, 'plugin_page' ) );
// }
/**
* Handles the plugin page
*
* @return void
*/
public function plugin_page() {
$action = isset( $_GET['action'] ) ? $_GET['action'] : 'list';
$id = isset( $_GET['id'] ) ? intval( $_GET['id'] ) : 0;
switch ($action) {
case 'view':
case 'edit':
case 'new':
$template = dirname( __FILE__ ) . '/partials/esi-management-ability-form.php';
break;
case 'delete':
esi_delete_ability($id);
$template = dirname( __FILE__ ) . '/partials/esi-management-ability-list.php';
break;
default:
$template = dirname( __FILE__ ) . '/partials/esi-management-ability-list.php';
break;
}
if ( file_exists( $template ) ) {
include $template;
}
}
}
<?php
/**
* Get all ability
*
* @param $args array
*
* @return array
*/
function esi_get_all_ability( $args = array() ) {
global $wpdb;
$defaults = array(
'number' => 20,
'offset' => 0,
'orderby' => 'id',
'order' => 'ASC',
);
$args = wp_parse_args( $args, $defaults );
$cache_key = 'ability-all';
$items = wp_cache_get( $cache_key, 'esi' );
if ( false === $items ) {
$items = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'esi_ability ORDER BY ' . $args['orderby'] .' ' . $args['order'] .' LIMIT ' . $args['offset'] . ', ' . $args['number'] );
wp_cache_set( $cache_key, $items, 'esi' );
}
return $items;
}
/**
* Fetch all ability from database
*
* @return array
*/
function esi_get_ability_count() {
global $wpdb;
return (int) $wpdb->get_var( 'SELECT COUNT(*) FROM ' . $wpdb->prefix . 'esi_ability' );
}
/**
* Fetch a single ability from database
*
* @param int $id
*
* @return array
*/
function esi_get_ability( $id = 0 ) {
global $wpdb;
return $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM ' . $wpdb->prefix . 'esi_ability WHERE id = %d', $id ) );
}
/**
* Insert a ability ability
*
* @param array $args
*/
function esi_insert_ability( $args = array() ) {
global $wpdb;
$defaults = array(
'id' => null,
'name' => '',
'description' => '',
'parent' => '',
);
$args = wp_parse_args( $args, $defaults );
$table_name = $wpdb->prefix . 'esi_ability';
// some basic validation
if ( empty( $args['name'] ) ) {
return new WP_Error( 'no-name', __( 'No Name provided.', 'esi' ) );
}
if ( empty( $args['description'] ) ) {
return new WP_Error( 'no-description', __( 'No Description provided.', 'esi' ) );
}
// remove row id to determine if ability or update
$row_id = (int) $args['id'];
unset( $args['id'] );
if ( ! $row_id ) {
// insert a ability
if ( $wpdb->insert( $table_name, $args ) ) {
return $wpdb->insert_id;
}
} else {
// do update method here
if ( $wpdb->update( $table_name, $args, array( 'id' => $row_id ) ) ) {
return $row_id;
}
}
return false;
}
/**
* Deletitem
*
* @param int $id
*
*/
function esi_delete_ability( $id = 0 ) {
global $wpdb;
$wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'esi_ability WHERE id = %d', $id ) );
}
<?php
/**
* Provide a admin area view for the plugin
*
* This file is used to markup the admin-facing aspects of the plugin.
*
* @link http://www.exapartners.com/
* @since 1.0.0
*
* @package Esi_Management
* @subpackage Esi_Management/admin/partials
*/
global $wpdb;
$item = esi_get_ability( $id );
$ability_list = esi_get_all_ability(array('number' => 1000));
add_thickbox();
?>
<div class="wrap">
<h1><?php _e( 'Add Ability', 'esi' ); ?></h1>
<form action="" method="post">
<table class="form-table">
<tbody>
<tr class="row-name">
<th scope="row">
<label for="name"><?php _e( 'Name', 'esi' ); ?></label>
</th>
<td>
<input type="text" name="name" id="name" class="regular-text" placeholder="<?php echo esc_attr( 'Add ability', 'esi' ); ?>" value="<?php echo ($item)?esc_attr( $item->name ):''; ?>" required="required" />
</td>
</tr>
<tr class="row-description">
<th scope="row">
<label for="description"><?php _e( 'Description', 'esi' ); ?></label>
</th>
<td>
<textarea name="description" id="description"placeholder="<?php echo esc_attr( '', 'esi' ); ?>" rows="5" cols="30" required="required"><?php echo ($item)?esc_attr( $item->description ):''; ?></textarea>
</td>
</tr>
<tr class="row-technology-id">
<th scope="row">
<label for="parent"><?php _e( 'Parent', 'esi' ); ?></label>
</th>
<td>
<select id="parent" class="chosen-select" name="parent">
<?php foreach ($ability_list as $ability) : ?>
<option value="<?= $ability->id ?>" <?= ($ability->id == $item->parent) ? "selected='selected'" : ""?>><?= $ability->name ?></option>
<?php endforeach; ?>
</select><br />
</td>
</tr>
</tbody>
</table>
<input type="hidden" name="field_id" value="<?php echo ($item)?$item->id:0; ?>">
<?php wp_nonce_field( '' ); ?>
<?php if ($_GET['action'] == 'view'): ?>
<?php submit_button( __( 'Update ability', 'esi' ), 'primary', 'submit ability' ); ?>
<?php else: ?>
<?php submit_button( __( 'Add ability', 'esi' ), 'primary', 'submit ability' ); ?>
<?php endif; ?>
</form>
</div>
<?php
/**
* Provide a admin area view for the plugin
*
* This file is used to markup the admin-facing aspects of the plugin.
*
* @link http://www.exapartners.com/
* @since 1.0.0
*
* @package Esi_Management
* @subpackage Esi_Management/admin/partials
*/
?>
<!-- This file should primarily consist of HTML with a little bit of PHP. -->
<div class="wrap">
<h2><?php _e( 'Ability List', 'esi' ); ?> <a href="<?php echo admin_url( 'admin.php?page=esi-management-ability&action=new' ); ?>" class="add-new-h2"><?php _e( 'Add New', 'esi' ); ?></a></h2>
<form method="post">
<input type="hidden" name="page" value="ttest_list_table">
<?php
$list_table = new Esi_Management_Ability_List();
$list_table->prepare_items();
$list_table->search_box( 'search', 'search_id' );
$list_table->display();
?>
</form>
</div>
......@@ -120,6 +120,7 @@ class Esi_Management {
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/modules/investment/functions-investment.php';
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/modules/investment-type/functions-investment-type.php';
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/modules/region/functions-region.php';
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/modules/ability/functions-ability.php';
// Default
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-esi-management-admin.php';
......@@ -164,6 +165,11 @@ class Esi_Management {
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/modules/region/class-esi-management-region-list.php';
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/modules/region/class-esi-management-region-form.php';
// Ability
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/modules/ability/class-esi-management-ability.php';
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/modules/ability/class-esi-management-ability-list.php';
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/modules/ability/class-esi-management-ability-form.php';
/**
* The class responsible for defining all actions that occur in the public-facing
* side of the site.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment