cloudlog/application/models/Note.php
Peter Goodhall 5c5bdf2f2f Add categories and management to notes feature
Introduces note categories with support for filtering, assigning, and managing categories. Adds category selection and creation to add/edit forms, category filtering to the notes list, and modals for deleting/merging categories and confirming note deletion. Updates controller and model logic to handle categories, and improves UI for notes management.
2026-01-09 13:17:56 +00:00

134 lines
3.6 KiB
PHP

<?php
class Note extends CI_Model {
function list_all($api_key = null, $filters = array()) {
if ($api_key == null) {
$user_id = $this->session->userdata('user_id');
} else {
$CI =& get_instance();
$CI->load->model('api_model');
if (strpos($this->api_model->access($api_key), 'r') !== false) {
$this->api_model->update_last_used($api_key);
$user_id = $this->api_model->key_userid($api_key);
}
}
$this->db->where('user_id', $user_id);
$category = isset($filters['category']) ? trim($filters['category']) : '';
$search = isset($filters['search']) ? trim($filters['search']) : '';
if ($category !== '') {
$this->db->where('cat', xss_clean($category));
}
if ($search !== '') {
$search_clean = xss_clean($search);
$this->db->group_start();
$this->db->like('title', $search_clean);
$this->db->or_like('note', $search_clean);
$this->db->group_end();
}
return $this->db->get('notes');
}
function list_categories() {
$user_id = $this->session->userdata('user_id');
$this->db->distinct();
$this->db->select('cat');
$this->db->where('user_id', $user_id);
$this->db->where('cat IS NOT NULL');
$this->db->where('cat !=', '');
$this->db->order_by('cat', 'ASC');
$query = $this->db->get('notes');
return array_map(function($row) { return $row->cat; }, $query->result());
}
function add() {
$chosen_category = trim($this->input->post('new_category'));
if ($chosen_category === '') {
$chosen_category = $this->input->post('category');
}
$chosen_category = $chosen_category === '' ? 'General' : $chosen_category;
$data = array(
'cat' => xss_clean($chosen_category),
'title' => xss_clean($this->input->post('title')),
'note' => xss_clean($this->input->post('content')),
'user_id' => $this->session->userdata('user_id')
);
$this->db->insert('notes', $data);
}
function edit() {
$chosen_category = trim($this->input->post('new_category'));
if ($chosen_category === '') {
$chosen_category = $this->input->post('category');
}
$chosen_category = $chosen_category === '' ? 'General' : $chosen_category;
$data = array(
'cat' => xss_clean($chosen_category),
'title' => xss_clean($this->input->post('title')),
'note' => xss_clean($this->input->post('content'))
);
$this->db->where('id', xss_clean($this->input->post('id')));
$this->db->where('user_id', $this->session->userdata('user_id'));
$this->db->update('notes', $data);
}
function delete($id) {
$this->db->delete('notes', array('id' => xss_clean($id), 'user_id' =>$this->session->userdata('user_id')));
}
function view($id) {
// Get Note
$this->db->where('id', xss_clean($id));
$this->db->where('user_id', $this->session->userdata('user_id'));
return $this->db->get('notes');
}
function ClaimAllNotes($id = NULL) {
// if $id is empty then use session user_id
if (empty($id)) {
// Get the first USER ID from user table in the database
$id = $this->db->get("users")->row()->user_id;
}
$data = array(
'user_id' => $id,
);
$this->db->update('notes', $data);
}
function CountAllNotes() {
// count all notes
$this->db->where('user_id =', NULL);
$query = $this->db->get('notes');
return $query->num_rows();
}
function replace_category($from, $to) {
$user_id = $this->session->userdata('user_id');
$from_clean = xss_clean(trim($from));
$to_clean = xss_clean(trim($to));
if ($from_clean === '') {
return 0;
}
if ($to_clean === '') {
$to_clean = 'General';
}
$this->db->where('user_id', $user_id);
$this->db->where('cat', $from_clean);
$this->db->update('notes', array('cat' => $to_clean));
return $this->db->affected_rows();
}
}
?>