Commit 3486fc4f authored by John Punzalan's avatar John Punzalan

Add Patent

parent 883c6217
......@@ -226,9 +226,12 @@ 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 );
$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' ) );
$plugin_admin_patent = new Esi_Management_Patent( $this->plugin_name, $this->version );
add_submenu_page( $this->plugin_name, __( 'Patent', 'esi' ), __( 'Patent', 'esi' ), 'manage_options', 'esi-management-patent', array( $plugin_admin_patent, 'plugin_page' ) );
}
/**
......
<?php
/**
* Handle the form submissions
*
* @package Package
* @subpackage Sub Package
*/
class Esi_Management_Patent_Form {
/**
* Hook 'em all
*/
public function __construct() {
add_action( 'admin_init', array( $this, 'handle_form' ) );
}
/**
* Handle the new patent and edit form
*
* @return void
*/
public function handle_form($is_ajax = false) {
if ( !isset( $_POST['submit_patent'] ) && !$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-patent' );
$field_id = isset( $_POST['field_id'] ) ? intval( $_POST['field_id'] ) : 0;
$code = isset( $_POST['code'] ) ? sanitize_text_field( $_POST['code'] ) : '';
$description = isset( $_POST['description'] ) ? sanitize_text_field( $_POST['description'] ) : '';
$entity_id = isset( $_POST['entity_id'] ) ? intval( $_POST['entity_id'] ) : '';
// some basic validation
if ( ! $code ) {
$errors[] = __( 'Error: Code is required', 'esi' );
}
if ( ! $entity_id ) {
$errors[] = __( 'Error: Entity 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(
'code' => $code,
'description' => $description,
'entity_id' => $entity_id,
);
// New or edit?
if ( ! $field_id ) {
$insert_id = esi_insert_patent( $fields );
$fields['id'] = $insert_id;
} else {
$fields['id'] = $field_id;
$insert_id = esi_insert_patent( $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_Patent_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_Patent_List extends \WP_List_Table {
function __construct() {
parent::__construct( array(
'singular' => 'patent',
'plural' => 'patent',
'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 patent 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 'code':
return $item->code;
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" />',
'code' => __( 'Code', 'esi' ),
'description' => __( 'Description', 'esi' ),
'entity_id' => __( 'Entity', '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-patent&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-patent&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-patent&action=view&id=' . $item->id ), $item->name, $this->row_actions( $actions ) );
}
/**
* Get sortable columns
*
* @return array
*/
function get_sortable_columns() {
$sortable_columns = array(
'code' => array( 'code', 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="patent_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_patent( $args );
$this->set_pagination_args( array(
'total_items' => esi_get_patent_count(),
'per_page' => $per_page
) );
}
}
<?php
/**
* Admin Menu
*/
class Esi_Management_Patent {
/**
* 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;
}
/**
* 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-patent-form.php';
break;
case 'delete':
esi_delete_patent($id);
$template = dirname( __FILE__ ) . '/partials/esi-management-patent-list.php';
break;
default:
$template = dirname( __FILE__ ) . '/partials/esi-management-patent-list.php';
break;
}
if ( file_exists( $template ) ) {
include $template;
}
}
}
<?php
/**
* Get all patent
*
* @param $args array
*
* @return array
*/
function esi_get_all_patent( $args = array() ) {
global $wpdb;
$defaults = array(
'number' => 20,
'offset' => 0,
'orderby' => 'id',
'order' => 'ASC',
);
$args = wp_parse_args( $args, $defaults );
$cache_key = 'patent-all';
$items = wp_cache_get( $cache_key, 'esi' );
if ( false === $items ) {
$items = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'esi_patent ORDER BY ' . $args['orderby'] .' ' . $args['order'] .' LIMIT ' . $args['offset'] . ', ' . $args['number'] );
wp_cache_set( $cache_key, $items, 'esi' );
}
return $items;
}
/**
* Fetch all patent from database
*
* @return array
*/
function esi_get_patent_count() {
global $wpdb;
return (int) $wpdb->get_var( 'SELECT COUNT(*) FROM ' . $wpdb->prefix . 'esi_patent' );
}
/**
* Fetch a single patent from database
*
* @param int $id
*
* @return array
*/
function esi_get_patent( $id = 0 ) {
global $wpdb;
return $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM ' . $wpdb->prefix . 'esi_patent WHERE id = %d', $id ) );
}
/**
* Insert a patent patent
*
* @param array $args
*/
function esi_insert_patent( $args = array() ) {
global $wpdb;
$defaults = array(
'id' => null,
'entity_id' => '',
'code' => '',
'description' => '',
);
$args = wp_parse_args( $args, $defaults );
$table_name = $wpdb->prefix . 'esi_patent';
// some basic validation
if ( empty( $args['code'] ) ) {
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 patent or update
$row_id = (int) $args['id'];
unset( $args['id'] );
if ( ! $row_id ) {
// insert a patent
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_patent( $id = 0 ) {
global $wpdb;
$wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->prefix . 'esi_patent 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_patent( $id );
$patent_list = esi_get_all_patent(array('number' => 1000));
$entity_list = esi_get_all_entity(array('number' => 1000));
add_thickbox();
?>
<div class="wrap">
<?php if ($_GET['action'] == 'view'): ?>
<h1><?php _e( 'Update Patent', 'esi' ); ?> : <?= $item->name ?></h1>
<?php else: ?>
<h1><?php _e( 'Add Patent', 'esi' ); ?></h1>
<?php endif; ?>
<form action="" method="post">
<table class="form-table">
<tbody>
<tr class="row-code">
<th scope="row">
<label for="code"><?php _e( 'Name', 'esi' ); ?></label>
</th>
<td>
<input type="text" name="code" id="code" class="regular-text" placeholder="<?php echo esc_attr( 'Add patent code', 'esi' ); ?>" value="<?php echo ($item)?esc_attr( $item->code ):''; ?>" 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-entity-id">
<th scope="row">
<label for="entity_id"><?php _e( 'Entity\'s patent assigned' , 'esi' ); ?></label>
</th>
<td>
<select id="entity_id" name="entity_id" class="chosen-select">
<option value="0">Select a entity</option>
<?php foreach ($entity_list as $entity) : ?>
<option value="<?= $entity->id ?>" <?= (isset($item) && $item->entity_id == $entity->id)?'selected="selected"':'' ?>><?= $entity->name ?></option>
<?php endforeach; ?>
</select>
</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 patent', 'esi' ), 'primary', 'submit patent' ); ?>
<?php else: ?>
<?php submit_button( __( 'Add patent', 'esi' ), 'primary', 'submit patent' ); ?>
<?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( 'Patent List', 'esi' ); ?> <a href="<?php echo admin_url( 'admin.php?page=esi-management-patent&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_Patent_List();
$list_table->prepare_items();
$list_table->search_box( 'search', 'search_id' );
$list_table->display();
?>
</form>
</div>
......@@ -121,6 +121,7 @@ class Esi_Management {
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';
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/modules/patent/functions-patent.php';
// Default
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-esi-management-admin.php';
......@@ -170,6 +171,11 @@ class Esi_Management {
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';
// Patent
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/modules/patent/class-esi-management-patent.php';
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/modules/patent/class-esi-management-patent-list.php';
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/modules/patent/class-esi-management-patent-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