<?php
namespace model;
/**
* [[
* __table = catalog
* __order = parentid, sort, id
* __primary = id
* __editable = title, name, parentid, status, sort
* __listable = id, title, status
* id = int catid
* name = string catname
* title = string cattitle
* parentid = int catparent
* status = bool catstatus
* sort = int catsort
* minorder = int catminorder
*
* fullpath = string
* url = object
* remainder = array
* ]]
*/
class Catalog extends Model implements Structural {
protected function init()
{
$this->registerFieldCallback('fullpath', 'makeFullPath');
$this->registerFieldCallback('url', 'makeURL');
if(!defined('ADMIN_MODE')) $this->addRestriction('status', '>', 0);
}
function prepareGet()
{
$this->update();
}
function get($selector) // string path || string fullpath || numeric id || array fullpath
{
// $this->db->debugOn();
$this->prepareGet();
//if(empty($selector)) return $this;
if(is_array($selector)) {
$pid = 0;
$remainder = null;
foreach($selector as $path) {
if(empty($path)) continue;
if(!empty($remainder)) {
array_push($remainder, $path);
continue;
}
$this->prepareGet();
$this->parentid = $pid;
$this->name = $path;
$rslt = $this->db->select($this->getJunction(), '*', $this->makeArray(self::CHANGED|self::PREDICATES, true), null, 1);
if($rslt && mysql_num_rows($rslt) > 0) {
$this->newResult($rslt);
} else {
$this->prepareGet(); // We changed path and parent before, resetting
$remainder = array($path);
continue;
}
if(!$this->hasResult()) break;
$pid = $this->id;
}
if($remainder) {
$this->remainder = $remainder;
}
return $this;
} elseif(is_numeric($selector)) {
$this->id = intval($selector);
} elseif(is_object($selector)) {
$this->id = $selector->sectionid;
} else {
$newselector = explode('/', $selector);
if(count($newselector) > 1) return $this->get($newselector);
$this->fullpath = $selector;
}
$this->fetch();
return $this;
}
function restrictToBrand(Manufacturer $mf)
{
$this->setJunction('INNER JOIN mf_in_cat USING (catid)', true);
$this->addRestriction('mfid', '=', $mf->id);
}
function getDescendantIds($and_self=false, $recursive=false, $secid=null) /* TODO: recursive */
{
if(is_null($secid)) $secid = $this->id;
$ids = $and_self ? array($secid) : array();
$where = array($this->unalias('parentid') => $secid);
$sort = $this->unalias($this->getDefaultOrder());
$rslt = $this->db->select($this->getTable(), $this->unalias('id'), $where, $sort);
if($rslt) while($r = mysql_fetch_row($rslt)) {
$ids[] = $r[0];
if($recursive) $ids = array_merge($ids, $this->getDescendantIds(false, true, $r[0]));
}
return $ids;
}
function firstChild()
{
$id = $this->id;
$this->prepareGet();
$this->parentid = $id;
$this->fetch();
return $this;
}
function getFirst()
{
$this->prepareGet();
$this->fetch();
return $this;
}
/* Structural interface */
function getPath() { return $this->name; }
function getFullPath() { return $this->fullpath; }
function getTitle() { return $this->title; }
function getByPath($path) { return $this->get($path); }
function getTopLevel()
{
$this->prepareGet();
$this->parentid = 0;
$this->fetchList();
return $this;
}
function getChildren()
{
$id = $this->id;
$this->prepareGet();
$this->parentid = $id;
$this->fetchList();
return $this;
}
function getParent() { return $this->get($this->parentid); }
function hasParent() { return $this->parentid > 0; }
/* Callbacks */
protected function makeFullPath()
{
$ancestor = clone $this;
if($ancestor->hasParent() && $ancestor->getParent() && $ancestor->hasResult()) {
$fullpath = $ancestor->fullpath . $this->name . '/';
} else {
if(defined('ADMIN_MODE')) $fullpath = '/'.$this->name.'/'; // I can't express how bad it is :(
else $fullpath = '/catalog/'.$this->name.'/';
}
return $fullpath;
}
protected function makeURL()
{
return new \helper\URL($this->fullpath);
}
}
?>
preferences:
47.23 ms | 402 KiB | 5 Q