Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
hipo_job_tool_delete_bmail_users.inc
1 <?php
17 require_once SQ_SYSTEM_ROOT.'/core/hipo/hipo_job.inc';
18 
32 {
33 
34 
40  function __construct($code_name='')
41  {
42  $this->uses_trans = FALSE;
43  parent::__construct($code_name);
44  $GLOBALS['SQ_SYSTEM']->lm->includePackageStrings('bulkmail');
45 
46  }//end constructor
47 
48 
58  function getCodeName()
59  {
60  return parent::getCodeName().'-'.$this->_running_vars['file_info']['name'];
61 
62  }//end getCodeName()
63 
64 
71  function getHipoName()
72  {
73  return translate('bm_hipo_name_delete_bmail_user');
74 
75  }//end getHipoName()
76 
77 
85  function getInitialStepData()
86  {
87  return Array(
88  Array(
89  'name' => translate('bm_hipo_deleting_bmail_users'),
90  'function_call' => Array(
91  'process_function' => 'processDelete',
92  ),
93  'running_mode' => 'server',
94  'auto_step' => TRUE,
95  'percent_done' => 0,
96  'complete' => FALSE,
97  'message' => '',
98  'allow_cancel' => TRUE,
99  ),
100  );
101 
102  }//end getInitialStepData()
103 
104 
111  function freestyle()
112  {
113  while (!empty($this->_running_vars['todo_asset_users'])) {
114  if (!$this->processDelete($this->_steps[0], get_class($this))) {
115  return FALSE;
116  }
117  }
118  return TRUE;
119 
120  }//end freestyle()
121 
122 
129  function prepare()
130  {
131  // check if email list supplied, if not look for file
132  if (!isset($this->_running_vars['email_list'])) {
133  // import the csv file
134  require_once SQ_FUDGE_PATH.'/csv/csv.inc';
135  $csv = new CSV($this->_running_vars['file_info']['tmp_name']);
136  $csv->import();
137 
138  // add users into the 'todo' array
139  $i = 0;
140  foreach ($csv->values as $line_value) {
141  $this->_running_vars['todo_asset_users'][$i]['email'] = trim($line_value[0]);
142  $i++;
143  }
144  } else {
145  $emails = $this->_running_vars['email_list'];
146  $i = 0;
147  foreach ($emails as $email) {
148  $this->_running_vars['todo_asset_users'][$i]['email'] = $email;
149  $i++;
150  }
151  }
152  $this->_running_vars['done_asset_users'] = Array();
153  return parent::prepare();
154 
155  }//end prepare()
156 
157 
167  function processDelete(&$step_data, $prefix)
168  {
169  if (!empty($this->_running_vars['todo_asset_users'])) {
170 
171  // import one bmail user
172  $am = $GLOBALS['SQ_SYSTEM']->am;
173  $am->includeAsset('bulkmail_user');
174  $index = array_shift(array_keys($this->_running_vars['todo_asset_users']));
175  $email = $this->_running_vars['todo_asset_users'][$index]['email'];
176 
177  // find the existing bmail user, and link here
178  $user = $this->_getExistingBmailUser($email);
179  if ($user != NULL) {
180  if (empty($this->_running_vars['root_nodes'])) {
181  // If no root node specified big delete
182  $am->purgeAsset($user->id);
183  $step_data['message'] = "Deleting Bulkmail User \"$user->name\" (#$user->id)";
184  } else {
185  // Otherwise delete by root node
186  $root_nodes = $this->_running_vars['root_nodes'];
187  for (reset($root_nodes); NULL !== ($k = key($root_nodes)); next($root_nodes)) {
188  $root_nodes[$k] = MatrixDAL::quote((string) $root_nodes[$k]);
189  }//end for
190  $treeid_sql = 'SELECT rt.treeid AS treeid FROM sq_ast_lnk rl INNER JOIN sq_ast_lnk_tree rt ON rl.linkid=rt.linkid WHERE rl.minorid IN ('.implode(',', $root_nodes).')';
191  $treeid_result = NULL;
192  try {
193  $treeid_query = MatrixDAL::preparePdoQuery($treeid_sql);
194  $treeid_result = MatrixDAL::executePdoAssoc($treeid_query);
195  } catch (Exception $e) {
196  throw new Exception($e->getMessage());
197  }//end try-catch
198 
199  $under_tree = '';
200  if (!empty($treeid_result)) {
201  $under_tree .= '(';
202  $bind_vars = Array();
203  foreach ($treeid_result as $treeid_index => $each_treeid) {
204  if (!empty($each_treeid['treeid'])) {
205  $bind_vars['treeid_'.$treeid_index] = $each_treeid['treeid'].'%';
206  if (!empty($treeid_index)) $under_tree .= ' OR ';
207  $under_tree .= 't.treeid LIKE :treeid_'.$treeid_index;
208  }//end if
209  }//end foreach
210  $under_tree .= ')';
211  }//end if
212 
213  // Something went wrong :(
214  if (!empty($under_tree)) {
215  $db = MatrixDAL::getDb();
216  $sql = Array(
217  'select' => 'SELECT l.linkid',
218  'from' => 'FROM sq_ast_lnk l',
219  'join' => 'INNER JOIN sq_ast_lnk_tree t ON l.linkid=t.linkid',
220  'where' => 'WHERE l.minorid=:bulkmail_user_id',
221  );
222  $sql2 = Array(
223  'select' => 'SELECT l.linkid',
224  'from' => 'FROM sq_ast_lnk l',
225  'join' => 'INNER JOIN sq_ast_lnk_tree t ON l.linkid=t.linkid',
226  'where' => 'WHERE l.minorid=:bulkmail_user_id AND '.$under_tree,
227  );
228  $result_all_links = NULL;
229  $result_wanted_links = NULL;
230  try {
231  $query = MatrixDAL::preparePdoQuery(implode(' ', $sql));
232  $query2 = MatrixDAL::preparePdoQuery(implode(' ', $sql2));
233  MatrixDAL::bindValueToPdo($query, 'bulkmail_user_id', $user->id);
234  MatrixDAL::bindValueToPdo($query2, 'bulkmail_user_id', $user->id);
235  foreach ($bind_vars as $bind_name => $bind_value) {
236  MatrixDAL::bindValueToPdo($query2, $bind_name, $bind_value);
237  }//end foreach
238  $result_all_links = MatrixDAL::executePdoAssoc($query);
239  $result_wanted_links = MatrixDAL::executePdoAssoc($query2);
240  } catch (Exception $e) {
241  throw new Exception($e->getMessage());
242  }//end try-catch
243 
244  if (!empty($result_all_links) && !empty($result_wanted_links)) {
245  // Cleaning up the array to make easier to work with
246  $all_links = Array();
247  foreach ($result_all_links as $link_info) {
248  if (!empty($link_info['linkid'])) {
249  $all_links[] = $link_info['linkid'];
250  }//end if
251  }//end foreach
252  $wanted_links = Array();
253  foreach ($result_wanted_links as $wanted_link_info) {
254  if (!empty($wanted_link_info['linkid'])) {
255  $wanted_links[] = $wanted_link_info['linkid'];
256  }//end if
257  }//end foreach
258 
259  // check if we are going to link in diffrent place or delete
260  if (!empty($this->_running_vars['link_asset']['assetid'])) {
261  $to_parentid = $this->_running_vars['link_asset']['assetid'];
262  $link_type = $this->_running_vars['link_asset']['link_type'];
263  foreach ($wanted_links as $linkid) {
264  $am->moveLink($linkid, $to_parentid, $link_type, -1);
265  }
266  $step_data['message'] = "Moving Bulkmail User \"$user->name\" (#$user->id)";
267  } else {
268  // Check if this is the last link or going to be
269  if ((count($all_links) == 1) || (count($wanted_links) == count($all_links))) {
270  // Last link, so delete
271  $am->purgeAsset($user->id);
272  $step_data['message'] = "Deleting Bulkmail User \"$user->name\" (#$user->id)";
273  } else {
274  // Otherwise just delete the requested linkids
275  foreach ($wanted_links as $linkid) {
276  $am->deleteAssetLink($linkid);
277  }//end foreach
278  $step_data['message'] = "Deleting Bulkmail User \"$user->name\" (#$user->id)";
279  }//end if
280  }//end else
281  }//end if
282  }//end if
283  }//end else
284  }//end if
285 
286  $step_data['message'] = translate('bm_deleting_bmail_user', $email);
287 
288  // add this user to the done array
289  $this->_running_vars['done_asset_users'][] = $email;
290 
291  // clean up
292  unset($this->_running_vars['todo_asset_users'][$index]);
293  $am->forgetAsset($user);
294 
295  }//end if not empty todo
296 
297  // update progress
298  if (empty($this->_running_vars['todo_asset_users'])) {
299  $step_data['percent_done'] = 100;
300  $step_data['complete'] = TRUE;
301  } else {
302  $total = count($this->_running_vars['todo_asset_users']) + count($this->_running_vars['done_asset_users']);
303  $step_data['percent_done'] =(count($this->_running_vars['done_asset_users']) / $total) * 100;
304  $step_data['complete'] = FALSE;
305  }
306 
307  return TRUE;
308 
309  }//end processDelete()
310 
311 
322  function &_getExistingBmailUser($email)
323  {
324  try {
325  $bind_vars['email'] = $email;
326  $assetid = MatrixDAL::executeOne('bulkmail_package', 'getExistingBmailUser', $bind_vars);
327  } catch (Exception $e) {
328  throw new Exception("Unable to get existing bulkmail user due to database error: ".$e->getMessage());
329  }
330 
331  $user = NULL;
332  if (!is_null($assetid) && !empty($assetid)) {
333  $user = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
334  }
335 
336  return $user;
337 
338  }//end _getExistingBmailUser()
339 
340 
341 }//end class
342 
343 ?>