dolibarr  7.0.0-beta
xinputfile.modules.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2005-2012 Laurent Destailleur <eldy@users.sourceforge.net>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  * or see http://www.gnu.org/
17  */
18 
24 include_once DOL_DOCUMENT_ROOT.'/core/modules/mailings/modules_mailings.php';
25 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
26 
27 
32 {
33  var $name='EmailsFromFile'; // Identifiant du module mailing
34  // This label is used if no translation is found for key XXX neither MailingModuleDescXXX where XXX=name is found
35  var $desc='EMails from a file'; // Libelle utilise si aucune traduction pour MailingModuleDescXXX ou XXX=name trouv�e
36  var $require_module=array(); // Module mailing actif si modules require_module actifs
37  var $require_admin=0; // Module mailing actif pour user admin ou non
38  var $picto='generic';
39  var $tooltip='UseFormatFileEmailToTarget';
40 
41 
47  function __construct($db)
48  {
49  $this->db=$db;
50  }
51 
52 
62  {
63  global $langs;
64  $langs->load("users");
65 
66  $statssql=array();
67  return $statssql;
68  }
69 
70 
79  function getNbOfRecipients($sql='')
80  {
81  return '';
82  }
83 
84 
91  function url($id)
92  {
93  global $langs;
94  return $langs->trans('LineInFile',$id);
95  //' - '.$langs->trans("File").' '.dol_trunc(,12);
96  }
97 
98 
104  function formFilter()
105  {
106  global $langs;
107 
108  $s='';
109  $s.='<input type="file" name="username" class="flat">';
110  return $s;
111  }
112 
120  function add_to_target($mailing_id,$filtersarray=array())
121  {
122  global $conf,$langs,$_FILES;
123 
124  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
125 
126  // For compatibility with Unix, MS-Dos or Macintosh
127  ini_set('auto_detect_line_endings', true);
128 
129  $cibles = array();
130 
131  $upload_dir=$conf->mailing->dir_temp;
132 
133  if (dol_mkdir($upload_dir) >= 0)
134  {
135  $resupload = dol_move_uploaded_file($_FILES['username']['tmp_name'], $upload_dir . "/" . $_FILES['username']['name'], 1, 0, $_FILES['username']['error']);
136  if (is_numeric($resupload) && $resupload > 0)
137  {
138  $cpt=0;
139 
140  $file=$upload_dir . "/" . $_FILES['username']['name'];
141  $handle = @fopen($file, "r");
142  if ($handle)
143  {
144  $i = 0;
145  $j = 0;
146 
147  $old = '';
148  while (!feof($handle))
149  {
150  $cpt++;
151  $buffer = trim(fgets($handle));
152  $tab=explode(';',$buffer,4);
153  $email=$tab[0];
154  $name=$tab[1];
155  $firstname=$tab[2];
156  $other=$tab[3];
157  if (! empty($buffer))
158  {
159  //print 'xx'.dol_strlen($buffer).empty($buffer)."<br>\n";
160  $id=$cpt;
161  if (isValidEMail($email))
162  {
163  if ($old <> $email)
164  {
165  $cibles[$j] = array(
166  'email' => $email,
167  'lastname' => $name,
168  'firstname' => $firstname,
169  'other' => $other,
170  'source_url' => '',
171  'source_id' => '',
172  'source_type' => 'file'
173  );
174  $old = $email;
175  $j++;
176  }
177  }
178  else
179  {
180  $i++;
181  $langs->load("errors");
182  $this->error = $langs->trans("ErrorFoundBadEmailInFile",$i,$cpt,$email);
183  }
184  }
185  }
186  fclose($handle);
187 
188  if ($i > 0)
189  {
190  return -$i;
191  }
192  }
193  else
194  {
195  $this->error = $langs->trans("ErrorFaildToOpenFile");
196  return -1;
197  }
198 
199  dol_syslog(get_class($this)."::add_to_target mailing ".$cpt." targets found");
200  }
201  else
202  {
203  $langs->load("errors");
204  if ($resupload < 0) // Unknown error
205  {
206  $this->error = '<div class="error">'.$langs->trans("ErrorFileNotUploaded").'</div>';
207  }
208  else if (preg_match('/ErrorFileIsInfectedWithAVirus/',$resupload)) // Files infected by a virus
209  {
210  $this->error = '<div class="error">'.$langs->trans("ErrorFileIsInfectedWithAVirus").'</div>';
211  }
212  else // Known error
213  {
214  $this->error = '<div class="error">'.$langs->trans($resupload).'</div>';
215  }
216  }
217  }
218 
219  ini_set('auto_detect_line_endings', false);
220 
221  return parent::add_to_target($mailing_id, $cibles);
222  }
223 
224 }
225 
formFilter()
Affiche formulaire de filtre qui apparait dans page de selection des destinataires de mailings...
dol_move_uploaded_file($src_file, $dest_file, $allowoverwrite, $disablevirusscan=0, $uploaderrorcode=0, $nohook=0, $varfiles='addedfile')
Make control on an uploaded file from an GUI page and move it to final destination.
Definition: files.lib.php:962
Class to offer a selector of emailing targets with Rule 'xinputfile'.
getSqlArrayForStats()
On the main mailing area, there is a box with statistics.
add_to_target($mailing_id, $filtersarray=array())
Ajoute destinataires dans table des cibles.
__construct($db)
Constructor.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
dol_mkdir($dir, $dataroot='', $newmask=null)
Creation of a directory (this can create recursive subdir)
url($id)
Renvoie url lien vers fiche de la source du destinataire du mailing.
getNbOfRecipients($sql='')
Return here number of distinct emails returned by your selector.
Parent class of emailing target selectors modules.