--- /dev/null
+<?php
+
+/**
+ * Gaslight Media Members Database
+ * Admin Data Import
+ *
+ * PHP version 5.5
+ *
+ * @category glmWordPressPlugin
+ * @package glmMembersDatabase
+ * @author Chuck Scott <cscott@gaslightmedia.com>
+ * @license http://www.gaslightmedia.com Gaslightmedia
+ * @version 0.1
+ */
+require_once GLM_MEMBERS_PLUGIN_PATH . '/models/admin/import/index.php';
+/*
+ * This class performs the work for the default action of the "Import" menu
+ * option.
+ *
+ */
+class GlmMembersAdmin_import_social extends GlmMembersAdmin_import_index
+{
+
+ const CSV_CHARS_PER_LINE = 6000;
+ /**
+ * WordPress Database Object
+ *
+ * @var $wpdb
+ * @access public
+ */
+ public $wpdb;
+ /**
+ * Plugin Configuration Data
+ *
+ * @var $config
+ * @access public
+ */
+ public $config;
+
+ /**
+ * errors
+ *
+ * @var $errors
+ * @access public
+ */
+ public $errors = array();
+
+ /**
+ * numberProcessed
+ *
+ * @var float
+ * @access public
+ */
+ public $numberProcessed = 0;
+
+ /**
+ * totalSocials
+ *
+ * @var float
+ * @access public
+ */
+ public $totalSocials = 0;
+
+ /**
+ * processingComplete
+ *
+ * @var bool
+ * @access public
+ */
+ public $processingComplete = false;
+
+ /**
+ * Constructor
+ *
+ * This contractor 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;
+
+ }
+
+ /**
+ * Perform Model Action
+ *
+ * This method does the work for this model and returns any resulting data
+ *
+ * @return array Status and data array
+ *
+ * 'status'
+ *
+ * True if successful and false if there was a fatal failure.
+ *
+ * 'menuItemRedirect'
+ *
+ * If not false, provides a menu item the controller should
+ * execute after this one. Normally if this is used, there would also be a
+ * modelRedirect value supplied as well.
+ *
+ * 'modelRedirect'
+ *
+ * If not false, provides an action the controller should execute after
+ * this one.
+ *
+ * 'view'
+ *
+ * A suggested view name that the controller should use instead of the
+ * default view for this model or false to indicate that the default view
+ * should be used.
+ *
+ * 'data'
+ *
+ * Data that the model is returning for use in merging with the view to
+ * produce output.
+ *
+ */
+ public function modelAction ($actionData = false)
+ {
+ // Set the view file
+ $view = 'social.html';
+ $failure = false;
+ $option = 'social';
+ $clearData = false;
+ $haveMembers = false;
+ $fileExists = false;
+ $isValid = false;
+ // Check to see if they have members
+ $haveMembers = $this->wpdb->get_var(
+ "SELECT count(id)
+ FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "members"
+ );
+ // $fileData - The main files needed for the member import
+ // - field: input field name
+ // - name: file name
+ // - exists: Does file exists. Set to false at first.
+ // - validate: Validation array. Header line must match this.
+ // - type: Type of file. Used in the processing function.
+ $fileData = array(
+ 'Social' => array(
+ 'field' => 'social_file',
+ 'name' => 'socialData.csv',
+ 'exists' => false,
+ 'validate' => array( 'member_id', 'facebook', 'twitter', 'pinterest',
+ 'google_plus', 'digg', 'linkedin', 'instagram', 'youtube', 'flickr',
+ 'blog', 'photobucket', 'rss',),
+ 'type' => 'social',
+ ),
+ );
+ // Setting readyToProcess to false (initialize)
+ $readyToProcess = false;
+
+ // Set the $option if found in $_REQUEST array
+ if (isset($_REQUEST['option']) && $_REQUEST['option'] != '') {
+ $option = $_REQUEST['option'];
+ }
+
+ // Set the $option2 if found in $_REQUEST array
+ if (isset($_REQUEST['option2']) && $_REQUEST['option2'] != '') {
+ $option2 = $_REQUEST['option2'];
+ }
+
+ // Set variable for the upload directory
+ $wpUploadDir = wp_get_upload_dir();
+
+ // Set the $uploadPath for import files
+ $uploadPath = $wpUploadDir['basedir'] . '/' . 'glm-member-import';
+
+ // If the folder for the upload import files doesn't exists create one.
+ if ( !is_dir( $uploadPath ) ) {
+ // Get old umask
+ $oldMask = umask(0);
+ // Set folder permission
+ mkdir( $uploadPath, 0770 );
+ // reset old umask
+ umask( $oldMask );
+ }
+
+ switch( $option ) {
+
+ case 'socialValidate';
+ $validFiles = 0;
+ // Set the view file
+ $view = 'socialValidate.html';
+ $fileCount = count( $fileData );
+ // Move any files uploaded
+ //echo '<pre>$_FILES: ' . print_r( $_FILES, true ) . '</pre>';
+ if ( isset( $_FILES ) ) {
+ foreach ( $fileData as $fileHeader => $file ) {
+ if ( !$_FILES[$file['field']]['error'] ) {
+ move_uploaded_file( $_FILES[$file['field']]['tmp_name'], $uploadPath . '/'. $file['name'] );
+ }
+ }
+ }
+ // Check that each file exists
+ foreach ( $fileData as $fileHeader => $file ) {
+ if ( is_file( $uploadPath . '/' . $file['name'] ) ) {
+ $fileData[$fileHeader]['exists'] = true;
+ $fData = $this->readCSVFileHeaders( $uploadPath . '/' . $file['name'] );
+ $isValid = ( $file['validate'] == $fData );
+ if ( $isValid ) {
+ $validFiles++;
+ }
+ $fileData[$fileHeader]['data'] = $fData;
+ $fileData[$fileHeader]['isValid'] = $isValid;
+ }
+ }
+ $readyToProcess = ( $validFiles == $fileCount );
+ $clearData = ( filter_var( $_REQUEST['clear_data'], FILTER_VALIDATE_BOOLEAN ) );
+ break;
+
+ case 'socialProcess':
+ $clearData = ( filter_var( $_REQUEST['clear_data'], FILTER_VALIDATE_BOOLEAN ) );
+ if ( $clearData ) {
+ // Empty the tables for the member data.
+ // Not including the management options.
+ $this->wpdb->query('DELETE FROM ' . GLM_MEMBERS_SOCIAL_PLUGIN_DB_PREFIX . 'social_urls');
+ }
+ $dataTable = GLM_MEMBERS_SOCIAL_PLUGIN_DB_PREFIX . 'social_urls';
+ $dataFormat = array( '%s', '%d', '%d', '%d' );
+ // Loop through the $fileData array.
+ // This will setup the wordpress options for each file.
+ foreach ( $fileData as $fileHeader => $file ) {
+ if ( is_file( $uploadPath . '/' . $file['name'] ) ) {
+ $fData = $this->readCSVFile( $uploadPath . '/' . $file['name'] );
+ $this->totalSocials = count( $fData );
+ //echo '<pre>$fData: ' . print_r( $fData, true ) . '</pre>';
+ foreach ( $fData as $member ) {
+ $this->numberProcessed++;
+ // Need to first get the member id from the database
+ // It will match from the old_member_id field
+ $memberId = $this->wpdb->get_var(
+ $this->wpdb->prepare(
+ "SELECT id
+ FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "members
+ WHERE old_member_id = %d",
+ $member['member_id']
+ )
+ );
+ if ( $memberId ) {
+ if ($member['facebook']) {
+ // add for facebook
+ $newId = $this->wpdb->insert(
+ $dataTable,
+ array(
+ 'url' => $member['facebook'],
+ 'ref_type' => $this->config['ref_type_numb']['Member'],
+ 'ref_dest' => $memberId,
+ 'social' => 1
+ ),
+ $dataFormat
+ );
+ }
+ if ($member['twitter']) {
+ // add for twitter
+ $newId = $this->wpdb->insert(
+ $dataTable,
+ array(
+ 'url' => $member['twitter'],
+ 'ref_type' => $this->config['ref_type_numb']['Member'],
+ 'ref_dest' => $memberId,
+ 'social' => 2
+ ),
+ $dataFormat
+ );
+ }
+ if ($member['pinterest']) {
+ // add for pinterest
+ $newId = $this->wpdb->insert(
+ $dataTable,
+ array(
+ 'url' => $member['pinterest'],
+ 'ref_type' => $this->config['ref_type_numb']['Member'],
+ 'ref_dest' => $memberId,
+ 'social' => 3
+ ),
+ $dataFormat
+ );
+ }
+ if ($member['google_plus']) {
+ // add for google_plus
+ $newId = $this->wpdb->insert(
+ $dataTable,
+ array(
+ 'url' => $member['google_plus'],
+ 'ref_type' => $this->config['ref_type_numb']['Member'],
+ 'ref_dest' => $memberId,
+ 'social' => 4
+ ),
+ $dataFormat
+ );
+ }
+ if ($member['digg']) {
+ $newId = $this->wpdb->insert(
+ $dataTable,
+ array(
+ 'url' => $member['digg'],
+ 'ref_type' => $this->config['ref_type_numb']['Member'],
+ 'ref_dest' => $memberId,
+ 'social' => 5
+ )
+ );
+ }
+ if ($member['linkedin']) {
+ // add for linkedin
+ $newId = $this->wpdb->insert(
+ $dataTable,
+ array(
+ 'url' => $member['linkedin'],
+ 'ref_type' => $this->config['ref_type_numb']['Member'],
+ 'ref_dest' => $memberId,
+ 'social' => 6
+ ),
+ $dataFormat
+ );
+ }
+ if ($member['instagram']) {
+ // add for instagram
+ $newId = $this->wpdb->insert(
+ $dataTable,
+ array(
+ 'url' => $member['instagram'],
+ 'ref_type' => $this->config['ref_type_numb']['Member'],
+ 'ref_dest' => $memberId,
+ 'social' => 7
+ ),
+ $dataFormat
+ );
+ }
+ if ($member['youtube']) {
+ // add for youtube
+ $newId = $this->wpdb->insert(
+ $dataTable,
+ array(
+ 'url' => $member['youtube'],
+ 'ref_type' => $this->config['ref_type_numb']['Member'],
+ 'ref_dest' => $memberId,
+ 'social' => 8
+ ),
+ $dataFormat
+ );
+ }
+ if ($member['flickr']) {
+ // add for flickr
+ $newId = $this->wpdb->insert(
+ $dataTable,
+ array(
+ 'url' => $member['flickr'],
+ 'ref_type' => $this->config['ref_type_numb']['Member'],
+ 'ref_dest' => $memberId,
+ 'social' => 9
+ ),
+ $dataFormat
+ );
+ }
+ if ($member['blog']) {
+ // add for blog
+ $newId = $this->wpdb->insert(
+ $dataTable,
+ array(
+ 'url' => $member['blog'],
+ 'ref_type' => $this->config['ref_type_numb']['Member'],
+ 'ref_dest' => $memberId,
+ 'social' => 10
+ ),
+ $dataFormat
+ );
+ }
+ if ($member['photobucket']) {
+ $newId = $this->wpdb->insert(
+ $dataTable,
+ array(
+ 'url' => $member['photobucket'],
+ 'ref_type' => $this->config['ref_type_numb']['Member'],
+ 'ref_dest' => $memberId,
+ 'social' => 11
+ ),
+ $dataFormat
+ );
+ }
+ }
+ }
+
+ }
+ }
+ if ( count( $this->errors ) == 0 ) {
+ $readyToProcess = true;
+ }
+ // Here we need to check to see if we processed all members.
+ // Also the counter has to increment the total processed so far.
+ if ( $this->numberProcessed == $this->totalSocials ) {
+ $this->processingComplete = true;
+ }
+ // Set the view file:<
+ $view = 'socialProcess.html';
+ break;
+
+ case 'social':
+ default:
+ // Set the view file
+ $view = 'social.html';
+ // check upload dir to see if they have any files in yet
+ foreach ( $fileData as $fileHeader => $file ) {
+ if ( is_file( $uploadPath . '/' . $file['name'] ) ) {
+ $fileData[$fileHeader]['exists'] = true;
+ $fileData[$fileHeader]['mtime'] = filemtime( $uploadPath . '/' . $file['name'] );
+ }
+ }
+
+ break;
+
+ }
+
+ // Setup the template data array
+ $templateData = array(
+ 'fileExists' => $fileExists,
+ 'option' => $option,
+ 'errors' => $this->errors,
+ 'numberProcessed' => $this->numberProcessed,
+ 'totalSocials' => $this->totalSocials,
+ 'completed' => $this->processingComplete,
+ 'data' => false,
+ 'fileData' => $fileData,
+ 'clearData' => $clearData,
+ 'csvData' => '<pre>$fileData: ' . print_r( $fileData, true ) . '</pre>',
+ 'readyToProcess' => $readyToProcess,
+ 'haveMembers' => $haveMembers,
+ 'isValid' => $isValid,
+ 'sampleFileUrl' => GLM_MEMBERS_PLUGIN_BASE_URL . '/sample-files/',
+ );
+
+ // Return status, suggested view, and data to controller
+ return array(
+ 'status' => true,
+ 'menuItemRedirect' => false,
+ 'modelRedirect' => false,
+ 'view' => 'admin/import/' . $view,
+ 'data' => $templateData,
+ );
+
+ }
+
+}