'/save_tag_group/:tags',
'untag' => '/untag/:thread_id',
'ungroup' => '/ungroup/:group_id',
);
}
function index(&$reg, &$req, &$resp)
{
$this->redirect('/dashboard/profile/');
}
//--------------------------------------------
// Display the form to edit a users profile.
//--------------------------------------------
function profile(&$reg, &$req, &$resp)
{
$this->checkLogin($reg, $req, $resp);
$resp->assign('content', 'dashboard_profile.html');
$this->setPage("Edit your Profile");
$this->setLayout('forum_base.html');
}
//--------------------------------------------
// Save a users profile.
//--------------------------------------------
function saveprofile(&$reg, &$req, &$resp)
{
$user = &$reg->get('user');
$info = &new InformationDisplay($reg, $req, $resp);
//--------------------------------------------
// Lol, this user isn't logged in...
//--------------------------------------------
$this->checkLogin($reg, $req, $resp);
//--------------------------------------------
// The user has specified the email check field,
// we'll assume that they're trying to change their
// email. But, it might be auto-filled in with something,
// so validating it is of course a priority.
//--------------------------------------------
$update_email = FALSE;
$update_pass = FALSE;
$new_email = NULL;
$new_pass = NULL;
$email_error = "";
$pass_error = "";
//--------------------------------------------
// This array will eventually become an array iterator
// that we will pass to the template. It does not necessarily
// need to hold errors :P
//--------------------------------------------
$errors = array();
if($req->get('email') && $req->get('email_check'))
{
$old_email = trim($req->get('email'));
$new_email = trim($req->get('email_check'));
$update_email = TRUE;
if($old_email != $user->get('email'))
{
$update_email = FALSE;
$email_error = "Your old email address does not match the one in your profile.";
}
if($update_email && !preg_match("#^[a-z0-9.!\#$%&\'*+-/=?^_`{|}~]+@([0-9.]+|([a-z0-9._-]+\.+[a-z]{2,4}))$#si", $new_email))
{
$update_email = FALSE;
$email_error = "The new email address that you specified is not a valid address.";
}
if($update_email && $old_email == $new_email)
{
$update_email = FALSE;
$email_error = "Hah. Why would you try to change your email address to what it already is?";
}
if($update_email)
{
$email_error = "Successfully changed your email address to ". $new_email .".";
}
$errors[] = array('text' => $email_error);
}
//--------------------------------------------
// They've specified their old password, we'll
// assume they are trying to change their password.
//--------------------------------------------
if($req->get('old_password'))
{
$update_pass = TRUE;
//--------------------------------------------
// The user didn't specify a new pass or validate
// it. We'll assume that browser autocompletion
// put in the old password and not give the user
// and error messages.
//--------------------------------------------
if($req->get('new_password') && $req->get('new_password_check'))
{
$old_pass = k4_hash($req->get('old_password'));
$new_pass = k4_hash($req->get('new_password'));
$new_pass_check = k4_hash($req->get('new_password_check'));
if($old_pass != $user->get('pass'))
{
$update_pass = FALSE;
$pass_error = "Your old password does not match the one in your profile.";
}
if($update_pass && $new_pass != $new_pass_check)
{
$update_pass = FALSE;
$pass_error = "Your new password and validation password do not match.";
}
if($update_pass)
{
$pass_error = "Successfully changed your password to ". $req->get('new_password') .".";
}
$errors[] = array('text' => $pass_error);
}
//--------------------------------------------
// They specified one or the other, but not both.
//--------------------------------------------
else if($req->get('new_password') || $req->get('new_password_check'))
{
$update_pass = FALSE;
$pass_error = "Please specify a new password and validate it.";
$errors[] = array('text' => $pass_error);
}
//--------------------------------------------
// No new passwords.
//--------------------------------------------
else
{
$update_pass = FALSE;
}
}
//--------------------------------------------
// We've passed all of the error checks.
//--------------------------------------------
if(empty($errors))
{
$errors[] = array('text' => "Nothing needed to be saved!");
}
$errors = &new FAArrayIterator($errors);
$resp->assignRef('error_messages', $errors);
//--------------------------------------------
// Do the updating.
//--------------------------------------------
if($update_pass)
{
$user->set('pass', $new_pass);
}
if($update_email)
{
$user->set('email', $new_email);
//TODO:{[should we validate the email by emailing the user?[saveprofile[DashboardController}
}
if($update_pass || $update_email)
{
$user->save();
}
$info->display('profileSaved');
}
//--------------------------------------------
// View this users tagged threads.. if any.
//--------------------------------------------
function tagged_threads(&$reg, &$req, &$resp)
{
$this->checkLogin($reg, $req, $resp);
$user = &$reg->get('user');
$finder = &$this->getFinder('bookmarked_threads');
$bthreads = &$finder->findAllWhere('bt.user_id=?', array($user->getId()), 'views DESC');
$resp->assign('in_tagged_threads', 1);
$resp->assignRef('tagged_threads', $bthreads);
$resp->assign('content', 'dashboard_tagged_threads.html');
$this->setPage("My Tagged Threads");
$this->setLayout('forum_base.html');
}
//--------------------------------------------
// View this users tagged threads.. if any, but
// get a smaller version for ajax junk :D
//--------------------------------------------
function tagged_threads_quick(&$reg, &$req, &$resp)
{
$this->checkXHR();
$this->checkLogin($reg, $req, $resp);
$user = &$reg->get('user');
$finder = &$this->getFinder('bookmarked_threads');
$bthreads = &$finder->findAllWhere('bt.user_id=?', array($user->getId()), 'views DESC', FALSE, 10);
$resp->assignRef('tagged_threads', $bthreads);
$this->setLayout('dashboard_tagged_threads_quick.html');
}
//--------------------------------------------
// View saved tag groups.
//--------------------------------------------
function tag_groups(&$reg, &$req, &$resp)
{
$this->checkLogin($reg, $req, $resp);
$user = &$reg->get('user');
$finder = &$this->getFinder('tag_groups');
$groups = &$finder->findAllBy('user_id', $user->getId());
$resp->assign('in_tag_groups', 1);
$resp->assignRef('tag_groups', $groups);
$resp->assign('content', 'dashboard_tag_groups.html');
$this->setPage("My Tag Groups");
$this->setLayout('forum_base.html');
}
//--------------------------------------------
// Get the tag groups but as a drop down.
//--------------------------------------------
function tag_groups_quick(&$reg, &$req, &$resp)
{
$this->checkXHR();
$this->checkLogin($reg, $req, $resp);
$user = &$reg->get('user');
$finder = &$this->getFinder('tag_groups');
$groups = &$finder->findAllWhere('user_id=?', array($user->getId()), FALSE, FALSE, 10);
$resp->assignRef('tag_groups', $groups);
$this->setLayout('dashboard_tag_groups_quick.html');
}
//--------------------------------------------
// Save a tag group.
//--------------------------------------------
function save_tag_group(&$reg, &$req, &$resp)
{
$this->checkLogin($reg, $req, $resp);
$user = &$reg->get('user');
$info = &new InformationDisplay($reg, $req, $resp);
$error = 'You must specify at least two existing tags in order to create a tag group.';
//--------------------------------------------
// Get and clean the tags that will go in this tag
// group.
//--------------------------------------------
// uses spaces because they are passed through the URL
$temp = preg_split("~(\s| )~", $req->get('tags'), -1, PREG_SPLIT_NO_EMPTY);
$tags = clean_tags($temp);
$num_tags = count($tags);
if($num_tags < 2)
{
$info->text($error);
}
//--------------------------------------------
// Try to get all of the tags that exist out of
// the ones specified.
//--------------------------------------------
$finder = &$this->getFinder('tags');
$tags = &$finder->findAllWhere("LOWER(ts.tag_name) IN(". substr(str_repeat('?,', $num_tags), 0, -1) .")", $tags);
//--------------------------------------------
// Whoops! None of the specified tags exist.
//--------------------------------------------
if($tags === NULL || $tags->numRows() < 2)
{
$info->text($error);
}
//--------------------------------------------
// Create the tag group.
//--------------------------------------------
$finder = &$this->getFinder('tag_groups');
$group = &$finder->createRecord();
$group->set('user_id', $user->getId());
$group->setTags($tags);
$group->save();
//--------------------------------------------
// Update the user.
//--------------------------------------------
$finder = &$this->getFinder('users');
$finder->updateWhere("num_tag_groups=num_tag_groups+1", "user_id=?", array($user->getId()));
//--------------------------------------------
// We're done!
//--------------------------------------------
$info->text("Successfully created the tag group!", '/dashboard/tag_groups/', 3);
}
//--------------------------------------------
// Untag a thread.
//--------------------------------------------
function untag(&$reg, &$req, &$resp)
{
$this->checkLogin($reg, $req, $resp);
$user = &$reg->get('user');
$thread_id = intval($req->get('thread_id'));
//--------------------------------------------
// Delete the bookmark.
//--------------------------------------------
$finder = &$this->getFinder('bookmarked_threads');
$finder->deleteWhere("user_id=? AND thread_id=?", array($user->getId(), $thread_id));
//--------------------------------------------
// Delete the tags.
//--------------------------------------------
$finder = &$this->getFinder('thread_user_tags');
$finder->deleteWhere("user_id=? AND thread_id=?", array($user->getId(), $thread_id));
//--------------------------------------------
// Update the user. (using the finder)
//--------------------------------------------
$finder = &$this->getFinder('users');
$finder->updateWhere("num_tagged_threads=num_tagged_threads-1", "user_id=?", array($user->getId()));
//--------------------------------------------
// Redirect to the previous page :D
//--------------------------------------------
$this->redirect($this->popReferer());
}
//--------------------------------------------
// Remove a tag group.
//--------------------------------------------
function ungroup(&$reg, &$req, &$resp)
{
$this->checkLogin($reg, $req, $resp);
$user = &$reg->get('user');
$info = &new InformationDisplay($reg, $req, $resp);
//--------------------------------------------
// Get tag group.
//--------------------------------------------
$finder = &$this->getFinder('tag_groups');
//$finder->deleteWhere("group_id=? AND user_id=?", array($group_id, $user->getId()));
$group = &$finder->find($req->get('group_id'));
if($group === NULL || $group->get('user_id') != $user->getId())
{
$info->text("The tag group that you're trying to remove doesn't exist.");
}
//--------------------------------------------
// Remove the tags from the group.
//--------------------------------------------
$finder = &$this->getFinder('group_tags');
$finder->deleteWhere("group_id=?", array($group->getId()));
//--------------------------------------------
// Remove the tag group.
//--------------------------------------------
$group->delete();
//--------------------------------------------
// Update the user. (using the finder)
//--------------------------------------------
$finder = &$this->getFinder('users');
$finder->updateWhere("num_tag_groups=num_tag_groups-1", "user_id=?", array($user->getId()));
//--------------------------------------------
// Redirect to the previous page :D
//--------------------------------------------
$this->redirect($this->popReferer());
}
}
?>