Sorry for the late
Adding a "send message to all users" option:
- Go to your database, and select your news table.
- At the top, if you are using phpMyAdmin, select to edit the table's structure.
- At the bottom you will see an "Add (1) columns..." click execute
- Create a new columns like this:
- Name: allowmessage
- Type: varchar
- Size: 3
- Predefined: As defined -> "no"
- Select "Null" checkbox
- Go to your class_news.php file and change it to this: (new stuff is commented with "NEW!")
PHP Code:
<?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;
private $allowmessage;//NEW!
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;
}
//NEW!
public function getAllowMessage(){
return $this->allowmessage;
}
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}");
}
}
//NEW!
public function changeAllowMessage($allow){
$mysidia = Registry::get("mysidia");
if($this->allowmessage != $allow){
$this->allowmessage = $allow;
$mysidia->db->update("news",array("allowmessage" => $this->allowmessage),"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}");
//NEW!
if($this->allowmessage == "yes")
$this->sendToAllUsers($todayDate);
}
//NEW!
public function sendToAllUsers($dateToday) {
$mysidia = Registry::get("mysidia");
$allusers = $mysidia->db->select("users", array("username"), "");
$messageTitle = "Update";
$messagetext = "A new news was posted on the site! Click <a href='/news'>here</a> to view!<br>";
while($user = $allusers->fetchColumn()){
$mysidia->db->insert("messages", array("fromuser" => "SYSTEM", "touser" => $user, "status" => "unread", "datesent" => $dateToday, "messagetitle" => $messageTitle, "messagetext" => $messagetext), "");
}
}
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;
}
}
?>
- Go to your newsview.php file inside the admincp folder and change it to this: (new stuff is commented with "NEW!")
PHP Code:
<?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(){
//include_once("../inc/ckeditor/ckeditor.php");
$mysidia = Registry::get("mysidia");
//$this->editor = new CKEditor;
//$this->editor->basePath = '../../../inc/ckeditor/';
$document = $this->document;
$document->setTitle("Edit");
$news = $this->getField("news");
if($mysidia->input->post("submit")){
/*if(!$mysidia->input->post("newstitle")) throw new BlankFieldException("No title");
if(!$mysidia->input->post("newscontent")) throw new BlanKFieldException("No content");*/
($mysidia->input->post("allowcomments"))?$allow = "yes":$allow = "no";
//NEW!
($mysidia->input->post("allowmessage"))?$allowM = "yes":$allowM = "no";
$news->editTitle($mysidia->input->post("newstitle"));
$news->editContent($this->format($mysidia->input->post("pagecontent")));
$news->changeAllowComment($allow);
//NEW!
$news->changeAllowMessage($allowM);
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);
//echo $mysidia->user->uid;
}
public function create(){
$mysidia = Registry::get("mysidia");
$document = $this->document;
$document->setTitle("Create");
if($mysidia->input->post("submit")){
$todayDate = new DateTime;
//H:i:s
$todayDate->setTimezone(new DateTimeZone('Europe/London'));
$date = $todayDate->format('Y-m-d H:i:s');
/*if(!$mysidia->input->post("newstitle")) throw new BlankFieldException("No title");
if(!$mysidia->input->post("newscontent")) throw new BlanKFieldException("No content");*/
($mysidia->input->post("allowcomments"))?$allow = "yes":$allow = "no";
($mysidia->input->post("allowmessage"))?$allowM = "yes":$allowM = "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, "allowmessage" => $allowM));
}
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, "allowmessage" => $allowM));
if($mysidia->input->post("allowmessage")){
//NEW!
$allusers = $mysidia->db->select("users", array("username"), "");
$today = new DateTime;
$dateToday = $today->format('Y-m-d');
$messageTitle = "Update";
$messagetext = "A new news was posted on the site! Click <a href='/news'>here</a> to view!<br>";
while($user = $allusers->fetchColumn()){
$mysidia->db->insert("messages", array("fromuser" => "SYSTEM", "touser" => $user, "status" => "unread", "datesent" => $dateToday, "messagetitle" => $messageTitle, "messagetext" => $messagetext), "");
}
}
}
$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;
}
}
?>
__________________
asp.net stole my soul.
|