Update sets

This commit is contained in:
Romain Neutron
2012-09-21 15:06:21 +02:00
parent 95a447f496
commit 46bd575bff
4 changed files with 122 additions and 131 deletions

View File

@@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/
use User_Adapter;
use Alchemy\Phrasea\Application;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
@@ -21,6 +21,7 @@ use Symfony\Component\HttpFoundation\ResponseHeaderBag;
*/
class set_export extends set_abstract
{
protected $app;
protected $storage = array();
protected $total_download;
protected $total_order;
@@ -34,45 +35,42 @@ class set_export extends set_abstract
/**
*
* @param string $lst
* @param int $sstid
* @param Application $app
* @param string $lst
* @param int $sstid
* @return set_export
*/
public function __construct($lst, $sstid, $storyWZid = null)
public function __construct(Application $app, $lst, $sstid, $storyWZid = null)
{
$Core = bootstrap::getCore();
$this->app = $app;
$registry = $this->app['phraseanet.registry'];
$appbox = appbox::get_instance($Core);
$session = $appbox->get_session();
$registry = $appbox->get_registry();
$user = $Core->getAuthenticatedUser();
$user = $app['phraseanet.user'];
$download_list = array();
$remain_hd = array();
if ($storyWZid) {
$repository = $Core->getEntityManager()->getRepository('\\Entities\\StoryWZ');
$repository = $app['EM']->getRepository('\\Entities\\StoryWZ');
$storyWZ = $repository->findByUserAndId($user, $storyWZid);
$storyWZ = $repository->findByUserAndId($this->app, $user, $storyWZid);
$lst = $storyWZ->getRecord()->get_serialize_key();
$lst = $storyWZ->getRecord($this->app)->get_serialize_key();
}
if ($sstid != "") {
$em = $Core->getEntityManager();
$repository = $em->getRepository('\Entities\Basket');
$repository = $app['EM']->getRepository('\Entities\Basket');
/* @var $repository \Repositories\BasketRepository */
$Basket = $repository->findUserBasket($sstid, $user, false);
$Basket = $repository->findUserBasket($this->app, $sstid, $user, false);
foreach ($Basket->getElements() as $basket_element) {
/* @var $basket_element \Entities\BasketElement */
$base_id = $basket_element->getRecord()->get_base_id();
$record_id = $basket_element->getRecord()->get_record_id();
$base_id = $basket_element->getRecord($this->app)->get_base_id();
$record_id = $basket_element->getRecord($this->app)->get_record_id();
if ( ! isset($remain_hd[$base_id])) {
if (!isset($remain_hd[$base_id])) {
if ($user->ACL()->is_restricted_download($base_id)) {
$remain_hd[$base_id] = $user->ACL()->remaining_download($base_id);
} else {
@@ -82,7 +80,8 @@ class set_export extends set_abstract
$current_element = $download_list[] =
new record_exportElement(
$basket_element->getRecord()->get_sbas_id(),
$app,
$basket_element->getRecord($this->app)->get_sbas_id(),
$record_id,
$Basket->getName() . '/',
$remain_hd[$base_id]
@@ -99,7 +98,7 @@ class set_export extends set_abstract
continue;
try {
$record = new record_adapter($basrec[0], $basrec[1]);
$record = new record_adapter($this->app, $basrec[0], $basrec[1]);
} catch (Exception_Record_AdapterNotFound $e) {
continue;
}
@@ -109,7 +108,7 @@ class set_export extends set_abstract
$base_id = $child_basrec->get_base_id();
$record_id = $child_basrec->get_record_id();
if ( ! isset($remain_hd[$base_id])) {
if (!isset($remain_hd[$base_id])) {
if ($user->ACL()->is_restricted_download($base_id)) {
$remain_hd[$base_id] =
$user->ACL()->remaining_download($base_id);
@@ -120,6 +119,7 @@ class set_export extends set_abstract
$current_element = $download_list[] =
new record_exportElement(
$app,
$child_basrec->get_sbas_id(),
$record_id,
$record->get_title(null, null, true) . '_' . $n . '/',
@@ -132,7 +132,7 @@ class set_export extends set_abstract
$base_id = $record->get_base_id();
$record_id = $record->get_record_id();
if ( ! isset($remain_hd[$base_id])) {
if (!isset($remain_hd[$base_id])) {
if ($user->ACL()->is_restricted_download($base_id)) {
$remain_hd[$base_id] =
$user->ACL()->remaining_download($base_id);
@@ -144,6 +144,7 @@ class set_export extends set_abstract
$current_element =
$download_list[$basrec[0] . '_' . $basrec[1]] =
new record_exportElement(
$app,
$record->get_sbas_id(),
$record_id,
'',
@@ -152,7 +153,7 @@ class set_export extends set_abstract
$remain_hd[$base_id] = $current_element->get_remain_hd();
}
$n ++;
$n++;
}
}
@@ -173,7 +174,7 @@ class set_export extends set_abstract
}
foreach ($download_element->get_downloadable() as $name => $properties) {
if ( ! isset($display_download[$name])) {
if (!isset($display_download[$name])) {
$display_download[$name] = array(
'size' => 0,
'total' => 0,
@@ -182,20 +183,20 @@ class set_export extends set_abstract
);
}
$display_download[$name]['total'] ++;
$display_download[$name]['total']++;
if ($properties !== false) {
$display_download[$name]['available'] ++;
$display_download[$name]['available']++;
$display_download[$name]['label'] = $properties['label'];
$display_download[$name]['class'] = $properties['class'];
$this->total_download ++;
$this->total_download++;
$display_download[$name]['size'] += $download_element->get_size($name);
} else {
$display_download[$name]['refused'][] = $download_element->get_thumbnail();
}
}
foreach ($download_element->get_orderable() as $name => $properties) {
if ( ! isset($display_orderable[$name])) {
if (!isset($display_orderable[$name])) {
$display_orderable[$name] = array(
'total' => 0,
'available' => 0,
@@ -203,11 +204,11 @@ class set_export extends set_abstract
);
}
$display_orderable[$name]['total'] ++;
$display_orderable[$name]['total']++;
if ($properties !== false) {
$display_orderable[$name]['available'] ++;
$this->total_order ++;
$display_orderable[$name]['available']++;
$this->total_order++;
} else {
$display_orderable[$name]['refused'][] = $download_element->get_thumbnail();
}
@@ -261,7 +262,7 @@ class set_export extends set_abstract
)
)
GROUP BY usr_id ";
$params = array(':usr_id' => $session->get_usr_id());
$params = array(':usr_id' => $app['phraseanet.user']->get_id());
}
$datas[] = array(
@@ -279,7 +280,7 @@ class set_export extends set_abstract
'sendermail' => $user->get_email()
);
$stmt = $appbox->get_connection()->prepare($sql);
$stmt = $app['phraseanet.appbox']->get_connection()->prepare($sql);
$stmt->execute($params);
$rs = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt->closeCursor();
@@ -390,15 +391,13 @@ class set_export extends set_abstract
*/
public function prepare_export(User_Adapter $user, Filesystem $filesystem, Array $subdefs, $rename_title, $includeBusinessFields)
{
if ( ! is_array($subdefs)) {
if (!is_array($subdefs)) {
throw new Exception('No subdefs given');
}
$includeBusinessFields = ! ! $includeBusinessFields;
$includeBusinessFields = !!$includeBusinessFields;
$appbox = appbox::get_instance(\bootstrap::getCore());
$session = $appbox->get_session();
$registry = $appbox->get_registry();
$registry = $this->app['phraseanet.registry'];
$unicode = new unicode();
@@ -458,17 +457,17 @@ class set_export extends set_abstract
$sd = $download_element->get_subdefs();
foreach ($download_element->get_downloadable() as $name => $properties) {
if ($properties === false || ! in_array($name, $subdefs)) {
if ($properties === false || !in_array($name, $subdefs)) {
continue;
}
if ( ! in_array($name, array('caption', 'caption-yaml')) && ! isset($sd[$name])) {
if (!in_array($name, array('caption', 'caption-yaml')) && !isset($sd[$name])) {
continue;
}
set_time_limit(100);
$subdef_export = $subdef_alive = false;
$n_files ++;
$n_files++;
$tmp_pathfile = array('path' => null, 'file' => null);
@@ -488,7 +487,7 @@ class set_export extends set_abstract
break;
case 'document':
$subdef_export = true;
$path = recordutils_image::stamp($sd[$name]);
$path = recordutils_image::stamp($app, $sd[$name]);
$tmp_pathfile = array(
'path' => $sd[$name]->get_path()
, 'file' => $sd[$name]->get_file()
@@ -509,10 +508,10 @@ class set_export extends set_abstract
'path' => $sd[$name]->get_path()
, 'file' => $sd[$name]->get_file()
);
if ( ! $user->ACL()->has_right_on_base($download_element->get_base_id(), "nowatermark")
&& ! $user->ACL()->has_preview_grant($download_element)
if (!$user->ACL()->has_right_on_base($download_element->get_base_id(), "nowatermark")
&& !$user->ACL()->has_preview_grant($download_element)
&& $sd[$name]->get_type() == media_subdef::TYPE_IMAGE) {
$path = recordutils_image::watermark($sd[$name]);
$path = recordutils_image::watermark($app, $sd[$name]);
if (file_exists($path)) {
$tmp_pathfile = array(
'path' => dirname($path)
@@ -593,7 +592,7 @@ class set_export extends set_abstract
$n = 1;
while (in_array(mb_strtolower($name), $file_names)) {
$n ++;
$n++;
$suffix = "-" . $n; // pour diese si besoin
$max_length = 31 - $sizeMaxExt - $sizeMaxAjout - mb_strlen($suffix);
$name = mb_strtolower($files[$id]["export_name"]);
@@ -617,21 +616,20 @@ class set_export extends set_abstract
'4', '5', '6', '7', '8', '9', '-', '_', '.', '#');
while (isset($name[$i])) {
if ( ! in_array(mb_strtolower($name[$i]), $good_keys))
if (!in_array(mb_strtolower($name[$i]), $good_keys))
$tmp_name .= '_';
else
$tmp_name .= $name[$i];
$tmp_name = str_replace('__', '_', $tmp_name);
$i ++;
$i++;
}
$files[$id]["export_name"] = $tmp_name;
if (in_array('caption', $subdefs)) {
$caption_dir = $registry->get('GV_RootPath') . 'tmp/desc_tmp/'
. time() . $session->get_usr_id()
. $session->get_ses_id() . '/';
. time() . $this->app['phraseanet.user']->get_id() . '/';
$filesystem->mkdir($caption_dir, 0750);
@@ -652,8 +650,7 @@ class set_export extends set_abstract
}
if (in_array('caption-yaml', $subdefs)) {
$caption_dir = $registry->get('GV_RootPath') . 'tmp/desc_tmp/'
. time() . $session->get_usr_id()
. $session->get_ses_id() . '/';
. time() . $this->app['phraseanet.user']->get_id() . '/';
$filesystem->mkdir($caption_dir, 0750);
@@ -706,7 +703,7 @@ class set_export extends set_abstract
$list['complete'] = false;
random::updateToken($token, serialize($list));
random::updateToken($this->app, $token, serialize($list));
$unicode = new \unicode();
@@ -727,7 +724,7 @@ class set_export extends set_abstract
$zip->addFile($path, $name);
if ($o == 'caption') {
if ( ! in_array(dirname($path), $toRemove)) {
if (!in_array(dirname($path), $toRemove)) {
$toRemove[] = dirname($path);
}
$toRemove[] = $path;
@@ -742,7 +739,7 @@ class set_export extends set_abstract
$list['complete'] = true;
$unicode = null;
random::updateToken($token, serialize($list));
random::updateToken($this->app, $token, serialize($list));
$filesystem->remove($toRemove);
$filesystem->chmod($zipFile, 0760);
@@ -758,11 +755,8 @@ class set_export extends set_abstract
* @param string $disposition
* @return \Symfony\Component\HttpFoundation\Response
*/
public static function stream_file(
$file, $exportname, $mime, $disposition = 'inline')
public static function stream_file(\registry $registry, $file, $exportname, $mime, $disposition = 'inline')
{
$registry = registry::get_instance();
$response = new Symfony\Component\HttpFoundation\Response();
$disposition = $disposition === 'attachment' ? ResponseHeaderBag::DISPOSITION_ATTACHMENT : ResponseHeaderBag::DISPOSITION_INLINE;
@@ -801,7 +795,7 @@ class set_export extends set_abstract
* @todo : merge this shitty fix with Response object.
*
*/
if ( ! headers_sent()) {
if (!headers_sent()) {
header("Pragma: public");
}
@@ -856,14 +850,11 @@ class set_export extends set_abstract
*/
public static function log_download(Array $list, $type, $anonymous = false, $comment = '')
{
//download
$appbox = appbox::get_instance(\bootstrap::getCore());
$session = $appbox->get_session();
$user = false;
if ($anonymous) {
} else {
$user = User_Adapter::getInstance($session->get_usr_id(), appbox::get_instance(\bootstrap::getCore()));
$user = $this->app['phraseanet.user'];
}
$tmplog = array();
@@ -878,10 +869,11 @@ class set_export extends set_abstract
foreach ($files as $record) {
foreach ($record["subdefs"] as $o => $obj) {
$sbas_id = phrasea::sbasFromBas($record['base_id']);
$record_object = new record_adapter($sbas_id, $record['record_id']);
$sbas_id = phrasea::sbasFromBas($this->app, $record['base_id']);
$session->get_logger($record_object->get_databox())
$record_object = new record_adapter($this->app, $sbas_id, $record['record_id']);
$this->app['phraseanet.logger']($record_object->get_databox())
->log($record_object, $event_name, $o, $comment);
if ($o != "caption") {
@@ -890,7 +882,7 @@ class set_export extends set_abstract
$log["poids"] = $obj["size"];
$log["shortXml"] = $record_object->get_caption()->serialize(caption_record::SERIALIZE_XML);
$tmplog[$record_object->get_base_id()][] = $log;
if ( ! $anonymous && $o == 'document')
if (!$anonymous && $o == 'document')
$user->ACL()->remove_remaining($record_object->get_base_id());
}
@@ -906,12 +898,12 @@ class set_export extends set_abstract
$list_base = array_unique(array_keys($tmplog));
if ( ! $anonymous) {
if (!$anonymous) {
$sql = "UPDATE basusr
SET remain_dwnld = :remain_dl
WHERE base_id = :base_id AND usr_id = :usr_id";
$stmt = $appbox->get_connection()->prepare($sql);
$stmt = $this->app['phraseanet.appbox']->get_connection()->prepare($sql);
foreach ($list_base as $base_id) {
if ($user->ACL()->is_restricted_download($base_id)) {