[castleparadox] / trunk / manager / resourceManager.php Repository:
ViewVC logotype

View of /trunk/manager/resourceManager.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 73 - (download) (annotate)
Fri Dec 4 09:17:24 2009 UTC (9 years ago) by inferiorminion
File size: 7595 byte(s)
Bug #30: Implement Project Soft Delete 
https://bugs.castleparadox.com/show_bug.cgi?id=30

<?php
/**
 * @package castleparadox
 * @subpackage manager
 * @version resourceManager.php, Jan 6, 2008 5:00:41 AM 
 * @author inferiorminion
 * @copyright Copyright &copy; 2008, Castle Paradox
 */
 
set_include_pathget_include_path() . PATH_SEPARATOR $_SERVER['DOCUMENT_ROOT'] );

include_once 
'./constants.php';
include_once 
'./manager/archiveManager.php';
include_once 
'./manager/databaseManager.php';
include_once 
'./manager/graphicManager.php';
include_once 
'./manager/fileManager.php';
include_once 
'./manager/projectManager.php';
 
/**
 * ResourceManager Class
 * @package castleparadox
 * @subpackage manager
 */
class ResourceManager {
    public static function 
getResource($id) {
        
$db DatabaseManager::getDatabase();
        
$sql 'SELECT type' .
                
' FROM ' RESOURCES_TABLE .
                
' WHERE id = ' $id;
                
        if ( !(
$result $db->sql_query($sql)) ) {
            
LayoutManager::fatalMessage('Database Error''Could not obtain Resource information'__LINE____FILE__$sql);
        }
    
        if ( !(
$resource $db->sql_fetchrow($result)) ) {
            
LayoutManager::fatalMessage('Database Error''Could not obtain Resource information'__LINE____FILE__$sql);
        }
        
        
$db->sql_freeresult($result);
        
        switch(
$resource['type']) {
            case 
Resource::$PROJECT:
                return 
ProjectManager::getProject($idtrue);
                break;
                
            case 
Resource::$ARCHIVE:
                return 
ArchiveManager::getArchive($id);
                break;
                
            case 
Resource::$GRAPHIC:
                return 
GraphicManager::getGraphic($id);
                break;
        }
        return 
null;
    }
    
    public static function 
getProjectResources($projectid$type null) {
        
$db DatabaseManager::getDatabase();
        
$sql 'SELECT r.id, r.type ' .
                
'FROM ' RESOURCES_TABLE ' r, ' PROJECT_RESOURCES_TABLE ' pr ' .
                
'WHERE pr.project = ' $projectid ' ' .
                    
'AND r.id = pr.resource ';
        if ( 
$type != null ) {
            
$sql .= 'AND r.type = ' $type;
        }
        
        if ( !(
$result $db->sql_query($sql)) ) {
            
LayoutManager::fatalMessage('Database Error''Could not obtain Resource information'__LINE____FILE__$sql);
        }
        
        
$rids = array();
        while ( 
$row $db->sql_fetchrow($result) ) {
            
$rids[] = $row;
        }
        
$db->sql_freeresult($result);

        
$resources = array();
        foreach( 
$rids as $resource ) {
            switch(
$resource['type']) {
                case 
Resource::$PROJECT:
                    
$resources[] = ProjectManager::getProject($resource['id']);
                    break;
                    
                case 
Resource::$ARCHIVE:
                    
$resources[] = ArchiveManager::getArchive($resource['id']);
                    break;
                    
                case 
Resource::$GRAPHIC:
                    
$resources[] = GraphicManager::getGraphic($resource['id']);
                    break;
            }
        }
        return 
$resources;
    }
    
    public static function 
getResourceProjects($resourceid$type null) {
        
$db DatabaseManager::getDatabase();
        
$sql 'SELECT p.id, p.type ' .
                
'FROM ' PROJECTS_TABLE ' p, ' PROJECT_RESOURCES_TABLE ' pr ' .
                
'WHERE pr.resource = ' $resourceid ' ' .
                    
'AND p.id = pr.project ';
        if ( 
$type != null ) {
            
$sql .= 'AND p.type = ' $type;
        }
        
        if ( !(
$result $db->sql_query($sql)) ) {
            
LayoutManager::fatalMessage('Database Error''Could not obtain Resource information'__LINE____FILE__$sql);
        }
        
        
$pids = array();
        while ( 
$row $db->sql_fetchrow($result) ) {
            
$pids[] = $row;
        }
        
$db->sql_freeresult($result);

        
$projects = array();
        foreach( 
$pids as $project ) {
            
$projects[] = ProjectManager::getProject($project['id']);
        }
        return 
$projects;
    }
    
    public static function 
submitResource($user$request$file) {
        
$db DatabaseManager::getDatabase();
        
//  Wrap everything in a transaction.  This is an all or nothing method
        
$db->sql_transaction('begin');
        
        
$myResource null;
        if(
$request['resourceID'] > ) {
            
$myResource ResourceManager::getResource($request['resourceID']);
            
ResourceManager::authorizeUser($myResource$user);
        } else {
            switch(
$request['type']) {
                case 
Resource::$GRAPHIC:
                    
$myResource = new Graphic();
                    break;
                case 
Resource::$ARCHIVE:
                    
$myResource = new Archive();
                    break;
            }
        }
        
$myResource->setName($request['name']);
        
$myResource->setCaption($request['caption']);
        if (
is_uploaded_file($file['userfile']['tmp_name']) ) {
            
preg_match("|\.([a-z0-9]{2,4})$|i"$file['userfile']['name'], $fileSuffix);
            if (
array_key_exists('datatype'$request) ) {
                if ( !
in_array($file['userfile']['type'], $request['datatype']) ) {
                    
$datatypes '';
                    
$counter 0;
                    foreach(
$request['datatype'] as $datatype) {
                        
$counter $counter 1;
                        if( 
strlen($datatypes) > ) {
                            if( 
count($request['datatype']) > ) {
                                
$datatypes .= ',';
                            }
                            
$datatypes .= ' ';
                            if( 
$counter == count($request['datatype']) ) {
                                
$datatypes .= 'or ';
                            }
                        }
                        
$datatypes .= $datatype;
                    }
                    
LayoutManager::addError('userfile''Wrong Data Type: Expected ' $datatypes ' and received ' $file['userfile']['type']);
                }
            }
            if (
array_key_exists('extension'$request) ) {
                if ( !
in_array($fileSuffix[0], $request['extension']) ) {
                    
$extensions '';
                    
$counter 0;
                    foreach(
$request['extension'] as $extension) {
                        
$counter $counter 1;
                        if( 
strlen($extensions) > ) {
                            if( 
count($request['extension']) > ) {
                                
$extensions .= ',';
                            }
                            
$extensions .= ' ';
                            if( 
$counter == count($request['extension']) ) {
                                
$extensions .= 'or ';
                            }
                        }
                        
$extensions .= $extension;
                    }
                    
LayoutManager::addError('userfile'print_r($request['extension']) . 'Wrong File Extension: Expected ' $extensions ' and received ' $fileSuffix[0]);
                }
            }
            switch( 
$myResource->getResourceType() ) {
                case 
Resource::$GRAPHIC:
                    
$properties getimagesize ($file['userfile']['tmp_name']);
                    if ( (
$properties[0] != 320) || ($properties[1] != 200) ) {
                        
LayoutManager::addError('userfile''Wrong Image Size: Expected 320 x 200 and received ' $properties[0] . ' x ' $properties[1]);
                    }
                    break;
            }
            if (!
LayoutManager::hasErrors()) {
                
$myResource->persistObject();
                
FileManager::insertFile($myResource->getResourceID(), $file['userfile']['tmp_name'], $file['userfile']['type'], $fileSuffix[0]);
                if ( 
$request['mode'] == 'add') {
                    
ProjectManager::addResource($request['project'], $myResource->getResourceID());
                }
            }
        }
        
$db->sql_transaction('commit');
        return 
$myResource;
    }
    
    public static function 
modifyResourceDeleted($resource$user$status) {
        if( 
ResourceManager::authorizeUser($resource$userfalse) ) {
            
$db DatabaseManager::getDatabase();
            
$db->sql_transaction('begin');
            
$resource->setDeleted($status);
            
$resource->persistObject();
            
$db->sql_transaction('commit');
            return 
true;
        }
        return 
false;
    }
    
    public static function 
authorizeUser($resource$user$displayError true) {
        if( 
$user->data['user_type'] == USER_FOUNDER ) {
            return 
true;
        }
        if( 
$resource->getResourceType() == Resource::$PROJECT ) {
            foreach(
$resource->getTeam() as $member ) {
                if ( 
$member->getUserID() == $user->data['user_id'] ) {
                    return 
true;
                }
            }
        } else {
            foreach(
ResourceManager::getResourceProjects($resource->getResourceID()) as $project ) {
                foreach(
$project->getTeam() as $member ) {
                    if ( 
$member->getUserID() == $user->data['user_id'] ) {
                        return 
true;
                    }
                }
            }
        }
        if( 
$displayError ) {
            
LayoutManager::fatalMessage('Authorization Error''Not Authorized to edit Resource'__LINE____FILE__'Resource id = ' $resource->getResourceID());
        } else {
            return 
false;
        }
    }
}
?>

Inferior Minion
ViewVC Help
Powered by ViewVC 1.0.4