Folder.inc 16.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
<?php
/**
 * $Id$
 *
 * Represents as folder as the per the folders table in the database.
 *
 * KnowledgeTree Open Source Edition
 * Document Management Made Simple
 * Copyright (C) 2004 - 2008 The Jam Warehouse Software (Pty) Limited
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License version 3 as published by the
 * Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * You can contact The Jam Warehouse Software (Pty) Limited, Unit 1, Tramber Place,
 * Blake Street, Observatory, 7925 South Africa. or email info@knowledgetree.com.
 *
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU General Public License version 3.
 *
 * In accordance with Section 7(b) of the GNU General Public License version 3,
 * these Appropriate Legal Notices must retain the display of the "Powered by
 * KnowledgeTree" logo and retain the original copyright notice. If the display of the
 * logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices
 * must display the words "Powered by KnowledgeTree" and retain the original
 * copyright notice.
 * Contributor( s): ______________________________________
 */

require_once(KT_LIB_DIR . "/foldermanagement/PhysicalFolderManagement.inc");
require_once(KT_LIB_DIR . "/documentmanagement/Document.inc");
require_once(KT_LIB_DIR . "/util/sanitize.inc");

class Folder extends KTEntity {

	/** folder primary key */
	var $iId;
	/** folder name */
	var $sName;
	/** folder description */
	var $sDescription;
	/** folder parent primary key */
	var $iParentID;
	/** primary key of user who created folder */
	var $iCreatorID;
	/** public status of folder */
	var $bIsPublic = false;
	/** comma deliminated string of parent ids */
	var $sParentFolderIDs;
	/** forward slash deliminated path from file system root */
	var $sFullPath;
    /** which permission object I get permissions from */
    var $iPermissionObjectID;
    /** lookup accelerator id */
    var $iPermissionLookupID;
    /** whether to restrict to only certain document types */
    var $bRestrictDocumentTypes = false;

    // {{{ KTEntity stuff
    var $_aFieldToSelect = array(
        'iId' => 'id',
        'sName' => 'name',
        'sDescription' => 'description',
        'iParentID' => 'parent_id',
        'iCreatorID' => 'creator_id',
        'bIsPublic' => 'is_public',
        'sFullPath' => 'full_path',
        'sParentFolderIDs' => 'parent_folder_ids',
        'iPermissionObjectID' => 'permission_object_id',
        'iPermissionLookupID' => 'permission_lookup_id',
        'bRestrictDocumentTypes' => 'restrict_document_types',
    );
    // }}}

	function getID() { return $this->iId; }
	function getName() { return ($this->sName); }
	function setName($sNewValue) { $this->sName = ($sNewValue); }
	function getDescription() { return ($this->sDescription); }
	function setDescription($sNewValue) { $this->sDescription = ($sNewValue); }
	function getParentID() { return $this->iParentID; }
	function setParentID($iNewValue) { $this->iParentID = $iNewValue; }
	function getCreatorID() { return $this->iCreatorID; }
	function setCreatorID($iNewValue) { $this->iCreatorID = $iNewValue; }
	function getIsPublic() { return $this->bIsPublic; }
	function setIsPublic($bNewValue) { $this->bIsPublic = $bNewValue; }
	function getFullPath() { return $this->sFullPath; }
	function getParentFolderIDs() { return $this->sParentFolderIDs; }

	function getPermissionObjectID() { return $this->iPermissionObjectID; }
	function setPermissionObjectID($iPermissionObjectID) { $this->iPermissionObjectID = $iPermissionObjectID; }
	function getPermissionLookupID() { return $this->iPermissionLookupID; }
	function setPermissionLookupID($iPermissionLookupID) { $this->iPermissionLookupID = $iPermissionLookupID; }

	function getRestrictDocumentTypes() { return $this->bRestrictDocumentTypes; }
	function setRestrictDocumentTypes($bRestrictDocumentTypes) { $this->bRestrictDocumentTypes = $bRestrictDocumentTypes; }

    // {{{ create()
    function create () {
        $oParentFolder =& Folder::get($this->iParentID);
        $this->iPermissionObjectID = $oParentFolder->getPermissionObjectID();
        $res = parent::create();
        if ($res === true) {
            KTPermissionUtil::updatePermissionLookup(Folder::get($this->getId()));
        }
        return $res;
    }
    // }}}

        /**
         * Returns a comma delimited string containing the parent folder ids, strips leading /
         *
         * @return String	comma delimited string containing the parent folder ids
         */
        function generateFolderIDs($iFolderId) {
            if (empty($iFolderId)) {
                    return;
            }
            $oFolder =& Folder::get($iFolderId);
            if (PEAR::isError($oFolder)) {
                return $oFolder;
            }
            $iParentId = $oFolder->getParentId();
            if (empty($iParentId)) {
                return $oFolder->getId();
            }
            $oParentFolder =& Folder::get($iParentId);
            if (PEAR::isError($oParentFolder)) {
                return $oParentFolder;
            }
            $sParentFolderParentFolderIds = $oParentFolder->getParentFolderIDs();
            if (empty($sParentFolderParentFolderIds)) {
                return sprintf('%s,%s', $iParentId, $oFolder->getId());;
            }
            return sprintf('%s,%s,%s', $sParentFolderParentFolderIds, $iParentId, $oFolder->getId());
        }

	/**
	 * Recursively generates forward slash deliminated string giving full path of document
	 * from file system root url
	 */
	function generateFullFolderPath($iFolderId) {
		//if the folder is not the root folder
		if ($iFolderId == 0) {
            return;
        }
		if ($iFolderId == 1) {
            $oFolder =& Folder::get(1);
            return $oFolder->getName();
        }

        $oFolder =& Folder::get($iFolderId);
		if (PEAR::isError($oFolder)) {
            global $default;
            $default->log->error("Invalid folder passed to generateFullFolderPath: %s", print_r($oFolder, true));
            return $oFolder;
        }
        $iParentId = $oFolder->getParentId();
        if (empty($iParentId)) {
            return $oFolder->getName();
        }
        $res = Folder::generateFullFolderPath($iParentId);
        if (PEAR::isError($res)) {
            return $res;
        }
        return sprintf('%s/%s', $res, $oFolder->getName());
	}

	/**
	 * Returns a forward slash deliminated string giving full path of document, strips leading /
	 */
	function generateFolderPath($iFolderID) {
		$sPath = Folder::generateFullFolderPath($iFolderID);
		return $sPath;
	}

    function _fieldValues () {
    	if ($this->getId() == 1)
    	{
			$this->sFullPath = null;
			$this->sParentFolderIDs = null;
    	}
    	else
    	{
    		$parent = Folder::get($this->iParentID);
        	$this->sFullPath = $parent->getFullPath();
        	if (!empty($this->sFullPath)) $this->sFullPath .= '/';
        	$this->sFullPath .= $this->sName;
        	$this->sParentFolderIDs = $parent->getParentFolderIDs();
			if (!empty($this->sParentFolderIDs)) $this->sParentFolderIDs .= ',';
        	$this->sParentFolderIDs .= $this->iParentID;
    	}
        return parent::_fieldValues();
        return array(
            'name' => $this->sName,
            'description' => $this->sDescription,
            'parent_id' => $this->iParentID,
            'creator_id' => $this->iCreatorID,
            'is_public' => KTUtil::anyToBool($this->bIsPublic),
            'full_path' => $this->sFullPath,
            'parent_folder_ids' => $this->sParentFolderIDs,
            'permission_object_id' => $this->iPermissionObjectID,
            'permission_lookup_id' => $this->iPermissionLookupID,
        );
    }

    function _table () {
        global $default;
        return $default->folders_table;
    }

	/**
	* Update the current folder values in the database
	*
	* @return boolean true on successful update, false otherwise and set $_SESSION["errorMessage"]
	*/
	function update($bPathChange = false) {
        $res = parent::update();
        if ($res === true) {
            if ($bPathChange) {
                // XXX: TransactionCheckPoint
                $this->updateChildPaths($this->iId);
                $this->updateDocumentPaths($this->iId);
            }
        }
        return $res;
	}

	function renameFolder($sOldPath) {
		PhysicalFolderManagement::renameFolder($sOldPath, $default->documentRoot . "/" . $this->sFullPath . "/" . $this->sName);
	}

	/**
	* When a folder is renamed, we must update
	* the paths of the children in the database
	*
	*/
	function updateChildPaths($iId) {
		global $default;
		//get the direct children
        $sql = $default->db;
		$aFolders =& Folder::getByParentId($iId);
		foreach ($aFolders as $oFolder) {
			$oFolder->update(true);
		}
		return;
	}

    /**
     * When a folder's path changes, we must update the paths in the
     * documents in that folder.  Sub-folders are handled elsewhere in
     * update().
     */

    function updateDocumentPaths($iId) {
        $aDocuments = Document::getList(array('folder_id = ?', $iId));
        if (PEAR::isError($aDocuments)) {
            return $aDocuments;
        }
        foreach ($aDocuments as $oDocument) {
            // Document->update() automatically adjusts the path.
            $oDocument->update();
            // XXX: Should handle failure here somehow, but rather get
            // most working than just the first two.  Must find a sane
            // way to handle transactions.
            // TransactionCheckPoint
        }
        return true;
    }

    /**
     * Returns the documents in this folder
     */
    function getDocumentIDs($iFolderID) {
		$sTable = KTUtil::getTableName('documents');
		$aQuery = array("SELECT id FROM {$sTable} WHERE folder_id = ?", array($iFolderID));
		$res = DBUtil::getResultArrayKey($aQuery,'id');
		if (PEAR::isError($res)) {
		    return $res;
		}
		return implode(',', $res);
    }

    function &get($iFolderID) {
        return KTEntityUtil::get('Folder', $iFolderID);
    }

	/**
	 * Checks if a folder with the same name and parent exists in the database already
	 */
	function exists() {
        $sTable = KTUtil::getTableName('folders');
        $sQuery = "SELECT count(*) as folder_count FROM $sTable WHERE name = ? AND parent_id = ?";/*ok*/
        $aParams = array($this->sName, $this->iParentID);
        $res = DBUtil::getOneResultKey(array($sQuery, $aParams), 'folder_count');
        if (PEAR::isError($res)) {
            return false; // return $res
        }
        return ($res != 0); // handle pre-existing duplicates gracefully.
	}

	/**
    * Static function
    * Get a list of Documents
    *
    * @param  String  Where clause (not required)
    *
    * @return Array array of Documents objects, false otherwise and set $_SESSION["errorMessage"]
    */
    function getList($sWhereClause = null, $aOptions = null) {
        return KTEntityUtil::getList2('Folder', $sWhereClause, $aOptions);
    }

	/**
	* Static function.
	* Get the full path for a folder
	*
	* @param 	Primary key of folder to generate path for
	*
	* @return String full path of folder
	*/
	function getFolderPath($iFolderID) {
		global $default;
		$oFolder = Folder::get($iFolderID);
		$sPath = $default->documentRoot . "/" . $oFolder->getFullPath() . "/" . $oFolder->getName() . "/";
		return $sPath;
	}

	/**
     * Static function.
     * Get the full path for a folder as an array
     *
     * @param int primary key of folder to generate path for
     *
     * @return array full path of folder as an array of folderIDs
     */
    function getFolderPathNamesAsArray($iFolderID) {
		global $default;


		$oFolder = Folder::get($iFolderID);
		$aPathArray = array();
		if ($oFolder) {
			if (strlen($oFolder->getFullPath()) > 0) {
				if (strlen($oFolder->getFullPath()) > 1) {
					$aPathArray = explode("/",$oFolder->getFullPath());
				} else {
					$aPathArray = array($oFolder->getFullPath());
				}

				//$aPathArray[count($aPathArray)] = $oFolder->getName();
			} else {
				$aPathArray = array($oFolder->getName());
			}
		}
		return $aPathArray;
    }

    // {{{
    function getPathArray() {
        return Folder::getFolderPathNamesAsArray($this->getID());
    }
    // }}}

	/**
     * Static function.
     * Get the full path for a folder as an array
     *
     * @param int primary key of folder to generate path for
     *
     * @return array full path of folder as an array of folderIDs
     */
    function getFolderPathAsArray($iFolderID) {
		global $default;
		$oFolder = Folder::get($iFolderID);
        if ($oFolder === false) {
            return false;
        }
		if (strlen($oFolder->getParentFolderIDs()) > 0) {
            if ($oFolder->iParentID == 0) {
                $aPathArray = array();
            } else if (strlen($oFolder->getParentFolderIDs()) > 1) {
				$aPathArray = explode(",",$oFolder->getParentFolderIDs());
			} else {
				$aPathArray = array($oFolder->getParentFolderIDs());
			}
			$aPathArray[count($aPathArray)] = $oFolder->getID();
		} else {
			$aPathArray = array($oFolder->getID());
		}
		return $aPathArray;
    }

	/**
	* Static function.
	* Get the path for a folder that will be displated to the user
	*
	* @param 	Primary key of folder to generate path for
	*
	* @return String full path of folder
	*/
	function getFolderDisplayPath($iFolderID) {
		global $default;
        $aPathNamesArray = Folder::getFolderPathNamesAsArray($iFolderID);

        foreach($aPathNamesArray as $k=>$v)
        {
            $aPathNamesArray[$k] = sanitizeForHTML($v);
        }
        if (count($aPathNamesArray) > 0) {
        	return implode(" &raquo; ", $aPathNamesArray);
        } else {
        	return "";
        }
	}

	/**
	* Static function
	* Get the primary key of the parent folder
	*
	* @param $iFolderID		Primary key of folder to get parent for
	*
	* @return integer primary key of parent folder
	*/
	function getParentFolderID($iFolderID) {
		if ($iFolderID != 0) {
		    $oFolder = Folder::get($iFolderID);
			return $oFolder->getParentFolderID();
		}
		return 0;
	}

	/**
	* Static function
	* Checks if a given folder already exists using the folder name
	*
	* @param $sName			Name of folder
	* @param $iParentID		Primary key of parent folder
	*
	* @return	true if the folder exists, false otherwise and set $_SESSION["errorMessage"]
	*/
	function folderExistsName($sName, $iParentID) {
        $sQuery = "SELECT id, name FROM " . KTUtil::getTableName('folders') . " WHERE name = ? AND parent_id = ?";/*ok*/
        $aParams = array($sName, $iParentID);
		$res = DBUtil::getResultArray(array($sQuery, $aParams));
		//var_dump($res);
		if (count($res) != 0) {

		    // mysql is case-insensitive - check using php
		    foreach ($res as $folder){
    		    $name = isset($folder['name']) ? $folder['name'] : '';
    		    if($name == $sName){
    		        return true;
    		    }
		    }
			return false;
		}
		return false;
	}

	/**
	* Checks if a given folder already exists using the folder name
	*
	* @param $iFolderID			Primary key of folder
	*
	* @return	true if the folder exists, false otherwise and set $_SESSION["errorMessage"]
	*/
	function folderExistsID($iFolderID) {
		$oFolder = Folder::get($iFolderID);
		if (PEAR::isError($oFolder)) {
		    return false; // no such folder, or bad ID
		} else {
		    return true;
		}
	}

	/**
	* Get the folder name using the primary key
	*
	* @param int primary key of folder to get name for
	*
	* @return String name on success, false otherwise and set $_SESSION["errorMessage"]
	*/
	function getFolderName($iFolderID) {
		$oFolder = Folder::get($iFolderID);
		if (PEAR::isError($oFolder)) {
		    return false; // return $oFolder;
		} else {
		    return $oFolder->getName();
		}
	}


    function getByParentIDAndLookupID($iParentID, $iLookupID) {
        return KTEntityUtil::getByDict('Folder', array(
            'parent_id' => $iParentID,
            'permission_lookup_id' => $iLookupID,
        ), array('multi' => true));
    }

	function getByParentId($iParentID) {
	    return KTEntityUtil::getByDict('Folder', array(
            'parent_id' => $iParentID,
        ), array('multi' => true));
	}

    // STATIC
    function &createFromArray($aOptions) {
        return KTEntityUtil::createFromArray('Folder', $aOptions);
    }

    function clearAllCaches() {
    	$GLOBALS["_OBJECTCACHE"]['Folder'] = array();
        return KTEntityUtil::clearAllCaches('Folder');
    }

}

?>