From 719e4492fb533893ef69149bedf3a37e6364d92a Mon Sep 17 00:00:00 2001 From: Salman Z Kaleem Date: Wed, 15 Jul 2015 10:54:07 -0400 Subject: [PATCH 1/5] Add script file, for ACF Loads when ACF input fields are rendered --- uc-people.js | 12 ++++++++++++ uc-people.php | 8 +++++++- 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 uc-people.js diff --git a/uc-people.js b/uc-people.js new file mode 100644 index 0000000..6ebbb80 --- /dev/null +++ b/uc-people.js @@ -0,0 +1,12 @@ +jQuery(document).ready(function($){ + if( $('#acf-field-netid_lookup').length ){ + $('#acf-field-netid_lookup').keyup(function(e){ + if( e.target.value.length > 2 ){ + var netidQuery = e.target.value; + /** + * Ajax lookup with netidQuery + */ + } + }); + } +}); \ No newline at end of file diff --git a/uc-people.php b/uc-people.php index 13b9746..51c98ba 100644 --- a/uc-people.php +++ b/uc-people.php @@ -169,4 +169,10 @@ function uc_people_settings_page(){ Date: Wed, 15 Jul 2015 11:03:25 -0400 Subject: [PATCH 2/5] Need to push --- uc-people.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/uc-people.js b/uc-people.js index 6ebbb80..8315df9 100644 --- a/uc-people.js +++ b/uc-people.js @@ -1,8 +1,9 @@ jQuery(document).ready(function($){ if( $('#acf-field-netid_lookup').length ){ $('#acf-field-netid_lookup').keyup(function(e){ - if( e.target.value.length > 2 ){ + if( e.target.value.length == 8 ){ var netidQuery = e.target.value; + console.log( netidQuery ); /** * Ajax lookup with netidQuery */ From 6386530e99afa996ba610c9cefce55730b3f30a8 Mon Sep 17 00:00:00 2001 From: Salman Z Kaleem Date: Wed, 15 Jul 2015 14:35:03 -0400 Subject: [PATCH 3/5] NetID LDAP Lookup Fills following fields; first/last name, email address, phone number --- acf-export.php | 190 +++++++++++++++++++++++++++++++++++++++++++++++++ uc-people.js | 40 ++++++++--- uc-people.php | 24 +++++++ 3 files changed, 245 insertions(+), 9 deletions(-) create mode 100644 acf-export.php diff --git a/acf-export.php b/acf-export.php new file mode 100644 index 0000000..34ee7a7 --- /dev/null +++ b/acf-export.php @@ -0,0 +1,190 @@ + 'acf_person', + 'title' => 'Person', + 'fields' => array ( + array ( + 'key' => 'field_55a6595eba964', + 'label' => 'First Name', + 'name' => 'first_name', + 'type' => 'text', + 'required' => 1, + 'default_value' => '', + 'placeholder' => '', + 'prepend' => '', + 'append' => '', + 'formatting' => 'html', + 'maxlength' => '', + ), + array ( + 'key' => 'field_55a6596c4a44d', + 'label' => 'Middle Name', + 'name' => 'middle_name', + 'type' => 'text', + 'default_value' => '', + 'placeholder' => '', + 'prepend' => '', + 'append' => '', + 'formatting' => 'html', + 'maxlength' => '', + ), + array ( + 'key' => 'field_55a65973063d2', + 'label' => 'Last Name', + 'name' => 'last_name', + 'type' => 'text', + 'required' => 1, + 'default_value' => '', + 'placeholder' => '', + 'prepend' => '', + 'append' => '', + 'formatting' => 'html', + 'maxlength' => '', + ), + array ( + 'key' => 'field_55a65979063d3', + 'label' => 'Title', + 'name' => 'title', + 'type' => 'text', + 'default_value' => '', + 'placeholder' => '', + 'prepend' => '', + 'append' => '', + 'formatting' => 'html', + 'maxlength' => '', + ), + array ( + 'key' => 'field_55a659a4063d4', + 'label' => 'About', + 'name' => 'about', + 'type' => 'wysiwyg', + 'default_value' => '', + 'toolbar' => 'full', + 'media_upload' => 'yes', + ), + array ( + 'key' => 'field_55a659aa063d5', + 'label' => 'File', + 'name' => 'file', + 'type' => 'file', + 'save_format' => 'object', + 'library' => 'all', + ), + array ( + 'key' => 'field_55a659b1063d6', + 'label' => 'Email', + 'name' => 'email', + 'type' => 'email', + 'default_value' => '', + 'placeholder' => '', + 'prepend' => '', + 'append' => '', + ), + array ( + 'key' => 'field_55a659c4063d7', + 'label' => 'Phone', + 'name' => 'phone', + 'type' => 'text', + 'default_value' => '', + 'placeholder' => '', + 'prepend' => '', + 'append' => '', + 'formatting' => 'html', + 'maxlength' => '', + ), + array ( + 'key' => 'field_55a659ca063d8', + 'label' => 'Phone (alternate)', + 'name' => 'phone_(alternate)', + 'type' => 'text', + 'default_value' => '', + 'placeholder' => '', + 'prepend' => '', + 'append' => '', + 'formatting' => 'html', + 'maxlength' => '', + ), + array ( + 'key' => 'field_55a659d2063d9', + 'label' => 'Fax', + 'name' => 'fax', + 'type' => 'text', + 'default_value' => '', + 'placeholder' => '', + 'prepend' => '', + 'append' => '', + 'formatting' => 'html', + 'maxlength' => '', + ), + array ( + 'key' => 'field_55a659de063da', + 'label' => 'Mailing Address', + 'name' => 'mailing_address', + 'type' => 'text', + 'default_value' => '', + 'placeholder' => '', + 'prepend' => '', + 'append' => '', + 'formatting' => 'html', + 'maxlength' => '', + ), + array ( + 'key' => 'field_55a659ee063db', + 'label' => 'Office Location', + 'name' => 'office_location', + 'type' => 'text', + 'default_value' => '', + 'placeholder' => '', + 'prepend' => '', + 'append' => '', + 'formatting' => 'html', + 'maxlength' => '', + ), + array ( + 'key' => 'field_55a659f7063dc', + 'label' => 'Office Hours', + 'name' => 'office_hours', + 'type' => 'text', + 'default_value' => '', + 'placeholder' => '', + 'prepend' => '', + 'append' => '', + 'formatting' => 'html', + 'maxlength' => '', + ), + array ( + 'key' => 'field_55a659ff063dd', + 'label' => 'Courses', + 'name' => 'courses', + 'type' => 'text', + 'default_value' => '', + 'placeholder' => '', + 'prepend' => '', + 'append' => '', + 'formatting' => 'html', + 'maxlength' => '', + ), + ), + 'location' => array ( + array ( + array ( + 'param' => 'post_type', + 'operator' => '==', + 'value' => 'person', + 'order_no' => 0, + 'group_no' => 0, + ), + ), + ), + 'options' => array ( + 'position' => 'normal', + 'layout' => 'no_box', + 'hide_on_screen' => array ( + ), + ), + 'menu_order' => 0, + )); +} +?> \ No newline at end of file diff --git a/uc-people.js b/uc-people.js index 8315df9..3af157d 100644 --- a/uc-people.js +++ b/uc-people.js @@ -1,13 +1,35 @@ jQuery(document).ready(function($){ - if( $('#acf-field-netid_lookup').length ){ - $('#acf-field-netid_lookup').keyup(function(e){ - if( e.target.value.length == 8 ){ - var netidQuery = e.target.value; - console.log( netidQuery ); - /** - * Ajax lookup with netidQuery - */ + if( $('#uc_people_netid_lookup').length ){ + $('#uc_people_netid_search').click(function(ev){ + ev.preventDefault(); + + var query = $('#uc_people_netid_lookup').val(); + if( query.length == 8 && query.match(/[a-z]{3}[0-9]{5}/) ){ + $.ajax({ + url: ajax_object.ajax_url, + data: { 'action': 'uc_netid_lookup', 'netid': query }, + success: function(response){ + if( !isEmpty(response) ){ + var fields = ['first_name', 'last_name', 'phone', 'email']; + fields.forEach(function(el,i,a){ + if( response.hasOwnProperty(el) ){ + $('#acf-field-'+el).val( response[el] ); + } + }); + } + }, + dataType: 'json' + }); } }); } -}); \ No newline at end of file +}); + +function isEmpty( object ){ + for( var key in object ){ + if( Object.prototype.hasOwnProperty.call(object,key) ){ + return false; + } + } + return true; +} \ No newline at end of file diff --git a/uc-people.php b/uc-people.php index 916391e..9f05b49 100644 --- a/uc-people.php +++ b/uc-people.php @@ -11,6 +11,7 @@ die; } include 'acf-export.php'; +require_once(WP_CONTENT_DIR . '/ldap.php'); function create_custom_custom_post_type(){ $labels = array( @@ -181,9 +182,32 @@ function uc_people_settings_page(){ '; + echo ''; + echo ''; + echo ''; +} + function uc_acf_admin_enqueue_scripts(){ wp_enqueue_script( 'ucpeoplejs', plugin_dir_url(__FILE__).'/uc-people.js', array('jquery'), '1.0' ); } add_action('acf/input/admin_enqueue_scripts', 'uc_acf_admin_enqueue_scripts'); + +add_action( 'wp_ajax_uc_netid_lookup', 'uc_netid_lookup_callback' ); +function uc_netid_lookup_callback(){ + $query = $_GET['netid']; + + $userInfo = netid_lookup($query); + + echo wp_json_encode($userInfo); + + wp_die(); +} + ?> From 86642402459e5cb9db5184a3d08c218417eaa787 Mon Sep 17 00:00:00 2001 From: Salman Z Kaleem Date: Thu, 16 Jul 2015 12:15:17 -0400 Subject: [PATCH 4/5] Added support for email-address lookup Using different LDAP function because I'm validating type of search via javascript --- uc-people.js | 21 +++++++++--- uc-people.php | 92 ++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 94 insertions(+), 19 deletions(-) diff --git a/uc-people.js b/uc-people.js index 3af157d..fe4951c 100644 --- a/uc-people.js +++ b/uc-people.js @@ -1,25 +1,38 @@ jQuery(document).ready(function($){ - if( $('#uc_people_netid_lookup').length ){ - $('#uc_people_netid_search').click(function(ev){ + if( $('#uc_people_lookup').length ){ + $('#uc_people_lookup_search').click(function(ev){ ev.preventDefault(); - var query = $('#uc_people_netid_lookup').val(); + var query = $('#uc_people_lookup').val(); + var type = null; if( query.length == 8 && query.match(/[a-z]{3}[0-9]{5}/) ){ + var type = 'netid'; + } else if( query.match(/uconn.edu/) ){ + var type = 'email'; + } + + if( type != null ){ $.ajax({ url: ajax_object.ajax_url, - data: { 'action': 'uc_netid_lookup', 'netid': query }, + data: { 'action': 'uc_people_lookup', 'query': query, 'type': type }, success: function(response){ if( !isEmpty(response) ){ + $('#uc_people_lookup_metabox p.howto').text("Person found"); var fields = ['first_name', 'last_name', 'phone', 'email']; fields.forEach(function(el,i,a){ + $('#acf-field-'+el).val(""); if( response.hasOwnProperty(el) ){ $('#acf-field-'+el).val( response[el] ); } }); + } else { + $('#uc_people_lookup_metabox p.howto').text("Person not found"); } }, dataType: 'json' }); + } else { + $('#uc_people_lookup_metabox p.howto').text("Invalid e-mail address or NetID entered"); } }); } diff --git a/uc-people.php b/uc-people.php index 9f05b49..1f6fc01 100644 --- a/uc-people.php +++ b/uc-people.php @@ -10,8 +10,9 @@ if ( ! defined( 'WPINC' ) ) { die; } -include 'acf-export.php'; -require_once(WP_CONTENT_DIR . '/ldap.php'); +require 'acf-export.php'; +//require_once(WP_CONTENT_DIR . '/ldap.php'); + function create_custom_custom_post_type(){ $labels = array( @@ -130,7 +131,7 @@ function importFile(){ $title = $v['First Name'].' '.$v['Last Name']; $args = array( 'post_title' => $title, - 'post_name' => 'testing-contact', + 'post_name' => strtolower($v['First Name']).'-'.strtolower($v['Last Name']), 'post_status' => 'publish', 'post_type' => 'person', 'menu_order' => ( !empty($v['Order']) ? $v['Order'] : 0 ), @@ -182,32 +183,93 @@ function uc_people_settings_page(){ '; - echo ''; - echo ''; +add_action( 'add_meta_boxes', 'uc_people_lookup_metabox' ); +function uc_people_lookup_metabox_callback(){ + echo '
'; + echo '

'; + echo '

'; + echo '

'; echo '
'; } + function uc_acf_admin_enqueue_scripts(){ wp_enqueue_script( 'ucpeoplejs', plugin_dir_url(__FILE__).'/uc-people.js', array('jquery'), '1.0' ); + wp_localize_script( 'ucpeoplejs', 'ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) ); } add_action('acf/input/admin_enqueue_scripts', 'uc_acf_admin_enqueue_scripts'); -add_action( 'wp_ajax_uc_netid_lookup', 'uc_netid_lookup_callback' ); -function uc_netid_lookup_callback(){ - $query = $_GET['netid']; +if( !function_exists('uc_people_lookup') ){ + function uc_people_lookup( $query, $type ){ + if( is_null($query) || is_null($type) ){ + return false; + } + //LDAP Config + $ldapdn = 'uid=web-aurora,ou=accounts,ou=ldap,dc=uconn,dc=edu'; + $ldappass = 'nyZqY6evmFBft2oS5fJDReeIa1W36CXViEPZCXpTRyD5IuiM2GCdeiDOlNJQOiu0'; + + $ldaphost = 'ldaps://ldap.uconn.edu:636'; + $ldapconn = ldap_connect($ldaphost) or die('Can\'t contact LDAP'); + + if($ldapconn){ + ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3); + $ldapbind = ldap_bind($ldapconn, $ldapdn, $ldappass) or die('Can\'t bind LDAP'); + + $search = ''; + if( $type == 'netid' ){ + $field = 'uid'; + } else if( $type == 'email' ){ + $field = 'mail'; + } + $search = '('.$field.'='.$query.')'; + + $ldap_fields = array('uid', 'title','cn', 'telephoneNumber', 'postalAddress', 'givenName', 'sn','mail','uconnpersonaffiliation','l','ou'); + $results = ldap_search($ldapconn, 'dc=uconn,dc=edu', $search, $ldap_fields); + if($results == false){ + return; + } + + $var = ldap_get_entries($ldapconn, $results); + $ldap_keys = array('title' ,'cn', 'telephonenumber', 'postaladdress', 'givenname', 'sn','mail','uconnpersonaffiliation','l','ou'); + $meta_keys = array('title', 'name', 'phone', 'address', 'first_name', 'last_name','email','affiliation','campus','department'); + $fields = array_combine($ldap_keys, $meta_keys); + $user_data = array(); + foreach( $var as $k=>$result ){ + if( is_int($k) ){ + foreach ($fields as $ldap_key=>$meta_key){ + if( isset($result) && isset($result[$ldap_key]) && isset($result[$ldap_key][0]) ){ + $count = $result[$ldap_key]['count'] - 1; + if( $count > 0 ){ + for ($i=0; $i <= $count; $i++) { + $user_data[$meta_key][$i] = $result[$ldap_key][$i]; + } + } else if( $count == 0 ) { + $user_data[$meta_key] = $result[$ldap_key][0]; + } + } + } + } + } + return $user_data; + } + ldap_close($ldapconn); + } +} + +add_action( 'wp_ajax_uc_people_lookup', 'uc_people_lookup_callback' ); +function uc_people_lookup_callback(){ + $query = $_GET['query']; + $type = $_GET['type']; - $userInfo = netid_lookup($query); + $userInfo = uc_people_lookup($query, $type); echo wp_json_encode($userInfo); wp_die(); } -?> +?> \ No newline at end of file From 823596f8f8420bf85e9ca2d6bff32fa30cacf9f5 Mon Sep 17 00:00:00 2001 From: Salman Z Kaleem Date: Thu, 16 Jul 2015 14:59:47 -0400 Subject: [PATCH 5/5] Use LDAP lookup in wp-content dir Also renames phone # array key to match UC-People's phone # label --- uc-people.js | 11 ++------- uc-people.php | 64 ++++----------------------------------------------- 2 files changed, 6 insertions(+), 69 deletions(-) diff --git a/uc-people.js b/uc-people.js index fe4951c..1ceb6e6 100644 --- a/uc-people.js +++ b/uc-people.js @@ -4,17 +4,10 @@ jQuery(document).ready(function($){ ev.preventDefault(); var query = $('#uc_people_lookup').val(); - var type = null; - if( query.length == 8 && query.match(/[a-z]{3}[0-9]{5}/) ){ - var type = 'netid'; - } else if( query.match(/uconn.edu/) ){ - var type = 'email'; - } - - if( type != null ){ + if( (query.length == 8 && query.match(/[a-z]{3}[0-9]{5}/)) || query.match(/uconn.edu/) ){ $.ajax({ url: ajax_object.ajax_url, - data: { 'action': 'uc_people_lookup', 'query': query, 'type': type }, + data: { 'action': 'uc_people_lookup', 'query': query }, success: function(response){ if( !isEmpty(response) ){ $('#uc_people_lookup_metabox p.howto').text("Person found"); diff --git a/uc-people.php b/uc-people.php index 1f6fc01..e0171ea 100644 --- a/uc-people.php +++ b/uc-people.php @@ -11,7 +11,7 @@ die; } require 'acf-export.php'; -//require_once(WP_CONTENT_DIR . '/ldap.php'); +require_once(WP_CONTENT_DIR . '/ldap.php'); function create_custom_custom_post_type(){ @@ -203,69 +203,13 @@ function uc_acf_admin_enqueue_scripts(){ add_action('acf/input/admin_enqueue_scripts', 'uc_acf_admin_enqueue_scripts'); -if( !function_exists('uc_people_lookup') ){ - function uc_people_lookup( $query, $type ){ - if( is_null($query) || is_null($type) ){ - return false; - } - //LDAP Config - $ldapdn = 'uid=web-aurora,ou=accounts,ou=ldap,dc=uconn,dc=edu'; - $ldappass = 'nyZqY6evmFBft2oS5fJDReeIa1W36CXViEPZCXpTRyD5IuiM2GCdeiDOlNJQOiu0'; - - $ldaphost = 'ldaps://ldap.uconn.edu:636'; - $ldapconn = ldap_connect($ldaphost) or die('Can\'t contact LDAP'); - - if($ldapconn){ - ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3); - $ldapbind = ldap_bind($ldapconn, $ldapdn, $ldappass) or die('Can\'t bind LDAP'); - - $search = ''; - if( $type == 'netid' ){ - $field = 'uid'; - } else if( $type == 'email' ){ - $field = 'mail'; - } - $search = '('.$field.'='.$query.')'; - - $ldap_fields = array('uid', 'title','cn', 'telephoneNumber', 'postalAddress', 'givenName', 'sn','mail','uconnpersonaffiliation','l','ou'); - $results = ldap_search($ldapconn, 'dc=uconn,dc=edu', $search, $ldap_fields); - if($results == false){ - return; - } - - $var = ldap_get_entries($ldapconn, $results); - $ldap_keys = array('title' ,'cn', 'telephonenumber', 'postaladdress', 'givenname', 'sn','mail','uconnpersonaffiliation','l','ou'); - $meta_keys = array('title', 'name', 'phone', 'address', 'first_name', 'last_name','email','affiliation','campus','department'); - $fields = array_combine($ldap_keys, $meta_keys); - $user_data = array(); - foreach( $var as $k=>$result ){ - if( is_int($k) ){ - foreach ($fields as $ldap_key=>$meta_key){ - if( isset($result) && isset($result[$ldap_key]) && isset($result[$ldap_key][0]) ){ - $count = $result[$ldap_key]['count'] - 1; - if( $count > 0 ){ - for ($i=0; $i <= $count; $i++) { - $user_data[$meta_key][$i] = $result[$ldap_key][$i]; - } - } else if( $count == 0 ) { - $user_data[$meta_key] = $result[$ldap_key][0]; - } - } - } - } - } - return $user_data; - } - ldap_close($ldapconn); - } -} - add_action( 'wp_ajax_uc_people_lookup', 'uc_people_lookup_callback' ); function uc_people_lookup_callback(){ $query = $_GET['query']; - $type = $_GET['type']; - $userInfo = uc_people_lookup($query, $type); + $userInfo = ldap_lookup($query); + $userInfo['phone'] = $userInfo['uup-phone1']; + unset($userInfo['uup-phone1']); echo wp_json_encode($userInfo);