ContentManager
<?php require_once "autoload.php";
/* The ContentManager interfaces between this application and the server calls to retrieve resources
(any UGC).
*/
class ContentManager extends Bean
{
private static $instance;
public static function getInstance()
{
if (!isset(self::$instance))
{
$c = __CLASS__;
self::$instance = new $c;
}
return self::$instance;
}
/* Fetches a the specific resource at $pathCollection and stores it into &$resources. This function
is called by getChildResources and will return true on success, false on failure.
*/
public function getResources($pathCollection, &$resources)
{
$ret = false;
$resources = $resources ? $resources : array();
do
{
foreach ($pathCollection as $path)
{
if (preg_match("/^\.?\/.*$/", $path))
{
$localCollection[] = $path;
}
else
{
$remoteCollection[] = $path;
}
}
$cm = CommunicationManager::getInstance();
if ($remoteCollection && !$cm->get("deviceId"))
{
break;
// print_r($remoteCollection);
// modal transactional confirmation
}
if ($remoteCollection && $cm->get("deviceId"))
{
$resources = array_merge($resources, $cm->getResources($remoteCollection));
}
$ret = true;
if (!$localCollection)
{
break;
}
$fm = FileManager::getInstance();
$rt = ResourceTable::getInstance();
foreach ($localCollection as $path)
{
$filepath = preg_replace("/^\./", "./fs", $path);
$resource = $rt->getResource($path);
$resources[] = $resource;
}
} while (false);
return $ret;
}
/* Retrieves all resources in the subdirectories of $pathCollection and stores them into &$resources
Returns true if successful, or false if unsuccesful.
*/
public function getChildResources($pathCollection, &$resources)
{
$ret = false;
$resources = $resources ? $resources : array();
do
{
foreach ($pathCollection as $path)
{
if (!$path)
{
$this->getResources(array("/", "./USER/PRINTS/", "./DEVICE/"), $resources);
continue;
}
if (preg_match("/^\.\/.*$/", $path))
$localCollection[] = $path;
else
{
$remoteCollection[] = $path;
}
}
$cm = CommunicationManager::getInstance();
if ($remoteCollection && !$cm->get("deviceId"))
{
break;
// print_r($remoteCollection);
// modal transactional confirmation
}
if ($remoteCollection && $cm->get("deviceId"))
{
$childResources = $cm->getChildResources($remoteCollection);
$childResources = $childResources ? $childResources : array();
$resources = array_merge($resources,$childResources);
//$resources = array_merge($resources, $cm->getChildResources($remoteCollection));
}
$ret = true;
if (!$localCollection)
{
break;
}
$pm = PersistenceManager::getInstance();
$fm = FileManager::getInstance();
foreach ($localCollection as $path)
{
$filepath = preg_replace("/^\./", "./fs", $path);
$dir = $fm->dir($filepath, "dir");
foreach ($dir as $file)
{
if (preg_match("/^\..*$/", $file))
continue;
$this->getResources(array($path.$file."/"), $resources);
}
$ids = $pm->load($path);
if (!$ids)
continue;
$ret &= $this->getResources($ids, $resources);
}
} while (false);
return $ret;
}
}
?>
Bean
<?php include_once "autoload.php";
abstract class Bean
{
public function set($field, &$value) { $this->$field = $value; }
public function &get($field) { return $this->$field; }
public function __construct()
{
}
public function map_to_vars(&$map)
{
foreach (array_keys(get_object_vars($this)) as $key)
{
$this->$key = $map[$key];
}
}
}
?>