/**
 * \brief Get an array of all the folders from a $RootFolder on down.
 * Recursive.  This is typically used to build a select list of folder names.
 *
 * \param $RootFolder default is entire software repository
 * \param $FolderArray returned array of folder_pk=>folder_name's
 *
 * \return $FolderArray of {folder_pk=>folder_name, folder_pk=>folder_name, ...}
 * in folder order.
 * If no folders are in the list, an empty array is returned.
 *
 * \todo Possibly this could be a common function and FolderListOption() could 
 *       use this for its data.  In general data collection and data formatting
 *       should be separate functions.
 */
function GetFolderArray($RootFolder, &$FolderArray)
{
    global $PG_CONN;
    if ($RootFolder == "-1") {
        $RootFolder = FolderGetTop();
    }
    if (empty($RootFolder)) {
        return $FolderArray;
    }
    /* Load this folder's name */
    $sql = "SELECT folder_name, folder_pk FROM folder WHERE folder_pk={$RootFolder} LIMIT 1;";
    $result = pg_query($PG_CONN, $sql);
    DBCheckResult($result, $sql, __FILE__, __LINE__);
    $row = pg_fetch_assoc($result);
    pg_free_result($result);
    $Name = trim($row['folder_name']);
    $FolderArray[$row['folder_pk']] = $row['folder_name'];
    /* Load any subfolders */
    $sql = "SELECT folder.folder_pk, folder.folder_name,\n            foldercontents.parent_fk\n            FROM folder, foldercontents\n            WHERE foldercontents.foldercontents_mode = " . FolderDao::MODE_FOLDER . "\n            AND foldercontents.parent_fk ={$RootFolder}\n            AND foldercontents.child_id = folder.folder_pk\n            AND folder.folder_pk is not null\n            ORDER BY folder_name";
    $result = pg_query($PG_CONN, $sql);
    DBCheckResult($result, $sql, __FILE__, __LINE__);
    if (pg_num_rows($result) > 0) {
        while ($row = pg_fetch_assoc($result)) {
            GetFolderArray($row['folder_pk'], $FolderArray);
        }
    }
    pg_free_result($result);
}
 function Output()
 {
     global $PG_CONN;
     global $PERM_NAMES;
     /* GET parameters */
     $folder_pk = GetParm('folder', PARM_INTEGER);
     $upload_pk = GetParm('upload', PARM_INTEGER);
     $users_group_pk = GetParm('group_pk', PARM_INTEGER);
     $group_pk = GetParm('group', PARM_INTEGER);
     $perm_upload_pk = GetParm('permupk', PARM_INTEGER);
     $perm = GetParm('perm', PARM_INTEGER);
     $newgroup = GetParm('newgroup', PARM_INTEGER);
     $newperm = GetParm('newperm', PARM_INTEGER);
     $public_perm = GetParm('public', PARM_INTEGER);
     // start building the output buffer
     $V = "";
     /* If perm_upload_pk is passed in, update either the perm or group_pk */
     $sql = "";
     if (!empty($perm_upload_pk)) {
         if ($perm === 0) {
             $sql = "delete from perm_upload where perm_upload_pk='{$perm_upload_pk}'";
         } else {
             if (!empty($perm)) {
                 $sql = "update perm_upload set perm='{$perm}' where perm_upload_pk='{$perm_upload_pk}'";
             } else {
                 if (!empty($group_pk)) {
                     $sql = "update perm_upload set group_fk='{$group_pk}' where perm_upload_pk='{$perm_upload_pk}'";
                 }
             }
         }
         if (!empty($sql)) {
             $result = @pg_query($PG_CONN, $sql);
             DBCheckResult($result, $sql, __FILE__, __LINE__);
             pg_free_result($result);
         }
     } else {
         if (!empty($newgroup) and !empty($newperm)) {
             // before inserting this new record, delete any record for the same upload and group since
             // that would be a duplicate
             $sql = "delete from perm_upload where upload_fk={$upload_pk} and group_fk={$newgroup}";
             $result = pg_query($PG_CONN, $sql);
             DBCheckResult($result, $sql, __FILE__, __LINE__);
             pg_free_result($result);
             // Don't insert a PERM_NONE.  NONE is the default
             if ($newperm != PERM_NONE) {
                 $sql = "insert into perm_upload (perm, upload_fk, group_fk) values ({$newperm}, {$upload_pk}, {$newgroup})";
                 $result = pg_query($PG_CONN, $sql);
                 DBCheckResult($result, $sql, __FILE__, __LINE__);
                 pg_free_result($result);
             }
             $newperm = $newgroup = 0;
         } else {
             if (!empty($public_perm)) {
                 $sql = "update upload set public_perm='{$public_perm}' where upload_pk='{$upload_pk}'";
                 $result = pg_query($PG_CONN, $sql);
                 DBCheckResult($result, $sql, __FILE__, __LINE__);
                 pg_free_result($result);
             }
         }
     }
     $root_folder_pk = GetUserRootFolder();
     if (empty($folder_pk)) {
         $folder_pk = $root_folder_pk;
     }
     // Get folder array folder_pk => folder_name
     $FolderArray = array();
     GetFolderArray($root_folder_pk, $FolderArray);
     /* define js_url */
     $V .= js_url();
     $text = _("Select the folder that contains the upload:  \n");
     $V .= "{$text}";
     /*** Display folder select list, on change request new page with folder= in url ***/
     $url = Traceback_uri() . "?mod=upload_permissions&folder=";
     $onchange = "onchange=\"js_url(this.value, '{$url}')\"";
     $V .= Array2SingleSelect($FolderArray, "folderselect", $folder_pk, false, false, $onchange);
     /*** Display upload select list, on change, request new page with new upload= in url ***/
     $text = _("Select the upload you wish to edit:  \n");
     $V .= "<br>{$text}";
     // Get list of all upload records in this folder that the user has PERM_ADMIN
     $UploadList = FolderListUploads_perm($folder_pk, PERM_ADMIN);
     /*
     if (empty($UploadList))
     {
     echo "You have no uploads in this folder for which you are an admin.  Hit the back button";
     return;
     }
     */
     // Make data array for upload select list.  Key is upload_pk, value is a composite
     // of the upload_filename and upload_ts.
     // Note that $UploadList may be empty so $UploadArray will be empty
     $UploadArray = array();
     foreach ($UploadList as $UploadRec) {
         $SelectText = htmlentities($UploadRec['name']);
         if (!empty($UploadRec['upload_ts'])) {
             $SelectText .= ", " . substr($UploadRec['upload_ts'], 0, 19);
         }
         $UploadArray[$UploadRec['upload_pk']] = $SelectText;
     }
     /* Get selected upload info to display*/
     if (empty($upload_pk)) {
         // no upload selected, so use the top one in the select list
         reset($UploadArray);
         $upload_pk = key($UploadArray);
     }
     /* Upload select list */
     $url = Traceback_uri() . "?mod=upload_permissions&folder={$folder_pk}&upload=";
     $onchange = "onchange=\"js_url(this.value, '{$url}')\"";
     $V .= Array2SingleSelect($UploadArray, "uploadselect", $upload_pk, false, false, $onchange);
     /* Get permissions for this upload */
     if (!empty($UploadArray)) {
         // Get upload.public_perm
         $sql = "select public_perm from upload where upload_pk='{$upload_pk}'";
         $result = pg_query($PG_CONN, $sql);
         DBCheckResult($result, $sql, __FILE__, __LINE__);
         $Row = pg_fetch_all($result);
         $public_perm = $Row[0]['public_perm'];
         pg_free_result($result);
         $text1 = _("Public Permission");
         $V .= "<p>{$text1} &nbsp;";
         $url = Traceback_uri() . "?mod=upload_permissions&folder={$folder_pk}&upload={$upload_pk}&public=";
         $onchange = "onchange=\"js_url(this.value, '{$url}')\"";
         $V .= Array2SingleSelect($PERM_NAMES, "publicpermselect", $public_perm, false, false, $onchange);
         $sql = "select perm_upload_pk, perm, group_pk, group_name from groups, perm_upload where group_fk=group_pk and upload_fk='{$upload_pk}'";
         $result = pg_query($PG_CONN, $sql);
         DBCheckResult($result, $sql, __FILE__, __LINE__);
         $PermArray = pg_fetch_all($result);
         pg_free_result($result);
         /* Get master array of groups */
         $sql = "select group_pk, group_name from groups order by group_name";
         $result = pg_query($PG_CONN, $sql);
         DBCheckResult($result, $sql, __FILE__, __LINE__);
         $GroupArray = array();
         while ($GroupRow = pg_fetch_assoc($result)) {
             $GroupArray[$GroupRow['group_pk']] = $GroupRow['group_name'];
         }
         pg_free_result($result);
         /* Permissions Table */
         $V .= "<p><table border=1>";
         $GroupText = _("Group");
         $PermText = _("Permission");
         $V .= "<tr><th>{$GroupText}</th><th>{$PermText}</th></tr>";
         foreach ($PermArray as $PermRow) {
             $V .= "<tr>";
             $V .= "<td>";
             // group
             $url = Traceback_uri() . "?mod=upload_permissions&group_pk={$users_group_pk}&upload={$upload_pk}&folder={$folder_pk}&permupk={$PermRow['perm_upload_pk']}&group=";
             $onchange = "onchange=\"js_url(this.value, '{$url}')\"";
             $V .= Array2SingleSelect($GroupArray, "groupselect", $PermRow['group_pk'], false, false, $onchange);
             $V .= "</td>";
             $V .= "<td>";
             // permission
             $url = Traceback_uri() . "?mod=upload_permissions&group_pk={$users_group_pk}&upload={$upload_pk}&folder={$folder_pk}&permupk={$PermRow['perm_upload_pk']}&perm=";
             $onchange = "onchange=\"js_url(this.value, '{$url}')\"";
             $V .= Array2SingleSelect($PERM_NAMES, "permselect", $PermRow['perm'], false, false, $onchange);
             $V .= "</td>";
             $V .= "</tr>";
         }
         /* Print one extra row for adding perms */
         $V .= "<tr>";
         $V .= "<td>";
         // group
         $url = Traceback_uri() . "?mod=upload_permissions&group_pk={$users_group_pk}&upload={$upload_pk}&folder={$folder_pk}&newperm={$newperm}&newgroup=";
         $onchange = "onchange=\"js_url(this.value, '{$url}')\"";
         $Selected = empty($newgroup) ? "" : $newgroup;
         $V .= Array2SingleSelect($GroupArray, "groupselectnew", $Selected, true, false, $onchange);
         $V .= "</td>";
         $V .= "<td>";
         // permission
         $url = Traceback_uri() . "?mod=upload_permissions&group_pk={$users_group_pk}&upload={$upload_pk}&folder={$folder_pk}&newgroup={$newgroup}&newperm=";
         $onchange = "onchange=\"js_url(this.value, '{$url}')\"";
         $Selected = empty($newperm) ? "" : $newperm;
         $V .= Array2SingleSelect($PERM_NAMES, "permselectnew", $Selected, false, false, $onchange);
         $V .= "</td>";
         $V .= "</tr>";
         $V .= "</table>";
         $text = _("All upload permissions take place immediately when a value is changed.  There is no submit button.");
         $V .= "<p>" . $text;
         $text = _("Add new groups on the last line.");
         $V .= "<br>" . $text;
     } else {
         $text = _("You have no permission to change permissions on any upload in this folder.");
         $V .= "<p>{$text}<p>";
     }
     $V .= "<hr>";
     $V .= $this->DisplayGroupMembership();
     if (!$this->OutputToStdout) {
         return $V;
     }
     print "{$V}";
     return;
 }