'/:(int)category_id/:category_name', ); } //-------------------------------------------- // We're viewing all of the categories or one single // category. //-------------------------------------------- function categories(&$reg, &$req, &$resp) { $user = &$reg->get('user'); $info = &new InformationDisplay($reg, $req, $resp); //-------------------------------------------- // Make sure that this user even has permission // to view categories! //-------------------------------------------- if(!$user->canDo('view', 'categories')) { $info->display('permsRequired'); } //-------------------------------------------- // So, we are looking at all of the categories. //-------------------------------------------- $finder = &$this->getFinder('categories'); if(!$req->get('category_id') || intval($req->get('category_id')) == 0) { //-------------------------------------------- // If this user has permission to add a category, // show the form to do it. //-------------------------------------------- if($user->canDo('add', 'categories')) { $resp->assign('category_create_form', 'category_create_form.html'); } //-------------------------------------------- // Get the categories. //-------------------------------------------- $categories = &$finder->findAll('name ASC'); $resp->assignRef('categories', $categories); //-------------------------------------------- // Finish off the usual template junk. //-------------------------------------------- $resp->assign('content', 'categories.html'); $this->setPage('Categories'); } //-------------------------------------------- // We're looking at a single category. //-------------------------------------------- else { //-------------------------------------------- // Get the category. //-------------------------------------------- $category = &$finder->find($req->get('category_id')); if($category === NULL) { $info->text("The selected category does not exist!"); } //-------------------------------------------- // Get this categorie's tags into a usable array. //-------------------------------------------- $tag_names = array(); $ctags = &$category->getTags(); while($ctags->next()) { $tag_names[] = &$ctags->current(); } //-------------------------------------------- // Now the following is VERY REDUNDANT.. as it is // basically just straight from the tags controller... // oh well. //-------------------------------------------- $tag_sql_where = "IN(". substr(str_repeat('?,', count($tag_names)), 0, -1) .")"; //-------------------------------------------- // Get the tags. //-------------------------------------------- $finder = &$this->getFinder('tags'); $tags = &$finder->findAllWhere("ts.tag_name ". $tag_sql_where, $tag_names); //-------------------------------------------- // Whoops! None of the tags we're looking for exist! //-------------------------------------------- if($tags === NULL) { $info->text("We're sorry but none of the tags you are looking for seem to exist."); } //-------------------------------------------- // Loop through the tags that we found and collect // their names and ids into an array so we can find // threads that use these tags. //-------------------------------------------- $tag_names = array(); $tag_ids = array(); while($tags->next()) { $tag = &$tags->current(); if(!in_array($tag->getId(), $tag_ids)) { $tag_names[] = $tag->get('tag_name'); $tag_ids[] = $tag->getId(); } } //-------------------------------------------- // Get the threads, same way as the tags do. //-------------------------------------------- $finder = &$this->getFinder('threads'); $finder->addTempFrom("k4_thread_tags tt2"); $threads = &$finder->findAllWhere("t.deleted=0 AND tt.thread_id = tt2.thread_id AND tt2.tag_id $tag_sql_where", $tag_ids, 't.created DESC', 'tt2.tag_id'); //-------------------------------------------- // Set some session info for the category relationships. //-------------------------------------------- $session = &$reg->get('session'); $session->set('category_id', $category->getId()); $session->set('tag_ids', $tag_ids); //-------------------------------------------- // Template.. //-------------------------------------------- $resp->assignRef('thread_list', $threads); $resp->assignRef('category', $category); $resp->assign('content', 'categories_single.html'); } $this->setLayout('forum_base.html'); } function relationships(&$reg, &$req, &$resp) { $session = &$reg->get('session'); $category_id = intval($session->get('category_id')); if($category_id > 0) { $tag_ids = array_values($session->get('tag_ids')); //-------------------------------------------- // Unset the session values. The reason I use // session info is so that // a) you NEED to be in the category page to get // the XML. // b) I didn't want to have to do any weird passing // of info through urls and such or extra queries. //-------------------------------------------- $session->set('category_id', $false = FALSE); $session->set('tag_ids', $array = array()); //-------------------------------------------- // Get similar categories //-------------------------------------------- $sql = "SELECT DISTINCT c.category_id, c.name, COUNT(ct2.tag_id) AS tag_count FROM k4_categories c, k4_category_tags ct1 JOIN k4_category_tags ct2 USING (category_id) WHERE ct1.tag_id $tag_sql_where AND c.category_id = ct2.category_id GROUP BY c.category_id ORDER BY tag_count DESC LIMIT 0, 15"; $dba = &$reg->get('dba'); $results = &$dba->executeQuery($sql, $tag_ids); $xml = ""; while($results->next()) { $category = &$results->current(); $xml .= ''; } $xml .= ""; $results->free(); $xml = htmlentities($xml); $resp->assign('content', $xml); $this->setLayout('blank.html'); } else { $this->redirect('/'); } } //-------------------------------------------- // Save a category. //-------------------------------------------- function _save(&$reg, &$req, &$resp, $edit = FALSE, $quick = FALSE) { $user = &$reg->get('user'); $info = &new InformationDisplay($reg, $req, $resp); //-------------------------------------------- // Check the referring page. //-------------------------------------------- if($this->popReferer() == '/') { $info->display('permsRequired'); } $finder = &$this->getFinder('categories'); //-------------------------------------------- // Simple formatting before more extensive checking // on the request variables. //-------------------------------------------- $errors = array(); $name = htmlentities(trim($req->get('name')), ENT_QUOTES, K4_CHARSET); $tags = trim($req->get('tags')); if(!$edit && strlen_utf($name) < 3) { $errors[] = 'name'; } if(strlen_utf($tags) < 3) { $errors[] = 'tags'; } //-------------------------------------------- // If we're adding a category that already exists, // see if the user has permission to edit it. //-------------------------------------------- $category = NULL; if(!$edit) { //-------------------------------------------- // Does a category with this name exist? //-------------------------------------------- $category = &$finder->findBy('name', $name); if($category !== NULL) { //-------------------------------------------- // This user does not have permission to edit // this category. //-------------------------------------------- if(!$user->canDo('edit', 'categories')) { $info->display('permsRequired'); } //-------------------------------------------- // Put us into edit mode. //-------------------------------------------- $edit = TRUE; } } //-------------------------------------------- // We're creating this category. //-------------------------------------------- if(!$edit) { //-------------------------------------------- // Whoops! We don't have permission to add // categories. Oh well. //-------------------------------------------- if(!$user->canDo('add', 'categories')) { $info->display('permsRequired'); } //-------------------------------------------- // Let's just make sure this person isn't impulse // adding categories. //-------------------------------------------- $session = &$reg->get('session'); if($session->get('added_categories') >= 5) { $info->text("Woah, you've added a lot of categories today. You're on a roll."); } //-------------------------------------------- // Add the category. //-------------------------------------------- $category = &$finder->createRecord(); $category->set('name', $name); $category->set('user_id', $user->getId()); $category->setTags($tags); } //-------------------------------------------- // We're editing this category. //-------------------------------------------- else { //-------------------------------------------- // Whoops! We don't have permission to edit // categories. Mooooooooooo. //-------------------------------------------- if(!$user->canDo('edit', 'categories')) { $info->display('permsRequired'); } //-------------------------------------------- // $category might be set if we're creating a // category that already exists.. hence editing // it. //-------------------------------------------- if($category === NULL) { $category = &$finder->find($req->get('category_id')); if($category === NULL) { $info->text("The category that you're trying to edit doesn't exist!"); } } //-------------------------------------------- // Loop through the existing tags and add them // onto the tags in the request variable. //-------------------------------------------- $existing_tags = &$category->getTags(); while($existing_tags->next()) { $tags .= ','. $existing_tags->current(); } $category->setEdit(); $category->setTags($tags); $category->set('name', $name); } //-------------------------------------------- // Okay.. this is a bit of an odd and redundant // way of making it past the save point. //-------------------------------------------- if(empty($errors)) { $category->save(); } //-------------------------------------------- // now, for some reason, PHP does not like me using // empty($category->getInvalid())... WTF!? //-------------------------------------------- if(empty($errors) && count($category->getInvalid()) == 0) { if(!$edit) { //-------------------------------------------- // increment the number of categories added. //-------------------------------------------- $num_categories = intval($session->get('added_categories'))+1; $session->set('added_categories', $num_categories); //-------------------------------------------- // Redirect the user to their newly created category! //-------------------------------------------- $path = '/categories/'. $category->getId() .'/'. k4_us($name) .'/'; $info->text("Successfully added the category $name.", $path, 3); } else { //-------------------------------------------- // Redirect the user to the category they just updated. //-------------------------------------------- $path = '/categories/'. $category->getId() .'/'. k4_us($name) .'/'; $info->text("Successfully updated the category ". $category->get('name') .".", $path, 3); } } else { //-------------------------------------------- // Yahtzee! Some errors occured.. time to go back. //-------------------------------------------- $resp->assign('category', $req->getArray(FA_REQUEST_POST)); $resp->assign('error_msg', TRUE); // Show the error message foreach ($category->getInvalid() as $invalid) { $resp->setError($invalid); } foreach ($errors as $field) { $resp->setError($field); } $this->index($reg, $req, $resp); } } //-------------------------------------------- // Let's create a category. //-------------------------------------------- function create(&$reg, &$req, &$resp) { $this->_save($reg, $req, $resp, FALSE, FALSE); } } ?>