namespace Baculum\API\Modules;
use PDO;
+use Baculum\Common\Modules\Errors\VolumeError;
/**
* Volume manager module.
];
}
- public function getVolumes($criteria = array(), $props = [], $limit_val = 0, $offset_val = 0) {
+ public function getVolumes($criteria = array(), $props = [], $limit_val = 0, $offset_val = 0, $order_by = null, $order_direction = 'DESC') {
$order_pool_id = 'PoolId';
$order_volume = 'VolumeName';
$db_params = $this->getModule('api_config')->getConfig('db');
$order_pool_id = strtolower($order_pool_id);
$order_volume = strtolower($order_volume);
}
+
+ // default sorting
$order = " ORDER BY $order_pool_id ASC, $order_volume ASC ";
+
+ // custom sorting
+ if (is_string($order_by)) {
+ $order = " ORDER BY $order_by $order_direction ";
+ }
+
if (key_exists('voltype', $props)) {
$voltypes = [];
switch ($props['voltype']) {
LEFT JOIN Pool AS pool3 ON Media.RecyclePoolId = pool3.PoolId
LEFT JOIN Storage USING (StorageId)
' . $where['where'] . $order . $limit . $offset;
- $volumes = VolumeRecord::finder()->findAllBySql($sql, $where['params']);
+ $statement = Database::runQuery($sql, $where['params']);
+ $volumes = $statement->fetchAll(\PDO::FETCH_OBJ);
$this->setExtraVariables($volumes);
return $volumes;
}
$voltype = $this->Request->contains('voltype') && $misc->isValidVolType($this->Request['voltype']) ? $this->Request['voltype'] : null;
$pool = $this->Request->contains('pool') && $misc->isValidName($this->Request['pool']) ? $this->Request['pool'] : null;
$storage = $this->Request->contains('storage') && $misc->isValidName($this->Request['storage']) ? $this->Request['storage'] : null;
+ $order_by = $this->Request->contains('order_by') && $misc->isValidColumn($this->Request['order_by']) ? $this->Request['order_by']: null;
+ $order_direction = $this->Request->contains('order_direction') && $misc->isValidOrderDirection($this->Request['order_direction']) ? $this->Request['order_direction'] : 'DESC';
$params = $props = [];
$props['storage'] = $storage;
}
+ if (is_string($order_by)) {
+ $jr = new \ReflectionClass('Baculum\API\Modules\VolumeRecord');
+ $sort_cols = $jr->getProperties();
+ $order_by_lc = strtolower($order_by);
+ $cols_excl = ['whenexpire'];
+ $columns = [];
+ foreach ($sort_cols as $cols) {
+ $name = $cols->getName();
+ // skip columns not existing in the catalog
+ if (in_array($name, $cols_excl)) {
+ continue;
+ }
+ $columns[] = $name;
+ }
+ if (!in_array($order_by_lc, $columns)) {
+ $this->output = VolumeError::MSG_ERROR_INVALID_PROPERTY;
+ $this->error = VolumeError::ERROR_INVALID_PROPERTY;
+ return;
+ }
+ }
+
$result = $this->getModule('volume')->getVolumes(
$params,
$props,
$limit,
- $offset
+ $offset,
+ $order_by,
+ $order_direction
);
$this->output = $result;
$this->error = VolumeError::ERROR_NO_ERRORS;
"type": "string",
"enum": ["Append", "Archive", "Disabled", "Full", "Used", "Cleaning", "Purged", "Recycle", "Read-Only", "Error"]
}
+ },
+ {
+ "name": "order_by",
+ "in": "query",
+ "required": false,
+ "description": "Sort by selected volume property (default poolid ASC, volumename ASC). There can be any volume property (mediaid, volumename, slot ...etc.) except whenexpire.",
+ "schema": {
+ "type": "string"
+ }
+ },
+ {
+ "name": "order_direction",
+ "in": "query",
+ "required": false,
+ "description": "Order direction. It can be 'asc' (ascending order) or 'desc' (descending order - default)",
+ "schema": {
+ "type": "string",
+ "enum": ["asc", "desc"]
+ }
}
]
}