*
* @param mixed $limit_val result limit value
* @param int $offset_val result offset value
+ * @param array $sort order by and order direction in form [[by1, direction1]]
* @param array $criteria SQL criteria to get job list
* @param array $jobs jobs used to get job properties per client
* @param string $mode result mode
* @return array clients or empty list if no client found
*/
- public function getClients($limit_val = 0, $offset_val = 0, $criteria = [], $jobs = [], $mode = self::CLIENT_RESULT_MODE_NORMAL) {
+ public function getClients($limit_val = 0, $offset_val = 0, $sort = [['Name', 'ASC']], $criteria = [], $jobs = [], $mode = self::CLIENT_RESULT_MODE_NORMAL) {
+ $extra_cols = ['os', 'version'];
+ $order = '';
+ if (count($sort) == 1 && !in_array($sort[0][0], $extra_cols)) {
+ $order = Database::getOrder($sort);
+ }
$limit = '';
if(is_int($limit_val) && $limit_val > 0) {
$limit = ' LIMIT ' . $limit_val;
' . $jwhere['where'] . '
GROUP BY ClientId
) AS J ON (Client.ClientId = J.ClientId)
- ' . $where['where'] . $offset . $limit;
+ ' . $where['where'] . $order . $offset . $limit;
$params = array_merge($jwhere['params'], $where['params']);
$statement = Database::runQuery($sql, $params);
- $result = $statement->fetchAll(\PDO::FETCH_OBJ);
+ $result = $statement->fetchAll(\PDO::FETCH_ASSOC);
$this->setSpecialFields($result);
+ if (count($sort) == 1 && in_array($sort[0][0], $extra_cols)) {
+ $misc = $this->getModule('misc');
+ // Order by os or version
+ $misc::sortResultsByField(
+ $result,
+ $sort[0][0],
+ $sort[0][1],
+ 'name',
+ $misc::ORDER_DIRECTION_ASC
+ );
+ }
if ($mode == self::CLIENT_RESULT_MODE_OVERVIEW) {
$sql = 'SELECT COUNT(1) AS count FROM Client ' . $where['where'];
$statement = Database::runQuery($sql, $where['params']);
private function setSpecialFields(&$result) {
for ($i = 0; $i < count($result); $i++) {
// Add operating system info and client version
- $result[$i]->os = '';
- $result[$i]->version = '';
- if (preg_match(self::CLIENT_UNAME_PATTERN, $result[$i]->uname, $match) === 1) {
- $result[$i]->os = $match['os'];
- $result[$i]->version = $match['version'];
+ $result[$i]['os'] = '';
+ $result[$i]['version'] = '';
+ if (preg_match(self::CLIENT_UNAME_PATTERN, $result[$i]['uname'], $match) === 1) {
+ $result[$i]['os'] = $match['os'];
+ $result[$i]['version'] = $match['version'];
}
}
}
public function getClientByName($name) {
$result = ClientRecord::finder()->findByName($name);
if (is_object($result)) {
- $result = [$result];
+ $result = [(array)$result];
$this->setSpecialFields($result);
- $result = array_shift($result);
+ $result = (object)array_shift($result);
}
return $result;
}
public function getClientById($id) {
$result = ClientRecord::finder()->findByclientid($id);
if (is_object($result)) {
- $result = [$result];
+ $result = [(array)$result];
$this->setSpecialFields($result);
- $result = array_shift($result);
+ $result = (object)array_shift($result);
}
return $result;
}
$os = $this->Request->contains('os') && $misc->isValidNameExt($this->Request['os']) ? $this->Request['os'] : '';
$version = $this->Request->contains('version') && $misc->isValidColumn($this->Request['version']) ? $this->Request['version'] : '';
$mode = $this->Request->contains('overview') && $misc->isValidBooleanTrue($this->Request['overview']) ? ClientManager::CLIENT_RESULT_MODE_OVERVIEW : ClientManager::CLIENT_RESULT_MODE_NORMAL;
+ $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']: null;
$result = $this->getModule('bconsole')->bconsoleCommand($this->director, array('.client'));
if ($result->exitcode === 0) {
$params = [];
$jobs = $result->output;
}
+ $sort = [];
+ if (!is_null($order_by)) {
+ if (is_null($order_direction)) {
+ $order_direction = 'ASC';
+ }
+ $cr = new \ReflectionClass('Baculum\API\Modules\ClientRecord');
+ $sort_cols = $cr->getProperties();
+ $order_by_lc = strtolower($order_by);
+ $columns = [];
+ foreach ($sort_cols as $cols) {
+ $columns[] = $cols->getName();
+ }
+ if (!in_array($order_by_lc, $columns)) {
+ $this->output = ClientError::MSG_ERROR_INVALID_PROPERTY;
+ $this->error = ClientError::ERROR_INVALID_PROPERTY;
+ return;
+ }
+ $sort = [[$order_by_lc, $order_direction]];
+ }
+
$clients = $this->getModule('client')->getClients(
$limit,
$offset,
+ $sort,
$params,
$jobs,
$mode