From 4328d832abb61c7c41e46cdee26d98ad6e7fb8a9 Mon Sep 17 00:00:00 2001 From: Romain Neutron Date: Fri, 21 Sep 2012 15:00:10 +0200 Subject: [PATCH] IUpdate databox fields and status --- lib/classes/databox/cgu.class.php | 24 +-- lib/classes/databox/field.class.php | 36 +++-- lib/classes/databox/status.class.php | 139 +++++++----------- .../databox/subdefsStructure.class.php | 12 +- 4 files changed, 94 insertions(+), 117 deletions(-) diff --git a/lib/classes/databox/cgu.class.php b/lib/classes/databox/cgu.class.php index 10a2bea159..5ee5c7752a 100644 --- a/lib/classes/databox/cgu.class.php +++ b/lib/classes/databox/cgu.class.php @@ -9,6 +9,9 @@ * file that was distributed with this source code. */ +use Alchemy\Phrasea\Application; +use Alchemy\Phrasea\Core\Configuration; + /** * * @license http://opensource.org/licenses/gpl-3.0 GPLv3 @@ -22,9 +25,9 @@ class databox_cgu return $this; } - public static function askAgreement() + public static function askAgreement(Application $app) { - $terms = self::getUnvalidated(); + $terms = self::getUnvalidated($app); $out = ''; @@ -45,26 +48,25 @@ class databox_cgu return $out; } - private static function getUnvalidated($home = false) + private static function getUnvalidated(Application $app, $home = false) { $terms = array(); - $appbox = appbox::get_instance(\bootstrap::getCore()); - $session = $appbox->get_session(); + $appbox = $app['phraseanet.appbox']; if ( ! $home) { - $user = User_Adapter::getInstance($session->get_usr_id(), $appbox); + $user = $app['phraseanet.user']; } foreach ($appbox->get_databoxes() as $databox) { try { $cgus = $databox->get_cgus(); - if ( ! isset($cgus[Session_Handler::get_locale()])) + if ( ! isset($cgus[$app['locale']])) throw new Exception('No CGus for this locale'); $name = $databox->get_viewname(); - $update = $cgus[Session_Handler::get_locale()]['updated_on']; - $value = $cgus[Session_Handler::get_locale()]['value']; + $update = $cgus[$app['locale']]['updated_on']; + $value = $cgus[$app['locale']]['value']; $userValidation = true; if ( ! $home) { @@ -84,9 +86,9 @@ class databox_cgu return $terms; } - public static function getHome() + public static function getHome($app) { - $terms = self::getUnvalidated(true); + $terms = self::getUnvalidated($app, true); $out = ''; diff --git a/lib/classes/databox/field.class.php b/lib/classes/databox/field.class.php index 4a19f16976..491b4dda5e 100644 --- a/lib/classes/databox/field.class.php +++ b/lib/classes/databox/field.class.php @@ -9,7 +9,12 @@ * file that was distributed with this source code. */ -use \Alchemy\Phrasea\Vocabulary; +use Alchemy\Phrasea\Application; +use Alchemy\Phrasea\Vocabulary; +use Alchemy\Phrasea\Vocabulary\ControlProvider\ControlProviderInterface; +use Alchemy\Phrasea\Metadata\Tag\Nosource; +use PHPExiftool\Driver\TagInterface; +use PHPExiftool\Driver\TagFactory; /** * @@ -154,8 +159,9 @@ class databox_field implements cache_cacheableInterface * @param $id * @return databox_field */ - protected function __construct(databox &$databox, $id) + protected function __construct(Application $app, databox &$databox, $id) { + $this->app = $app; $this->set_databox($databox); $this->sbas_id = $databox->get_sbas_id(); @@ -191,7 +197,7 @@ class databox_field implements cache_cacheableInterface $this->tbranch = $row['tbranch']; try { - $this->Vocabulary = Vocabulary\Controller::get($row['VocabularyControlType']); + $this->Vocabulary = Vocabulary\Controller::get($this->app, $row['VocabularyControlType']); $this->VocabularyRestriction = ! ! $row['RestrictToVocabularyControl']; } catch (Exception $e) { @@ -242,7 +248,7 @@ class databox_field implements cache_cacheableInterface * @param int $id * @return \databox_field */ - public static function get_instance(databox &$databox, $id) + public static function get_instance(Application $app, databox &$databox, $id) { $cache_key = 'field_' . $id; $instance_id = $databox->get_sbas_id() . '_' . $id; @@ -250,7 +256,7 @@ class databox_field implements cache_cacheableInterface try { self::$_instance[$instance_id] = $databox->get_data_from_cache($cache_key); } catch (Exception $e) { - self::$_instance[$instance_id] = new self($databox, $id); + self::$_instance[$instance_id] = new self($app, $databox, $id); $databox->set_data_to_cache(self::$_instance[$instance_id], $cache_key); } } @@ -287,7 +293,7 @@ class databox_field implements cache_cacheableInterface public function delete() { - caption_field::delete_all_metadatas($this); + caption_field::delete_all_metadatas($this->app, $this); $connbas = $this->get_connection(); $sql = 'DELETE FROM metadatas_structure WHERE id = :id'; @@ -461,16 +467,16 @@ class databox_field implements cache_cacheableInterface $tag = new $classname(); } else { - $tag = \PHPExiftool\Driver\TagFactory::getFromRDFTagname($tagName); + $tag = TagFactory::getFromRDFTagname($tagName); } return $tag; } - public function set_tag(\PHPExiftool\Driver\Tag $tag = null) + public function set_tag(TagInterface $tag = null) { if ($tag === null) { - $tag = new \Alchemy\Phrasea\Metadata\Tag\Nosource(); + $tag = new Nosource(); } $this->tag = $tag; @@ -531,10 +537,10 @@ class databox_field implements cache_cacheableInterface /** * Set a vocabulary * - * @param Vocabulary\ControlProvider\ControlProviderInterface $vocabulary + * @param ControlProviderInterface $vocabulary * @return \databox_field */ - public function setVocabularyControl(Vocabulary\ControlProvider\ControlProviderInterface $vocabulary = null) + public function setVocabularyControl(ControlProviderInterface $vocabulary = null) { $this->Vocabulary = $vocabulary; @@ -787,7 +793,7 @@ class databox_field implements cache_cacheableInterface return $this->on_error; } - public static function create(databox $databox, $name, $multi) + public static function create(Application $app, databox $databox, $name, $multi) { $sorter = 0; @@ -823,7 +829,7 @@ class databox_field implements cache_cacheableInterface $databox->delete_data_from_cache(databox::CACHE_META_STRUCT); - return self::get_instance($databox, $id); + return self::get_instance($app, $databox, $id); } public static function generateName($name) @@ -857,9 +863,7 @@ class databox_field implements cache_cacheableInterface */ public function __wakeup() { - $appbox = \appbox::get_instance(\bootstrap::getCore()); - $databox = $appbox->get_databox($this->sbas_id); - $this->set_databox($databox); + $this->set_databox($this->app['phraseanet.appbox']->get_databox($this->sbas_id)); return; } diff --git a/lib/classes/databox/status.class.php b/lib/classes/databox/status.class.php index a8d4c379b4..cec4f14628 100644 --- a/lib/classes/databox/status.class.php +++ b/lib/classes/databox/status.class.php @@ -9,7 +9,9 @@ * file that was distributed with this source code. */ +use Alchemy\Phrasea\Application; use MediaAlchemyst\Specification\Image as ImageSpecification; +use MediaAlchemyst\Alchemyst; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\HttpFoundation\File\UploadedFile; use Symfony\Component\HttpFoundation\File\Exception\FileException; @@ -57,16 +59,14 @@ class databox_status * @param int $sbas_id * @return status */ - private function __construct($sbas_id) + private function __construct(Application $app, $sbas_id) { - $this->status = array(); $path = $url = false; - $appbox = \appbox::get_instance(\bootstrap::getCore()); - $sbas_params = phrasea::sbas_params(); - $registry = registry::get_instance(); + $sbas_params = phrasea::sbas_params($app); + $registry = $app['phraseanet.registry']; if ( ! isset($sbas_params[$sbas_id])) { return; @@ -75,7 +75,7 @@ class databox_status $path = $this->path = $registry->get('GV_RootPath') . "config/status/" . urlencode($sbas_params[$sbas_id]["host"]) . "-" . urlencode($sbas_params[$sbas_id]["port"]) . "-" . urlencode($sbas_params[$sbas_id]["dbname"]); $url = $this->url = "/custom/status/" . urlencode($sbas_params[$sbas_id]["host"]) . "-" . urlencode($sbas_params[$sbas_id]["port"]) . "-" . urlencode($sbas_params[$sbas_id]["dbname"]); - $databox = $appbox->get_databox((int) $sbas_id); + $databox = $app['phraseanet.appbox']->get_databox((int) $sbas_id); $xmlpref = $databox->get_structure(); $sxe = simplexml_load_string($xmlpref); @@ -111,24 +111,22 @@ class databox_status return $this; } - public static function getStatus($sbas_id) + public static function getStatus(Application $app, $sbas_id) { if ( ! isset(self::$_status[$sbas_id])) - self::$_status[$sbas_id] = new databox_status($sbas_id); + self::$_status[$sbas_id] = new databox_status($app, $sbas_id); return self::$_status[$sbas_id]->status; } - public static function getDisplayStatus() + public static function getDisplayStatus(Application $app) { if (self::$_statuses) { return self::$_statuses; } - $appbox = appbox::get_instance(\bootstrap::getCore()); - $session = $appbox->get_session(); - $user = User_Adapter::getInstance($session->get_usr_id(), $appbox); + $user = $app['phraseanet.user']; $sbas_ids = $user->ACL()->get_granted_sbas(); @@ -147,11 +145,9 @@ class databox_status return self::$_statuses; } - public static function getSearchStatus() + public static function getSearchStatus(Application $app) { - $appbox = appbox::get_instance(\bootstrap::getCore()); - $session = $appbox->get_session(); - $user = User_Adapter::getInstance($session->get_usr_id(), $appbox); + $user = $app['phraseanet.user']; $statuses = array(); @@ -227,41 +223,30 @@ class databox_status return $stats; } - public static function getPath($sbas_id) + public static function getPath(Application $app, $sbas_id) { if ( ! isset(self::$_status[$sbas_id])) { - self::$_status[$sbas_id] = new databox_status($sbas_id); + self::$_status[$sbas_id] = new databox_status($app, $sbas_id); } return self::$_status[$sbas_id]->path; } - public static function getUrl($sbas_id) + public static function getUrl(Application $app, $sbas_id) { if ( ! isset(self::$_status[$sbas_id])) { - self::$_status[$sbas_id] = new databox_status($sbas_id); + self::$_status[$sbas_id] = new databox_status($app, $sbas_id); } return self::$_status[$sbas_id]->url; } - public static function deleteStatus(Filesystem $filesystem, $sbas_id, $bit) + public static function deleteStatus(Application $app, \databox $databox, $bit) { - $core = \bootstrap::getCore(); - $appbox = \appbox::get_instance($core); - - $user = $core->getAuthenticatedUser(); - - if ( ! $user->ACL()->has_right_on_sbas($sbas_id, 'bas_modify_struct')) { - return false; - } - - $status = self::getStatus($sbas_id); + $status = self::getStatus($app, $sbas_id); if (isset($status[$bit])) { - $connbas = connection::getPDOConnection($sbas_id); - - $databox = $appbox->get_databox((int) $sbas_id); + $connbas = connection::getPDOConnection($app, $sbas_id); $doc = $databox->get_dom_structure(); if ($doc) { @@ -284,11 +269,11 @@ class databox_status $databox->saveStructure($doc); if (null !== $status[$bit]['img_off']) { - $filesystem->remove($status[$bit]['path_off']); + $app['filesystem']->remove($status[$bit]['path_off']); } if (null !== $status[$bit]['img_on']) { - $filesystem->remove($status[$bit]['path_on']); + $app['filesystem']->remove($status[$bit]['path_on']); } unset(self::$_status[$sbas_id]->status[$bit]); @@ -300,21 +285,11 @@ class databox_status return false; } - public static function updateStatus($sbas_id, $bit, $properties) + public static function updateStatus(Application $app, $sbas_id, $bit, $properties) { - $core = \bootstrap::getCore(); + self::getStatus($app, $sbas_id); - $user = $core->getAuthenticatedUser(); - - if ( ! $user->ACL()->has_right_on_sbas($sbas_id, 'bas_modify_struct')) { - return false; - } - - self::getStatus($sbas_id); - - $appbox = \appbox::get_instance($core); - - $databox = $appbox->get_databox((int) $sbas_id); + $databox = $app['phraseanet.appbox']->get_databox((int) $sbas_id); $doc = $databox->get_dom_structure($sbas_id); if ($doc) { @@ -383,17 +358,9 @@ class databox_status return false; } - public static function deleteIcon(Filesystem $filesystem, $sbas_id, $bit, $switch) + public static function deleteIcon(Application $app, $sbas_id, $bit, $switch) { - $core = \bootstrap::getCore(); - - $user = $core->getAuthenticatedUser(); - - if ( ! $user->ACL()->has_right_on_sbas($sbas_id, 'bas_modify_struct')) { - return false; - } - - $status = self::getStatus($sbas_id); + $status = self::getStatus($app, $sbas_id); $switch = in_array($switch, array('on', 'off')) ? $switch : false; @@ -403,7 +370,7 @@ class databox_status if ($status[$bit]['img_' . $switch]) { if (isset($status[$bit]['path_' . $switch])) { - $filesystem->remove($status[$bit]['path_' . $switch]); + $app['filesystem']->remove($status[$bit]['path_' . $switch]); } $status[$bit]['img_' . $switch] = false; @@ -413,15 +380,9 @@ class databox_status return true; } - public static function updateIcon(Filesystem $filesystem, $sbas_id, $bit, $switch, UploadedFile $file) + public static function updateIcon(Application $app, $sbas_id, $bit, $switch, UploadedFile $file) { - $core = \bootstrap::getCore(); - $user = $core->getAuthenticatedUser(); - $registry = $core->getRegistry(); - - if ( ! $user->ACL()->has_right_on_sbas($sbas_id, 'bas_modify_struct')) { - throw new Exception_Forbidden(); - } + $registry = $app['phraseanet.registry']; $switch = in_array($switch, array('on', 'off')) ? $switch : false; @@ -429,8 +390,8 @@ class databox_status throw new Exception_InvalidArgument(); } - $url = self::getUrl($sbas_id); - $path = self::getPath($sbas_id); + $url = self::getUrl($app, $sbas_id); + $path = self::getPath($app, $sbas_id); if ($file->getSize() >= 65535) { throw new Exception_Upload_FileTooBig(); @@ -440,7 +401,7 @@ class databox_status throw new Exception_Upload_Error(); } - self::deleteIcon($filesystem, $sbas_id, $bit, $switch); + self::deleteIcon($app, $sbas_id, $bit, $switch); $name = "-stat_" . $bit . "_" . ($switch == 'on' ? '1' : '0') . ".gif"; @@ -453,7 +414,7 @@ class databox_status $custom_path = $registry->get('GV_RootPath') . 'www/custom/status/'; - $filesystem->mkdir($custom_path, 0750); + $app['filesystem']->mkdir($custom_path, 0750); //resize status icon 16x16px $imageSpec = new ImageSpecification(); @@ -464,7 +425,7 @@ class databox_status $destPath = sprintf("%s%s", $custom_path, basename($path . $name)); try { - $core['media-alchemyst'] + $app['media-alchemyst'] ->open($filePath) ->turninto($destPath, $imageSpec) ->close(); @@ -478,17 +439,17 @@ class databox_status return true; } - public static function operation_and($stat1, $stat2) + public static function operation_and(Application $app, $stat1, $stat2) { - $conn = connection::getPDOConnection(); + $conn = connection::getPDOConnection($app); $status = '0'; if (substr($stat1, 0, 2) === '0x') { - $stat1 = self::hex2bin(substr($stat1, 2)); + $stat1 = self::hex2bin($app, substr($stat1, 2)); } if (substr($stat2, 0, 2) === '0x') { - $stat2 = self::hex2bin(substr($stat2, 2)); + $stat2 = self::hex2bin($app, substr($stat2, 2)); } $sql = 'select bin(0b' . trim($stat1) . ' & 0b' . trim($stat2) . ') as result'; @@ -505,17 +466,17 @@ class databox_status return $status; } - public static function operation_and_not($stat1, $stat2) + public static function operation_and_not(Application $app, $stat1, $stat2) { - $conn = connection::getPDOConnection(); + $conn = connection::getPDOConnection($app); $status = '0'; if (substr($stat1, 0, 2) === '0x') { - $stat1 = self::hex2bin(substr($stat1, 2)); + $stat1 = self::hex2bin($app, substr($stat1, 2)); } if (substr($stat2, 0, 2) === '0x') { - $stat2 = self::hex2bin(substr($stat2, 2)); + $stat2 = self::hex2bin($app, substr($stat2, 2)); } $sql = 'select bin(0b' . trim($stat1) . ' & ~0b' . trim($stat2) . ') as result'; @@ -532,17 +493,17 @@ class databox_status return $status; } - public static function operation_or($stat1, $stat2) + public static function operation_or(Application $app, $stat1, $stat2) { - $conn = connection::getPDOConnection(); + $conn = connection::getPDOConnection($app); $status = '0'; if (substr($stat1, 0, 2) === '0x') { - $stat1 = self::hex2bin(substr($stat1, 2)); + $stat1 = self::hex2bin($app, substr($stat1, 2)); } if (substr($stat2, 0, 2) === '0x') { - $stat2 = self::hex2bin(substr($stat2, 2)); + $stat2 = self::hex2bin($app, substr($stat2, 2)); } $sql = 'select bin(0b' . trim($stat1) . ' | 0b' . trim($stat2) . ') as result'; @@ -559,7 +520,7 @@ class databox_status return $status; } - public static function dec2bin($status) + public static function dec2bin(Application $app, $status) { $status = (string) $status; @@ -567,7 +528,7 @@ class databox_status throw new \Exception('Non-decimal value'); } - $conn = connection::getPDOConnection(); + $conn = connection::getPDOConnection($app); $sql = 'select bin(' . $status . ') as result'; @@ -585,7 +546,7 @@ class databox_status return $status; } - public static function hex2bin($status) + public static function hex2bin(Application $app, $status) { $status = (string) $status; if (substr($status, 0, 2) === '0x') { @@ -596,7 +557,7 @@ class databox_status throw new \Exception('Non-hexadecimal value'); } - $conn = connection::getPDOConnection(); + $conn = connection::getPDOConnection($app); $sql = 'select BIN( CAST( 0x' . trim($status) . ' AS UNSIGNED ) ) as result'; diff --git a/lib/classes/databox/subdefsStructure.class.php b/lib/classes/databox/subdefsStructure.class.php index a47b8c6d8c..8b9a13d137 100644 --- a/lib/classes/databox/subdefsStructure.class.php +++ b/lib/classes/databox/subdefsStructure.class.php @@ -14,7 +14,7 @@ * @license http://opensource.org/licenses/gpl-3.0 GPLv3 * @link www.phraseanet.com */ -class databox_subdefsStructure implements IteratorAggregate +class databox_subdefsStructure implements IteratorAggregate, Countable { /** * @@ -31,6 +31,16 @@ class databox_subdefsStructure implements IteratorAggregate return new ArrayIterator($this->AvSubdefs); } + public function count() + { + $n = 0; + foreach($this->AvSubdefs as $subdefs) { + $n += count($subdefs); + } + + return $n; + } + /** * * @param databox $databox