Skip to content
This repository has been archived by the owner on Sep 10, 2021. It is now read-only.

Commit

Permalink
ENH: Added download part
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles Marion committed Mar 1, 2011
1 parent 298562f commit 8428aab
Show file tree
Hide file tree
Showing 16 changed files with 4,267 additions and 4 deletions.
3 changes: 2 additions & 1 deletion application/Bootstrap.php
Expand Up @@ -23,7 +23,6 @@ protected function _initDoctype()
*/
protected function _initConfig()
{
date_default_timezone_set('Europe/Paris');
Zend_Loader::loadClass( "UserDao", BASE_PATH . '/application/models/dao');
Zend_Loader::loadClass( "ItemDao", BASE_PATH . '/application/models/dao');
if (isset($_POST['sid']))
Expand All @@ -39,6 +38,8 @@ protected function _initConfig()

$config = new Zend_Config_Ini(APPLICATION_CONFIG, $configGlobal->environment);
Zend_Registry::set('config', $config);

date_default_timezone_set('Europe/Paris');
// InitDatabase
$configDatabase = new Zend_Config_Ini(DATABASE_CONFIG, $configGlobal->environment);
if ($configDatabase->database->type == 'pdo')
Expand Down
1 change: 1 addition & 0 deletions application/controllers/BrowseController.php
Expand Up @@ -32,6 +32,7 @@ public function indexAction()

$javascriptText=array();
$javascriptText['view']=$this->t('View');
$javascriptText['download']=$this->t('Download');
$javascriptText['edit']=$this->t('Edit');
$javascriptText['delete']=$this->t('Delete');
$javascriptText['share']=$this->t('Share');
Expand Down
194 changes: 194 additions & 0 deletions application/controllers/DownloadController.php
@@ -0,0 +1,194 @@
<?php

/**
* AJAX request for the admin Controller
*/
class DownloadController extends AppController
{
public $_models=array("Folder",'Item');
public $_daos=array();
public $_components=array();

/*index
* @param ?folders=12-13 (will download a zip of the folder 12 and 13 ,recusively)
* @param ?folders=12,1-13,1 (will download a zip of the folder 12 and 13 ,recusively) //Need testing
* @param ?items=12-13 (will download a zip containing the last revisions of the items 12 and 13)
* @param ?items=12,1-13 (will download a zip containing the revision 1 of item 12 and last revision of item 13)
* @param ?items=12,1 (will download the revision 1 of the item 12, a zip if there are multiple bitstream or simply the file)
*/
public function indexAction()
{
$this->_helper->layout->disableLayout();
$itemIds=$this->_getParam('items');
$folderIds=$this->_getParam('folders');
if(!isset($itemIds)&&!isset($folderIds))
{
throw new Zend_Exception("No parameters");
}
$folderIds=explode('-',$folderIds);
$folders=array();
foreach($folderIds as $folderId)
{
$tmp=explode(',', $folderId);
if(empty($tmp[0]))
{
continue;
}
$folder=$this->Folder->load($tmp[0]);
if($folder==false)
{
continue;
}
if(!isset($tmp[0])||$tmp[0]==1)
{
$folder->recursive=true;
}
else
{
$folder->recursive=false;
}
}
$folders= $this->Folder->load($folderIds);

$itemIds=explode('-',$itemIds);
$revisions=array();
if(!empty($itemIds))
{
foreach($itemIds as $itemId)
{
// check revision
$tmp=explode(',', $itemId);
if(empty($tmp[0]))
{
continue;
}
$item=$this->Item->load($tmp[0]);
if($item==false||!$this->Item->policyCheck($item,$this->userSession->Dao))
{
continue;
}
if(isset($tmp[1]))
{
$tmp=$this->Item->getRevision($item,$tmp[1]);
if($tmp!==false)
{
$revisions[]=$tmp;
}
}
else
{
$tmp=$this->Item->getLastRevision($item);
if($tmp!==false)
{
$revisions[]=$tmp;
}
}
}
}

if(empty($folders)&&empty($revisions))
{
throw new Zend_Exception("Permissions problem");
}
if(empty($folders)&&count($revisions)==1)
{
$revision=$revisions[0];
$bitstreams=$revision->getBitstreams();
if(count($bitstreams)==0)
{
throw new Zend_Exception("Empty item");
}
elseif(count($bitstreams)==1)
{
$bitstream=$bitstreams[0];
$this->view->mimetype = $bitstream->getMimetype();
$this->view->path = $bitstream->getPath();
$this->view->name =$bitstream->getName();
if(!file_exists($this->view->path))
{
throw new Zend_Exception("Unable to find file on the disk");
}
$this->render('onebitstream');
}
else
{
Zend_Loader::loadClass("ZipStream",BASE_PATH.'/library/Zipstream/');
$this->_helper->viewRenderer->setNoRender();
$name=$revision->getItem()->getName();
$name=substr($name, 0,50);
$zip = new ZipStream($name.'.zip');
foreach ($bitstreams as $bitstream)
{
$zip->add_file_from_path('test/'.$bitstream->getName(), $bitstream->getPath());
}
$zip->finish();
}
}
else
{
Zend_Loader::loadClass("ZipStream",BASE_PATH.'/library/Zipstream/');
$this->_helper->viewRenderer->setNoRender();
if(count($folders)==1&&empty($revisions))
{
$name=$folders[0]->getName();
$name=substr($name, 0,50);
}
else
{
$name="Custom";
}
$zip = new ZipStream($name.'.zip');
$zip=$this->createZipRecursive($zip,'',$folders,$revisions);
$zip->finish();
}
}//end index

/** create zip recursive*/
private function createZipRecursive($zip,$path,$folders,$revisions)
{
foreach($revisions as $revision)
{
$bitstreams=$revision->getBitstreams();
foreach ($bitstreams as $bitstream)
{
$zip->add_file_from_path($path.'/'.$bitstream->getName(), $bitstream->getPath());
}
}
foreach($folders as $folder)
{
if(!$this->Folder->policyCheck($folder,$this->userSession->Dao))
{
continue;
}
$items=$folder->getItems();
$subRevisions=array();
foreach($items as $item)
{
if(!$this->Item->policyCheck($item,$this->userSession->Dao))
{
continue;
}
$tmp=$this->Item->getLastRevision($item);
if($tmp!==false)
{
$subRevisions[]=$tmp;
if(isset($folder->recursive)&&$folder->recursive==false)
{
$bitstreams=$subRevisions->getBitstreams();
foreach ($bitstreams as $bitstream)
{
$zip->add_file_from_path($path.'/'.$bitstream->getName(), $bitstream->getPath());
}
}
}
}
if(!isset($folder->recursive)||$folder->recursive)
{
$zip=$this->createZipRecursive($zip,$path.'/'.$folder->getName(),$folder->getFolders(),$subRevisions);
}
}
return $zip;
}
} // end class


4 changes: 2 additions & 2 deletions application/models/pdo/BitstreamModel.php
Expand Up @@ -30,7 +30,6 @@ public function initBitstream($assetstoreDao,$name,$path)
$bitstreamDao = new BitstreamDao;
$bitstreamDao->setName($name);
$bitstreamDao->setPath($path);
$bitstreamDao->fillPropertiesFromPath();

$tmpPath=$assetstoreDao->getPath().'/'.rand(1, 1000);
if(!file_exists($assetstoreDao->getPath()))
Expand All @@ -55,7 +54,8 @@ public function initBitstream($assetstoreDao,$name,$path)
{
throw new Zend_Exception("Unable to move file ".$path.' to '.$fullPath);
}
$bitstreamDao->setPath($fullPath);
$bitstreamDao->setPath($fullPath);
$bitstreamDao->fillPropertiesFromPath();
$bitstreamDao->setAssetstoreId($assetstoreDao->getKey());
return $bitstreamDao;
}
Expand Down
53 changes: 53 additions & 0 deletions application/models/pdo/FolderModel.php
Expand Up @@ -23,6 +23,59 @@ class FolderModel extends AppModelPdo
'parent' => array('type'=>MIDAS_MANY_TO_ONE, 'model'=>'Folder', 'parent_column'=> 'parent_id', 'child_column' => 'folder_id'),
);

/** check if the policy is valid*/
function policyCheck($folderDao,$userDao=null,$policy=0)
{
if(!$folderDao instanceof FolderDao||!is_numeric($policy))
{
throw new Zend_Exception("Error param.");
}
if($userDao==null)
{
$userId= -1;
}
else if(!$userDao instanceof UserDao)
{
throw new Zend_Exception("Should be an user.");
}
else
{
$userId = $userDao->getUserId();
}

$subqueryUser= $this->select()
->setIntegrityCheck(false)
->from(array('p' => 'folderpolicyuser'),
array('folder_id'))
->where('policy >= ?', $policy)
->where('p.folder_id >= ?', $folderDao->getKey())
->where('user_id = ? ',$userId);

$subqueryGroup = $this->select()
->setIntegrityCheck(false)
->from(array('p' => 'folderpolicygroup'),
array('folder_id'))
->where('policy >= ?', $policy)
->where('p.folder_id >= ?', $folderDao->getKey())
->where('( '.$this->_db->quoteInto('group_id = ? ',MIDAS_GROUP_ANONYMOUS_KEY).' OR
group_id IN (' .new Zend_Db_Expr(
$this->select()
->setIntegrityCheck(false)
->from(array('u2g' => 'user2group'),
array('group_id'))
->where('u2g.user_id = ?' , $userId)
.'))' ));

$sql = $this->select()
->union(array($subqueryUser, $subqueryGroup));
$rowset = $this->fetchAll($sql);
if(count($rowset)>0)
{
return true;
}
return false;
}//end policyCheck

/** get the size and the number of item in a folder*/
public function getSizeFiltered($folders,$userDao=null,$policy=0)
{
Expand Down
69 changes: 69 additions & 0 deletions application/models/pdo/ItemModel.php
Expand Up @@ -19,6 +19,59 @@ class ItemModel extends AppModelPdo
'keywords' => array('type'=>MIDAS_MANY_TO_MANY, 'model'=>'ItemKeyword', 'table' => 'item2keyword', 'parent_column'=> 'item_id', 'child_column' => 'keyword_id'),
);

/** check if the policy is valid*/
function policyCheck($itemdao,$userDao=null,$policy=0)
{
if(!$itemdao instanceof ItemDao||!is_numeric($policy))
{
throw new Zend_Exception("Error param.");
}
if($userDao==null)
{
$userId= -1;
}
else if(!$userDao instanceof UserDao)
{
throw new Zend_Exception("Should be an user.");
}
else
{
$userId = $userDao->getUserId();
}

$subqueryUser= $this->select()
->setIntegrityCheck(false)
->from(array('p' => 'itempolicyuser'),
array('item_id'))
->where('policy >= ?', $policy)
->where('p.item_id >= ?', $itemdao->getKey())
->where('user_id = ? ',$userId);

$subqueryGroup = $this->select()
->setIntegrityCheck(false)
->from(array('p' => 'itempolicygroup'),
array('item_id'))
->where('policy >= ?', $policy)
->where('p.item_id >= ?', $itemdao->getKey())
->where('( '.$this->_db->quoteInto('group_id = ? ',MIDAS_GROUP_ANONYMOUS_KEY).' OR
group_id IN (' .new Zend_Db_Expr(
$this->select()
->setIntegrityCheck(false)
->from(array('u2g' => 'user2group'),
array('group_id'))
->where('u2g.user_id = ?' , $userId)
.'))' ));

$sql = $this->select()
->union(array($subqueryUser, $subqueryGroup));
$rowset = $this->fetchAll($sql);
if(count($rowset)>0)
{
return true;
}
return false;
}//end policyCheck

/** Get the last revision
* @return ItemRevisionDao*/
function getLastRevision($itemdao)
Expand All @@ -34,6 +87,22 @@ function getLastRevision($itemdao)
->setIntegrityCheck(false)
));
}

/** Get revision
* @return ItemRevisionDao*/
function getRevision($itemdao,$number)
{
if(!$itemdao instanceof ItemDao||!$itemdao->saved)
{
throw new Zend_Exception("Error param.");
}
return $this->initDao('ItemRevision', $this->fetchRow($this->select()->from('itemrevision')
->where('item_id = ?', $itemdao->getItemId())
->where('revision = ?', $number)
->limit(1)
->setIntegrityCheck(false)
));
}

/** Add a revision to an item
* @return void*/
Expand Down

0 comments on commit 8428aab

Please sign in to comment.