[ Index ]

MailPress 544

[ 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($step = 0) 
   7      {
   8          if (isset($_GET['step']) && !empty($_GET['step'])) $step = (int) $_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)', MP_TXTDOM) . '</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($_GET['id'], $_GET['sid']);
  54              break;
  55              case 3:
  56                  $this->start_trace($step);
  57                  $import = $this->import( $_GET['id'], $_GET['sid'] );
  58                  $this->end_trace(true);
  59                  if ($import)
  60                      $this->success('<p>' . sprintf(__("<b>Number of records</b> : <i>%s</i>", MP_TXTDOM), $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($_GET['step'], false);
  93          }
  94  ?>
  95      <h3><?php _e('File scan', MP_TXTDOM); ?></h3>
  96      <p><?php _e("Here are the sheets found in the excel file, please select one", MP_TXTDOM); ?></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 class='button-primary' type='submit' 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($_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 (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($_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($_GET['step'], false);
 165          }
 166  
 167          $this->emailcol = (isset($_GET['col'])) ? $_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_filter('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', MP_TXTDOM); ?></h3>
 179          <p><?php _e('Optional, you can import the MailPress users in a specific mailing list ...', MP_TXTDOM); ?></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', MP_TXTDOM))); ?><?php endif; ?>
 181  <?php         if (class_exists('MailPress_newsletter')) : ?>
 182          <h3><?php _e('Newsletter', MP_TXTDOM); ?></h3>
 183          <p>
 184              <input type='checkbox' name='no_newsletter' id='no_newsletter' />
 185              <?php _e('<b>Delete</b> all subscriptions.', MP_TXTDOM); ?>
 186          </p>
 187          <p>
 188              <input type='checkbox' name='newsletter' id='newsletter' /> 
 189              <?php _e('<b>Add</b> default subscriptions.', MP_TXTDOM); ?>
 190          </p>
 191  <?php         endif; ?>
 192          <h3><?php _e('File scan', MP_TXTDOM); ?></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>'.", MP_TXTDOM), $columns[$this->emailcol]);
 196              echo '&nbsp;';
 197              _e('However, you can select another column.<br /> Invalid emails will not be inserted.', MP_TXTDOM); 
 198  ?>
 199          </p>
 200          <table class='widefat'>
 201              <thead>
 202                  <tr>
 203                      <td style='width:auto;'><?php _e('Choose email column', MP_TXTDOM); ?></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 if ($k == $this->emailcol) echo "checked='checked'"; ?> /><span><?php echo $v; ?></span></td>
 211  <?php
 212                  $i++;
 213              }
 214  ?>
 215                  </tr>
 216                  <tr>
 217                      <td><?php _e('Choose name column', MP_TXTDOM); ?></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 (!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', MP_TXTDOM), 
 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($_GET['step'], false);
 293          }
 294  
 295          $this->emailcol = $_POST['is_email'];
 296          $this->namecol  = $_POST['is_name'];
 297  
 298          if (class_exists('MailPress_mailinglist'))
 299          {
 300              $mailinglist_ok = ('-1' != $_POST['mailinglist']);
 301              if ($mailinglist_ok)
 302              {
 303                  $this->mailinglist_ID = $_POST['mailinglist'];
 304                  add_filter('MailPress_mailinglist_default', array($this, 'mailinglist_default'), 8, 1);
 305  
 306                  $mailinglist_name = MP_Mailinglist::get_name($this->mailinglist_ID);
 307              }
 308          }
 309  
 310          if (class_exists('MailPress_newsletter'))
 311          {
 312              $no_newsletter_ok = isset($_POST['no_newsletter']);
 313              $newsletter_ok     = isset($_POST['newsletter']);
 314          }
 315  
 316          $i = 0;
 317          $columns = array_shift($this->excel->sheets[$sid]['cells']);
 318          foreach ($this->excel->sheets[$sid]['cells'] as $r => $row)
 319          {
 320              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;}
 321  
 322              $i++;
 323  
 324              $curremail = trim(strtolower($row[$this->emailcol]));
 325              $currname  = trim($row[$this->namecol]);
 326              $mp_user_id = $this->sync_mp_user($curremail, $currname);
 327  
 328              if ($mp_user_id)
 329              {
 330                  if (isset($mailinglist_ok) && $mailinglist_ok)
 331                  {
 332                      $this->sync_mp_user_mailinglist($mp_user_id, $this->mailinglist_ID, $curremail, $mailinglist_name);
 333                  }
 334                  if ($no_newsletter_ok)
 335                  {
 336                      $this->sync_mp_user_no_newsletter($mp_user_id);
 337                  }
 338                  if ($newsletter_ok)
 339                  {
 340                      $this->sync_mp_user_newsletter($mp_user_id);
 341                  }
 342  
 343                  foreach ($row as $k => $v)
 344                  {
 345                      if ($k == $this->emailcol) continue;
 346                      if ($k == $this->namecol) continue;
 347  
 348                      $this->sync_mp_usermeta($mp_user_id, $columns[$k], $v);
 349                  }
 350              }
 351          }
 352          return $i;
 353      }
 354  
 355  	function mailinglist_default($default)
 356      {
 357          return (isset($this->mailinglist_ID)) ? $this->mailinglist_ID : $default;
 358      }
 359  }
 360  new MP_import_excel(__('Import your <strong>excel</strong> file.', MP_TXTDOM), __('Import Excel file', MP_TXTDOM));


Generated: Thu Apr 28 18:38:52 2016 Cross-referenced by PHPXref 0.7.1