Mys 1.3.6 News System

Forum
Last Post
Threads / Messages

KittHaven

Member
Member
Joined
May 21, 2013
Messages
479
Points
28
Age
25
Location
Devon, UK
Mysidian Dollar
8,299
Hello~ I finally finished porting IntoRain's News System over to 1.3.6! I have attached a .zip that has the main files so you can drop them into your site. Nothing should be overwritten.

Step 1 - Prepare the Database

Go to your PHP Myadmin and add 2 new database tables. Remember to use the prefix that your site uses (if you kept the default it will be 'adopts_'). The pictures below should show what you need to add.

adopts_news :

2023-07-23 02_19_30-localhost _ MySQL _ mysidia _ adopts_news _ phpMyAdmin 5.2.0 and 11 more p...png

adopts_newscomments :

2023-07-23 02_20_57-localhost _ MySQL _ mysidia _ adopts_newscomments _ phpMyAdmin 5.2.0 and 1...png


Step 2 - Preparing the Model Files

Next step is to open your site files. Go to model/domainmodel and create 2 new files. news.php and newscomments.php.

news.php:

PHP:
<?php
namespace Model\DomainModel;
use DateTime;
use HTMLPurifier;
use Resource\Core\Model;
use Resource\Core\Registry;
use Resource\Exception\InvalidIDException;
use Resource\Utility\Date;
use Resource\Collection\ArrayList;
class News extends Model{
 
    protected $nid;
    protected $user;
    protected $title;
    protected $content;
    protected $date;
    protected $posted;
    protected $allowcomments;
    private $htmlPurifier;
 
    public function __construct($nid, $dto = NULL, $htmlPurifier = TRUE){ 
        $mysidia = Registry::get("mysidia");
        if($htmlPurifier) $this->htmlPurifier = new HTMLPurifier;
        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 getComments(){
        $mysidia = Registry::get("mysidia");
        $stmt = $mysidia->db->select("newscomments",array("cid"),"nid = {$this->getID()} ORDER BY date,nid");
        $comments = new ArrayList;
        while($dto = $stmt->fetchObject()){
            $comments->add(new NewsComments($dto->cid, $dto));
        }
        return $comments;
    
    }
    public function addComment($content, $date, $user){
        $mysidia = Registry::get("mysidia");
        if($this->htmlPurifier) $content = $this->htmlPurifier->purify($this->format($content));
        $mysidia->db->insert("newscomments", ["cid" => NULL, "nid" => $this->nid, "user" => $mysidia->user->getID(), "comment" => $content, "date" => $date]);
    }
    public function format($text){
        return stripslashes(html_entity_decode(strip_tags($text, "<strong><em><b><i><u><s><p><sub><sup><a><li><ul><ol>")));
    }
 
    public function todayDate($format = NULL){
        $dateTime = new DateTime;
        $date = $dateTime->format($format);
        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();
    }
 
    protected function save($field, $value){
        $mysidia = Registry::get("mysidia");
        $mysidia->db->update("news", [$field => $value], "nid='{$this->nid}'");    
    }
}
?>

newscomments.php:

PHP:
<?php

namespace Model\DomainModel;
use DateTime;
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 $user;
    protected $comment;
    protected $date;
 
    public function __construct($cid, $dto = NULL){
        $mysidia = Registry::get("mysidia");
        if(!$dto){
            $whereClause = "cid = '{$cid}'";
             $dto = $mysidia->db->select("newscomments",array(),$whereClause)->fetchObject();
            if(!is_object($dto)) throw new InvalidIDException("Comment {$cid} does not exist...");
        }
        parent::__construct($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 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 getComment(){
        return $this->comment;
    }
 
    public function getDate($format = NULL){
        return $format ? $this->date->format($format) : $this->date;
    }
 
    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);
    }
 
    protected function save($field, $value){
        $mysidia = Registry::get("mysidia");
        $mysidia->db->update("newscomments", [$field => $value], "cid='{$this->cid}'");
    }
}
?>

Step 3 - Preparing the Controller

Next, open your controller/main folder, and add a new newscontroller.php file.

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\NoPermissionException;
use Resource\Exception\BlankFieldException;
use Resource\Exception\GuestNoaccessException;

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 DESC 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");
        $news = new News($nid);
        if($news->getPosted() == "no"){
            if($mysidia->usergroup->getpermission("canmanagecontent") != "yes")//rootadmins, admins and artists
            {
                throw new NoPermissionException("This message doesn't exist or isn't yet viewable.");
            }
        }
        $total = $mysidia->db->select("newscomments",["cid"],"nid = {$news->getID()}")->rowCount();
        $pagination = new Pagination($total, $mysidia->settings->pagination,
                                     "news/view/{$news->getID()}", $mysidia->input->get("page"));           
        $stmt = $mysidia->db->select("newscomments", [], "nid='{$news->getID()}' ORDER BY date DESC LIMIT {$pagination->getLimit()},{$pagination->getRowsperPage()}");
        $newscom = new ArrayList;
        while($dto = $stmt->fetchObject()){
            $newscom->add(new NewsComments($dto->cid, $dto));
        }
        $this->setField("pagination", $pagination);
        $this->setField("news", $news);
        $this->setField("newscomments", $newscom);
        if($mysidia->input->post("submit")){
            if(!$mysidia->input->rawPost("commenttext"))
                throw new BlankFieldException("Comment cannot be blank.");
            if($mysidia->user->getPermission("canvm") == "no")
                throw new NoPermissionException("You don't have permission to post comments.");
            if($news->getPosted() == "no")
                throw new NoPermissionException("Can't post comments to drafts.");
            $count = $mysidia->db->select("newscomments",array("cid"),"comment= '{$mysidia->input->rawPost("commenttext")}' and nid= '{$news->getID()}' and user= '{$mysidia->user->getID()}' LIMIT 1")->rowCount();
            if($count != 0)
                throw new NoPermissionException("Oops, seems like you tried to post the same comment.");
            if($news->getAllowComment() == "no")
                throw new NoPermissionException("No comments allowed on this news post.");
            if(!$mysidia->user->isLoggedIn())
                throw new GuestNoaccessException("guest");
        }
    
        }
    }
?>

Step 4 - Preparing the View

Next, open your view/main folder, and add a new newsview.php file.

newsview.php:

PHP:
<?php
namespace View\Main;
use Model\DomainModel\News;
use Model\DomainModel\NewsComments;
use Resource\Core\Registry;
use Resource\Core\View;
use Resource\Core\Model;
use Resource\Core\Mysidia;
use Resource\Model\DomainModel;
use Resource\GUI\Document\Comment;
use Resource\GUI\Container\FieldSet;
use Resource\GUI\Container\Form;
use Resource\GUI\Component\Button;
use Service\Builder\CKEditorBuilder;

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");
    
        $document->add(new Comment("<div id='news' class='news'>"));
        $iterator = $news->iterator();
        while($iterator->hasNext()){
            $news = $iterator->next();
            $newsField = new Fieldset("news");
            $comments = $news->getCommentNumber();
            $newsField->add(new Comment("<span class='date'>Date: {$news->getDate()}</span><br>",FALSE));
            $newsField->add(new Comment("<span class='title'><a href='{$home}news/view/{$news->getID()}'>{$news->getTitle()}</span>",FALSE));
            $newsField->add(new Comment("<span class='author'>by <a href='{$home}profile/view/{$news->getUsername()}' target='_blank'>{$news->getUsername()}</a></span>",FALSE));
            $newsField->add(new Comment("<span class='comment'><a href='{$home}news/view/{$news->getID()}'>Comments({$comments})</a></span><br>",FALSE));
            $newsField->add(new Comment("<span class='content'><img class='authorimg' src='{$home}{$news->getProfile()->getAvatar()}'>{$news->getContent()}</span>",FALSE));
        
            $document->add($newsField);
            }
            $document->add(new Comment('</div>'));
            $document->addLangvar($pagination->showPage());
        }
        public function view(){
            $mysidia = Registry::get("mysidia");
            $home = SCRIPTPATH. "/";
            $document = $this->document;
            $news = $this->getField("news");
            $comments = $this->getField("newscomments");
            $pagination = $this->getField("pagination");
            ($news->getPosted() == "yes")? $newsState = "":$newsState = " (draft)";
            $document->setTitle("news");
            $document->add(new Comment("<div id='news' class='news'>"));
            $newsField = new Fieldset("news");
            $newsField->add(new Comment("<span class='date'>Date: {$news->getDate()}</span><br>",FALSE));
            $newsField->add(new Comment("<span class='title'>{$news->getTitle()}{$newsState}</span>",FALSE));
            $newsField->add(new Comment("<span class='author'>by <a href='{$home}profile/view/{$news->getUserID()}' target='_blank'>{$news->getUsername()}</a></span>",FALSE));
            $newsField->add(new Comment("<span class='content'><p><img class='authorimg' src='{$home}{$news->getProfile()->getAvatar()}'>{$news->getContent()}<p></span>",FALSE));
            $document->add($newsField);
            $document->add(new Comment("<h1>comments</h1>",FALSE));
            if($mysidia->input->post("submit")){
                $date = $news->todayDate();
                $content = $mysidia->input->rawPost("commenttext");
                $user = $mysidia->user->getID();
                $nid = $news->getID();
            
                $news->addComment($content,$date,$user);
                Header('Location: ' . $_SERVER['REQUEST_URI']);
            
            }
        
            $count = $mysidia->db->select("newscomments",["cid"],"nid = {$news->getId()}")->rowCount();
            if($count == 0){
                $document->add(new Comment("There are no comments to display."));
            }
            else{
                $iterator = $comments->iterator();
                while($iterator->hasNext()){
                    $comments = $iterator->next();
                    $commentField = new Fieldset("comment");
                    $commentField->add(new Comment("<span class='cdate'>Date: {$comments->getDate()}</span><br>",FALSE));
                    $commentField->add(new Comment("<span class='cauthor'>by <a href='{$home}profile/view/{$comments->getUserID()}' target='_blank'>{$comments->getUsername()}</a></span>",FALSE));
                    $commentField->add(new Comment("<span class='ccontent'><p><img src='{$home}{$comments->getProfile()->getAvatar()}'>{$comments->getComment()}<p></span>",FALSE));
                    $document->add($commentField);
                }
                $document->addLangvar($pagination->showPage());
            }
            if($news->getAllowComment() == "yes"){
                $editor = new CKEditorBuilder("basic");
                $commentForm = new Form("comment", "", "post");
                $commentForm->add(new Comment("<br>Leave a comment below", FALSE));
                $commentForm->add($editor->buildEditor("commenttext", "CKEditor for Mysidia " . Mysidia::version));
                $commentForm->add(new Button("Comment", "submit", "submit"));
                $document->add($commentForm);
            }
            else{
                $document->add(new Comment("<br>Comments have been disabled.</div>",FALSE));
            }
        
        }
    }
?>

That should allow you to view news on your site, read/add comments, etc. Now we need to add AdminCP functions!
 

Attachments

  • NewsFixed.zip
    8.7 KB · Views: 4
Last edited:
Step 5 - Preparing the AdminCP Controller

Next, open your controller/admincp folder, and add a new newscontroller.php file.

newscontroller.php:

PHP:
<?php
namespace Controller\AdminCP;
use Exception;
use Model\DomainModel\News;
use Model\DomainModel\NewsComments;
use Resource\Collection\ArrayList;
use Resource\Core\AppController;
use Resource\Core\Pagination;
use Resource\Core\Registry;
use Resource\Exception\InvalidIDException;
use Resource\Exception\NoPermissionException;
class NewsController extends AppController{
    public function __construct(){  
        parent::__construct("");
        $mysidia = Registry::get("mysidia");
        if($mysidia->usergroup->getpermission("canmanagecontent") != "yes"){
            throw new NoPermissionException("You do not have permission to manage news.");
        }
    }
    
    public function index(){
        $mysidia = Registry::get("mysidia");
        $total = $mysidia->db->select("news")->rowCount();
        if($total == 0) throw new InvalidIDException("There's no news to display.");
        $pagination = new Pagination($total, $mysidia->settings->pagination, "admincp/news", $mysidia->input->get("page"));
        $stmt = $mysidia->db->select("news", [], "1 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 comments(){
        $mysidia = Registry::get("mysidia");
        $total = $mysidia->db->select("newscomments")->rowCount();
        if($total == 0) throw new InvalidIDException("There are no comments to display.");
        $pagination = new Pagination($total, $mysidia->settings->pagination, "admincp/news/comments", $mysidia->input->get("page"));
        $stmt = $mysidia->db->select("newscomments", [], "1 ORDER BY nid ASC LIMIT {$pagination->getLimit()},{$pagination->getRowsperPage()}");
        $newscomments = new ArrayList;
        while($dto = $stmt->fetchObject()){ 
            $newscomments->add(new NewsComments($dto->cid, $dto));       
        }
        $this->setField("pagination", $pagination);
        $this->setField("newscomments", $newscomments);
    }
    public function add(){
        $mysidia = Registry::get("mysidia");
        $stmt = $mysidia->db->select("news");   
        $news = new ArrayList;
        while($dto = $stmt->fetchObject()){
            $news->add(new News($dto->nid, $dto));
        }
        $this->setField("news", $news);
    }
    
    public function edit($nid = NULL){
        $mysidia = Registry::get("mysidia");
        if(!$nid) return $this->index();
        $news = new News($nid);
        $this->setField("news", $news); 
    }
    
    public function delete($nid = NULL){
        $mysidia = Registry::get("mysidia");
        if(!$nid) return $this->index();
        $news = new News($nid);
        $this->setField("news", $news); 
    }
    public function editcomment($cid = NULL){
        $mysidia = Registry::get("mysidia");
        if(!$cid) return $this->index();
        $comment = new NewsComments($cid);
        $this->setField("comment", $comment);   
    }
    
    public function deletecomment($cid = NULL){
        $mysidia = Registry::get("mysidia");
        if(!$cid) return $this->index();
        $comment = new NewsComments($cid);
        $this->setField("comment", $comment);   
    }
}
?>

Step 6 - Preparing the AdminCP View
Next, open your view/admincp folder, and add a new newsview.php file.

newsview.php:

PHP:
<?php
namespace View\AdminCP;
use Resource\Collection\LinkedList;
use Resource\Core\Registry;
use Resource\Core\View;
use Resource\GUI\Component\TextField;
use Resource\GUI\Component\Image;
use Resource\GUI\Component\Link;
use Resource\GUI\Component\CheckBox;
use Resource\GUI\Component\Button;
use Resource\GUI\Container\FieldSet;
use Resource\GUI\Container\Form;
use Resource\GUI\Container\TCell;
use Resource\GUI\Document\Comment;
use Resource\GUI\Element\Align;
use Resource\Utility\URL;
use Resource\Utility\Date;
use Service\Builder\CKEditorBuilder;
use Service\Builder\TableBuilder;
use Service\Helper\TableHelper;
class NewsView extends View{
    
    public function index(){
        $mysidia = Registry::get("mysidia");
        $home = SCRIPTPATH. '/';
        $document = $this->document;    
        $news = $this->getField("news");
        $document->setTitle("News");
        $document->add(new Comment("You can view and manage news below.<br>"));
        $document->add(new Comment("<a href='{$home}admincp/news/add'>Add News</a><br>"));
        $document->add(new Comment("<a href='{$home}admincp/news/comments'>View Comments</a><br>"));
        $helper = new TableHelper;
        $newsTable = new TableBuilder("news");
        $newsTable->setAlign(new Align("center", "middle"));
        $newsTable->buildHeaders("NewsID", "Author", "Title", "Date", "Posted", "Comments", "Edit", "Delete", "View");      
        $newsTable->setHelper($helper);
        
        $iterator = $news->iterator();
        while($iterator->hasNext()){
            $news = $iterator->next();
            $cells = new LinkedList;
            $cells->add(new TCell($news->getID()));
            $cells->add(new TCell($news->getUsername()));
            $cells->add(new TCell($news->getTitle()));
            $cells->add(new TCell($news->getDate("Y-m-d")));
            $cells->add(new TCell($news->getPosted()));
            $cells->add(new TCell($news->getAllowComment()));
            $cells->add(new TCell($helper->getEditLink($news->getID())));
            $cells->add(new TCell($helper->getDeleteLink($news->getID())));
            $cells->add(new TCell(new Link("news/view/{$news->getID()}", "View News")));
            $newsTable->buildRow($cells);              
        }
        $document->add($newsTable);
        $pagination = $this->getField("pagination");
        $document->addLangvar($pagination->showPage());
    }
    
    public function comments(){
        $mysidia = Registry::get("mysidia");
        $newscomments = $this->getField("newscomments");
        $home = SCRIPTPATH. '/';
        $document = $this->document;
        $document->setTitle("Comments");
        $document->add(new Comment("You can view and manage news comments below.<br>"));
        $document->add(new Comment("<a href='{$home}admincp/news/add'>Add News</a><br>"));
        $document->add(new Comment("<a href='{$home}admincp/news'>View News</a><br>"));
        $helper = new TableHelper;
        $commentTable = new TableBuilder("comments");
        $commentTable->setAlign(new Align("center", "middle"));
        $commentTable->buildHeaders("CommentID", "NewsID", "User", "Comment", "Date", "Edit", "Delete");        
        $commentTable->setHelper($helper);
        
        $iterator = $newscomments->iterator();
        while($iterator->hasNext()){
            $newscomments = $iterator->next();
            $cells = new LinkedList;
            $cells->add(new TCell($newscomments->getID()));
            $cells->add(new TCell($newscomments->getNewsID()));
            $cells->add(new TCell($newscomments->getUsername()));
            $cells->add(new TCell($newscomments->getComment()));
            $cells->add(new TCell($newscomments->getDate("Y-m-d")));
                $editimg = new Image("templates/icons/cog.gif");        
                $editurl = new URL("admincp/news/editcomment/{$newscomments->getID()}");
                $delimg = new Image("templates/icons/delete.gif");      
                $delurl = new URL("admincp/news/deletecomment/{$newscomments->getID()}");
            $cells->add(new TCell(new Link($editurl, $editimg)));
            $cells->add(new TCell(new Link($delurl, $delimg)));
            $commentTable->buildRow($cells);
        }
        $document->add($commentTable);
        $pagination = $this->getField("pagination");
        $document->addLangvar($pagination->showPage());
        
    }
    public function add(){
        $mysidia = Registry::get("mysidia");
        $home = SCRIPTPATH. '/';
        $document = $this->document;
        $news = $this->getField("news");
        if($mysidia->input->post("submit")){
            if(!$mysidia->input->post("newstitle")) throw new BlankFieldException("You've left the title blank!");
            if(!$mysidia->input->post("pagecontent")) throw new BlankFieldException("You've left the content blank!");
            $date = new Date;
            $title = $mysidia->input->post("newstitle");
            $content = $mysidia->input->rawPost("pagecontent");
            $user = $mysidia->user->getID();
            ($mysidia->input->post("allowcomments"))?$allow = "yes":$allow = "no";
            ($mysidia->input->post("draft"))?$draft = "yes":$draft = "no";
            $mysidia->db->insert("news", ["nid" => NULL, "user" => $user, "title" => $title, "date" => $date->format('Y-m-d H:i:s'), "content" => $content, "posted" => $draft, "allowcomments" => $allow]);
            $document->add(new Comment("Successfully added news! Fill this page again to create a new post. Click <a href='{$home}admincp/news'>here</a> to see the news page.</br>"));
        }
        $document->setTitle("Add News");
        $document->add(new Comment("Here you can add news to the site.</br>"));
        $document->add(new Comment("<a href='{$home}admincp/news'>View News</a><br>"));
        $document->add(new Comment("<a href='{$home}admincp/news/comments'>View Comments</a><br>"));
        $newsForm = new Form("news", "add", "post");
        $editor = new CKEditorBuilder("full");
        
        $date = new Date;
        $basic = new FieldSet;
        $basic->add(new Comment("<b>Date:</b> {$date->format('Y-m-d')},&nbsp;&nbsp;", FALSE));
        $basic->add(new Comment("<b>Posting as:</b> {$mysidia->user->getUsername()}"));
        $basic->add(new Comment("<b>Title:</b> ", FALSE));
        $basic->add(new TextField("newstitle"));
        $basic->add(new Comment("<b>Content:</b> ", FALSE));
        $basic->add($editor->buildEditor("pagecontent", "Enter your content here."));
        $basic->add(new CheckBox("Allow comments on this news", "allowcomments", 1, "array"));
        $basic->add(new CheckBox("Post news immediately", "draft", 1, "array"));
        $basic->add(new Comment("<em>Leave unchecked to save as a draft.</em>"));
        $newsForm->add($basic);
        $newsForm->add(new Button("Post", "submit", "submit"));
        $document->add($newsForm);  
    }
    
    public function edit(){
        $mysidia = Registry::get("mysidia");
        $home = SCRIPTPATH. '/';
        $document = $this->document;
        $news = $this->getField("news");
        if($mysidia->input->post("submit")){
            if(!$mysidia->input->post("newstitle")) throw new BlankFieldException("You've left the title blank!");
            if(!$mysidia->input->post("pagecontent")) throw new BlankFieldException("You've left the content blank!");
            $date = $news->getDate('Y-m-d H:i:s');
            $title = $mysidia->input->post("newstitle");
            $content = $mysidia->input->rawPost("pagecontent");
            $user = $mysidia->user->getID();
            ($mysidia->input->post("allowcomments"))?$allow = "yes":$allow = "no";
            ($mysidia->input->post("draft"))?$draft = "yes":$draft = "no";
            $mysidia->db->update("news", ["user" => $user, "title" => $title, "date" => $date, "content" => $content, "posted" => $draft, "allowcomments" => $allow], "nid = '{$news->getID()}'");
            Header('Location: ' . $_SERVER['REQUEST_URI']);
        }
        $document->setTitle("Edit News");
        $document->add(new Comment("Here you can edit news added to the site.</br>"));
        $document->add(new Comment("<a href='{$home}admincp/news/add'>Add New News</a><br>"));
        $document->add(new Comment("<a href='{$home}admincp/news'>View News</a><br>"));
        $document->add(new Comment("<a href='{$home}admincp/news/comments'>View Comments</a><br>"));
        $newsForm = new Form("news", "{$news->getID()}", "post");
        $editor = new CKEditorBuilder("full");
        
        ($news->getAllowComment() == 'yes')?$allow = "yes":$allow = "no";
        ($news->getPosted() == 'yes')?$draft = "yes":$draft = "no";
        $basic = new FieldSet;
        $basic->add(new Comment("<b>Date:</b> {$news->getDate('Y-m-d')},&nbsp;&nbsp;", FALSE));
        $basic->add(new Comment("<b>Author:</b> {$mysidia->user->getUsername()}"));
        $basic->add(new Comment("<b>Title:</b> ", FALSE));
        $basic->add(new TextField("newstitle", $news->getTitle()));
        $basic->add(new Comment("<b>Content:</b> ", FALSE));
        $basic->add($editor->buildEditor("pagecontent", $news->getContent()));
        $basic->add(new CheckBox("Allow comments on this news", "allowcomments", 1, "{$allow}"));
        $basic->add(new CheckBox("Post news immediately", "draft", 1, "{$draft}"));
        $basic->add(new Comment("<em>Leave unchecked to save as a draft.</em>"));
        $newsForm->add($basic);
        $newsForm->add(new Button("Post", "submit", "submit"));
        $document->add($newsForm);  
        
    }
    
    public function delete(){
        $mysidia = Registry::get("mysidia");
        $home = SCRIPTPATH. '/';
        $news = $this->getField("news");
        $document = $this->document;
        $document->setTitle("Delete News");
        $document->add(new Comment("Here you can delete news from the site. <b>Warning:</b> This is irreversible.</br>"));
        $document->add(new Comment("<a href='{$home}admincp/news/add'>Add New News</a><br>"));
        $document->add(new Comment("<a href='{$home}admincp/news'>View News</a><br>"));
        $document->add(new Comment("<a href='{$home}admincp/news/comments'>View Comments</a><br>"));
        if($mysidia->input->post("delete")){
            $mysidia->db->delete("news", "nid = '{$news->getID()}'");
            $mysidia->db->delete("newscomments", "nid = '{$news->getID()}'");
            Header("Location: ../../news");
        }
        $deleteForm = new Form("delete", "{$news->getID()}", "post");
        $delete = new Fieldset;
        $delete->add(new Comment("Are you sure you wish to delete this news?"));
        $delete->add(new Button("Yes!", "delete", "submit"));
        $deleteForm->add($delete);
        $document->add($deleteForm);
        ($news->getPosted() == "yes")? $newsState = "":$newsState = " (draft)";
        $newsField = new Fieldset("news");
        $newsField->add(new Comment("<div id='news' class='news'>"));
        $newsField->add(new Comment("<span class='date'>Date: {$news->getDate()}</span><br>",FALSE));
        $newsField->add(new Comment("<span class='title'>{$news->getTitle()}{$newsState}</span>",FALSE));
        $newsField->add(new Comment("<span class='author'>by <a href='{$home}profile/view/{$news->getUserID()}' target='_blank'>{$news->getUsername()}</a></span>",FALSE));
        $newsField->add(new Comment("<span class='content'><p><img class='authorimg' src='{$home}{$news->getProfile()->getAvatar()}'>{$news->getContent()}<p></span></div>",FALSE));
        $document->add($newsField);
       
    }
    public function editcomment(){
        $mysidia = Registry::get("mysidia");
        $home = SCRIPTPATH. '/';
        $document = $this->document;
        $comment = $this->getField("comment");
        if($mysidia->input->post("submit")){
            if(!$mysidia->input->post("pagecontent")) throw new BlankFieldException("You've left the content blank!");
            $content = $mysidia->input->rawPost("pagecontent");
            $mysidia->db->update("newscomments", ["comment" => $content], "cid = '{$comment->getID()}'");
            Header('Location: ' . $_SERVER['REQUEST_URI']);
        }
        $document->setTitle("Edit Comment");
        $document->add(new Comment("Here you can edit comments users have added to the site.</br>"));
        $document->add(new Comment("<a href='{$home}admincp/news/add'>Add New News</a><br>"));
        $document->add(new Comment("<a href='{$home}admincp/news'>View News</a><br>"));
        $document->add(new Comment("<a href='{$home}admincp/news/comments'>View Comments</a><br>"));
        $commentForm = new Form("comment", "{$comment->getID()}", "post");
        $editor = new CKEditorBuilder("basic");
        $basic = new FieldSet;
        $basic->add(new Comment("<b>Date:</b> {$comment->getDate('Y-m-d')},&nbsp;&nbsp;", FALSE));
        $basic->add(new Comment("<b>Posted by:</b> {$comment->getUsername()}<br>"));
        $basic->add($editor->buildEditor("pagecontent", $comment->getComment()));
        $commentForm->add($basic);
        $commentForm->add(new Button("Post", "submit", "submit"));
        $document->add($commentForm);   
        
    }
    
    public function deletecomment(){
        $mysidia = Registry::get("mysidia");
        $home = SCRIPTPATH. '/';
        $document = $this->document;
        $comment = $this->getField("comment");
        $document->setTitle("Delete Comment");
        $document->add(new Comment("Here you can delete comments users have added to the site. <b>Warning:</b> This is irreversible.</br>"));
        $document->add(new Comment("<a href='{$home}admincp/news/add'>Add New News</a><br>"));
        $document->add(new Comment("<a href='{$home}admincp/news'>View News</a><br>"));
        $document->add(new Comment("<a href='{$home}admincp/news/comments'>View Comments</a><br>"));
        if($mysidia->input->post("delete")){
            $mysidia->db->delete("newscomments", "cid = '{$comment->getID()}'");
            Header("Location: ../../news/comments");
        }
        $deleteForm = new Form("delete", "{$comment->getID()}", "post");
        $delete = new Fieldset;
        $delete->add(new Comment("Are you sure you wish to delete this comment?"));
        $delete->add(new Button("Yes!", "delete", "submit"));
        $deleteForm->add($delete);
        $document->add($deleteForm);
        $commentField = new Fieldset("comment");
        $commentField->add(new Comment("<div id='news' class='news'>"));
        $commentField->add(new Comment("<span class='cdate'>Date: {$comment->getDate()}</span><br>",FALSE));
        $commentField->add(new Comment("<span class='cauthor'>by <a href='{$home}profile/view/{$comment->getUserID()}' target='_blank'>{$comment->getUsername()}</a></span>",FALSE));
        $commentField->add(new Comment("<span class='ccontent'><p><img src='{$home}{$comment->getProfile()->getAvatar()}'>{$comment->getComment()}<p></span></div>",FALSE));
        $document->add($commentField);
    }
}
?>
 
Last edited:
Step 7 - Styling
We're almost done! Next open the style.css file from your site theme so we can add a bit of styling to jazz the news up. Put it in both your main theme and your acp theme if you want styling on both!

CSS:
.news .content .authorimg{
   float:left;
   border: 1px dotted black;
   padding:2px;
   margin-right:10px;
   margin-bottom:4px;
   }
  
   .news fieldset .title:before{
   content:'>>> ';
   font-size:11px;
   }
  
   .news{
   color:brown;
   }
   .news fieldset .content{
   text-align:justify;
   color:black;
   }
  
   .news .date {
   font-size:10px;
   font-weight: bold;
   color:brown;
   }
  
   .news .author a{
   color:orange;
   }
  
   .news .author a:hover{
   color:brown;
   }
  
   .news .title {
   font-size:25px;
   font-weight:bold;
   color:orange;
  
   }
  
   .news .author:before {
   content: ' | ';
   }
   .news .author {
   color:brown;
   font-size:10px;
   }
  
   .news .comment{
   font-size:10px;
   }
  
   .news .comment:before{
   content: ' | ';
   }
  
   .pages{
   color:brown;
   }
   .page{
   color:white;
   background-color:brown;
   border-radius:5px;
   border:2px solid brown;
   }
   .page:hover{
   color:orange;
   background-color:white;
   border:0px;
   }
  
   .cdate{
   font-size:10px;
   font-weight: bold;
   color:brown;
   }
  
   .cauthor{
   color:brown;
   font-size:10px;
   }
   .ccontent{
   font-size:10px;
   text-align:justify;
   color:black;
   }
  
   .ccontent img{
   float:left;
   border: 1px dotted black;
   padding:2px;
   margin-right:10px;
   margin-bottom:4px;
   }

Step 8 - AdminCP Links
To add links to the news to the sidebar of your AdminCP, find and open model/viewmodel/adminsidebar.php.

Find:

PHP:
$components->add(new Division(new Comment("Content", FALSE)));
        $content = new Division;
        $content->add(new Link("admincp/content/add", "Add a Custom Page"));
        $content->add(new Link("admincp/content/edit", "Edit Custom Pages"));
        $content->add(new Link("admincp/content/delete", "Delete Custom Pages"));
        $components->add($content);

And add this underneath:

PHP:
$components->add(new Division(new Comment("News", FALSE)));
        $news = new Division;
        $news->add(new Link("admincp/news", "View News"));
        $news->add(new Link("admincp/news/comments", "View News Comments"));
        $news->add(new Link("admincp/news/add", "Add News"));
        $components->add($news);

Feel free to move the News links if you want it somewhere new!

And that should be it! I do have a few bonuses that I might write short tutorials for at a later date, including swapping whether news is set to be posted automatically and whether comments are allowed automatically. But it's late so that might come at a later time lol.

Let me know if you encounter any bugs and I can try to troubleshoot with you. Enjoy!
 
Images showing the mod off!

Main page:
  Spoiler: Img 


Viewing news and comments:
  Spoiler: Img 


AdminCP all news:
  Spoiler: Img 


AdminCP viewing comments:
  Spoiler: Img 


Adding news through AdminCP:
  Spoiler: Img 


Editing and deleting news through AdminCP:
  Spoiler: Img 


Editing and deleting comments through AdminCP:
  Spoiler: Img 
 
Thanks for this! I was just manually updating a page with the information and there was no pagination.
 
Quick question: My news feed isn't registering line breaks and if I put <br> it just prints that as well. It's also not registering any formatting like bold or italics. Where would I mess with this setting?
 
Quick question: My news feed isn't registering line breaks and if I put <br> it just prints that as well. It's also not registering any formatting like bold or italics. Where would I mess with this setting?
I'm not sure actually, I think it might be an issue with how the text editor CKEditor is set up on mysidia. I use the default ckeditor builder to create it, so I don't know if I could fix it (well, not to say it's impossible because obviously it wouldn't be, just I don't know how off the top of my head :LOL:)... the default mysidia shoutbox has the same issue as well unfortunately, if users try to bold or italicise their text it doesn't take. I can try to have a look at it but I haven't got much experience with how the ckeditor is installed on mysidia lol. If I do solve it I'll post a fix here, but no promises :LOL: My first idea is that mysidia strips the html tags from the text editor when submitted. Probably for safety reasons.

A temporary fix is to go directly into your database and put the correct tags in. You could add the tags in while making the news, then copy your text, submit the news, and paste it into the database and the tags will then work. Not very efficient, I know, but for a temporary measure that could be a way of doing it until I find a fix. Though you won't be able to edit the news if you do this because it will strip the tags when you submit again, so make sure you make any changes in the database. Or copy and paste any changes you make into the database again.

2023-11-12 00_32_31-Mysidia Adoptables v1.3.6 and 12 more pages - Personal - Microsoft​ Edge.png 2023-11-12 00_32_40-localhost _ MySQL _ mysidia _ adopts_news _ phpMyAdmin 5.2.0 and 12 more p...png
 
Okay, I kinda figured out what was doing it. It's not the CKEditor, it's the HTML Purifier bundle that Mysidia uses. I removed all references to HTML purifier that the news addon used but for some reason it's not working, so need some more work to try and figure it out.

Did manage to fix the shoutbox though. Just for some reason it's not working for the news, even though it shouldn't be using it anymore.....

EDIT: Replaced the shoutbox file with a better one.
 

Attachments

  • shoutboxservice.php
    1.5 KB · Views: 2
Last edited:
Managed to fix it! Actually easier than I thought. Turns out Mysidia has a function where you can use rawPost instead of post and it keeps the tags. Here are the replacement files. If you haven't changed anything in them you can just replace them. I also removed the HTML purifier because that seemed to also remove the tags lol.

Let me know if there's a bug because it's 2:30am so can't guarantee I covered all bases :LOL:

For reference I changed:

PHP:
$content = $mysidia->input->post("pagecontent");

to:

PHP:
$content = $mysidia->input->rawPost("pagecontent");

I didn't apply it to the title but it would probably work if you wanted. So something like:

Code:
$title = $mysidia->input->rawPost("newstitle");

EDIT: I readded HTML Purifier to user comments as that should be sanitised for safety. Did manage to fix the tag issues. It was so so simple lmao. I kept it removed from adding news as you should only have users you trust posting news anyway. I'm probably going to look at some other things with the news system I want to tweak so if I do that at some point I'll post an update here. In the future I might re-add HTMLPurifier back to addings news when I fix up some things so I can make sure it's working.
 

Attachments

  • NewsFixed.zip
    8.7 KB · Views: 5
Last edited:
Any advice on how to get this to play nice with the Bootstrap 5 theme you made? It doesn't have the style.css if I add it, it still seems to ignore the file as well. So I'm not getting the nice CSS and things are slightly squished on the news page (attaching the image)
1706328187150.png
 
Did you add the styling in part 7 to your CSS file? I'm not sure how the Bootstrap mod works but wherever you have the rest of your site CSS is where part 7 should go.
 
I added it to the file in Bootstrap you change the colors/css in, and it seems to be mostly working it's not doing the outlines / boxes but it did un-squish

1706397489360.png
 
Seems to be working for me!! Love the Mod, just one question - and its likely something I am doing wrong - I am using 1.3.6 for this site with Green as the theme -- is it supposed to create a Button for the adminCP section?

or just show the word News and then give you the options?

Everything's working, just not getting a button :) Added everything and modded the css etc. Function great, and if no button in adminCP thats fine!
 
Any advice on how to get this to play nice with the Bootstrap 5 theme you made? It doesn't have the style.css if I add it, it still seems to ignore the file as well. So I'm not getting the nice CSS and things are slightly squished on the news page (attaching the image)
View attachment 729


I use Bootstrap a lot for the 1.3.4 sites, and I believe the one to mod for css is style.kyt or something very similar. Not sure if Bootstrap 5 is something different, its late and I am tired :D
 

Similar threads

Users who are viewing this thread

  • Forum Contains New Posts
  • Forum Contains No New Posts

Forum statistics

Threads
4,283
Messages
33,144
Members
1,604
Latest member
grandcorsair
BETA

Latest Threads

Top