[ Index ]

MailPress 7.1

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

title

Body

[close]

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

   1  <?php
   2  class MP_import_excel extends MP_import_importer_
   3  {
   4      var $id = 'excel';
   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  
  23                      if ( $sniff )
  24                      {
  25                          if ( $sniff == 1 )
  26                          {
  27                              $this->message_report( "** INFO  ** ! Only one sheet in excel spreadsheet " );
  28                              $this->end_trace( true );
  29                              $this->fileform( $this->file_id, 0 );
  30                          }
  31                          else
  32                          {
  33                              $this->message_report( " SHEET FORM ! There are $sniff sheets detected " );
  34                              $this->end_trace( true );            
  35                              $this->sheetform();
  36                          }
  37                      }
  38                      else
  39                      {
  40                          $this->end_trace( true );    
  41                          $this->error( '<p><strong>' . __( 'Unable to determine sheet(s)', 'MailPress' ) . '</strong></p>' );
  42                      }
  43  
  44                  }
  45                  else
  46                  {
  47                      $this->message_report( "** ERROR ** ! Could not upload the file" );
  48                      $this->end_trace( false );
  49                  }
  50  
  51              break;
  52              case 2 :
  53                  $this->fileform( MP_AdminPage::$get_['id'], MP_AdminPage::$get_['sid'] );
  54              break;
  55              case 3:
  56                  $this->start_trace( $step );
  57                  $import = $this->import( MP_AdminPage::$get_['id'], MP_AdminPage::$get_['sid'] );
  58                  $this->end_trace( true );
  59                  if ( $import )
  60                      $this->success( '<p>' . sprintf( __( "<b>Number of records</b> : <i>%s</i>", 'MailPress' ), $import ) . '</strong></p>' );
  61                  else 
  62                      $this->error( '<p><strong>' . $this->file . '</strong></p>' );
  63              break;
  64          }
  65          $this->footer();
  66      }
  67  
  68  // step 0
  69  
  70  // step 1
  71  
  72  	function sniff( $step, $first = true )
  73      {
  74          $this->message_report( " sniff $step    ! >>> " . $this->file );
  75  
  76          require_once  'parseexcel/parseexcel.lib.php';
  77          $this->excel = new Spreadsheet_Excel_Reader( $this->file );
  78  
  79          $this->hasheader = true;
  80  
  81          return ( $first ) ? count( $this->excel->boundsheets ) : true;
  82      }
  83  
  84  	function sheetform() 
  85      {
  86          if ( !isset( $this->excel ) )
  87          {
  88              $this->file_id = ( int ) $id;
  89              $this->file    = get_attached_file( $this->file_id );
  90              if ( !file_exists( $this->file ) ) { $this->message_report( "File not found " . $this->file ); return false; }
  91  
  92              $this->sniff( MP_AdminPage::$get_['step'], false );
  93          }
  94  ?>
  95      <h3><?php _e( 'File scan', 'MailPress' ); ?></h3>
  96      <p><?php _e( "Here are the sheets found in the excel file, please select one", 'MailPress' ); ?></p>
  97      <div>
  98  <?php
  99          foreach ( $this->excel->boundsheets as $sid => $sheet )
 100          {
 101  ?>
 102      <form action="<?php echo MailPress_import; ?>&amp;mp_import=excel&amp;step=2&amp;sid=<?php echo $sid; ?>&amp;id=<?php echo $this->file_id; ?>" method="post" style="float:left;">
 103          <input type="submit" class="button-primary" value="<?php echo attribute_escape( $sheet['name'] ); ?>" />&nbsp;&nbsp;
 104      </form>
 105  <?php
 106          }
 107  ?>
 108      </div>
 109  <?php
 110      }
 111  
 112  	function find_email( $sid )
 113      {
 114          $this->message_report( " FIND EMAIL !" );
 115  
 116          if ( !isset( $this->excel ) )
 117          {
 118              $this->file_id = ( int ) $id;
 119              $this->file    = get_attached_file( $this->file_id );
 120              if ( !file_exists( $this->file ) ) { $this->message_report( "File not found " . $this->file ); return false; }
 121  
 122              $this->sniff( MP_AdminPage::$get_['step'], false );
 123          }
 124  
 125          $email = array();
 126          $i = 0;
 127  
 128          foreach ( $this->excel->sheets[$sid]['cells'] as $r => $row )
 129          {
 130              foreach ( $row as $c => $v )    if ( MailPress::is_email( $v ) ) if ( isset( $email[$c] ) ) $email[$c]++; else $email[$c] = 1;
 131  
 132              $i++;
 133              if ( $i > 9 ) break;
 134          }
 135  
 136          if ( 0 == count( $email ) )
 137          {
 138              $this->message_report( ' **WARNING* ! Unable to determine email location' );
 139              return false;
 140          }
 141  
 142          asort( $email );
 143          $x = array_flip( $email );
 144          $this->emailcol = end( $x );
 145          
 146          $this->message_report( ' email      ! ' . sprintf( 'Email probably in column %s', $this->emailcol ) );
 147  
 148          return $this->emailcol;
 149      }
 150          
 151  // step 2
 152          
 153  	function fileform( $id, $sid ) 
 154      {
 155          $this->start_trace( MP_AdminPage::$get_['step'] );
 156          $this->message_report( " ANALYSIS   ! for sheet #{$sid}" );
 157  
 158          if ( !isset( $this->excel ) )
 159          {
 160              $this->file_id = ( int ) $id;
 161              $this->file    = get_attached_file( $this->file_id );
 162              if ( !file_exists( $this->file ) ) { $this->message_report( "File not found " . $this->file ); return false; }
 163  
 164              $this->sniff( MP_AdminPage::$get_['step'], false );
 165          }
 166  
 167          $this->emailcol = ( isset( MP_AdminPage::$get_['col'] ) ) ? MP_AdminPage::$get_['col'] : $this->find_email( $sid );
 168          $this->end_trace( true );
 169  
 170          if( !empty( $this->excel ) )
 171          {
 172              if ( current_user_can( 'MailPress_manage_mailinglists' ) ) add_action( 'admin_print_footer_scripts', array( __CLASS__, 'footer_scripts' ), 1 );
 173  
 174              $columns = array_shift( $this->excel->sheets[$sid]['cells'] );
 175  ?>
 176      <form id="mp_import" action="<?php echo MailPress_import; ?>&amp;mp_import=excel&amp;step=3&amp;id=<?php echo $this->file_id; ?>&amp;sid=<?php echo $sid; ?>" method="post">
 177  <?php     if ( current_user_can( 'MailPress_manage_mailinglists' ) ) : ?>
 178          <h3><?php _e( 'Mailing list', 'MailPress' ); ?></h3>
 179          <p><?php _e( 'Optional, you can import the MailPress users in a specific mailing list ...', 'MailPress' ); ?></p>
 180  <?php            MP_Mailinglist::dropdown( array( 'htmlname' => 'mailinglist', 'htmlid' => 'mailinglist', 'selected' => get_option( MailPress_mailinglist::option_name_default ), 'hierarchical' => true, 'orderby' => 'name', 'hide_empty' => '0', 'show_option_none' => __( 'Choose mailinglist', 'MailPress' ) ) ); ?><?php endif; ?>
 181  <?php         if ( class_exists( 'MailPress_newsletter' ) ) : ?>
 182          <h3><?php _e( 'Newsletter', 'MailPress' ); ?></h3>
 183          <p>
 184              <input type="checkbox" name="no_newsletter" id="no_newsletter" />
 185              <?php _e( '<b>Delete</b> all subscriptions.', 'MailPress' ); ?>
 186          </p>
 187          <p>
 188              <input type="checkbox" name="newsletter" id="newsletter" /> 
 189              <?php _e( '<b>Add</b> default subscriptions.', 'MailPress' ); ?>
 190          </p>
 191  <?php         endif; ?>
 192          <h3><?php _e( 'File scan', 'MailPress' ); ?></h3>
 193          <p>
 194  <?php 
 195              printf( __( "On the first records (see hereunder), the file scan found that the email is in column '<strong>%s</strong>'.", 'MailPress' ), $columns[$this->emailcol] );
 196              echo '&nbsp;';
 197              _e( 'However, you can select another column.<br /> Invalid emails will not be inserted.', 'MailPress' ); 
 198  ?>
 199          </p>
 200          <table class="wp-list-table widefat fixed striped zyxw">
 201              <thead>
 202                  <tr>
 203                      <td style="width:auto;"><?php _e( 'Choose email column', 'MailPress' ); ?></td>
 204  <?php
 205              $i = 1;
 206              foreach ( $columns as $k => $v )
 207              {
 208                  while ( $i < $k ) { echo '<td></td>'; $i++; }
 209  ?>
 210                      <td><input type="radio" name="is_email" value="<?php echo $k; ?>"<?php checked( $k, $this->emailcol ); ?> /><span><?php echo $v; ?></span></td>
 211  <?php
 212                  $i++;
 213              }
 214  ?>
 215                  </tr>
 216                  <tr>
 217                      <td><?php _e( 'Choose name column', 'MailPress' ); ?></td>
 218  <?php
 219              $i = 1;
 220              foreach ( $columns as $k => $v )
 221              {
 222                  while ( $i < $k ) { echo '<td></td>'; $i++; }
 223  ?>
 224                      <td><input type="radio" name="is_name" value="<?php echo $k; ?>" /><span><?php echo $v; ?></span></td>
 225  <?php
 226                  $i++;
 227              }
 228  ?>
 229                  </tr>
 230              </thead>
 231              <tbody>
 232  <?php
 233  
 234              $i = 0;
 235              foreach ( $this->excel->sheets[$sid]['cells'] as $r => $row )
 236              {
 237  ?>
 238                  <tr>
 239                      <td></td>
 240  <?php
 241                  $j = 1;
 242                  foreach ( $row as $k => $v )
 243                  {
 244                      while ( $j < $k ) { echo '<td></td>'; $j++; }
 245  ?>
 246                      <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>
 247  <?php
 248                      $j++;
 249                  }
 250  ?>
 251                  </tr>
 252  <?php
 253                  $i++;
 254                  if ( $i > 9 ) break;
 255              }
 256  ?>
 257              </tbody>
 258          </table>
 259          <p class="submit">
 260              <input class="button-primary" type="submit" value="<?php echo attribute_escape( __( 'Submit' ) ); ?>" />
 261          </p>
 262      </form>
 263  <?php
 264          }
 265      }
 266  
 267  	public static function footer_scripts() 
 268      {
 269          wp_register_script( 'mp-import', '/' . MP_PATH . 'mp-includes/js/mp_mailinglist_dropdown.js', array( 'jquery' ), false, 1 );
 270          wp_localize_script( 'mp-import',     'mp_ml_select_L10n', array( 
 271              'error' => __( 'Please, choose a mailinglist', 'MailPress' ), 
 272              'select' => 'mailinglist', 
 273              'form'   => 'mp_import',
 274              'l10n_print_after' => 'try{convertEntities( mp_ml_select_L10n );}catch( e ){};' 
 275           ) );
 276  
 277          wp_enqueue_script( 'mp-import' );
 278      }
 279  
 280  // step 3
 281  
 282  	function import( $id, $sid ) 
 283      {
 284          $this->message_report( " IMPORT     ! for sheet #{$sid}" );
 285  
 286          if ( !isset( $this->excel ) )
 287          {
 288              $this->file_id = ( int ) $id;
 289              $this->file    = get_attached_file( $this->file_id );
 290              if ( !file_exists( $this->file ) ) { $this->message_report( "File not found " . $this->file ); return false; }
 291  
 292              $this->sniff( MP_AdminPage::$get_['step'], false );
 293          }
 294  
 295          $this->emailcol = MP_AdminPage::$pst_['is_email'];
 296          $this->namecol  = ( isset( MP_AdminPage::$pst_['is_name'] ) )  ? MP_AdminPage::$pst_['is_name'] : null;
 297  
 298          $mailinglist_ok = ( class_exists( 'MailPress_mailinglist' ) && ( '-1' != MP_AdminPage::$pst_['mailinglist'] ) );
 299          if ( $mailinglist_ok )
 300          {
 301              $this->mailinglist_ID = MP_AdminPage::$pst_['mailinglist'];
 302              add_filter( 'MailPress_mailinglist_default', array( $this, 'mailinglist_default' ), 8, 1 );
 303  
 304              $mailinglist_name = MP_Mailinglist::get_name( $this->mailinglist_ID );
 305          }
 306  
 307          $newsletter_ok       = ( class_exists( 'MailPress_newsletter' ) && isset( MP_AdminPage::$pst_['newsletter'] ) );
 308          $no_newsletter_ok = ( class_exists( 'MailPress_newsletter' ) && isset( MP_AdminPage::$pst_['no_newsletter'] ) );
 309  
 310          $i = 0;
 311          $columns = array_shift( $this->excel->sheets[$sid]['cells'] );
 312          foreach ( $this->excel->sheets[$sid]['cells'] as $r => $row )
 313          {
 314              if ( !isset( $row[$this->emailcol] ) ) {$this->message_report( "Email column " . $this->emailcol . " not found in file " . $this->file . " for sheet #" . $sid . " in row #" . $r ); continue;}
 315  
 316              $i++;
 317  
 318              $curremail = trim( strtolower( $row[$this->emailcol] ) );
 319              $currname  = ( isset( $this->namecol ) ) ? trim( $row[$this->namecol] ) : '';
 320              $mp_user_id = $this->sync_mp_user( $curremail, $currname );
 321  
 322              if ( $mp_user_id )
 323              {
 324                  if ( $mailinglist_ok )
 325                  {
 326                      $this->sync_mp_user_mailinglist( $mp_user_id, $this->mailinglist_ID, $curremail, $mailinglist_name );
 327                  }
 328                  if ( $no_newsletter_ok )
 329                  {
 330                      $this->sync_mp_user_no_newsletter( $mp_user_id );
 331                  }
 332                  if ( $newsletter_ok )
 333                  {
 334                      $this->sync_mp_user_newsletter( $mp_user_id );
 335                  }
 336  
 337                  foreach ( $row as $k => $v )
 338                  {
 339                      if ( $k == $this->emailcol ) continue;
 340                      if ( isset( $this->namecol ) && ( $k == $this->namecol ) ) continue;
 341  
 342                      $this->sync_mp_usermeta( $mp_user_id, $columns[$k], $v );
 343                  }
 344              }
 345          }
 346          return $i;
 347      }
 348  
 349  	function mailinglist_default( $default )
 350      {
 351          return ( isset( $this->mailinglist_ID ) ) ? $this->mailinglist_ID : $default;
 352      }
 353  }
 354  new MP_import_excel( __( 'Import your <strong>excel</strong> file.', 'MailPress' ), __( 'Import Excel file', 'MailPress' ) );


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