[ Index ]

MailPress 7.1

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

title

Body

[close]

/mp-includes/class/options/import/importers/ -> csv.php (source)

   1  <?php
   2  class MP_import_csv extends MP_import_importer_
   3  {
   4      var $id = 'csv';
   5  
   6  	function dispatch() 
   7      {
   8          $step = $this->get_step();
   9  
  10          $this->header();
  11          switch ( $step ) 
  12          {
  13              case 0 :
  14                  $this->greet();
  15              break;
  16              case 1 :
  17                  $this->start_trace( $step );
  18                  if ( $this->handle_upload() )
  19                  {
  20                             $this->message_report( " ANALYSIS   !" );
  21                          $sniff = $this->sniff( $step );
  22                      $this->end_trace( true );
  23                      if ( $sniff )
  24                          $this->fileform();
  25                      else
  26                          $this->error( '<p><strong>' . __( 'Unable to determine email location', 'MailPress' ) . '</strong></p>' );
  27                  }
  28                  else
  29                  {
  30                      $this->message_report( "** ERROR ** ! Could not upload the file" );
  31                      $this->end_trace( false );
  32                  }
  33              break;
  34              case 2:
  35                  $this->start_trace( $step );
  36                  $import = $this->import( MP_AdminPage::$get_['id'] );
  37                  $this->end_trace( true );
  38                  if ( $import )
  39                      $this->success( '<p>' . sprintf( __( "<b>File imported</b> : <i>%s</i>", 'MailPress' ), $this->file ) . '</p><p><strong>' . sprintf( __( "<b>Number of records</b> : <i>%s</i>", 'MailPress' ), $import ) . '</strong></p>' );
  40                  else 
  41                      $this->error( '<p><strong>' . $this->file . '</strong></p>' );
  42              break;
  43          }
  44          $this->footer();
  45      }
  46  
  47  // step 0
  48  
  49  // step 1
  50  
  51  	function sniff( $step, $first=true )
  52      {
  53          $this->message_report( " sniff $step    ! >>> " . $this->file );
  54  
  55          require_once  'parsecsv/parsecsv.lib.php';
  56          $this->csv = new parseCSV();
  57          $this->csv->auto( $this->file );
  58          $this->hasheader = true;
  59  
  60          return ( $first ) ? $this->find_email() : true;
  61      }
  62  
  63  	function find_email()
  64      {
  65          $i = 0;
  66          $email = array();
  67          foreach ( $this->csv->data as $row )
  68          {
  69              foreach ( $row as $k => $v )    if ( MailPress::is_email( $v ) ) if ( isset( $email[$k] ) ) $email[$k]++; else $email[$k] = 1;
  70  
  71              $i++;
  72              if ( $i > 9 ) break;
  73          }
  74  
  75          if ( 0 == count( $email ) )
  76          {
  77              $this->message_report( ' **WARNING* ! Unable to determine email location' );
  78              return false;
  79          }
  80  
  81          asort( $email );
  82          $x = array_flip( $email );
  83          $this->emailcol = end( $x );
  84          
  85          $this->message_report( ' email      ! ' . sprintf( 'Email probably in column %s', $this->emailcol ) );
  86  
  87          return true;
  88      }
  89  
  90  	function fileform() 
  91      {
  92          if ( current_user_can( 'MailPress_manage_mailinglists' ) ) add_action( 'admin_print_footer_scripts', array( __CLASS__, 'footer_scripts' ), 1 );
  93  ?>
  94      <form id="mp_import" action="<?php echo MailPress_import; ?>&amp;mp_import=csv&amp;step=2&amp;id=<?php echo $this->file_id; ?>" method="post">
  95  <?php     if ( current_user_can( 'MailPress_manage_mailinglists' ) ) : ?>
  96          <h3><?php _e( 'Mailing list', 'MailPress' ); ?></h3>
  97          <p><?php _e( 'Optional, you can import the MailPress users in a specific mailing list ...', 'MailPress' ); ?></p>
  98  <?php            MP_Mailinglist::dropdown( array( 'htmlname' => 'mailinglist', 'htmlid' => 'mailinglist', 'hierarchical' => true, 'orderby' => 'name', 'hide_empty' => '0', 'show_option_none' => __( 'Choose mailinglist', 'MailPress' ) ) ); ?>
  99  <?php endif; ?>
 100  <?php if ( class_exists( 'MailPress_newsletter' ) ) : ?>
 101          <h3><?php _e( 'Newsletter', 'MailPress' ); ?></h3>
 102          <p>
 103              <input type="checkbox" name="no_newsletter" id="no_newsletter" />
 104              <?php _e( '<b>Delete</b> all subscriptions.', 'MailPress' ); ?>
 105          </p>
 106          <p>
 107              <input type="checkbox" name="newsletter" id="newsletter" /> 
 108              <?php _e( '<b>Add</b> default subscriptions.', 'MailPress' ); ?>
 109          </p>
 110  <?php endif; ?>
 111          <h3><?php _e( 'File scan', 'MailPress' ); ?></h3>
 112          <p><?php printf( __( "On the first records (see hereunder), the file scan found that the email is in column '<strong>%s</strong>'.", 'MailPress' ), $this->emailcol ); ?>
 113          <?php _e( 'However, you can select another column.<br /> Invalid emails will not be inserted.', 'MailPress' ); ?></p>
 114          <table class="wp-list-table widefat fixed striped zyxw">
 115              <thead>
 116                  <tr>
 117                      <td style="width:auto;"><?php _e( 'Choose email column', 'MailPress' ); ?></td>
 118  <?php
 119          foreach ( $this->csv->data as $row )
 120          {
 121              foreach ( $row as $k => $v )
 122              {
 123  ?>
 124                      <td><input type="radio" name="is_email" value="<?php echo $k; ?>"<?php checked( $k, $this->emailcol ); ?> /><span><?php echo $k; ?></span></td>
 125  <?php
 126              }
 127              break;
 128          }
 129  ?>
 130                  </tr>
 131                  <tr>
 132                      <td><?php _e( 'Choose name column', 'MailPress' ); ?></td>
 133  <?php
 134          foreach ( $this->csv->data as $row )
 135          {
 136              foreach ( $row as $k => $v )
 137              {
 138  ?>
 139                      <td><input type="radio" name="is_name" value="<?php echo $k; ?>" /><span><?php echo $k; ?></span></td>
 140  <?php
 141              }
 142              break;
 143          }
 144  ?>
 145                  </tr>
 146              </thead>
 147              <tbody>
 148  <?php
 149          $i = 0;
 150          foreach ( $this->csv->data as $row )
 151          {
 152  ?>
 153                  <tr>
 154                      <td></td>
 155  <?php
 156              foreach ( $row as $k => $v )
 157              {
 158  ?>
 159                      <td><span <?php if ( $k == $this->emailcol ) if ( !MailPress::is_email( $v ) ) echo 'style="background-color:#fdd;"'; else echo 'style="background-color:#dfd;"';?>><?php echo $v; ?></span></td>
 160  <?php
 161              }
 162  ?>
 163                  </tr>
 164  <?php
 165              $i++;
 166              if ( $i > 9 ) break;
 167          }
 168  ?>
 169              </tbody>
 170          </table>
 171          <p class="submit">
 172              <input type="submit" class="button-primary" value="<?php echo esc_attr( __( 'Submit' ) ); ?>" />
 173          </p>
 174      </form>
 175  <?php
 176      }
 177  
 178  	public static function footer_scripts() 
 179      {
 180          wp_register_script( 'mp-import', '/' . MP_PATH . 'mp-includes/js/mp_mailinglist_dropdown.js', array( 'jquery' ), false, 1 );
 181          wp_localize_script( 'mp-import',     'mp_ml_select_L10n', array( 
 182              'error' => __( 'Please, choose a mailinglist', 'MailPress' ), 
 183              'select' => 'mailinglist', 
 184              'form'   => 'mp_import',
 185              'l10n_print_after' => 'try{convertEntities( mp_ml_select_L10n );}catch( e ){};' 
 186           ) );
 187  
 188          wp_enqueue_script( 'mp-import' );
 189      }
 190  
 191  // step 2
 192  
 193  	function import( $id ) 
 194      {
 195          $this->file_id = ( int ) $id;
 196          $this->file    = get_attached_file( $this->file_id );
 197  
 198          $this->message_report( " IMPORTING  !" );
 199  
 200          $this->sniff( 2, false );
 201  
 202          if ( !is_file( $this->file ) ) {    $this->message_report( "File not found" . $this->file ); return false;}
 203  
 204          $this->emailcol = MP_AdminPage::$pst_['is_email'];
 205          $this->namecol  = ( isset( MP_AdminPage::$pst_['is_name'] ) )  ? MP_AdminPage::$pst_['is_name'] : null;
 206  
 207          $mailinglist_ok = ( class_exists( 'MailPress_mailinglist' ) && ( '-1' != MP_AdminPage::$pst_['mailinglist'] ) );
 208          if ( $mailinglist_ok )
 209          {
 210              $this->mailinglist_ID = MP_AdminPage::$pst_['mailinglist'];
 211              add_filter( 'MailPress_mailinglist_default', array( $this, 'mailinglist_default' ), 8, 1 );
 212  
 213              $mailinglist_name = MP_Mailinglist::get_name( $this->mailinglist_ID );
 214          }
 215  
 216          $newsletter_ok       = ( class_exists( 'MailPress_newsletter' ) && isset( MP_AdminPage::$pst_['newsletter'] ) );
 217          $no_newsletter_ok = ( class_exists( 'MailPress_newsletter' ) && isset( MP_AdminPage::$pst_['no_newsletter'] ) );
 218  
 219          $i = 0;
 220          foreach ( $this->csv->data as $row )
 221          {
 222              $i++;
 223  
 224              $curremail = trim( strtolower( $row[$this->emailcol] ) );
 225              $currname  = ( isset( $this->namecol ) ) ? trim( $row[$this->namecol] ) : '';
 226              $mp_user_id = $this->sync_mp_user( $curremail, $currname );
 227  
 228              if ( $mp_user_id )
 229              {
 230                  if ( $mailinglist_ok )
 231                  {
 232                      $this->sync_mp_user_mailinglist( $mp_user_id, $this->mailinglist_ID, $curremail, $mailinglist_name );
 233                  }
 234                  if ( $no_newsletter_ok )
 235                  {
 236                      $this->sync_mp_user_no_newsletter( $mp_user_id );
 237                  }
 238                  if ( $newsletter_ok )
 239                  {
 240                      $this->sync_mp_user_newsletter( $mp_user_id );
 241                  }
 242  
 243                  foreach ( $row as $k => $v )
 244                  {
 245                      if ( $k == $this->emailcol ) continue;
 246                      if ( isset( $this->namecol ) && ( $k == $this->namecol ) ) continue;
 247  
 248                      $this->sync_mp_usermeta( $mp_user_id, $k, $v );
 249                  }
 250              }
 251          }
 252          return $i;
 253      }
 254  
 255  	function mailinglist_default( $default )
 256      {
 257          return ( isset( $this->mailinglist_ID ) ) ? $this->mailinglist_ID : $default;
 258      }
 259  }
 260  new MP_import_csv( __( 'Import your <strong>csv</strong> file.', 'MailPress' ), __( 'Import Csv', 'MailPress' ) );


Generated: Mon Mar 11 18:33:33 2019 Cross-referenced by PHPXref 0.7.1