Help with 1.3.6 News Mod

Forum
Last Post
Threads / Messages

KittHaven

Member
Member
Joined
May 21, 2013
Messages
478
Points
28
Age
25
Location
Devon, UK
Mysidian Dollar
8,292
Hello! I am attempting to bring the 1.3.4 news mod to 1.3.6 but have hit a snag.

So far there's no adminCP support, but I successfully have it showing on the news page with pagination:

2023-07-21 00_16_36-Greenshot.png


But for some reason I can't get the view page working. And I have no idea why lol. I get an error saying it doesn't exist on the server.

The URL is news/view/(newsID)

2023-07-21 00_17_29-404 Not Found and 8 more pages - Personal - Microsoft​ Edge.png

Here are my files, any help would be appreciated lol:

model/domainmodel/news.php:

PHP:
<?php

namespace Model\DomainModel;
use ArrayObject;
use Resource\Core\Model;
use Resource\Core\Registry;
use Resource\Exception\InvalidIDException;
use Resource\Native\MysString;
use Resource\Utility\Date;

class News extends Model{
    
    protected $nid;
    protected $user;
    protected $title;
    protected $content;
    protected $date;
    protected $posted;
    protected $allowcomments;
    
    public function __construct($nid, $dto = NULL){     
        $mysidia = Registry::get("mysidia");
        if(!$dto){
            $whereClause = "nid = '{$nid}'";
             $dto = $mysidia->db->select("news",array(),$whereClause)->fetchObject();
            if(!is_object($dto)) throw new InvalidIDException("News {$nid} does not exist...");
        }
        parent::__construct($dto);
    }   
    
    protected function createFromDTO($dto){
        parent::createFromDTO($dto);
        $this->date = new Date($dto->date);
    }

    public function getID(){
        return $this->nid;
    }

    public function getProfile(){
        return new UserProfile($this->user);
    } 
    
    public function getUserID($fetchMode = ""){
        if($fetchMode == Model::MODEL) return new Member($this->user);
        else return $this->user;
    }

    public function getUsername(){
        if(!$this->user) return NULL;
        return $this->getUserID(Model::MODEL)->getUsername();
    }

    public function getTitle(){
        return $this->title;
    }

    public function getContent(){
        return $this->content;
    }
    
    public function getDate($format = NULL){
        return $format ? $this->date->format($format) : $this->date;
    }

    public function getPosted(){
        return $this->posted;
    }

    public function getAllowComment(){
        return $this->allowcomments;
    }

    public function changeAllowComment(){
        $mysidia = Registry::get("mysidia");
        if($this->allowcomments != $allow){
            $this->allowcomments = $allow;
            $this->save("allowcomments", $this->allowcomments);
        }
    }

    public function addComment($content, $date, $user){
        $mysidia = Registry::get("mysidia");
        if($this->nid != 0) return FALSE;
        else{
            $date = new Date;
            $mysidia->db->insert("newscomments", ["cid" => NULL, "nid" => $this->nid, "uid" => $user->getID(), "comment" => $this->format($content), "date" => $date->format("Y-m-d H:i:s")]);
        }
        return TRUE;
    }

    public function format($text){
        return preg_replace('`<((script)|(style))[^>]*>[^<]*</\1>`si', '', stripslashes(html_entity_decode($text)));
    }

    public function saveDraft(){
        $mysidia = Registry::get("mysidia");
        $todayDate = $this->todayDate();
        if($this->date != $todayDate) $this->newDate($todayDate);
        $this->posted = "no";
        $this->save("posted", $this->posted);
    }

    public function post(){
        $mysidia = Registry::get("mysidia");
        $todayDate = $this->todayDate();
        if($this->date != $todayDate) $this->newDate($todayDate);
        $this->posted = "yes";
        $this->save("posted", $this->posted);
    }

    public function editContent($content){
        $mysidia = Registry::get("mysidia");
        $this->content = $content;
        $this->save("content", $this->content);
    }
    
    public function editTitle($title){
         $mysidia = Registry::get("mysidia");
        $this->title = $title;
        $this->save("title", $this->title);
    }
    
    public function todayDate(){
        $dateTime = new DateTime;
        $date = $dateTime->format('Y-m-d H:i:s');
        return $date;
    }
    public function newDate($date){
        $mysidia = Registry::get("mysidia");
        $this->date = $date;
        $this->save("date", $this->date);
    }

    public function getCommentNumber(){
        $mysidia = Registry::get("mysidia");
        $stmt = $mysidia->db->select("newscomments", ["cid"], "nid = '{$this->nid}'");
        return $stmt->rowCount();
    }
    
    public function getComments(){
        $mysidia = Registry::get("mysidia");
        $stmt = $mysidia->db->join("news", "news.nid = newscomments.nid")
                        ->select("newscomments", [], "nid = '{$this->nid}'");
        $comments = new ArrayObject;
        while($dto = $stmt->fetchObject()){
            $comments[] = new NewsComments($dto->nid, $dto);
        }
        return $comments;
    }
    
    protected function save($field, $value){
        $mysidia = Registry::get("mysidia");
        $mysidia->db->update("news", [$field => $value], "nid='{$this->nid}'");       
    }
}

model/domainmodel/newscomments.php:

PHP:
<?php

namespace Model\DomainModel;
use Resource\Core\Model;
use Resource\Core\Registry;
use Resource\Exception\InvalidIDException;
use Resource\Utility\Date;

class NewsComments extends Model{
    
    protected $cid;
    protected $nid;
    protected $uid;
    protected $comment;
    protected $date;
    
    public function __construct($cid, $dto = NULL){
        $mysidia = Registry::get("mysidia");
        if(!$dto){
            $prefix = constant("PREFIX");
            $dto = $mysidia->db->join("news", "news.nid = newscomments.nid")
                               ->select("newscomments", [], "{$prefix}newscomments.cid = :cid", ["cid" => $cid])->fetchObject();
            if(!is_object($dto)) throw new InvalidIDException("The comment ID {$cid} does not exist...");
        }
        $this->createFromDTO($dto);
    }
    
    protected function createFromDTO($dto){
        parent::createFromDTO($dto);
        $this->date = new Date($dto->date);
    }

    public function getID(){
        return $this->cid;
    }

    public function getNewsID(){
        return $this->nid;
    }
    
    public function getUserID($fetchMode = ""){
        if($fetchMode == Model::MODEL) return new Member($this->user);
        else return $this->user;
    }

    public function getUsername(){
        if(!$this->user) return NULL;
        return $this->getUserID(Model::MODEL)->getUsername();
    }
    
    public function getComments(){
        return $this->comment;
    }
    
    public function getDate($format = NULL){
        return $format ? $this->date->format($format) : $this->date;
    }

    public function setComments($comment){
        $mysidia = Registry::get("mysidia");
        $this->comment = $comment;
        $this->save("comment", $this->comment);
    }
    
    public function todayDate(){
        $dateTime = new DateTime;
        $date = $dateTime->format('Y-m-d H:i:s');
        return $date;
    }
    public function newDate($date){
        $mysidia = Registry::get("mysidia");
        $this->date = $date;
        $mysidia->db->update("newscomments",["date" => $this->date],"cid = {$this->cid}");
    }
    
    protected function save($field, $value){
        $mysidia = Registry::get("mysidia");
        $mysidia->db->update("newscomments", [$field => $value], "cid='{$this->cid}'");       
    }
}

newscontroller.php:

PHP:
<?php

namespace Controller\Main;
use Model\DomainModel\News;
use Model\DomainModel\NewsComments;
use Resource\Core\AppController;
use Resource\Core\Registry;
use Resource\Core\Pagination;
use Resource\Collection\ArrayList;
use Resource\Exception\InvalidIDException;
use Resource\Exception\NoPermissionException;


class NewsController extends AppController{

    public function __construct(){
        parent::__construct("member");   
    }
    
    public function index(){
        $mysidia = Registry::get("mysidia");
        $total = $mysidia->db->select("news", ["nid"], "posted='yes'")->rowCount();
        if($total == 0) throw new NoPermissionException("There is currently no news to display!");
        $pagination = new Pagination($total, $mysidia->settings->pagination,
                                     "news", $mysidia->input->get("page"));               
        $stmt = $mysidia->db->select("news", [], "posted='yes' ORDER BY nid ASC LIMIT {$pagination->getLimit()},{$pagination->getRowsperPage()}");   
        $news = new ArrayList;
        while($dto = $stmt->fetchObject()){
            $news->add(new News($dto->nid, $dto));
        }
        $this->setField("pagination", $pagination);
        $this->setField("news", $news);
        }

    public function view($nid){
        $mysidia = Registry::get("mysidia");
        $this->news = new News($nid);
        }
    }
?>

newsview.php:

PHP:
<?php

namespace View\Main;

use Smarty;
use Resource\Core\Registry;
use Resource\Core\View;
use Resource\Core\Model;
use Resource\Model\DomainModel;
use Model\DomainModel\News;
use Model\DomainModel\NewsComments;
use Model\DomainModel\UserProfile;
use Resource\GUI\Document\Comment;
use Resource\GUI\Container\FieldSet;
use Resource\Native\MysString;

class NewsView extends View{

    public function index(){
        $mysidia = Registry::get("mysidia");
        $home = SCRIPTPATH."/";

        $pagination = $this->getField("pagination");
        $news = $this->getField("news");
        $document = $this->document;       
        $document->setTitle("News");
        
        $iterator = $news->iterator();
        while($iterator->hasNext()){
            $news = $iterator->next();
              $comments = $news->getCommentNumber();
            $document->add(new Comment("<div id='news' class='news'>"));
            $document->add(new Comment("<span class='date'>Date: {$news->getDate()}</span><br>",FALSE));
            $document->add(new Comment("<span class='title'><a href='/news/view/{$news->getID()}'>{$news->getTitle()}</a></span>",FALSE));
            $document->add(new Comment("<span class='author'>by <a href='/profile/view/{$news->getUserID()}' target='_blank'>{$news->getUsername()}</a></span>",FALSE));
            $document->add(new Comment("<span class='comment'><a href='/news/view/{$news->getID()}'>Comments({$comments})</a></span><br>",FALSE));
            $document->add(new Comment("<span class='content'><img class='authorimg' src='{$home}{$news->getProfile()->getAvatar()}'>{$news->getContent()}</span></div>"));
            $document->add(new Comment('</br></br>'));
            }
            
            $document->addLangvar($pagination->showPage());
        }

        public function view(){
            $mysidia = Registry::get("mysidia");
            $news = $this->getField("news");
            $document = $this->document;
            $document->setTitle("News");
            $document->add(new Comment('Test'));
        }
    }

As you can see I'm trying to use view($nid) to call the ID in the URL but even news/view doesn't work. So I'm a little lost. I think it has to do with the __construct but I don't know what's wrong with it. Thank you!
 
It seems you ain’t passing variables from controller to view correctly. In the class NewsController, method view, this line will not work:

PHP:
$this->news = new News($nid);

You need to change it to something like this:

PHP:
$this->setField("news", $news);

Also I am not sure it is necessary to mess with the script path in the view class file. The error is enough strange and I think routing ain’t done properly.
 
Last edited:
It seems you ain’t passing variables from controller to view correctly. In the class NewsController, method view, this line will not work:

PHP:
$this->news = new News($nid);

You need to change it to something like this:

PHP:
$this->setField("news", $news);

Also I am not sure it is necessary to mess with the script path in the view class file. The error is enough strange and I think routing ain’t done properly.
Thank you but turns out it was me being a big dumb and forgetting the script was installed in a subfolder :LOL: So technically /news/view didn't exist because it was supposed to be /mysidia/news/view so it's working now. This is why I probably shouldn't try and code at 4am lmao! That's also why I've been using scriptpath with the $home variable because my actual site and my vanilla test site are both in subfolders, just makes it easier when moving files across without having to change links lol. I just forgot to use it in places that needed it this time :rolleyes:
 

Similar threads

Users who are viewing this thread

  • Forum Contains New Posts
  • Forum Contains No New Posts

Forum statistics

Threads
4,277
Messages
33,118
Members
1,602
Latest member
BerrieMilk
BETA

Latest Threads

Top