From 09b1139d10d22286308dede096bad2e134de9939 Mon Sep 17 00:00:00 2001 From: Laury GvR Date: Fri, 12 Jul 2019 12:12:34 -0400 Subject: [PATCH] Preliminary summary retrieval for dashboard --- defines.php | 1 + routes/dashboard.php | 349 +++++++++++++++++++++++++++++++++++++++++++ routes/members.php | 148 ++++++++++++++++++ setup/routes.php | 129 +--------------- 4 files changed, 501 insertions(+), 126 deletions(-) create mode 100644 routes/dashboard.php create mode 100644 routes/members.php diff --git a/defines.php b/defines.php index a3b1c5e..38fdd2b 100644 --- a/defines.php +++ b/defines.php @@ -62,6 +62,7 @@ define('GLM_MEMBERS_REST_API_PLUGIN_SETUP_PATH', GLM_MEMBERS_REST_API_PLUGIN_PAT define('GLM_MEMBERS_REST_API_PLUGIN_DB_SCRIPTS', GLM_MEMBERS_REST_API_PLUGIN_SETUP_PATH.'/databaseScripts'); define('GLM_MEMBERS_REST_API_PLUGIN_CLASS_PATH', GLM_MEMBERS_REST_API_PLUGIN_PATH.'/classes'); define('GLM_MEMBERS_REST_API_PLUGIN_CONFIG_PATH', GLM_MEMBERS_REST_API_PLUGIN_PATH.'/config'); +define('GLM_MEMBERS_REST_API_PLUGIN_ROUTES_PATH', GLM_MEMBERS_REST_API_PLUGIN_PATH.'/routes'); // Parameters related to the Main GLM Member DB plugin - Depending on what's going on these may already defined by the main plugin $pluginsPath = str_replace(GLM_MEMBERS_REST_API_PLUGIN_SLUG, '', GLM_MEMBERS_REST_API_PLUGIN_PATH); diff --git a/routes/dashboard.php b/routes/dashboard.php new file mode 100644 index 0000000..25c292c --- /dev/null +++ b/routes/dashboard.php @@ -0,0 +1,349 @@ + + * @license http://www.gaslightmedia.com Gaslightmedia + * @release adminHooks.php,v 1.0 2014/10/31 19:31:47 cscott Exp $ + * @link http://dev.gaslightmedia.com/ + */ + +/** + * Require the Contact Data Class. + */ +require_once GLM_MEMBERS_PLUGIN_CLASS_PATH.'/data/dataMemberInfo.php'; + +require_once GLM_MEMBERS_EVENTS_PLUGIN_CLASS_PATH.'/data/dataEvents.php'; + +/** + * Documentation for Wordpress API and adding custom endpoints. + * https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/ + */ +class GLMA_Dashboard_Rest_Controller +{ + /** + * __construct + * + * Class Constructor + * + * @param $wpdb Wordpress wpdb Database Object + * @param $config GlmAssociate config array + */ + public function __construct( $wpdb, $config ) + { + $this->namespace = 'glm/v1/'; + $this->resource_name = 'dashboard'; + + // Setup wpdb and config + $this->wpdb = $wpdb; + $this->config = $config; + } + + /** + * register_routes + * + * Registers the routes for this class. + * + * @return void + */ + public function register_routes() + { + // Members Route + register_rest_route( + $this->namespace, // namespace + $this->resource_name , // resource_name + array( // array of arrays for multiple methods. Add more arrays for different endpoints. + array( // endpoint for getting members + 'methods' => WP_REST_Server::READABLE, + 'callback' => function( $request ) { + // Callback function can be a lot shorter. + // You could just create a new model to get the results in an array + // and use return rest_ensure_response( $data ); + + // $memberData = new GlmDataMemberInfo( $this->wpdb, $this->config ); + + // $where = " + // ( + // SELECT access + // FROM ".GLM_MEMBERS_PLUGIN_DB_PREFIX."members + // WHERE id = T.member + // ) IN ( + // ".$this->config['access_numb']['NoAccess'].", + // ".$this->config['access_numb']['Moderated'].", + // ".$this->config['access_numb']['Full']." + // ) + // AND T.id < 200 + // AND T.lat <> 0 + // AND T.lon <> 0 + // AND T.status = ".$this->config['status_numb']['Active']; + + // // Get List of Members + // $members = $memberData->getList( $where, 'member_name'); + + $where = + " WHERE access + IN ( + ".$this->config['access_numb']['NoAccess'].", + ".$this->config['access_numb']['Moderated'].", + ".$this->config['access_numb']['Full']." + )"; + $sql = + "SELECT * + FROM ".GLM_MEMBERS_PLUGIN_DB_PREFIX."members + ".$where.";"; + //$members = get_members_summary(); // Call to undefined function get_members_summar() + $members = $this->wpdb->get_results( $sql ); + + + $summary[] = "test"; + $summary[] = $members; + + $events = $this->get_events_summary(); + $summary[] = $events; + $summary[] = $this->get_coupons_summary(); + $summary[] = "packages str"; + + return rest_ensure_response( array_values( $summary ) ); + }, + ) + ) + ); + + // Member Route + register_rest_route( + $this->namespace, + $this->namespace . '/(?P[\d]+)', + array( + array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => function( $request ){ + $params = $request->get_params(); + }, + ) + ) + ); + } + + public function get_collection_params(){ + return array( + 'page' => array( + 'description' => 'Current page of the collection', + 'type' => 'integer', + 'default' => 1, + 'sanitize_callback' => 'absint', + ), + 'per_page' => array( + 'description' => 'Maximum number of items to be returned in result set.', + 'type' => 'integer', + 'default' => 10, + 'sanitize_callback' => 'absint', + ), + 'search' => array( + 'description' => 'Limit results to those matching a string', + 'type' => 'string', + 'sanitize_callback' => 'sanitize_text_field', + ), + ); + } + + public function authorization_status_code(){ + $status = 401; + + if ( is_user_logged_in() ) { + $status = 403; + } + return $status; + } + + // Get members + //public function getList( $where = '', $order = '', $fieldVals = true, $idField = 'id', $start = false, $limit = false, $prohibitListOptions = false, $appendSelect = null ) + public function get_members_summary() { + + + $sql = + "SELECT * + FROM ".GLM_MEMBERS_PLUGIN_DB_PREFIX."members"; + + $members = $this->wpdb->get_results( $sql ); + + return $members; + // $this->selectGroupFields(); + + // // For testing only + // /* + // echo "

getList():
+ // + // + // + // + // + // + // + // + // + //
Where:$where
Order:$order
Field Vals:$fieldVals
ID Field:$idField
Start:$start
Limit:$limit
Prohibit List Options$prohibitListOptions
Append Select$appendSelect
+ //

+ // "; + // */ + + // $seed = false; + + // // NOTE: $fieldVals not yet implemented + + // // Check if we're asked to block all list field options. Only enable this, the parameter does not permit turning it off + // if ($prohibitListOptions) { + // $this->$optionProhibitListOptions = true; + // } + + // // Get field specifications for this instance + // $this->buildFieldsList('l'); + + // // If pseudo random order is requested + // $this->pseudoRand = false; + // if ($order == 'pseudo-random') { + + // // Set pseudo random flag and clear the order string + // $this->pseudoRand = true; + // $order = ''; + + // } + + // // If random order is requested + // $this->rand = false; + // if ($order == 'random') { + + // // Set rand and pseudoRand flags and clear the order string + // // Note that random uses pseudo-random code but overrides the seed each time + // $this->rand = true; + // $this->pseudoRand = true; + // $order = ''; + + // // Generate a new seed each time - this causes full random ordering and overrides "pseudo-random" + // $seed = intval(time()*1000000+microtime()*1000000); + + // } + + // // If doing pseudo-random or random ordering, generate array of IDs in pseudo-random order + // $idString = ''; + // if ($this->pseudoRand || $this->rand) { + + // $prSql = "SELECT $idField + // FROM $this->table T + // "; + // if (trim($where) != '') { + // $prSql .= "WHERE $where + // "; + // } + + // $idList = $this->wpdb->get_results($prSql, ARRAY_A); + // $prList = $this->genPseudoRandIdArray($idList, $start, $limit, 'id', 'GLM_PR_SEED', $seed); + // $idString = $prList['idString']; + + // } + + // if ( $appendSelect ) { + // $this->select .= ', ' . $appendSelect; + // } + + // $sql = "SELECT $this->select + // FROM $this->table T + // WHERE true + // "; + // if (trim($where != '')) { + // $sql .= "AND $where + // "; + // } + + // // If there's a pseudo-random id list to use for results selection + // if ($idString != '') { + // $sql .= "AND T.$idField IN ($idString) + // "; + // } + + // if (!$this->pseudoRand && trim($order != '')) { + // $sql .= "ORDER BY $order + // "; + // } + + // // If $start and $limit, we're doing paging (pseudo-random does it's own paging) + // $paging = false; + // if ($start !== false && $limit > 0) { + + // // If using pseudo-random, don't use the SQL limit. The pseudo-random code does it's own paging + // if (!$this->pseudoRand) { + // $sql .= "limit ".($start-1).", $limit"; + // } + + // $paging = true; + // } + + // $list = $this->wpdb->get_results($sql, ARRAY_A); + + // if (count($list) == 0) { + // return false; + // } + + // // Setup result array + // $newList = array(); + + // // Process field output data for each result - use ID field as key + // foreach ( $list as $k => $v ) { + // $newList[$v['id']] = $this->processOutputData($v, 'l'); + // } + + // // If pseudo-random, sort the list by $prList + // if ($this->pseudoRand) { + // $this->pseudoRandDataSort($prList, $newList); + // } + + // // If we're doing paging, return that data along with the list + // if ($paging) { + // $c = count($list); + // return array( + // 'start' => $start, + // 'limit' => $limit, + // 'returned' => $c, + // 'last' => $start + count($list) - 1, + // 'list' => $newList + // ); + // } + + // $this->restoreFields(); + // // Since we're not doing paging, return the list only + // return $newList; + } + + // Get events + public function get_events_summary() { + return "events summary"; + } + + // Get packages + public function get_packages_summary() { + return "packages summary"; + } + + // Get coupons + public function get_coupons_summary() { + return "coupons summary"; + } + +} + +/** + * Create Action for the REST API Route. + */ +add_action( + 'rest_api_init', + function(){ + $controller = new GLMA_Dashboard_Rest_Controller( $this->wpdb, $this->config ); + $controller->register_routes(); + } +); + + diff --git a/routes/members.php b/routes/members.php new file mode 100644 index 0000000..421bbd2 --- /dev/null +++ b/routes/members.php @@ -0,0 +1,148 @@ + + * @license http://www.gaslightmedia.com Gaslightmedia + * @release adminHooks.php,v 1.0 2014/10/31 19:31:47 cscott Exp $ + * @link http://dev.gaslightmedia.com/ + */ + +/** + * Require the Contact Data Class. + */ +require_once GLM_MEMBERS_PLUGIN_CLASS_PATH.'/data/dataMemberInfo.php'; + +/** + * Documentation for Wordpress API and adding custom endpoints. + * https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/ + */ +class GLMA_Members_Rest_Controller +{ + /** + * __construct + * + * Class Constructor + * + * @param $wpdb Wordpress wpdb Database Object + * @param $config GlmAssociate config array + */ + public function __construct( $wpdb, $config ) + { + $this->namespace = 'glm/v1/'; + $this->resource_name = 'members'; + + // Setup wpdb and config + $this->wpdb = $wpdb; + $this->config = $config; + } + + /** + * register_routes + * + * Registers the routes for this class. + * + * @return void + */ + public function register_routes() + { + // Members Route + register_rest_route( + $this->namespace, // namespace + $this->resource_name , // resource_name + array( // array of arrays for multiple methods. Add more arrays for different endpoints. + array( // endpoint for getting members + 'methods' => WP_REST_Server::READABLE, + 'callback' => function( $request ) { + // Callback function can be a lot shorter. + // You could just create a new model to get the results in an array + // and use return rest_ensure_response( $data ); + $memberData = new GlmDataMemberInfo( $this->wpdb, $this->config ); + + $where = " + ( + SELECT access + FROM ".GLM_MEMBERS_PLUGIN_DB_PREFIX."members + WHERE id = T.member + ) IN ( + ".$this->config['access_numb']['NoAccess'].", + ".$this->config['access_numb']['Moderated'].", + ".$this->config['access_numb']['Full']." + ) + AND T.id < 200 + AND T.lat <> 0 + AND T.lon <> 0 + AND T.status = ".$this->config['status_numb']['Active']; + + // Get List of Members + $members = $memberData->getList( $where, 'member_name'); + + return rest_ensure_response( array_values($members ) ); + }, + ) + ) + ); + + // Member Route + register_rest_route( + $this->namespace, + $this->namespace . '/(?P[\d]+)', + array( + array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => function( $request ){ + $params = $request->get_params(); + }, + ) + ) + ); + } + + public function get_collection_params(){ + return array( + 'page' => array( + 'description' => 'Current page of the collection', + 'type' => 'integer', + 'default' => 1, + 'sanitize_callback' => 'absint', + ), + 'per_page' => array( + 'description' => 'Maximum number of items to be returned in result set.', + 'type' => 'integer', + 'default' => 10, + 'sanitize_callback' => 'absint', + ), + 'search' => array( + 'description' => 'Limit results to those matching a string', + 'type' => 'string', + 'sanitize_callback' => 'sanitize_text_field', + ), + ); + } + + public function authorization_status_code(){ + $status = 401; + + if ( is_user_logged_in() ) { + $status = 403; + } + return $status; + } + +} + +/** + * Create Action for the REST API Route. + */ + add_action( + 'rest_api_init', + function(){ + $controller = new GLMA_Members_Rest_Controller( $this->wpdb, $this->config ); + $controller->register_routes(); + } + ); diff --git a/setup/routes.php b/setup/routes.php index ef7ac87..6d90391 100644 --- a/setup/routes.php +++ b/setup/routes.php @@ -19,130 +19,7 @@ require_once GLM_MEMBERS_PLUGIN_CLASS_PATH.'/data/dataMemberInfo.php'; /** - * Documentation for Wordpress API and adding custom endpoints. - * https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/ + * Require the routes */ -class GLMA_Member_Rest_Controller -{ - /** - * __construct - * - * Class Constructor - * - * @param $wpdb Wordpress wpdb Database Object - * @param $config GlmAssociate config array - */ - public function __construct( $wpdb, $config ) - { - $this->namespace = 'glm/v1/'; - $this->resource_name = 'members'; - - // Setup wpdb and config - $this->wpdb = $wpdb; - $this->config = $config; - } - - /** - * register_routes - * - * Registers the routes for this class. - * - * @return void - */ - public function register_routes() - { - // Members Route - register_rest_route( - $this->namespace, // namespace - $this->resource_name , // resource_name - array( // array of arrays for multiple methods. Add more arrays for different endpoints. - array( // endpoint for getting members - 'methods' => WP_REST_Server::READABLE, - 'callback' => function( $request ) { - // Callback function can be a lot shorter. - // You could just create a new model to get the results in an array - // and use return rest_ensure_response( $data ); - $memberData = new GlmDataMemberInfo( $this->wpdb, $this->config ); - - $where = " - ( - SELECT access - FROM ".GLM_MEMBERS_PLUGIN_DB_PREFIX."members - WHERE id = T.member - ) IN ( - ".$this->config['access_numb']['NoAccess'].", - ".$this->config['access_numb']['Moderated'].", - ".$this->config['access_numb']['Full']." - ) - AND T.id < 200 - AND T.lat <> 0 - AND T.lon <> 0 - AND T.status = ".$this->config['status_numb']['Active']; - - // Get List of Members - $members = $memberData->getList( $where, 'member_name'); - - return rest_ensure_response( array_values($members ) ); - }, - ) - ) - ); - - // Member Route - register_rest_route( - $this->namespace, - $this->namespace . '/(?P[\d]+)', - array( - array( - 'methods' => WP_REST_Server::READABLE, - 'callback' => function( $request ){ - $params = $request->get_params(); - }, - ) - ) - ); - } - - public function get_collection_params(){ - return array( - 'page' => array( - 'description' => 'Current page of the collection', - 'type' => 'integer', - 'default' => 1, - 'sanitize_callback' => 'absint', - ), - 'per_page' => array( - 'description' => 'Maximum number of items to be returned in result set.', - 'type' => 'integer', - 'default' => 10, - 'sanitize_callback' => 'absint', - ), - 'search' => array( - 'description' => 'Limit results to those matching a string', - 'type' => 'string', - 'sanitize_callback' => 'sanitize_text_field', - ), - ); - } - - public function authorization_status_code(){ - $status = 401; - - if ( is_user_logged_in() ) { - $status = 403; - } - return $status; - } - -} - -/** - * Create Action for the REST API Route. - */ - add_action( - 'rest_api_init', - function(){ - $controller = new GLMA_Member_Rest_Controller( $this->wpdb, $this->config ); - $controller->register_routes(); - } - ); +require_once GLM_MEMBERS_REST_API_PLUGIN_ROUTES_PATH.'/dashboard.php'; +require_once GLM_MEMBERS_REST_API_PLUGIN_ROUTES_PATH.'/members.php'; \ No newline at end of file -- 2.17.1