John P . Looney wrote:

>  Someone else mentioned this earlier in the week .. but does anyone have
> some example code for a smart /articles active page ? 
> 
>  Something that'll look for /articles/sometopic/article_url.html as well
> as /articles/82 /articles/sometopic/93 etc.
> 
>  I think I can work it out myself, but it's a lot of effort! :)

attached to this message is a class I started to develop for the MOVE 
expample site.

it was not inensely tested, but shouldn't be harmful anyway...

you give it a path, and it will fetch topics and/or article automatically.

> Kate



-- 
Best Regards,
David Guerizec           Open Source Developer
Aurora R&D               [EMAIL PROTECTED]
Midgard core developer   http://www.midgard-project.org/
<?
if(!defined("MOVE_SNIPPET_GET_ARTICLE")) {
define("MOVE_SNIPPET_GET_ARTICLE",1);

class Move_Article {
var $id;
var $up;
var $topic;
var $name;
var $title;
var $abstract;
var $content;
var $url;
var $author;
var $extra1;
var $extra2;
var $extra3;
var $score;
var $type;
var $sitegroup;
var $calstart;
var $caldays;
var $icon;
var $view;
var $print;
var $creator;
var $created;
var $revisor;
var $revision;
var $revised;
var $approver;
var $approved;
var $locker;
var $locked;
};

function Approve_Article(&$a, $b=true) {
  return mgd_approve_article($a->id, $b);
}

function Lock_Article(&$a, $b=true) {
  if((!$a->lock) != (!$b)) return mgd_toggle_article_lock($a->id);
  else                     return false;
}

function Unlock_Article(&$a) {
  if($a->lock) return mgd_toggle_article_lock($a->id);
  else         return false;
}

function Score_Article(&$a) {
  return mgd_update_article_score($a->id, $a->score);
}

function Create_Article(&$a) {
  if(!($a->id = mgd_create_article($a->up, $a->topic, $a->name,
                                   $a->title, $a->abstract, $a->content,
                                   $a->author[0], $a->url, $a->calstart,
                                   $a->caldays, $a->icon, $a->view,
                                   $a->print, $a->extra1, $a->extra2,
                                   $a->extra3, $a->type))) return false;
  mgd_update_article_score($a->id, $a->score);
  return $a->id;
}

function Update_Article(&$a) {
  if(!($id = mgd_update_article($a->id, $a->topic, $a->name,
                                $a->title, $a->abstract, $a->content,
                                $a->author[0], $a->url, $a->calstart,
                                $a->caldays, $a->icon, $a->view,
                                $a->print, $a->extra1, $a->extra2,
                                $a->extra3))) return false;
  mgd_update_article_score($a->id, $a->score);
  mgd_update_article_type($a->id, $a->type);
  return $id;
}

// Move_Get_Article: automatic retreival of an article depending on a path
// if it cannot get an article, it will get a topic instead.
//
class Move_Get_Article {
var $root_topic;
var $topic = array();
var $article = array();
var $current_article;
var $current_topic;
var $type = false;
var $a_tag_open = "<a href=\"$midgard->self%s\">";
function Move_Get_Article($root, $geturl = 1) {
  if(!is_array($root)) {
    $tmp = explode("/", $root);
    $root = array();
    for($i = 0; $i < count($tmp); $i++) {
      $e = trim($tmp[$i]);
      if($e) $root[] = $e;
    }
  }
  if($topic = mgd_get_topic_by_name(0, $root[0])) {
    for($i = 1;
        $i < count($root) &&
             $topic = mgd_get_topic_by_name($topic->id, $root[$i]);
        $i++);
    if($topic) $this->root_topic = $topic;
  }
  $this->current_topic = $this->root_topic;
  if($geturl) $this->get_from_url();
}
function get_from_url($path = "") {
  global $argc, $argv;
  if(!$path) $path = $argv;
  if(count($path)) {
    return $this->walk_topics($path);
  } else
    return $this->type = false;
}

function make_article_url($id, $stopper=0) {
  if($stopper == $id || !($art = mgd_get_article($id))) return "";
  if(!($art = mgd_get_article($id))) return "";
  $url = "$art->name.html";
  while($art->up != $stopper && ($art = mgd_get_article($art->up))) {
    $url = "$art->name/$url";
  }
  return $this->make_topic_url($art->topic) . $url;
}

function make_topic_url($id) {
  $top = $this->root_topic;
  $stopper = $top->id;
  if($stopper == $id || !($top = mgd_get_topic($id))) return "";
  $url = "$top->name/";
  while($top->up != $stopper && ($top = mgd_get_topic($top->up))) {
    $url = "$top->name/$url";
  }
  return $url;
}

function walk_topics($path) {
  $this->topic = array();
  $this->article = array();
  $root=$this->root_topic;
  $count = count($path);
  if($tmp = mgd_get_topic_by_name($root->id, $path[0])) {
    $tmp->url_path = $this->make_topic_url($tmp->id);
    $this->topic[0] = $topic = $tmp;
    for($i = 1; $i < $count; $i++) {
      if($tmp = mgd_get_topic_by_name($topic->id, $path[$i])) {
        $tmp->url_path = $this->make_topic_url($tmp->id);
        $this->topic[$i] = $topic = $tmp;
      } else {
//        $this->current_topic = $topic;
        return $this->walk_articles($path, $i - 1);
      }
    }
  } else {
//    $this->current_topic = $topic;
    return $this->walk_articles($path, -1);
  }
  $this->current_topic = $topic;
  return $this->type = "topic";
}

function walk_articles($path, $index) {
  $idx = $index + 1;
  if($index < -1) return $this->type = false;
  if($index < 0) $root = $this->root_topic;
  else           $root = $this->topic[$index];

  if($article = mgd_get_article_by_name($root->id, $path[$idx])) {
    $article->url_path = $this->make_article_url($article->id);
    $this->article[0] = $article;
    for($i = $idx + 1; $i < count($path); $i++) {
      if($tmp = mgd_get_reply_by_name($article->id, $path[$i])) {
        $tmp->url_path = $this->make_article_url($tmp->id);
        $this->article[$i - $idx] = $article = $tmp;
      } else {
//      unset($this->topic[$index]);
        return $this->walk_articles($path, $index - 1);
      }
    }
  } else {
//  unset($this->topic[$index]);
    return $this->walk_articles($path, $index - 1);
  }
  if(!tmp) return $this->type = false;
  $this->current_article = $article;
  return $this->type = "article";
}

function location($a_tag_open, $a_tag_close, $sep) {
  for($i = 0; $i < count($this->topic); $i++) {
    $topic = $this->topic[$i];
    if($i) $code .= $sep;
    $code .= sprintf($a_tag_open, $topic->url_path)
           . $topic->name . $a_tag_close;
  }
  if($code) $code .= $sep;
  for($i = 0; $i < count($this->article); $i++) {
    $article = $this->article[$i];
    if($i) $code .= $sep;
    $code .= sprintf($a_tag_open, $article->url_path)
           . $article->title . $a_tag_close;
  }
  return ($code);
}

function list_latest_articles($id, $max=0) {
  if($art = mgd_list_topic_articles($id)) while(--$max && $art->fetch()) {
    $code .= sprintf($this->a_tag_open, $this->make_article_url($art->id))
           . $art->title . "</a><br>\n";
  }
  return ($code);
}

function list_latest_replies($id, $max=0) {
  if($art = mgd_list_reply_articles($id)) while(--$max && $art->fetch()) {
    $code .= sprintf($this->a_tag_open, $this->make_article_url($art->id))
           . $art->title . "</a><br>\n";
  }
  return ($code);
}

function list_topics($topic_id, $max=0) {
  if($top = mgd_list_topics($topic_id)) while(--$max && $top->fetch()) {
    $code .= sprintf($this->a_tag_open, $this->make_topic_url($top->id))
           . $top->name . "</a><br>\n";
  }
  return ($code);
}
//// end get_article object
};

} else { //MOVE_SNIPPET_GET_ARTICLE
  if(defined("MGD_SNIPPET_WARN_DOUBLE")) { 
    echo "WARNING: Snippet MOVE_SNIPPET_GET_ARTICLE already included<hr>\n";
  }
} //MOVE_SNIPPET_GET_ARTICLE
?>

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to