Commit 2d2b0f200d620e980c42a149695a645673a0ab6a

Authored by nbm
1 parent 1ccc93a3

Add KTBrowseUtil, which currently only handles converting a path into a

tuple of the folder and document that path refers to.  And, potentially,
an additional label attached at the end of the path.


git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@3603 c91229c3-7414-0410-bfa2-8a42b809f60b
lib/browse/browseutil.inc.php 0 → 100644
  1 +<?php
  2 +/* vim: set expandtab tabstop=4 shiftwidth=4 foldmethod=marker: */
  3 +/**
  4 + * $Id$
  5 + *
  6 + * Utilities helpful to traversing the document repository
  7 + *
  8 + * Copyright (c) 2005 Jam Warehouse http://www.jamwarehouse.com
  9 + *
  10 + * This program is free software; you can redistribute it and/or modify
  11 + * it under the terms of the GNU General Public License as published by
  12 + * the Free Software Foundation; either version 2 of the License, or
  13 + * (at your option) any later version.
  14 + *
  15 + * This program is distributed in the hope that it will be useful,
  16 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18 + * GNU General Public License for more details.
  19 + *
  20 + * You should have received a copy of the GNU General Public License
  21 + * along with this program; if not, write to the Free Software
  22 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23 + *
  24 + * @version $Revision$
  25 + * @author Neil Blakey-Milner <nbm@jamwarehouse.com>, Jam Warehouse (Pty) Ltd, South Africa
  26 + * @package lib.browse
  27 + */
  28 +
  29 +require_once(KT_LIB_DIR . '/actions/documentaction.inc.php');
  30 +
  31 +class KTBrowseUtil {
  32 + // {{{ folderOrDocument
  33 + function folderOrDocument($sPath, $bAction = false) {
  34 + $sFileName = basename($sPath);
  35 + $sFolderPath = dirname($sPath);
  36 +
  37 + $aFolderInfo = KTBrowseUtil::_folderOrDocument($sFolderPath);
  38 +
  39 + if ($aFolderInfo === false) {
  40 + return $aFolderInfo;
  41 + }
  42 +
  43 + list($iFolderID, $iDocumentID) = $aFolderInfo;
  44 +
  45 + if ($iDocumentID && $bAction) {
  46 + $aActions = array_keys(KTDocumentActionUtil::getDocumentActions());
  47 + if (in_array($sFileName, $aActions)) {
  48 + return array($iFolderID, $iDocumentID, $sFileName);
  49 + }
  50 + return false;
  51 + }
  52 +
  53 + $sQuery = "SELECT id FROM folders WHERE parent_id = ? AND name = ?";
  54 + $aParams = array($iFolderID, $sFileName);
  55 + $id = DBUtil::getOneResultKey(array($sQuery, $aParams), 'id');
  56 + if (PEAR::isError($id)) {
  57 + // XXX: log error
  58 + return false;
  59 + }
  60 + if ($id) {
  61 + return array($id, null, null);
  62 + }
  63 +
  64 + $sQuery = "SELECT id FROM documents WHERE folder_id = ? AND filename = ?";
  65 + $aParams = array($iFolderID, $sFileName);
  66 + $iDocumentID = DBUtil::getOneResultKey(array($sQuery, $aParams), 'id');
  67 +
  68 + if (PEAR::isError($iDocumentID)) {
  69 + // XXX: log error
  70 + return false;
  71 + }
  72 +
  73 + if ($iDocumentID) {
  74 + return array($iFolderID, $iDocumentID, null);
  75 + }
  76 +
  77 + if ($bAction) {
  78 + // $aActions = array_keys(KTFolderAction::getFolderActions());
  79 + $aActions = array('ktcore.delete');
  80 + if (in_array($sFileName, $aActions)) {
  81 + return array($iFolderID, null, $sFileName);
  82 + }
  83 + }
  84 + return false;
  85 + }
  86 +
  87 + function _folderOrDocument($sPath) {
  88 + global $default;
  89 + $sFileName = basename($sPath);
  90 + $sFolderPath = dirname($sPath);
  91 +
  92 + $aFolderNames = split('/', $sFolderPath);
  93 +
  94 + $iFolderID = 0;
  95 +
  96 + $aRemaining = $aFolderNames;
  97 + while (count($aRemaining)) {
  98 + $sFolderName = $aRemaining[0];
  99 + $aRemaining = array_slice($aRemaining, 1);
  100 + if ($sFolderName === "") {
  101 + continue;
  102 + }
  103 + $sQuery = "SELECT id FROM folders WHERE parent_id = ? AND name = ?";
  104 + $aParams = array($iFolderID, $sFolderName);
  105 + $id = DBUtil::getOneResultKey(array($sQuery, $aParams), 'id');
  106 + if (PEAR::isError($id)) {
  107 + // XXX: log error
  108 + return false;
  109 + }
  110 + if (is_null($id)) {
  111 + // Some intermediary folder path doesn't exist
  112 + return false;
  113 + }
  114 + $default->log->error("iFolderID set to " . print_r($id, true));
  115 + $iFolderID = (int)$id;
  116 + }
  117 +
  118 + $sQuery = "SELECT id FROM documents WHERE folder_id = ? AND filename = ?";
  119 + $aParams = array($iFolderID, $sFileName);
  120 + $iDocumentID = DBUtil::getOneResultKey(array($sQuery, $aParams), 'id');
  121 +
  122 + if (PEAR::isError($iDocumentID)) {
  123 + // XXX: log error
  124 + return false;
  125 + }
  126 +
  127 + if ($iDocumentID === null) {
  128 + $sQuery = "SELECT id FROM folders WHERE parent_id = ? AND name = ?";
  129 + $aParams = array($iFolderID, $sFileName);
  130 + $id = DBUtil::getOneResultKey(array($sQuery, $aParams), 'id');
  131 + if (PEAR::isError($id)) {
  132 + // XXX: log error
  133 + return false;
  134 + }
  135 + if (is_null($id)) {
  136 + if ($sFileName === "") {
  137 + return array($iFolderID, null);
  138 + }
  139 + // XXX: log error
  140 + return array($iFolderID, false);
  141 + }
  142 + /* if (substr($path, -1) !== "/") {
  143 + header("Location: " . $_SERVER["PHP_SELF"] . "/");
  144 + } */
  145 + return array($id, null);
  146 + }
  147 +
  148 + return array($iFolderID, (int)$iDocumentID);
  149 + }
  150 + // }}}
  151 +}
... ...
tests/browseutil/folderOrDocument.php 0 → 100644
  1 +<?php
  2 +
  3 +require_once("../../config/dmsDefaults.php");
  4 +require_once(KT_LIB_DIR . '/browse/browseutil.inc.php');
  5 +
  6 +error_reporting(E_ALL);
  7 +
  8 +var_dump(KTBrowseUtil::folderOrDocument("/Root Folder/test.sxw"));
  9 +var_dump(KTBrowseUtil::folderOrDocument("/Root Folder/test.sxw/ktcore.delete"));
  10 +var_dump(KTBrowseUtil::folderOrDocument("/Root Folder/test.sxw/ktcore.delete", true));
  11 +var_dump(KTBrowseUtil::folderOrDocument("/Root Folder/Default Unit"));
  12 +var_dump(KTBrowseUtil::folderOrDocument("/Root Folder/Default Unit/ktcore.delete"));
  13 +var_dump(KTBrowseUtil::folderOrDocument("/Root Folder/Default Unit/ktcore.delete", true));
  14 +
  15 +?>
... ...