EIOS/inc/class_NOTE.php

193 lines
6.8 KiB
PHP

<?php
class class_NOTE{
function __construct(){
GLOBAL $ST;//,$DB;
//$this->DB = $DB;
$this->TNotes = $ST['dbpf'].'_note_notes';
include_once 'class_SQLS.php';
$this->SQLS = new class_SQLS();
//$this->table_create();
if(isset($_SESSION['user'])){
$this->USER = $_SESSION['user'];
}else{
$this->USER = array();
}
}
//Таблица: acs_note_notes: id, data_c, id_user, title, notes, json, status
/**
* Создание таблицы
*/
private function table_create(){
$sql = 'CREATE TABLE IF NOT EXISTS `'.$this->TNotes.'` (
id int(11) NOT NULL AUTO_INCREMENT,
data_c int(11) NOT NULL,
id_user bigint NOT NULL,
title varchar(255) NOT NULL DEFAULT "",
notes text DEFAULT NULL,
json longtext DEFAULT NULL,
status int(11) NOT NULL,
primary key (id) ) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4;';
$rez = $this->SQLS->sqlUPD($sql);
}
function HTML(){
GLOBAL $smarty;
$this->smarty = $smarty;
$out = array(); $regim = 'show';
$GET = $_GET;
//echo '<pre>'.print_r($GET,1).'</pre>'; exit();
//https://eios.mkgtu.ru/notes/edit/12/
//Array( [notes] => [page] => edit [des] => 12)
$msg = array();
if(isset($_POST['add'])){
$msg = $this->add($_POST);
$GET['page'] = 'show';
}
if(isset($_POST['edit'])){
$msg = $this->edit($_POST);
$GET['page'] = 'show';
}
if(isset($GET['page'])){
if($GET['page']=='add'){
$regim = $GET['page'];
}
if($GET['page']=='edit'){
$regim = $GET['page'];
$id = (int)$GET['des'];
$tmp = $this->item($id);
//echo '<pre>'.print_r($tmp,1).'</pre>'; exit();
$item = $tmp['item'];
$smarty->assign('item', $item);
}
}
if($regim=='show') {
$items = $this->items();
$smarty->assign('items', $items);
}
$smarty->assign('msg', $msg);
$smarty->assign('regim', $regim);
$out['html'] = $smarty->fetch('notes/tpl_notes_main.html');
return $out;
}
function itemsHTML($filter){
include_once 'inc/smarty.php';
$rez = $this->items($filter);
$out['html']='';
if(count($rez['items'])) {
foreach ($rez['items'] as $k => $v) {
$smarty->assign('it',$v);
$out['html'] .= $smarty->fetch('notes/tpl_notes_fly_item.html');
}
}
return $out;
}
function addHTML(){
include_once 'inc/smarty.php';
$item['title'] = 'Заметка от '.date('H:i d.m.Y');
$item['notes'] = '';
$smarty->assign('item',$item);
$smarty->assign('regim','add');
$out['html'] = $smarty->fetch('notes/tpl_note_add.html');
$out['html'] = str_replace('note_add_ajREP','note_add_aj',$out['html']);
return $out;
}
/**
* Получение всех элементов
* @param $filter (array)
* @return (array)
*/
function items($filter=array()){
$out = array();
if(!isset($filter['order'])) $filter['order']='data_c DESC';
if(!isset($filter['limit'])) {
$filter['limit']['start']='';
$filter['limit']['step']='';
$limits = '';
}else{
$limits = ' LIMIT '.$filter['limit']['start'].','.$filter['limit']['step'];
}
$USER = $this->USER;
$sql = 'SELECT * FROM '.$this->TNotes.' WHERE id_user='.$USER['id'].' ORDER BY '.$filter['order'].$limits;
$rez = $this->SQLS->sqlSEL($sql);
//echo '<pre>'.print_r($rez,1).'</pre>'; exit();
$out = $rez;
//if(!$rez['err']&&$rez['kol']){
// foreach($rez['items'] as $k => $v){
//
// }
//}
return $out;
}
/**
* Получение одного элемента
* @param $id (integer)
* @return (array) err:0|1 item
*/
function item($id){
$USER = $this->USER;
$sql = 'SELECT * FROM '.$this->TNotes.' WHERE id_user='.$USER['id'].' AND id='.$id;
$rez = $this->SQLS->sqlSEL($sql);
if(count($rez['items'])) {
$rez['item'] = $rez['items'][1];
}else{
$rez['item'] = array();
}
unset($rez['items']);
$out = $rez;
//if(!$rez['err']&&$rez['kol']){
// foreach($rez['items'] as $k => $v){
//
// }
//}
return $out;
}
/**
* Подготовка данных для таблицы
* @param $data (array)
* @return (array)
*/
public function prepare_data($data){
GLOBAL $DB;
$USER = $this->USER;
//Таблица: acs_note_notes: id, data_c, id_user, title, notes, json, status
if(isset($data['data_c'])) $data['data_c']=strtotime($data['data_c']); else $data['data_c']=time();
if(isset($data['id_user'])) $data['id_user']=(int)$data['id_user']; else $data['id_user']=$USER['id'];
if(isset($data['title'])) $data['title']=$DB->rescape($data['title']); else $data['title']='';
if(isset($data['notes'])) $data['notes']=$DB->rescape($data['notes']); else $data['notes']='';
if(isset($data['json'])) $data['json']=$DB->rescape(json_encode($data['json'],JSON_UNESCAPED_UNICODE));
else $data['json']=$DB->rescape(json_encode(array()));
if(isset($data['status'])) $data['status']=$DB->rescape($data['status']); else $data['status']=0;
return $data;
}
/**
* Добавление элемента в таблицу
* @param $data (array)
* @return (array)
*/
public function add($data){
$data = $this->prepare_data($data);
//Таблица: acs_note_notes: id, data_c, id_user, title, notes, json, status
$sql = 'INSERT INTO '.$this->TNotes.' VALUES(0,'.$data['data_c'].','.$data['id_user'].',"'.$data['title'].'","'.$data['notes'].'","'.$data['json'].'",'.$data['status'].')';
$out = $this->SQLS->sqlINS($sql);
return $out;
}
/**
* Редактирование элемента
* @param $data (array)
* @return (array)
*/
public function edit($data){
$data = $this->prepare_data($data);
//Таблица: acs_note_notes: id, data_c, id_user, title, notes, json, status
$sql = 'UPDATE '.$this->TNotes.' SET title="'.$data['title'].'", notes="'.$data['notes'].'", json="'.$data['json'].'",status='.$data['status'].' WHERE id='.$data['id'];
$out = $this->SQLS->sqlUPD($sql);
return $out;
}
}