<?php
class sharing_permissions {
function pull_organizations_sharing_info ($org_id) {
GLOBAL $head_dynamic_style, $unique_seq, $organization_term;
//$head_dynamic_style .= ".grouprow {color: #FFFFFF; font-size: 18; background-color: #000099; font-weight: bolder; width: 60%}
//.shareintro {font-weight: bolder;}
//";
$sql = "SELECT group_name, group_id FROM groups WHERE group_id != '1' ORDER BY group_name";
$result = run_query ($sql, "Pull group names for sharing");
$final_html .= "<table><tr><td colspan=\"3\" class=\"generictabletop\">Check the Following Box to Share Your Client Records With Every ".$organization_term." Listed Below:<input type=\"checkbox\" name=\"form_answer[0]\" value=\"yes\"";
//For each org, find out if we are sharing with them, and check the box if true
$sqlg = "SELECT * FROM sharing_permissions WHERE sharing_org_id = '".$org_id."' AND sharing_with_org_id = '0'";
$share_check_C = run_query ($sqlg, "Pull sharing status");
if(num_rows($share_check_C) > 0) {
$final_html .= "checked";
}
$final_html .= ">";
$final_html .= "<p>OR</p><p>Choose Individual ".$organization_term." to Share With by Checking the Associated Boxes Below.</p><p></p></td></tr>";
$final_html .= "<tr><td class=\"generictabletop\">".$organization_term."</td><td class=\"generictabletop\">Do They Share Client Records With You?</td><td class=\"generictabletop\">Check Box to Share Client Records With Them</td></tr>";
$total_groups = num_rows($result);
for ($i=0; $i < $total_groups; $i++) {
$group_row = fetch_array ($result, "Group Info Pull", $i);
$final_html .= "<tr><td class=\"genericsubtop\" colspan=\"3\">".$group_row["group_name"]."</td>";
//Pull out the List of Organizations
$sqlb = "SELECT org_id, org_name FROM organizations WHERE org_group_id = '".$group_row[$unique_seq."group_id"]."' AND org_type != 'group' ORDER BY org_name";
//echo $sqlb."<p>";
$org_result = run_query ($sqlb, "Pull orgs for permissions table");
$total_orgs = num_rows($org_result);
for($z=0; $z < $total_orgs; $z++) {
$org_row = fetch_array ($org_result, "org info for permissions table", $z);
$z_org_id = $unique_seq."org_id";
//For each org, find out if they are sharing with us
$sqlc = "SELECT * FROM sharing_permissions WHERE sharing_org_id = '".$org_row[$z_org_id]."' AND (sharing_with_org_id = '".$org_id."' OR sharing_with_org_id = '0')";
//echo $sqlc."<p>";
$share_check = run_query ($sqlc, "Pull sharing status");
if(num_rows($share_check) > 0) {$share_message = "Yes";}
else {$share_message = "No";}
$final_html .= "<tr><td class=\"generictd\"> ".$org_row["org_name"]."</td>";
$final_html .= "<td class=\"generictd\">".$share_message."</td>";
$final_html .= "<td class=\"generictd\"><input type=\"checkbox\" name=\"form_answer[".$org_row[$z_org_id]."]\" value=\"yes\"";
//For each org, find out if we are sharing with them, and check the box if true
$sqld = "SELECT * FROM sharing_permissions WHERE sharing_org_id = '".$org_id."' AND sharing_with_org_id = '".$org_row[$z_org_id]."'";
$share_check_B = run_query ($sqld, "Pull sharing status");
if(num_rows($share_check_B) > 0) {
$final_html .= "checked";
}
$final_html .= "></td></tr>";
}
}
$final_html .= "</table>";
return $final_html;
}
function insert_edited_sharing_permissions ($org_id, $form_answer) {
global $unique_seq;
$sql = "SELECT org_id FROM organizations WHERE org_type != 'group' AND org_type != 'root'";
$orgs_result = run_query ($sql, "Insert permission changes");
$total_orgs = num_rows($orgs_result);
for($i=0; $i < $total_orgs; $i++) {
$row_org_id = fetch_array($orgs_result, "Orgs Sharing Insert", $i);
$row_org_id = $row_org_id[0];
$sqlj = "SELECT * FROM sharing_permissions WHERE sharing_org_id = '".$org_id."' AND sharing_with_org_id = '".$row_org_id."'";
$resultj = run_query ($sqlj, "Find existing sharing records");
if (num_rows($resultj) > 0) {$share_on_or_off = "on";}
else {$share_on_or_off = "off";}
if ($form_answer[$row_org_id] == "yes" && $share_on_or_off == "off") {
$sqlm = "INSERT INTO sharing_permissions (sharing_org_id, sharing_with_org_id) VALUES ('".$org_id."', '".$row_org_id."')";
run_query ($sqlm, "Inserting sharing");
}
if (!$form_answer[$row_org_id] && $share_on_or_off == "on") {
$sqlo = "DELETE FROM sharing_permissions WHERE sharing_org_id = '".$org_id."' AND sharing_with_org_id = '".$row_org_id."'";
run_query ($sqlo, "Deleting Sharing");
}
}
$sqlj = "SELECT * FROM sharing_permissions WHERE sharing_org_id = '".$org_id."' AND sharing_with_org_id = '0'";
$resultj = run_query ($sqlj, "Find existing sharing records");
if (num_rows($resultj) > 0) {$share_on_or_off = "on";}
else {$share_on_or_off = "off";}
if ($form_answer["0"] == "yes" && $share_on_or_off == "off") {
$sqlm = "INSERT INTO sharing_permissions (sharing_org_id, sharing_with_org_id) VALUES ('".$org_id."', '0')";
run_query ($sqlm, "Inserting sharing");
}
if (!$form_answer["0"] && $share_on_or_off == "on") {
$sqlo = "DELETE FROM sharing_permissions WHERE sharing_org_id = '".$org_id."' AND sharing_with_org_id = '0'";
run_query ($sqlo, "Deleting Sharing");
}
}
}
?>