public function test_term_tree() { // create a vocabulary. if (Vocabulary::get('format_test')) { Vocabulary::get('format_test')->delete(); } $v = Vocabulary::create(array('name' => 'format_test', 'description' => "Vocabulary used for testing Format::term_tree()", 'features' => array('hierarchical'))); // nest some terms. /** * A * | \ * B C * | | \ * D E F // E has no descendants! * | \ | \ * G H I J // G has no descendants! * / \ \ \ * K L M N **/ $a = $v->add_term("A"); $b = $v->add_term("B", $v->get_term($a->id)); $c = $v->add_term("C", $v->get_term($a->id)); $d = $v->add_term("D", $v->get_term($b->id)); $e = $v->add_term("E", $v->get_term($c->id)); $f = $v->add_term("F", $v->get_term($c->id)); $g = $v->add_term("G", $v->get_term($d->id)); $h = $v->add_term("H", $v->get_term($d->id)); $i = $v->add_term("I", $v->get_term($f->id)); $j = $v->add_term("J", $v->get_term($f->id)); $k = $v->add_term("K", $v->get_term($h->id)); $l = $v->add_term("L", $v->get_term($h->id)); $m = $v->add_term("M", $v->get_term($i->id)); $n = $v->add_term("N", $v->get_term($j->id)); $tree_output = Format::term_tree($v->get_tree(), 'test', array('itemstart' => '<li>', 'itemattr' => '', 'liststart' => '<ol>', 'wrapper' => '%s')); $expected_output = <<<END <ol class="tree" id="tree_test"><li>A<ol><li>B<ol><li>D<ol><li>G</li> <li>H<ol><li>K</li> <li>L</li> </ol></li> </ol></li> </ol></li> <li>C<ol><li>E</li> <li>F<ol><li>I<ol><li>M</li> </ol></li> <li>J<ol><li>N</li> </ol></li> </ol></li> </ol></li> </ol></li> </ol> END; $result = $tree_output === $expected_output; if (!$result) { $this->output(sprintf('<strong>Expected:</strong><br><textarea rows="16">%s</textarea><br><strong>Got:</strong><br><textarea rows="16">%s</textarea>', $expected_output, $tree_output)); } $this->assert_true($result, "Output does not match desired HTML"); // clean up $v->delete(); }
/** * Returns the tags vocabulary * * @return Vocabulary The tags vocabulary */ public static function vocabulary() { return Vocabulary::get(self::$vocabulary); }
/** * Remove a version if it's owner or a privileged person requested to do so **/ public function theme_route_remove_addon_version($theme, $params) { $theme->post = Post::get(array('content_type' => Post::type('addon'), 'slug' => $params['slug'])); // Check if the current user has access to this addon. $theme->permitted_versions = $this->addon_permitted_versions($theme->post); $vocab = Vocabulary::get(self::CATALOG_VOCABULARY); $term = $vocab->get_term($params['version']); if ($term && in_array($term->term, $theme->permitted_versions)) { $term->delete(); } $remaining = $vocab->get_object_terms('addon', $theme->post->id); if (count($remaining) == 0) { // No versions left on this addon, so remove it entirely $theme->post->delete(); // We should propably redirect to the $type overview page instead Utils::redirect(Site::get_url('habari')); } // Redirect so the displayed page uses the updated version list Utils::redirect($theme->post->permalink); }
/** * Rename a Vocabulary. * @return boolean true if the Vocabulary was renamed, false otherwise **/ public static function rename($name, $newname) { $vocab = Vocabulary::get($name); $vocab->name = $newname; $result = $vocab->update(); return $result; }
public function validate_newvocab($value, $control, $form) { if (isset($form->oldname) && $form->oldname->value && $value == $form->oldname->value) { return array(); } if (Vocabulary::get($value) instanceof Vocabulary) { return array(_t('Please choose a vocabulary name that does not already exist.', 'termmenus')); } return array(); }
/** * Renames a category * If the master category exists, the categories will be merged with it. * If not, it will be created first. * * Adapted from Tags::rename() * * @param mixed tag The category text, slug or id to be renamed * @param mixed master The category to which it should be renamed, or the slug, text or id of it **/ public static function rename($master, $category, $object_type = 'post') { $vocabulary = Vocabulary::get(self::$vocabulary); $type_id = Vocabulary::object_type_id($object_type); // get the term to be renamed $term = $vocabulary->get_term($category); // get the master term $master_term = $vocabulary->get_term($master); // check if it already exists if (!isset($master_term->term)) { // it didn't exist, so we assume it's text and create it $term->term_display = $master; $term->term = $master; $term->update(); // that's it, we're done. EventLog::log(_t('Category %s has been renamed to %s.', array($category, $master), 'simplecategories'), 'info', 'category', 'simplecategories'); } else { if (!$master_term->is_descendant_of($term)) { $posts = array(); $posts = Posts::get(array('vocabulary' => array('any' => array($term), 'not' => array($master_term)), 'nolimit' => true)); // categorize all the $category Posts as $master foreach ($posts as $post) { // $vocabulary->set_object_terms( 'post', $post->id, $master ); $master_term->associate('post', $post->id); } // move the old $term's children over to $master_term foreach ($term->children() as $child) { // is this needed? // $child = $vocabulary->get_term( $child->id ); $vocabulary->move_term($child, $master_term); } // delete the old $term and all its associations self::delete_category($term->id); EventLog::log(_t('Category %s has been merged into %s.', array($category, $master), 'simplecategories'), 'info', 'category', 'simplecategories'); } else { Session::notice(_t('Cannot merge %1$s into %2$s, since %2$s is a descendant of %1$s', array($term, $master), 'shelves')); } } }
/** * Get posts by vocabulary * - vocabulary => an array describing parameters related to vocabularies attached to posts. This can be one of two forms: * - object-based, in which an array of Term objects are passed * - any => posts associated with any of the terms are returned * - all => posts associated with all of the terms are returned * - not => posts associated with none of the terms are returned * - property-based, in which an array of vocabulary names and associated fields are passed * - vocabulary_name:term => a vocabulary name and term slug pair or array of vocabulary name and term slug pairs, any of which can be associated with the posts * - vocabulary_name:term_display => a vocabulary name and term display pair or array of vocabulary name and term display pairs, any of which can be associated with the posts * - vocabulary_name:not:term => a vocabulary name and term slug pair or array of vocabulary name and term slug pairs, none of which can be associated with the posts * - vocabulary_name:not:term_display => a vocabulary name and term display pair or array of vocabulary name and term display pairs, none of which can be associated with the posts * - vocabulary_name:all:term => a vocabulary name and term slug pair or array of vocabulary name and term slug pairs, all of which must be associated with the posts * - vocabulary_name:all:term_display => a vocabulary name and term display pair or array of vocabulary name and term display pairs, all of which must be associated with the posts */ public function test_get_posts_by_vocabulary() { // setup // create a couple Vocabularies and Terms if (Vocabulary::get("fizz")) { Vocabulary::get("fizz")->delete(); } $fizz = Vocabulary::create(array('name' => 'fizz', 'description' => 'Vocabulary for Posts testing.', 'features' => array('free'))); $fizz_term = new Term(array('term' => 'fizz', 'term_display' => 'Fizz')); $fizz->add_term($fizz_term); $extra_fizz_term = new Term(array('term' => 'extra fizzy', 'term_display' => 'Extra Fizzy')); $fizz->add_term($extra_fizz_term); if (Vocabulary::get("buzz")) { Vocabulary::get("buzz")->delete(); } $buzz = Vocabulary::create(array('name' => 'buzz', 'description' => 'Another Vocabulary for Posts testing.', 'features' => array('free'))); $buzz_term = new Term(array('term' => 'buzz', 'term_display' => 'Buzz')); $buzz->add_term($buzz_term); // create some Posts and associate them with the two Vocabularies for ($i = 1; $i <= 20; $i++) { $post = Post::create(array('title' => "Test Post {$i}", 'content' => 'If this were really a post...', 'user_id' => $this->user->id, 'status' => Post::status('published'), 'content_type' => Post::type('entry'), 'pubdate' => DateTime::date_create(time()))); $post->info->testing_vocab = 1; $post->info->i = $i; $post->info->commit(); if ($i % 3 === 0) { $fizz->set_object_terms('post', $post->id, array($fizz_term->term)); } if ($i % 5 === 0) { $buzz->set_object_terms('post', $post->id, array($buzz_term->term)); } } // Object-based syntax $total_posts = Posts::count_total(); $any_vocab_posts = Posts::get(array('ignore_permissions' => true, 'vocabulary' => array("any" => array($fizz_term, $buzz_term)), 'nolimit' => 1, 'count' => 'DISTINCT {posts}.id')); $all_vocab_posts = Posts::get(array('ignore_permissions' => true, 'vocabulary' => array("all" => array($fizz_term, $buzz_term)), 'nolimit' => 1, 'count' => 'DISTINCT {posts}.id')); $not_vocab_posts = Posts::get(array('ignore_permissions' => true, 'vocabulary' => array("not" => array($fizz_term, $buzz_term)), 'nolimit' => 1, 'count' => 'DISTINCT {posts}.id')); $this->assert_true($any_vocab_posts > $all_vocab_posts, "Any: {$any_vocab_posts} should be greater than All: {$all_vocab_posts}"); $this->assert_true($not_vocab_posts > $all_vocab_posts, "Not: {$not_vocab_posts} should be greater than All: {$all_vocab_posts}"); $this->assert_true($not_vocab_posts < $total_posts, "Not: {$not_vocab_posts} should be less than Total: {$total_posts}"); $this->assert_equal($any_vocab_posts + $not_vocab_posts, $total_posts, "Any: {$any_vocab_posts} plus Not: {$not_vocab_posts} should equal Total: {$total_posts}"); // Property-based syntax $any_vocab_posts = Posts::get(array('ignore_permissions' => true, 'vocabulary' => array("fizz:term" => "fizz", "buzz:term" => "buzz"), 'nolimit' => 1, 'count' => 'DISTINCT {posts}.id')); $all_vocab_posts = Posts::get(array('ignore_permissions' => true, 'vocabulary' => array("fizz:all:term" => "fizz", "buzz:all:term" => "buzz"), 'nolimit' => 1, 'count' => 'DISTINCT {posts}.id')); $not_vocab_posts = Posts::get(array('ignore_permissions' => true, 'vocabulary' => array("fizz:not:term" => "fizz", "buzz:not:term" => "buzz"), 'nolimit' => 1, 'count' => 'DISTINCT {posts}.id')); $this->assert_true($any_vocab_posts > $all_vocab_posts, "Any: {$any_vocab_posts} should be greater than All: {$all_vocab_posts}"); $this->assert_true($not_vocab_posts > $all_vocab_posts, "Not: {$not_vocab_posts} should be greater than All: {$all_vocab_posts}"); $this->assert_true($not_vocab_posts < $total_posts, "Not: {$not_vocab_posts} should be less than Total: {$total_posts}"); $this->assert_equal($any_vocab_posts + $not_vocab_posts, $total_posts, "Any: {$any_vocab_posts} plus Not: {$not_vocab_posts} should equal Total: {$total_posts}"); // teardown Posts::get(array('ignore_permissions' => true, 'has:info' => 'testing_vocab', 'nolimit' => 1))->delete(); $fizz->delete(); $buzz->delete(); }
public function test_object_terms() { $post = Post::create( array( 'title' => 'Unit Test Post', 'content' => 'This is a unit test post to test setting and getting terms.', 'user_id' => 1, 'status' => Post::status( 'draft' ), 'content_type' => Post::type( 'entry' ), 'pubdate' => HabariDateTime::date_create(), ) ); $v = Vocabulary::get( 'tags' ); // Test setting terms with strings $new_terms = array( 'habari', 'unit test' ); $v->set_object_terms( 'post', $post->id, $new_terms ); $terms = $v->get_object_terms( 'post', $post->id ); $t = array(); foreach( $terms as $term ) { $t[] = (string)$term; } $this->assert_equal( 2, count( $terms ) ); $this->assert_equal( 0, count( array_diff( $new_terms, $t ) ) ); // Test get_all_object_terms $nv = Vocabulary::create( array( 'name' => 'animals', 'description' => 'Types of animals.', 'features' => array( 'hierarchical' ) ) ); $root = $nv->add_term( 'Animal Kingdom' ); $nv->set_object_terms( 'post', $post->id, array( $root ) ); $terms = Vocabulary::get_all_object_terms( 'post', $post->id ); $new_terms[] = 'Animal Kingdom'; $t = array(); foreach( $terms as $term ) { $t[] = (string)$term; } $this->assert_equal( 3, count( $terms ) ); Utils::debug( 3, count( $terms ) ); $this->assert_equal( 0, count( array_diff( $new_terms, $t ) ) ); Utils::debug( 0, count( array_diff( $new_terms, $t ) ) ); $v->delete_term( 'unit test' ); $post->delete(); $nv->delete(); }
public function action_block_content_category_archives($block, $theme) { $categories = array(); $v = Vocabulary::get('categories'); if ($v) { $results = $v->get_tree(); if (count($results) > 0) { /* must we? Shouldn't the foreach just fail gracefully? */ foreach ($results as $result) { $count = ''; if ($block->show_counts) { $count = Posts::get(array('tag_slug' => $result->term, 'count' => 'term')); } $url = URL::get('display_entries_by_category', array('category_slug' => $result->term)); $categories[] = array('category' => $result->term_display, 'count' => $count, 'url' => $url); } } $block->categories = $categories; } }
public static function save_version($post = null, $versions = array()) { if (isset($post) && count($versions) !== 0) { $vocabulary = Vocabulary::get(self::$vocabulary); $extant_terms = $vocabulary->get_associations($post->id, 'addon'); foreach ($versions as $key => $version) { $term_display = "{$post->id} {$key} {$post->info->repo_url}"; $found = false; foreach ($extant_terms as $eterm) { if ($eterm->term_display == $term_display) { // This is super-cheesy! $found = true; $term = $eterm; break; } } if (!$found) { $term = new Term(array('term_display' => $post->id . " {$key}")); } foreach ($version as $field => $value) { $term->info->{$field} = $value; } if ($found) { $term->update(); } else { $vocabulary->add_term($term); $term->associate('addon', $post->id); } } } else { // post didn't work or there was no version. } }
/** * Handler thread creation form */ public function filter_handle_thread_reply($output, $form, $thread, $forum) { if (!self::has_permission('reply', $thread)) { return _t('<p>You are not authorized to reply to this thread.</p>'); } $postdata = array('user_id' => User::identify()->id, 'pubdate' => HabariDateTime::date_create(), 'content_type' => Post::type('reply'), 'content' => $form->content->value, 'status' => Post::status('published')); $reply = new Post($postdata); $reply->insert(); if (self::$anonymity && $form->anonymous->value == TRUE) { $reply->info->anonymous = TRUE; } // Do vocab stuff $vocab = Vocabulary::get(self::$vocab); $parent_term = $vocab->get_term($thread->slug); $reply_term = $vocab->add_term($reply->slug, $parent_term); $reply->update(); Utils::redirect($reply->permalink); return '...'; }
private static function subpage_stub($term) { if (is_string($term)) { $term = Vocabulary::get(self::$vocabulary)->get_term($term); } if (null == $term) { return false; } $ancestors = $term->ancestors(); $stub_parts = array(); foreach ($ancestors as $ancestor) { $stub_parts[] = $ancestor->term; } $stub_parts[] = $term->term; return implode('/', $stub_parts); }
// Utils::debug( $_GET ); } ?> <div class="container"> <?php echo $form; ?> </div> <div class="container plugins activeplugins"> <?php // this should be in the plugin, not on this page. $all_shelves = array(); $all_shelves = Vocabulary::get('shelves')->get_tree(); // Utils::debug( $all_shelves ); if (count($all_shelves) > 0) { $right = array(); foreach ($all_shelves as $shelf) { while (count($right) > 0 && $right[count($right) - 1] < $shelf->mptt_right) { array_pop($right); } $pad = count($right) * 30; $titlelink = sprintf('<a href="%s" title="%s">%s</a>', URL::get('admin', array('page' => 'posts', 'search' => Options::get('shelves__single', _t('shelf', 'shelves')) . ':' . $shelf->term)), _t("Manage content categorized '%s'", array($shelf->term_display), 'shelves'), $shelf->term_display); $dogs_eat_cats = _t('Contains %d posts.', array(Posts::get(array('vocabulary' => array('shelves:term' => $shelf->term), 'count' => 'term'))), 'shelves'); // debugging $titlelink .= "<h4>{$shelf->mptt_left} :: {$shelf->mptt_right}</h4>"; $dropbutton = '<ul class="dropbutton"><li><a href="' . URL::get('admin', array('page' => 'shelves', 'action' => 'edit', 'shelf' => $shelf->term)) . '" title="' . _t("Rename or move '{$shelf->term_display}'") . '">' . _t("Edit") . '</a></li><li><a href="' . URL::get('admin', array('page' => 'shelves', 'action' => 'delete', 'shelf' => $shelf->term)) . '" title="' . _t("Delete '{$shelf->term_display}'") . '">' . _t("Delete") . '</a></li></ul>'; echo "\n<div class='item plugin clear' style='border-left: {$pad}px solid #e9e9e9; border-color:#e9e9e9;'><div class='head'>"; echo "\n{$titlelink} {$dropbutton}\n</div><p>{$dogs_eat_cats}</p></div>";
/** * function delete_shelf * Deletes an existing shelf and all relations to it. **/ public static function delete_shelf($shelf = '') { $vocabulary = Vocabulary::get(self::$vocabulary); // should there be a Plugins::act( 'shelf_delete_before' ...? $term = $vocabulary->get_term($shelf); if (!$term) { return false; // no match for shelf } $result = $vocabulary->delete_term($term); if ($result) { EventLog::log(sprintf(_t('%1$s \'%2$s\' deleted.'), array(Options::get('shelves__singule', _t('shelf', 'shelves')), $shelf)), 'info', 'content', 'shelves'); } // should there be a Plugins::act( 'shelf_delete_after' ...? return $result; }