<?php
class BaseController
{
function BaseController($config,$db,$dbtable,$admin=false)
{
//echo "<br>".__FILE__.__LINE__;
$this->html = array();
$this->html['page'] = (isset($this->postget["page"]))?$this->postget["page"]:'';
$this->postget = $_POST?$_POST:$_GET;
$this->config = $config;
$this->db = $db;
$this->dbtable = $dbtable;
$this->model = new BaseModel($this->db,$this->config["db_prefix"].$this->dbtable);
$this->admin = $admin;
//echo "<br>".__FILE__.__LINE__;
}
function think()
{
switch (@$this->postget["action"])
{
//case $this->dbtable."_add_submit":$this->_add();break;
case $this->dbtable."_delete_submit":$this->delete($this->postget["id"]);break;
case $this->dbtable."_update_submit":$this->update($this->postget);break;
case $this->dbtable."_update_bunch_submit":$this->update_bunch();break;
}
}
function gets($condition="")
{
//echo " \n<br>".basename(__FILE__).":".__LINE__.":".$condition;
$html = array();
$count_ = $this->count_($condition);
$html['count_total'] = $count_;
$html["pagination"] = Misc::pagination($count_,$this->config["pagination"],@$this->postget["page"]);
$html["items"] = $this->gets_offset_limit($html["pagination"]["offset"], $this->config["pagination"],$condition);
return $html;
}
function count_($condition="")
{
//echo " \n<br>".basename(__FILE__).":".__LINE__;
if(!$this->admin)
{
if($condition)
$condition .= ($condition)?" AND ":" WHERE ";
$condition .= " is_enabled=1";
}
return $this->model->count_($condition);
}
function gets_offset_limit($offset=0,$limit=0,$condition="")
{
if(!isset($offset))
$offset = 0;
if(!isset($limit) || $limit==0)
{
$limit = (int)$this->config['pagination'];
}
if(!$this->admin)
{
if($condition)
$condition .= ($condition)?" AND ":" WHERE ";
$condition .= " is_enabled=1";
}
$condition .=" ORDER BY rank DESC, title, id DESC";
if($offset || $limit)
$condition .=" LIMIT $offset,$limit;";
//echo $condition;
$result = $this->model->gets($condition);
return $result;
}
function update_bunch()
{
$fields = array("is_enabled"=>0);
$delete = false;
foreach ($this->postget as $key=>$value)
{
if(preg_match("#^id_(\d+)_title$#",$key))$fields["title"] = Misc::_safe_db_text($value);
if(preg_match("#^id_(\d+)_comment$#",$key)) $fields["comment"] = Misc::_safe_db_text($value);
if(preg_match("#^id_(\d+)_rank$#",$key)) $fields["rank"] = (int)$value;
if(preg_match("#^id_(\d+)_is_enabled$#",$key)) $fields["is_enabled"] = (isset($value))?"1":"0";
if(preg_match("#^id_(\d+)_delete$#",$key))$delete=true;
if(preg_match("#^id_(\d+)$#",$key))
{
$fields["id"] = $value;
if($delete)
$this->model->delete("WHERE id=".$fields["id"]);
else
$this->model->update($fields);
$fields = array("is_enabled"=>0);
$delete = false;
}
}
}
function update()
{
if(!@$this->postget["id"]>0)
$this->model->fields = array();
else
$this->model->get("WHERE id=".(int)$this->postget["id"]);
echo " asdf<br>".basename(__FILE__).":".__LINE__;
$this->model->fields["is_enabled"] = (isset($this->postget["is_enabled"]))?1:0;
$this->model->fields["title"] = ($this->postget["title"] != "")?Misc::_nl2br($this->postget["title"]):"";
$this->model->fields["description"] = Misc::_safe_db_text($this->postget["description"]);
$this->model->fields["rank"] = (int)$this->postget["rank"];
//var_dump($this->model->fields);
$this->model->update();
}
function delete($id)
{
$this->model->delete("WHERE id=$id");
}
function get($condition)
{
if((int)$condition>0)
$condition = " WHERE id=".(int)$condition;
return $this->model->get($condition);
}
}
?>