All pastes #73996 Raw Edit

k4st

public php v1 · immutable
#73996 ·published 2006-06-28 15:05 UTC
rendered paste body
<?php//--------------------------------------------// Deal with everything that has to do with threads.//--------------------------------------------class ThreadController extends k4PageController {		function getPreFilters() 	{		return array('request', 'session', 'user', 'debug_pre', 'template');	}		//--------------------------------------------	// Get some of the more complicated routes to some of	// the actions.	//--------------------------------------------	function getRoutes() 	{		return array(			'thread' => '/:thread_id/:thread_name/',			'edit' => '/edit/:thread_id',			'quickedit' => '/quickedit/:thread_id',		);	}		//--------------------------------------------	// This is actually the index action, but since there	// are variables in the route it is named differently.	// Otherwise, this deals with viewing threads.	//--------------------------------------------		function thread(&$reg, &$req, &$resp) 	{		$info = &new InformationDisplay($reg, $req, $resp);				//--------------------------------------------		// Try to get the thread.		//--------------------------------------------				$threads = &$this->getFinder('threads');		$thread = &$threads->find($req->get('thread_id'));				if ($thread == NULL) 		{			$info->text("The selected thread does not exist.");		} 		else 		{			$user = &$reg->get('user');						//--------------------------------------------			// Check permissions			//--------------------------------------------						if(!$user->canDo('view', 'threads')) 			{				$info->display('permsRequired');			}									//--------------------------------------------			// Alter the number of people who have viewed this thread.			//--------------------------------------------						// We need to know if and when the user last saw this thread			$session = &$reg->get('session');			$viewings = &$session->get('k4_viewings');						// If this is the first time the user sees this thread in the			// current session, increment the number of views for the thread			if (!isset($viewings['threads'][$thread->getId()])) 			{				$thread->set('views', 1 + $thread->get('views'));				$thread->save();			}						// Save the time the the user last accessed this thread			$viewings['threads'][$thread->getId()] = time();						//--------------------------------------------			// Set some permissions to the template.			//--------------------------------------------						$resp->assign('can_add_comments', intval($user->canDo('post', 'comments')));			$resp->assign('can_view_comments', intval($user->canDo('view', 'comments')));			$resp->assign('can_rate_comments', intval($user->canDo('rate', 'comments')));			$resp->assign('can_edit_posts', $user->canDo('edit', 'comments') + $user->canDo('edit', 'threads'));						//--------------------------------------------						$thread->setUser($user);			$thread->setPermissions();						//--------------------------------------------			// Done. Display the right templates.			//--------------------------------------------						$this->setPage($thread->get('name'));			$resp->assignRef('thread', $thread);			$resp->assign('content', 'comments.html');			$this->setLayout('forum_base.html');		}	}		//--------------------------------------------	// Generic function to check if a user can create a thread	// Through permissions and such.	//--------------------------------------------		function _checkCreateThread(&$reg, &$req, &$resp) 	{		$user = &$reg->get('user');		$info = &new InformationDisplay($reg, $req, $resp);				if (!$user->canDo('post', 'threads')) 		{			$info->display('permsRequired');		}				if ($user->get('karma') < 1) 		{			$info->display('karmaRequired');		}	}		//--------------------------------------------	// Generic function to check if a user can edit a thread	// Through permissions and such.	//--------------------------------------------		function _checkEditThread(&$reg, &$req, &$resp) 	{		$user = &$reg->get('user');		$info = &new InformationDisplay($reg, $req, $resp);				if($req->get('thread_id') == 0)		{			$info->text("The thread that you are trying to edit doesn't exist!");		}				if (!$user->canDo('edit', 'threads')) 		{			$info->display('permsRequired');		}	}		//--------------------------------------------	// Add a thread to the database.	//--------------------------------------------		function &_saveThread(&$reg, &$req, &$resp, $edit = FALSE, $quick_edit = FALSE)	{		$user = &$reg->get('user');		$info = &new InformationDisplay($reg, $req, $resp);				//--------------------------------------------		// Register some REQUEST filters.. once registered, the		// $req->get('varname') will always be changed!		//--------------------------------------------		$req->registerFilter('name', new K4TrimFilter());		$req->registerFilter('body', new K4BBParserFilter());				if(!$edit)		{			//--------------------------------------------			// We're creating this thread.			//--------------------------------------------						$this->_checkCreateThread($reg, $req, $resp);						//--------------------------------------------			// Loop through the possible author invites and			// make an array of their names. The reason why			// it's done this way is for total backward 			// compatability with non-ajax and non-javascript			// users.			//--------------------------------------------						$authors = array();						for($i = 1; $i <= 10; $i++)			{				$res = $req->get('co_author_'. $i);				if($res && $res != $user->get('name') && $res != '')				{					$authors[] = trim($res);				}			}						//--------------------------------------------			// Add the thread to the database.			//--------------------------------------------			$threads = &$this->getFinder('threads');			$thread = &$threads->createRecord();			$thread->set('created', time());			$thread->set('name', $req->get('name'));			$thread->set('body', $req->get('body'));			$thread->set('user_id', $user->getId());			$thread->setTags($req->get('tags'));			$thread->setUser($user);			$thread->setKarma();			$thread->setUserKarma();			$thread->inviteCoAuthors($authors);		}		else		{			//--------------------------------------------			// We're updating this thread.			//--------------------------------------------						$this->_checkEditThread($reg, $req, $resp);			//--------------------------------------------			// Get our thread.			//--------------------------------------------			$finder = &$this->getFinder('threads');			$thread = &$finder->find($req->get('thread_id'));			if($thread === NULL)			{				$info->text("The thread that you are trying to edit doesn't exist!");			}						//--------------------------------------------			// Make sure the the thread record knows what type			// of editing it's doing.			//--------------------------------------------						$thread->setUser($user);						if($quick_edit)			{				$thread->setQuickEdit();			}			else			{				// TODO:{[Need to use asArray here because the handling of _REQUEST elements that are arrays is screwed up.[_saveThread[ThreadController}				$post = &$req->asArray();								$thread->setEdit();				$thread->setTags($req->get('tags'));				$thread->setCoAuthors($post['coauthors']);				$thread->set('name', $req->get('name'));			}						$thread->set('body', $req->get('body'));		}				//--------------------------------------------		// Return the thread!		//--------------------------------------------				return $thread;	}		//--------------------------------------------	// Save a thread, either by creating or updating	// it.	//--------------------------------------------		function _save(&$reg, &$req, &$resp, $edit = FALSE, $quick_edit = FALSE) 	{		$info = &new InformationDisplay($reg, $req, $resp);		$thread = &$this->_saveThread($reg, $req, $resp, $edit, $quick_edit);				if ($thread->save()) 		{			// redirect us			$path = '/thread/'. $thread->getId() . '/'. k4_us($thread->get('name')) .'/';			$info->text('Your thread has been saved.', $path, 3);		} 		else 		{						//--------------------------------------------			// Yahtzee! Some errors occured.. time to go back.			//--------------------------------------------						$resp->assign('thread', $req->getArray(FA_REQUEST_POST));			$resp->assign('error_msg', TRUE); // Show the error message						foreach ($thread->getInvalid() as $invalid) 			{				$resp->setError($invalid);			}						$func = $edit ? 'edit' : 'create';						$this->$func($reg, $req, $resp);		}	}		//--------------------------------------------	// Display for to create a thread.	//--------------------------------------------		function create(&$reg, &$req, &$resp) 	{		$this->_checkCreateThread($reg, $req, $resp);				$resp->assign('content', 'thread_create_form.html');				$this->setPage("Post a Thread");		$this->setLayout("forum_base.html");	}		//--------------------------------------------	// Show the form to edit a comment.	//--------------------------------------------		function edit(&$reg, &$req, &$resp)	{		$user = &$reg->get('user');		$info = &new InformationDisplay($reg, $req, $resp);				//--------------------------------------------		// Standard error checking.		//--------------------------------------------				$this->_checkEditThread($reg, $req, $resp);				//--------------------------------------------		// Get our thread.		//--------------------------------------------				$finder = &$this->getFinder('threads');		$thread = &$finder->find($req->get('thread_id'));				if($thread === NULL)		{			$info->text("The thread that you are trying to edit doesn't exist!");		}				//--------------------------------------------		// Permissions...		//--------------------------------------------				$can_invite_author = $can_remove_author = 0;				if($thread->get('user_id') == $user->getId())		{			$can_invite_author = 1;			$can_remove_author = 1;		}				// TODO:{[For the time being I've decided to not show a form for the original author to delete an author invite. I'm still thinking about this.[edit[ThreadController}				$thread->set('can_invite_author', $can_invite_author);		$thread->set('can_remove_author', $can_remove_author);				//--------------------------------------------		// Finish off with template stuff.		//--------------------------------------------				$resp->assign('content', 'thread_edit_form.html');		$resp->assignRef('thread', $thread);		$this->setPage("Edit a Thread");		$this->setLayout("forum_base.html");	}		//--------------------------------------------	// Show the form to quick edit a comment.	//--------------------------------------------		function quickedit(&$reg, &$req, &$resp)	{		$user = &$reg->get('user');				//--------------------------------------------		// Standard error checking.		//--------------------------------------------				$this->_checkEditThread($reg, $req, $resp);				//--------------------------------------------		// Get our comment.		//--------------------------------------------				$finder = &$this->getFinder('threads');		$thread = &$finder->find($req->get('thread_id'));				if($thread === NULL)		{			$this->informationPage("The thread that you are trying to edit doesn't exist!");		}				//--------------------------------------------		// Finish off with template stuff.		//--------------------------------------------				$resp->assignRef('thread', $thread);		$this->setLayout("thread_quickedit_form.html");	}		//--------------------------------------------	// Create the thread.. or at least try!	//--------------------------------------------		function post(&$reg, &$req, &$resp) 	{		$this->_save($reg, $req, $resp, FALSE, FALSE);	}		//--------------------------------------------	// Update the thread.. or at least try!	//--------------------------------------------		function update(&$reg, &$req, &$resp) 	{		$this->_save($reg, $req, $resp, TRUE, FALSE);	}		//--------------------------------------------	// Quick update the thread.. or at least try!	//--------------------------------------------		function quickupdate(&$reg, &$req, &$resp) 	{		$thread = &$this->_saveThread($reg, $req, $resp, TRUE, TRUE);		$thread->save();				//--------------------------------------------		// We're done, pass the new comment to the template		//--------------------------------------------				$finder = &$this->getFinder('threads');		$thread = &$finder->find($thread->getId());				$resp->assign('thread', $thread);		$this->setLayout("thread_view_body.html");	}		//--------------------------------------------	// Cancel quick editing.	//--------------------------------------------		function canceledit(&$reg, &$req, &$resp)	{		$finder = &$this->getFinder('threads');		$thread = &$finder->find($req->get('thread_id'));				$resp->assign('thread', $thread);		$this->setLayout("thread_view_body.html");	}}?>