Вынос функционала blog в отдельный класс

master
indeferend 4 years ago
parent 6c1142f338
commit 9610250d13

@ -1,9 +1,56 @@
<?php
//модель для блога
class blog_model
{
public $table = "text_content";
function __construct($table=false)
{
//custom table in db
if ($table!==false) $this->table = $table;
}
public function get_it($id)
{
$res = LE::$DB->query_single("SELECT * FROM `".$this->table."` WHERE `id`=".$id);
$res['data'] = json_decode($res['data'],1);
return $res;
}
public function get_list($inp=false)
{
return LE::$DB->query_arr("SELECT * FROM `".$this->table."`",'id');
}
public function save_it($id,$data=[])
{
$data['id']=PRE::INT($id);
return LE::$DB->SAVE($this->table,$data);
}
public function rem_it($id=0)
{
$id=PRE::INT($id);
if (!$id>0) return false;
$res = LE::$DB->DEL($this->table,$id);
return ($res>0);
}
protected function check_dest_folder($f)
{
if (!is_dir($f))
if(mkdir($f,0777,true)===false)
return false;
return true;
}
public function add_img()
{
if (!isset($_FILES['upload'])) return false;
$dest = WEBDIR.'pub_data/upload/img/';
if (!$this->check_dest_folder($dest)) return false;
return LE_FS::SAVE_POST(['f_name'=>'upload','path'=>$dest]);
}
}

@ -117,7 +117,7 @@ abstract class LE_MOD_CONTROLLER
//возвращать массив ответа как есть, не оборачивая в data
if (isset($res['as_is']) && $res['as_is'])
{
unset($res['as_id']);
unset($res['as_is']);
$out = $res;
}

@ -1,94 +1,45 @@
<?php
class CONTR extends LE_MOD_CONTROLLER {
protected function check_dest_folder($f)
{
if (!is_dir($uploaddir))
if(mkdir($uploaddir,0777,true)===false)
return false;
return true;
}
protected function _ajx_add_img($data)
{
if (!isset($_FILES['upload'])) return false;
$dest = WEBDIR.'pub_data/upload/img/';
if (!$this->check_dest_folder($dest)) return false;
$filename = LE_FS::SAVE_POST(['f_name'=>'upload','path'=>$dest]);
if( ($filename = $this->model->add_img()) !==false )
return ['url'=>'/pub_data/upload/img/'.$filename, 'as_is'=>1];
return false;
}
protected function _ajx_save_content($data)
{
$id = PRE::INT($data['id']);
$html_cont = $data['html_cont'];
preg_match('!(<h1>(.*?)<\/h1>)?(.*)!simu',$html_cont,$out);
$html_cont = $out[3];
$html_head = trim($out[2]);
//return;
$save_data = ['id'=>$id,'html'=>$html_cont,'head'=>$html_head];
$id = LE::$DB->SAVE('text_content',$save_data);
return $id;
return $this->model->save_it($id,['html'=>$out[3],'head'=>trim($out[2])]);
}
protected function _ajx_remove_it($inp)
{
if (!isset($inp['id'])) return false;
$id=PRE::INT($inp['id']);
if (!$id>0) return false;
$res = LE::$DB->DEL('text_content',$id);
return ($res>0);
return $this->model->rem_it($inp['id']);
}
protected function _inp_default($inp)
{
$res = LE::$DB->query_arr("SELECT * FROM `text_content`",'id');
$to_tpl['cont_list'] = $res;
$data = $this->model->get_list();
$to_tpl['cont_list'] = $data;
return LE::$TPL->fetch('blog/list',$to_tpl);
}
protected function _inp_edit($inp)
{
$id = PRE::INT($inp);
if ($id>0)
{
$res = LE::$DB->query_single("SELECT * FROM `text_content` WHERE `id`=".$id);
$it_data = json_decode($res['data'],1);
}
else
{
$res = ['html'=>'','head'=>''];
$it_data = [];
$id=0;
}
$to_tpl = [
'data'=>$it_data,
'id'=>$id,
'html_cont'=>$res['html'],
'head'=>$res['head']
];
$id = PRE::INT($inp)+0;
$res = ($id>0) ? $this->model->get_it($id) : ['html'=>'','head'=>'','data'=>[]];
$to_tpl = [ 'data'=>$res['data'], 'id'=>$id, 'html_cont'=>$res['html'], 'head'=>$res['head']];
return LE::$TPL->fetch('blog/edit_item',$to_tpl);
}
}
include CLSDIR."blog.php";
$blog_model = new blog_model;
$controller = new CONTR($le_mod_loader->url,$blog_model);
LE::$TPL->mod_cont .= $controller->start();
LE::$TPL->mod_cont .= (new CONTR($le_mod_loader->url,$blog_model))->start();
Loading…
Cancel
Save