wavelog/application/models/User_options_model.php

86 lines
3.2 KiB
PHP
Raw Permalink Normal View History

2023-08-18 12:49:12 +00:00
<?php
2023-08-18 14:42:04 +00:00
class User_options_model extends CI_Model {
public function set_option($option_type, $option_name, $option_array, $uid = null) {
if (empty($uid)) {
$uid = $this->session->userdata('user_id');
2025-04-24 16:10:44 +00:00
}
$sql = 'INSERT INTO user_options (user_id,option_type,option_name,option_key,option_value) VALUES (?,?,?,?,?) ON DUPLICATE KEY UPDATE option_value = ?';
foreach ($option_array as $option_key => $option_value) {
2023-08-18 14:42:04 +00:00
$query = $this->db->query($sql, array($uid, $option_type, $option_name, $option_key, $option_value, $option_value));
}
2026-07-11 16:17:29 +02:00
if (isset($query) && $query) {
return true;
} else {
2026-07-11 20:56:56 +02:00
log_message('error', 'set_option() failed for user_id: ' . ($uid ?? 'null') . ', option_type: ' . ($option_type ?? 'null') . ', option_name: ' . ($option_name ?? 'null'));
2026-07-11 16:17:29 +02:00
}
2023-08-18 14:42:04 +00:00
}
2023-12-04 23:02:53 +01:00
public function set_option_at_all_users($option_type, $option_name, $option_array) {
$query = $this->db->select('user_id')->get('users');
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$user_id = $row->user_id;
$sql = 'INSERT INTO user_options (user_id, option_type, option_name, option_key, option_value) VALUES (?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE option_value = ?';
foreach ($option_array as $option_key => $option_value) {
$this->db->query($sql, array($user_id, $option_type, $option_name, $option_key, $option_value, $option_value));
}
}
return true;
2023-12-04 23:02:53 +01:00
} else {
log_message('error', 'set_option_at_all_users() failed because users table is empty');
2023-12-04 23:02:53 +01:00
}
2024-04-10 09:03:46 +02:00
}
2023-12-04 23:02:53 +01:00
public function get_options($option_type, $option_array = null, $uid = null) {
if (empty($uid)) {
$uid = $this->session->userdata('user_id');
2024-03-07 16:34:22 +00:00
}
$sql_more = "";
$array_sql_value = array($uid, $option_type);
if (is_array($option_array)) {
foreach ($option_array as $key => $value) {
$sql_more .= ' AND ' . $key . ' = ?';
$array_sql_value[] = $value;
}
}
$sql = 'SELECT option_name, option_key, option_value FROM user_options WHERE user_id = ? AND option_type = ?' . $sql_more;
return $this->db->query($sql, $array_sql_value);
2023-08-18 12:49:12 +00:00
}
public function get_all_options_for_user($uid = null) {
if (empty($uid)) {
$uid = $this->session->userdata('user_id');
}
$sql = 'SELECT option_type, option_name, option_key, option_value FROM user_options WHERE user_id = ?';
$rows = $this->db->query($sql, array($uid))->result_array();
$options = array();
foreach ($rows as $r) {
// index by type -> name -> key; first row per (type,name,key) wins,
// matching the previous get_options()->row() behaviour
$options[$r['option_type']][$r['option_name']][$r['option_key']] = $r['option_value'];
}
return $options;
}
2025-10-27 15:37:56 +01:00
public function del_option($option_type, $option_name, $option_array = null, $uid = null) {
if (empty($uid)) {
2025-10-27 15:37:56 +01:00
$uid = $this->session->userdata('user_id');
}
$sql_more = "";
$array_sql_value = array($uid, $option_type, $option_name);
if (is_array($option_array)) {
foreach ($option_array as $key => $value) {
$sql_more .= ' AND ' . $key . ' = ?';
$array_sql_value[] = $value;
}
2024-04-10 09:03:46 +02:00
}
$sql = 'DELETE FROM user_options WHERE user_id = ? AND option_type = ? AND option_name = ?' . $sql_more;
return $this->db->query($sql, $array_sql_value);
2023-08-18 14:58:27 +00:00
}
2023-08-18 12:49:12 +00:00
}