*/
class ClientManager extends APIModule {
- public function getClients($limit_val = 0, $offset_val = 0) {
- $criteria = new TActiveRecordCriteria;
- if(is_numeric($limit_val) && $limit_val > 0) {
- $criteria->Limit = $limit_val;
+ /**
+ * SQL query builder.
+ *
+ * @var TDbCommandBuilder command builder
+ */
+ private static $query_builder;
+
+ /**
+ * Get the SQL query builder instance.
+ * Note: Singleton
+ *
+ * @return TDbCommandBuilder command builder
+ */
+ private function getQueryBuilder() {
+ if (is_null(self::$query_builder)) {
+ $record = ClientRecord::finder();
+ $connection = $record->getDbConnection();
+ $tableInfo = $record->getRecordGateway()->getRecordTableInfo($record);
+ self::$query_builder = $tableInfo->createCommandBuilder($connection);
+ }
+ return self::$query_builder;
+ }
+
+ /**
+ * Get client list.
+ *
+ * @param mixed $limit_val result limit value
+ * @param int $offset_val result offset value
+ * @param array $criteria SQL criteria to get job list
+ * @return array clients or empty list if no client found
+ */
+ public function getClients($limit_val = 0, $offset_val = 0, $criteria = []) {
+ $limit = '';
+ if(is_int($limit_val) && $limit_val > 0) {
+ $limit = ' LIMIT ' . $limit_val;
}
+ $offset = '';
if (is_int($offset_val) && $offset_val > 0) {
- $criteria->Offset = $offset_val;
+ $offset = ' OFFSET ' . $offset_val;
+ }
+ $where = Database::getWhere($criteria);
+
+ $sql = 'SELECT *
+FROM Client
+' . $where['where'] . $offset . $limit;
+
+ $builder = $this->getQueryBuilder();
+ if (count($where['params']) == 0) {
+ /**
+ * Please note that in case no params the TDbCommandBuilder::applyCriterias()
+ * returns empty the PDO statement handler. From this reason here
+ * the query is called directly by PDO.
+ */
+ $connection = JobRecord::finder()->getDbConnection();
+ $connection->setActive(true);
+ $pdo = $connection->getPdoInstance();
+ $statement = $pdo->query($sql);
+
+ } else {
+ $command = $builder->applyCriterias($sql, $where['params']);
+ $statement = $command->getPdoStatement();
+ $command->query();
}
- return ClientRecord::finder()->findAll($criteria);
+ return $statement->fetchAll(\PDO::FETCH_OBJ);
}
public function getClientByName($name) {
} elseif (in_array($value[$i]['operator'], ['IS', 'IS NOT'])) {
$cond[] = "{$key} {$value[$i]['operator']} {$value[$i]['vals']}";
$value[$i]['operator'] = '';
+ } elseif ($value[$i]['operator'] == 'LIKE') {
+ $cond[] = "{$key} {$value[$i]['operator']} :{$kval}{$i}";
+ $vals[":{$kval}{$i}"] = $value[$i]['vals'];
+ $value[$i]['operator'] = '';
} else {
$cond[] = "$key = :{$kval}{$i}";
$vals[":{$kval}{$i}"] = $value[$i]['vals'];
$misc = $this->getModule('misc');
$limit = $this->Request->contains('limit') ? intval($this->Request['limit']) : 0;
$offset = $this->Request->contains('offset') && $misc->isValidInteger($this->Request['offset']) ? (int)$this->Request['offset'] : 0;
+ $plugin = $this->Request->contains('plugin') && $misc->isValidAlphaNumeric($this->Request['plugin']) ? $this->Request['plugin'] : '';
$result = $this->getModule('bconsole')->bconsoleCommand($this->director, array('.client'));
if ($result->exitcode === 0) {
- $clients = $this->getModule('client')->getClients($limit, $offset);
+ $params = [];
+ if (!empty($plugin)) {
+ $params['Plugins'] = [];
+ $params['Plugins'][] = [
+ 'operator' => 'LIKE',
+ 'vals' => "%{$plugin}%"
+ ];
+ }
+ $clients = $this->getModule('client')->getClients(
+ $limit,
+ $offset,
+ $params
+ );
array_shift($result->output);
$clients_output = array();
foreach($clients as $client) {