Mys 1.3.4 News System

Forum
Last Post
Threads / Messages

IntoRain

Moderator
Moderator
Joined
Jul 31, 2013
Messages
459
Points
0
Location
Portugal
Mysidian Dollar
16,687
Hello! I know some people wanted a system like this so I decided to make one. This mod will allow you to post news to your users - you can create, edit and delete news. Using javascript, it has also a simple pagination system that will show a certain amount of news per page. Users can comment on each news post if you allow comments. News can be posted or saved as drafts to be edited later, admins can see drafts on the site but normal users can't. You can control users' comments: edit, delete and allow/disallow.

IMPORTANT:
If you have your site installed in a subfolder or if you changed page links, you might need to change some of the URLs used in the code. Please address to this post.

IMPORTANT:
It's not possible to send PM's to all users (selecting that option does nothing) about the updates yet!

IMPORTANT:
At the bottom of the post, there's a .rar you can download with the 4 new files (so you won't need to create the files yourself)

Part 0: The looks
After adding this, you should get something like this:
All news
Showing a news and its comments
Showing a specific news that doesn't allow comments

And the acp:
Control all news
Create news ones

Part 1: The database
  • The first step to make this work is create two tables on your website's database. For this go to phpMyAdmin, select your database and wait for everything to load on the left.
  • Scroll down and you will see "Create table". You will be creating two tables: news and newscomments (with whatever prefix your database has)
  • In this page, create 7 columns for the news and 5 for the newscomments (click Add columns -> Execute, to add more lines) with the exact info you will see on the images linked below (it's in portuguese, but the order should be the same) - be careful with the names!! And make sure the ID is set to AUTO_INCREMENT (A_I).
  • After this, your two tables should show up on the left
TUTORIAL IMAGES:
News table and
Newscomments table

Part 2: The objects
Go to your file manager now and let's start coding!
Inside the classes folder, create a file named class_news.php and add this content:

PHP:
<?php

use Resource\Native\Object;
use Resource\Native\Arrays;

class News extends Object {


	private $id;
	private $user;
	private $title;
	private $content;
	private $date;
	private $posted;
	private $comments;
	private $allowcomment;

	 public function __construct($id){
	 	$mysidia = Registry::get("mysidia");
	 	$whereClause = "id = '{$id}'";
	 	$row = $mysidia->db->select("news",array(),$whereClause)->fetchObject();
	 	if(!is_object($row)) throw new NoPermissionException("News doesn't exist");
	 	foreach($row as $key => $val){
           		 $this->$key = $val;     		 
        	}
        	$this->comments = $mysidia->db->select("newscomments",array("id"),"newsID = {$this->id} ORDER BY date,id");	
        }
        
        public function getID(){
        	return $this->id;
	}
	
	public function getUser(){
		return $this->user;
	}
	
	public function getUserObject(){
		$mysidia = Registry::get("mysidia");
		$user = new Member($this->user);
		return $user;
	}
	public function getTitle(){
		return $this->title;
	}
	
	public function getContent(){
		return $this->content;
	}
	
	public function getDate(){
		return $this->date;
	}
	
	public function getPosted(){
		return $this->posted;
	}
	
	public function getAllowComment(){
		return $this->allowcomment;
	}
	
	public function changeAllowComment($allow){
		$mysidia = Registry::get("mysidia");
		if($this->allowcomment != $allow){
			$this->allowcomment = $allow;
			$mysidia->db->update("news",array("allowcomment" => $this->allowcomment),"id = {$this->id}");
		}
		
	}
	
	public function addComment($content,$date,$user){
		$mysidia = Registry::get("mysidia");
		$mysidia->db->insert("newscomments",array("comment" => $this->format($content), "date" => $date, "userID" => $user, "newsID" => $this->id));
		$this->comments = $mysidia->db->select("newscomments",array("id"),"newsID = {$this->id} ORDER BY date,id");
	}
	
	private function format($text){
         	$text = html_entity_decode($text);
        	 $text = stripslashes($text);
        	 $text = str_replace("rn","",$text);
         	return $text;
        }
    
	public function saveDraft(){
		$mysidia = Registry::get("mysidia");
		$todayDate = $this->todayDate();
		if($this->date != $todayDate) $this->newDate($todayDate);
		$this->posted = "no";
		$mysidia->db->update("news",array("posted" => $this->posted),"id = {$this->id}");
	}
	
	public function post(){
		$mysidia = Registry::get("mysidia");
		$todayDate = $this->todayDate();
		if($this->date != $todayDate) $this->newDate($todayDate);
		$this->posted = "yes";
		$mysidia->db->update("news",array("posted" => $this->posted),"id = {$this->id}");
	}
        
        public function editContent($content){
        	$mysidia = Registry::get("mysidia");
        	$this->content = $content;
        	$mysidia->db->update("news",array("content" => $this->content),"id = {$this->id}");
        }
        
        public function editTitle($title){
        	 $mysidia = Registry::get("mysidia");
        	$this->title = $title;
        	$mysidia->db->update("news",array("title" => $this->title),"id = {$this->id}");
        }
        
        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("news",array("date" => $this->date),"id = {$this->id}");
        }
        
        public function getCommentNumber(){
        	$count = $this->comments->rowCount();
        	return $count;
        }
        
        public function getComments(){
        	return $this->comments;
        }
        
}

?>

In the same folder, create a class_newscomments.php file with these contents:

PHP:
<?php

use Resource\Native\Object;
use Resource\Native\Arrays;

class NewsComments extends Object {


	private $id;
	private $userID;
	private $newsID;
	private $comment;
	private $date;

	 public function __construct($id){
	 	$mysidia = Registry::get("mysidia");
	 	$whereClause = "id = '{$id}'";
	 	$row = $mysidia->db->select("newscomments",array(),$whereClause)->fetchObject();
	 	if(!is_object($row)) throw new NoPermissionException("News comment doesn't exist");
	 	foreach($row as $key => $val){
           		 $this->$key = $val;     		 
        	}	
        }
        
        public function getID(){
        	return $this->id;
	}
	
	public function getUser(){
		return $this->userID;
	}
	
	public function getUserObject(){
		$mysidia = Registry::get("mysidia");
		$user = new Member($this->userID);
		return $user;
	}
	
	public function getNews(){
		return $this->newsID;
	}
	
	public function getNewsObject(){
		$mysidia = Registry::get("mysidia");
		$news = new News($this->newsID);
		return $news;
	}
	
	public function getContent(){
		return $this->comment;
	}
	
	public function getDate(){
		return $this->date;
	}
        
        public function todayDate(){
        	$dateTime = new DateTime;
        	$date = $dateTime->format('Y-m-d H:i:s');
        	return $date;
        }    
        
        public function setContent($content){
        	$mysidia = Registry::get("mysidia");
        	$this->content = $content;
        	$mysidia->db->update("newscomments",array("comment" => $this->content),"id = {$this->id}");
        }
        

}

?>

Part 3: Displaying news on your site!
Now go to the main folder (where the files like account.php and some folders like admincp and classes are) and create the news.php file with these contents:

PHP:
<?php

use Resource\Native\String;
use Resource\Native\Float;
use Resource\Collection\ArrayList;

class NewsController extends AppController{

    public function __construct(){
        parent::__construct("member");	
    }
	
	public function index(){
	
		$mysidia = Registry::get("mysidia");
		$allnews = $mysidia->db->select("news",array("id"),"posted = 'yes' ORDER BY date DESC");
		$count = $allnews->rowCount();
		if($count == 0) throw new NoPermissionException("There are currently no news to display.");
		$this->setField("allnews",new DatabaseStatement($allnews));		

	}
	
	public function view(){
		$mysidia = Registry::get("mysidia");
		$pageURL = 'http';
		 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
		 $pageURL .= "://";
		 if ($_SERVER["SERVER_PORT"] != "80") {
		  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
		 } else {
		  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
		  }
		 
		$parts = Explode('/', $pageURL);
		$id = $parts[count($parts) - 1];
		$news = new News($id);
		
		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.");
			}
		}
		
		/*include_once("inc/ckeditor/ckeditor.php"); 	
		$editor = new CKEditor;	
	    	$editor->basePath = '../../inc/ckeditor/';
	    	$editor2 = $editor->editor("commentcontent", "Write here");
	    	$this->setField("editor",new DataObject($editor2));*/
	    	
	    	if($mysidia->input->post("submit")){
	    		if(!$mysidia->input->post("commentcontent"))
	    			throw new BlankFieldException("No content or content has an invalid character.");
	    		if($mysidia->usergroup->getpermission("canadopt") != "yes")
	    			throw new NoPermissionException("No permission");
	    		if($news->getPosted() == "no")
	    			throw new NoPermissionException("Can't post comments to drafts.");
	    		if($this->commentAlreadyExists($mysidia->input->post("commentcontent"),$id))
	    			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.");
	    	}  
	    	
	    	$news = new News($id); 	
	    	$this->setField("news",$news);

	}
	
	private function commentAlreadyExists($comment, $newsID){
		$mysidia = Registry::get("mysidia");
		$count = $mysidia->db->select("newscomments",array("id"),"comment = '{$comment}' and newsID = '{$newsID}' LIMIT 1")->rowCount();
		return ($count > 0);
	}
	
}
?>

Next, in the folder view, create newsview.php with these contents:

PHP:
<?php

class NewsView extends View{
	
	public function index(){
	
		$mysidia = Registry::get("mysidia");
		$document = $this->document;		
        	$document->setTitle("News");
        	
	
        	$allnews = $this->getField("allnews")->get();
        	$count = $allnews->rowCount();
        	$pagesTotal = ceil($count/2);
        	$document->add(new Comment("<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js'></script>
        	<script>        	
        			 var current = 1;
				 var max = current * 2;
				 var min = 1;
        		     
			$(document).ready(function(){
				
				$('fieldset+br').hide();
				$('[name^=news]').hide();
				
				for(var i = min; i <= max; i++)
				  {
				  	$('[name=news' + i +']').show();
				  	$('[name=news' + i +']+br').show();
				  } 	
				  
				  $('span[id^=page]').click(function(){
				  	current = $(this).html();
				  	max = current * 2;
				  	oldMax = (current - 1) * 2;
					
				  	min = oldMax + 1;
			  	
				  	$('fieldset+br').hide();
				  	$('[name^=news]').hide();
				  	
				  for(var i = min; i <= max; i++)
				  {
				  	$('[name=news' + i +']').show();
				  	$('[name=news' + i +']+br').show();
				  } 
				  
				  	
				  });
				});
			</script>"));
        	$document->add(new Comment("<span class='pages'><center>"));
        	for($page = 1; $page <= $pagesTotal; $page++)
        	{
        		$document->add(new Comment("<span id='page{$page}' class='page'>{$page}</span> | ",FALSE));      		
        	}
        	
        	
        	$document->add(new Comment("</center></span><br><div id='news' class='news'>"));
        	$index = 0;
        	while($news = $allnews->fetchColumn())
        	{
        		$newsObj = new News($news);
        		$index++;
        		$newsField = new Fieldset("news$index");
      			$comments = $newsObj->getCommentNumber();
        		$newsField->add(new Comment("<span class='date'>Date: {$newsObj->getDate()}</span><br>",FALSE));
        		$newsField->add(new Comment("<span class='title'>{$newsObj->getTitle()}</span>",FALSE));
        		$newsField->add(new Comment("<span class='author'>by <a href='/profile/view/{$newsObj->getUserObject()->getUsername()}' target='_blank'>{$newsObj->getUserObject()->getUsername()}</a></span>",FALSE));
        		$newsField->add(new Comment("<span class='comment'><a href='/news/view/{$newsObj->getID()}'>Comments({$comments})</a></span><br>",FALSE));
        		$newsField->add(new Comment("<span class='content'><img class='authorimg' src='{$newsObj->getUserObject()->getprofile()->getavatar()}'>{$newsObj->getContent()}</span>",FALSE));
        		
        		$document->add($newsField);
        	}
        	
        	$document->add(new Comment('</div>'));	
	}
	
	public function view(){
		$mysidia = Registry::get("mysidia");
		$document = $this->document;
		$news = $this->getField("news");
		($news->getPosted() == "yes")? $newsState = " ":$newsState = "(draft)";
		$newsName = $news->getTitle();
		$newsContent = $news->getContent();
		$newsAuthor = $news->getUserObject()->getUsername();
		$newsDate = $news->getDate();
		$newsComments = $news->getComments();
		$document->setTitle("news");
		
		$document->add(new Comment("<div id='news' class='news'>"));
		$newsField = new Fieldset("news");
		$newsField->add(new Comment("<span class='date'>Date: {$newsDate}</span><br>",FALSE));
        	$newsField->add(new Comment("<span class='title'>{$newsName}{$newsState}</span>",FALSE));
        	$newsField->add(new Comment("<span class='author'>by <a href='/profile/view/{$newsAuthor}' target='_blank'>{$newsAuthor}</a></span>",FALSE));
        	$newsField->add(new Comment("<span class='content'><p><img class='authorimg' src='../../{$news->getUserObject()->getprofile()->getavatar()}'>{$newsContent}<p></span>",FALSE));
		$document->add($newsField);
		$document->add(new Comment("<h1>comments</h1>",FALSE));
		
		if($mysidia->input->post("submit")){
			$todayDate = new DateTime;
			$date = $todayDate->format('Y-m-d H:i:s');
			$content = $mysidia->input->post("commentcontent");
			$user = $mysidia->user->uid;
			$newsID = $news->getID();
			
			$news->addComment($content,$date,$user);
			$document->add(new Comment("<b>Successfully added comment.</b>",FALSE));
			
		}
		
		$newsComments = $this->getField("news")->getComments();
		if($newsComments->rowCount() != 0){
		while($commentID = $newsComments->fetchColumn()){
			$comment = new NewsComments($commentID);
			$commentField = new Fieldset("comment{$comment->getID()}");
			$commentField->add(new Comment("<span class='cdate'>Date: {$comment->getDate()}</span><br>",FALSE));
			$commentField->add(new Comment("<span class='cauthor'>by <a href='/profile/view/{$comment->getUserObject()->getUsername()}' target='_blank'>{$comment->getUserObject()->getUsername()}</a></span>",FALSE));
			$commentField->add(new Comment("<span class='ccontent'><p><img src='../../{$comment->getUserObject()->getprofile()->getavatar()}'>{$comment->getContent()}<p></span>",FALSE));
			$document->add($commentField);
		}
		}
		else{
			$document->add(new Comment("No comments yet."));
		}
		
		if($news->getAllowComment() == "yes"){
		$commentForm = new Form("comment","","post");
		$commentForm->add(new TextArea("commentcontent", "", 4, 50));
		$commentForm->add(new Button("Submit","submit","submit"));
		$document->add($commentForm);
		$document->add(new Comment('</div>'));
		}
		else{
			$document->add(new Comment("<b>This page doesn't allow any more comments.</b>"));
		}
	}

	
}
?>

With this code, it will display two news per page. Change these values to change it to whatever you want (in the same file):

Click here


Now you will be able to access [yoursite]/news page and see that there aren't any news there. (Don't forget to add a /news link to your site! I added mine under Home).

Part 4: The AdminCP
Now let's add the adminCP pages so you can control all news and comments.

Inside the lang folder, there's an admincp folder. Create a lang_news.php file with these contents:

PHP:
<?php

//Language variables used for AdminCP/News Page

$lang['default_title'] = "News";

?>
Inside the admincp folder create a news.php file with these contents:

PHP:
<?php

class ACPNewsController extends AppController{

    const PARAM = "pageurl";
	private $editor;
	
	public function __construct(){	
	    parent::__construct();
		include_once("../inc/ckeditor/ckeditor.php"); 	
		$mysidia = Registry::get("mysidia");
	    $this->editor = new CKEditor;	
	    $this->editor->basePath = '../../../inc/ckeditor/';
		if($mysidia->usergroup->getpermission("canmanagecontent") != "yes"){
		    throw new NoPermissionException("You do not have permission to manage users.");
		}
	}
	
	public function index(){
	    parent::index();
	    $mysidia = Registry::get("mysidia");
	    $allnews = $mysidia->db->select("news",array("id"),"");
	    $this->setField("news",new DatabaseStatement($allnews));
		
	}

	public function viewcomments(){
	$mysidia = Registry::get("mysidia");
	$pageURL = 'http';
		 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
		 $pageURL .= "://";
		 if ($_SERVER["SERVER_PORT"] != "80") {
		  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
		 } else {
		  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
		  }
		 $parts = Explode('/', $pageURL);
		$id = $parts[count($parts) - 1];
		
		$news = new News($id);
		$this->setField("news",$news);
		
		 
		 if($mysidia->input->post("submit2")){
		 	$news = new News($id);
			$this->setField("news2",$news);
		 }

	}
	
	public function editcomment(){
		$mysidia = Registry::get("mysidia");
		$pageURL = 'http';
		 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
		 $pageURL .= "://";
		 if ($_SERVER["SERVER_PORT"] != "80") {
		  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
		 } else {
		  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
		  }
		 
		$parts = Explode('/', $pageURL);
		$id = $parts[count($parts) - 1];
		
		$newsComment = new NewsComments($id);
		$comment = $this->format($newsComment->getContent());
		
		if($mysidia->input->post("submit")){
			if(!$mysidia->input->post("commentcontent"))
				throw new BlankFieldException("No content");
			else
				$comment = $this->format($newsComment->getContent());
		}
				
		$this->setField("newscomments",$newsComment);
		$editor2 = $this->editor->editor("commentcontent",$comment);
		$this->setField("editor",new DataObject($editor2));
		
	}
	
	public function edit(){
		$mysidia = Registry::get("mysidia");
		if($mysidia->input->post("submit")){
			if(!$mysidia->input->post("newstitle")) throw new BlankFieldException("No title");
			if(!$mysidia->input->post("pagecontent")) throw new BlanKFieldException("No content");
			}
			
		$pageURL = 'http';
		 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
		 $pageURL .= "://";
		 if ($_SERVER["SERVER_PORT"] != "80") {
		  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
		 } else {
		  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
		  }
		 
		$parts = Explode('/', $pageURL);
		$id = $parts[count($parts) - 1];
		
		$news = new News($id);
		$this->setField("news",$news);
		$editor = $this->editor;
		$this->setField("editor",new DataObject($editor));
	}
	
	public function create(){
	$mysidia = Registry::get("mysidia");
		if($mysidia->input->post("submit")){
			if(!$mysidia->input->post("newstitle")) throw new BlankFieldException("No title");
			if(!$mysidia->input->post("pagecontent")) throw new BlanKFieldException("No content");
		}
		
		$editor = $this->editor->editor("pagecontent", "");	
		$this->setField("editor",new DataObject($editor));
	}
	
		    private function format($text){
         $text = html_entity_decode($text);
         $text = stripslashes($text);
         $text = str_replace("rn","",$text);
         return $text;
    }
}
?>

Now inside the folder admincp/view create a file newsview.php with these contents:

PHP:
<?php

use Resource\Native\String;
use Resource\Collection\LinkedList;
use Resource\Collection\LinkedHashMap;

class ACPNewsView extends View{
	
	private $editor;
	
	public function index(){
	    //parent::index();
	    $mysidia = Registry::get("mysidia");
		$document = $this->document;	
		$document->setTitle("Manage News And Comments");
		
		$pagesTable = new TableBuilder("news");
		$pagesTable->setAlign(new Align("center", "middle"));
		
		$pagesTable->buildHeaders("News Title", "Author", "Date", "Edit", "Publish/Save", "Delete", "View Comments", "View News");
		
		$allnews = $this->getField("news")->get();
		
		if($mysidia->input->post("submit")){
			$id = $mysidia->input->post("submit");
			$newsObj = new News($id);
			$newsObj->saveDraft();
			$document->add(new Comment("<b>Successfully saved news as draft</b>",TRUE));			
		}
		
		if($mysidia->input->post("submit1")){
			$id = $mysidia->input->post("submit1");
			$newsObj = new News($id);
			$newsObj->post();
			$document->add(new Comment("<b>Successfully added and published news.</b>",TRUE));
		}
		
		if($mysidia->input->post("submit2")){
			$id = $mysidia->input->post("submit2");
			$newsObj = new News($id);
			$document->add(new Comment("Are you sure you wish to delete this news?",TRUE));
			$form = new Form("title","","post");
			$form->add(new Button("Yes","submit5",$id));
			$document->add($form);		
			
		}
		if($mysidia->input->post("submit5")){
			$id = $mysidia->input->post("submit5");
			$mysidia->db->delete("news","id = {$id}");
			$document->add(new Comment("Successfully deleted news",TRUE));
			$allnews = $mysidia->db->select("news",array("id"),"");
		}
			
		while($news = $allnews->fetchColumn()){
			$newsObj = new News($news);
			$cells = new LinkedList;
			($newsObj->getPosted() == "yes")? $draft = "":$draft = "(draft)";
			$title = "{$newsObj->getTitle()} {$draft}";
			$cells->add(new TCell($title));
			$cells->add(new TCell($newsObj->getUserObject()->getUsername()));
			$cells->add(new TCell($newsObj->getDate()));
			$cells->add(new TCell(new Link("admincp/news/edit/{$news}","Edit")));
			
			$form = new Form("title","","post");
			$form->add(new Button("Save As Draft","submit",$news));
			$form2 = new Form("title","","post");
			$form2->add(new Button("Publish","submit1",$news));
			$form3 = new Form("title","","post");
			$form3->add(new Button("Delete","submit2",$news));
					
			($newsObj->getPosted() == "yes")? $cells->add(new TCell($form)) : $cells->add(new TCell($form2));
			$cells->add(new TCell($form3));
			$cells->add(new TCell(new Link("admincp/news/viewcomments/{$news}","View Comments")));
			$cells->add(new TCell(new Link("news/view/{$news}","View News On Site")));
			$pagesTable->buildRow($cells);	
		}
		
		$document->add($pagesTable);
		$document->add(new Comment("<a href='/admincp/news/create'>Create</a>",TRUE));
       
	}
	
	public function viewcomments(){
		$mysidia = Registry::get("mysidia");
		$document = $this->document;
		$document->setTitle("editing comments");
		$news = $this->getField("news");
		
		$newsComments = $news->getComments();
		
		
		$pagesTable = new TableBuilder("news");
		$pagesTable->setAlign(new Align("center", "middle"));
		
		$pagesTable->buildHeaders("Author", "Date", "Content", "Edit", "Delete");
		
		if($mysidia->input->post("submit")){
			$document->add(new Comment("Are you sure you wish to delete this comment?",FALSE));
			$id = $mysidia->input->post("submit");
			$form = new Form("title","","post");
			$form->add(new Button("Yes","submit2",$id));
			$document->add($form);

		}
					
		if($mysidia->input->post("submit2")){
				$id = $mysidia->input->post("submit2");
				//echo $id;
				$mysidia->db->delete("newscomments","id = {$id}");
				$document->add(new Comment("<b>Comment deleted successfully.</b>",FALSE));
				$news = $this->getField("news2");
				$newsComments = $news->getComments();
		}
			
		while($newsID = $newsComments->fetchColumn()){
			try{
				$newsComment = new NewsComments($newsID);
				$cells = new LinkedList;
				$cells->add(new TCell($newsComment->getUserObject()->getUsername()));
				$cells->add(new TCell($newsComment->getDate()));
				$cells->add(new TCell($newsComment->getContent()));
				$cells->add(new TCell(new Link("admincp/news/editcomment/{$newsComment->getID()}","Edit")));
				$form = new Form("form","","post");
				$form->add(new Button("Delete","submit",$newsID));
				$cells->add(new TCell($form));
				$pagesTable->buildRow($cells);
			}
			catch(NoPermissionException $e){
				
			}
			
		}
		
		$document->add($pagesTable);
	}
	
	public function editcomment(){
		$mysidia = Registry::get("mysidia");
		$document = $this->document;
		$document->setTitle("editing comments");
		$newsComment = $this->getField("newscomments");
		$editor = $this->getField("editor")->get();
		
		if($mysidia->input->post("submit")){
				$newsComment->setContent($mysidia->input->post("commentcontent"));
				$document->add(new Comment("<b>Edited news comment successfully. Text displayed is the old one.<br></b>",FALSE));
				$newsComment = $this->getField("newscomments");
				$editor = $this->getField("editor")->get();
		}
		
		$document->add(new Comment("Author: {$newsComment->getUserObject()->getUsername()} / Date: {$newsComment->getDate()}",FALSE));
		$form = new Form("form","","post");
		
		$form->add(new Comment($editor,FALSE));
		$form->add(new Button("Submit","submit","submit"));
		$document->add($form);
	}
	
	public function edit(){	
		$mysidia = Registry::get("mysidia");
		$document = $this->document;
		$document->setTitle("Edit");
		
		
			$news = $this->getField("news");					
			if($mysidia->input->post("submit")){
			
			($mysidia->input->post("allowcomments"))?$allow = "yes":$allow = "no";
			
			$news->editTitle($mysidia->input->post("newstitle"));
			$news->editContent($this->format($mysidia->input->post("pagecontent")));
			$news->changeAllowComment($allow);
			
			if($mysidia->input->post("submit") == "submitDraft"){
				$news->saveDraft();
			}
			
			else{
				$news->post();
			}
			$document->add(new Comment("<b>Successfully changed news.</b>"));
		}
		
		$editor = $this->getField("editor")->get()->editor("pagecontent", $this->format($news->getContent()));
		$form = new Form("title","","post");
		$form->add(new Comment("<b>Date</b>:{$news->getDate()} and <b>Author</b>: {$news->getUserObject()->getUsername()} and <b>Published</b>: {$news->getPosted()}"));
		$form->add(new Comment("<b>Title</b>:"));
		$form->add(new TextField("newstitle",$news->getTitle()));
		$form->add(new Comment("<b>Contents</b>:"));
		$form->add(new Comment($editor));
		$comments = new CheckBox("Allow comments on this news", "allowcomments", 1, "array");
		if($news->getAllowComment() == "yes")
			$comments->setChecked("allowcomments");
		$comments2 = new CheckBox("Send message to all users notifying about the existence of an update", "allowmessage", 2, "array");
		$form->add($comments);
		$form->add($comments2);
		$form->add(new Button("Save as draft","submit","submitDraft"));
		$form->add(new Button("Publish","submit","submitPublish"));
		$document->add($form);
		
	
	}
	
	public function create(){
		$mysidia = Registry::get("mysidia");
		$document = $this->document;
		$document->setTitle("Create");
		
		if($mysidia->input->post("submit")){
			$todayDate = new DateTime;
			$date = $todayDate->format('Y-m-d H:i:s');
		
			($mysidia->input->post("allowcomments"))?$allow = "yes":$allow = "no";
				
			
			if($mysidia->input->post("submit") == "submitDraft"){
							
			$mysidia->db->insert("news",array("title" => $mysidia->input->post("newstitle"), "content" => $this->format($mysidia->input->post("pagecontent")), "user" => $mysidia->user->uid, "date" => $date, "posted" => "no","allowcomment" => $allow));
			}
			
			else{
							
			$mysidia->db->insert("news",array("title" => $mysidia->input->post("newstitle"), "content" => $this->format($mysidia->input->post("pagecontent")), "user" => $mysidia->user->uid, "date" => $date, "posted" => "yes","allowcomment" => $allow));
			
				if($mysidia->input->post("allowmessage")){
					//$mysidia->db->insert("");
				}
			}
			$document->add(new Comment("<b>Successfully changed news. Fill this page again to create a new post. Click <a href='/news' target='_blank'>here</a> to see the news page.</b>"));
		}
		$todayDate = new DateTime;
		$date = $todayDate->format('Y-m-d');
		$editor = $this->getField("editor")->get();
		$form = new Form("title","","post");
		$form->add(new Comment("<b>Date</b>:{$date} and <b>Author</b>: {$mysidia->user->username} and <b>Published</b>: No"));
		$form->add(new Comment("<b>Title</b>:"));
		$form->add(new TextField("newstitle",""));
		$form->add(new Comment("<b>Contents</b>:"));
		$form->add(new Comment($editor));
		$comments = new CheckBox("Allow comments on this news", "allowcomments", 1, "array");
		$comments2 = new CheckBox("Send message to all users notifying about the existence of an update", "allowmessage", 2, "array");
		$form->add($comments);
		$form->add($comments2);
		$form->add(new Button("Save as draft","submit","submitDraft"));
		$form->add(new Button("Publish","submit","submitPublish"));
		$document->add($form);
	}
	
	    private function format($text){
         $text = html_entity_decode($text);
         $text = stripslashes($text);
         $text = str_replace("rn","",$text);
         return $text;
    }
}
?>

And so you can add the admincp/news link to your site, go to the file class_adminsidebar.php in the folder classes and find this:

PHP:
		$components->add(new Division(new Comment("Images", FALSE)));
		$image = new Division;
		$image->add(new Link("admincp/image/upload", "Upload Images"));
		$image->add(new Link("admincp/image/delete", "Erase Images"));
		$image->add(new Link("admincp/image/settings", "Adoptable Signature Image/GD Settings"));
		$components->add($image);

Add this after that piece of code:

PHP:
		$components->add(new Division(new Comment("News", FALSE)));
		$news = new Division;
		$news->add(new Link("admincp/news", "Manage News and Comments"));
		$news->add(new Link("admincp/news/create", "Create News"));
		$components->add($news);

Part 5: The CSS
If you just added started adding news you will see that the news won't display like what the pictures show. I modified the CSS in order to look like that.
If you are using the main theme, go to templates/main/media/style-city.css and add this at the bottom:

PHP:
.news fieldset{
color:black;
background-image: url(data:image/png;base64,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);
border-radius:5px;
border:white;
background-color: white;
/*border:2px dotted black;*/
border-bottom: 3px solid brown;

}

.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;
}

If you aren't using the main theme, Add that to the css of whatever theme you are using. Right click the page to see the HTML and check what each class does what.


----------------

Please warn me of any bugs you find. I tested it and it should be working properly, so please warn me if you find something that wasn't supposed to happen.
 

Attachments

  • Mys134 - News System.rar
    8.5 KB · Views: 61
Last edited:
Nice work, I am sure it took you quite a while to get this done and it must've been a valuable experience. It seems that you are getting a good understanding of the OOP and MVC system, which is nice to know. I hope users like it. ^^

I was wondering though, can you possibly provide a .rar or .zip file for users to download the files and uploads easily? This is likely to work for most sites since I see most of the changes are in new files.
 
Brilliant! Once I get my file transfers working again (glitches are killing me) I'll most certainly be trying this out! I've been dying for a news system, haha <3 ((Though I still plan to fix up my own...just hard to juggle learning code with everything else that's going on...))
 
Nice work, I am sure it took you quite a while to get this done and it must've been a valuable experience. It seems that you are getting a good understanding of the OOP and MVC system, which is nice to know. I hope users like it. ^^

I was wondering though, can you possibly provide a .rar or .zip file for users to download the files and uploads easily? This is likely to work for most sites since I see most of the changes are in new files.

Thank you! I had contact with OOP before in other language, but it's the first time using MVC, although it makes sense xD Getting the pages in jQuery to work was actually what took me longer to get (and I found two bugs today, so I'm not really confident about it xD ). I tried to implement this one but for some reason it wasn't working, so I made my own (it's simple and actually just hides the news that aren't supposed to show on a certain page lol)

Sure thing! I will add them to the first post! Actually makes things easier xD

Brilliant! Once I get my file transfers working again (glitches are killing me) I'll most certainly be trying this out! I've been dying for a news system, haha <3 ((Though I still plan to fix up my own...just hard to juggle learning code with everything else that's going on...))

Thank you! Hopefully this will be helpful for you!
 
I understand what you mean. Yeah Mysidia is not quite friendly with JQuery and javascript libraries at this moment, but it is expected to change in future. I am glad to know that you figured it out in the end, it could definitely take a while. Kyttias also told me that getting JQuery to work with Mysidia was painful. XD
 
I understand what you mean. Yeah Mysidia is not quite friendly with JQuery and javascript libraries at this moment, but it is expected to change in future. I am glad to know that you figured it out in the end, it could definitely take a while. Kyttias also told me that getting JQuery to work with Mysidia was painful. XD

It seems that adding .js files with jQuery to the header/template makes it not work properly for some reason, it took a while to figure out why it wasn't making a simple popup when the only thing I did was alert('hey') xD
A good thing about javascript in mysidia is that you can magically send php variables to the javascript since the script is inside a comment, without the need to query the database with ajax. So that's a good side! xD
 
You're welcome! Glad it worked for you! ^^ If you detect any bug, don't hesitate to tell me!
 
The only thing I noticed is that if you have your site in a subfolder, you need to go in and change the following code in the /view/newsview.php file:

Code:
$newsObj = new News($news);
        		$index++;
        		$newsField = new Fieldset("news$index");
      			$comments = $newsObj->getCommentNumber();
        		$newsField->add(new Comment("<span class='date'>Date: {$newsObj->getDate()}</span><br>",FALSE));
        		$newsField->add(new Comment("<span class='title'>{$newsObj->getTitle()}</span>",FALSE));
        		$newsField->add(new Comment("<span class='author'>by <a href='/profile/view/{$newsObj->getUserObject()->getUsername()}' target='_blank'>{$newsObj->getUserObject()->getUsername()}</a></span>",FALSE));
        		$newsField->add(new Comment("<span class='comment'><a href='/news/view/{$newsObj->getID()}'>Comments({$comments})</a></span><br>",FALSE));
        		$newsField->add(new Comment("<span class='content'><img class='authorimg' src='{$newsObj->getUserObject()->getprofile()->getavatar()}'>{$newsObj->getContent()}</span>",FALSE));

I added my subfolder name, "DigiChars", before the " /news/view/ " and " /profile/view/ " text.
 
This looks really good! I cant wait to try this out --- thanks for sharing this with us! :)
 
Nice mod, will definitely use this once I start working on my site. Right now I've been diggin g deep into learning MVC, OOP, and PDO before I even touch Mysidia.
 
Just created and added everything and it is functioning perfectly! I have to fix up the formatting a little bit so it works more neatly with the theme, but it's awesome so far!

How do you think one should go about making it so that a news alert appears at the top of the page until the member visits the news page? So each time a new news post is added members are alerted when they logon?
 
Nice mod, will definitely use this once I start working on my site. Right now I've been diggin g deep into learning MVC, OOP, and PDO before I even touch Mysidia.

Thank you! Hope it works well for you! ^^ OOP is one of those concepts that will make so much sense once it clicks xD

Just created and added everything and it is functioning perfectly! I have to fix up the formatting a little bit so it works more neatly with the theme, but it's awesome so far!

How do you think one should go about making it so that a news alert appears at the top of the page until the member visits the news page? So each time a new news post is added members are alerted when they logon?

Yay glad it worked for you! ^^ THank you for the input!
I was thinking of sending a new message to users to alert them of a new news, but I kinda got lazy and didn't do it lol
I guess it would work exactly like the messages system that alerts users when they get a new message. Maybe another table that contains all users and a field that says if they have been to the news page ever since a new news was posted. So when posting a news, all fields would reset to "unread" for all users and while it was unread, a message would appear somewhere in the site
 
Okay, I'm having this error:
CREATE TABLE `newscomments` (

`id` INT( 5 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`newsID` INT( 5 ) NOT NULL DEFAULT 'None',
`userID` INT( 5 ) NOT NULL DEFAULT 'None',
`comment` VARCHAR( 300 ) NULL DEFAULT NULL ,
`date` VARCHAR( 30 ) NULL DEFAULT NULL
) ENGINE = MYISAM
MySQL said: Documentation

#1067 - Invalid default value for 'newsID'

Any ideas?
 
Okay, I'm having this error:
CREATE TABLE `newscomments` (

`id` INT( 5 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`newsID` INT( 5 ) NOT NULL DEFAULT 'None',
`userID` INT( 5 ) NOT NULL DEFAULT 'None',
`comment` VARCHAR( 300 ) NULL DEFAULT NULL ,
`date` VARCHAR( 30 ) NULL DEFAULT NULL
) ENGINE = MYISAM
MySQL said: Documentation

#1067 - Invalid default value for 'newsID'

Any ideas?

Did you write "None" instead of selecting 'None' from the list? If it's not that, you can try running it directly and removing the default part:

Code:
CREATE TABLE  `[your_prefix_here]_newscomments` (

`id` INT( 5 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`newsID` INT( 5 ) NOT NULL,
`userID` INT( 5 ) NOT NULL,
`comment` VARCHAR( 300 ) NULL DEFAULT NULL ,
`date` VARCHAR( 30 ) NULL DEFAULT NULL
) ENGINE = MYISAM

This is by clicking on the "SQL" tab. Like here: http://i.imgur.com/VGRaF82.png
 
The phpMyAdmin that my host is using seems to be a bit dated, so it doesn't actually have a drop-down for most of the parts. I'll go through and run it directly to see if that'll work.

Thank you :)
 

Similar threads

Users who are viewing this thread

  • Forum Contains New Posts
  • Forum Contains No New Posts

Forum statistics

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

Latest Threads

Top