From 75bd2f4ce59e57e3bf6555c7a857243bf244d2a5 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Fri, 29 Jun 2018 16:43:28 -0400 Subject: [PATCH] Adding contact export This will also have the custom fields in the export. --- models/admin/ajax/contactsListExport.php | 265 ++++++++++++++++++++ models/admin/contacts/index.php | 11 +- setup/validActions.php | 15 +- views/admin/ajax/contactsListExport.html | 72 ++++++ views/admin/ajax/contactsListExportCsv.html | 49 ++++ views/admin/contacts/index.html | 113 +++++++++ 6 files changed, 516 insertions(+), 9 deletions(-) create mode 100644 models/admin/ajax/contactsListExport.php create mode 100644 views/admin/ajax/contactsListExport.html create mode 100644 views/admin/ajax/contactsListExportCsv.html diff --git a/models/admin/ajax/contactsListExport.php b/models/admin/ajax/contactsListExport.php new file mode 100644 index 0000000..e27d2f8 --- /dev/null +++ b/models/admin/ajax/contactsListExport.php @@ -0,0 +1,265 @@ + + * @license http://www.gaslightmedia.com Gaslightmedia + * @version 0.1 + */ + + +// Load Member Info data abstract +require_once GLM_MEMBERS_CONTACTS_PLUGIN_CLASS_PATH.'/data/dataContacts.php'; + +/** + * + * This class exports the currently selected contacts list + * to a printable HTML file, to a CSV file, or otherwise. + */ +class GlmMembersAdmin_ajax_contactsListExport extends GlmDataContacts +{ + + /** + * WordPress Database Object + * + * @var $wpdb + * @access public + */ + public $wpdb; + /** + * Plugin Configuration Data + * + * @var $config + * @access public + */ + public $config; + + /** + * Constructor + * + * This constructor sets up this model. At this time that only includes + * storing away the WordPress data object. + * + * @return object Class object + * + */ + public function __construct ( $wpdb, $config ) + { + + // Save WordPress Database object + $this->wpdb = $wpdb; + + // Save plugin configuration object + $this->config = $config; + + parent::__construct( false, false ); + + } + + public function checkFlag( $t ) { return isset( $_REQUEST[$t] ) && $_REQUEST[$t] == 'on'; } + /** + * Perform Model Action + * + * This modelAction takes an AJAX image upload and stores the image in the + * media/images directory of the plugin. + * + * This model action does not return, it simply does it's work then calls die(); + * + * @param $actionData + * + * Echos JSON string as response and does not return + */ + public function modelAction ( $actionData = false ) + { + + // When processing member info records, also get primary contact data + $this->postProcessPrimaryContact = true; + + $where = ' true '; + $haveMembers = false; + $list = false; + $success = false; + $haveFilter = false; + $numbDisplayed = false; + $lastDisplayed = false; + $paging = true; + $prevStart = false; + $nextStart = false; + $start = 1; + $limit = 20; // Set to the number of listings per page + $namesList = false; + $CFSelect = false; + $CFHeaders = false; + + // Check selected fields + $select = array( + 'exportId' => $this->checkFlag( 'exportId' ), + 'exportFname' => $this->checkFlag( 'exportFname' ), + 'exportLname' => $this->checkFlag( 'exportLname' ), + 'exportAddr1' => $this->checkFlag( 'exportAddr1' ), + 'exportAddr2' => $this->checkFlag( 'exportAddr2' ), + 'exportCity' => $this->checkFlag( 'exportCity' ), + 'exportCounty' => $this->checkFlag( 'exportCounty' ), + 'exportState' => $this->checkFlag( 'exportState' ), + 'exportZip' => $this->checkFlag( 'exportZip' ), + 'exportCountry' => $this->checkFlag( 'exportCountry' ), + 'exportOrg' => $this->checkFlag( 'exportOrg' ), + 'exportTitle' => $this->checkFlag( 'exportTitle' ), + 'exportOfficePhone' => $this->checkFlag( 'exportOfficePhone' ), + 'exportHomePhone' => $this->checkFlag( 'exportHomePhone' ), + 'exportMobilePhone' => $this->checkFlag( 'exportMobilePhone' ), + 'exportAltPhone' => $this->checkFlag( 'exportAltPhone' ), + 'exportFax' => $this->checkFlag( 'exportFax' ), + 'exportEmail' => $this->checkFlag( 'exportEmail' ), + 'exportAltEmail' => $this->checkFlag( 'exportAltEmail' ), + 'exportMemberType' => $this->checkFlag( 'exportMemberType' ), + ); + + + // echo '
$_REQUEST: ' . print_r( $_REQUEST, true ) . '
'; + + // Check for selection of Custom Fields + $fieldPluginActive = apply_filters( 'glm-members-customfields-active', false ); + if ( $fieldPluginActive ) { + $customFields = apply_filters( 'glm-member-db-fields-get-members-fields', false, 'glm-member-db-contacts' ); + if ( $customFields ) { + foreach ( $customFields as $cf ) { + if ( isset( $_REQUEST['exportCF'][$cf['id']] ) ) { + $CFSelect[] = $cf['id']; + $CFHeaders[] = $cf['field_name']; + } + } + } + } + + // Only return information records that are active + + // Check if there is a member_type filter + if ( isset( $_REQUEST['filterMemberTypes'] ) && $memberTypeFilter = filter_var( $_REQUEST['filterMemberTypes'], FILTER_VALIDATE_INT ) ) { + $where .= " AND T.ref_dest IN ( + SELECT DISTINCT(id) + FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "members + WHERE member_type = $memberTypeFilter + )"; + $mTypeSelected = $memberTypeFilter; + } + + // Check for a text search + if ( trim( $_REQUEST['text_search'] ) != '' ) { + $textSearch = addslashes( filter_var( $_REQUEST['text_search'], FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES ) ); + $filterText = esc_sql($textSearch); + $where .= " AND ( + lname LIKE '%$filterText%' OR + fname LIKE '%$filterText%' OR + org LIKE '%$filterText%' OR + descr LIKE '%$filterText%' + )"; + } + + // Get a current list of contacts without paging + $list = $this->getList( $where, false, 'lname, fname' ); + + // echo '
$list: ' . print_r( $list, true ) . '
'; + // exit; + + // If we have list entries - even if it's an empty list + $success = true; + $haveMembers = false; + if ( $list !== false ) { + + $success = true; + + // If we have any entries + $memberCount = count($list); + if ( $memberCount > 0 ) { + + $haveMembers = true; + + if ( $select['exportMemberType'] ) { + foreach( $list as $k => $v ) { + // Get each contacts members member type + $list[$k]['member_type'] = $this->wpdb->get_var( + $this->wpdb->prepare( + "SELECT name + FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "member_type + WHERE id IN ( + SELECT member_type + FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "members + WHERE id = %d + )", + $v['ref_dest'] + ) + ); + } + } + + // If Custom Fields are selected + if ( $CFSelect && !empty( $CFSelect ) ) { + + foreach( $list as $k => $v ) { + foreach ( $CFSelect as $cf_key => $cf_id ) { + $list[$k][$CFHeaders[$cf_key]] = apply_filters( 'glm_custom_fields_by_entity_id', $CFHeaders[$cf_key], $v['id'] ); + } + } + + } + + } + } + + // Compile template data + $templateData = array( + 'select' => $select, + 'haveMembers' => $haveMembers, + 'contacts' => $list, + 'memberCount' => $memberCount, + 'categories' => $categories, + 'haveFilter' => $haveFilter, + 'filterArchived' => $filterArchived, + 'filterFeatured' => $filterFeatured, + 'filterPending' => $filterPending, + 'numbDisplayed' => $numbDisplayed, + 'lastDisplayed' => $lastDisplayed, + 'paging' => $paging, + 'prevStart' => $prevStart, + 'nextStart' => $nextStart, + 'start' => $start, + 'limit' => $limit, + 'namesList' => $namesList, + 'CFSelect' => $CFSelect, + 'CFHeaders' => $CFHeaders, + ); + + $view = 'admin/ajax/contactsListExport.html'; + if ( $_REQUEST['type'] == 'csv' ) { + $view = 'admin/ajax/contactsListExportCsv.html'; + header("Content-Type: text/csv"); + header("Content-Disposition: attachment; filename=file.csv"); + } else { + header("Content-Type: text/html"); + header("Content-Disposition: attachment; filename=file.html"); + } + + // Disable caching + header( 'Cache-Control: no-cache, no-store, must-revalidate' ); // HTTP 1.1 + header( 'Pragma: no-cache' ); // HTTP 1.0 + header( 'Expires: 0' ); // Proxies + + // Return status, suggested view, and data to controller + return array( + 'status' => $success, + 'menuItemRedirect' => false, + 'modelRedirect' => false, + 'view' => $view, + 'data' => $templateData + ); + + } + +} diff --git a/models/admin/contacts/index.php b/models/admin/contacts/index.php index 730bfc4..71129b6 100644 --- a/models/admin/contacts/index.php +++ b/models/admin/contacts/index.php @@ -137,7 +137,8 @@ class GlmMembersAdmin_contacts_index extends GlmDataContacts $start = 1; $limit = 20; // Set to the number of listings per page $namesList = false; - $customFieldResults = false; + $customFieldResults = false; + $member_types = false; $where = ''; @@ -150,6 +151,11 @@ class GlmMembersAdmin_contacts_index extends GlmDataContacts 'list' ); + // Get a list of member_types for filtering + require_once GLM_MEMBERS_PLUGIN_CLASS_PATH . '/data/dataMemberTypes.php'; + $MemberTypes = new GlmDataMemberTypes( $this->wpdb, $this->config ); + $member_types = $MemberTypes->getList(); + // If this is a logged in member user, then show their contacts only if (isset($this->config['loggedInUser']['contactUser'])) { @@ -769,7 +775,8 @@ class GlmMembersAdmin_contacts_index extends GlmDataContacts 'namesList' => $namesList, 'EntityManagerRole' => $this->config['contact_role_numb']['EntityManager'], 'slug' => GLM_MEMBERS_CONTACTS_PLUGIN_SLUG, - 'customFieldResults' => $customFieldResults + 'customFieldResults' => $customFieldResults, + 'member_types' => $member_types, ); diff --git a/setup/validActions.php b/setup/validActions.php index a349b85..7755cfd 100644 --- a/setup/validActions.php +++ b/setup/validActions.php @@ -33,25 +33,26 @@ $glmMembersContactsAddOnValidActions = array( 'adminActions' => array( 'ajax' => array( - 'selectContactMember' => GLM_MEMBERS_CONTACTS_PLUGIN_SLUG + 'selectContactMember' => GLM_MEMBERS_CONTACTS_PLUGIN_SLUG, + 'contactsListExport' => GLM_MEMBERS_CONTACTS_PLUGIN_SLUG ), 'members' => array( - 'contacts' => GLM_MEMBERS_CONTACTS_PLUGIN_SLUG + 'contacts' => GLM_MEMBERS_CONTACTS_PLUGIN_SLUG, ), 'member' => array( - 'contacts' => GLM_MEMBERS_CONTACTS_PLUGIN_SLUG + 'contacts' => GLM_MEMBERS_CONTACTS_PLUGIN_SLUG, ), 'profile' => array( - 'index' => GLM_MEMBERS_CONTACTS_PLUGIN_SLUG + 'index' => GLM_MEMBERS_CONTACTS_PLUGIN_SLUG, ), 'contacts' => array( - 'index' => GLM_MEMBERS_CONTACTS_PLUGIN_SLUG + 'index' => GLM_MEMBERS_CONTACTS_PLUGIN_SLUG, ), 'management' => array( - 'contacts' => GLM_MEMBERS_CONTACTS_PLUGIN_SLUG + 'contacts' => GLM_MEMBERS_CONTACTS_PLUGIN_SLUG, ), 'import' => array( - 'contacts' => GLM_MEMBERS_CONTACTS_PLUGIN_SLUG + 'contacts' => GLM_MEMBERS_CONTACTS_PLUGIN_SLUG, ), ), 'frontActions' => array( diff --git a/views/admin/ajax/contactsListExport.html b/views/admin/ajax/contactsListExport.html new file mode 100644 index 0000000..3daedae --- /dev/null +++ b/views/admin/ajax/contactsListExport.html @@ -0,0 +1,72 @@ + + + + + + Total found: {$memberCount}   + List of Contacts +
+ + + + + + {if $select.exportId}{/if} + {if $select.exportFname}{/if} + {if $select.exportLname}{/if} + {if $select.exportAddr1}{/if} + {if $select.exportAddr2}{/if} + {if $select.exportCity}{/if} + {if $select.exportCounty}{/if} + {if $select.exportState}{/if} + {if $select.exportZip}{/if} + {if $select.exportCountry}{/if} + {if $select.exportOrg}{/if} + {if $select.exportTitle}{/if} + {if $select.exportOfficePhone}{/if} + {if $select.exportHomePhone}{/if} + {if $select.exportMobilePhone}{/if} + {if $select.exportAltPhone}{/if} + {if $select.exportFax}{/if} + {if $select.exportEmail}{/if} + {if $select.exportAltEmail}{/if} + {if $select.exportMemberType}{/if} + {if $CFHeaders}{foreach $CFHeaders as $cHead}{/foreach}{/if} + + + + {if $haveMembers} + {foreach $contacts as $m} + + {if $select.exportId}{/if} + {if $select.exportFname}{/if} + {if $select.exportLname}{/if} + {if $select.exportAddr1}{/if} + {if $select.exportAddr2}{/if} + {if $select.exportCity}{/if} + {if $select.exportCounty}{/if} + {if $select.exportState}{/if} + {if $select.exportZip}{/if} + {if $select.exportCountry}{/if} + {if $select.exportOrg}{/if} + {if $select.exportTitle}{/if} + {if $select.exportOfficePhone}{/if} + {if $select.exportHomePhone}{/if} + {if $select.exportMobilePhone}{/if} + {if $select.exportAltPhone}{/if} + {if $select.exportFax}{/if} + {if $select.exportEmail}{/if} + {if $select.exportAltEmail}{/if} + {if $select.exportMemberType}{/if} + {if $CFHeaders}{foreach $CFHeaders as $cHead}{/foreach}{/if} + + + {/foreach} + {else} + + {/if} + +
IDFirst NameLast NameAddress 1Address 2CityCountyStateZIPCountryOrgTitleOffice PhoneHome PhoneMobile PhoneAlt PhoneFaxE-MailAlt E-MailMember Type{$cHead}
{$m.id}{$m.fname}{$m.lname}{$m.addr1}{$m.addr2}{if $m.city}{$m.city.name}{/if}{$m.county}{if $m.state}{$m.state.value}{/if}{$m.zip}{$m.country}{$m.org}{$m.title}{$m.office_phone}{$m.home_phone}{$m.mobile_phone}{$m.alt_phone.value}{$m.fax}{$m.email}{$m.alt_email}{$m.member_type}{$m[$cHead]}
(no Contacts listed)
+ + + diff --git a/views/admin/ajax/contactsListExportCsv.html b/views/admin/ajax/contactsListExportCsv.html new file mode 100644 index 0000000..2973c3d --- /dev/null +++ b/views/admin/ajax/contactsListExportCsv.html @@ -0,0 +1,49 @@ +{if $haveMembers} + +{if $select.exportId}"ID",{/if} +{if $select.exportFname}"First Name",{/if} +{if $select.exportLname}"Last Name",{/if} +{if $select.exportAddr1}"Address 1",{/if} +{if $select.exportAddr2}"Address 2",{/if} +{if $select.exportCity}"City",{/if} +{if $select.exportCounty}"County",{/if} +{if $select.exportState}"State",{/if} +{if $select.exportZip}"ZIP/Postal",{/if} +{if $select.exportCountry}"Country",{/if} +{if $select.exportOrg}"Org",{/if} +{if $select.exportTitle}"Title",{/if} +{if $select.exportOfficePhone}"Office Phone",{/if} +{if $select.exportHomePhone}"Home Phone",{/if} +{if $select.exportMobilePhone}"Mobile Phone",{/if} +{if $select.exportAltPhone}"Alt Phone",{/if} +{if $select.exportFax}"Fax",{/if} +{if $select.exportEmail}"E-Mail",{/if} +{if $select.exportAltEmail}"Alt E-Mail",{/if} +{if $select.exportMemberType}"Member Type",{/if} +{if $CFHeaders}{foreach $CFHeaders as $cHead}"{$cHead}",{/foreach}{/if} + +{foreach $contacts as $m} +{if $select.exportId}"{$m.id}",{/if} +{if $select.exportFname}"{$m.fname}",{/if} +{if $select.exportLname}"{$m.lname}",{/if} +{if $select.exportAddr1}"{$m.addr1}",{/if} +{if $select.exportAddr2}"{$m.addr2}",{/if} +{if $select.exportCity}"{if $m.city}{$m.city.name}{/if}",{/if} +{if $select.exportCounty}"{$m.county}",{/if} +{if $select.exportState}"{if $m.state}{$m.state.value}{/if}",{/if} +{if $select.exportZip}"{$m.zip}",{/if} +{if $select.exportCountry}"{$m.country}",{/if} +{if $select.exportOrg}"{$m.org}",{/if} +{if $select.exportTitle}"{$m.title}",{/if} +{if $select.exportOfficePhone}"{$m.office_phone}",{/if} +{if $select.exportHomePhone}"{$m.home_phane}",{/if} +{if $select.exportMobilePhone}"{$m.mobile_phone}",{/if} +{if $select.exportAltPhone}"{$m.alt_phone}",{/if} +{if $select.exportFax}"{$m.fax}",{/if} +{if $select.exportEmail}"{$m.email}",{/if} +{if $select.exportAltEmail}"{$m.alt_email}",{/if} +{if $select.exportMemberType}"{$m.member_type}",{/if} +{if $CFHeaders}{foreach $CFHeaders as $cHead}"{$m[$cHead]}",{/foreach}{/if} + +{/foreach} +{else}No Contacts Selected{/if} diff --git a/views/admin/contacts/index.html b/views/admin/contacts/index.html index 9856f82..c15741d 100644 --- a/views/admin/contacts/index.html +++ b/views/admin/contacts/index.html @@ -13,6 +13,9 @@ {/if}

+ {if !$fromMemberMenu} +
Contact Export/Reports
+ {/if} {/if}
@@ -84,6 +87,93 @@
+ {* BEGIN: Export Form *} +
+
+ + + + + + + + + + + + + + + + {* Custom Field Part *} + {if apply_filters( 'glm-members-customfields-active', false )} + + + + + {/if} + + + + +
Name Search: +
+
Member Types: + +
Fields to Export: + + + + + + + + +
Check All / Uncheck All
+ Contact ID
+ First Name
+ Last Name
+ Address 1
+ Address 2
+ City
+ County
+ State
+ ZIP
+ Country
+
+ Organization
+ Title/Position
+ Office Phone
+ Home Phone
+ Mobile Phone
+ Alt Phone
+ Fax Phone
+ Email
+ Alt Email
+ Member Type
+
+
Custom Fields + {$customFields = apply_filters('glm-member-db-fields-get-members-fields', false, 'glm-member-db-contacts')} + {if $customFields} + {foreach $customFields as $cf} + {$cf.field_name}
+ {/foreach} + {/if} +
Export to: + Export for Print
+ Export to Spreadsheet (CSV) +
+ Cancel + +
+
+ {* END: Export Form *} + -- 2.17.1