<?php

/**
 * Webdav file system cache.
 * 
 * $_SESSION['WebdavCache'] => array(
 *   $type => array(
 *     $id => array(
 *       'name'         => '', 
 *       'realpath'     => '', 
 *       'size'         => '', 
 *       'mimetype'     => '', 
 *       'K_TYP_KORESP' => ''
 *     )
 *   ),
 *   $special_type = array(
 *     $id => $type
 *   ),
 *   '_pisma_all' => array(
 *     $pismoID => array(
 *       $ind => array(
 *         'name' => '',
 *         'K_TYP_KORESP' => ''
 *       )
 *     )
 *   )
 * )
 * 
 * $type:
 *   real:    PISMO    - (IN7_DZIENNIK_KORESP_COLUMN)
 *   real:    PROJEKT  - (IN7_MK_BAZA_DYSTRYBUCJI_COLUMN)
 *   real:    DRUK     - (CRM_LISTA_ZASOBOW_COLUMN)
 *   virtual: PROJEKT_PISMA_{$id} - pisma związane w projektem
 *   virtual: PROJEKTY_OSTATNIE
 * 
 * usage:
 *   array get('PISMO', 123, 'realpath') - returns file realpath if exists in cache
 *   array get('PISMO', 123) - returns file params
 *   array get('PISMO', null, 'getIds') - returns array of ids in cache
 *   array get('PROJEKT_PISMA_{$id}', null, 'getIds') - returns array of ids in cache
 *   array get('PROJEKT_PISMA_{$id}', null, 'getFileNames') - returns array of file names in cache
 *   set('PROJEKT', 123, 'realpath', 'proj_dir_name') - 
 */
class WebdavCache {

	/**
	 * @param $type - PROJEKT, PISMO, DRUK, PROJEKT_PISMA
	 */
	function get($type, $id = null, $task = '') {
		if (false !== strpos($type, 'PISM')) {
			self::_cacheFileInitPisma();
		}

		if ($task == 'getIds') {
			if (isset($_SESSION['WebdavCache'][$type])) {
				return array_keys($_SESSION['WebdavCache'][$type]);
			}
		}
		else if ($task == 'getFileNames') {
			$fileNames = array();
			if (isset($_SESSION['WebdavCache'][$type])) {
				foreach ($_SESSION['WebdavCache'][$type] as $kFileID => $v) {
					if (is_array($v)) {
						if (isset($v['name'])) {
							$fileNames[] = $v['name'];
						}
						else {
							foreach ($v as $vFile) {
								if (isset($vFile['name'])) {
									$fileNames[] = $vFile['name'];
								}
							}
						}
					}
					else if (array_key_exists($v, $_SESSION['WebdavCache'])) {
						if (isset($_SESSION['WebdavCache'][$v][$kFileID])) {
							if (is_array($_SESSION['WebdavCache'][$v][$kFileID])) {
								if (isset($_SESSION['WebdavCache'][$v][$kFileID]['name'])) {
									$fileNames[] = $_SESSION['WebdavCache'][$v][$kFileID]['name'];
								}
								else {
									foreach ($_SESSION['WebdavCache'][$v][$kFileID] as $vFile) {
										if (isset($vFile['name'])) {
											$fileNames[] = $vFile['name'];
										}
									}
								}
							}
						}
					}
					else {
					}
				}
				return $fileNames;
			}
		}
		else {
			if (isset($_SESSION['WebdavCache'][$type][$id])) {
				switch ($task) {
					case '':
						return $_SESSION['WebdavCache'][$type][$id];
						break;
					case 'name':
					case 'realpath':
					case 'size':
					case 'mimetype':
					case 'K_TYP_KORESP':
						if (isset($_SESSION['WebdavCache'][$type][$id])) {
							if (isset($_SESSION['WebdavCache'][$type][$id][$task])) {
								return $_SESSION['WebdavCache'][$type][$id][$task];
							}
							else if (is_array($_SESSION['WebdavCache'][$type][$id])) {
								$ret = array();
								foreach ($_SESSION['WebdavCache'][$type][$id] as $v) {
									$val =  V::get($task, null, $v);
									if (!empty($val)) {
										$ret[] = $val;
									}
								}
								return $ret;
							}
						}
						break;
					default:
						
				}
			}
		}
		return null;
	}

	function set($type, $id, $task = '', $value = null) {
		if (!isset($_SESSION['WebdavCache'])) $_SESSION['WebdavCache'] = array();
		if (!isset($_SESSION['WebdavCache'][$type])) $_SESSION['WebdavCache'][$type] = array();
		if (!isset($_SESSION['WebdavCache'][$type][$id])) $_SESSION['WebdavCache'][$type][$id] = array();

		switch ($task) {
			case 'name':
				$_SESSION['WebdavCache'][$type][$id]['name'] = $value;
				break;
			case 'realpath':
				$_SESSION['WebdavCache'][$type][$id]['realpath'] = $value;
				break;
			case 'size':
				$_SESSION['WebdavCache'][$type][$id]['size'] = $value;
				break;
			case 'mimetype':
				$_SESSION['WebdavCache'][$type][$id]['mimetype'] = $value;
				break;
			case 'K_TYP_KORESP':
				$_SESSION['WebdavCache'][$type][$id]['K_TYP_KORESP'] = $value;
				break;
			default:
				if ($type == 'PROJEKTY_OSTATNIE') {
					$_SESSION['WebdavCache']['PROJEKTY_OSTATNIE'][$id] = 'PROJEKT';
				}
		}
	}

	/**
	 * Fetch all files PISMA in cache array.
	 * 
	 * use $_SESSION['WebdavCache']['_pisma_all']
	 */
	function _cacheFileInitPisma() {
		if (!empty($_SESSION['WebdavCache']['_pisma_all'])) {
			//myLog(__CLASS__, __FUNCTION__, __LINE__, " glob cache time (".(time() - $_SESSION['WebdavCache']['_pisma_all']['_ts']).") ...");
			if (time() - $_SESSION['WebdavCache']['_pisma_all']['_ts'] < 300) {
				return;
			}
		}
		$_SESSION['WebdavCache']['_pisma_all'] = array();
		$_SESSION['WebdavCache']['_pisma_all']['_ts'] = time();
		$basePath = WebdavHelper::getBaseDir('IN7_DZIENNIK_KORESP_COLUMN');
		myLog(__CLASS__, __FUNCTION__, __LINE__, " glob start ...");
		$files = glob("{$basePath}/*/*.*", GLOB_NOSORT);
		for ($i = 0, $cnt = count($files); $i < $cnt; $i++) {
			$fileID = 0;
			$file = $files[$i];
			$file = str_replace($basePath . '/', '', $file);
			$fileParts = explode('/', $file);
			if (count($fileParts) == 2) {
				list($fileID, $tmp) = explode('.', $fileParts[1], 2);
			}
			if ($fileID > 0) {
				$_SESSION['WebdavCache']['_pisma_all'][$fileID][] = array('name'=>$fileParts[1], 'realpath'=>$file,'K_TYP_KORESP'=>$fileParts[0]);
			}
		}
		myLog(__CLASS__, __FUNCTION__, __LINE__, " glob end count(".count($files).") _pisma_all(".count($_SESSION['WebdavCache']['_pisma_all']).") ");
	}

	function setPismaByIds($projektID, $pismaIds) {
		self::_cacheFileInitPisma();
		//$_SESSION['WebdavCache']['_pisma_all'][$fileID][] = array('realpath'=>$file, 'K_TYP_KORESP'=>$fileParts[0]);
		if (empty($pismaIds)) {
			return;
		}
		foreach ($pismaIds as $pismoID) {
			if (!empty($_SESSION['WebdavCache']['_pisma_all'][$pismoID])) {
				foreach ($_SESSION['WebdavCache']['_pisma_all'][$pismoID] as $kInd => $vPismo) {
					$_SESSION['WebdavCache']['PISMO'][$pismoID][] = $vPismo;
					$_SESSION['WebdavCache']['PROJEKT_PISMA_' . $projektID][$pismoID] = 'PISMO';
				}
			}
		}
	}

}
