[ Index ]

MailPress 544

[ 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($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                      $this->end_trace(true);
  23                      if ($sniff)
  24                          $this->fileform();
  25                      else
  26                          $this->error('<p><strong>' . __('Unable to determine email location', MP_TXTDOM) . '</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( $_GET['id'] );
  37                  $this->end_trace(true);
  38                  if ($import)
  39                      $this->success('<p>' . sprintf(__("<b>File imported</b> : <i>%s</i>", MP_TXTDOM), $this->file) . '</p><p><strong>' . sprintf(__("<b>Number of records</b> : <i>%s</i>", MP_TXTDOM), $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 (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_filter('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', MP_TXTDOM); ?></h3>
  97          <p><?php _e('Optional, you can import the MailPress users in a specific mailing list ...', MP_TXTDOM); ?></p>
  98  <?php            MP_Mailinglist::dropdown(array('htmlname' => 'mailinglist', 'htmlid' => 'mailinglist', 'hierarchical' => true, 'orderby' => 'name', 'hide_empty' => '0', 'show_option_none' => __('Choose mailinglist', MP_TXTDOM))); ?>
  99  <?php endif; ?>
 100  <?php if (class_exists('MailPress_newsletter')) : ?>
 101          <h3><?php _e('Newsletter', MP_TXTDOM); ?></h3>
 102          <p>
 103              <input type='checkbox' name='no_newsletter' id='no_newsletter' />
 104              <?php _e('<b>Delete</b> all subscriptions.', MP_TXTDOM); ?>
 105          </p>
 106          <p>
 107              <input type='checkbox' name='newsletter' id='newsletter' /> 
 108              <?php _e('<b>Add</b> default subscriptions.', MP_TXTDOM); ?>
 109          </p>
 110  <?php endif; ?>
 111          <h3><?php _e('File scan', MP_TXTDOM); ?></h3>
 112          <p><?php printf(__("On the first records (see hereunder), the file scan found that the email is in column '<strong>%s</strong>'.", MP_TXTDOM), $this->emailcol); ?>
 113          <?php _e('However, you can select another column.<br /> Invalid emails will not be inserted.', MP_TXTDOM); ?></p>
 114          <table class='widefat'>
 115              <thead>
 116                  <tr>
 117                      <td style='width:auto;'><?php _e('Choose email column', MP_TXTDOM); ?></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', MP_TXTDOM); ?></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 (!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 class='button-primary' type='submit' 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', MP_TXTDOM), 
 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 = $_POST['is_email'];
 205          $this->namecol  = $_POST['is_name'];
 206  
 207          if (class_exists('MailPress_mailinglist'))
 208          {
 209              $mailinglist_ok = ('-1' != $_POST['mailinglist']);
 210              if ($mailinglist_ok)
 211              {
 212                  $this->mailinglist_ID = $_POST['mailinglist'];
 213                  add_filter('MailPress_mailinglist_default', array($this, 'mailinglist_default'), 8, 1);
 214  
 215                  $mailinglist_name = MP_Mailinglist::get_name($this->mailinglist_ID);
 216              }
 217          }
 218  
 219          if (class_exists('MailPress_newsletter'))
 220          {
 221              $no_newsletter_ok = isset($_POST['no_newsletter']);
 222              $newsletter_ok     = isset($_POST['newsletter']);
 223          }
 224  
 225          $i = 0;
 226          foreach ($this->csv->data as $row)
 227          {
 228              $i++;
 229  
 230              $curremail = trim(strtolower($row[$this->emailcol]));
 231              $currname  = trim($row[$this->namecol]);
 232              $mp_user_id = $this->sync_mp_user($curremail, $currname);
 233  
 234              if ($mp_user_id)
 235              {
 236                  if (isset($mailinglist_ok) && $mailinglist_ok)
 237                  {
 238                      $this->sync_mp_user_mailinglist($mp_user_id, $this->mailinglist_ID, $curremail, $mailinglist_name);
 239                  }
 240                  if ($no_newsletter_ok)
 241                  {
 242                      $this->sync_mp_user_no_newsletter($mp_user_id);
 243                  }
 244                  if ($newsletter_ok)
 245                  {
 246                      $this->sync_mp_user_newsletter($mp_user_id);
 247                  }
 248  
 249                  foreach ($row as $k => $v)
 250                  {
 251                      if ($k == $this->emailcol) continue;
 252                      if ($k == $this->namecol) continue;
 253  
 254                      $this->sync_mp_usermeta($mp_user_id, $k, $v);
 255                  }
 256              }
 257          }
 258          return $i;
 259      }
 260  
 261  	function mailinglist_default($default)
 262      {
 263          return (isset($this->mailinglist_ID)) ? $this->mailinglist_ID : $default;
 264      }
 265  }
 266  new MP_import_csv(__('Import your <strong>csv</strong> file.', MP_TXTDOM), __('Import Csv', MP_TXTDOM));


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