r2332 - add "file not found" message
authorDenis Ovsienko <infrastation@yandex.ru>
Wed, 14 Jan 2009 18:00:26 +0000 (18:00 +0000)
committerDenis Ovsienko <infrastation@yandex.ru>
Wed, 14 Jan 2009 18:00:26 +0000 (18:00 +0000)
 - discard AJAX helper for file linker
 - toss around file management panel parts

file_link_helper.php [deleted file]
inc/database.php
inc/interface.php
inc/navigation.php
inc/ophandlers.php

diff --git a/file_link_helper.php b/file_link_helper.php
deleted file mode 100644 (file)
index 6a82236..0000000
+++ /dev/null
@@ -1,75 +0,0 @@
-<?php
-       require 'inc/init.php';
-       // This is our context.
-       $pageno = 'files';
-       $tabno = 'default';
-       fixContext();
-       if (!permitted())
-       {
-               renderAccessDenied();
-               die;
-       }
-?>
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" style="height: 100%;">
-<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
-<?php
-echo '<title>' . getTitle ($pageno, $tabno) . "</title>\n";
-echo "<link rel=stylesheet type='text/css' href=pi.css />\n";
-echo "<link rel=icon href='" . getFaviconURL() . "' type='image/x-icon' />";
-?>
-</head>
-<body style="height: 100%;">
-<form action="javascript:;">
-<div style="background-color: #f0f0f0; border: 1px solid #3c78b5; padding: 10px; height: 100%; text-align: center; margin: 5px;">
-<h2>Choose a file:</h2><br><br>
-<input type=hidden id='file_name'>
-<select size=<?php echo getConfigVar ('MAXSELSIZE'); ?> id="file_id">
-<?php
-       $entity_type = $_REQUEST['entity_type'];
-       $entity_id = $_REQUEST['entity_id'];
-
-       // Append different param to URL depending on entity_type
-       switch ($entity_type)
-       {
-               case 'ipv4net':
-                       $entity_param = 'id';
-                       break;
-               case 'ipv4rspool':
-                       $entity_param = 'pool_id';
-                       break;
-               case 'ipv4vs':
-                       $entity_param = 'vs_id';
-                       break;
-               case 'object':
-                       $entity_param = 'object_id';
-                       break;
-               case 'rack':
-                       $entity_param = 'rack_id';
-                       break;
-               case 'user':
-                       $entity_param = 'user_id';
-                       break;
-       }
-
-
-       $files = getAllUnlinkedFiles($entity_type, $entity_id);
-       foreach ($files as $file)
-       {
-               echo "<option value='${file['id']}' onclick='getElementById(\"file_name\").value=\"${file['name']}\";'>${file['name']}</option>\n";
-       }
-?>
-</select><br><br>
-<?php
-       echo "<input type='submit' value='Proceed' onclick='" .
-       "if (getElementById(\"file_id\").value != \"\") {\n" .
-       "       opener.location=\"${root}process.php?page=$entity_type&tab=files&op=linkFile&entity_type=$entity_type&entity_id=$entity_id&$entity_param=$entity_id&file_id=\"+getElementById(\"file_id\").value+\"&file_name=\"+getElementById(\"file_name\").value; \n" .
-       "       window.close();" .
-       "}" .
-       "'>";
-?>
-</div>
-</form>
-</body>
-</html>
index e9f43b1..9911f13 100644 (file)
@@ -3408,7 +3408,8 @@ function getAllFiles ()
        return $ret;
 }
 
-// Return a list of files which are not linked to the specified record
+// Return a list of files which are not linked to the specified record. This list
+// will be used by printSelect().
 function getAllUnlinkedFiles ($entity_type = NULL, $entity_id = 0)
 {
        if ($entity_type == NULL || $entity_id == 0)
@@ -3418,27 +3419,16 @@ function getAllUnlinkedFiles ($entity_type = NULL, $entity_id = 0)
        }
        global $dbxlink;
        $sql =
-               'SELECT id, name, type, size, ctime, mtime, atime, comment FROM File ' .
+               'SELECT id, name FROM File ' .
                'WHERE id NOT IN (SELECT file_id FROM FileLink WHERE entity_type = ? AND entity_id = ?) ' .
-               'ORDER BY name';
+               'ORDER BY name, id';
        $query = $dbxlink->prepare($sql);
        $query->bindParam(1, $entity_type);
        $query->bindParam(2, $entity_id);
        $query->execute();
        $ret=array();
-       $count=0;
        while ($row = $query->fetch (PDO::FETCH_ASSOC))
-       {
-               $ret[$count]['id'] = $row['id'];
-               $ret[$count]['name'] = $row['name'];
-               $ret[$count]['type'] = $row['type'];
-               $ret[$count]['size'] = $row['size'];
-               $ret[$count]['ctime'] = $row['ctime'];
-               $ret[$count]['mtime'] = $row['mtime'];
-               $ret[$count]['atime'] = $row['atime'];
-               $ret[$count]['comment'] = $row['comment'];
-               $count++;
-       }
+               $ret[$row['id']] = $row['name'];
        return $ret;
 }
 
index 1105f8d..48980de 100644 (file)
@@ -1463,6 +1463,7 @@ function printLog ($log)
                                175 => array ('code' => 'error', 'format' => 'Invalid netmask'),
                                176 => array ('code' => 'error', 'format' => 'This network already exists'),
                                177 => array ('code' => 'error', 'format' => 'commitUpdateRack() failed'),
+                               178 => array ('code' => 'error', 'format' => 'file not found'),
 
                                200 => array ('code' => 'warning', 'format' => 'generic warning: %s'),
                                201 => array ('code' => 'warning', 'format' => 'nothing happened...'),
@@ -5690,56 +5691,55 @@ function renderFilesForEntity ($entity_type = NULL, $id_name = NULL, $entity_id
                return;
        }
 
-       function printNewItemTR ($entity_type, $entity_id)
+       showMessageOrError();
+       startPortlet ('Upload new');
+       echo "<table border=0 cellspacing=0 cellpadding='5' align='center'>\n";
+       printOpFormIntro ('addFile', array ('entity_type' => $entity_type, 'entity_id' => $entity_id, 'MAX_FILE_SIZE' => convertToBytes(get_cfg_var('upload_max_filesize'))), TRUE);
+       echo "<tr>";
+       echo "<td class=tdleft><input type='file' size='10' name='file' tabindex=100></td>\n";
+       echo "<td class=tdleft><input type='text' size='15' name='comment' tabindex=101></td><td>\n";
+       printImageHREF ('CREATE', 'Upload file', TRUE, 99);
+       echo "</td></tr></form>";
+       echo "</table><br>\n";
+       finishPortlet();
+
+       $files = getAllUnlinkedFiles ($entity_type, $entity_id);
+       if (count ($files))
        {
-               global $root;
-               printOpFormIntro ('addFile', array ('entity_type' => $entity_type, 'entity_id' => $entity_id, 'MAX_FILE_SIZE' => convertToBytes(get_cfg_var('upload_max_filesize'))), TRUE);
-               echo "<tr><td>";
-               printImageHREF ('add', 'Upload file', TRUE, 99);
-               echo "</td>";
-               echo "<td class=tdleft><input type='file' size='10' name='file' tabindex=100></td>\n";
-               echo "<td class=tdleft><input type='text' size='15' name='comment' tabindex=101></td>\n";
-               echo "<td></td>\n";
-               echo "<td><a href='javascript:;' onclick='window.open(\"${root}file_link_helper.php?entity_type=$entity_type&entity_id=$entity_id";
-               echo "\",\"findlink\",\"height=700, width=400, location=no, menubar=no, resizable=yes, scrollbars=no, status=no, titlebar=no, toolbar=no\");'>";
-               printImageHREF ('link', 'Link an existing file');
-               echo "</a> &nbsp;&nbsp;";
-               printImageHREF ('add', 'Upload file', TRUE, 99);
-               echo "</td></tr></form>";
+               startPortlet ('Use existing');
+               printOpFormIntro ('linkFile');
+               printSelect ($files, 'file_id');
+               printImageHREF ('ADD', 'Link file', TRUE);
+               finishPortlet();
        }
 
-       showMessageOrError();
-       startPortlet ('Files');
        $filelist = getFilesOfEntity ($entity_type, $entity_id);
-       echo "<table border=0 cellspacing=0 cellpadding='5' align='center' class='widetable'>\n";
-       echo "<tr><th>&nbsp;</th><th>Name</th><th>Comment</th><th>Size</th><th>Actions</th></tr>\n";
-
-       if (getConfigVar ('ADDNEW_AT_TOP') == 'yes')
-               printNewItemTR($entity_type, $entity_id);
-       foreach ($filelist as $file_id => $file)
+       if (count ($filelist))
        {
-               printOpFormIntro ('updateFile', array ('file_id' => $file_id, 'link_id' => $file['link_id'], 'name' => $file['name']));
-               echo "<tr valign=top><td><a href='${root}process.php?op=deleteFile&page=${pageno}&tab=${tabno}&file_id=${file_id}&${id_name}=${entity_id}&name=${file['name']}'>";
-               printImageHREF ('delete', 'Unlink and delete file');
-               echo '</a></td>';
-               printf("<td class='tdleft'><a href='%/?page=file&file_id=%s'><strong>%s</strong></a>", $root, $file_id, $file['name']);
-               echo "<td class=tdleft><input type='text' name='comment' value='${file['comment']}' size=15></td>";
-               printf("<td class=tdleft>%s</td>", formatFileSize($file['size']));
-               echo "<td><a href='${root}download.php?file_id=${file_id}'>";
-               printImageHREF ('download', 'Download file');
-               echo '</a> ';
-               echo "<a href='${root}process.php?op=unlinkFile&page=${pageno}&tab=${tabno}&link_id=${file['link_id']}&${id_name}=${entity_id}&name=${file['name']}'>";
-               printImageHREF ('clear', 'Unlink file');
-               echo '</a> ';
-               printImageHREF ('save', 'Save changes', TRUE);
-               echo "</td></form></tr>\n";
+               startPortlet ('Manage linked');
+               echo "<table border=0 cellspacing=0 cellpadding='5' align='center' class='widetable'>\n";
+               echo "<tr><th>&nbsp;</th><th>Name</th><th>Comment</th><th>Size</th><th>Actions</th></tr>\n";
+               foreach ($filelist as $file_id => $file)
+               {
+                       printOpFormIntro ('updateFile', array ('file_id' => $file_id, 'link_id' => $file['link_id'], 'name' => $file['name']));
+                       echo "<tr valign=top><td><a href='${root}process.php?op=deleteFile&page=${pageno}&tab=${tabno}&file_id=${file_id}&${id_name}=${entity_id}&name=${file['name']}'>";
+                       printImageHREF ('delete', 'Unlink and delete file');
+                       echo '</a></td>';
+                       printf("<td class='tdleft'><a href='%/?page=file&file_id=%s'><strong>%s</strong></a>", $root, $file_id, $file['name']);
+                       echo "<td class=tdleft><input type='text' name='comment' value='${file['comment']}' size=15></td>";
+                       printf("<td class=tdleft>%s</td>", formatFileSize($file['size']));
+                       echo "<td><a href='${root}download.php?file_id=${file_id}'>";
+                       printImageHREF ('download', 'Download file');
+                       echo '</a> ';
+                       echo "<a href='${root}process.php?op=unlinkFile&page=${pageno}&tab=${tabno}&link_id=${file['link_id']}&${id_name}=${entity_id}&name=${file['name']}'>";
+                       printImageHREF ('clear', 'Unlink file');
+                       echo '</a> ';
+                       printImageHREF ('save', 'Save changes', TRUE);
+                       echo "</td></form></tr>\n";
+               }
+               echo "</table><br>\n";
+               finishPortlet();
        }
-       if (getConfigVar ('ADDNEW_AT_TOP') != 'yes')
-               printNewItemTR($entity_type, $entity_id);
-
-       echo "</table><br>\n";
-       finishPortlet();
-
 }
 
 // Set of wrapper functions to reduce duplicate code
index 155742d..42362e9 100644 (file)
@@ -93,7 +93,8 @@ $msgcode['rack']['files']['addFile']['ERR'] = 100;
 $msgcode['rack']['files']['updateFile']['OK'] = 70;
 $msgcode['rack']['files']['updateFile']['ERR'] = 100;
 $msgcode['rack']['files']['linkFile']['OK'] = 71;
-$msgcode['rack']['files']['linkFile']['ERR'] = 100;
+$msgcode['rack']['files']['linkFile']['ERR1'] = 178;
+$msgcode['rack']['files']['linkFile']['ERR2'] = 100;
 $msgcode['rack']['files']['unlinkFile']['OK'] = 72;
 $msgcode['rack']['files']['unlinkFile']['ERR'] = 100;
 $msgcode['rack']['files']['deleteFile']['OK'] = 73;
@@ -228,7 +229,8 @@ $msgcode['object']['files']['addFile']['ERR'] = $msgcode['rack']['files']['addFi
 $msgcode['object']['files']['updateFile']['OK'] = $msgcode['rack']['files']['updateFile']['OK'];
 $msgcode['object']['files']['updateFile']['ERR'] = $msgcode['rack']['files']['updateFile']['ERR'];
 $msgcode['object']['files']['linkFile']['OK'] = $msgcode['rack']['files']['linkFile']['OK'];
-$msgcode['object']['files']['linkFile']['ERR'] = $msgcode['rack']['files']['linkFile']['ERR'];
+$msgcode['object']['files']['linkFile']['ERR1'] = $msgcode['rack']['files']['linkFile']['ERR1'];
+$msgcode['object']['files']['linkFile']['ERR2'] = $msgcode['rack']['files']['linkFile']['ERR2'];
 $msgcode['object']['files']['unlinkFile']['OK'] = $msgcode['rack']['files']['unlinkFile']['OK'];
 $msgcode['object']['files']['unlinkFile']['ERR'] = $msgcode['rack']['files']['unlinkFile']['ERR'];
 $msgcode['object']['files']['deleteFile']['OK'] = $msgcode['rack']['files']['deleteFile']['OK'];
@@ -295,7 +297,8 @@ $msgcode['ipv4net']['files']['addFile']['ERR'] = $msgcode['rack']['files']['addF
 $msgcode['ipv4net']['files']['updateFile']['OK'] = $msgcode['rack']['files']['updateFile']['OK'];
 $msgcode['ipv4net']['files']['updateFile']['ERR'] = $msgcode['rack']['files']['updateFile']['ERR'];
 $msgcode['ipv4net']['files']['linkFile']['OK'] = $msgcode['rack']['files']['linkFile']['OK'];
-$msgcode['ipv4net']['files']['linkFile']['ERR'] = $msgcode['rack']['files']['linkFile']['ERR'];
+$msgcode['ipv4net']['files']['linkFile']['ERR1'] = $msgcode['rack']['files']['linkFile']['ERR1'];
+$msgcode['ipv4net']['files']['linkFile']['ERR2'] = $msgcode['rack']['files']['linkFile']['ERR2'];
 $msgcode['ipv4net']['files']['unlinkFile']['OK'] = $msgcode['rack']['files']['unlinkFile']['OK'];
 $msgcode['ipv4net']['files']['unlinkFile']['ERR'] = $msgcode['rack']['files']['unlinkFile']['ERR'];
 $msgcode['ipv4net']['files']['deleteFile']['OK'] = $msgcode['rack']['files']['deleteFile']['OK'];
@@ -375,7 +378,8 @@ $msgcode['ipv4vs']['files']['addFile']['ERR'] = $msgcode['rack']['files']['addFi
 $msgcode['ipv4vs']['files']['updateFile']['OK'] = $msgcode['rack']['files']['updateFile']['OK'];
 $msgcode['ipv4vs']['files']['updateFile']['ERR'] = $msgcode['rack']['files']['updateFile']['ERR'];
 $msgcode['ipv4vs']['files']['linkFile']['OK'] = $msgcode['rack']['files']['linkFile']['OK'];
-$msgcode['ipv4vs']['files']['linkFile']['ERR'] = $msgcode['rack']['files']['linkFile']['ERR'];
+$msgcode['ipv4vs']['files']['linkFile']['ERR1'] = $msgcode['rack']['files']['linkFile']['ERR1'];
+$msgcode['ipv4vs']['files']['linkFile']['ERR2'] = $msgcode['rack']['files']['linkFile']['ERR2'];
 $msgcode['ipv4vs']['files']['unlinkFile']['OK'] = $msgcode['rack']['files']['unlinkFile']['OK'];
 $msgcode['ipv4vs']['files']['unlinkFile']['ERR'] = $msgcode['rack']['files']['unlinkFile']['ERR'];
 $msgcode['ipv4vs']['files']['deleteFile']['OK'] = $msgcode['rack']['files']['deleteFile']['OK'];
@@ -451,7 +455,8 @@ $msgcode['ipv4rspool']['files']['addFile']['ERR'] = $msgcode['rack']['files']['a
 $msgcode['ipv4rspool']['files']['updateFile']['OK'] = $msgcode['rack']['files']['updateFile']['OK'];
 $msgcode['ipv4rspool']['files']['updateFile']['ERR'] = $msgcode['rack']['files']['updateFile']['ERR'];
 $msgcode['ipv4rspool']['files']['linkFile']['OK'] = $msgcode['rack']['files']['linkFile']['OK'];
-$msgcode['ipv4rspool']['files']['linkFile']['ERR'] = $msgcode['rack']['files']['linkFile']['ERR'];
+$msgcode['ipv4rspool']['files']['linkFile']['ERR1'] = $msgcode['rack']['files']['linkFile']['ERR1'];
+$msgcode['ipv4rspool']['files']['linkFile']['ERR2'] = $msgcode['rack']['files']['linkFile']['ERR2'];
 $msgcode['ipv4rspool']['files']['unlinkFile']['OK'] = $msgcode['rack']['files']['unlinkFile']['OK'];
 $msgcode['ipv4rspool']['files']['unlinkFile']['ERR'] = $msgcode['rack']['files']['unlinkFile']['ERR'];
 $msgcode['ipv4rspool']['files']['deleteFile']['OK'] = $msgcode['rack']['files']['deleteFile']['OK'];
@@ -518,7 +523,8 @@ $msgcode['user']['files']['addFile']['ERR'] = $msgcode['rack']['files']['addFile
 $msgcode['user']['files']['updateFile']['OK'] = $msgcode['rack']['files']['updateFile']['OK'];
 $msgcode['user']['files']['updateFile']['ERR'] = $msgcode['rack']['files']['updateFile']['ERR'];
 $msgcode['user']['files']['linkFile']['OK'] = $msgcode['rack']['files']['linkFile']['OK'];
-$msgcode['user']['files']['linkFile']['ERR'] = $msgcode['rack']['files']['linkFile']['ERR'];
+$msgcode['user']['files']['linkFile']['ERR1'] = $msgcode['rack']['files']['linkFile']['ERR1'];
+$msgcode['user']['files']['linkFile']['ERR2'] = $msgcode['rack']['files']['linkFile']['ERR2'];
 $msgcode['user']['files']['unlinkFile']['OK'] = $msgcode['rack']['files']['unlinkFile']['OK'];
 $msgcode['user']['files']['unlinkFile']['ERR'] = $msgcode['rack']['files']['unlinkFile']['ERR'];
 $msgcode['user']['files']['deleteFile']['OK'] = $msgcode['rack']['files']['deleteFile']['OK'];
index cc491a5..defbf13 100644 (file)
@@ -1488,15 +1488,29 @@ function addFileToEntity ()
 
 function linkFileToEntity ()
 {
-       assertUIntArg ('entity_id', __FUNCTION__);
        assertUIntArg ('file_id', __FUNCTION__);
-       assertStringArg ('file_name', __FUNCTION__);
-
-       $error = commitLinkFile ($_REQUEST['file_id'], $_REQUEST['entity_type'], $_REQUEST['entity_id']);
+       $etype_by_pageno = array // this 1:1 mapping may change later
+       (
+               'ipv4net' => 'ipv4net',
+               'ipv4rspool' => 'ipv4rspool',
+               'ipv4vs' => 'ipv4vs',
+               'object' => 'object',
+               'rack' => 'rack',
+               'user' => 'user',
+       );
+       global $page, $pageno;
+       $entity_type = $etype_by_pageno[$pageno];
+       $bypass_name = $page[$pageno]['bypass'];
+       assertUIntArg ($bypass_name, __FUNCTION__);
+
+       $fi = getFileInfo ($_REQUEST['file_id']);
+       if ($fi === NULL)
+               return buildRedirectURL ('ERR1'); // file not found
+       $error = commitLinkFile ($_REQUEST['file_id'], $entity_type, $_REQUEST[$bypass_name]);
        if ($error != '')
-               return buildRedirectURL ('ERR', array ($error));
+               return buildRedirectURL ('ERR2', array ($error)); // linking failed
 
-       return buildRedirectURL ('OK', array ($_REQUEST['file_name']));
+       return buildRedirectURL ('OK', array ($fi['name']));
 }
 
 function updateFile ()