Add route for contacts
authorSteve Sutton <steve@gaslightmedia.com>
Thu, 11 Apr 2019 14:15:35 +0000 (10:15 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Thu, 11 Apr 2019 14:15:35 +0000 (10:15 -0400)
For getting contact list.

setup/routes.php [new file with mode: 0644]

diff --git a/setup/routes.php b/setup/routes.php
new file mode 100644 (file)
index 0000000..dcd9d06
--- /dev/null
@@ -0,0 +1,211 @@
+<?php
+/**
+ * Gaslight Media Members Database
+ * GLM Members Misc Routes For API
+ *
+ * PHP version 5.5
+ *
+ * @category glmWordPressPlugin
+ * @package  glmMembersDatabase
+ * @author   Chuck Scott <cscott@gaslightmedia.com>
+ * @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_CONTACTS_PLUGIN_CLASS_PATH.'/data/dataContacts.php';
+
+/**
+ * Documentation for Wordpress API and adding custom endpoints.
+ * https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/
+ */
+
+class glmAssoc_Controller_Contacts
+{
+    /**
+     * __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 = 'contacts';
+
+        // Setup wpdb and config
+        $this->wpdb   = $wpdb;
+        $this->config = $config;
+    }
+
+    /**
+     * register_routes
+     *
+     * Registers the routes for this class.
+     *
+     * @return void
+     */
+    public function register_routes()
+    {
+        register_rest_route(
+            'glm/v1',   // namespace
+            'contacts', // route
+            array(      // array of arrays for multiple methods. Add more arrays for different endpoints.
+                array(  // endpoint for getting contacts
+                    '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 );
+                        $contacts = array();
+                        $Contact = new GlmDataContacts( $this->wpdb, $this->config );
+
+                        $where = " access NOT IN ( 35, 45, 10, 90 )
+                        AND contact_role = 20";
+                        $contactData = $Contact->getList( $where, 'lname' );
+
+                        if ( isset( $contactData ) && is_array( $contactData ) && !empty( $contactData ) ) {
+                            foreach ( $contactData as $contact ) {
+                                // Get cgcs
+                                $cgcs = apply_filters( 'glm_custom_fields', 'CGCS?', $contact['id'] );
+
+                                if ( $contact['contact_publish']['value'] == true ) {
+                                    $fname        = $contact['fname'];
+                                    $lname        = $contact['lname'];
+                                    $addr1        = $contact['addr1'];
+                                    $addr2        = $contact['addr2'];
+                                    $city         = $contact['city'];
+                                    $state        = isset( $contact['state']['value'] ) ? $contact['state']['value']: '';
+                                    $zip          = $contact['zip'];
+                                    $phone        = $contact['home_phone'];
+                                    $mobile_phone = $contact['home_phone'];
+                                    $email        = $contact['alt_email'];
+                                    $office_phone = '';
+                                    $fax          = '';
+                                    $org          = '';
+                                    $title        = '';
+                                } else {
+                                    // $fname        = $contact['business_fname'];
+                                    // $lname        = $contact['business_lname'];
+                                    $fname        = $contact['fname'];
+                                    $lname        = $contact['lname'];
+                                    $addr1        = $contact['business_addr1'];
+                                    $addr2        = $contact['business_addr2'];
+                                    $city         = $contact['business_city'];
+                                    $state        = isset( $contact['business_state']['value'] ) ? $contact['business_state']['value']: '';
+                                    $zip          = $contact['business_zip'];
+                                    $phone        = $contact['business_phone'];
+                                    $mobile_phone = $contact['business_mobile'];
+                                    $email        = $contact['business_email'];
+                                    $office_phone = $contact['office_phone'];
+                                    $fax          = $contact['fax'];
+                                    $org          = $contact['org'];
+                                    $title        = $contact['title'];
+                                }
+                                $contacts[] = array(
+                                    'id'           => $contact['id'],
+                                    'ref_dest'     => $contact['ref_dest'],
+                                    'fname'        => trim( $fname ),
+                                    'lname'        => trim( $lname ),
+                                    'addr1'        => $addr1,
+                                    'addr2'        => $addr2,
+                                    'city'         => $city,
+                                    'state'        => $state,
+                                    'zip'          => $zip,
+                                    'phone'        => trim( $phone ),
+                                    'fax'          => trim( $fax ),
+                                    'email'        => $email,
+                                    'url'          => $url,
+                                    'org'          => $org,
+                                    'title'        => $title,
+                                    'mobile_phone' => trim( $mobile_phone ),
+                                    'office_phone' => trim( $office_phone ),
+                                    'cgcs'         => isset( $cgcs ) && $cgcs == 'Yes' ? true : false,
+                                );
+                            }
+                        }
+
+                        return rest_ensure_response( $contacts );
+                    },
+                    // 'permission_callback' => function(){ // can require capability for this rest endpoint.
+                    //     return current_user_can( 'read' );
+                    // },
+                )
+            )
+        );
+    }
+
+    /**
+     * get_item_schema
+     *
+     * Returns the API schema.
+     *
+     * @param $request API Request
+     *
+     * @return $schema Array with item schema
+     */
+    public function get_item_schema( $request )
+    {
+        $schema = array(
+            '$schema'    => 'http://json-schema.org/draft-04/schema#',
+            'title'      => 'contact',
+            'properties' => array(
+                'id' => array(
+                    'description' => esc_html__( 'Unique identifier for contact' ),
+                    'type'        => 'integer',
+                    'context'     => array( 'view' ),
+                    'readonly'    => true,
+                ),
+                'org' => array(
+                    'descrption' => esc_html__( 'Organization / Company Name' ),
+                    'type'       => 'string',
+                ),
+                'fname' => array(
+                    'descrption' => esc_html__( 'First Name' ),
+                    'type'       => 'string',
+                ),
+                'lname' => array(
+                    'descrption' => esc_html__( 'Last Name' ),
+                    'type'       => 'string',
+                ),
+                'address1' => array(
+                    'description' => esc_html__( 'Address 1' ),
+                    'type'        => 'string',
+                ),
+                'address2' => array(
+                    'description' => esc_html__( 'Address 2' ),
+                    'type'        => 'string',
+                ),
+                'city' => array(
+                    'description' => esc_html__( 'City' ),
+                    'type'        => 'string',
+                ),
+                'state' => array(
+                    'description' => esc_html__( 'State' ),
+                    'type'        => 'string',
+                ),
+                'zip' => array(
+                    'description' => esc_html__( 'Zip' ),
+                    'type'        => 'string',
+                ),
+            ),
+        );
+        return $schema;
+    }
+}
+
+/**
+ * Create Action for the REST API Route.
+ */
+add_action(
+    'rest_api_init',
+    function(){
+        $controller = new glmAssoc_Controller_Contacts( $this->wpdb, $this->config );
+        $controller->register_routes();
+    }
+);