config = new AauthConfig(); $this->DBGroup = $this->config->dbProfile; $this->table = $this->config->dbTableUserVariables; if ($db instanceof ConnectionInterface) { $this->db = & $db; } else { $this->db = Database::connect($this->DBGroup); } } /** * Find user varialbe * * Find User Variable by userId, dataKey & optional system * * @param integer $userId User id * @param string $dataKey Key of variable * @param boolean $system Whether system variable * * @return string|boolean */ public function find(int $userId, string $dataKey, bool $system = null) { $builder = $this->builder(); $builder->select('data_value'); $builder->where('user_id', $userId); $builder->where('data_key', $dataKey); $builder->where('system', ($system ? 1 : 0)); if ($row = $builder->get()->getFirstRow('array')) { return $row['data_value']; } return false; } /** * Find all user variables * * @param integer $userId User id * @param boolean $system Whether system variable * * @return object */ public function findAll(int $userId, bool $system = null) { $builder = $this->builder(); $builder->where('user_id', $userId); $builder->where('system', ($system ? 1 : 0)); return $builder->get()->getResult(); } /** * Update/Insert User Variable * * @param integer $userId User id * @param string $dataKey Key of variable * @param string $dataValue Value of variable * @param boolean $system Whether system variable * * @return BaseBuilder */ public function save(int $userId, string $dataKey, string $dataValue, bool $system = null) { $builder = $this->builder(); $builder->where('user_id', $userId); $builder->where('data_key', $dataKey); $builder->where('system', ($system ? 1 : 0)); if ($builder->countAllResults()) { $response = $this->update($userId, $dataKey, $dataValue, $system); } else { $response = $this->insert($userId, $dataKey, $dataValue, $system); } return $response; } /** * Inserts User Variable * * @param integer $userId User id * @param string $dataKey Key of variable * @param string $dataValue Value of variable * @param boolean $system Whether system variable * @return BaseBuilder */ public function insert(int $userId, string $dataKey, string $dataValue, bool $system = null) { $builder = $this->builder(); $data['user_id'] = $userId; $data['data_key'] = $dataKey; $data['data_value'] = $dataValue; $data['system'] = ($system ? 1 : 0); $data['created_at'] = date('Y-m-d H:i:s'); $data['updated_at'] = date('Y-m-d H:i:s'); return $builder->insert($data); } /** * Update User Variable * * @param integer $userId User id * @param string $dataKey Key of variable * @param string $dataValue Value of variable * @param boolean $system Whether system variable * * @return BaseBuilder */ public function update(int $userId, string $dataKey, string $dataValue, bool $system = null) { $builder = $this->builder(); $builder->where('user_id', $userId); $builder->where('data_key', $dataKey); $builder->where('system', ($system ? 1 : 0)); $data['data_value'] = $dataValue; $data['updated_at'] = date('Y-m-d H:i:s'); return $builder->set($data)->update(); } /** * Delete User Variable * * @param integer $userId User id * @param string $dataKey Key of variable * @param boolean $system Whether system variable * * @return BaseBuilder */ public function delete(int $userId, string $dataKey, bool $system) { $builder = $this->builder(); $builder->where('user_id', $userId); $builder->where('data_key', $dataKey); $builder->where('system', ($system ? 1 : 0)); return $builder->delete(); } /** * Provides a shared instance of the Query Builder. * * @param string $table Table name * * @return BaseBuilder */ protected function builder(string $table = null) { if ($this->builder instanceof BaseBuilder) { return $this->builder; } $table = empty($table) ? $this->table : $table; if (! $this->db instanceof BaseConnection) { $this->db = Database::connect($this->DBGroup); } $this->builder = $this->db->table($table); return $this->builder; } }