Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
hipo_job_tool_import_bmail_user.inc
1 <?php
17 require_once SQ_SYSTEM_ROOT.'/core/hipo/hipo_job.inc';
18 
34 {
35 
36 
42  function __construct($code_name='')
43  {
44  $this->uses_trans = FALSE;
45  parent::__construct($code_name);
46  $GLOBALS['SQ_SYSTEM']->lm->includePackageStrings('bulkmail');
47 
48  }//end constructor
49 
50 
60  public static function paintConfig(&$o, $class, $write_access)
61  {
62  $o->openField(translate('bm_tool_import_bmail_user_hipo_job_import_threshold'));
63 
64  if ($write_access) {
65  text_box($class.'[SQ_HIPO_BULKMAIL_USER_IMPORT_THRESHOLD]', SQ_HIPO_BULKMAIL_USER_IMPORT_THRESHOLD, 5);
66  } else {
67  echo SQ_HIPO_BULKMAIL_USER_IMPORT_THRESHOLD;
68  }
69  echo ' '.translate('assets');
70 
71  $o->closeField();
72 
73  }//end paintConfig()
74 
75 
82  public static function getConfigVars()
83  {
84  return Array(
85  'SQ_HIPO_BULKMAIL_USER_IMPORT_THRESHOLD' => Array('editable' => 1, 'default' => 5),
86  );
87 
88  }//end getConfigVars()
89 
90 
101  {
102  if (SQ_HIPO_BULKMAIL_USER_IMPORT_THRESHOLD == 0) return 0;
103  if (!isset($this->_running_vars['todo_asset_users'])) {
104  return 0;
105  }
106  $user_count = count($this->_running_vars['todo_asset_users']);
107 
108  return (($user_count / SQ_HIPO_BULKMAIL_USER_IMPORT_THRESHOLD) * 100);
109 
110  }//end getThresholdPercentageRequired()
111 
112 
122  function getCodeName()
123  {
124  return parent::getCodeName().'-'.$this->_running_vars['file_info']['name'];
125 
126  }//end getCodeName()
127 
128 
135  function getHipoName()
136  {
137  return translate('bm_hipo_name_import_bmail_user');
138 
139  }//end getHipoName()
140 
141 
150  {
151  return Array(
152  Array(
153  'name' => translate('bm_hipo_importing_bmail_users'),
154  'function_call' => Array(
155  'process_function' => 'processImport',
156  ),
157  'running_mode' => 'server',
158  'auto_step' => TRUE,
159  'percent_done' => 0,
160  'complete' => FALSE,
161  'message' => '',
162  'allow_cancel' => TRUE,
163  ),
164  );
165 
166  }//end getInitialStepData()
167 
168 
175  function freestyle()
176  {
177  while (!empty($this->_running_vars['todo_asset_users'])) {
178  if (!$this->processImport($this->_steps[0], get_class($this))) {
179  return FALSE;
180  }
181  }
182  return TRUE;
183 
184  }//end freestyle()
185 
186 
193  function prepare()
194  {
195  // import the csv file
196  require_once SQ_FUDGE_PATH.'/csv/csv.inc';
197  $csv = new CSV($this->_running_vars['file_info']['tmp_name']);
198 
199  $csv_start = 1;
200  if (isset($this->_running_vars['csv_header']) && $this->_running_vars['csv_header'] == TRUE) {
201  $csv_start = 2;
202  }
203  $csv->import($csv_start);
204 
205  // add users into the 'todo' array
206  $i = 0;
207  foreach ($csv->values as $line_value) {
208  $this->_running_vars['todo_asset_users'][$i]['name'] = trim($line_value[0]);
209  $this->_running_vars['todo_asset_users'][$i]['email'] = trim($line_value[1]);
210  $i++;
211  }
212  $this->_running_vars['done_asset_users'] = Array();
213  return parent::prepare();
214 
215  }//end prepare()
216 
217 
227  function processImport(&$step_data, $prefix)
228  {
229  if (!empty($this->_running_vars['todo_asset_users'])) {
230 
231  // import one bmail user
232  $GLOBALS['SQ_SYSTEM']->am->includeAsset('bulkmail_user');
233  $array_keys = array_keys($this->_running_vars['todo_asset_users']);
234  $index = array_shift($array_keys);
235  $create_in = $GLOBALS['SQ_SYSTEM']->am->getAsset($this->_running_vars['create_in_assetid']);
236  $name = $this->_running_vars['todo_asset_users'][$index]['name'];
237  $email = $this->_running_vars['todo_asset_users'][$index]['email'];
238 
239  $user = new Bulkmail_User();
240  $user->setAttrValue('name', $name);
241 
242  // turn off error reporting if we are linking user with same address
243  if ($this->_running_vars['new_link']) {
244  $old_reporting = error_reporting(0);
245  }
246  $status = $user->setAttrValue('email', $email);
247  if ($this->_running_vars['new_link']) {
248  error_reporting($old_reporting);
249  if (!$status) {
250  // find the existing bmail user, and link here
251  $user = $this->_getExistingBmailUser($email);
252  if ($name != $user->name) {
253  trigger_error("(Bulkmail User #$user->id) \"$name\" does not match existing name \"$user->name\"");
254  $step_data['message'] = "Skipping \"$user->name\" (#$user->id)";
255  } else {
256  $create_in->createLink($user, SQ_LINK_TYPE_1);
257  $step_data['message'] = "Linking \"$user->name\" (#$user->id)";
258  }
259  }
260  }
261 
262  // create new user as usual
263  if ($status) {
264  $link = Array(
265  'asset' => $create_in,
266  'link_type' => SQ_LINK_TYPE_1,
267  );
268  $status = $user->create($link);
269  }
270  $step_data['message'] = translate('bm_importing_bmail_user', $email);
271 
272  // add this user to the done array
273  $this->_running_vars['done_asset_users'][] = $email;
274 
275  // clean up
276  unset($this->_running_vars['todo_asset_users'][$index]);
277  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($user);
278  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($create_in);
279 
280  }//end if not empty todo
281 
282  // update progress
283  if (empty($this->_running_vars['todo_asset_users'])) {
284  $step_data['percent_done'] = 100;
285  $step_data['complete'] = TRUE;
286  } else {
287  $total = count($this->_running_vars['todo_asset_users']) + count($this->_running_vars['done_asset_users']);
288  $step_data['percent_done'] =(count($this->_running_vars['done_asset_users']) / $total) * 100;
289  $step_data['complete'] = FALSE;
290  }
291 
292  return TRUE;
293 
294  }//end processImport()
295 
296 
307  function &_getExistingBmailUser($email)
308  {
309  try {
310  $bind_vars['email'] = $email;
311  $assetid = MatrixDAL::executeOne('bulkmail_package', 'getExistingBmailUser', $bind_vars);
312  } catch (Exception $e) {
313  throw new Exception('Unable to get existing bulkmail user due to database error: '.$e->getMessage());
314  }
315 
316  $user = NULL;
317  if (!is_null($assetid)) {
318  $user = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
319  }
320 
321  return $user;
322 
323  }//end _getExistingBmailUser()
324 
325 
326 }//end class
327 
328 
329 ?>