ACC SHELL
<?php
/**
* Settings_model
* To set right $domain and $root_relation, make one instance right after including
*
* @author Melounek
*/
class Settings_model extends Base_model {
public $table = "settings";
public static $domain;
public static $root_relation; // which settings should filter by domain
public static $settings; // result from get_all()
public function __construct($domain=null){
self::$domain = str_replace('www.',false,strtolower($_SERVER['SERVER_NAME']));
$this->set_root_relation();
}
/**
* find and set idr of root node for this domain
*/
public function set_root_relation($idr=null){
if(empty($idr)){
if(empty(self::$domain)) die("you have to set self::\$domain. [Settings_model::set_root_relation]");
$query = " # zjisti idr root uzlu dane domeny a vrati root_relation
select * from relations where id =
(SELECT id_relations FROM `settings` WHERE `name`='alias' AND `value` = '".self::$domain."' limit 1)";
}else{
$query = " # vrati root_relation podle idr
select * from relations where id = '".$idr."'";
}
self::$root_relation = $this->db->query($query)->row_array();
if(empty(self::$root_relation) && !_ADMIN){
die("This domain isn't in dattabase like alias. [settings_model::set_root_relation]");
}
return $this;
}
/**
* Get all settings of relation
*
* @param int $rel_id
* @param bool $strict false = will not report criticals if find nothing (used in admin)
* @return array|false
*/
public function get_all($rel_id=null,$strict=true){
if(!$rel_id) $rel_id = @self::$root_relation['id'];
if($rel_id){
$res = $this->db->query(" # take settings for my_uri::sef()
select * from settings where id_relations =
(SELECT id FROM relations r WHERE lft <= (select lft from relations where id=$rel_id limit 1)
&& rgt >= (select rgt from relations where id=$rel_id limit 1)
&& (select id from settings where id_relations=r.id && name='name' limit 1)
order by rgt limit 1)")->result_array();
foreach($res as $s){
if(in_array($s['name'],array("alias"))){
self::$settings[$s['name']][] = $s['value']; // some goes to array
}else{
self::$settings[$s['name']] = $s['value']; // take values to array
}
}
if(!empty(self::$settings)){
return self::$settings;
}
}
return false;
}
public function get_one($id){
// todo
}
/**
* @param string $name
* @param string $value
* @param string $lang 2-3 chars
* @param bool $multiple if its true, it will allways insert next row, not update
*/
public function save_row($name,$value,$lang=null,$multiple=false){
if(!$value) return true; // no blank values
if(!self::$root_relation['id']) Tools::critical("try to save settings without self::\$root_relation");
$row = $this->db->query(" # find same setting
select value from settings where name='".$name."' && id_relations='".self::$root_relation['id']."' && lang='".$lang."'")->row_array();
if($multiple && !empty($row['value']) && $row['value']==$value) return true; // no duplicates
if(empty($row) || $multiple){
$sql = "insert into settings (id_relations,name,value,lang) values ('".self::$root_relation['id']."','".$name."','".$value."','".$lang."')";
}else{
$sql = "update settings set value='".$value."', lang='".$lang."' where name='".$name."' && id_relations='".self::$root_relation['id']."'";
}
$this->db->query($sql);
return true;
}
}
ACC SHELL 2018