getSitegroup(); if ($sitegroup) { $this->whereAdd("sitegroup in (0,$sitegroup)"); } } function staticList($k,$v) { $t = new DataObjects_Topic; $t->$k = $v; $t->find(); return $t; } /* * static Get by path * return an article or topic or PEAR::error if not found! * */ function staticGetByPath($request) { //echo "GET $request"; if (!$request) { return; } $parts = explode('/',$request); // get the first bit... $topic = DataObjects_Topic::staticGetTopic(0,'name',rawurldecode($parts[0])); if (!$topic) { return; } array_shift($parts); while ($parts) { $lasttopic = $topic; $topic = $topic->getTopic('name', rawurldecode($parts[0])); if (!$topic) { if (count($parts) == 1) { if ($art = $lasttopic->getArticle('name', rawurldecode($parts[0]))) { return $art; } } return; } array_shift($parts); $lasttopic = $topic; } return $topic; } function staticGetTopic($up,$key,$val) { $topic = new DataObjects_Topic; $topic->up = $up; $topic->get($key,$val); if (!$topic->N) { return; } return $topic; } function getTopic($key,$val) { $topic = new DataObjects_Topic; $topic->up = $this->id; $topic->get($key,$val); if (!$topic->N) { return; } return $topic; } function getArticle($key,$val) { $article = new DataObjects_Article; $article->topic = $this->id; $article->get($key,$val); if (!$article->N) { return; } return $article; } function listArticles($key=NULL,$val=NULL,$sort=NULL) { $article = new DataObjects_Article; $article->topic = $this->id; if ($key !== NULL) { $article->$key = $val; } if ($sort !== NULL) { $this->orderBy($sort); } $article->orderBy('created DESC'); $article->find(); if (!$article->N) { return; } return $article; } function getLinkURL($base='',$top=0) { if ($base && ($base{strlen($base)-1} == "/")) { $base = substr($base,0,-1); } //echo "GOT TOP $top $base {$this->name}"; $ret = rawurlencode($base); $end = ""; if (!$top || ($this->id != $top)) { $end = rawurlencode($this->name); } $t = $this; while($t->up) { $t = DataObjects_Topic::staticGet($t->up); if ($t->id == $top) { break; } // echo "END IS -- $end --"; $end = rawurlencode($t->name) . "/". $end; //echo "END IS -- $end --"; } //echo "SETTING TO {$base}$end"; if ($end && $end{0} != "/") { $end = "/{$end}"; } return "{$base}$end"; } function buildLinkURL($base, $top=0) { $this->linkurl = $this->getLinkURL($base ,$top); } var $linkurl; // base url for topic function buildIconBlob() { $this->iconBlob = ""; if ($this->view) { $this->iconBlob = DataObjects_Blobs::staticGet( $this->view); //echo "{$this->iconBlob->name} "; $this->iconBlob->getUrl(); $this->iconBlob->hspace = (int) ((100- $this->iconBlob->sizex) /2) + 5; return; } $this->iconBlob = ""; //echo "LOOKING FOR IMG"; if (!($atts = $this->listattachments())) return; while ($atts->fetch()) { if (!preg_match("/00x100\.([a-z]+)$/i",$atts->name)) continue; $atts->getUrl(); if (!@$atts->size_x) { $atts->size_x = 100; $atts->size_y = 100; } $this->iconBlob = $atts; $t = DataObjects_Topic::staticGet($this->id); $t->view = $atts->id; $t->update(); return; } } function listAttachments() { return DataObjects_Blobs::staticList(get_class($this),$this->id); } function getAttachment($k,$v=NULL) { $t = new DataObjects_Blobs; $t->ptable = substr(get_class($this),strlen('dataobjects_')); $t->pid = $this->id; $t->get($k,$v); if (!$t) { return; } return $t; } function createAttachment($attachment, $data,$size) { $attachment->ptable = substr(get_class($this),strlen('dataobjects_')); $attachment->pid = $this->id; $attachment->insert(); $attachment->updateData($data,$size); } function getChildTypes() { return array("topic","article"); } function findChildrenOfType($type) { $classname = DB_DataObject::staticAutoloadTable($type); $t = new $classname; $t->findChildren($this->id); return $t; } function findChildren($parentid=null) { $this->up = $parentid; $this->find(); } function getParent() { $t = get_class($this); if (!$this->up) { return; } $c = new $t; $c->get($this->up); if (!$this->N) { return; } return $c; } function createChild($name,$type) { //echo "Create child $name, $type" ; if ($this->getTopic('name',$name)) { //echo 'creating error TTT'; return PEAR::raiseError('topic exists',NULL); } if ($this->getArticle('name',$name)) { //echo 'creating error AAA'; return PEAR::raiseError('article exists',NULL); } //DB_DataObject::debugLevel(1); switch ($type) { case 'dir': $t = new DataObjects_Topic; $t->up = $this->id; $t->name = $name; $t->insert(); return $t; break; case 'file': $t = new DataObjects_Article; $t->topic = $this->id; $t->name = $name; $t->insert(); return $t; break; } } /* overriden databobjects */ function insert() { //$this->_setSitegroup(); not at present! $this->_setCreated(); $this->_setRevised(); if (!$this->title) { $this->title = $this->name; } return parent::insert(); } function update() { $this->_setRevised(); return parent::update(); } /* set created/creator */ function _setCreated() { //$auth = PEAR::getStaticProperty('Auth','singleton'); //$this->creator = $auth->getUserId(); $date = new Date(time()); $this->created = $date->format('%Y-%m-%d %H:%M:%S'); } /* set revised/revisor */ function _setRevised() { //$auth = PEAR::getStaticProperty('Auth','singleton'); //$this->revisor = $auth->getUserId(); $date = new Date(time()); $this->revised = $date->format('%Y-%m-%d %H:%M:%S'); $this->revision++; } /* permissions management */ function canCreate() { return $this->canDelete(); } function canDelete() { $auth = &PEAR::getStaticProperty('Auth','singleton'); if (!$auth->isAdmin()) { return TRUE;; } $parent = $this->getParent(); if (!$parent) { return; } return $parent->canCreate(); } function canEdit() { if (!$this->owner) { return true; } $owner = DataObjects_Grp::staticGet($this->owner); $auth = PEAR::getStaticProperty('Auth','singleton'); if (!$owner) { return $auth->isAdmin(); } $user = $auth->getUser(); return $user->isMember($owner->id); } function dumptoFs($dir) { // echo "
";print_r($this);
$thisdir = "{$dir}/{$this->name}";
$title = $this->title;
if (!strlen($this->title)) $title = $this->name;
$out = "".htmlspecialchars($title)." ";
$out .= $this->description . "
";
if (strlen(trim($this->code))) {
$out .= "\n\n\n
__BODY__\n\n" . $this->code;
}
$out .= '';
$this->file_write_contents("$thisdir/index.html",$out);
$tops = $this->findChildrenOfType('topic');
while($tops->fetch()) {
$tops->dumptoFs($thisdir);
}
$tops = $this->findChildrenOfType('article');
while($tops->fetch()) {
$tops->dumptoFs($thisdir);
}
$blobs = $this->listAttachments();
if (!$blobs) {
return;
}
while ($blobs->fetch()) {
$blobs->dumptoFs($thisdir);
}
}
function file_write_contents($file,$cont) {
//echo "Writing $file
";//.htmlspecialchars($cont)."
";
//return;
//require_once 'System.php';
//echo 'mkdir -p '. escapeshellarg(dirname($file));
mkdir( dirname($file));
$fh = fopen($file, 'w');
fwrite($fh,$cont);
fclose($fh);
}
}
?>