Odyssey
billingEmailFunctions.i
1 <?php
2 /* File: functions.php
3  * Purpose: There are multiple functions here that help out data.php.
4  */
5 
6 function getCCWhenDone() {
7  return array("phil");
8 }
9 
10 function GetImageLocation() {
11  return "/home/homecu/images/logo.png";
12 }
13 
14 function GetIsDev() {
15  global $SYSENV; // Where this is used is so deep. I don't want to add another parameter all the way up (5+ levels.)
16  return $SYSENV["devmode"] == 1;
17 }
18 
19 function GetEditPath() {
20  $editFile = "cuissues_edit.prg";
21  // I feel like this is very safe. I was considering getting the domain from $_SERVER["SCRIPT_URI"] but I'm not sure if that is safe.
22  $editPath = GetIsDev() ? "http://localhost:8000/hcuadm/$editFile" : "https://my.homecu.net/hcuadm/$editFile";
23  return $editPath;
24 }
25 
26 /**
27  * function setupWorkflow($dbh, $productId, $productText, $featureDetailId, $cu, $cuName, &$errors, &$transactionSQLs, $staffId, &$taskEmails, &$issueEmails)
28  * This sets up the workflow
29  *
30  * @param integer $dbh -- the database connection
31  * @param string $productId -- the product id from cuprodlist
32  * @param string $productText -- the product name from cuprodlist
33  * @param integer $featureDetailId -- the feature detail id
34  * @param string $cu -- the credit union id
35  * @param string $cuName -- the credit union name
36  * @param array $errors -- the errors encountered
37  * @param array $transactionSQLs -- the transaction SQLs (creates, updates, and deletes that will be done together)
38  * @param string $staffId -- the logged in user
39  * @param array $taskEmails -- the task emails to be sent out (compile a list and if the transaction doesn't fail, send them.)
40  * @param array $issueEmails -- the issue emails to be sent out (compile a list and if the transaction doesn't fail, send them.)
41  * @return {true|false} -- true if an workflow was created or will be created
42  */
43 function SetupWorkflow($dbh, $productId, $productText, $featureDetailId, $cu, $cuName, $staffId) {
44  try {
45  $workflowCreated = false;
46  $taskEmails = array();
47  $issueEmails = array();
48 
49  $cu = strtolower($cu);
50  $sql = "select 'FOUND' as found from cutrack where billing_feature_id = $featureDetailId";
51  $sth = db_query($sql, $dbh);
52  if (!$sth) {
53  throw new exception("Found query failed.", 1);
54  }
55  if (db_num_rows($sth) > 0) {
56  throw new exception("Workflow already exists.", 2);
57  }
58 
59  $sql = "select nextval('cutrackitem_trackitem_id_seq'::text::regclass) as next_item, nextval('cutrack_track_id_seq'::text::regclass) as next_track";
60  $sth = db_query($sql, $dbh);
61  if (!$sth) {
62  throw new exception("nextval query failed.", 4);
63  }
64  $row = db_fetch_row($sth, 0);
65  $nextTaskId = intval($row[0]);
66  $issueId = intval($row[1]);
67  $issueName = "";
68 
69  $sql = "select i.issue_id, i.code, i.owner, ii.item_id, ii.title, ii.item_desc, ii.view_order, ii.owner as task_owner, ii.parent, ii.sales_item_id
70  from cuissues i
71  left join cuissuesitem ii on i.issue_id = ii.issue_id
72  where i.gen_for_product = '$productId'
73  order by i.issue_id";
74  $sthList = db_query($sql, $dbh);
75  if (!$sthList) {
76  throw new exception("Issue select failed.", 3);
77  }
78  $issueResults = array();
79 
80  $insertIssueSQL = "insert into cutrack
81  (track_id, issue, assigned_to, billing_feature_id, previous_task_owner, user_name, status, target_date_type, entry_date,
82  last_activity_date, issue_id)";
83  $insertTasksSQL = "insert into cutrackitem (trackitem_id, track_id, title, item_desc, view_order, parent, assigned_to, issue_item_id, billing_item_id)";
84 
85  $taskSQLPartArray = array();
86  $subTaskArray = array();
87  $hasTasks = false;
88  $parentTaskId = $nextTaskId;
89 
90  $first = true;
91  $parentIMap = array();
92  $parentI = 0;
93  for($i = 0; $row = db_fetch_assoc($sthList, $i); $i++) {
94  if ($first) {
95  $issueOwner = isset($row["owner"]) ? trim($row["owner"]) : $staffId;
96  $templateIssueId = isset($row["issue_id"]) ? trim($row["issue_id"]) : "";
97  $taskOwner = isset($row["task_owner"]) ? trim($row["task_owner"]) : "";
98  $issue = isset($row["code"]) ? trim($row["code"]) : "";
99  $issueName = $issue;
100  $insertIssueSQL .= " values ($issueId, '" . prep_save($issue, 90) . "', '" . prep_save($issueOwner, 30) . "', $featureDetailId,
101  '" . prep_save($taskOwner, 12) . "', '" . prep_save($cu, 12) . "', 'Active', 'TBD', now(), now(), $templateIssueId)";
102  $sth = db_query($insertIssueSQL, $dbh);
103  if (!$sth) {
104  throw new exception("issue insert failed.", 5);
105  }
106 
107  $first = false;
108  }
109 
110  $taskId = isset($row["item_id"]) ? trim($row["item_id"]) : "";
111 
112  if($taskId != "" && $templateIssueId == $row["issue_id"]) { // Ensure that tasks created belong to the issue created.
113  $title = isset($row["title"]) ? trim($row["title"]) : "";
114  $description = isset($row["item_desc"]) ? trim($row["item_desc"]) : "";
115  $viewOrder = isset($row["view_order"]) ? intval($row["view_order"]) : 0;
116  $owner = isset($row["task_owner"]) ? trim($row["task_owner"]) : "";
117  $parent = isset($row["parent"]) ? intval($row["parent"]) : 0;
118  $taskTemplateId = isset($row["item_id"]) ? intval($row["item_id"]) : 0;
119  $billingItemId = isset($row["sales_item_id"]) ? intval($row["sales_item_id"]) : 0;
120 
121  if ($billingItemId != 0) {
122  $sql = "select fd.id from cubillfeaturedetail fd
123  inner join cubillfeaturedetail wfd
124  on fd.sales_order_detail_id = wfd.sales_order_detail_id and wfd.id = $featureDetailId and fd.billing_status= 1
125  and fd.sales_item_id = $billingItemId and (fd.workflow_completed is null or fd.workflow_completed = 'N')
126  order by id desc";
127  $sth = db_query($sql, $dbh);
128  if (!$sth) {
129  throw new exception("Billing item id query failed.", 6);
130  }
131  $billingItemId = db_fetch_row($sth, 0);
132  $billingItemId = intval($billingItemId[0]);
133  $row["sales_item_id"] = $billingItemId; // Save this if it is a subtask.
134  }
135 
136  if ($parent == 0) {
137  $taskSQLPartArray[] = "($parentTaskId, $issueId, '" . prep_save($title, 80) . "', '" . prep_save($description) . "',
138  $viewOrder, 0, '" . prep_save($owner, 12) . "', $taskTemplateId, $billingItemId)";
139  $parentIMap[$taskTemplateId] = $parentI ++;
140  $parentTaskId ++;
141  } else {
142  if (!HCU_array_key_exists($parent, $subTaskArray)) {
143  $subTaskArray[$parent] = array();
144  }
145  $subTaskArray[$parent][] = array("description" => $description, "viewOrder" => $viewOrder, "owner" => $owner, "taskTemplateId" => $taskTemplateId,
146  "billingItemId" => $billingItemId, "title" => $title);
147  }
148  $hasTasks = true;
149  }
150 
151  $issueResults[] = $row;
152  }
153 
154  $subTaskId = $parentTaskId;
155  if ($hasTasks) {
156  $sql = "$insertTasksSQL values " . implode(", ", $taskSQLPartArray);
157  $sth = db_query($sql, $dbh);
158  if (!$sth) {
159  throw new exception("Parent task insert failed.", 8);
160  }
161 
162  $taskSQLPartArray = array();
163  foreach($subTaskArray as $parentId => $detailRecords) {
164  foreach($detailRecords as $record) {
165  $parentTaskId = $nextTaskId + $parentIMap[$parentId];
166  extract($record);
167 
168  $taskSQLPartArray[] = "($subTaskId, $issueId, '" . prep_save($title, 80) . "', '" . prep_save($description) . "', $viewOrder, $parentTaskId,
169  '" . prep_save($owner, 12) . "', $taskTemplateId, $billingItemId)";
170 
171  $subTaskId ++;
172  }
173  }
174 
175  if (count($taskSQLPartArray) > 0) { // Make sure that there are any subtasks.
176  $sql = "$insertTasksSQL values " . implode(", ", $taskSQLPartArray);
177  $sth = db_query($sql, $dbh);
178  if (!$sth) {
179  throw new exception("Sub task creation failed.", 9);
180  }
181  }
182 
183  $sql = "select setval('cutrackitem_trackitem_id_seq'::text::regclass, $subTaskId, false)";
184  // Because inserts are done with the id, sequence is not updated.
185  $sth = db_query($sql, $dbh);
186  if (!$sth) {
187  throw new exception("Setval query failed.", 10);
188  }
189  }
190 
191  if (!$first) { // means that there is an issue created so now send email.
192  $results = CreateToLine($dbh, array(trim($issueOwner)));
193  if ($results["status"] !== "000") {
194  throw new exception ($results["error"], 13);
195  }
196  $to = $results["data"];
197  $subject = "$productText job has been created.";
198  $text = "Billing contract for $cu has been set up. Workflow is set up for $productText which is assigned to you.";
199  $issueEmails[] = array("to" => $to, "subject" => $subject, "text" => $text, "issueName" => $issueName, "issueId" => $issueId);
200  $results = NotifyFirstTaskOwner($dbh, $templateIssueId, $issueId, $staffId, $issueOwner);
201  if ($results["status"] !== "000") {
202  throw new exception ($results["error"], 14);
203  }
204  $taskEmails = array_merge($taskEmails, $results["data"]["taskEmails"]);
205  $workflowCreated = true;
206  } else {
207 
208  $sql = "update cubillfeaturedetail set billing_status = case when billing_status = 5 then 5 else 4 end where id = $featureDetailId";
209  $sth = db_query($sql, $dbh);
210  if (!$sth) {
211  throw new exception("update cubillfeaturedetail query failed.", 11);
212  }
213  }
214 
215  $returnArray = array("status" => "000", "error" => "",
216  "data" => array("workflowCreated" => $workflowCreated, "taskEmails" => $taskEmails, "issueEmails" => $issueEmails, "issueId" => $issueId));
217  } catch(exception $e) {
218  $returnArray = array("status" => $e->getCode(), "error" => $e->getMessage());
219  }
220 
221  return $returnArray;
222 }
223 
224 /**
225  * function createTaskEmail($to, $subject, $text, $issue, $title, $description, $cuName, $cuCode, $issueId = 0, $taskId = 0, $text2 = "", $completedNotes = "")
226  * This creates an email for a task. This will happen when the prior task is completed or the task is reactivated or several other reasons. It will distinctly show if it is DEV or Prod.
227  *
228  * @param array $to -- contains list of receipents in the to, cc, and bcc lines.
229  * @param string $subject -- the subject of the email
230  * @param string $text -- the first line of email contents (what happened? e.g. "samuel finished his job. Yours is next.")
231  * @param string $issue -- the title of the job that the task belongs to
232  * @param string $title -- the title of the task
233  * @param string $description -- the description of the task
234  * @param string $cuName -- the credit union
235  * @param integer $issueId -- the id of the job
236  * @param integer $taskId -- the id of the task
237  * @param string $text2 -- for some messages, there is an extra line
238  * @param string $completedNotes -- the notes to tell the next task owner
239  */
240 function CreateTaskEmail($to, $subject, $text, $issue, $title, $description, $cuName, $cuCode, $issueId = 0, $taskId = 0, $text2 = "", $completedNotes = "") {
241 
242  $editPath = GetEditPath();
243  $isOnDev = GetIsDev();
244 
245  $color = $isOnDev ? "#6A1B13" : "#016103";
246 
247  $message = "<table style='border-width:0px;border-collapse:separate;'>
248  <tr><td style='border-bottom:1px solid black; margin-bottom:5px;' colspan=2>$text</td></tr>";
249  $plainMessage = $text;
250 
251  if ($text2 != "") {
252  $message .= "<tr><td colspan=2>$text2</td></tr>";
253  $plainMessage .= "\n$text2";
254  }
255 
256  $message .= "<tr><td><b>Job:</b></td><td>$issue</td></tr>
257  <tr><td><b>Task:</b></td><td>$title</td></tr>
258  <tr><td><b>Description:</b></td><td>$description</td></tr>
259  <tr><td><b>CU:</b></td><td>$cuName ($cuCode)</td></tr><tr>";
260  $plainMessage .= "\nJob:\t$issue
261  \nTask:\t$title
262  \nDescription:\t$description
263  \nCU:\t$cuName ($cuCode)";
264  if ($completedNotes != "") {
265  $message .= "<tr><td><b>Notes:</b></td><td>$completedNotes</td></tr>";
266  $plainMessage .= "\nNotes:\t$completedNotes";
267  }
268 
269  if ($taskId != 0){
270  $message .= "<td style=\"padding: 5px;\"><a href='$editPath?trackId=$issueId&trackItemId=$taskId' style=\"background-color: $color; color: #fff;
271  text-decoration: none; padding: 5px 13px; font-size: 16px; display: block;\">View Task</a></td>";
272  $plainMessage .= "\nView Task <$editPath?trackId=$issueId&trackItemId=$taskId>";
273  }
274  if ($issueId != 0) {
275  $message .= "<td style=\"padding: 5px;\"><a href='$editPath?trackId=$issueId' style=\"background-color: $color; color: #fff;
276  text-decoration: none; padding: 5px 13px; font-size: 16px; display: block;\">View Job</a></td>";
277  $plainMessage .= "\nView Job <$editPath?trackId=$issueId>";
278  }
279  $message .= "</tr></table><br><hr>";
280  CreateEmail($to, $subject, $message, $isOnDev);
281 }
282 
283 /**
284  * function createIssueEmail($to, $subject, $text, $issue, $status, $cuName, $cuCode, $issueId = 0, $text2 = "", $completedNotes = "")
285  * This creates an email for the job. This is when a job is created or is finished
286  *
287  * @param array $to -- contains list of receipents in the to, cc, and bcc lines.
288  * @param string $subject -- the subject of the email
289  * @param string $text -- the first line of email contents (what happened? e.g. "samuel finished his job. Yours is next.")
290  * @param string $issue -- the title of the job
291  * @param string $status -- the status of the job
292  * @param string $cuName -- the credit union
293  * @param integer $issueId -- the id of the job
294  * @param string $text2 -- for some messages, there is an extra line
295  * @param string $completedNotes -- the notes to tell the next task owner
296  */
297 function CreateIssueEmail($to, $subject, $text, $issue, $status, $cuName, $cuCode, $issueId = 0, $text2 = "", $completedNotes = "") {
298  $editPath = GetEditPath();
299  $isOnDev = GetIsDev();
300 
301  $color = $isOnDev ? "#6A1B13" : "#016103";
302 
303  $message = "<table style='border-width:0px;border-collapse:separate;'><colgroup><col style='width:150px'><col></colgroup>
304  <tr><td style='border-bottom:1px solid black; margin-bottom:5px;' colspan=2>$text</td></tr>";
305 
306  if ($text2 != "") {
307  $message .= "<tr><td colspan=2>$text2</td></tr>";
308  }
309 
310  $message .= "<tr><td><b>Title:</b></td><td>$issue</td></tr>
311  <tr><td><b>Status:</b></td><td>$status</td></tr>
312  <tr><td><b>CU:</b></td><td>$cuName ($cuCode)</td></tr><tr>";
313 
314  if ($completedNotes != "") {
315  $message .= "<tr><td><b>Notes:</b></td><td>$completedNotes</td></tr>";
316  }
317 
318  if ($issueId != 0) {
319  $message .= "<td style=\"padding: 5px;\"><a href='$editPath?trackId=$issueId' style=\"background-color: $color; color: #fff;
320  text-decoration: none; padding: 5px 13px; font-size: 16px; display: block;\">View Job</a></td>";
321  }
322  $message .= "</tr></table><br><hr>";
323  CreateEmail($to, $subject, $message, $isOnDev);
324 }
325 
326 /**
327  * function createEmail($to, $subject, $message, $plainMessage, $logoSource, $isOnDev)
328  * Actually creates the email with headers and whatnot and then sends it off.
329  *
330  * @param array $to -- contains list of receipents in the to, cc, and bcc lines.
331  * @param string $subject -- the subject of the email
332  * @param string $message -- the rich text version created by either the createIssueEmail or the createTaskEmail function.
333  * @param string $plainMessage -- the plain text version created by either the createIssueEmail or the createTaskEmail function.
334  * @param string $logoSource -- the source for the HomeCU logo at the bottom of the email
335  * @param boolean $isOnDev -- if it is on dev, then send it from another address so that I can divide the two.
336  */
337 function CreateEmail($to, $subject, $message, $isOnDev) {
338  $base64 = chunk_split(base64_encode(file_get_contents(GetImageLocation())));
339 
340  $msg = "$message<br><img src='data:image/png;base64, $base64' alt='HomeCU logo' />";
341 
342  $notify = new ErrorMail;
343  $notify->mailto = $to;
344  $notify->mailfrom = $isOnDev ? "billingdev@homecu.com" : "billing@homecu.com";
345  $notify->mailfromname = $isOnDev ? "Development Billing" : "Billing";
346  $notify->subject = $subject;
347  $notify->htmlMsgbody = $msg;
348  $notify->callingfunction = __FUNCTION__;
349  $notify->file = __FILE__;
350  $notify->SendMail();
351 }
352 
353 /**
354  * function createToLine($dbh, $owners, &$errors)
355  * creates the to array from the list of logins
356  *
357  * @param integer $dbh -- the database connection
358  * @param array $owners -- list of logins
359  * @param array $errors -- any errors encountered
360  * @param array $sqls -- the SQLs used
361  * @return array --
362  * $to -- emails separated by a semicolon for people on the "to" line.
363  * $cc -- emails separated by a semicolon for people on the "cc" line.
364  * $bcc -- emails separated by a semicolon for people on the "bcc" line.
365  */
366 function CreateToLine($dbh, $owners) {
367  try {
368  $to = "";
369  $cc = "";
370  $bcc = "";
371  if (count($owners) > 0) {
372 
373  foreach($owners as $i => $owner) {
374  $owners[$i] = strtolower(prep_save($owner, 12));
375  }
376 
377  $sql = "select user_name, notifyemail from dmsmonitorusers
378  where trim(user_name) in ('" . implode("', '", $owners) . "') and notifyemail is not null and trim(from notifyemail) <> ''";
379  $sth = db_query($sql, $dbh);
380  if (!$sth) {
381  throw new exception("createToLine query failed.", 1);
382  }
383 
384  $toArray = array();
385 
386  for($i = 0; $row = db_fetch_assoc($sth, $i); $i++) {
387  $username = isset($row["user_name"]) ? trim($row["user_name"]) : "";
388  $email = isset($row["notifyemail"]) ? trim($row["notifyemail"]) : "";
389  $thisRecord = "$username <$email>";
390  $toArray[] = $thisRecord;
391  }
392  }
393 
394  $returnArray = array("status" => "000", "error" => "", "data" => $toArray);
395  } catch (exception $e) {
396  $returnArray = array("status" => $e->getCode(), "error" => $e->getMessage());
397  }
398 
399  return $returnArray;
400 }
401 
402 function NotifyFirstTaskOwner($dbh, $templateId, $issueId, $staffId, $issueOwnerId) {
403  try {
404  $staffId = trim($staffId);
405  $issueOwnerId = trim($issueOwnerId);
406 
407  $taskEmails = array();
408  $sql = "with ordered_trackitem as (select ti.issue_id, ti.item_id, ti.owner, ti.parent, ti.title, ti.item_desc,
409  row_number() over (partition by ti.parent order by ti.parent, ti.view_order, ti.item_id) as number
410  from cuissuesitem ti
411  where ti.issue_id = $templateId)
412  select sub.owner as sassigned_to, parent.owner as passigned_to, sub.title as stitle, parent.title as ptitle, sub.item_desc as sitem_desc,
413  parent.item_desc as pitem_desc, sub.item_id as strackitem_id, parent.item_id as ptrackitem_id, t.owner, t.code
414  from cuissues t
415  left join ordered_trackitem parent on t.issue_id = parent.issue_id and parent.parent = 0 and parent.number = 1
416  left join ordered_trackitem sub on t.issue_id = sub.issue_id and sub.parent = parent.item_id and sub.number = 1
417  where t.issue_id = $templateId";
418  $sth = db_query($sql, $dbh);
419  if (!$sth) {
420  throw new exception("first task owner query failed.", 1);
421  }
422  $hasTasks = false;
423  $first = true;
424  for($i = 0; $record = db_fetch_assoc($sth, $i); $i++) {
425  if ($first) {
426  $issue = isset($record["code"]) ? trim($record["code"]) : "";
427 
428  if ($record["strackitem_id"] != "") {
429  if (isset($record["sassigned_to"])) {
430  $nextTaskOwner = trim($record["sassigned_to"]);
431  } else if (isset($record["passigned_to"])) {
432  $nextTaskOwner = trim($record["passigned_to"]);
433  } else {
434  $nextTaskOwner = trim($issueOwnerId);
435  }
436 
437  $title = $record["stitle"];
438  $description = $record["sitem_desc"];
439  $taskId = $record["strackitem_id"];
440  $hasTasks = true;
441  } else if ($record["ptrackitem_id"] != "") {
442  $nextTaskOwner = isset($record["passigned_to"]) ? trim($record["passigned_to"]) : trim($issueOwnerId);
443  $title = $record["ptitle"];
444  $description = $record["pitem_desc"];
445  $taskId = $record["ptrackitem_id"];
446  $hasTasks = true;
447  }
448 
449  $first = false;
450  }
451  }
452 
453  $sql = "update cutrack set previous_task_owner = '$nextTaskOwner' where track_id = $issueId";
454  $sth = db_query($sql, $dbh);
455  if (!$sth) {
456  throw new exception("update cutrack failed.", 2);
457  }
458 
459  if ($nextTaskOwner != "" && (strtolower($nextTaskOwner) != strtolower($issueOwnerId) || strtolower($nextTaskOwner) != strtolower($staffId))) {
460  $text = "Job has been created. You are the first task owner.";
461  $results = CreateToLine($dbh, array($nextTaskOwner));
462  $to = $results["data"];
463 
464  $subject = "You are the next task owner.";
465 
466  $taskEmails[] = array("to" => $to, "subject" => $subject, "text" => $text, "issue" => $issue, "title" => $title, "description" => $description,
467  "issueId" => $issueId);
468  }
469 
470  $returnArray = array("status" => "000", "error" => "", "data" => array("taskEmails" => $taskEmails));
471  } catch (exception $e) {
472  $returnArray = array("status" => $e->getCode(), "error" => $e->getMessage());
473  }
474  return $returnArray;
475 }
476 
477 /* This will send an email to the next task owner if it is not the same as previously. If it is the last task, then an email will be sent to the issue owner. */
478 function notifyNextTaskOwner($dbh, $issueId, $taskId, $staffId, $mode, $issueOwnerId, &$errors, &$transactionSQLs, &$taskEmails, &$issueEmails,
479  $completeNotes = "", $forceEmail = false, $reassigned = false, $movedTaskId = 0, $movedAfter = false) {
480 
481  $issueOwnerId = trim($issueOwnerId);
482  $staffId = trim($staffId);
483 
484  $sql = "select assigned_to, issue from cutrack where track_id = $issueId limit 1";
485  $sth = db_query($sql,$dbh);
486  $newError = trim(dbErrorNoFormatting());
487  $gridData = array();
488  if ($newError != "") {
489  $errors[] = $newError;
490  db_free_result($sth);
491  } else {
492  $dRecord = db_fetch_row($sth, 0);
493  $issueOwnerId = $dRecord[0];
494  $issue = $dRecord[1];
495  }
496 
497  $sql = "select * from cutrackitem where track_id = $issueId order by parent, view_order, trackitem_id";
498  $sth = db_query($sql,$dbh);
499  $newError = trim(dbErrorNoFormatting());
500  $gridData = array();
501  if ($newError != "") {
502  $errors[] = $newError;
503  db_free_result($sth);
504  } else {
505  $records = array();
506  $taskIndex = 0;
507  $moveIndex = 0;
508  $shownIndex = 0;
509  for($i = 0; $dRecord = db_fetch_assoc($sth, $i); $i++) {
510  $addRecord = $dRecord["complete_date"] == "" && $dRecord["task_status"] != 2;
511  $thisTaskId = $dRecord["trackitem_id"];
512  $parent = $dRecord["parent"];
513 
514  if ($thisTaskId == $taskId) {
515  switch($mode) {
516  case "deleted":
517  $addRecord = false;
518  break;
519  case "rearranged":
520  $taskIndex = $shownIndex;
521  break;
522  case "promoted":
523  $dRecord["parent"] = 0;
524  $promotedRecord = $dRecord;
525  $addRecord = false;
526  break;
527  case "finished":
528  case "inactivated":
529  $addRecord = false;
530  break;
531  case "reactivated":
532  $addRecord = true;
533  break;
534  }
535  }
536 
537  if ($addRecord && $thisTaskId == $movedTaskId) {
538  $moveIndex = $movedAfter ? $shownIndex + 1 : $shownIndex;
539  }
540 
541  if ($addRecord) {
542  $records[] = $dRecord;
543  $shownIndex++;
544  }
545 
546  }
547 
548 
549  if ($mode == "rearranged") {
550  $task = $records[$taskIndex];
551  if ($taskIndex > $moveIndex) {
552  array_splice($records, $taskIndex, 1);
553  array_splice($records, $moveIndex, 0, array($task));
554  } else {
555  array_splice($records, $moveIndex, 0, array($task));
556  array_splice($records, $taskIndex, 1);
557  }
558  } else if ($mode == "promoted") {
559  $promoteIndex = -1;
560  $i = 0;
561  foreach($records as $record) {
562  if ($record["parent"] != 0) {
563  break;
564  }
565 
566  if ($record["trackitem_id"] == $promotedRecord["parent"]) {
567  $promoteIndex = $i + 1;
568  break;
569  }
570  $i++;
571  }
572  array_splice($records, $promoteIndex, 0, array($promotedRecord));
573  }
574 
575  $parentId = -1;
576  $parentTask = null;
577  $childTask = null;
578  $parentFound = false;
579  $childFound = false;
580 
581  foreach($records as $record) {
582  $parent = intval($record["parent"]);
583  $taskId = intval($record["trackitem_id"]);
584  if ($parent == 0) {
585  if (!$parentFound) {
586  $parentFound = true;
587  $parentTask = $record;
588  $parentId = $taskId;
589  }
590  } else if ($parent == $parentId) {
591  if (!$childFound) {
592  $childFound = true;
593  $childTask = $record;
594  }
595  }
596 
597  if ($parentFound && ($childFound || $parent > $parentId)) {
598  break;
599  }
600  }
601  }
602 
603  $record = isset($childTask) ? $childTask : (isset($parentTask) ? $parentTask : null);
604 
605  $title = trim($record["title"]);
606  $description = trim($record["item_desc"]);
607  $taskId = intval($record["trackitem_id"]);
608 
609  $nextTaskOwner = trim($childTask["assigned_to"]);
610  if ($nextTaskOwner == "") {
611  $nextTaskOwner = trim($parentTask["assigned_to"]);
612  }
613  if ($nextTaskOwner == "") {
614  $nextTaskOwner = trim($issueOwnerId);
615  }
616 
617  $sql = "select previous_task_owner from cutrack where track_id = $issueId";
618  $sth = db_query($sql, $dbh);
619  if (!$sth) {
620  $errors[] = "Select query failed.";
621  }
622  $results = db_fetch_all($sth);
623  $previousTaskOwner = $results === false ? "" : trim($results[0]["previous_task_owner"]);
624 
625  // Now we can send an email.
626  if (isset($record)) {
627  if ($forceEmail || (strtolower($nextTaskOwner) != strtolower($previousTaskOwner))) {
628  $transactionSQLs[] = "update cutrack set previous_task_owner = '$nextTaskOwner' where track_id = $issueId";
629 
630  if ($mode == "new" && (strtolower($nextTaskOwner) != strtolower($issueOwnerId))) {
631  if ($forceEmail || (strtolower($nextTaskOwner) != strtolower($staffId))) {
632  $text = "Job has been created. You are the first task owner.";
633 
634  try {
635  $results = CreateToLine($dbh, array($nextTaskOwner));
636  if ($results["status"] !== "000") {
637  $errors[] = $results["error"];
638  }
639  $to = $results["data"];
640  } catch(exception $e) {
641  $errors[] = "createToLine query failed.";
642  }
643 
644  }
645  } else if ($mode == "rearranged" || $mode == "promoted") {
646  $text = "Tasks were rearranged. $nextTaskOwner is the next task owner, not $previousTaskOwner.";
647 
648  try {
649  $results = CreateToLine($dbh, array($nextTaskOwner, $previousTaskOwner));
650  if ($results["status"] !== "000") {
651  $errors[] = $results["error"];
652  }
653  $to = $results["data"];
654  } catch(exception $e) {
655  $errors[] = "createToLine query failed.";
656  }
657 
658  } else if ($mode == "reactivated") {
659  $text = "A task has been reactivated. $nextTaskOwner is the next task owner, not $previousTaskOwner.";
660 
661  try {
662  $results = CreateToLine($dbh, array($nextTaskOwner, $previousTaskOwner));
663  if ($results["status"] !== "000") {
664  $errors[] = $results["error"];
665  }
666  $to = $results["data"];
667  } catch(exception $e) {
668  $errors[] = "createToLine query failed.";
669  }
670 
671  } else {
672 
673 
674  if ($forceEmail || (strtolower($nextTaskOwner) != strtolower($staffId))) { // Make sure that user doesn't already know.
675  $text = "The task assigned to $previousTaskOwner has been $mode. " . ($reassigned ? "The next task was reassigned to you." : "You are the next active task owner.");
676 
677  try {
678  $results = CreateToLine($dbh, array($nextTaskOwner));
679  if ($results["status"] !== "000") {
680  $errors[] = $results["error"];
681  }
682  $to = $results["data"];
683  } catch(exception $e) {
684  $errors[] = "createToLine query failed.";
685  }
686 
687  }
688  }
689 
690  if (count($errors) == 0 && isset($to)) {
691  $subject = "You are the next task owner.";
692  $taskEmails[] = array("to" => $to, "subject" => $subject, "text" => $text, "issue" => $issue, "title" => $title, "description" => $description,
693  "issueId" => $issueId, "taskId" => $taskId, "completeNotes" => $completeNotes);
694  }
695 
696  }
697  } else {
698 
699  if ($forceEmail || ($mode != "created" && strtolower($issueOwnerId) != strtolower($staffId))) {
700  // I guess that it doesn't make sense for the job to be already completed if it only has begun.
701  $subject = "Your job is nearly complete.";
702 
703  try {
704  $results = CreateToLine($dbh, array($issueOwnerId));
705  if ($results["status"] !== "000") {
706  $errors[] = $results["error"];
707  }
708  $to = $results["data"];
709  } catch(exception $e) {
710  $errors[] = "createToLine query failed.";
711  }
712 
713 
714 
715  if (count($errors) == 0 && isset($to)) {
716  $text = "Tasks have been completed for your job. Please review and complete this job.";
717  $issueEmails[] = array("to" => $to, "subject" => $subject, "text" => $text, "issue" => $issue, "status" => $status, "issueId" => $issueId, "completeNotes" => $completeNotes);
718  }
719 
720  }
721  }
722 }
723