function fix_this_comment($r, $num, $from, $messageid) { global $db; global $alloc_from_addresses2; if ($r["commentEmailUIDORIG"] != $num) { unset($projectID); if ($r["commentMaster"] == "task" && $r["commentMasterID"]) { $q = prepare("select projectID from task where taskID = %d", $r["commentMasterID"]); $db->query($q); $task_row = $db->row(); $projectID = $task_row["projectID"]; } // Try figure out and populate the commentCreatedUser/commentCreatedUserClientContactID fields list($from_address, $from_name) = parse_email_address($from); $person = new person(); $personID = $person->find_by_email($from_address); $personID or $personID = $person->find_by_name($from_name); $sql = array(); $sql[] = prepare("commentEmailUID = '%s'", trim($num)); if ($personID) { $sql[] = prepare("commentCreatedUser = %d", $personID); $sql[] = "commentCreatedUserClientContactID = NULL"; } else { $sql[] = "commentCreatedUser = NULL"; $cc = new clientContact(); $clientContactID = $cc->find_by_email($from_address, $projectID); $clientContactID or $clientContactID = $cc->find_by_name($from_name, $projectID); $clientContactID and $sql[] = prepare("commentCreatedUserClientContactID = %d", $clientContactID); } $sql[] = prepare("commentCreatedUserText = '%s'", trim($from)); $sql[] = prepare("commentEmailMessageID = '%s'", trim($messageid)); if (!in_array($from_address, $alloc_from_addresses2)) { // don't update items that are from alloc $q = prepare("UPDATE comment SET " . implode(",", $sql) . " WHERE commentID = %d", $r["commentID"]); $db->query($q); printorlog("FIXED: " . $q . " (old uid: " . $r["commentEmailUIDORIG"] . ")", "blue"); } } else { // Try figure out and populate the commentCreatedUser/commentCreatedUserClientContactID fields list($from_address, $from_name) = parse_email_address($from); if (!in_array($from_address, $alloc_from_addresses2)) { // don't update items that are from alloc $sql = array(); $sql[] = prepare("commentEmailUID = '%s'", trim($num)); $sql[] = prepare("commentEmailMessageID = '%s'", trim($messageid)); $q = prepare("UPDATE comment SET " . implode(",", $sql) . " WHERE commentID = %d", $r["commentID"]); $db->query($q); printorlog("GOOD: " . $q, "green"); } } }
function get_person_and_client($from_address, $from_name, $projectID = null) { $current_user =& singleton("current_user"); $person = new person(); $personID = $person->find_by_email($from_address); $personID or $personID = $person->find_by_name($from_name); if (!$personID) { $cc = new clientContact(); $clientContactID = $cc->find_by_email($from_address); $clientContactID or $clientContactID = $cc->find_by_name($from_name, $projectID); } // If we don't have a $from_name, but we do have a personID or clientContactID, get proper $from_name if (!$from_name) { if ($personID) { $from_name = person::get_fullname($personID); } else { if ($clientContactID) { $cc = new clientContact(); $cc->set_id($clientContactID); $cc->select(); $from_name = $cc->get_value("clientContactName"); } else { $from_name = $from_address; } } } return array($personID, $clientContactID, $from_name); }
/** * Convert a comma separated string of names, into an array with email addresses * @param string $people * @param string $entity the related entity that can assist in the look up * @param integer $entityID the id of the related entity * @return array an array of people, indexed by their email address */ public function get_people($options = array(), $entity = "", $entityID = "") { $person_table =& get_cached_table("person"); $people = $options; if ($entity && $entityID) { $e = new $entity(); $e->set_id($entityID); $e->select(); in_array("default", $people) and $default_recipients = $e->get_all_parties(); in_array("internal", $people) and $internal_recipients = $e->get_all_parties(); } // remove default and internal from the array $clean_people = array_diff($people, array("default", "internal")); if (is_object($e)) { $projectID = $e->get_project_id(); $p = new project(); $p->set_id($projectID); $p->select(); $client = $p->get_foreign_object("client"); $clientID = $client->get_id(); } foreach ((array) $default_recipients as $email => $info) { if ($info["selected"]) { $rtn[$email] = $this->reduce_person_info($info); } } foreach ((array) $internal_recipients as $email => $info) { if ($info["selected"] && !$info["external"]) { $rtn[$email] = $this->reduce_person_info($info); } } foreach ((array) $clean_people as $person) { $bad_person = true; $person = trim($person); // personID if (is_numeric($person)) { if ($person_table[$person]["personActive"]) { $rtn[$person_table[$person]["emailAddress"]] = $person_table[$person]; $bad_person = false; continue; } // email addresses } else { if (in_str("@", $person)) { foreach ($person_table as $pid => $data) { if (same_email_address($person, $data["emailAddress"]) && $data["personActive"]) { $rtn[$data["emailAddress"]] = $data; $bad_person = false; continue 2; } } if ($ccID = clientContact::find_by_email($person)) { $cc = new clientContact(); $cc->set_id($ccID); $cc->select(); $rtn[$cc->get_value("clientContactEmail")] = $cc->row(); $bad_person = false; continue; } // If we get here, then return the email address entered list($e, $n) = parse_email_address($person); $rtn[$e] = array("emailAddress" => $e, "name" => $n); $bad_person = false; continue; // usernames, partial and full names } else { foreach ($person_table as $pid => $data) { // If matches username if (strtolower($person) == strtolower($data["username"]) && $data["personActive"]) { $rtn[$data["emailAddress"]] = $data; $bad_person = false; continue 2; } } foreach ($person_table as $pid => $data) { // If matches name if (strtolower($person) == strtolower($data["firstName"] . " " . $data["surname"]) && $data["personActive"]) { $rtn[$data["emailAddress"]] = $data; $bad_person = false; continue 2; } } foreach ($person_table as $pid => $data) { // If matches a section of name, eg: a search for "Ale" will match the full name "Alex Lance" if (strtolower($person) == strtolower(substr(strtolower($data["firstName"] . " " . $data["surname"]), 0, strlen($person))) && $data["personActive"]) { $rtn[$data["emailAddress"]] = $data; $bad_person = false; continue 2; } } if ($ccID = clientContact::find_by_nick($person, $clientID)) { $cc = new clientContact(); $cc->set_id($ccID); $cc->select(); $rtn[$cc->get_value("clientContactEmail")] = $cc->row(); $bad_person = false; continue; } if ($ccID = clientContact::find_by_name($person, $projectID)) { $cc = new clientContact(); $cc->set_id($ccID); $cc->select(); $rtn[$cc->get_value("clientContactEmail")] = $cc->row(); $bad_person = false; continue; } if ($ccID = clientContact::find_by_partial_name($person, $projectID)) { $cc = new clientContact(); $cc->set_id($ccID); $cc->select(); $rtn[$cc->get_value("clientContactEmail")] = $cc->row(); $bad_person = false; continue; } } } if ($bad_person) { die("Unable to find person: " . $person); } } foreach ((array) $rtn as $id => $p) { $rtn[$id] = $this->reduce_person_info($p); } return (array) $rtn; }